1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package Exec; import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Scanner; public class RuntimeDemo { public static void main(String[] args) throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, ClassNotFoundException { RuntimeExec(); RuntimeExecReflect1(); RuntimeExecReflect2(); RuntimeExecOutput1(); RuntimeExecOutput2(); RuntimeExecOutput3(); } public static void RuntimeExec() throws IOException { Runtime.getRuntime().exec("open -a Calculator.app"); } public static void RuntimeExecReflect1() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { Class clazz = Runtime.class; Method method = clazz.getMethod("getRuntime",null); Runtime r = (Runtime) method.invoke(null,null); r.exec("open -a Calculator.app"); } public static void RuntimeExecReflect2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, ClassNotFoundException { Class clazz = Class.forName("java.lang.Runtime"); Method getRuntimeMethod = clazz.getMethod("getRuntime",null); Method execMethod = clazz.getMethod("exec",String.class); execMethod.invoke(getRuntimeMethod.invoke(null,null),"open -a Calculator.app"); } public static void RuntimeExecOutput1() throws IOException { Runtime runtime = Runtime.getRuntime(); Process execProcess = runtime.exec("whoami"); InputStream inputStream = execProcess.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } public static void RuntimeExecOutput2() throws IOException { Runtime runtime = Runtime.getRuntime(); Process execProcess = runtime.exec("whoami"); InputStream inputStream = execProcess.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); } System.out.println(new String(byteArrayOutputStream.toByteArray())); } public static void RuntimeExecOutput3() throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { String str = "whoami"; String rt = new String(new byte[]{106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 82, 117, 110, 116, 105, 109, 101}); Class<?> c = Class.forName(rt); Method m1 = c.getMethod(new String(new byte[]{103, 101, 116, 82, 117, 110, 116, 105, 109, 101})); Method m2 = c.getMethod(new String(new byte[]{101, 120, 101, 99}), String.class); Object obj2 = m2.invoke(m1.invoke(null, new Object[]{}), new Object[]{str}); Method m = obj2.getClass().getMethod(new String(new byte[]{103, 101, 116, 73, 110, 112, 117, 116, 83, 116, 114, 101, 97, 109})); m.setAccessible(true); Scanner s = new Scanner((InputStream) m.invoke(obj2, new Object[]{})).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : ""; System.out.println(result); } }
|