4

I've seen a few sites talking about injecting DLL's (such as http://www.codeproject.com/KB/DLL/DLL_Injection_tutorial.aspx), but I'm struggling with how to get an EXE to work. any help/tips would be appreciated.

The best way I know how to explain it is "RunPE" where you execute an exe in the memory. Does that help at all?

Jack B Nimble
  • 5,039
  • 4
  • 40
  • 62
user785255
  • 61
  • 1
  • 1
  • 5
  • 4
    You might begin by describing the problem you want to solve here. In general, it's not possible to "inject" one complete EXE into another process, and especially not with Python. There may be a better way to do what you want to do. – Greg Hewgill Jun 06 '11 at 01:01
  • It is my understanding that it is possible to replace a function pointer in one process to reference a function somewhere else in memory. It is furthermore my understanding that such functionality is typically disabled by modern operating systems through memory access controls. – motoku Jun 06 '11 at 01:28

5 Answers5

3

If you're asking how to inject code into a running Python process, what you want is https://github.com/lmacken/pyrasite .

Daniel F
  • 13,684
  • 11
  • 87
  • 116
fractalcat
  • 31
  • 2
1

You can use the Reflective DLL Injector as described here. Metasploit project uses it to load its meterpreter plug-ins. AFAIK this is the only way to inject a DLL, as MS officially does not support "injecting" from memory, only loading from file system.

On a low level, nothing forbids you from allocating a memory region, loading code there, marking it executable.

Note, that none of these techniques are Python specific or even Python related - it is a win32 problem.

Konrads
  • 2,206
  • 2
  • 30
  • 45
1

What you're talking about is re-implementing UPX in python with more stuff. Things you would need to do in order to do this: Change all VirtualAlloc calls to be VirtualAllocEx calls. Change all Loadlibrary calls to be loadlibraryEX calls. Implement the relocation fix-ups.

A better approach would probably be tweaking UPX to output a DLL instead of an executable. Then using some python DLL injection code to throw that into another process. You're going to be working with CTypes a lot if you want to do this. Fair warning...

RobotHumans
  • 807
  • 10
  • 25
0

To inject a shared object (.so, .dll) into any process you can use injector with C, or pyinjector with python/shell.

To inject python code into a running python process, you can use hypno.

kmaork
  • 5,722
  • 2
  • 23
  • 40
0

I would recommend this book http://www.amazon.com/Gray-Hat-Python-Programming-Engineers/dp/1593271921 - especially the chapters on writing your own debugger, but it covers the metasploit and other tools as described above.

lifeisstillgood
  • 3,265
  • 2
  • 21
  • 22