在Java的反序列化远程代码执行过程中,最终就是通过调用Runtime类的exec函数来执行系统命令。
如下代码,通过执行Runtime.getRuntime().exec()函数执行 calc.exe 命令
package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec("calc.exe");
java.io.InputStream is = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("GBK"))); //设置读取的时候的编码为GBK
p.waitFor();
if(p.exitValue()!=0){
//说明命令执行失败
}else{
String s = null;
while((s=reader.readLine())!=null){
System.out.println(s);
}
}
}
}