Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added crypto class and cypher suites #1722

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
775ad35
added crypto class and cypher suites
sebmichel1983 Nov 22, 2020
cee1f88
added changelog entries
sebmichel1983 Nov 22, 2020
6568bc3
changed key sanitization request
sebmichel1983 Nov 22, 2020
bed84e1
fixed typo in vignere cipher
sebmichel1983 Nov 22, 2020
66bfeb0
fixed typo in vigenere
sebmichel1983 Nov 22, 2020
a371a6b
Removed 2.7
lukas-mertens Nov 22, 2020
6f33f39
Merge pull request #1 from lukas-mertens/dev
sebmichel1983 Nov 22, 2020
b49a08b
Addded 2.7 back in
lukas-mertens Nov 22, 2020
ba80385
Fixed lint
lukas-mertens Nov 22, 2020
c44a9d0
Added init
lukas-mertens Nov 22, 2020
40dae62
Removed self
lukas-mertens Nov 22, 2020
be96050
Reverted self
lukas-mertens Nov 22, 2020
b891849
changed code_table name
sebmichel1983 Nov 22, 2020
b30c82c
Code_table fix
lukas-mertens Nov 22, 2020
3edd060
Merge branch 'dev' of https://github.com/sebmichel1983/pwntools into dev
sebmichel1983 Nov 22, 2020
6a7cb55
Merge branch 'dev' into dev
lukas-mertens Nov 22, 2020
47a3b0f
Merge pull request #2 from lukas-mertens/dev
sebmichel1983 Nov 22, 2020
5b2acda
renamed cypher classes
sebmichel1983 Nov 25, 2020
415376f
Merge branch 'dev' of https://github.com/sebmichel1983/pwntools into dev
sebmichel1983 Nov 25, 2020
e2df120
renamed cipher classes
sebmichel1983 Nov 25, 2020
7c2fe96
changed type check to isinstance in vignere cipher
sebmichel1983 Nov 25, 2020
9526faf
changed class names to cipher_classname
sebmichel1983 Nov 25, 2020
3a91799
renamed crypto class in init
sebmichel1983 Nov 25, 2020
1f2a31b
normalized naming convention
sebmichel1983 Nov 25, 2020
b3c71af
fixed typo in vigenere cipher
sebmichel1983 Nov 25, 2020
c824825
changed if conditions to PEP8 standard
sebmichel1983 Nov 26, 2020
17e3ca3
test
procrash Dec 15, 2020
4be7c6f
cleanup
procrash Dec 15, 2020
1d3e5b5
Update
procrash Dec 15, 2020
febf259
Changed init.py
procrash Dec 15, 2020
789a710
added test.py to pwnlib folder
sebmichel1983 Dec 15, 2020
1de0165
moved test file
sebmichel1983 Dec 15, 2020
69d45ca
Not working
procrash Dec 15, 2020
7a42efd
changed CHANGELOG.md and index.rst in docs
sebmichel1983 Dec 15, 2020
5b09060
Latest changes
procrash Dec 15, 2020
f5710f9
Merge branch 'dev' of https://github.com/sebmichel1983/pwntools into dev
procrash Dec 15, 2020
ee5e32a
added more cipher suites and _notes.md
sebmichel1983 Dec 15, 2020
fa14ff0
Merge branch 'dev' into dev
Arusekk Apr 19, 2022
eb5579b
Merge branch 'dev' into dev
peace-maker Jun 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ The table below shows which release corresponds to each branch, and what date th
## 4.12.0 (`dev`)
- [#2202][2202] Fix `remote` and `listen` in sagemath
- [#2117][2117] Add -p (--prefix) and -s (--separator) arguments to `hex` command
- [#1722][1722] Added Crypto class and cipher suites

[2202]: https://github.com/Gallopsled/pwntools/pull/2202
[2117]: https://github.com/Gallopsled/pwntools/pull/2117
[1722]: https://github.com/Gallopsled/pwntools/pull/1722

## 4.11.0 (`beta`)

Expand Down Expand Up @@ -127,6 +129,7 @@ In memoriam — [Zach Riggle][zach] — long time contributor and maintainer of
- [#2123][2123] Fix ROP without a writeable cache directory
- [#2124][2124] Fix `tube.recvpred()` timeout argument


[1975]: https://github.com/Gallopsled/pwntools/pull/1975
[1979]: https://github.com/Gallopsled/pwntools/pull/1979
[2011]: https://github.com/Gallopsled/pwntools/pull/2011
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Each of the ``pwntools`` modules is documented here.
constants
config
context
crypto
dynelf
encoders
elf/*
Expand Down
1 change: 1 addition & 0 deletions pwnlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'asm',
'atexception',
'atexit',
'crypto',
'commandline',
'constants',
'context',
Expand Down
60 changes: 60 additions & 0 deletions pwnlib/crypto/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from __future__ import absolute_import

from pwnlib.crypto.autokey import cipher_autokey
from pwnlib.crypto.cipher_reverse import cipher_reverse
from pwnlib.crypto.xor import cipher_xor
from pwnlib.crypto.vigenere import cipher_vigenere
from pwnlib.crypto.transposition import cipher_transposition
from pwnlib.crypto.rot13 import cipher_rot13
from pwnlib.crypto.morse import cipher_morse
from pwnlib.crypto.hex import cipher_hex
from pwnlib.crypto.decimal import cipher_decimal
from pwnlib.crypto.caesar import cipher_caesar
from pwnlib.crypto.binary import cipher_binary
from pwnlib.crypto.base64 import cipher_base64
from pwnlib.crypto.bacon import cipher_bacon
from pwnlib.crypto.atbash import cipher_atbash
from pwnlib.crypto.affine import cipher_affine
from pwnlib.crypto.porta import cipher_porta
from pwnlib.crypto.caesar_progressive import cipher_caesar_progressive
from pwnlib.crypto.beaufort import cipher_beaufort
from pwnlib.crypto.chao import cipher_chao
from pwnlib.crypto.columnar_transposition import cipher_columnar_transposition
from pwnlib.crypto.gronsfeld import cipher_gronsfeld
from pwnlib.crypto.keyword import cipher_keyword
from pwnlib.crypto.myszkowski_transposition import cipher_myszkowski_transposition
from pwnlib.crypto.substitution import cipher_substitution
from pwnlib.crypto.trifid import cipher_trifid
from pwnlib.crypto.vic import cipher_vic
from pwnlib.crypto.zigzag import cipher_zigzag


__all__ = [
'cipher_reverse',
'cipher_xor',
'cipher_vigenere',
'cipher_transposition',
'cipher_rot13',
'cipher_morse',
'cipher_hex',
'cipher_decimal',
'cipher_caesar',
'cipher_binary',
'cipher_base64',
'cipher_bacon',
'cipher_atbash',
'cipher_autokey',
'cipher_porta',
'cipher_affine',
'cipher_caesar_progressive',
'cipher_beaufort',
'cipher_chao',
'cipher_columnar_transposition',
'cipher_gronsfeld',
'cipher_keyword',
'cipher_myszkowski_transposition',
'cipher_substitution',
'cipher_trifid',
'cipher_vic',
'cipher_zigzag'
]
2 changes: 2 additions & 0 deletions pwnlib/crypto/_notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/tigertv/secretpy/tree/master/secretpy/ciphers
https://github.com/lukaszbanasiak/python-ciphers
38 changes: 38 additions & 0 deletions pwnlib/crypto/affine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# based on: https://github.com/tigertv/secretpy/blob/master/secretpy/ciphers/affine.py

class cipher_affine:
def process(self, alphabet, key, text, isEncrypt):
a = key[0]
b = key[1]
ans = ""
aInverse = self.__getInverse(a, alphabet)

try:
for char in text:
if isEncrypt == 1:
alphI = (alphabet.index(char) * a + b) % len(alphabet)
else:
alphI = (aInverse * (alphabet.index(char) - b)) % len(alphabet)
enc = alphabet[alphI]
ans += enc

except ValueError:
wrchar = char.encode('utf-8')
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")

return ans


def __getInverse(self, a, alphabet):
for i in range(1, len(alphabet)):
if ((int(a)*int(i)) % int(len(alphabet))) == 1:
return i
return 0


def encrypt(self, text, key, alphabet=u"abcdefghijklmnopqrstuvwxyz"):
return self.process(alphabet, key, text, 1)


def decrypt(self, text, key, alphabet=u"abcdefghijklmnopqrstuvwxyz"):
return self.process(alphabet, key, text, -1)
29 changes: 29 additions & 0 deletions pwnlib/crypto/atbash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import string

class cipher_atbash:
def __init__(self):
self.alphabet = list(string.ascii_uppercase)


def encrypt(self, clear):
return self.process(clear)


def decrypt(self, cipher):
return self.process(cipher)


def process(self, text):
reverse_alphabet = list(reversed(self.alphabet))
code_dictionary = dict(zip(self.alphabet, reverse_alphabet))

chars = list(text.upper())
result = ""

for char in chars:
if char in code_dictionary:
result += code_dictionary.get(char)
else:
result += char

return result
35 changes: 35 additions & 0 deletions pwnlib/crypto/autokey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# based on: https://github.com/tigertv/secretpy/blob/master/secretpy/ciphers/autokey.py

class cipher_autokey:
def process(self, alphabet, key, text, isEncrypt):
ans = ""
for i in range(len(text)):
m = text[i]
if i < len(key):
k = key[i]
else:
if isEncrypt == 1:
k = text[i - len(key)]
else:
k = ans[i - len(key)]
try:
alphI = alphabet.index(m)
except ValueError:
wrchar = m.encode('utf-8')
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
try:
alphI += isEncrypt * alphabet.index(k)
except ValueError:
wrchar = k.encode('utf-8')
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
alphI = alphI % len(alphabet)
enc = alphabet[alphI]
ans += enc
return ans


def encrypt(self, text, key, alphabet=u"abcdefghijklmnopqrstuvwxyz"):
return self.process(alphabet, key, text, 1)

def decrypt(self, text, key, alphabet=u"abcdefghijklmnopqrstuvwxyz"):
return self.process(alphabet, key, text, -1)
39 changes: 39 additions & 0 deletions pwnlib/crypto/bacon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import re

class cipher_bacon:
def __init__(self):
self.code_table = self.generate_code_table()


def generate_code_table(self):
bacon_dict = {}

for i in range(0, 26):
tmp = bin(i)[2:].zfill(5)
tmp = tmp.replace('0', 'a')
tmp = tmp.replace('1', 'b')
bacon_dict[tmp] = chr(65 + i)

return bacon_dict


def encrypt(self, cleartext):
cipher = ''
bacon_dict = {v: k for k, v in self.code_table.items()} # hack to get key from value - reverse dict
#cleartext = normalize('NFKD', cleartext).encode('ascii', 'ignore') # replace national characters to ASCII equivalents
cleartext = cleartext.upper()
cleartext = re.sub(r'[^A-Z]+', '', cleartext)

for i in cleartext:
cipher += bacon_dict.get(i).upper()
return cipher


def decrypt(self, ciphertext):
cleartext = ''
ciphertext = ciphertext.lower()
ciphertext = re.sub(r'[^ab]+', '', ciphertext)

for i in range(0, int(len(ciphertext) / 5)):
cleartext += self.code_table.get(ciphertext[i * 5:i * 5 + 5], ' ')
return cleartext
9 changes: 9 additions & 0 deletions pwnlib/crypto/base64.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import base64

class cipher_base64:
def encrypt(self, cleartext):
return base64.b64encode(cleartext.encode('utf-8')).decode()


def decrypt(self, ciphertext):
return base64.b64decode(ciphertext.encode('utf-8')).decode()
29 changes: 29 additions & 0 deletions pwnlib/crypto/beaufort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# based on: https://github.com/tigertv/secretpy/blob/master/secretpy/ciphers/beaufort.py

class cipher_beaufort:
def process(self, alphabet, key, text):
ans = ""
for i in range(len(text)):
char = text[i]
keychar = key[i % len(key)]
try:
alphIndex = alphabet.index(keychar)
except ValueError:
wrchar = keychar.encode('utf-8')
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
try:
alphIndex -= alphabet.index(char)
except ValueError:
wrchar = char.encode('utf-8')
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
alphIndex %= len(alphabet)
ans += alphabet[alphIndex]
return ans


def encrypt(self, text, key, alphabet=u"abcdefghijklmnopqrstuvwxyz"):
return self.process(alphabet, key, text)


def decrypt(self, text, key, alphabet=u"abcdefghijklmnopqrstuvwxyz"):
return self.process(alphabet, key, text)
10 changes: 10 additions & 0 deletions pwnlib/crypto/binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import binascii

class cipher_binary:
def encrypt(self, data):
return bin(int(binascii.hexlify(data.encode('utf-8')),16))


def decrypt(self, data):
n = int(data, 2)
return binascii.unhexlify('%x' % n).decode()
28 changes: 28 additions & 0 deletions pwnlib/crypto/caesar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import string

class cipher_caesar:
def __init__(self):
self.alphabet = string.ascii_lowercase + string.ascii_uppercase


def encrypt(self, data, key, mode):
return self.process(data, key, mode)


def decrypt(self, data, key, mode):
return self.process(data, key, mode)


def process(self, text, key, mode):
result = ''

for char in text:
index = self.alphabet.find(char)
if index == -1:
result += char
else:
new_index = index + key if mode == 1 else index - key
new_index %= len(self.alphabet)
result += self.alphabet[new_index:new_index+1]

return result
29 changes: 29 additions & 0 deletions pwnlib/crypto/caesar_progressive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# based on: https://github.com/tigertv/secretpy/blob/master/secretpy/ciphers/caesar_progressive.py

from pwnlib.crypto.helpers.alphabets import *

class cipher_caesar_progressive:
__alphabet = al.ENGLISH

def process(self, alphabet, key, text, isEncrypt):
alphabet = alphabet or self.__alphabet
ans = ""
for i, char in enumerate(text):
try:
alphIndex = alphabet.index(char)
except ValueError as e:
wrchar = char.encode('utf-8')
e.args = (
"Can't find char '" + wrchar + "' of text in alphabet!",)
raise
alphIndex = (alphIndex + isEncrypt * (key + i)) % len(alphabet)
ans += alphabet[alphIndex]
return ans


def encrypt(self, text, key, alphabet=None):
return self.process(alphabet, key, text, 1)


def decrypt(self, text, key, alphabet=None):
return self.process(alphabet, key, text, -1)
39 changes: 39 additions & 0 deletions pwnlib/crypto/chao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# based on: https://github.com/tigertv/secretpy/blob/master/secretpy/ciphers/chao.py

class cipher_chao:

def process(self, text, isEncrypt, tp_alphabet, tc_alphabet):
ret = ''
for c in text:
try:
if isEncrypt:
i = tp_alphabet.index(c)
ret += tc_alphabet[i]
else:
i = tc_alphabet.index(c)
ret += tp_alphabet[i]
except ValueError:
wrchar = c.encode('utf-8')
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
tc_alphabet = self.permuteAlphabet(tc_alphabet, i, True)
tp_alphabet = self.permuteAlphabet(tp_alphabet, i, False)
return ret


def encrypt(self, text, key, alphabet=None):
return self.process(text, True, alphabet, key)


def decrypt(self, text, key, alphabet=None):
return self.process(text, False, alphabet, key)


def permuteAlphabet(self, alphabet, i, isCrypt):
alphabet = alphabet[i:] + alphabet[:i]
nadir = len(alphabet) / 2
if isCrypt:
alphabet = alphabet[0] + alphabet[2:int(nadir)+1] + alphabet[1] + alphabet[int(nadir)+1:]
else:
alphabet = alphabet[1:] + alphabet[0]
alphabet = alphabet[:2] + alphabet[3:int(nadir)+1] + alphabet[2] + alphabet[int(nadir)+1:]
return alphabet
Loading
Loading