liftoff / pyminifier

Pyminifier is a Python code minifier, obfuscator, and compressor.
GNU General Public License v3.0
1.45k stars 223 forks source link

--obfuscate with class members #61

Open tady159 opened 8 years ago

tady159 commented 8 years ago

I was trying a simple test with a class, and I have noticed that although pyminifier does obfuscate class members within the class itself, it doesn't change calls from outside the class, here's an example to illustrate this:

file test.py

class Test:
    integer = 0
    letter = 'A'
    decimal = 1.9

    def __init__(self,integer, letter, decimal):
        self._integer = integer
        self._letter = letter
        self._decimal = decimal

    def getInteger(self):
        return self._integer

t = Test(1, "B", 2.62)

print(t.getInteger())
print(t.integer)

And here's the result using

pyminifier --obfuscate test.py
M=print
class o:
 m=0
 v='A'
 f=1.9
 def __init__(g,m,v,f):
  g._integer=m
  g._letter=v
  g._decimal=f
 def y(g):
  return g._integer
t=o(1,"B",2.62)
M(t.getInteger())
M(t.integer)
# Created by pyminifier (https://github.com/liftoff/pyminifier)

It can be noticed that although "integer" and "getInteger" do get obfuscated inside the class, on the call outside the class, they're not, and of course, this code can't run, because 'o' object has no attribute 'getInteger', etc.

sejans commented 8 years ago

If you want only this to be fixed, you can take commit (https://github.com/sejans/pyminifier/commit/62622e9171407b056c2633f3fd7a37a08f1764e3) that solves this problem - https://github.com/liftoff/pyminifier/issues/54: --obfuscate-variables - obfuscates class static attributes as well

The result will be something like this:

class utM:
 integer=0
 letter='A'
 decimal=1.9
 def __init__(utw,integer,letter,decimal):
  utw._integer=integer
  utw._letter=letter
  utw._decimal=decimal
 def getInteger(utw):
  return utw._integer
t=utM(1,"B",2.62)
print(t.getInteger())
print(t.integer)