JAVA命令执行

file:RCE.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class RuntimeTest{
public static void main (String args []) throws Exception{
if (args.length==0) {
System.exit(1); //没有参数就退出
}
String command = args[0];
Runtime run = Runtime.getRuntime();
Process pro = run. exec(command); //执行命令
InputStreamReader in = new InputStreamReader(pro.getInputStream());
BufferedReader buff = new BufferedReader(in);
for(String temp = buff.readLine();temp!=null;temp=buff.readLine()){
System.out.println(temp); //输出结果
}
buff .close();
in.close();
}
}
1
2
javac -encoding UTF-8 RuntimeTest.java
java RuntimeTest "whoami"

JAVA命令执行
http://example.com/2024/05/13/JAVA命令执行/
Author
chaye
Posted on
May 13, 2024
Licensed under