限制嵌入式 python 实例的功能
有没有办法限制在嵌入式解释器下运行的 python 脚本的能力?具体来说,我希望阻止脚本执行以下操作:
- 导入 python 扩展模块(即 .pyd 模块),应用程序特别允许的除外。
- 以任何方式操纵进程(即启动新进程或终止应用程序)。
- 任何类型的网络。
- 操作文件系统(例如创建、修改和删除文件)。
Is there a way to limit the abilities of python scripts running under an embedded interpretor? Specifically I wish to prevent the scripts from doing things like the following:
- Importing python extension modules (ie .pyd modules), except those specifically allowed by the application.
- Manipulating processes in any way (ie starting new processes, or terminating the application).
- Any kind of networking.
- Manipulating the file system (eg creating, modifying and deleting files).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。没有简单的方法可以在 CPython 上防止这些事情。您的选择是:
No. There's no easy way to prevent those things on CPython. Your options are:
也许这个会有所帮助。您提供了有关如何使用 ast 的示例。
Maybe this can be helpful. You have an example provided on how to work with the ast.
你想要什么 Google 的 Unladen Swallow 项目,Python 版本的 App Engine 运行。
模块受到严格限制,不允许使用 ctypes,套接字会根据某些策略或其他策略进行匹配,换句话说,您将获得与 Java 产品一致的 Python 沙盒版本。
我想指出的是,这使得该系统几乎毫无用处。对于任何比另一个 [App Engine] 应用程序更酷的东西来说毫无用处。忘记猴子修补系统模块,甚至对自己的堆栈的访问也受到限制。完全不动态。
OT:游戏通常会嵌入 LUA 来编写脚本,也许你应该检查一下。
What you want it Google's Unladen Swallow project that Python version of App Engine runs on.
Modules are severely restricted,
ctypes
are not allowed, sockets are matched against some policy or other, in other words you get a sandboxed version of Python, in line with their Java offering.I'd like to point out that this makes the system almost useless. Well useless for anything cooler than yet another [App Engine] App. Forget monkey-patching system modules, and even access to own stack is restricted. Totally un-dynamic-like.
OT: games typically embed LUA for scripting, perhaps you should check it out.