简体   繁体   English

在线虚拟机中安全地评估perl代码

[英]evaluating perl code securely in a virtual machine online

update 2 : I got this working and it's live :) 更新2 :我得到了这个工作,它的生活:)

update: Please check the comments, @ikegami answer is working but with a slight problem. 更新:请检查评论,@ urggami答案是否正常但有轻微问题。 I may be doing something pretty simple wrong. 我可能正在做一些非常简单的错误。


I recently decided I had to learn Linux/Perl really well, and then I started building this www.tryperl.com as a learning project. 我最近决定我必须学习Linux / Perl,然后我开始构建这个www.tryperl.com作为学习项目。 It's a Cloud IDE of sorts. 它是各种各样的云IDE

(It's only been a few months since I first laid hands on perl, linux, bash, osx, so please go easy on me) (自从我第一次接触perl,linux,bash,osx以来仅仅几个月,所以请放轻松我)

Currently I eval code with Safe.pm , but my game plan is : 目前我使用Safe.pm评估代码,但我的游戏计划是:

A separate Amazon EC2 VM that will eval unsafe perl code and return the result. 一个单独的Amazon EC2 VM,它将评估不安全的 perl代码并返回结果。 The VM will be blocked from the internet and load balanced. VM将被阻止从互联网和负载平衡。 I can then reset this machine from a snapshot every now and then. 然后,我可以偶尔从快照重置此计算机。

This is mostly the code I will use to eval perl code on the server, I use Time::Out for timeout management: 主要是我将用于评估服务器上的perl代码的代码,我使用Time :: Out进行超时管理:

my $code = ..
my $arg = ..

#create a file with random name
my $filename = rand().".pl";
open(FILE,">$filename")
print FILE $code;
close(FILE);

#use Time::Out to timeout after 10 secs

my $ret = timeout 10 => sub {
  #run the file just created with $arg as an argument. << This is IMP 
  my $r = `perl $filename $arg`;
  return $r;
};
if ($@){
    return $@;
}   
return $ret;

My Issues 我的问题

  1. Can I somehow avoid physical file creation and pipe the $code with the $arg ? 我可以以某种方式 避免物理文件创建使用$ arg管道$ code吗? Like open(FILE,"perl <some_magic> | ") . 喜欢 open(FILE,"perl <some_magic> | ") Might sound stupid but I had to ask :( . This has been solved by @ikegami in the comments. 可能听起来很愚蠢,但我不得不问:( 。这已经在评论中由@ikegami解决了。

  2. I can't seem to use Capture::Tiny . 我似乎无法使用Capture :: Tiny I tried doing this inside the timeout block : 我尝试在超时块中执行此操作:
    my ($stdout, $stderr, $count) = capture { system('echo Hello') };
    But I kept getting empty strings! 但我一直在变空! :( Is it because it's inside the timeout? (I don't care too much about this though) :(是因为它在超时内吗?(虽然我不太关心这个)

  3. Are there any other security flaws I'm not seeing? 我还没有看到任何其他安全漏洞吗? Should I be doing a fork limit ? 我应该做叉限制吗? How should I begin doing that? 我应该怎么做呢? (Links and some pointers would be nice) (链接和一些指针会很好)

  4. The author of http://www.perltuts.com says in this article he uses qemu and a debian image to run his code. http://www.perltuts.com的作者在本文中说他使用qemu和debian图像来运行他的代码。 Apart from fork limiting, Is his approach semantically similar to mine ? 除了限制叉, 他的方法在语义上与我的相似吗? (Forgive my ignorance here again, Remember I touched a linux box only a few months ago) (再次请原谅我的无知,记得我几个月前才碰过一个linux盒子)

My dev box is OSX 10.8, Production server is RHEL for front-end and Ubuntu for the Perl eval Machine. 我的开发盒是OSX 10.8,生产服务器是RHEL用于前端,Ubuntu用于Perl eval机器。 I run on EC2. 我在EC2上运行。 See the full stack details here . 此处查看完整堆栈详细信息

Any detailed answer will be will appreciated and rewarded with rep and unicorn dust :) 任何详细的答案将得到赞赏和奖励与rep和独角兽​​尘埃:)

After 13 days of hard work, I finally did it!! 经过13天的努力,我终于做到了!

I explored FreeBSD Jails , my lack of networking experience and the fact that I had to rebuild the OS many times drove me away. 我探索了FreeBSD Jails ,我缺乏网络经验以及我不得不多次重建操作系统驱使我离开的事实。 FreeBSD is really cool though!! FreeBSD真的很酷!! I'll get back to it soon. 我很快就会回来。

I looked bleakly at OpenVZ and then thanks to @ewwhite I revisited Linux Containers and gave it a proper shot. 我在OpenVZ看起来很惨淡,然后感谢@ewwhite我重新访问了Linux Containers并给了它一个合适的镜头。

The docs aren't great but this article really helped with everything. 文档并不好,但这篇文章真的对一切都有帮助。

  • I setup multiple containers under a load balancer : HAProxy . 我在负载均衡器下设置了多个容器: HAProxy
  • I have a perl installation inside each container with the cpan modules I need. 我在每个容器内部都安装了一个perl,并带有我需要的cpan模块。
  • My PerlExecutor application is a Dancer app that runs on Starman , it runs under owned by a limited user which has less privileges and has limits in limits.conf 我的PerlExecutor应用程序是一个在Starman上运行的Dancer应用程序,它由一个有限用户拥有,该用户拥有较少的权限并且在limits.conf中有限制。
  • The container is blocked from the internet. 容器被禁止上网。

One Limitation : I don't know much about networking so I blocked the jails from the internet by disabling port forwarding on the host. 一个限制 :我对网络知之甚少,所以我通过在主机上禁用端口转发来阻止来自互联网的监狱。 However, the jails still need to be on the network for the host to communicate to it, as a result, you can still do a ping inside the jail which will resolve the domain but it won't respond . 但是,jail仍然需要在网络上供主机与之通信,因此,您仍然可以在jail中执行ping操作 ,这将解析域但不会响应 So any web requests inside fail. 因此任何内部Web请求都会失败。 I also do a string scan for Ping and block it. 我也为Ping进行字符串扫描并阻止它。

Any suggestions or improvements will be very welcome! 任何建议或改进都将非常受欢迎!

I'd like to thank @JakeFeasel @ikegami @ewwhite @chris-s and the guys at ubuntu.SE and unix.SE for their help : 我要感谢@JakeFeasel @ikegami @ewwhite @ chris-s以及ubuntu.SE和unix.SE的帮助:

This is what it looks like: 这就是它的样子:

TryPerl架构图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM