There seem to be issues when we attach to a process run with pwntools. Sometimes the SIGTRAP signal of a breakpoint is not intercepted by our ptrace but is sent to the process, causing it to stop unexpectedly. I was not able to reproduce the issue using d.run or subprocess followed by d.attach.
For example, the following script causes an error after a few sendline cycles (often during the first interaction).
io = process('./pkm')
d = debugger()
input("press enter to continue")
d.attach(io.pid)
bp = d.breakpoint(position = 0x401C8b, callback = lambda x, y: print('hit'), hardware=True)
d.cont()
io.recv(90)
for i in range(100):
io.sendline(b'0')
io.recv(100)
print('add_pkm')
io.sendline(b'5')
d.wait()
assert bp.hit_count == 100
print('Done!!!!')
d.kill()

Meanwhile, the following script seems to work properly.
io = subprocess.Popen(['./pkm'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
d = debugger()
input("press enter to continue")
pid = io.pid
d.attach(pid)
bp = d.breakpoint(position = 0x401C8b, callback = lambda x, y: print('hit'), hardware=True)
d.cont()
io.stdout.read(90)
for i in range(100):
io.stdin.write(b'0\n')
io.stdin.flush()
io.stdout.flush()
io.stdout.read(100)
print('add_pkm')
io.stdin.write(b'5\n')
io.stdin.flush()
d.wait()
assert bp.hit_count == 100
print('Done!!!!')
d.kill()
Without breakpoints, everything works properly.
From my tests, the ptrace wait does not return, and it SEEMS as if the SIGTRAP is sent directly to the process (I hope and believe this is not really the case). I am working on understanding what pwntools does that can interfere with our bps.
Thanks to @danmaam (and his passion for cursed implementations) for highlighting the original strange behavior.
There seem to be issues when we attach to a process run with pwntools. Sometimes the SIGTRAP signal of a breakpoint is not intercepted by our ptrace but is sent to the process, causing it to stop unexpectedly. I was not able to reproduce the issue using
d.runorsubprocessfollowed byd.attach.For example, the following script causes an error after a few
sendlinecycles (often during the first interaction).Meanwhile, the following script seems to work properly.
Without breakpoints, everything works properly.
From my tests, the ptrace wait does not return, and it SEEMS as if the SIGTRAP is sent directly to the process (I hope and believe this is not really the case). I am working on understanding what pwntools does that can interfere with our bps.
Thanks to @danmaam (and his passion for cursed implementations) for highlighting the original strange behavior.