Skip to content

Instantly share code, notes, and snippets.

@Pratik151
Last active March 11, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pratik151/58fd921116ce314d796b to your computer and use it in GitHub Desktop.
Save Pratik151/58fd921116ce314d796b to your computer and use it in GitHub Desktop.
Code is to show how ctypes can be used to retrieve address of functions from dll
import ctypes
kernel32 = ctypes.windll.kernel32
handle = kernel32.LoadLibraryW(u'kernel32.dll')
winExec_address = kernel32.GetProcAddress(handle,'WinExec') #Gets base address of WinExec function
exit_address = kernel32.GetProcAddress(handle,'ExitProcess') #Gets address of ExitProcess module
code = '''
;execcmd.asm
[Section .text]
global _start
_start:
jmp short GetCommand
CommandReturn:
pop ebx ;ebx holds the string
xor eax,eax
mov [ebx + 7],al ;insert the NULL character
push ebx
mov ebx,%s
call ebx ;call
xor eax,eax ;zero the register again, clears winexec retval
push eax
mov ebx, %s
call ebx ;call ExitProcess(0);
GetCommand:
;the N at the end of the db will be replaced with a null character
call CommandReturn
db "cmd.exeN"
''' % (hex(winExec_address), hex(exit_address))
print code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment