gpt4 book ai didi

python - Python 函数 "compile"和 "compiler.parse"是否安全(沙盒)?

转载 作者:太空狗 更新时间:2023-10-30 01:56:00 25 4
gpt4 key购买 nike

我计划在网络环境中使用这些函数,所以我担心这些函数是否可以被利用并用于在服务器上执行恶意软件。

编辑:我不执行结果。我解析 AST 树和/或捕获 SyntaxError。

这是有问题的代码:

try:
#compile the code and check for syntax errors
compile(code_string, filename, "exec")
except SyntaxError, value:
msg = value.args[0]

(lineno, offset, text) = value.lineno, value.offset, value.text

if text is None:
return [{"line": 0, "offset": 0,
"message": u"Problem decoding source"}]

else:
line = text.splitlines()[-1]

if offset is not None:
offset = offset - (len(text) - len(line))
else:
offset = 0

return [{"line": lineno, "offset": offset, "message": msg}]

else:
#no syntax errors, check it with pyflakes
tree = compiler.parse(code_string)
w = checker.Checker(tree, filename)
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))

checker.Checker 是解析 AST 树的 pyflakes 类。

最佳答案

我认为更有趣的问题是你用编译函数做什么?运行它们绝对是不安全的。

我已经测试了一些我能想到的漏洞利用,因为它只是一个语法检查器(不能重新定义类/函数等)我不认为无论如何都可以让 python在编译时执行任意代码

关于python - Python 函数 "compile"和 "compiler.parse"是否安全(沙盒)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com