安全的Python解释器?
有安全的Python解释器吗?
想象一下您可以在计算机上运行的 Python VM,它限制了操作。 无法打开文件、无法进行系统调用等。 它只是将 stdin 转换为 stdout,可能使用文本处理 + 数学等。
是否存在这样安全的 Python VM?
Is there a secure Python intepreter?
Imagine a Python VM you can run on your machine, that restricts the operations.
No files can be opened, no system calls, etc.
It just transforms stdin to stdout, maybe with text processing + math etc.
Does such a secure Python VM exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道没有这样一个公开分发的“安全解释器”(显然 Google 有一个在 App Engine 中使用的安全解释器,尽管与您想要的限制有些不同,例如,某些文件可以打开,以只读方式)。不过,有一些声明,例如 此处,尽管我无法验证它们。 Pypy 的 Python in a Sandbox 可能是最值得尝试的一个,考虑到 pypy 开发团队的高素质和声誉(他们非常不太可能提出未经证实的主张)。
I know of no such "secure interpreter" that is openly distributed (obviously Google has one that it uses in App Engine, though with somewhat different restrictions from those you desire, e.g., certain files can be opened, in a read-only way). There are some claims for it, though, e.g. here, though I can't verify them. Pypy's Python in a Sandbox is probably the top one worth trying, given the high quality and reputation of pypy's development team (they're VERY unlikely to make unsubstantiated claims).
您可以使用 SecurityManager 在 JVM 上运行 Jython,该 SecurityManager 允许您指定允许/禁止的操作。
You could run Jython on the JVM with a SecurityManager that allows you to specify permitted / disallowed operations.
您不需要修改 Python 来限制某种意义上的执行。只需查看 codepad.org,您可以在其中粘贴代码(Python 和其他语言)并运行它以及显示的输出。代码在非常受限的环境中运行,但这只是操作系统配置。 (示例粘贴)
You don't need a modified Python to restrict execution in a certain sense. Just look at codepad.org, a pastebin where you can paste code (in Python and other languages) and have it run and the output shown. The code runs in a very restricted environment, but that's just the OS configuration. (Example paste)
您可以在权限受限的 .NET 应用程序域内运行 IronPython。
这肯定可以在 Windows 中工作,并且可能/可能在 Mono 中工作(我真的不能说)。
您需要编写一个嵌入 IronPython 解释器并将脚本传递给它的小程序。
Iron Python in Action 书中关于嵌入的章节中的第一个示例展示了编写这样一个启动器。
我不记得它是否涵盖应用程序域,但该信息应该在网络上的某个地方。
You could run IronPython inside a .NET appdomain that has restricted privileges.
This would definitely work in Windows, and possibly/probably in Mono (I couldn't really say).
You would need to write a little program that embeds the IronPython interpreter and passes the script to it.
The first example in the chapter on embedding in the book Iron Python in Action shows to write such a launcher.
I don't recall if it covers appdomains, but that info should be on the Web somewhere.
我是一名本科生,在第一年,我们学习了Python。我们有这些称为“CodeLabs”的东西,必须定期提交。它的工作原理是提出一个问题,要求学生将答案输入到文本框中,然后在一些测试用例上运行该代码并检查它们的返回值
有一天,codelabs 网站 (turingscraft.com) 变得无法访问,因为有人决定运行一个无休止的 while 循环并在其中调用 os.fork() 。
对于 turingscraft.com 的管理员来说,这显然是一个问题。然而,他们后来找到了一种方法来限制学生访问此类命令。
如果我是你,我会查找有关他们网站的信息。也许他们发布了一些有关此问题以及如何解决此问题的信息
I am an undergrad and in my first year, we were taught python. We had these things called "CodeLabs" that had to be submitted periodically. It works by asking a question and asking the student to input their answer into a text box and running that code on some test cases and checking their return values
One day, the codelabs website (turingscraft.com) became inaccessible because someone decided to run an endless while loop and calling os.fork() inside it.
This was obviously a problem for the administrators of turingscraft.com. However, they later found a way to restrict access to such commands for students.
If I were you, I would look up information about their site. Maybe they posted some information about this and how to fix it
您始终可以访问源代码并制作您自己风格的 Python。如果有足够多的人需要它,它很快就会启动并运行。
You could always go to the source code and make your own flavor of Python. If enough people need it, it will be no time before it's up and running.
我最近一直在玩弄这个。我的要求包括 Python 3.x,它可以立即排除 Jython 和 IronPython 等解决方案。无论如何,我都会犹豫是否要走这条路,因为我从来不信任用户模式语言虚拟机。
既然如此,就我的目的而言,迄今为止最好的解决方案是将其完全从解释器手中夺走并在严格锁定的容器(OpenVZ 或类似容器)中运行。然而,这是对问题的重锤(尽管不是全虚拟化的大锤),并且如果您必须运行真正大量的独立解释器,则可能不可行。
不过,一个好处是,因为它不依赖于任何特定解释器的安全性,所以您可以在环境中使用您想要的任何任意语言——您不必将自己与 Python 或一组语言联系起来/可用于 JVM 或 .NET/Mono 的实现。
I've been toying with this lately. My requirements include Python 3.x which immediately takes solutions like Jython and IronPython off the table. I'd be hesitant to take that route anyway, as I've never trusted user-mode language VMs.
That being the case, for my purposes the best solution so far is to take it out of the hands of the interpreter completely and run in a strongly locked-down container (OpenVZ or similar). However, this is taking a hammer to the problem (albeit not the sledgehammer of full virtualization), and may not be viable if you have to run a truly huge number of isolated interpreters.
One upside, though, is that because it doesn't rely on the security of any particular interpreter, you can use any arbitrary language you want in the environment -- you don't have to tie yourself to Python or the set of languages/implementations available for JVM or .NET/Mono.
安全性难道不是操作系统的工作吗?
我的意思是,创建一个对文件等具有受限访问权限的用户。然后让虚拟机仅以这些权限运行。
或者也许我在胡说八道。我不是系统管理员或安全专家,但我倾向于使用专门的工具来做事。
Isn't security more a job for the operating system ?
I mean, create a user with restricted access to files and such. Then let the vm be ran only with these rights.
Or maybe I'm speaking nonsense. I'm no sysadmin or security expert, but I tend to do things with the tools that are made for it.