You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多文件项目中混淆代码出现ImportError:无法从include导入foo

多文件Python脚本混淆后出现导入错误:cannot import name 'foo' from 'include'

问题场景

我编写了一款可生成混淆文件与反混淆器的脚本混淆程序,但在多文件项目中运行混淆后的代码时,抛出错误:cannot import name 'foo' from 'include'。

原始未混淆代码

main.py

from include import foo

if __name__ == "__main__":
    foo()

include.py

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

混淆后代码

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')

反混淆器代码

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]     

问题根源

错误的核心是反混淆器通过exec(self.code)执行原代码时,作用域被限制在PySource_obf.DeObfuscate方法的局部作用域,而非模块的全局命名空间。

当运行混淆后的include.py时,它会实例化PySource_obf并执行反混淆后的原代码(def foo():...),但这个foo函数仅存在于DeObfuscate方法的局部作用域中,不会被注册到include模块的全局命名空间。因此当main.py的反混淆代码尝试from include import foo时,include模块中没有foo这个名称,直接触发导入错误。

解决方案

修改反混淆器的exec调用,指定其使用当前模块的全局命名空间执行代码,确保原代码中定义的函数、变量能被注册到模块级别,供其他文件导入。

修改后的反混淆器代码

import base64
import zlib
import sys

class PySource_obf:
    def __init__(self, code):
        self.code = code
        self.DeObfuscate()
        
    def DeObfuscate(self):
        try:
            self.BaseDecrypt()
            # 获取当前模块的全局命名空间,让exec在该作用域执行代码
            module_globals = sys.modules[__name__].__dict__
            exec(self.code, module_globals)
        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]     

额外注意:确保混淆后的文件运行时,反混淆器模块(如pysource_obf.script_57242852)的路径能被Python解释器正确识别。

内容的提问来源于stack exchange,提问作者Nick Vinesmoke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:53:10