-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The basic idea is actually pretty simple: there is a single python interpreter, so hooks in different threads can share python state but need to acquire the GIL to enter hooks. Since hooks typically do not block, this should not introduce any new deadlock conditions in the target program. Properly supporting multithreading required a ton of insane hacks. The notable one is patching CPython calls to pthread_cond_init and pthread_mutex_init to use the pshared attribute (since our python threads are actually running under a different pgid). However, I found out we are getting an ancient version of pthreads. I had to patch the Dynamorio private loader to read the ELF symbol version table and ignore versions that are marked 'hidden'. I'm not quite sure why we would get the wrong version by default, since parsing symbol versions is not required (and some loaders may simply not do it), so idk. There were a bunch of hangs which took a long time to debug--- cases where the GIL was not properly dropped around exits, and cases where our own thread structures were not properly free'd around exits. We also attempt to call Py_Finalize on exit now, but it sometimes seems to return an error code still. CTRL-C still may not work correctly.
- Loading branch information
Showing
12 changed files
with
652 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This is basically just ltrace.py but we also print the thread number | ||
# and print a message when threads are created. | ||
|
||
from pyda import * | ||
from pwnlib.elf.elf import ELF | ||
from pwnlib.util.packing import u64 | ||
import string | ||
import sys, time | ||
|
||
p = process() | ||
|
||
e = ELF(p.exe_path) | ||
e.address = p.maps[p.exe_path].base | ||
|
||
plt_map = { e.plt[x]: x for x in e.plt } | ||
|
||
def guess_arg(x): | ||
printable_chars = bytes(string.printable, 'ascii') | ||
|
||
# Is pointer? | ||
if x > 0x100000000: | ||
try: | ||
data = p.read(x, 0x20) | ||
if all([c in printable_chars for c in data[:4]]): | ||
return str(data[:data.index(0)]) | ||
except: | ||
pass | ||
|
||
return hex(x) | ||
|
||
def lib_hook(p): | ||
name = plt_map[p.regs.rip] | ||
print(f"[thread {p.tid}] {name}(" + ", ".join([ | ||
f"rdi={guess_arg(p.regs.rdi)}", | ||
f"rsi={guess_arg(p.regs.rsi)}", | ||
f"rdx={guess_arg(p.regs.rdx)}", | ||
f"rcx={guess_arg(p.regs.rcx)}", | ||
]) + ")") | ||
|
||
def thread_entry(p): | ||
print(f"thread_entry for {p.tid}") | ||
|
||
p.set_thread_entry(thread_entry) | ||
|
||
for x in e.plt: | ||
p.hook(e.plt[x], lib_hook) | ||
|
||
p.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.