Injects relocatable code into the process memory and executes it. @warning: Don't forget to free the memory when you're done with it! Otherwise you'll be leaking memory in the target process. @see: L{inject_dll} @type payload: str @param paylo
(self, payload, lpParameter=0)
| 3445 | # ------------------------------------------------------------------------------ |
| 3446 | |
| 3447 | def inject_code(self, payload, lpParameter=0): |
| 3448 | """ |
| 3449 | Injects relocatable code into the process memory and executes it. |
| 3450 | |
| 3451 | @warning: Don't forget to free the memory when you're done with it! |
| 3452 | Otherwise you'll be leaking memory in the target process. |
| 3453 | |
| 3454 | @see: L{inject_dll} |
| 3455 | |
| 3456 | @type payload: str |
| 3457 | @param payload: Relocatable code to run in a new thread. |
| 3458 | |
| 3459 | @type lpParameter: int |
| 3460 | @param lpParameter: (Optional) Parameter to be pushed in the stack. |
| 3461 | |
| 3462 | @rtype: tuple( L{Thread}, int ) |
| 3463 | @return: The injected Thread object |
| 3464 | and the memory address where the code was written. |
| 3465 | |
| 3466 | @raise WindowsError: An exception is raised on error. |
| 3467 | """ |
| 3468 | |
| 3469 | # Uncomment for debugging... |
| 3470 | ## payload = '\xCC' + payload |
| 3471 | |
| 3472 | # Allocate the memory for the shellcode. |
| 3473 | lpStartAddress = self.malloc(len(payload)) |
| 3474 | |
| 3475 | # Catch exceptions so we can free the memory on error. |
| 3476 | try: |
| 3477 | # Write the shellcode to our memory location. |
| 3478 | self.write(lpStartAddress, payload) |
| 3479 | |
| 3480 | # Start a new thread for the shellcode to run. |
| 3481 | aThread = self.start_thread(lpStartAddress, lpParameter, bSuspended=False) |
| 3482 | |
| 3483 | # Remember the shellcode address. |
| 3484 | # It will be freed ONLY by the Thread.kill() method |
| 3485 | # and the EventHandler class, otherwise you'll have to |
| 3486 | # free it in your code, or have your shellcode clean up |
| 3487 | # after itself (recommended). |
| 3488 | aThread.pInjectedMemory = lpStartAddress |
| 3489 | |
| 3490 | # Free the memory on error. |
| 3491 | except Exception: |
| 3492 | self.free(lpStartAddress) |
| 3493 | raise |
| 3494 | |
| 3495 | # Return the Thread object and the shellcode address. |
| 3496 | return aThread, lpStartAddress |
| 3497 | |
| 3498 | # TODO |
| 3499 | # The shellcode should check for errors, otherwise it just crashes |
no test coverage detected