rocky / python-uncompyle6

A cross-version Python bytecode decompiler
GNU General Public License v3.0
3.74k stars 408 forks source link

Whitespace in variable names #312

Closed ZetaTwo closed 2 years ago

ZetaTwo commented 4 years ago

Description

uncompyle6 blindly trists the names of the variables in the code object even if they are not valid Python symbol names. This can lead to incorrect decompilation output

How to Reproduce

If we compile the following Python code into bytecode:

x = "Hello"
y = "World"
EVIL = 1
print(x + " " + y)

and manually rename the variable "EVIL" to "y = 'Hello' #", the output when running will still be "Hello World" but if we decompile it, the output from uncompyle will be:

x = "Hello"
y = "World"
y = 'Hello' # = 1
print(x + " " + y)

and running this code gives the output "Hello Hello".

A slightly more realistic example can be found below:

$ git clone https://github.com/ZetaTwo/python-obfuscator
$ cd python-obfuscator
$ ./test.sh

In this bytecode, one variable is named "i = 0\n j" making the decompiled implementation of the RC4 function different from the bytecode.

Expected behavior

I expect uncompyle6 to either warn about the presence of improperly named variables or simply rename them by for example replacing all invalid characters with "_".

Environment

$ uncompyle6 --version                       
uncompyle6 3.6.4

$ python3 -c "import sys; print(sys.version)"
3.7.3 (default, Oct  7 2019, 12:56:13) 
[GCC 8.3.0]

OS: Ubuntu 19.04

Additional Environment or Context

I understand that you generally don't support obfuscated Python code but I think this is very simple to implement and does not contribute to making the code more complex. I'd be willing to implement this and provide a PR for this but I would like your opinion if it should be a warning or renaming the variables or maybe even some command line option to choose between the two.

rocky commented 4 years ago

I'd be willing to implement this and provide a PR for this but I would like your opinion if it should be a warning or renaming the variables or maybe even some command line option to choose between the two.

Sure, If you are willing to do this, I'm okay with adding this feature. However, the place where this would be done is in the cross-version disassembly xdis.

Then an option would be added here to pass over the setting to the xdis disassembler.

Probably an issue there should be opened as well.

ZetaTwo commented 4 years ago

Ok great. I'll open another issue and continue the discussion there. Thank you.