📚 OpenRCE is preserved as a read-only archive. Launched at RECon Montreal in 2005. Registration and posting are disabled.








Flag: Tornado! Hurricane!

 Forums >>  Debuggers  >>  Can PaiMei Launch a Process?

Topic created on: June 12, 2007 13:55 CDT by mborohovski .

So I'm trying to use PaiMei to do some API call tracing and all is working out well, except for the following: I want to be able to spawn a process from within my script and attach to that with pydbg. Now, I can spawn a process just fine, but I have to sleep or wait before I attach to it until it has a PID, and by that point if the process is a short one it may have completed execution.

I'm wondering if there's a way to have PaiMei start a process "inside" pydbg so to speak, so it's attached right as soon as the process begins as opposed to having to wait.

Anyone have any experience with this or useful insights?

  Paolo     June 12, 2007 16:01.26 CDT
Yes, of course you can do it. Just:


my_debugger = pydbg()
my_debugger.set_callback(EXCEPTION_BREAKPOINT,somebphandler)
my_debugger.load('notepad.exe')
my_debugger.debug_event_loop()

  mborohovski     June 12, 2007 16:35.32 CDT
Actually, .load() doesn't allow me to set breakpoints after it loads the process.

I get a "Could not set breakpoint" error. I think this has to do with the fact that for some reason load doesn't actually execute the process completely; as if it's in a suspended state?

  pedram     June 12, 2007 19:07.50 CDT
Use the load routine first and then set all your breakpoints at the initial breakpoint handler. So something along the lines of:

def bp_hander (dbg):
  if dbg.first_breakpoint:
    # set all other breakpoints
    return DBG_CONTINUE

Make sense?

  mborohovski     June 13, 2007 09:08.00 CDT
Thanks, we figured that out late last night.

  mborohovski     June 13, 2007 09:41.25 CDT
As a result, however, a few more questions have arisen.

Is there a way to set breakpoints when a function is loaded? I know about the LOAD_DLL_DEBUG_EVENT, but I can't load it in a handler that calls because the DLL isn't actually loaded until after the handler executes and the debug loop continues.

I also know about enumerate_modules() and am using that to only set breakpoints for modules that are loaded at the beginning (if 'notepad.exe' doesn't load ws2_32 at the beginning, don't set a breakpoint there).

But if ws2_32 is loaded at some point, I'd like to be able to then set the breakpoint there. Any ideas?

Also, any way to get the arguments passed into the function for an API call?

  jms     June 13, 2007 10:25.14 CDT
Hey man,

1) Us the callback from the LOAD_DLL_DEBUG_EVENT to do the handling, you just do a:

last_dll = dbg.get_system_dll(-1);

So whenever the DLL's are loaded, it waits until the DLL is loaded up and then you can access it. As a fail-safe if the DLL is loaded on its own then -1 will not return it correctly, but adding an enumerate_modules() call in your handler will accurately check for it.

2) To get the arguments passed to the function call you use the hooks() container:


hooks = utils.hooks_container()
send = dbg.func_resolve("ws2_32","send");
hooks.add(dbg,send,4,None,sendCallback);

def sendCallback(dbg,args,ret):

     print "You sent to:" + args[2]

     return DBG_CONTINUE



Now if you are looking at some of the more complex examples in a socket call where there are structs being passed around, etc. you have to use :


dbg.read(args[x], length_of_read);


It can get tricky, but I have wrapped most of ws2_32 and successfully pulled IP's, ports, data, etc. and udraw'd it. Thanks to Pedram for helping me with that of course :)

  mborohovski     June 13, 2007 10:32.54 CDT
The only issue with the hooks solution is I'd need to know the number of arguments passed in, wouldn't I?

Also, doesn't dbg.get_system_dll(-1) get a previously loaded DLL? Which means I'd have to wait until a new one is loaded before I could set a bp for the old one?

I'm saying if a process loads a DLL ten minutes after it started...how can I catch that and set a breakpoint?

  jms     June 13, 2007 10:55.28 CDT
Hmmm maybe I am not completely understanding what you are asking, if you are attempting to trace an API call, API being WIN32 API then you should know the number of arguments. If you are trying to figure out an unknown function, then you need to read off the stack directly, this requires a bit more work, and you'll have to dereference any pointers for the arguments.

If the process loads a DLL ten minutes in, then you could use the LOAD_DLL callback, with some threading.

1) LOAD_DLL callback runs in thread 1
2) dllBpSetter() runs in thread 2
3) LOAD_DLL callback fires the new thread just before the RETURN DBG_CONTINUE
4) dllBpSetter() sets the breakpoints on the DLL.

Try that? Not sure if that will work for you.

Note: Registration is required to post to the forums.

There are 31,328 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06
Question about memor...
Dec/12


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit