通过执行Runtime.getRuntime().exec()函数执行 calc.exe 命令
package first;
public class RCE2 {
public static void main(String[] args) {
String command1 = "calc.exe";
try {
Process process = Runtime.getRuntime().exec(command1);
process.waitFor();
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
在Windows平台上,运行被调用程序的DOS窗口在程序执行完毕后往往并不会自动关闭,从而导致Java应用程序阻塞在waitfor()语句。导致该现象的一个可能的原因是,该可执行程序的标准输出比较多,而运行窗口的标准输出缓冲区不够大。解决的办法是,利用Java中Process类提供的方法让Java虚拟机截获被调用程序的DOS运行窗口的标准输出,在waitfor()命令之前读出窗口的标准输出缓冲区中的内容
package first;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class RCE2 {
public static void main(String[] args) {
String command1 = "ipconfig";
try {
Process process = Runtime.getRuntime().exec(command1);
java.io.InputStream is = process.getInputStream();// getInputStream()方法用于获取流程和子流程的输入流。
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("GBK")));//InputStreamReader类是从字节流到字符流的桥接器
String s = null;
while((s=reader.readLine())!=null){
System.out.println(s);
}
process.waitFor();
}catch(Exception ex) {
ex.printStackTrace();
}
}
}