forked from xFGhoul/PythonProtector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
318 lines (280 loc) · 10.5 KB
/
Copy pathanalysis.py
File metadata and controls
318 lines (280 loc) · 10.5 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
"""
____ ____ __ __
/ __ \\ __ __ / __ \\ _____ ____ / /_ ___ _____ / /_
/ /_/ // / / // /_/ // ___// __ \\ / __// _ \\ / ___// __/
/ ____// /_/ // ____// / / /_/ // /_ / __// /__ / /_
/_/ \\__, //_/ /_/ \\____/ \\__/ \\___/ \\___/ \\__/
/____/
Made With ❤️ By Ghoul & Marci
"""
import os
import ctypes
from ctypes import WinDLL
from ..types import Event, Logger
from ..abc import Module
from ..utils.webhook import Webhook
class AntiAnalysis(Module):
def __init__(
self, webhook: Webhook, logger: Logger, exit: bool, report: bool, event: Event
) -> None:
self.webhook: Webhook = webhook
self.logger: Logger = logger
self.exit: bool = exit
self.report: bool = report
self.event: Event = event
self.kernel32: WinDLL = ctypes.windll.kernel32
self.ntdll: WinDLL = ctypes.windll.ntdll
@property
def name(self) -> str:
return "Anti Analysis"
@property
def version(self) -> int:
return 1.0
def CheckDebugPrivilege(self) -> None:
"""Check Debug Privilege"""
hToken = ctypes.c_void_p()
if (
self.ntdll.NtOpenProcessToken(
self.kernel32.GetCurrentProcess(), 0x0020, ctypes.byref(hToken)
)
!= 0
):
return
privileges = ctypes.create_string_buffer(1024)
return_length = ctypes.c_ulong()
if (
self.ntdll.NtQueryInformationToken(
hToken,
0x03,
ctypes.byref(privileges),
ctypes.sizeof(privileges),
ctypes.byref(return_length),
)
!= 0
):
self.ntdll.NtClose(hToken)
return
debug_privilege = (ctypes.c_int * (return_length.value // 8)).from_buffer(
privileges
)
for priv in debug_privilege:
if priv.s_luid.LowPart == 21 and priv.s_attributes & 0x00000002:
self.ntdll.NtClose(hToken)
self.logger.info("Debug Privilege Found Enabled")
if self.report:
self.webhook.send("Debug Privilege Enabled", self.name)
self.event.dispatch(
"debug_privilege_found", "Debug Privilege Enabled", self.name
)
self.event.dispatch(
"pyprotector_detect", "Debug Privilege Enabled", self.name
)
if self.exit:
os._exit(1)
self.ntdll.NtClose(hToken)
def HideThreads(self) -> None:
"""Hide Current Threads"""
PID = self.kernel32.GetCurrentProcessId()
hProcess = self.kernel32.OpenProcess(0x1F0FFF, False, PID)
if hProcess is None:
return
TID = self.kernel32.GetCurrentThreadId()
hThread = self.kernel32.OpenThread(0x1F03FF, False, TID)
if hThread is None:
self.kernel32.CloseHandle(hProcess)
return
self.ntdll.NtSetInformationThread(
hThread, 0x11, ctypes.byref((ctypes.c_int(1)), ctypes.sizeof(ctypes.c_int))
)
self.kernel32.CloseHandle(hThread)
self.kernel32.CloseHandle(hProcess)
def CheckDebugObject(self) -> None:
"""Check If Debug Object Handle Found"""
PID = self.kernel32.GetCurrentProcessId()
hProcess = self.kernel32.OpenProcess(0x1F0FFF, False, PID)
if hProcess is None:
return
HasDebugObject = (
self.ntdll.NtQueryInformationProcess(
hProcess,
0x1E,
ctypes.byref(ctypes.c_int(0)),
ctypes.sizeof(ctypes.c_int),
ctypes.byref(ctypes.c_int(0)),
)
== 0
)
if HasDebugObject:
self.kernel32.CloseHandle(hProcess)
self.logger.info("Debug Object Handle Found")
if self.report:
self.webhook.send("Debug Object Handle Detected", self.name)
self.event.dispatch(
"debug_object_handle_found",
"Debug Object Handle Detected",
self.name,
)
self.event.dispatch(
"pyprotector_detect",
"Debug Object Handle Detected",
self.name,
)
if self.exit:
os._exit((1))
def CheckSEDebugName(self) -> None:
"""Check SE Debug Name"""
PID = self.kernel32.GetCurrentProcessId()
hProcess = self.kernel32.OpenProcess(0x1F0FFF, False, PID)
if hProcess is None:
return
DebugObjectHandle = ctypes.c_void_p()
if (
self.ntdll.NtQueryObject(
hProcess,
0x1F,
ctypes.byref(DebugObjectHandle),
ctypes.sizeof(DebugObjectHandle),
None,
)
== 0
):
self.kernel32.CloseHandle(hProcess)
self.logger.info("Debug Object Handle Found")
if self.report:
self.webhook.send("Debug Object Handle Detected", self.name)
self.event.dispatch(
"se_debug_name", "Debug Object Handle Detected", self.name
)
self.event.dispatch(
"pyprotector_detect", "Debug Object Handle Detected", self.name
)
if self.exit:
os._exit((1))
def CheckNtGlobalFlag(self) -> None:
"""Check NT_GLOBAL_FLAG_DEBUGGED"""
PEB = ctypes.c_int(0)
self.ntdll.NtQueryInformationProcess.argtypes = [
ctypes.c_void_p,
ctypes.c_int,
ctypes.c_void_p,
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_ulong),
]
self.ntdll.NtQueryInformationProcess(
self.kernel32.GetCurrentProcess(),
0,
ctypes.byref(PEB),
ctypes.sizeof(PEB),
None,
)
if (PEB.value & 0x00000001) != 0:
self.logger.info(
"NT_GLOBAL_FLAG_DEBUGGED Found in the Process Environment Block"
)
if self.report:
self.webhook.send(
"NT_GLOBAL_FLAG_DEBUGGED Found in the Process Environment Block",
self.name,
)
self.event.dispatch(
"nt_global_flag_debugged",
"NT_GLOBAL_FLAG_DEBUGGED Found in the Process Environment Block",
self.name,
)
self.event.dispatch(
"pyprotector_detect",
"NT_GLOBAL_FLAG_DEBUGGED Found in the Process Environment Block",
self.name,
)
if self.exit:
os._exit(1)
def CheckHardwareBreakpoints(self) -> None:
"""Check For Exisiting Hardware Breakpoints"""
ThreadContext = ctypes.c_void_p()
TID = self.kernel32.GetCurrentThreadId()
hThread = self.kernel32.OpenThread(0x1F03FF, False, TID)
if hThread is None:
return
if not self.kernel32.GetThreadContext(hThread, ctypes.byref(ThreadContext)):
self.kernel32.CloseHandle(hThread)
return
if (
ThreadContext.contents.Dr0 != 0
or ThreadContext.contents.Dr1 != 0
or ThreadContext.contents.Dr2 != 0
or ThreadContext.contents.Dr3 != 0
):
self.kernel32.CloseHandle(hThread)
self.logger.info("Hardware Breakpoints Found Set")
if self.report:
self.webhook.send("Hardware Breakpoints Found Set", self.name)
self.event.dispatch(
"hardware_breakpoint_set",
"Hardware Breakpoints Found Set",
self.name,
)
self.event.dispatch(
"pyprotector_detect",
"Hardware Breakpoints Found Set",
self.name,
)
if self.exit:
os._exit(1)
self.kernel32.CloseHandle(hThread)
def CheckDebugFilterState(self) -> None:
"""Check Debug Filter State Being !=0"""
self.ntdll.DbgSetDebugFilterState(0, 0)
DebugFilterState = ctypes.c_uint()
self.ntdll.DbgQueryDebugFilterState(0, ctypes.byref(DebugFilterState))
if DebugFilterState.value != 0:
self.logger.info("Debug Filter State is not 0")
if self.report:
self.webhook.send(
"Debug Filter State `!= 0`, debugging detected", self.name
)
self.event.dispatch(
"debug_filter_state", "Debug Filter State is not 0", self.name
)
self.event.dispatch(
"pyprotector_detect", "Debug Filter State is not 0", self.name
)
if self.exit:
os._exit(1)
def CheckPEB(self) -> None:
"""Check PEB For BeingDebugged"""
class PEB(ctypes.Structure):
_fields_ = [("BeingDebugged", ctypes.c_byte)]
process = self.kernel32.GetCurrentProcess()
peb = PEB()
self.ntdll.NtQueryInformationProcess(
process, 0, ctypes.pointer(peb), ctypes.sizeof(peb), None
)
if peb.BeingDebugged:
self.logger.info("Process Being Debugged")
if self.report:
self.webhook.send("Process Found Being Debugged", self.name)
self.event.dispatch(
"peb_being_debugged", "Process Found Being Debugged", self.name
)
self.event.dispatch(
"pyprotector_detect", "Process Found Being Debugged", self.name
)
if self.exit:
os._exit(1)
def StartAnalyzing(self) -> None:
if self.report:
self.logger.info("Starting Analysis Checks")
self.CheckDebugPrivilege()
self.CheckDebugObject()
self.CheckSEDebugName()
self.CheckNtGlobalFlag()
self.CheckHardwareBreakpoints()
self.CheckDebugFilterState()
self.CheckPEB()
if self.report:
self.logger.info("Finished All Analysis Checks")
if self.report:
self.logger.info("Hiding Threads")
self.HideThreads()
if self.report:
self.logger.info("Threads Hidden")