Python obfuscation
Carl Friedrich Bolz
cfbolz at gmx.de
Wed Nov 9 18:49:57 EST 2005
hi!
bonono at gmail.com wrote:
> How effective can it be when python is designed to make writing this
> kind of code hard(hopefully impossible) ? The most effective would be
> renaming function and may be variables but if the functions are kept
> short, they would at most looks like haskell ;-)
>
There just cannot be a python obfuscator that works for a general python
program. The problem is that on the one hand regular strings can be used
to lookup values in namespaces (e.g. with getattr) and on the other hand
the lookup of names can be controlled (e.g. with __getattr__ and
friends). Therefore any string can potentially contain a name that would
have to be changed to keep the code working after obfuscation. For
example how would you automatically obfuscate the following code:
class HelloWorld(object):
def hello(self):
return "world"
def world(self):
return "!"
if __name__ == '__main__':
h = HelloWorld()
s = "hello"
while 1:
f = getattr(h, s, None)
print s,
if f is None:
break
s = f()
While this is surely a contrived case that intentionally mixes names and
strings that are used for something in the application there are also
quite often legitimate use cases for this sort of behaviour. Duck typing
is basically based on this.
Cheers,
Carl Friedrich Bolz
More information about the Python-list
mailing list