耗时一个月开发的OJ在线判题系统,文末有项目地址,目前还在更新代码~
到目前为止,核心流程已经实现,但是想要上线的话,安全吗?
用户提交恶意代码,怎么办?下面是几种恶意代码的情况
1、执行超时
占用时间资源,导致程序卡死,不释放资源
package com.yupi.yojcodesandbox.unsafe;
/**
* 无限睡眠(阻塞程序执行)
*/
public class SleepError {
public static void main(String[] args) throws InterruptedException {
long ONE_HOUR = 60 * 60 * 1000l;
Thread.sleep(ONE_HOUR);
System.out.println("睡醒了");
}
}
2、占用内存
占用内存资源,导致空间浪费
import java.util.ArrayList;
import java.util.List;
/**
* 无限占用空间(浪费系统内存)
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
List<byte[]> bytes = new ArrayList<>();
while (true) {
bytes.add(new byte[10000]);
}
}
}
(注意把引入的包名去掉,再编译,执行)
实际运行上述程序,会发现内存到达一定空间后,程序就自动报错:
java.lang.OutOfMemoryError: Java heap space
,而不是无限增减内存占用,直到系统死机。
这是JVM的一种保护机制。
可以使用 JVisualVM 或 JConsole 工具,连接到 JVM 虚拟机上来可视化查看运行状态
JConsole 是 jdk 自带的工具 在下面的目录里面:
C:\Program Files\Java\jdk1.8.0_152\bin\jconsle.exe
如图
3、读文件,信息泄露
比如直接通过相对路径获取项目配置文件,获取到密码:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
/**
* 读取服务器文件(文件信息泄露)
*/
public class Main {
public static void main(String[] args) throws InterruptedException, IOException {
String userDir = System.getProperty("user.dir");
String filePath = userDir + File.separator + "src/main/resources/application.yml";
List<String> allLines = Files.readAllLines(Paths.get(filePath));
System.out.println(String.join("\n", allLines));
}
}
4、写文件,植入木马
可以直接向服务器上写入文件,比如一个木马程序:java -version 2>&1
(示例命令)
- java -version 用于显示 Java 版本信息。这会将版本信息输出到标准错误流(stderr)而不是标准输出流(stdout)。
- 2>&1 将标准错误流重定向到标准输出流。这样,Java 版本信息就会被发送到标准输出流。
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
/**
* 向服务器写文件(植入危险程序)
*/
public class Main {
public static void main(String[] args) throws InterruptedException, IOException {
String userDir = System.getProperty("user.dir");
String filePath = userDir + File.separator + "src/main/resources/木马程序.bat";
String errorProgram = "java -version 2>&1";
Files.write(Paths.get(filePath), Arrays.asList(errorProgram));
System.out.println("写木马成功,你完了哈哈");
}
}
5、运行其他程序
直接通过 Process 执行危险程序,或者电脑上的其他程序
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 运行其他程序(比如危险木马)
*/
public class Main {
public static void main(String[] args) throws InterruptedException, IOException {
String userDir = System.getProperty("user.dir");
String filePath = userDir + File.separator + "src/main/resources/木马程序.bat";
Process process = Runtime.getRuntime().exec(filePath);
process.waitFor();
// 分批获取进程的正常输出
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// 逐行读取
String compileOutputLine;
while ((compileOutputLine = bufferedReader.readLine()) != null) {
System.out.println(compileOutputLine);
}
System.out.println("执行异常程序成功");
}
}
6、执行高危操作
甚至都不用写木马文件,直接执行系统自带的危险命令
- 比如删除服务器的所有文件(太残暴,不演示)
- 比如执行 dir(windows) , ls (linux) 获取你系统上的所有文件信息
项目地址
(求求大佬们赏个star~)
前端:https://github.com/IMZHEYA/yoj-frontend
后端:https://github.com/IMZHEYA/yoj-backend
代码沙箱:https://github.com/IMZHEYA/yoj-code-sandbox