python-bytecode | Python Bytecode talk/code/examples
kandi X-RAY | python-bytecode Summary
kandi X-RAY | python-bytecode Summary
Tema: Seguridad/Ingeniería inversa sobre Python. Licencia: CC BY NC SA.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of python-bytecode
python-bytecode Key Features
python-bytecode Examples and Code Snippets
Community Discussions
Trending Discussions on python-bytecode
QUESTION
Problem Statement
I have a file (no extension) with some nicely formatted python opcodes that I would like to reassemble into the original .py file (or as close as I can).
Recreating Problem
I can recreate a file like the one I have. Begin with a file called test.py
, with the contents:
ANSWER
Answered 2019-Jun-30 at 23:17There is some confusion about what uncompyle6 does. It starts with Python bytecode, or more accurately "wordcode" if this is Python 3.6 or greater. Alternatively it is often used to decompile a Python-compiled file which contains bytecode.
Judging from what you show above, what I believe you want to do is start with a text representation of bytecode produced by the version-specific disassembler that comes with (and only completely works on) the version that Python is running.
Here is the reason you get that strange "Import Error" message above from uncompyle6. It looks at the beginning of the text file you have weirdly called a Python compiled file. That file starts with the ASCII-encoded string "1
" and uncompyle6 is interpreting that according to the specific format for Python compiled file, where the beginning of the file contains some sort of Python-encoded version string, technically called a "magic number".
Never fear though, I have written a few more tools to get you closer to where you want to get to. Specifically, I wrote a Python cross-version assembler to match Python's built-in disassembler.
This is in my github project python-xasm.
Using that, you can produce real Python bytecode which can be run. And if the code you wrote indeed is like from something Python spit out, it probably can be decompiled back into high-level Python.
However, xasm currently does need a little more help than what you have above. Specifically it won't guess from opcode names which Python version(s) they can belong to. Matching opcode names with acceptable Python versions is even harder than you might think. If you see LOAD_CONST
, you also need to consider whether this is instruction takes 2 bytes or 3. If 2 then it is Python 3.6 and greater otherwise it is Python < 3.6. And if that is not hard enough already, some versions of Python change the opcode value for a particular opcode name! Therefore it is possible that you might not be able to exactly determine which Python interpreter some assembly comes from. But I am assuming you don't care, as long as whatever you come up with is consistent.
So with the above, now back to solve your question.
First produce real bytecode. You could do it like this
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install python-bytecode
You can use python-bytecode like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page