Reverse Engineering Python for Strings

Question: is it possible to reverse-engineer python objects into a string?


Information: I am currently working on a project that distributes my research in several dozen computers, and not just on it. Being a good programmer, I make it a generalized program that can be used not only for its specific program. Thus, computers that run its code will not have and will not import their .py files to import them. The way I did this in the past is just all that is needed in one file and transfer the path to this file to my system, which then imports this code as a from its state as a string (using the information obtained from this question).

But that made me think. Now I can import from a string and execute the code perfectly and dandy. all this is good and good, but it made me think if I have a way around it so that it passes me the code? I'm especially interested in something like that. let's say he has it all in his file:

def hello(): print "Hello world!" 

when importing this file, "hello" will be added to the global namespace. but now I want to transfer it over the network. you can’t call pickle β€œhi” and make it work, I tried, uncovers, refuses.

on the other end, I have node computers using the exec code in newMod. dict "to create the code. therefore, if I could take" hello "and push it through some function that returns" def hello (): \ n print \ "Hello world! \" "to my the exec method could work. I would be golden. I could make it have no more than one file, because I could just redesign any custom imports that it has.

I honestly expect the answer to be no, but I thought it was worth it. Simply put, I want to take the module that was imported and convert it to a string.

+4
source share
2 answers

Although this is not a good approach, you can only select __code__ from a function ...

 >>> def foo(): ... print "Hello" >>> import pickle >>> code = pickle.dumps(foo.__code__) >>> def bar(): ... pass >>> bar() >>> bar.__code__ = pickle.loads(code) >>> bar() Hello 
+1
source

With the marshal's module, you can save code objects back and forth:

 >>> source = """ ... def hello(): ... print "Hello, world!" ... """ >>> >>> code = compile(source, '<mycustomcode>', 'exec') >>> class Module(object): pass ... >>> module = Module() >>> exec code in module.__dict__ >>> module.hello() Hello, world! >>> import marshal >>> marshal.dumps(code) 'c\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\ x00@ \x00\x00\x00s\r\x00\x00\x00d\ x00\x00\x84\x00\x00Z\x00\x00d\x01\x00S(\x02\x00\x00\x00c\x00\x00\x00\x00\x00\x00 \x00\x00\x01\x00\x00\x00C\x00\x00\x00s\t\x00\x00\x00d\x01\x00GHd\x00\x00S(\x02\x 00\x00\x00Ns\r\x00\x00\x00Hello, world!(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x 00\x00\x00(\x00\x00\x00\x00s\x0e\x00\x00\x00<mycustomcode>t\x05\x00\x00\x00hello \x02\x00\x00\x00s\x02\x00\x00\x00\x00\x01N(\x01\x00\x00\x00R\x00\x00\x00\x00(\x0 0\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x0e\x00\x00\x00<mycustomcode>t\ x08\x00\x00\x00<module>\x02\x00\x00\x00s\x00\x00\x00\x00' 

Not sure if this is a good approach though ...

0
source

All Articles