Import error in obfuscated code by my script

48 Views Asked by At

I wrote my program that obfuscates the script and returns obfuscated files and deobfuscator, but it throws an error cannot import name 'foo' from 'include' with multi-file projects.

scripts before obfuscation: main.py

from include import foo

if __name__ == "__main__":
    foo()

include.py

def foo():
    print("hello world")

scripts after obfuscation: main.py

from pysource_obf.script_57242852 import PySource_obf
PySource_obf(b'x\x9c+\xf7s\x8a\x0cp\x0e\x0c\xf1\xcbw\xcawvL\x0f\xcd\xf41\x0f\x0b(\xf5\xac\xf256\xa8\n(\xf4\x0c\r\x0b\xf3)\x8e\xb0\x8c\xcc\xf2\xab\xb0\x08\x0e\xd0\xf7\xcc\x0c\xa9\xc8\xae\xccL\xd3\xae\xf2\xc9\n\r-\xb4p\xae,)\xf7\xad\x0c\xcb\t\r)0\xb1\x08\xf6\xcdJ\x0e\x0b(\xb2\xf0\xf6\xa9\xf0J\x05\x00.\xd8\x1e7')

include.py

from pysource_obf.script_57242852 import PySource_obf
PySource_obf(b'x\x9c\xb3\xb5-7\xf2\xce\xcf+s4\x08\xf5\n-/\xc8\xb7\xf0I\xcb\xb0\xa8\xf0\xca\x0b\xad\x8a,\xf0\xf3t*\xf3\xf5)LwvN\x0f\xcd\x0c\xf11\x88\xd0\xb7\xa8\xf4\xf4\x0b\x0b\xf1*\xc86\xc9\xaa\xf2J\x05\x00n}\x14o')

code of deobfuscator:

import base64
import zlib
class PySource_obf:
    def __init__(self, code):
        self.code = code
        self.DeObfuscate()
        
    def DeObfuscate(self):
        try:
            self.BaseDecrypt()
            exec(self.code)
        except Exception as runTimeError:
            print("Runtime error occurred, error: ",end='')
            print(runTimeError)

    def BaseDecrypt(self):
        self.code = zlib.decompress(self.code)
        self.code = self.code[::-1]
        self.code = base64.b64decode(self.code)
        self.code = zlib.decompress(self.code)
        self.code = self.code[::-1]
        self.code = self.code.decode('utf-8')
        self.code = self.code[::-1]     

So when I run obfuscated main.py it should print hello world, but It throws the error cannot import name 'foo' from 'include'. I tried many methods, but I still can't solve it. I'll be glad for any help.

0

There are 0 best solutions below