Skip to content

Instantly share code, notes, and snippets.

@xire-
Last active January 16, 2017 11:28
Show Gist options
  • Save xire-/ce00b60cec1fab7dc3f68d3c30542d86 to your computer and use it in GitHub Desktop.
Save xire-/ce00b60cec1fab7dc3f68d3c30542d86 to your computer and use it in GitHub Desktop.
Execute a given program with custom args and env vars. Can be used by gdb as exec wrapper.
#!/usr/bin/python2
import sys
from ctypes import cdll, c_char_p, cast
from ctypes.util import find_library
# to use with gdb:
# set exec-wrapper ./wrapper_template.py
# custom args and env vars
ARGS = [
'arg1',
'arg2',
]
ENVARS = [
'VAR1=foo',
'VAR2=bar',
]
def main():
if len(sys.argv) < 2:
usage()
else:
wrap(sys.argv[1], ARGS, ENVARS)
def usage():
print("Usage: {:s} target_program".format(sys.argv[0]))
exit(0)
def wrap(vuln, args, envars):
libc = cdll.LoadLibrary(find_library("c"))
execve = libc.execve
argp = py2c_str_list(args)
envp = py2c_str_list(envars)
execve(vuln, argp, envp)
# if it reach this point, execve has failed
print('execve failed!')
def py2c_str_list(py_list):
py_list += [None]
str_ptrs = (c_char_p * len(py_list))()
for i, s in enumerate(py_list):
str_ptrs[i] = cast(s, c_char_p)
return str_ptrs
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment