-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
459 lines (379 loc) · 16 KB
/
__init__.py
File metadata and controls
459 lines (379 loc) · 16 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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import os
import configparser
import subprocess
import traceback
import threading
import webbrowser
from typing import Optional, Dict
from binaryninja import Settings, log_info, log_error, log_warn, BinaryViewType, interaction, PluginCommand, BackgroundTask # type: ignore
from binaryninja.binaryview import BinaryView # type: ignore
from binaryninja.debugger import DebuggerEventType, DebuggerController, DebuggerEvent # type: ignore
from binaryninja.enums import MessageBoxButtonSet, MessageBoxIcon, MessageBoxButtonResult # type: ignore
from .settings import SETTING_MAP, register_scyllahide_settings
g_binary_states: Dict[str, 'BinaryDebugState'] = {}
g_state_lock: threading.Lock = threading.Lock()
SCYLLAHIDE_DEFAULTS: Dict[str, str] = {
"DLLNormal": "1",
"DLLStealth": "0",
"DLLUnload": "0",
"RemoveDebugPrivileges": "0",
"KillAntiAttach": "0",
"AutostartServer": "0",
"ServerPort": "1337"
}
class BinaryDebugState:
def __init__(self, bv: BinaryView, controller: DebuggerController) -> None:
self.file_path: str = bv.file.filename
self.controller: DebuggerController = controller
self.callback_id: Optional[int] = None
self.handled_initial_stop: bool = False
self.injected: bool = False
self.lock: threading.Lock = threading.Lock()
def on_debug_event(self, event: DebuggerEvent) -> None:
if event.type == DebuggerEventType.LaunchEventType:
with self.lock:
self.handled_initial_stop = False
self.injected = False
elif event.type == DebuggerEventType.TargetStoppedEventType:
with self.lock:
if self.handled_initial_stop:
return
self.handled_initial_stop = True
if not is_scyllahide_enabled():
log_info("[ScyllaNinja] Disabled - skipping ScyllaHide injection")
return
if perform_injection(self.controller):
with self.lock:
self.injected = True
def get_scylla_dir() -> str:
return Settings().get_string("debugger.scyllahide.02_directory")
def validate_scyllahide_directory() -> bool:
try:
scylla_dir = get_scylla_dir()
if not scylla_dir or not os.path.isdir(scylla_dir):
log_error(f"[ScyllaNinja] Invalid directory: {scylla_dir}")
return False
required_files = [
"InjectorCLIx64.exe",
"InjectorCLIx86.exe",
"HookLibraryx64.dll",
"HookLibraryx86.dll"
]
missing = []
for filename in required_files:
if not os.path.exists(os.path.join(scylla_dir, filename)):
missing.append(filename)
if missing:
log_error(f"[ScyllaNinja] Missing files in directory '{scylla_dir}':")
for filename in missing:
log_error(f" - {filename}")
return False
return True
except Exception as e:
log_error(f"[ScyllaNinja] Directory validation failed: {e}")
return False
def write_scylla_ini() -> bool:
try:
settings = Settings()
profile_name = settings.get_string("debugger.scyllahide.01_profile")
scylla_dir = get_scylla_dir()
ini_path = os.path.join(scylla_dir, "scylla_hide.ini")
config = configparser.ConfigParser()
if os.path.exists(ini_path):
try:
config.read(ini_path)
except configparser.Error as e:
log_warn(f"[ScyllaNinja] Could not parse existing INI: {e}, creating new")
except Exception as e:
log_warn(f"[ScyllaNinja] Error reading INI file: {e}, creating new")
if not config.has_section("SETTINGS"):
config.add_section("SETTINGS")
if profile_name and profile_name != "Custom":
config.set("SETTINGS", "CurrentProfile", profile_name)
else:
section_name = "BinaryNinja Custom"
config.set("SETTINGS", "CurrentProfile", section_name)
if not config.has_section(section_name):
config.add_section(section_name)
for bn_key, ini_key in SETTING_MAP.items():
full_key = f"debugger.scyllahide.{bn_key}"
try:
value = settings.get_bool(full_key)
config.set(section_name, ini_key, "1" if value else "0")
except Exception:
config.set(section_name, ini_key, "0")
for key, value in SCYLLAHIDE_DEFAULTS.items():
config.set(section_name, key, value)
with open(ini_path, 'w') as f:
config.write(f)
return True
except (IOError, PermissionError) as e:
log_error(f"[ScyllaNinja] Cannot write to INI file: {e}")
return False
except Exception as e:
log_error(f"[ScyllaNinja] Failed to write INI: {e}")
log_error(traceback.format_exc())
return False
# binja's debugger doesn't have a way to get the process id of the target program currently, will be changed when this is added
def get_target_pid(controller: DebuggerController) -> Optional[int]:
try:
if not controller.connected:
return None
modules = controller.modules
if not modules:
return None
main_module_name = modules[0].short_name
if not main_module_name:
log_error("[ScyllaNinja] Main module has no name")
return None
main_module_name = main_module_name.lower()
main_module_base = os.path.splitext(main_module_name)[0]
processes = controller.processes
if not processes:
log_error("[ScyllaNinja] No processes available")
return None
for proc in processes:
proc_name_lower = proc.name.lower()
proc_name_base = os.path.splitext(proc_name_lower)[0]
if proc_name_lower == main_module_name or proc_name_base == main_module_base:
log_info(f"[ScyllaNinja] Detected target PID {proc.pid} (exact match: {proc.name})")
return proc.pid
fuzzy_match = None
for proc in processes:
proc_name_lower = proc.name.lower()
if main_module_name in proc_name_lower or proc_name_lower in main_module_name:
fuzzy_match = proc
break
if fuzzy_match:
log_warn(f"[ScyllaNinja] Fuzzy match found: {fuzzy_match.name} (PID: {fuzzy_match.pid})")
result = interaction.show_message_box(
"ScyllaHide - Confirm Target Process",
f"Fuzzy match detected:\n\nProcess: {fuzzy_match.name}\nPID: {fuzzy_match.pid}\n\nInject into this process?",
MessageBoxButtonSet.YesNoButtonSet,
MessageBoxIcon.QuestionIcon
)
if result == MessageBoxButtonResult.YesButton:
return fuzzy_match.pid
process_choices = [f"{proc.name} (PID: {proc.pid})" for proc in processes]
choice_index = interaction.get_choice_input(
"ScyllaHide - Select Target Process",
"Please select the target process:",
process_choices
)
if choice_index is None:
log_info("[ScyllaNinja] User cancelled process selection")
return None
selected_pid = processes[choice_index].pid
log_info(f"[ScyllaNinja] User selected PID {selected_pid} ({processes[choice_index].name})")
return selected_pid
except Exception as e:
log_error(f"[ScyllaNinja] Error getting PID: {e}")
log_error(traceback.format_exc())
return None
def get_target_architecture(controller: DebuggerController) -> Optional[str]:
try:
if hasattr(controller, 'data') and controller.data:
arch = controller.data.arch
if arch.address_size == 8:
return "x64"
elif arch.address_size == 4:
return "x86"
else:
log_warn(f"[ScyllaNinja] Unknown architecture: address_size={arch.address_size}")
return None
return None
except Exception as e:
log_error(f"[ScyllaNinja] Error detecting architecture: {e}")
log_error(traceback.format_exc())
return None
def is_scyllahide_enabled() -> bool:
try:
settings = Settings()
return settings.get_bool("debugger.scyllahide.00_enable")
except Exception:
return False
def perform_injection(controller: DebuggerController) -> bool:
task = BackgroundTask("ScyllaNinja: Validating...")
try:
if not validate_scyllahide_directory():
log_error("[ScyllaNinja] Directory validation failed")
return False
task.progress = "ScyllaNinja: Detecting process..."
pid = get_target_pid(controller)
if not pid or not isinstance(pid, int) or pid <= 0:
log_error("[ScyllaNinja] Failed to detect valid PID")
return False
task.progress = "ScyllaNinja: Detecting architecture..."
arch = get_target_architecture(controller)
if not arch:
log_error("[ScyllaNinja] Failed to detect architecture")
return False
task.progress = "ScyllaNinja: Writing configuration..."
if not write_scylla_ini():
log_error("[ScyllaNinja] Failed to write INI file")
return False
scylla_dir = get_scylla_dir()
injector_exe = f"InjectorCLI{arch}.exe"
dll_name = f"HookLibrary{arch}.dll"
injector_path = os.path.join(scylla_dir, injector_exe)
dll_path = os.path.join(scylla_dir, dll_name)
task.progress = f"ScyllaNinja: Injecting into PID {pid} ({arch})..."
log_info(f"[ScyllaNinja] Injecting into PID {pid} ({arch})...")
result = subprocess.run(
[injector_path, f"pid:{pid}", dll_path, "nowait"],
capture_output=True,
text=True,
cwd=scylla_dir,
timeout=10
)
if result.stdout:
for line in result.stdout.splitlines():
log_info(f"[InjectorCLI] {line}")
if result.stderr:
for line in result.stderr.splitlines():
log_error(f"[InjectorCLI] {line}")
if result.returncode == 0:
task.progress = "ScyllaNinja: Injection successful"
log_info("[ScyllaNinja] Injection successful")
return True
else:
log_error(f"[ScyllaNinja] Injection failed (code {result.returncode})")
return False
except subprocess.TimeoutExpired:
log_error("[ScyllaNinja] Injection timeout")
return False
except Exception as e:
log_error(f"[ScyllaNinja] Injection error: {e}")
log_error(traceback.format_exc())
return False
finally:
task.finish()
def register_debug_callback(bv: BinaryView) -> bool:
file_path: str = bv.file.filename
old_state = None
with g_state_lock:
if file_path in g_binary_states:
old_state = g_binary_states[file_path]
del g_binary_states[file_path]
if old_state:
try:
if old_state.callback_id is not None:
old_state.controller.remove_event_callback(old_state.callback_id)
log_info(f"[ScyllaNinja] Cleaned up stale callback for {file_path}")
except Exception as e:
log_warn(f"[ScyllaNinja] Failed to clean up old callback: {e}")
try:
controller = DebuggerController(bv)
state = BinaryDebugState(bv, controller)
state.callback_id = controller.register_event_callback(state.on_debug_event, "ScyllaNinja - ScyllaHide Injection")
with g_state_lock:
g_binary_states[file_path] = state
return True
except Exception as e:
log_error(f"[ScyllaNinja] Failed to register callback: {e}")
return False
def on_view_open(bv: BinaryView) -> None:
success = register_debug_callback(bv)
if not success:
log_error(f"[ScyllaNinja] Failed to register debug callback for {bv.file.filename}")
def manual_inject_handler(bv: BinaryView) -> None:
try:
controller = DebuggerController(bv)
if not controller.connected:
interaction.show_message_box(
"ScyllaNinja - Not Debugging",
"No active debug session. Start debugging first.",
MessageBoxButtonSet.OKButtonSet,
MessageBoxIcon.ErrorIcon
)
return
file_path = bv.file.filename
state = None
with g_state_lock:
state = g_binary_states.get(file_path)
if not state:
success = register_debug_callback(bv)
if not success:
log_error("[ScyllaNinja] Failed to register debug state")
interaction.show_message_box(
"ScyllaNinja - Error",
"Failed to initialize debug state. Cannot inject.",
MessageBoxButtonSet.OKButtonSet,
MessageBoxIcon.ErrorIcon
)
return
with g_state_lock:
state = g_binary_states.get(file_path)
already_injected = False
if state:
with state.lock:
already_injected = state.injected
if already_injected:
result = interaction.show_message_box(
"ScyllaHide - Already Injected",
"ScyllaHide has already been injected into this process.\n\nInject again anyway?",
MessageBoxButtonSet.YesNoButtonSet,
MessageBoxIcon.WarningIcon
)
if result != MessageBoxButtonResult.YesButton:
return
log_info("[ScyllaNinja] Manual injection requested")
success = perform_injection(controller)
if success:
with g_state_lock:
state = g_binary_states.get(file_path)
if state:
with state.lock:
state.injected = True
else:
interaction.show_message_box(
"ScyllaHide - Injection Failed",
"Failed to inject ScyllaHide. Check the log for details.",
MessageBoxButtonSet.OKButtonSet,
MessageBoxIcon.ErrorIcon
)
except Exception as e:
log_error(f"[ScyllaNinja] Manual injection error: {e}")
log_error(traceback.format_exc())
interaction.show_message_box(
"ScyllaNinja - Error",
f"Error during injection: {e}",
MessageBoxButtonSet.OKButtonSet,
MessageBoxIcon.ErrorIcon
)
def is_debugging(bv: BinaryView) -> bool:
try:
controller = DebuggerController(bv)
return controller.connected
except Exception:
return False
def init_plugin() -> None:
try:
register_scyllahide_settings()
except Exception as e:
log_error(f"[ScyllaNinja] Failed to register settings: {e}")
if not validate_scyllahide_directory():
result = interaction.show_message_box(
"ScyllaNinja - Setup Required",
"ScyllaHide directory is not configured or is missing required files.\n\n"
"Would you like to download ScyllaHide now?\n\n"
"The required files are:\n"
"- InjectorCLIx64.exe\n"
"- InjectorCLIx86.exe\n"
"- HookLibraryx64.dll\n"
"- HookLibraryx86.dll\n\n"
"After downloading, extract the files and configure the directory in:\n"
"Edit > Settings > Debugger > scyllahide",
MessageBoxButtonSet.YesNoButtonSet,
MessageBoxIcon.QuestionIcon
)
if result == MessageBoxButtonResult.YesButton:
webbrowser.open("https://github.com/x64dbg/ScyllaHide/releases/tag/v1.4")
BinaryViewType.add_binaryview_finalized_event(on_view_open)
PluginCommand.register(
"ScyllaNinja\\Inject ScyllaHide",
"Manually inject ScyllaHide into the debugged process",
manual_inject_handler,
is_debugging
)
init_plugin()