0

I am given a piece of python byte code written by others without the source file.

It can be run successfully. And I want to disassemble it.

The problem is that after executing the following code:

import dis

dis.dis(byte_code)

I just got the function name, instead of the function body.

In addition, if I try to import the byte code, I'll get some errors:

ImportError: Bad magic number

So, is there some way to disassemble the function body?

martineau
  • 119,623
  • 25
  • 170
  • 301
Arolia
  • 511
  • 2
  • 6
  • 12
  • How did you import the byte code? – martineau May 14 '17 at 15:14
  • from _https://docs.python.org/2/library/dis.html_: `dis.dis([bytesource]) Disassemble the bytesource object. bytesource can denote either a module, a class, a method, a function, or a code object. For a module, it disassembles all functions. For a class, it disassembles all methods. For a single code sequence, it prints one line per bytecode instruction. If no object is provided, it disassembles the last traceback.` – boardrider May 14 '17 at 15:19
  • The bad magic number is probably because you are running a different python version. See the accepted answer to http://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error – cdarke May 14 '17 at 15:33
  • @martineau I just save the code obj to code.pyc, and write in another python script: import code – Arolia May 15 '17 at 01:47
  • @cdarke I have seen this answer, but the problem is that I have neither the source code nor the version. The only thing I can do with it is to run the code obj. – Arolia May 15 '17 at 01:50
  • Luke: I suggest you try `import the_module`, then `dis.dis(the_module)`. – martineau May 15 '17 at 02:19
  • @martineau Is there another way to import the module? I've tried to but failed. – Arolia May 15 '17 at 12:31
  • Who gave you the module, why don't they have the version and the source? – cdarke May 15 '17 at 14:12
  • @cdarke It's actually a part of reverse engineering, but I have the problem just disassembling it. – Arolia May 15 '17 at 14:48
  • Luke: There are other ways of importing, but essentially they'd essentially be equivalent—so I wouldn't bother describing them. In your question you said "It can be run successfully." How are you doing that if not by `import`ing it? – martineau May 15 '17 at 16:13
  • I just use the exec(marshal.load(file)) . – Arolia May 16 '17 at 03:00

0 Answers0