鉴于我的代码是开源的并且我在服务器上运行,并且我接受近乎原始的代码,那么对我来说最糟糕的情况是什么?
我正在研究几个案例,在这些案例中,接受近乎原始的代码会容易得多。那么,
- 如果你不能使用 lambda,你能对表达式做的最糟糕的事情是什么,以及如何做?
- 如果不能使用 import,那么对执行的代码最糟糕的情况是什么?如何使用? (不能使用 X == 扫描字符串来查找 X)
此外,如果有人能想到给定 d = {key:value,...} 的表达式,则 B 是不必要的: expr.format(key) == d[key]
不改变格式的外观。
I'm looking at several cases where it would be far, far, far easier to accept nearly-raw code. So,
- What's the worst you can do with an expression if you can't lambda, and how?
- What's the worst you can do with executed code if you can't use import and how?
(can't use X == string is scanned for X)
Also, B is unecessary if someone can think of such an expr that given d = {key:value,...}:
expr.format(key) == d[key]
Without changing the way the format looks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于表达式来说,最糟糕的情况就是
服务器进程以
root
身份运行。否则,您可以通过执行,或者使服务器停止运行“nofollow">shell fork 炸弹:
或者在 Python 本身中执行临时(但具有足够破坏性)fork 炸弹:
扫描
__import__
不会有帮助,因为有无数种方法可以到达请注意,
eval
和exec
函数也可用于以间接方式创建上述任何函数。如果您想在服务器上进行安全的表达式计算,请使用ast.literal_eval
。
The worst you can do with an expression is on the order of
if the server process is running as
root
. Otherwise, you can fill up memory and crash the process withor bring the server to a grinding halt by executing a shell fork bomb:
or execute a temporary (but destructive enough) fork bomb in Python itself:
Scanning for
__import__
won't help, since there's an infinite number of ways to get to it, includingNote that the
eval
andexec
functions can also be used to create any of the above in an indirect way. If you want safe expression evaluation on a server, useast.literal_eval
.任意Python代码?
...
Arbitrary Python code?
...
再多的白名单或黑名单也无法阻止人们接触 Python 的危险部分。例如,您提到在未定义“open”的沙箱中运行。但我可以这样做来得到它:
如果你说我不会有
os
,那么我可以这样做:或者
等等,等等,等等。一切都是相连的,真正的力量就在那里某处。坏人会发现它。
No amount of whitelisting or blacklisting is going to keep people from getting to dangerous parts of Python. You mention running in a sandbox where "open" is not defined, for example. But I can do this to get it:
and if you say I won't have
os
, then I can do:or
etc, etc, etc. Everything is connected, and the real power is in there somewhere. Bad guys will find it.