【问题标题】:Contest platform protection from bad codes竞赛平台保护免受不良代码的影响
【发布时间】:2022-07-15 20:41:40
【问题描述】:

我有一个 npm “编译运行”库。

通过 node.js 使用 child_process 它将运行程序代码 (c++,c,node.js,java,pytho)

我的问题是那里的一些命令对我很危险。 例如,如果 python 运行此代码:

import os 
os.system("shutdown /s /t 1")
print(sum(map(int,input().split())))

我有一个 VDS 服务器,因此我无法仅针对类似请求保护该站点。 对我来说只有一种方法Gpedit.msc 但是太多了,我不知道它的性能分类。

我该如何保护。这里有很多案例。文件保护代码保护。

有没有人知道 codeforces.com、acmp.ru、leetcode.com 是否有关于如何做到这一点的信息。

帮帮我谢谢。

但我无法限制它。

const {VM} = require('vm2');
const {c, cpp, node, python, java} = require('compile-run');

const vm = new VM({
    timeout: 1000,
    allowAsync: true,
    require: {
        external:true
    },
    sandbox: {
        require,
  console , 
    }
});
 
vmCode = `
const {c, cpp, node, python, java} = require('compile-run');
python.runSource(
`import os
os.system("shutdown /s /t 1")
print(1)\`, 
{ stdin: '', timeout: 3000, compileTimeout:3000  }   ,async(err, data) => {
      console.log(data)
});
`

try{
    vm.run(vmCode)
}catch(e){
    console.log("Err: ",e)
}

【问题讨论】:

  • 恐怕这个问题对 SO 来说太宽泛了。它也不是关于编程,而是关于系统设置,因此这里可能是题外话。我不知道编码平台究竟是如何保护免受恶意代码侵害的,但它们很可能使用某种虚拟化,例如每个会话的轻量级虚拟机。
  • 是的,一个非常困难的问题 - 应该咀嚼一些东西吗?
  • @JakobStark .. 我添加了一些新内容

标签: python c++ node.js security


【解决方案1】:

我建议使用 Docker 来避免此类问题:

  • 用户的代码可能会删除文件,但这只会影响虚拟环境
  • Docker 允许使用 docker run --cpus 1 --memory 10M 等标志指定限制

您需要将源目录、输入和输出文件绑定为mount points(一起或单独)-v /path/to/host/dir:/container/dir

此外,您可以使用--network none 指定no network connections

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2017-05-18
    • 1970-01-01
    相关资源
    最近更新 更多