-
-
Save integeruser/dcf2d1a290db1811e8a26cd7b22e919b to your computer and use it in GitHub Desktop.
Normalize environment when running a program with and without GDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
def exploit(): | |
payload = '\xde\xad\xbe\xef' | |
return payload | |
# Usage: in gdb, execute `set exec-wrapper ./wrapper.py` | |
# Set arguments and environment variables for the program | |
# arg0 (real path of the executable to run) is set automatically | |
args = ['arg1', exploit(), 'arg3'] | |
env = ['VAR1=VALUE1'] | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) != 2: | |
print('Usage %s executable' % sys.argv[0]) | |
sys.exit(1) | |
executable = sys.argv[1] | |
import os | |
path = os.path.realpath(executable) | |
from ctypes import cdll, c_char_p, cast | |
from ctypes.util import find_library | |
args = [path] + args + [None] | |
argp = (c_char_p * len(args))() | |
for i, arg in enumerate(args): | |
argp[i] = cast(arg, c_char_p) | |
env += [None] | |
envp = (c_char_p * len(env))() | |
for i, var in enumerate(env): | |
envp[i] = cast(var, c_char_p) | |
libc = cdll.LoadLibrary(find_library('c')) | |
libc.execve(path, argp, envp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment