-
Notifications
You must be signed in to change notification settings - Fork 2
/
ultishift.py
543 lines (465 loc) · 22.4 KB
/
ultishift.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
import datetime, hashlib, base64, string, pyDes, os, yaml, shutil, re, sys
from Cryptodome.Random import get_random_bytes
from Cryptodome.Cipher import AES
# declare global variables
path = os.getenv("APPDATA")
version = 'CReader-6'
pathList = [f'{path}/ultishift', f'{path}/ultishift/fileconvert', f'{path}/ultishift/notes', f'{path}/ultishift/fileconvert/encrypted', f'{path}/ultishift/fileconvert/decrypted', f'{path}/ultishift/config']
fileList = [f'{path}/ultishift/config/defaultconfig.yaml']
defaultData = ['"[please_insert_a_excessively_long_key]"', '"[please_insert_a_excessively_long_key]"', '"[please_insert_a_excessively_long_key]"', '11charsnumb', '32randommixedcharacterandnumbers', '"filekey[please_insert_a_excessively_long_key]"', '123456789123456789']
def fileintegrity():
# define success rate
passed = 0
failed = 0
# declare config file contents
configfile = 'class:'\
f'\n - identification: {version}'\
'\nprivate:'\
'\n - privatekey: "[please_insert_a_excessively_long_key]"'\
'\n - randomdata1: "[please_insert_a_excessively_long_key]"'\
'\n - randomdata2: "[please_insert_a_excessively_long_key]"'\
'\ndesencrypt:'\
'\n - desdata1: 11charsnumb'\
'\n - desdata2: 32randommixedcharacterandnumbers'\
'\nfile:'\
'\n - filekey: "filekey[please_insert_a_excessively_long_key]"'\
'\ndiscord:'\
'\n - keyformatting: False'\
'\n - peerid: 123456789123456789'
for path in pathList:
if os.path.exists(path):
passed += 1
else:
failed += 1
os.mkdir(path)
for file in fileList:
if os.path.isfile(file):
passed += 1
else:
failed += 1
with open(file,'w') as data:
data.write(configfile)
data.close()
# create the read stream
stream = open(f'{path}/defaultconfig.yaml', 'r')
# load the yaml data with the loader
data = yaml.load(stream, Loader=yaml.FullLoader)
# version verification
if os.path.isfile(f'{path}/defaultconfig.yaml') == True:
if data['class'][0]['identification'] != version:
print('(FileIntegrity): Incorrect version!\n(FileIntegrity): Rebuilt the default config.')
shutil.copyfile(f'{path}/defaultconfig.yaml' , f'{path}/defaultconfig.yaml.old')
with open(f'{path}/defaultconfig.yaml','w') as file:
file.write(configfile)
file.close()
try:
configloader('defaultconfig.yaml', noexceptions=False)
except:
print(Exception.with_traceback())
print('(FileIntegrity): \'defaultconfig.yaml\' seems to be broken.\n(FileIntegrity): Rebuilding new config.yaml..')
shutil.copyfile(f'{path}/defaultconfig.yaml' , f'{path}/defaultconfig.yaml.broken')
with open(f'{path}/defaultconfig.yaml','w') as file:
file.write(configfile)
file.close()
def configloader(filename, noexceptions=False):
try:
# create the read stream
stream = open(f'{path}/ultishift/config/{filename}', 'r')
except FileNotFoundError:
return print(f'[ERROR] (ConfigLoader): \'{filename}\' doesn\'t exist.')
except KeyError:
return print(f'[ERROR] (ConfigLoader): \'{filename}\' is broken. Is the file up to date with the defaultconfig?')
# load the yaml data with the loader
cl = yaml.load(stream, Loader=yaml.FullLoader)
if cl['class'][0]['identification'] == version:
# check desdata
try:
if len(cl['desencrypt'][0]['desdata1']) != 11:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'desdata1\' has an invalid length. It must be 11 characters long.\n[ERROR] (ConfigLoader): Config has been reverted.')
print(f'[ERROR] (ConfigLoader): \'desdata1\' has an invalid length. It must be 11 characters long.')
raise ValueError
elif len(cl['desencrypt'][1]['desdata2']) != 32:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'desdatay2\' has an invalid length. It must be 32 characters long.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise ValueError
elif len(cl['desencrypt'][0]['desdata1']) == 11:
desmatch = re.search(r'^[^\W_]{3,48}$', cl['desencrypt'][0]['desdata1'])
if desmatch == None:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'desdata1\' contains non alphabetical and numerical data.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise ValueError
elif len(cl['desencrypt'][1]['desdata2']) == 32:
desmatch = re.search(r'^[^\W_]{3,48}$', cl['desencrypt'][1]['desdata2'])
if desmatch == None:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'desdata2\' contains non alphabetical and numerical data.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise ValueError
except TypeError:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'desencrypt\' has missing data.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise TypeError
# check private data
try:
if len(cl['private'][0]['privatekey']) != 0:
pass
elif len(cl['private'][1]['randomdata1']) != 0:
pass
elif len(cl['private'][2]['randomdata2']) != 0:
pass
except:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'private\' has missing data.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise ValueError
# check file data
try:
if len(cl['file'][0]['filekey']) != 0:
pass
except:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'filekey\' has missing data.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise ValueError
# check discord data
try:
if cl['discord'][0]['keyformatting'] == True:
newdiscordformat = '```'
elif cl['discord'][0]['keyformatting'] == False:
newdiscordformat = ''
else:
newdiscordformat = ''
print(f'[WARNING] (ConfigLoader): \'discord\' has invalid data. It should contain \'True\' or \'False\'.\n[WARNING] (ConfigLoader): Defaulting value to False.')
except TypeError:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'discord\' has missing data.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise TypeError
# check discord peerid data
try:
if len(str(cl['discord'][1]['peerid'])) <= 17:
print(f'[WARNING] (ConfigLoader): \'peerid\' hasn\'t been set or is invalid.')
elif len(str(cl['discord'][1]['peerid'])) == 18:
lettersinid = re.search(r'\d{18}', str(cl['discord'][1]['peerid']))
if lettersinid == None:
print(f'[WARNING] (ConfigLoader): \'peerid\' is an invalid Discord userid.')
elif len(str(cl['discord'][1]['peerid'])) >= 18:
lettersinid = re.search(r'\d{18}', str(cl['discord'][1]['peerid']))
if lettersinid == None:
print(f'[WARNING] (ConfigLoader): \'peerid\' is an invalid Discord userid.')
if str(cl['discord'][1]['peerid']).__contains__(','):
global idarray, peerid
idarray = str(cl['discord'][1]['peerid']).split(',')
i = 0
peerid = ''
for data in idarray:
idarray.insert(i+1, data)
peerid += f'<@{data}> '
idarray.pop(i)
i += 1
else:
print(f'[WARNING] (ConfigLoader): \'peerid\' is an invalid Discord userid.')
except:
if noexceptions == True:
return print(f'[ERROR] (ConfigLoader): \'discord\' has missing data.\n[ERROR] (ConfigLoader): Config has been reverted.')
raise ValueError
global desdata1, desdata2, bigprivatekey, randomdata1, randomdata2, filekey, discordformat, loadedfile
desdata1 = cl['desencrypt'][0]['desdata1']
desdata2 = cl['desencrypt'][1]['desdata2']
bigprivatekey = cl['private'][0]['privatekey']
randomdata1 = cl['private'][1]['randomdata1']
randomdata2 = cl['private'][2]['randomdata2']
filekey = cl['file'][0]['filekey']
discordformat = newdiscordformat
peerid = str(f"<@{cl['discord'][1]['peerid']}>")
loadedfile = filename
configData = [bigprivatekey, randomdata1, randomdata2, desdata1, desdata2, filekey, peerid]
defaultcheck = 0
for data in defaultData:
for input in configData:
if data == input:
defaultcheck += 1
if peerid == '<@None>':
peerid = ''
if defaultcheck != 0:
print(f'[WARNING] (ConfigLoader): You\'re still using default config data in \'{loadedfile}\'.')
return print(f'(ConfigLoader): \'{loadedfile}\' has been successfully loaded.')
else:
if noexceptions == True:
return print(f'(ConfigLoader): Incorrect Version!\n(ConfigLoader): Reverting back to {loadedfile}.')
raise ValueError
def privatekey(request):
if request == 'privatekey':
newkey = f'{randomdata1}DtQLqY9kSaoO9B1TsoGWZp9LyG22/K86gIexwv4{randomdata2}WpvA/rO1tlvyyXU9yo/KJjIyQ+GwV80jCldrAinERthk/tqkcz0w7fdt'
numberkey = (len(newkey)*newkey) * 3
secretkey = (f'{numberkey}{bigprivatekey}oa7kGn2LXElrYq9oa3qPILJkLiaGtNe4JpXxG7udyaTVWgb1n1L54sIyVSnROErfQzZFVsjoCHfKMNGoBMpaTRB{numberkey}') * 35
return secretkey
elif request == 'iv':
return f'{desdata1}='
elif request == 'key':
return f'{desdata2}'
def encryptor(message, password):
# encrypt the data with AES
encrypteddata = aesencrypt(message, password)
# cipher the information
important = cencrypt(encrypteddata, 86, decrypt=False)
# cipher the information again
bigkey = cencrypt(important, 45, decrypt=False)
try:
k = pyDes.triple_des(
base64.b64decode(privatekey('key')),
mode=pyDes.CBC,
IV=base64.b64decode(privatekey('iv')),
padmode=pyDes.PAD_PKCS5
)
encryptedStr = k.encrypt(bigkey)
except:
return print(f'[ERROR] (Encryptor): \'desencrypt\' seems to contain symbols.')
# setting it to base64
decodeddes = bytes.decode(base64.b64encode(encryptedStr))
# cipher
desunciphered = cencrypt(decodeddes, 54, decrypt=False)
# use the dynamic
corekey = aesencrypt(desunciphered, privatekey('privatekey'))
# cipher the dynamicly generated AES
publickey = cencrypt(corekey, 28, decrypt=False)
# exportable and encrypted message
if discordformat == '```' and peerid != '':
if password != '':
return f'{peerid} `{password}`\n{discordformat}{publickey}{discordformat}'
return f'{peerid}\n{discordformat}{publickey}{discordformat}'
return f'{discordformat}{publickey}{discordformat}'
def decryptor(msg, password):
# lets reverse this message
topmessage = cencrypt(msg, 28, decrypt=True)
# decrypt the aes using the privatekey
try:
uncleaned = aesdecrypt(topmessage, password, useprivatekey=True)
except:
return print('\n[ERROR] Privatekey is incorrect.')
# shift the characters back
unciphered = cencrypt(uncleaned, 54, decrypt=True)
# clean the base64 off of the ciphered data
cleaned = base64.b64decode(unciphered)
try:
k = pyDes.triple_des(
base64.b64decode(privatekey('key')),
mode=pyDes.CBC,
IV=base64.b64decode(privatekey('iv')),
padmode=pyDes.PAD_PKCS5
)
decryptedStr = k.decrypt(cleaned)
decryptedStr = bytes.decode(decryptedStr)
except:
return print('\n[ERROR] desencrypt data is incorrect.')
# lets reverse this message
bigkey = cencrypt(decryptedStr, 45, decrypt=True)
# deciphering the ciphered message
deimportant = cencrypt(bigkey, 86, decrypt=True)
try:
# decrypt the aes without private key
decrypted = aesdecrypt(deimportant, password, useprivatekey=False)
except:
return print('\n[ERROR] Password is incorrect.')
# exportable and decrypted message
return decrypted
def aesencrypt(message, password):
# generate salt
salt = get_random_bytes(AES.block_size)
# generate the private key via salt/pass
private_key = hashlib.scrypt(password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32)
# create the cipher config
cipher_config = AES.new(private_key, AES.MODE_GCM)
# decrypt the cipher text
cipher_text, tag = cipher_config.encrypt_and_digest(bytes(message, 'utf-8'))
# format the data
data = f'{base64.b64encode(cipher_text).decode("utf-8")},{base64.b64encode(salt).decode("utf-8")},{base64.b64encode(cipher_config.nonce).decode("utf-8")},{base64.b64encode(tag).decode("utf-8")}'
return data
def aesdecrypt(encryptedmessage, password, useprivatekey=False):
# create an array and split strings
privatearray = encryptedmessage.split(',')
# {cipherdata:privatearray[0], salt:privatearray[1], nonce:privatearray[2], tag:privatearray[3]}
# generate the private key via salt/pass
if useprivatekey == True:
kprivate_key = hashlib.scrypt(privatekey('privatekey').encode(), salt=base64.b64decode(privatearray[1]), n=2**14, r=8, p=1, dklen=32)
else:
kprivate_key = hashlib.scrypt(password.encode(), salt=base64.b64decode(privatearray[1]), n=2**14, r=8, p=1, dklen=32)
# create the cipher config
kcipher = AES.new(kprivate_key, AES.MODE_GCM, nonce=base64.b64decode(privatearray[2]))
# decrypt the cipher text
kdecrypted = kcipher.decrypt_and_verify(base64.b64decode(privatearray[0]), base64.b64decode(privatearray[3]))
return bytes.decode(kdecrypted)
def cencrypt(text, key=17, decrypt=False):
characters = string.ascii_lowercase + string.ascii_uppercase + string.digits + " " + string.punctuation
if decrypt==True:
key = len(characters) - key
table = str.maketrans(characters, characters[key:] + characters[:key])
cipheredtable = text.translate(table)
return cipheredtable
def fileencrypt(filename, extension):
outext = str(input('Insert output file extension [pref: .bin]: '))
if outext == '':
outext = '.bin'
print('[WARNING] (FileEncrypt): File extension was not provided. Using default \'.bin\'.')
if filename == '':
return print('[ERROR] (FileEncrypt): You must provide a file name.')
if os.path.isfile(f'{path}/ultishift/fileconvert/{filename}{extension}'):
with open(f'{path}/ultishift/fileconvert/{filename}{extension}','rb') as file:
filedata = file.read()
# convert the original file data to hex
hexdata = filedata.hex()
# encrypt the data with AES
encrypteddata = aesencrypt(hexdata, filekey)
# cipher the information
important = cencrypt(encrypteddata, 86, decrypt=False)
# cipher the information again
output = cencrypt(important, 45, decrypt=False)
# output the file as bin
with open(f'{path}/ultishift/fileconvert/encrypted/{filename}{outext}','w') as newfile:
newfile.write(output)
return print('(FileEncrypt): File has been encrypted!')
else:
return print(f'[ERROR] (FileEncrypt): \'{filename}{extension}\' doesn\'t exist.')
def filedecrypt(filename, extension):
if filename == '':
return print('[ERROR] (FileEncrypt): You must provide a file name.')
if extension == '':
extension = '.bin'
print('[WARNING] (FileEncrypt): File extension was not provided. Using default \'.bin\'.')
outext = str(input('Insert the real decrypted file extension [pref: .exe]: '))
if os.path.isfile(f'{path}/ultishift/fileconvert/{filename}{extension}'):
with open(f'{path}/ultishift/fileconvert/{filename}{extension}','r') as file:
filedata = file.read()
# lets reverse this message
bigkey = cencrypt(filedata, 45, decrypt=True)
# deciphering the ciphered message
deimportant = cencrypt(bigkey, 86, decrypt=True)
try:
# decrypt the aes without private key
output = aesdecrypt(deimportant, filekey, useprivatekey=False)
except:
return print('\n[ERROR] Filekey is incorrect.')
# decode it from hex back to bytes
output = bytes.fromhex(output)
# out file back to bytes
with open(f'{path}/ultishift/fileconvert/decrypted/{filename}{outext}','wb') as newfile:
newfile.write(output)
return print('(FileEncrypt): File has been decrypted!')
else:
return print(f'[ERROR] (FileEncrypt): \'{filename}{extension}\' doesn\'t exist.')
def np_encrypt(filename, data, password):
""" Establishing a byte array to store encrypted text in """
byte_msg_arr = []
for bytes in encryptor(data, password).encode('utf-8'):
byte_msg_arr.append(bytes)
""" Exporting the note without a password """
if password == '':
file = open(f'{path}/ultishift/notes/{filename}.ults', 'w')
file.write('\n')
for bytes in byte_msg_arr:
file.write(f'{str(bytes)} ')
file.close()
else:
file = open(f'{path}/ultishift/notes/{filename}.ults', 'w')
byte_pass_arr = []
for bytez in encryptor(password, '').encode('utf-8'):
byte_pass_arr.append(bytez)
for bytez in byte_pass_arr:
file.write(f'{str(bytez)} ')
file.write('\n')
for bytes in byte_msg_arr:
file.write(f'{str(bytes)} ')
file.close()
return f'SUCCESS: "{filename}.ults" has been created!'
def np_decrypt(filename):
try:
file = open(f'{path}/ultishift/notes/{filename}.ults', 'r').readlines()
except FileNotFoundError:
return f'[X] ERROR: "{filename}.ults" was not found.'
counter = 0
password = ''
byte_pass_arr = []
byte_msg_arr = []
for line in file:
if counter == 0:
if len(line) > 1:
""" Remove spaces from the line """
line_data = line.split(' ')
line_data.pop(-1)
""" Establishing a byte array to store encrypted password text in """
for data in line_data:
byte_pass_arr.append(int(data))
""" Decrypt the password given """
password = decryptor(''.join(map(chr, bytes(byte_pass_arr))), '')
else:
password = ''
else:
""" Remove spaces from the line """
line_data = line.split(' ')
line_data.pop(-1)
""" Establishing a byte array to store encrypted text in """
for data in line_data:
byte_msg_arr.append(int(data))
counter += 1
""" Decrypting the note """
return decryptor(''.join(map(chr, bytes(byte_msg_arr))), password)
def np_list(directory):
notes = ''
for files in os.listdir(directory):
if '.ults' or '.yaml' in files:
notes += f'[#] {files}\n'
return notes
def main():
try:
text = input(f'{"-"*44}\n--> Ultishift | Loaded: {loadedfile}\n{"-"*44}\n[A] Encrypt\n[B] Decrypt\n[C] File Encrypt\n[D] File Decrypt\n[E] Notepad - Encrypt\n[F] Notepad - Decrypt\n[G] Config Loader\n[X] Exit\n\nSelection: ')
if text.lower() == 'a': # message encrypt only
password = input('Password: ')
data = str(input('Text2Encrypt: '))
print(f'\n\n{encryptor(data, password)}')
elif text.lower() == 'b': # message decrypt only
password = input('Password: ')
data = str(input('Text2Decrypt: '))
print(f'\n\n{decryptor(data, password)}')
elif text.lower() == 'c': # file encrypt only
data = str(input('Filename2Encrypt: '))
filext = str(input('File extension [.*]: '))
fileencrypt(data, filext)
elif text.lower() == 'd': # file decrypt only
data = str(input('Filename2Decrypt: '))
filext = str(input('File extension [.bin]: '))
filedecrypt(data, filext)
elif text.lower() == 'e': # notepad - encrypt
print(f'{"=="*10}\nNotes in Directory:\n{np_list(f"{path}/ultishift/notes")}')
filename = input('[Encryption] Filename [no ext]: ')
if len(filename) != 0:
password = input('Password: ')
data = ''
data += f'This note was written on: {datetime.datetime.now()}\n\n'
while True:
msg = str(input('Done? Type: "::end"-> '))
if msg == '::end':
break
data += f'{msg}\n'
print(f'\n{np_encrypt(filename, data, password)}')
else:
print('\n[ERROR] You cannot write a nameless note.')
elif text.lower() == 'f': # notepad - decrypt
print(f'{"=="*10}\nNotes in Directory:\n{np_list(f"{path}/ultishift/notes")}')
filename = str(input('[Decryption] Filename [no ext]: '))
if len(filename) != 0:
print(f'\n{np_decrypt(filename)}')
else:
print('\n[ERROR] You cannot write a nameless note.')
elif text.lower() == 'g': # config loader
print(f'{"=="*10}\nConfigs in Directory:\n{np_list(f"{path}/ultishift/config")}')
data = str(input('File2Load [name.yaml]: '))
try:
configloader(data, noexceptions=True)
except PermissionError:
print('\n[ERROR] Permission denied in "/config".')
elif text.lower() == 'x':
print('\nExiting..')
sys.exit()
main()
except KeyboardInterrupt:
print('x\n\nExiting..')
if __name__ == '__main__':
fileintegrity()
main()