-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_fortify.py
More file actions
500 lines (396 loc) · 15.8 KB
/
py_fortify.py
File metadata and controls
500 lines (396 loc) · 15.8 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
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
#Py-Fortify
#!/usr/bin/env python
# coding: utf-8
# By Ishan Oshada : https://github.com/Ishanoshada/Py-Fortify
#1:36 AM 23/6/26
import marshal,base64,gzip,zlib,bz2,lzma,binascii,codecs,os,random,argparse,string ,subprocess
try:
import python_minifier
except:
os.system("pip install python_minifier")
print("\x1b[1m\x1b[92m" + "[+] dependencs installed")
import python_minifier
try:
import pyfiglet
except:
os.system("pip install pyfiglet")
print("\x1b[1m\x1b[92m" + "[+] dependencs installed")
import pyfiglet
try:
import requests
except:
os.system("pip install requests")
print("\x1b[1m\x1b[92m" + "[+] dependencs installed")
import requests
try:
import colorama
except:
os.system("pip install colorama")
print("\x1b[1m\x1b[92m" + "[+] dependencs installed")
colorama
try:
import tqdm
except:
os.system("pip install tqdm")
print("\x1b[1m\x1b[92m" + "[+] dependencs installed")
import tqdm
import time,sys
###################################
_name_ = "Py-Fortify"
class bc:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
YELLOW = '\033[1;33m'
RED= '\033[1;31m'
WHITE= '\033[0m'
UNDERLINE = '\033[4m'
bcolors = ['\033[95m'
'\033[94m',
'\033[96m',
'\033[92m',
'\033[93m',
'\033[91m',
'\033[1;33m',
'\033[1;31m',
]
FONTS = (
"basic",
"o8",
"cosmic",
"graffiti",
"chunky",
"epic",
"poison",
"doom",
"avatar",
"3-d",
"alligator2",
"banner3-D",
"block",
"bubble",
"digital",
"doh",
"doom",
"epic",
"fuzzy",
"isometric1",
"letters",
"nancyj-fancy",
"ogre",
"poison",
"roman",
"shadow",
"starwars",
"stop",
"univers",
"usaflag",
)
##################################
def m1(code):
try:
code = code.encode()
except:
code = code
en = gzip.compress(marshal.dumps(compile(code,_name_,"exec")))
output = "import base64,marshal,binascii,gzip,zlib\n"
output += f"#Encoded by {_name_}\n"
output += "try:\n"
output += f"\texec(marshal.loads(gzip.decompress({en})))\n"
output += "except Exception as b:\n\tprint(f'Error by {b} ')"
return output
def m2(data):
en = lzma.compress(base64.b64encode(marshal.dumps(compile(data,_name_,"exec"))))
return f"import marshal,base64\nexec(marshal.loads(base64.b64decode(lzma.decompress({en}))))"
def m3(data):
_name_ = "Py-Fortify"
en = lzma.compress(base64.b85encode(marshal.dumps(compile(data,_name_,"exec"))))
return f"import marshal,base64\nexec(marshal.loads(base64.b85decode(lzma.decompress({en}))))"
def m4(data):
_name_ = "Py-Fortify"
en = lzma.compress(marshal.dumps(compile(data,_name_,"exec")))
output = f"#Encoded by {_name_}\n"
output += "import binascii,lzma,base64,marshal,gzip,zlib\n"
output += f"\ntry:\n\t\texec(marshal.loads(lzma.decompress({en})))\n\n\nexcept Exception as b:\n\n\t\t"+"print(f'\\n\\n\tError to : {b}\\n\\n ')"
return output
def m5(data):
en = base64.b64encode(lzma.compress(marshal.dumps(compile(data,_name_,"exec"))))
return f"import marshal,lzma\nexec(marshal.loads(lzma.decompress(base64.b64decode({en}))))"
def m6(data):
_name_ = "Py-Fortify"
en = lzma.compress(zlib.compress(marshal.dumps(compile(data,_name_,"exec"))))
return f"import marshal,zlib,lzma\nexec(marshal.loads(zlib.decompress(lzma.decompress({en}))))"
def m7(data):
_name_ = "Py-Fortify"
en = lzma.compress(gzip.compress(marshal.dumps(compile(data,_name_,"exec"))))
return f"import marshal,gzip\nexec(marshal.loads(gzip.decompress(lzma.decompress({en}))))"
def m8(data):
en = lzma.compress(marshal.dumps(compile(data,_name_,"exec")))
return f"import marshal\nexec(marshal.loads(lzma.decompress({en})))"
def m9(code):
_name_ = "Py-Fortify"
en = base64.b64encode(gzip.compress(marshal.dumps(compile(code,_name_,"exec"))))
output = f"#ENCODED BY {_name_} \n#you can try this decode\n"
#output += "#https://github.com/Ishanoshada/Py-Fortify"
output += "import marshal,base64,gzip,zlib,bz2,lzma\n"
output += f"exec(marshal.loads(gzip.decompress(base64.b64decode({en}))))"
return output
def m10(code):
# code = python_minifier.minify(code)
code1 = compile(code,_name_,'exec')
en = base64.b64encode(gzip.compress(marshal.dumps(code1)))
return f"import marshal,base64,gzip,lzma;exec(marshal.loads(gzip.decompress(base64.b64decode({en}))))"
def m11(code):
try:
code = code.encode()
except:
code = code
finally:
en = base64.b64encode(gzip.compress(marshal.dumps(compile(code,_name_,"exec"))))
output = f"#ENCODED BY {_name_} \n#you can try this decode\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
output += "import marshal,base64,gzip,zlib,bz2,lzma\n"
output += f"exec(marshal.loads(gzip.decompress(base64.b64decode({en}))))"
return output
def m20(code):
data= python_minifier.minify(code).encode()
en = base64.b64encode(gzip.compress(marshal.dumps(compile(code,_name_,"exec"))))
output = "#https://github.com/Ishanoshada/Py-Fortify\n"
output += f"#ENCODED BY {_name_} \n#you can try this decode\n"
output += "import marshal,base64,gzip,zlib,bz2,lzma\n"
output += f"exec(marshal.loads(gzip.decompress(base64.b64decode({en}))))"
return output
def l1(data,line):
output = "import marshal,base64,lzma,gzip,zlib,binascii\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
for i in range(line):
if int(line/2) == i:
en = gzip.compress(base64.b85encode(marshal.dumps(data)))
output += f"exec(marshal.loads(base64.b85decode(gzip.decompress({en}))));"
else:
da = f"#{base64.b85encode(lzma.compress(data.encode())).decode()}"
en = gzip.compress(base64.b85encode(marshal.dumps(da)))
output += f"exec(marshal.loads(base64.b85decode(gzip.decompress({en}))));"
return output
def l2(data,line):
output = "import marshal,base64,lzma,gzip,zlib,binascii\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
for i in range(line):
if int(line/2) == i:
en = lzma.compress(gzip.compress(base64.b85encode(marshal.dumps(data))))
output += f"exec(marshal.loads(base64.b85decode(gzip.decompress(lzma.decompress({en})))));"
else:
da = f"#{base64.b85encode(lzma.compress(data.encode())).decode()}"
en = lzma.compress(gzip.compress(base64.b85encode(marshal.dumps(da))))
output += f"exec(marshal.loads(base64.b85decode(gzip.decompress(lzma.decompress({en})))));"
return output
def l3(code,n:int):
try:
code = code.encode()
except:
code =code
output = f"#ENCODE BY{_name_} \n#you can try this decode\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
output += "import marshal,base64,gzip,zlib,bz2\n"
for i in range(n):
en = base64.b64encode(gzip.compress(marshal.dumps(f"#{bz2.compress(base64.b64encode(code))}")))
output += f"exec(marshal.loads(gzip.decompress(base64.b64decode({en}))));"
if int(i/2) == i:
en = base64.b64encode(gzip.compress(marshal.dumps(code)))
output += f"exec(marshal.loads(gzip.decompress(base64.b64decode({en}))));"
return output
def l4(code,n:int):
try:
code = code.encode()
except:
code =code
output = f"#ENCODED BY {_name_} \n#you can try this decode\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
output += "import marshal,base64,gzip,zlib,bz2,lzma\n"
for i in range(n):
en = base64.b64encode(gzip.compress(marshal.dumps(compile(f"#{bz2.compress(base64.b64encode(marshal.dumps(code)))}",_name_,"exec"))))
output += f"exec(marshal.loads(gzip.decompress(base64.b64decode({en}))));"
if int(i/2) == i:
en = base64.b64encode(gzip.compress(marshal.dumps(compile(code,_name_,"exec"))))
output += f"exec(marshal.loads(gzip.decompress(base64.b64decode({en}))));"
return output
def l5(code, n: int):
_name_ = "Py-Fortify"
try:
code = code.encode()
except:
code = code
output = f"#ENCODED BY {_name_} \n#you can try this decode\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
output += "import marshal, base64,lzma\n"
for i in range(n):
en = base64.a85encode(marshal.dumps(compile(f"#{base64.a85encode(marshal.dumps(code)).decode()}", _name_, 'exec'))).decode()
output += f'exec(marshal.loads(base64.a85decode("""{en}""")))\n'
if int(i / 2) == i:
en = base64.a85encode(marshal.dumps(compile(code, _name_, 'exec'))).decode()
output += f'exec(marshal.loads(base64.a85decode("""{en}""")))\n'
return output
def l6(code, n: int):
_name_ = "Py-Fortify"
try:
code = code.encode()
except:
code = code
output = f"#ENCODED BY {_name_} \n#you can try this decode\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
output += "import marshal, base64, codecs\n"
for i in range(n):
en = base64.b64encode(marshal.dumps(compile(f"#codecs.encode({code}, 'rot_13').decode()", _name_, 'exec'))).decode()
output += f'exec(marshal.loads(base64.b64decode("""{en}""")))\n'
if int(i / 2) == i:
en = base64.b64encode(marshal.dumps(compile(code, _name_, 'exec'))).decode()
output += f'exec(marshal.loads(base64.b64decode("""{en}""")))\n'
return output
def xor_encrypt(data, key):
key = key.encode()
encrypted = bytearray()
for i in range(len(data)):
encrypted.append(data[i] ^ key[i % len(key)])
return encrypted
def l7(code, n: int):
try:
code = code.encode()
except:
code = code
output = f"#ENCODED BY {_name_} \n#you can try this decode\n"
output += "#https://github.com/Ishanoshada/Py-Fortify\n"
output += "import marshal, base64\n"
for i in range(n):
en = base64.b64encode(xor_encrypt(marshal.dumps(compile(f"#{base64.b64encode(xor_encrypt(marshal.dumps(code), 'ishh')).decode()}", _name_, 'exec')), 'ishh')).decode()
output += f'exec(marshal.loads(xor_encrypt(base64.b64decode("""{en}"""), "ishh")))\n'
if int(i / 2) == i:
en = base64.b64encode(xor_encrypt(marshal.dumps(compile(code, _name_, 'exec')), 'ishh')).decode()
output += f'exec(marshal.loads(xor_encrypt(base64.b64decode("""{en}"""), "ishh")))\n'
return output
###################################
colorama.init(autoreset=True)
def printf(text):
return text.title().center(os.get_terminal_size().columns)
# Initialize current version
CURRENT_VERSION = "2" # Update this to your current version
# URL for the version file
VERSION_URL = "https://raw.githubusercontent.com/Ishanoshada/Py-Fortify/master/.version"
# URL for the main script file
SCRIPT_URL = "https://raw.githubusercontent.com/Ishanoshada/Py-Fortify/master/py_fortify.py"
def update():
"""Checks for updates and updates the script if a new version is available."""
try:
# Get the latest version from the version file
latest_version = requests.get(VERSION_URL).text.strip()
#print(f"Current version: {CURRENT_VERSION}, Latest version: {latest_version}")
# Compare versions
if int(latest_version) > int(CURRENT_VERSION):
print("A new version is available. Updating...")
# Download the latest script
r = requests.get(SCRIPT_URL)
with open('py_fortify.py', 'wb') as f:
f.write(r.content)
print("Updated successfully to version " + latest_version)
print("You can run this again")
sys.exit(1)
else:
pass
#print("You are running the latest version.")
except Exception as e:
print(f"Error in update: {e}")
def logo():
font = random.choice(FONTS)
color1 = random.choice(bcolors)
color2 = random.choice(bcolors)
while color1 == color2:
color2 = random.choice(bcolors)
print(color1 + "•" * os.get_terminal_size().columns, end="\n" * 2)
print(
color2
+ pyfiglet.figlet_format(
"Py\nFortify",
width=os.get_terminal_size().columns,
justify="center",
font=font,
),
end="",
)
print(color1 + "•" * os.get_terminal_size().columns, end="\n" * 2)
def parse_args():
parser = argparse.ArgumentParser(description="obfuscate python programs".title())
parser._optionals.title = "syntax".title()
parser.add_argument(
"-i", "--input", type=str, help="input file name".title(), required=True
)
parser.add_argument(
"-o", "--output", type=str, help="output file name".title(), required=True
)
parser.add_argument(
"-e", "--exec", type=str, help="block -i/ interactive (-e 1)".title(), required=False
)
parser.add_argument(
"-m", "--method", type=str, help=" 1: high encode method is used/ 2: Both methods are used at once [2 recommended] ".title(), required=True
)
parser.add_argument(
"-c",
"--complexity",
type=int,
help="complexity of obfuscation. 50 recommended".title(),
required=True,
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit()
return parser.parse_args()
def m_encoded(data):
m_func = [m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11]
method = random.choice(m_func)
return method(data)
def l_encoded(data,x):
l_func = [l1,l2,l3,l4,l6,l7]
method = random.choice(l_func)
return method(data,x)
###################################
def main():
logo()
args = parse_args()
print(bc.OKCYAN)
print(random.choice(bcolors) + "\t[•] encoding ".title() + args.input)
with tqdm.tqdm(total=args.complexity, bar_format=colorama.Style.BRIGHT + '{l_bar}' +
colorama.Fore.CYAN + colorama.Back.WHITE + '{bar}' + colorama.Style.RESET_ALL +
colorama.Fore.CYAN + '| {n_fmt}/{total_fmt}' + colorama.Style.RESET_ALL) as pbar:
with open(args.input) as file:
for i in range(args.complexity):
if i == 0:
output = m20(file.read())
if str(args.exec) == "1":
output = l6(output,2)
output = l_encoded(output,50)
if i == int(int(args.complexity)/2):
if str(args.method) == "2":
output = l_encoded(output,50)
else:
output = m_encoded(output)
time.sleep(0.1)
pbar.update(1)
fol = str(args.output).replace('.py',' ').replace(" ","")
a = subprocess.getoutput(f"mkdir {fol} ")
if "exists" in a:
pass
with open(fol+"/"+args.output, "w") as out:
output = m20(output)
out.write(output)
print("\n\n")
print((bc.OKCYAN+ f"\t[•] encoding successful!\n\n\tSaved as :- {fol}/".title() + args.output))
if __name__ == "__main__":
try:
update()
except:
pass
main()
###################################