如何阻止脚本的所有网络访问?
假设我有一个用Python或Ruby编写的脚本,或者一个用C编写的程序,如何确保该脚本没有访问网络能力?
Suppose I have a script written in Python or Ruby, or a program written in C. How do I ensure that the script has no access to network capabilities?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您或多或少通过用“沙箱”标记来自己给出了一个通用答案,因为这就是您所需要的,某种沙箱。我想到的是:使用在 JVM 上运行的 JPython 或 JRuby。在 JVM 中,您可以使用策略文件创建沙箱,这样 JVM 中的代码就无法执行您不允许的操作。
对于C代码来说,就更困难了。强力的答案可能是在没有网络功能的虚拟机中运行 C 代码。对于这个问题,我现在确实没有更优雅的答案。 :)
You more or less gave a generic answer yourself by tagging it with "sandbox" because that's what you need, some kind of sandbox. Things that come to mind are: using JPython or JRuby that run on the JVM. Within the JVM you can create a sandbox using a policy file so no code in the JVM can do thing you don't allow.
For C code, it's more difficult. The brute force answer could be to run your C code in a virtual machine with no networking capabilities. I really don't have a more elegant answer right now for that one. :)
除非您使用 Python 的沙盒版本(例如使用 PyPy),否则没有可靠的方法可以从脚本本身内部关闭网络访问。当然,您可以在关闭网络访问的虚拟机下运行。
Unless you're using a sandboxed version of Python (using PyPy for example), there is no reliable way to switch-off network access from within the script itself. Of course, you could run under a VM with the network access shut off.
防火墙可以阻止特定应用程序或进程访问网络。 ZoneAlarms 是一个很好的工具,我过去曾用它来完成您想要的操作。因此,它可以通过编程方式完成,但我对操作系统编程的了解还不够,无法提供有关如何进行此操作的任何建议。
Firewalls can block specific applications or processes from accessing the network. ZoneAlarms is a good one that I have used to do exactly what you want in the past. So it can be done programatically, but I don't know near enough about OS programming to offer any advice on how to go about doing it.