-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc4Cryptanalysis.py
More file actions
332 lines (259 loc) · 11.3 KB
/
rc4Cryptanalysis.py
File metadata and controls
332 lines (259 loc) · 11.3 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import pickle
import os.path
import numpy as np
from Crypto.Cipher import ARC4
from Crypto import Random
from math import log
import matplotlib.pyplot as plt
KEY_SIZE = 16
TOP_PASSWORDS = 100000
ENCRYPTIONS_SAMPLE_PICKLE = 'encryptedPws.p'
SINGLE_PW_PICKLE = 'singlePw.p'
BYTE_DIST_PICKLE = 'byteDist.p'
KEYSTREAM_DIST_PICKLE = 'keystream.p'
TLS_FINISH = 'TLS Finished: password='
rand = Random.new()
def save(filename, data):
print('Pickling to {}'.format(filename))
outfile = open(filename, 'wb')
pickle.dump(data, outfile)
outfile.close()
def load(filename):
print('Loading {}'.format(filename))
inputFile = open(filename, 'rb')
data = pickle.load(inputFile)
inputFile.close()
return data
def x_or(byteArrOne, byteArrTwo):
return bytearray(
[byteOne ^ byteTwo for byteOne, byteTwo
in zip(byteArrOne, byteArrTwo)])
def generateKeystreamDist(password, count, keyReuses, forceReload=False):
pickledFile = '{}_{}'.format(password, KEYSTREAM_DIST_PICKLE)
if not forceReload and os.path.isfile(pickledFile):
keystreamDist = load(pickledFile)
else:
print('Regenerating keystream distribution')
byteSize = 256
message = TLS_FINISH + password
messageLen = len(message)
messageBytes = bytearray(bytes(message))
step = int(count / 10)
curStep = step
keystreamDist = np.zeros(shape=(messageLen, byteSize), dtype=int)
for keyCount in range(count):
key = rand.read(KEY_SIZE)
rc4 = ARC4.new(key)
if keyCount == curStep:
print('{}% done'.format(100 * curStep / count))
curStep += step
for keyReuse in range(keyReuses):
encryptedMsg = bytearray(bytes(rc4.encrypt(message)))
keystream = x_or(encryptedMsg, messageBytes)
for position, byte in enumerate(keystream):
keystreamDist[position][byte] += 1
save(pickledFile, keystreamDist)
return keystreamDist
def getKeystreamProbabilityDist(keystreamDist):
totalByteCounts = np.sum(keystreamDist, axis=1)
keystreamProbs = np.divide(keystreamDist, totalByteCounts[:, None], dtype=float)
return keystreamProbs
def graphKeystreamDistByBytePos(keystreamDist, pos):
plt.plot(keystreamDist[pos, :])
plt.title('Keystream Byte Frequencies for CT Position {}'.format(pos))
plt.ylabel('Frequency')
plt.xlabel('Byte Value')
axes = plt.gca()
axes.set_xlim([-5, 270])
plt.show()
def graphKeystreamDistByByteValue(keystreamDist, byteVal):
plt.plot(keystreamDist[:, byteVal])
plt.title('Keystream Byte Frequencies for Byte Value {}'.format(byteVal))
plt.ylabel('Frequency')
plt.xlabel('CT Position')
plt.show()
def _genEncryptionDist(i, message, encryptedPws):
key = rand.read(KEY_SIZE)
rc4 = ARC4.new(key)
encryptedPws[i] = bytearray(bytes(rc4.encrypt(message)))
def generateEncryptionDist(password, count, forceReload=False):
pickledFile = '{}_{}'.format(password, SINGLE_PW_PICKLE)
if not forceReload and os.path.isfile(pickledFile):
print('generateEncryptionDist loading pickled file')
pwEncryptions = load(pickledFile)
else:
print('Regenerating encryption distribution')
message = TLS_FINISH + password
encryptedPws = np.ndarray(shape=(count, len(message)), dtype=np.uint8)
step = int(count / 100)
curStep = step
for i in range(count):
if i == curStep:
print('{}% done'.format(100 * curStep / count))
curStep += step
key = rand.read(KEY_SIZE)
rc4 = ARC4.new(key)
encryptedPws[i] = bytearray(bytes(rc4.encrypt(message)))
pwEncryptions = (encryptedPws, password)
save(pickledFile, pwEncryptions)
return pwEncryptions
# encrypt passwords with randomly generated key
def encryptPassword(password, count):
message = TLS_FINISH + password
encryptedPws = np.ndarray(shape=(count, 2), dtype=object)
for i in range(count):
key = rand.read(KEY_SIZE)
rc4 = ARC4.new(key)
encryptedPws[i][0] = rc4.encrypt(message)
encryptedPws[i][1] = password
return encryptedPws
def getEncryptionSamples(passwords, encryptionCount, forceReload=False):
if not forceReload and os.path.isfile(ENCRYPTIONS_SAMPLE_PICKLE):
print('loading pickled file')
matrix = load(ENCRYPTIONS_SAMPLE_PICKLE)
else:
print('getEncryptionSamples regenerating encryption samples')
samples = []
for pw in passwords:
samples.extend(encryptPassword(pw, encryptionCount))
matrix = np.asmatrix(samples)
save(ENCRYPTIONS_SAMPLE_PICKLE, matrix)
return matrix
# log probabilities of passwords
def computePasswordProbabilities(passwordCounts, totalCount):
probabilities = {}
totalProb = log(totalCount)
for password in passwordCounts:
probabilities[password] = log(passwordCounts[password]) - totalProb
return probabilities
def getPasswordCounts(filename):
probabilities = {}
totalCount = 0
with open(filename, 'r') as pwFile:
for line in pwFile:
splitLine = line.strip().split()
if len(splitLine) >= 2:
count = int(splitLine[0])
totalCount += count
password = splitLine[1]
if password not in probabilities:
probabilities[password] = count
else:
probabilities[password] += count
# don't really care about super uncommon ones now
if count == 1:
break
return probabilities, totalCount
def getSortedPasswords(passwordProbabilities):
return sorted([(pw, passwordProbabilities[pw]) for pw in passwordProbabilities],
key=lambda val: val[1], reverse=True)
def loadByteProbabilities(password, pwEncryptions=None, forceReload=False):
pickledFile = '{}_{}'.format(password, BYTE_DIST_PICKLE)
byteSize = 256
numBytes = len(TLS_FINISH + password)
if not forceReload and os.path.isfile(pickledFile):
frequencies = load(pickledFile)
elif pwEncryptions is not None:
print('loadByteProbabilities regenerating byte frequencies')
frequencies = np.array([np.bincount(pwEncryptions[:, i])
for i in range(pwEncryptions.shape[1])])
# frequencies = np.zeros(shape=(numBytes, byteSize), dtype=np.uint)
#
# for index, encryption in enumerate(pwEncryptions):
# for byteNum, byte in enumerate(encryption):
# frequencies[byteNum][int(byte)] += 1
save(pickledFile, frequencies)
else:
raise ValueError('No pickled byteDist.p found and pwEncryptions is None')
return frequencies
def graphByteProbabilitiesByBytePos(password, frequencies):
numBytes = len(TLS_FINISH + password)
# print('Frequencies: ', frequencies[:5, 0])
for bytePosition in range(numBytes):
plt.title('Byte Probabilities for {}'.format(password))
plt.xlabel('Bytes in CT Position {}'.format(bytePosition))
plt.ylabel('Frequencies')
plt.plot(frequencies[bytePosition, :])
plt.show()
def graphByteFreqsByByteValue(password, frequencies):
byteSize = 256
# print('Frequencies: ', frequencies[:5, 0])
for byte in range(byteSize):
plt.title('Byte Frequencies for {}'.format(password))
plt.xlabel('CT Positions'.format(byte))
plt.ylabel('Frequency of Byte Value {}'.format(byte))
plt.plot(frequencies[:, byte])
plt.show()
def graphEntropyByBytePosition(password, frequencies):
byteSize = 256
messageLen = len(TLS_FINISH + password)
expectedCounts = np.mean(frequencies, axis=1)
total = np.sum(frequencies, axis=1)
print('Expected:', expectedCounts / total)
print('Argmax:', np.argmax(frequencies, axis=1))
def getExpectedByteCounts(frequencies):
return np.mean(frequencies, axis=0)
def graphByteEntropyByByteValue(password, frequencies):
byteSize = 256
messageLen = len(TLS_FINISH + password)
expectedCounts = getExpectedByteCounts(frequencies)
total = np.sum(frequencies, axis=0)
print('Total: ', total)
def singleByteBiasEstimateByte(encryptionByteFreqs, keystreamProbDist):
byteSize = 256
candidateWeights = np.ndarray(shape=(byteSize), dtype=float)
candidateDist = np.ndarray(shape=(byteSize), dtype=int)
for candidateByte in range(byteSize):
for keyCandidate in range(byteSize):
candidateDist[keyCandidate] = encryptionByteFreqs[candidateByte ^ keyCandidate]
candidateWeights[candidateByte] = np.sum(candidateDist * np.log(keystreamProbDist))
return np.argmax(candidateWeights)
def basicPTRecoveryAttack(encryptionByteFreqs, keystreamProbDist):
messageLen = encryptionByteFreqs.shape[0]
decrypted = bytearray([singleByteBiasEstimateByte(encryptionByteFreqs[cipherPos],
keystreamProbDist[cipherPos])
for cipherPos in range(messageLen)])
print('Estimated decryption:', decrypted)
def main():
password = '123456'
# pwCounts, totalPasswords = getPasswordCounts('rockyou-withcount.txt')
# pwProbabilities = computePasswordProbabilities(pwCounts, totalPasswords)
#
# count = 0
# print('Probability of 123456:', pwProbabilities['123456'])
# print('Probability of 12345:', pwProbabilities['12345'])
# print('Probability of password:', pwProbabilities['password'])
# for password in pwProbabilities:
# count += 1
# if count > 10:
# break
# print('Probability of {}: {}'.format(password, pwProbabilities[password]))
# commonPwProbs = getSortedPasswords(pwProbabilities)[:TOP_PASSWORDS]
# mostCommonPws = [pw for (pw, probability) in commonPwProbs]
# print('Most common pws:', mostCommonPws[:10])
#
# # encryptionSamples = getEncryptionSamples(mostCommonPws, 100)
# print('PwProbabilities[{}]: {}'.format(password, pwProbabilities[password]))
mostCommonPwSamples, password = generateEncryptionDist(password, 2 ** 22)
# bincount = np.apply_along_axis(np.bincount, 1, mostCommonPwSamples)
# bincount = np.matrix([np.bincount(mostCommonPwSamples[:, i]) for i in range(mostCommonPwSamples.shape[1])])
# sums = np.sum(bincount, axis=1)
# print('BinCount:', bincount[0])
# print('Sums[0]:', sums[0])
#
# # graphByteProbabilities(password, mostCommonPwSamples)
# byteProbs = loadByteProbabilities(password, mostCommonPwSamples)
byteProbs = loadByteProbabilities(password, mostCommonPwSamples, True)
# print('Counts equal:', np.array_equal(bincount, byteProbs))
# print('ByteProbs:', byteProbs[1])
# graphByteProbabilitiesByBytePos(password, byteProbs)
# graphByteFreqsByByteValue(password, byteProbs)
# graphByteEntropyByByteValue(password, byteProbs)
# graphEntropyByBytePosition(password, byteProbs)
keystreamFreqs = generateKeystreamDist(password, 2 ** 20, 5)
# graphKeystreamDistByBytePos(keystreamFreqs, 1)
# graphKeystreamDistByByteValue(keystreamFreqs, 0)
keystreamProbDist = getKeystreamProbabilityDist(keystreamFreqs)
basicPTRecoveryAttack(byteProbs, keystreamProbDist)
if __name__ == '__main__':
main()