-
Notifications
You must be signed in to change notification settings - Fork 8
/
cr_methods.py
191 lines (152 loc) · 7.29 KB
/
cr_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import bitstring
import hmac
import hashlib
from Crypto.Cipher import AES
def pad_out(sequence, target_bit_length):
output = sequence.copy()
padding_size_needed = target_bit_length - output.len
output.append(bitstring.BitString(bin='0'*padding_size_needed))
return output
from Crypto.Cipher import AES
def encrypt(cipher, clear_sequence):
response_sequence = bitstring.BitString(cipher.encrypt(clear_sequence.tobytes()))
return response_sequence
def decrypt(cipher, cipher_sequence):
response_sequence = bitstring.BitString(cipher.decrypt(cipher_sequence.tobytes()))
return response_sequence
def aes_ecb(key_sequence):
return AES.new(key_sequence.tobytes(), AES.MODE_ECB)
def aes_cbc(key_sequence):
return AES.new(key_sequence.tobytes(), AES.MODE_CBC, IV=bitstring.BitString(bin='0'*128).tobytes())
def aes_ctr(key_sequence):
def trivial():
return bitstring.BitString(bin='0'*128).tobytes()
return AES.new(key_sequence.tobytes(), AES.MODE_CTR, counter=trivial)
################################################################################
# Password Preparations
def trivial(sequence):
return sequence
def pad_password(password_sequence):
return pad_out(password_sequence, 128)
def md5_password(password_sequence):
return bitstring.BitString(hex=hashlib.md5(password_sequence.tobytes()).hexdigest())
def ssl_password(password_sequence):
return bitstring.BitString(hex=hashlib.sha256(password_sequence.tobytes()).hexdigest())[:128]
def pkcs(password_sequence):
BS = 16
password_bytes = password_sequence.tobytes()
remaining = BS - len(password_bytes) % BS
pad = bytes( [remaining] * remaining)
result = bytearray()
result.extend(password_bytes)
result.extend(pad)
return result
#################################################################################
# Message Responders
def get_cipher_message_responder(cipher_operation, cipher):
def cipher_message_responder(key_sequence, challenge_sequence):
if key_sequence.len != 128 or challenge_sequence.len != 128:
raise ValueError("only 128bit sequences supported. %d, %d" % (key_sequence.len, challenge_sequence.len))
return cipher_operation(cipher(key_sequence), challenge_sequence)
return cipher_message_responder
def hmacmd5(key, message):
return bitstring.BitString(hex=hmac.new(key.tobytes(), message.tobytes(), digestmod=hashlib.md5).hexdigest())
def md5concat(password_sequence, challenge_sequence):
input_sequence = challenge_sequence.copy()
input_sequence.append(password_sequence)
return bitstring.BitString(hex=hashlib.md5(input_sequence.tobytes()).hexdigest())
def xor(password_sequence, challenge_sequence):
response_sequence = challenge_sequence ^ password_sequence
return response_sequence
from Crypto.Hash import CMAC
def aes_cmac(password_sequence, challenge_sequence):
cobj = CMAC.new(password_sequence.tobytes(), ciphermod=AES)
cobj.update(challenge_sequence.tobytes())
return bitstring.BitString(hex=cobj.hexdigest())
##################################################################################
# Message Responder Modifiers
def get_trivial_responder(message_responder):
return message_responder
def get_rev_responder(message_responder):
def rev_responder(password_sequence, challenge_sequence):
challenge_sequence = challenge_sequence.copy()
challenge_sequence.reverse()
response_sequence = message_responder(password_sequence, challenge_sequence)
response_sequence = response_sequence[:128]
response_sequence.reverse()
response_sequence.overwrite(response_sequence, 0)
return response_sequence
return rev_responder
def get_swp_responder(message_responder):
def swp_responder(password_sequence, challenge_sequence):
response_sequence = message_responder(challenge_sequence[:128], password_sequence)
return response_sequence
return swp_responder
def get_swprev_responder(message_responder):
def swprev_responder(password_sequence, challenge_sequence):
challenge_sequence = challenge_sequence.copy()
challenge_sequence.reverse()
response_sequence = message_responder(challenge_sequence[:128], password_sequence)
response_sequence = response_sequence[:128]
response_sequence.reverse()
response_sequence.overwrite(response_sequence, 0)
return response_sequence
return swprev_responder
def bitswap(input_sequence, size=8):
input_sequence = input_sequence.copy()
for pos in range(0, input_sequence.len, size):
byte_sequence = input_sequence[pos:pos + size]
byte_sequence.reverse()
input_sequence.overwrite(byte_sequence, pos)
return input_sequence
def get_bitswapped_responder(message_responder):
def bitswapped_responder(password_sequence, challenge_sequence):
challenge_sequence = bitswap(challenge_sequence)
response_sequence = message_responder(password_sequence, challenge_sequence[:128])
response_sequence.overwrite(bitswap(response_sequence[:128]),0)
return response_sequence
return bitswapped_responder
def get_rev_bitswapped_responder(message_responder):
def rev_bitswapped_responder(password_sequence, challenge_sequence):
bitswapped_responder = get_bitswapped_responder(message_responder)
challenge_sequence = challenge_sequence.copy()
challenge_sequence.reverse()
response_sequence = bitswapped_responder(password_sequence, challenge_sequence)
response_sequence.reverse()
response_sequence.overwrite(response_sequence, 0)
return response_sequence
return rev_bitswapped_responder
def get_wordbitswapped_responder(message_responder):
def wordbitswapped_responder(password_sequence, challenge_sequence):
challenge_sequence = bitswap(challenge_sequence, size=16)
response_sequence = message_responder(password_sequence, challenge_sequence[:128])
response_sequence.overwrite(bitswap(response_sequence[:128], size=16),0)
return response_sequence
return wordbitswapped_responder
def get_rev_wordbitswapped_responder(message_responder):
def rev_wordbitswapped_responder(password_sequence, challenge_sequence):
bitswapped_responder = get_wordbitswapped_responder(message_responder)
challenge_sequence = challenge_sequence.copy()
challenge_sequence.reverse()
response_sequence = bitswapped_responder(password_sequence, challenge_sequence)
response_sequence.reverse()
response_sequence.overwrite(response_sequence, 0)
return response_sequence
return rev_wordbitswapped_responder
def get_longbitswapped_responder(message_responder):
def longbitswapped_responder(password_sequence, challenge_sequence):
challenge_sequence = bitswap(challenge_sequence, size=32)
response_sequence = message_responder(password_sequence, challenge_sequence[:128])
response_sequence.overwrite(bitswap(response_sequence[:128], size=32),0)
return response_sequence
return longbitswapped_responder
def get_rev_longbitswapped_responder(message_responder):
def rev_longbitswapped_responder(password_sequence, challenge_sequence):
bitswapped_responder = get_longbitswapped_responder(message_responder)
challenge_sequence = challenge_sequence.copy()
challenge_sequence.reverse()
response_sequence = bitswapped_responder(password_sequence, challenge_sequence)
response_sequence.reverse()
response_sequence.overwrite(response_sequence, 0)
return response_sequence
return rev_longbitswapped_responder