-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataInfo.py
556 lines (547 loc) · 19.1 KB
/
DataInfo.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/python
# coding: utf-8
import hashlib
import logging
from Crypto.Cipher import AES
from Crypto.Util import Counter
from Crypto import Random
from Crypto.PublicKey import RSA
import os
import sys
import argparse
import base64
import getpass
def sysinfo():
global G,Y,B,R,W
system = os.uname() # define the system
if system:
G = '\033[92m' # green
Y = '\033[93m' # yellow
B = '\033[94m' # blue
R = '\033[91m' # red
W = '\033[0m' # white
return G , Y , B , R , W
def banner():
# my banner ;]
print R +""" _____ _ _____ __ """
os.system('sleep 0.2')
print B +""" | __ \ | | |_ _| / _| """
os.system('sleep 0.2')
print Y +""" | | | | __ _| |_ __ _ | | _ __ | |_ ___ """
os.system('sleep 0.2')
print R +""" | | | |/ _` | __/ _` | | | | '_ \| _/ _ \ """
os.system('sleep 0.2')
print B +""" | |__| | (_| | || (_| |_| |_| | | | || (_) |"""
os.system('sleep 0.2')
print B +""" |_____/ \__,_|\__\__,_|_____|_| |_|_| \___/ """
os.system('sleep 0.2')
print R +""" # coded by mahmoudadel - facebook.com/mahmoudadel0x """
print W + """
"""
def parser_error(errmsg):
print("Usage: python " + sys.argv[0] + " [Options] use -h for help")
print("Error: " + errmsg )
sys.exit()
def parse_args():
# parse the arguments
parser = argparse.ArgumentParser(epilog='\tExample: \rpython ' + sys.argv[0] + " -in file -o file -enc aes-256-cfb -r 1024")
parser.error = parser_error
parser._optionals.title = "OPTIONS"
parser.add_argument('-enc','--encrypt', help="encrypt a file with a cipher - Usage = '-enc' $Cipher")
parser.add_argument('-dec','--decrypt', help="decrypt a file with the encryption algorithm")
parser.add_argument('-k','--key', help="key to use for encryption ! NOTE : if no key specify random one will be generated")
parser.add_argument('-pubkeyrsa','--rsa',help="RSA asymmetric crypto algorithm operations to generate just type bit lenth to get PublicKey type pub")
parser.add_argument('-f','--form',help="PublicKeyCrypto export format valid : [PEM , DER ,openssh]")
parser.add_argument('-integrity', '--hash', help="check file integrity with hash functions")
parser.add_argument('-in', '--input', help='imput file')
parser.add_argument('-e','--encode',help="encode file with encoding algorithms")
parser.add_argument('-o', '--output', help='output file')
parser.add_argument('-r','--random',help="simple random data generator",type=int)
parser.add_argument('-d','--decode',help='decode a file')
global hash ,input , output , encode ,decode, random, encrypt, decrypt, keyarg, rsa, args,form # global the args variables
args = parser.parse_args()
return args
def hashfunc():
checkfileexist = os.path.isfile(input)
if checkfileexist==1:
pass
else:
print >> sys.stderr,R + "[!]" + W + "input file is not exist ![+] exiting"
sys.exit()
file = open(input,'r')
file = file.read()
md5 = hashlib.md5(file)
md5 = md5.hexdigest()
sha256 = hashlib.sha256(file)
sha256 = sha256.hexdigest()
sha224 = hashlib.sha224(file)
sha224 = sha224.hexdigest()
sha512 = hashlib.sha512(file)
sha512 = sha512.hexdigest()
sha384 = hashlib.sha384(file)
sha384 = sha384.hexdigest()
sha1 = hashlib.sha1(file)
sha1 = sha1.hexdigest()
if hash:
filename = os.path.basename(input)
if (hash=="sha256"):
print ("file : " + filename + "\n" + """%s
hash is :"""%(R) + sha256)
if output:
f = open(output,'w')
f.write(sha256 + "\n")
if(hash=="sha1"):
print("file : " + filename + "\n" + """%s
hash is : """%(R) + sha1)
if output:
f = open(output,'w')
f.write(sha1 + "\n")
if(hash=="sha224"):
print ("file : " + filename + "\n" + """%s
hash is :"""%(R) + sha224)
if output:
f = open(output,'w')
f.write(sha224 + "\n")
if (hash=="md5"):
print ("file : " + filename + "\n" + """%s
hash is :"""%(R) + md5)
if output:
f = open(output,'w')
f.write(md5 + "\n")
if (hash=="sha512"):
print ("file : " + filename + "\n" + """%s
hash is :"""%(R) + sha512)
if output:
f = open(output,'w')
f.write(sha512 + "\n")
if (hash=="sha384"):
print ("file : " + filename + "\n" + """%s
hash is :"""%(R) + sha384)
if output:
f = open(output,'w')
f.write(sha384 + "\n")
def encodefunc():
checkfileexist = os.path.isfile(input)
if checkfileexist==1:
pass
else:
print >> sys.stderr,R + "[!]" + "file" + G + " " + input + " " + "is not exist" + W + " " + "[!] now exiting"
sys.exit()
file = open(input,'r+w')
fileread = file.read()
if decode:
print "%s%s[+]%s decoding"%(R,G,B)
os.system('sleep 2')
if (os.path.splitext(input)[-1])==".encoded":
print ("file is encoded now decoding")
print R + "[!]" + W + "warning if the file encoding is already unknown the app will corrupt it"
os.system('sleep 2')
if decode=="base64": ##base64 decoding
filereadecoded = base64.b64decode(fileread)
file = open(input,'w')
file.write(filereadecoded + "\n")
newfilename = os.path.splitext(str(input))[0]
os.rename(str(input),str(newfilename))
print "InputFile : " + str(input)
print "Output : " + str(newfilename)
if decode=="base32": ##base32 decoding
filereadecoded = base64.b32decode(fileread)
file = open(input,'w')
file.write(filereadecoded + "\n")
newfilename = os.path.splitext(str(input))[0]
os.rename(str(input),str(newfilename))
print "InputFile : " + str(input)
print "Output : " + str(newfilename)
if decode=="base16": ##base16 decoding
filereadecoded = base64.b16decode(fileread)
file = open(input,'w')
file.write(filereadecoded + "\n")
newfilename = os.path.splitext(str(input))[0]
os.rename(str(input),str(newfilename))
print "InputFile : " + str(input)
print "Output : " + str(newfilename)
else:
print "error file name string didnt end with 'encoded' do you mean --encoding ?"
print R + "[!] exiting"
sys.exit()
if encode:
print ("%s%s[+]%s encoding"%(R,B,G))
os.system('sleep 2')
if encode=="base64": #base 64 encoding
filereadencoded = base64.b64encode(fileread)
file = open(input,'w')
file.write(filereadencoded + "\n")
newfilenamea = input + ".encoded"
newfilename = os.path.basename(newfilenamea)
os.rename(str(input),str(newfilenamea))
print "InputFile : " + str(input)
print "Output : " + str(newfilename)
if encode=="base32": #base 32 encoding
filereadencoded = base64.b32encode(fileread)
file = open(input,'w')
file.write(filereadencoded + "\n")
newfilenamea = input + ".encoded"
newfilename = os.path.basename(str(newfilenamea))
os.rename(str(input),str(newfilenamea))
print "InputFile : " + str(input)
print "Output : " + str(newfilename)
if encode=="base16": # base 16 encoding
filreadencoded = base64.b16encode(fileread)
file = open(input,'w')
file.write(filereadencoded + "\n")
newfilenamea = input + ".encoded"
newfilename = os.path.basename(str(newfilenamea))
os.rename(str(input),str(newfilenamea))
print "InputFile : " + str(input)
print "Output : " + str(newfilename)
file.close()
def pseurand():
os.system('sleep 1') # for style only ^^
ifencode = raw_input("do you want to encode the random bytes with base64 ? " + R + " [y/n] ")
if ifencode=="y": # base64 encode to the random generated data
randomnum = os.urandom(random).encode("base64")
else:
randomnum = os.urandom(random) # ascii encoding to random generated data
if output:
file = open(output,'w')
file.write(randomnum + "") # write random data to file
else:
print B + '[+]' + "Random data is : " + '\n' + W + randomnum
class AESCIPHER():
def __init__(self,**kwargs):
self.arguments = kwargs
global pad,unpad,BS
BS = 16 # block size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
self.arguments["key"]
self.arguments["plaintext"]
self.arguments["ciphertext"]
def encryptCBC(self):
iv = Random.get_random_bytes(16)
plaintext = pad(self.arguments["plaintext"])
key = hashlib.sha256(self.arguments["key"].encode('utf-8')).digest()
encryption = AES.new(key, AES.MODE_CBC, iv)
if encode == "base64":
return base64.b64encode(iv + encryption.encrypt(plaintext))
else:
return (iv + encryption.encrypt(plaintext))
def decryptCBC(self):
if decode == "base64":
ciphertext = base64.b64decode(self.arguments["ciphertext"])
else:
ciphertext = (self.arguments["ciphertext"])
key = hashlib.sha256(self.arguments["key"].encode('utf-8')).digest()
iv = ciphertext[:16]
encryption = AES.new(key, AES.MODE_CBC, iv)
return unpad(encryption.decrypt( ciphertext[16:] ))
def encryptOFB(self):
iv = Random.get_random_bytes(16)
plaintext = pad(self.arguments["plaintext"])
key = hashlib.sha256(self.arguments["key"].encode('utf-8')).digest()
encryption = AES.new(key, AES.MODE_OFB, iv)
if encode == "base64":
return base64.b64encode(iv + encryption.encrypt(plaintext))
else:
return (iv + encryption.encrypt(plaintext))
def decryptOFB(self):
if decode == "base64":
ciphertext = base64.b64decode(self.arguments["ciphertext"])
else:
ciphertext = (self.arguments["ciphertext"])
key = hashlib.sha256(self.arguments["key"].encode('utf-8')).digest()
iv = ciphertext[:16]
encryption = AES.new(key, AES.MODE_OFB, iv)
return unpad(encryption.decrypt( ciphertext[16:] ))
def encryptCFB(self):
iv = Random.get_random_bytes(16)
plaintext = pad(self.arguments["plaintext"])
key = hashlib.sha256(self.arguments["key"].encode('utf-8')).digest()
encryption = AES.new(key, AES.MODE_CFB, iv)
if encode == "base64":
return base64.b64encode(iv + encryption.encrypt(plaintext))
else:
return (iv + encryption.encrypt(plaintext))
def decryptCFB(self):
if decode == "base64":
ciphertext = base64.b64decode(self.arguments["ciphertext"])
else:
ciphertext = (self.arguments["ciphertext"])
key = hashlib.sha256(self.arguments["key"].encode('utf-8')).digest()
iv = ciphertext[:16]
encryption = AES.new(key, AES.MODE_CFB, iv)
return unpad(encryption.decrypt( ciphertext[16:] ))
def crypto(keyarg):
checkfileexist = os.path.isfile(input)
if checkfileexist==1:
pass
else:
print >> sys.stderr,R + '[!]' + "file : " + W + " " + input + " " + "is not exist" + " " + "[!] now exiting"
sys.exit()
file = open(input,'r+w')
fileread = file.read()
if encrypt == 'AES-256-CFB' or encrypt == 'aes-256-cfb':
if keyarg:
pass
else:
keyarg = getpass.getpass(B + "[!]" "Enter AES-CFB encryption password : ")
if keyarg:
pass
else:
keyarg = (Random.get_random_bytes(256)).encode("base64")
keyexportpath = raw_input(B + "[!]" + "Random key generated Enter export path ? :")
if os.path.isfile(keyexportpath)==1:
file = open(keyexportpath, 'wb')
file.write(keyarg)
aes = AESCIPHER(key=keyarg,plaintext=fileread,ciphertext=None).encryptCFB()
print B + "[+]" + W + "encrypting"
os.system('sleep 0.8')
if output:
file = open(output, 'wb')
file.write(aes)
else:
checkwantwrite = raw_input(B + "[?]" + W + "do you want to overwrite input file data :? [y/n]")
if checkwantwrite=='y':
file = open(input, 'w')
file.write(aes)
newfilenamea = input + ".encrypted"
newfilename = os.path.basename(newfilenamea)
os.rename(str(input),str(newfilenamea))
if checkwantwrite=='n':
print B + '[+]' + W + 'encrypted data : ' + '\n' + aes
if decrypt == 'AES-256-CFB' or decrypt == 'aes-256-cfb':
if keyarg:
pass
else:
keyarg = getpass.getpass(B + "[!]" "Enter AES-CFB decryption password : ")
if keyarg:
pass
else:
print R + "[!]" + "Error in key input now exiting"
sys.exit()
aes = AESCIPHER(key=keyarg,ciphertext=fileread,plaintext=None).decryptCFB()
print B + "[+]" + W + "decrypting"
os.system('sleep 0.8')
if output:
file = open(output, 'wb')
file.write(aes)
else:
checkwantwrite = raw_input(B + "[?]" + "do you want to overwrite file data :? " + W + "[y/n]")
if checkwantwrite=='y':
file = open(input, 'w')
file.write(aes)
newfilename = os.path.splitext(str(input))[0]
if os.path.splitext(str(input))[-1] == ".encrypted":
os.rename(str(input),str(newfilename))
if checkwantwrite=='n':
print B + '[+]' + W + 'encrypted data : ' + '\n' + aes
if encrypt == 'AES-256-CBC' or encrypt == 'aes-256-cbc':
if keyarg:
pass
else:
keyarg = getpass.getpass(B + "[!]" "Enter AES-CBC encryption password : ")
if keyarg:
pass
else:
keyarg = (Random.get_random_bytes(256)).encode("base64")
keyexportpath = raw_input(B + "[!]" + "Random key generated Enter export path ? :")
if os.path.isfile(keyexportpath)==1:
file = open(keyexportpath, 'wb')
file.write(keyarg)
aes = AESCIPHER(key=keyarg,plaintext=fileread,ciphertext=None).encryptCBC()
print B + "[+]" + W + "encrypting"
os.system('sleep 0.8')
if output:
file = open(output, 'wb')
file.write(aes)
else:
checkwantwrite = raw_input(B + "[?]" + W + "do you want to overwrite file data :? [y/n]")
if checkwantwrite=='y':
file = open(input, 'w')
file.write(aes)
newfilenamea = input + ".encrypted"
newfilename = os.path.basename(newfilenamea)
os.rename(str(input),str(newfilenamea))
if checkwantwrite=='n':
print B + '[+]' + W + 'encrypted data : ' + '\n' + aes
if decrypt == 'AES-256-CBC' or decrypt == 'aes-256-cbc':
if keyarg:
pass
else:
keyarg = getpass.getpass(B + "[!]" "Enter AES-CBC decryption password : ")
if keyarg:
pass
else:
print R + "[!]" + "Error in key input now exiting"
sys.exit()
aes = AESCIPHER(key=keyarg,ciphertext=fileread,plaintext=None).decryptCBC()
print B + "[+]" + W + "decrypting"
os.system('sleep 0.8')
if output:
file = open(output, 'wb')
file.write(aes)
else:
checkwantwrite = raw_input(B + "[?]" + W + "do you want to overwrite file data :? [y/n]")
if checkwantwrite=='y':
file = open(input, 'w')
file.write(aes)
newfilename = os.path.splitext(str(input))[0]
if os.path.splitext(str(input))[-1] == ".encrypted":
os.rename(str(input),str(newfilename))
if checkwantwrite=='n':
print B + '[+]' + W + 'encrypted data : ' + '\n' + aes
if encrypt == 'AES-256-OFB' or encrypt == 'aes-256-ofb':
if keyarg:
pass
else:
keyarg = getpass.getpass(B + "[!]" "Enter AES-OFB encryption password : ")
if keyarg:
pass
else:
keyarg = (Random.get_random_bytes(256)).encode("base64")
keyexportpath = raw_input(B + "[!]" + "Random key generated Enter export path ? :")
if os.path.isfile(keyexportpath)==1:
file = open(keyexportpath, 'wb')
file.write(keyarg)
aes = AESCIPHER(key=keyarg,plaintext=fileread,ciphertext=None).encryptOFB()
print B + "[+]" + W + "encrypting"
os.system('sleep 0.8')
if output:
file = open(output, 'wb')
file.write(aes)
else:
checkwantwrite = raw_input(B + "[?]" + W + "do you want to overwrite file data :? [y/n]")
if checkwantwrite=='y':
file = open(input, 'w')
file.write(aes)
newfilenamea = input + ".encrypted"
newfilename = os.path.basename(newfilenamea)
os.rename(str(input),str(newfilenamea))
if checkwantwrite=='n':
print B + '[+]' + W + 'encrypted data : ' + '\n' + aes
if decrypt == 'AES-256-OFB' or decrypt == 'aes-256-ofb':
if keyarg:
pass
else:
keyarg = getpass.getpass(B + "[!]" "Enter AES-OFB decryption password : ")
if keyarg:
pass
else:
print R + "[!]" + "Error in key input now exiting"
sys.exit()
aes = AESCIPHER(key=keyarg,ciphertext=fileread,plaintext=None).decryptOFB()
print B + "[+]" + W + "decrypting"
os.system('sleep 0.8')
if output:
file = open(output, 'wb')
file.write(aes)
else:
checkwantwrite = raw_input(B + "[?]" + W + "do you want to overwrite file data :? [y/n]")
if checkwantwrite=='y':
file = open(input, 'w')
file.write(aes)
newfilename = os.path.splitext(str(input))[0]
if os.path.splitext(str(input))[-1] == ".encrypted":
os.rename(str(input),str(newfilename))
if checkwantwrite=='n':
print B + '[+]' + W + 'encrypted data : ' + '\n' + aes
def PublicKeyCrypto(input,output,form,password):
if (rsa == 1024 or 2048 or 4096 or 8192) and (rsa != "pub") :
print B +'[+]' + W + "generating RSA keys" + "\n"
a1 = hex(id(rsa))
print (B + "IO" +"["+a1+"]" + " RSA algorithms" + W + "\n")
key = RSA.generate(rsa)
print B +'[+] ' + W + str(rsa) + " bit lenth " + " RSA keys generated "
if output:
pass
else:
output = raw_input(R + '[!]' + W + "Enter rsa key export path : ")
f = open(output,'w')
if form == "pem" or "der" or "DER" or "PEM":
if form=="PEM":
form == "pem"
if form=="DER":
form == "der"
if not form:
rr = raw_input(R + '[!]' + "Error please specify valid export format " + B + "[pem/der]")
form = rr
if password:
print B + "[+]" + "password specify the key will be encrypted"
if form == "pem" and password:
print B + "[*]" + "writing pem keys to :" + output
f.write(str(key.exportKey('PEM',passphrase=str(password))) + "\n")
if form == "der" and password:
print B + "[*]" + "writing pem keys to :" + output
f.write(str(key.exportKey('DER',passphrase=str(password))) + "\n")
if form == "pem" and not password:
print B + "[*]" + "writing pem keys to :" + output
f.write(str(key.exportKey('PEM')) + "\n")
if form == "der" and not password:
print B + "[*]" + "writing pem keys to :" + output
f.write(str(key.exportKey('DER')) + "\n")
f.close()
os.chmod(output, 0700)
if rsa == "pub":
if input:
pass
else:
input = raw_input("Enter private key file path : ")
f = open(input, 'r')
fread = f.read()
if password:
key = RSA.importKey(externKey=fread,passphrase=str(password))
else:
key = RSA.importKey(externKey=fread)
publickeyvar = key.publickey()
f = open (output, 'w')
print B +'[+] ' + " writing RSA key"
f.write(str(publickeyvar.exportKey('PEM')) + "\n")
# main function
def main():
sysinfo()
if rsa:
banner()
PublicKeyCrypto(input=input,output=output,form=form,password=keyarg)
if encrypt or decrypt:
banner()
crypto(keyarg=keyarg)
if hash and encode and decode:
print >> sys.stderr, R + '[!]' + W + " Error you cant use encoding and hash functions at one time"
sys.exit()
if hash and random :
print >> sys.stderr, R + '[!]' + W + " Error you cant use random and hash functions at one time"
sys.exit()
if random and (encode or decode):
print >> sys.stderr, R + '[!]' + W + " Error you cant use random and encoding functions at one time"
sys.exit()
if random and (encrypt or decrypt):
print >> sys.stderr, R + '[!]' + W + " Error you cant use random and encryption functions at one time"
sys.exit()
if hash and (encrypt or decrypt):
print >> sys.stderr, R + '[!]' + W + " Error you cant use hash and encryption functions at one time"
sys.exit()
if hash:
banner()
hashfunc()
if encode or decode and not encrypt or decrypt:
banner()
encodefunc()
if random:
banner()
pseurand()
if __name__ == '__main__':
parse_args()
hash = args.hash
input = args.input
output = args.output
encode = args.encode
decode = args.decode
random = args.random
encrypt = args.encrypt
decrypt = args.decrypt
keyarg = args.key
rsa = int(args.rsa)
form = args.form
if not output:
output = None
main()