forked from c3c/E-Safenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esafenet.py
355 lines (300 loc) · 11.8 KB
/
esafenet.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# E-Safenet encryption/decryption suite.
# Copyright (C) 2014 Jan Laan, Cedric Van Bockhaven
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file LICENSE. if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import sys
import os
import random
import simplelzo1x #Self-created safenet-specific module, see simplelzo1x folder.
import struct
import random
import cPickle
import collections
import argparse
import partial_c
import partial_binary
"""
Esafenet: A class to perform encryption/decryption operations on E-Safenet files.
Jan Laan, Cedric Van Bockhaven; 2014
Created for a Research project, Master System and Network Engineering, University of Amsterdam.
This class enables you to encrypt or decrypt files and folders with the E-Safenet algorithm.
It also allows you to generate (not truly) random keys, and find keys using a known-plaintext attack.
"""
class Esafenet:
"""
Encrypt a given text, with the key given.
Returns the encrypted text.
"""
@staticmethod
def encrypt_file(text, key):
compressed_bytes = simplelzo1x.compress(text[:512])
compressed_len = len(compressed_bytes)
#3 bytes act as checksum for the second 512 bytes of the message. The 1 is static.
checksum = sum(ord(a) for a in text[512:1024]) | 1 << 24
padding_end = (512 - compressed_len)
header = "b" + '\x14' + "#" + "e" + struct.pack('<h', padding_end) + struct.pack('<h', compressed_len) + struct.pack('<I',checksum) + "E-SafeNet" + '\x00\x00\x00' + "LOCK" + '\x00'*(padding_end-28)
return header + Esafenet.__xor_with_key(compressed_bytes, key) + Esafenet.__xor_with_key(text[512:], key)
"""
Encrypt an entire folder at the given location, with the key and store it on disk
"""
@staticmethod
def encrypt_folder(folder, key, dest_folder):
if not os.path.isdir(dest_folder):
os.mkdir(dest_folder)
for root, dirs, files in os.walk(folder):
nr = root.replace(folder, "")
if nr:
nr += "/"
for d in dirs:
if not os.path.isdir(dest_folder + "/" + nr + d):
os.mkdir(dest_folder + "/" + nr + d)
for f in files:
fl = open(root + "/" + f, "rb")
enc = Esafenet.encrypt_file(fl.read(), key)
with open(dest_folder + "/" + nr + f, "wb") as fh:
fh.write(enc)
"""
Decrypt a given text, with the key.
Returns the file's text
"""
@staticmethod
def decrypt_file(text, key):
plain = ""
offset = ord(text[4]) | ord(text[5]) << 8 #offset is stored in these 2 bytes in little-endian order.
decr_header = Esafenet.__xor_with_key(text[offset:512], key)
# plain_header = ""
plain_header = simplelzo1x.decompress(decr_header)
plain_file = Esafenet.__xor_with_key(text[512:], key)
return plain_header + plain_file
"""
Xor any text with a given key.
The key should be an array of byte values.
An Esafenet key has a length of 512 bytes.
"""
@staticmethod
def __xor_with_key(text, key):
xored = ""
for idx, c in enumerate(text):
keyb = key[idx % len(key)]
keyb = 0 if keyb is None else keyb
xored += chr(ord(c) ^ keyb)
return xored
"""
Decrypt all files in an entire folder at the given location, with the key and store it on disk
"""
@staticmethod
def decrypt_folder(folder, key, dest_folder):
if not os.path.isdir(dest_folder):
os.mkdir(dest_folder)
for root, dirs, files in os.walk(folder):
nr = root.replace(folder, "")
if nr:
nr += "/"
for d in dirs:
if not os.path.isdir(dest_folder + "/" + nr + d):
os.mkdir(dest_folder + "/" + nr + d)
for f in files:
fl = open(root + "/" + f, "rb")
dec = Esafenet.decrypt_file(fl.read(), key)
with open(dest_folder + "/" + nr + f, "wb") as fh:
fh.write(dec)
"""
Perform a known-plaintext attack. Obtain a key given an encrypted file and its plaintext counterpart.
The file should be at least 1.5 KB for this to work (1536 bytes)
Returns the found key
"""
@staticmethod
def find_key(crypted_text, plain_text, key_len=512):
if len(plain_text) < 512:
raise EsafenetException("This plaintext attack requires at least 1536 bytes (1.5 KB) of data to work")
text = plain_text[512:1024] #uses this part of the text for plaintext finding. (The first 512 bytes will be compressed, so unusable)
compare = plain_text[1024:1536] #todo: better comparison (min 512 bytes)
start = 0
while(start < len(crypted_text)):
j = 0
plain = ""
key = []
for i in range(start, len(crypted_text)):
if(j < key_len):
key.append(ord(text[j]) ^ ord(crypted_text[i]))
else:
plain = plain + chr((ord(crypted_text[i]) ^ key[j % key_len]))
j += 1
if compare in plain:
key = collections.deque(key)
key.rotate(start % key_len) #The key is always returned for offset 0
return key
else:
start += 1
return None #if we haven't returned by the end of the file, no key was found
"""
Stores a given key on disk
"""
@staticmethod
def store_key(key, dest_filename):
with open(dest_filename, "wb") as fh:
cPickle.dump(key, fh)
"""
Generate a (not really) random Esafenet key, and store it on disk
This function generates an array of 512 random bytes, and stores it.
"""
@staticmethod
def generate_key():
key = []
random.seed()
for i in range(512):
key.append(random.randint(0, 255))
return key
@staticmethod
def binary_find_key(text):
return partial_binary.find_binary_key(text)
@staticmethod
def text_find_key(infolder, language, outfile):
partial_c.process_parallel(infolder, language, outfile)
#We even have our own exception!
class EsafenetException(Exception):
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Esafenet encryption/decryption/key generation")
parser.add_argument('a', metavar='action', type=str, help='Action to perform', choices = ['encrypt', 'decrypt', 'encrypt_folder', 'decrypt_folder', 'keygen', 'findkey', 'pattern_decrypt'])
parser.add_argument('--infile', type=argparse.FileType('rb'), help='Input file', required=False)
parser.add_argument('--key', type=argparse.FileType('rb'), help='Key file', required=False)
parser.add_argument('--outfile', type=argparse.FileType('wb'), help='Output file', required=False, default=sys.stdout)
parser.add_argument('--infolder', type=str, help='Input folder', required=False)
parser.add_argument('--outfolder', type=str, help='Output folder', required=False)
parser.add_argument('--comp_file', type=argparse.FileType('rb'), help='Plaintext comparison file used by findkey', required=False)
parser.add_argument('--type', metavar='pattern_type', type=str, help='Type for pattern decrypt (binary or text)', choices = ['binary', 'text'], required=False)
parser.add_argument('--language', metavar='text_pattern_language', type=str, help='Language for text pattern decrypt (C, PHP or CS)', choices = ['C', 'PHP', 'CS'], required=False)
args = parser.parse_args()
if args.a == 'keygen':
if args.outfile == None:
parser.print_usage()
print "error: outfile is required for the keygen action"
sys.exit(1)
cPickle.dump(Esafenet.generate_key(), args.outfile)
elif args.a == 'encrypt':
if args.infile == None:
parser.print_usage()
print "error: infile is required for the encrypt action"
sys.exit(1)
if args.outfile == None:
parser.print_usage()
print "error: outfile is required for the encrypt action"
sys.exit(1)
if args.key == None:
parser.print_usage()
print "error: keyfile is required for the encrypt action"
sys.exit(1)
f = Esafenet.encrypt_file(args.infile.read(), cPickle.load(args.key))
args.outfile.write(f)
if args.outfile.name != '<stdout>':
print "Encryption: %d bytes written to %s" % (len(f), args.outfile.name)
elif args.a == 'encrypt_folder':
if args.infolder == None or not os.path.isdir(args.infolder):
parser.print_usage()
print "error: infolder is required for the encrypt_folder action"
sys.exit(1)
if args.outfolder == None:
parser.print_usage()
print "error: outfolder is required for the encrypt_folder action"
sys.exit(1)
if args.key == None:
parser.print_usage()
print "error: keyfile is required for the encrypt_folder action"
sys.exit(1)
Esafenet.encrypt_folder(args.infolder, cPickle.load(args.key), args.outfolder)
print "Folder encryption: all written to %s" % args.outfolder
elif args.a == 'decrypt':
if args.infile == None:
parser.print_usage()
print "error: infile is required for the decrypt action"
sys.exit(1)
if args.outfile == None:
parser.print_usage()
print "error: outfile is required for the decrypt action"
sys.exit(1)
if args.key == None:
parser.print_usage()
print "error: keyfile is required for the decrypt action"
sys.exit(1)
f = Esafenet.decrypt_file(args.infile.read(), cPickle.load(args.key))
args.outfile.write(f)
if args.outfile.name != '<stdout>':
print "Decryption: %d bytes written to %s" % (len(f), args.outfile.name)
elif args.a == 'decrypt_folder':
if args.infolder == None or not os.path.isdir(args.infolder):
parser.print_usage()
print "error: infolder is required for the decrypt_folder action"
sys.exit(1)
if args.outfolder == None:
parser.print_usage()
print "error: outfolder is required for the decrypt_folder action"
sys.exit(1)
if args.key == None:
parser.print_usage()
print "error: keyfile is required for the decrypt_folder action"
sys.exit(1)
Esafenet.decrypt_folder(args.infolder, cPickle.load(args.key), args.outfolder)
print "Folder decryption: all written to %s" % args.outfolder
elif args.a == 'findkey':
if args.infile == None:
parser.print_usage()
print "error: infile is required for the find_key action"
sys.exit(1)
if args.comp_file == None:
parser.print_usage()
print "error: comp_file is required for the find_key action"
sys.exit(1)
if args.outfile == None:
parser.print_usage()
print "error: outfile is required for the decrypt_folder action"
sys.exit(1)
key = Esafenet.find_key(args.infile.read(), args.comp_file.read())
cPickle.dump(key, args.outfile)
if args.outfile.name != '<stdout>':
print "Success: key written to %s" % args.outfile.name
elif args.a == 'pattern_decrypt':
if args.type == None:
parser.print_usage()
print "error: pattern type is required for the pattern_decrypt action"
sys.exit(1)
if args.outfile == None:
parser.print_usage()
print "error: outfile is required for the pattern_decrypt action (for storing the key)"
sys.exit(1)
if args.type == 'binary':
if args.infile == None:
parser.print_usage()
print "error: infile is required for the pattern_decrypt binary action"
sys.exit(1)
key = Esafenet.binary_find_key(args.infile.read())
cPickle.dump(key, args.outfile)
if args.outfile.name != '<stdout>':
print "Decryption: key written to %s (%s 0-bytes)" % (args.outfile.name, key.count(0))
elif args.type == 'text':
if args.infolder == None:
parser.print_usage()
print "error: infolder is required for the pattern_decrypt text action"
sys.exit(1)
if args.outfolder == None:
parser.print_usage()
print "error: outfolder is required for the pattern_decrypt text action"
sys.exit(1)
if args.language == None:
parser.print_usage()
print "error: language is required for the pattern_decrypt text action"
sys.exit(1)
key = Esafenet.text_find_key(args.infolder, args.language, args.outfile)