python compiled code ofuscation

Michael Hudson mwh21 at cam.ac.uk
Tue Aug 1 14:55:36 EDT 2000


Jesus Cea Avion <jcea at argo.es> writes:

> > The first argument to exec can be a string or a compile'd string.
> 
> You can really use compiled strings?   :-?
> 
> >>> import new
> >>> a=open("gestor_cache.pyc").read()
> >>> modname=new.module("gestor_cache")
> >>> modname
> <module 'gestor_cache' (built-in)>
> >>> exec(a,modname.__dict__)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> ValueError: embedded '\0' in exec string

Not quite; for one thing there's an 8 byte header in a .pyc and you
need to unmarsal the result into a code object first.

Try this:

>>> f=open("/usr/lib/python1.5/string.pyc")
>>> f.seek(8,0)
>>> exec marshal.load(f) in d
>>> d={}
>>> d.keys()
['_idmap', 'rfind', '_apply', 'capitalize', 'atoi_error', 'octdigits',
'expandtabs', '_long', 'digits', 'join', 'lower', 'count', 'atof',
'atol', 'hexdigits', 'zfill', 'atoi', 'atof_error', 'swapcase',
'__builtins__', 'joinfields', 'lowercase', '__doc__', 'translate',
'index', 'rstrip', 'split', 'splitfields', 'atol_error', '_float',
'rindex', '_int', 'whitespace', '_idmapL', '_StringType', 'rjust',
'find', 'ljust', 'strip', 'index_error', 'capwords', 'replace',
'center', 'maketrans', 'uppercase', 'lstrip', 'letters', 'upper']

>>>-f.close()-<wink>-ly y'rs
M.

-- 
  That's why the smartest companies use Common Lisp, but lie about it
  so all their competitors think Lisp is slow and C++ is fast.  (This
  rumor has, however, gotten a little out of hand. :)
                                        -- Erik Naggum, comp.lang.lisp



More information about the Python-list mailing list