|
1 | 1 | package org.joychou.controller; |
2 | 2 |
|
3 | | - |
4 | | -import org.springframework.stereotype.Controller; |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.joychou.security.AntObjectInputStream; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
5 | 7 | import org.springframework.web.bind.annotation.RequestMapping; |
6 | | -import org.springframework.web.bind.annotation.ResponseBody; |
| 8 | +import org.springframework.web.bind.annotation.RestController; |
7 | 9 |
|
| 10 | +import javax.servlet.http.Cookie; |
8 | 11 | import javax.servlet.http.HttpServletRequest; |
9 | | -import java.io.InputStream; |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.IOException; |
10 | 14 | import java.io.ObjectInputStream; |
| 15 | +import java.util.Base64; |
11 | 16 |
|
12 | 17 | /** |
13 | | - * @author JoyChou ([email protected]) |
14 | | - * @Date 2018年06月14日 |
15 | | - * @Desc 该应用必须有Commons-Collections包才能利用反序列化命令执行。 |
| 18 | + * Deserialize RCE using Commons-Collections gadget. |
| 19 | + * |
| 20 | + * @author JoyChou @2018-06-14 |
16 | 21 | */ |
17 | | - |
18 | | -@Controller |
| 22 | +@RestController |
19 | 23 | @RequestMapping("/deserialize") |
20 | 24 | public class Deserialize { |
21 | 25 |
|
22 | | - @RequestMapping("/test") |
23 | | - @ResponseBody |
24 | | - public static String deserialize_test(HttpServletRequest request) throws Exception{ |
25 | | - try { |
26 | | - InputStream iii = request.getInputStream(); |
27 | | - ObjectInputStream in = new ObjectInputStream(iii); |
28 | | - in.readObject(); // 触发漏洞 |
29 | | - in.close(); |
30 | | - return "test"; |
31 | | - }catch (Exception e){ |
32 | | - return "exception"; |
| 26 | + |
| 27 | + private static Logger logger= LoggerFactory.getLogger(Deserialize.class); |
| 28 | + |
| 29 | + /** |
| 30 | + * java -jar ysoserial.jar CommonsCollections5 "open -a Calculator" | base64 |
| 31 | + * Add the result to rememberMe cookie. |
| 32 | + * |
| 33 | + * http://localhost:8080/deserialize/rememberMe/vul |
| 34 | + */ |
| 35 | + @RequestMapping("/rememberMe/vul") |
| 36 | + public static String rememberMeVul(HttpServletRequest request) |
| 37 | + throws IOException, ClassNotFoundException { |
| 38 | + |
| 39 | + Cookie[] cookies = request.getCookies(); |
| 40 | + String rememberMe = ""; |
| 41 | + |
| 42 | + if (null == cookies) { |
| 43 | + logger.info("No cookies."); |
| 44 | + } else { |
| 45 | + for (Cookie cookie : cookies) { |
| 46 | + if ( cookie.getName().equals("rememberMe") ) { |
| 47 | + rememberMe = cookie.getValue(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (StringUtils.isBlank(rememberMe) ) { |
| 53 | + return "No rememberMe cookie. Right?"; |
33 | 54 | } |
| 55 | + |
| 56 | + byte[] decoded = Base64.getDecoder().decode(rememberMe); |
| 57 | + ByteArrayInputStream bytes = new ByteArrayInputStream(decoded); |
| 58 | + ObjectInputStream in = new ObjectInputStream(bytes); |
| 59 | + in.readObject(); |
| 60 | + in.close(); |
| 61 | + |
| 62 | + return "Are u ok?"; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check deserialize class using black list. |
| 67 | + * |
| 68 | + * http://localhost:8080/deserialize/rememberMe/security |
| 69 | + */ |
| 70 | + @RequestMapping("/rememberMe/security") |
| 71 | + public static String rememberMeBlackClassCheck(HttpServletRequest request) |
| 72 | + throws IOException, ClassNotFoundException { |
| 73 | + |
| 74 | + Cookie[] cookies = request.getCookies(); |
| 75 | + String rememberMe = ""; |
| 76 | + |
| 77 | + if (null == cookies) { |
| 78 | + logger.info("No cookies in /rememberMe/security"); |
| 79 | + } else { |
| 80 | + for (Cookie cookie : cookies) { |
| 81 | + if ( cookie.getName().equals("rememberMe") ) { |
| 82 | + rememberMe = cookie.getValue(); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (StringUtils.isBlank(rememberMe) ) { |
| 88 | + return "No rememberMe cookie. Right?"; |
| 89 | + } |
| 90 | + |
| 91 | + byte[] decoded = Base64.getDecoder().decode(rememberMe); |
| 92 | + ByteArrayInputStream bytes = new ByteArrayInputStream(decoded); |
| 93 | + AntObjectInputStream in = new AntObjectInputStream(bytes); |
| 94 | + in.readObject(); |
| 95 | + in.close(); |
| 96 | + |
| 97 | + return "I'm very OK."; |
34 | 98 | } |
35 | 99 | } |
0 commit comments