伙伴们。我正在开发一个 ASP.NET MVC 应用程序,例如 Hackerrank / Leetcode 等。它提供了一系列问题,用户必须在他的计算机上解决这些问题,然后提交源代码。该应用程序将支持 C#、Java、C++ 和 Pascal 解决方案。一切都很好,直到我意识到没有安全感。
我的应用程序的工作原理如下:
用户将其源代码提交到服务器(program.cs、main.cpp 等)
服务器编译代码。如果创建了可执行文件,应用程序会认为“一切正常,我们可以运行这个program.exe / main.exe Guy”。
服务器运行 .exe 文件 N 次,其中 N 是每个问题的测试次数。然后,它将 .exe 文件的输出与该特定测试用例的正确输出进行比较。
问题是我找不到安全运行该代码的方法。用户可以提交一些恶意代码。服务器将 100% 运行它。该怎么办?
最初我以这种方式运行 .exe
Process p = new Process();
p.StartInfo.FileName = (.exe file path goes here)
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
//Send the imput, get the output
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用SandBox。
AppDomain ap = AppDomain.CreateDomain("ProgramEXE",null);
string assemblyPath = (.exe location);
ap.ExecuteAssembly(assemblyPath, Imput);
AppDomain.Unload(ap);
Run Code Online (Sandbox Code Playgroud)
也许我必须授予一组权限,但该怎么做呢?如果程序是恶意的,如何停止它,如何在虚拟机中运行它或其他什么?请给我一个提示。
例如,如果不受信任的代码如下所示:
using System;
using …
Run Code Online (Sandbox Code Playgroud)