4
4
import org .dom4j .io .SAXReader ;
5
5
import org .slf4j .Logger ;
6
6
import org .slf4j .LoggerFactory ;
7
+ import org .springframework .data .web .ProjectedPayload ;
8
+ import org .springframework .http .HttpEntity ;
9
+ import org .springframework .http .ResponseEntity ;
7
10
import org .springframework .web .bind .annotation .*;
8
11
9
12
import javax .servlet .http .HttpServletRequest ;
27
30
import org .apache .commons .digester3 .Digester ;
28
31
import org .jdom2 .input .SAXBuilder ;
29
32
import org .joychou .util .WebUtils ;
33
+ import org .xmlbeam .annotation .XBRead ;
30
34
31
35
/**
32
36
* Java xxe vuln and security code.
38
42
@ RequestMapping ("/xxe" )
39
43
public class XXE {
40
44
41
- private static Logger logger = LoggerFactory .getLogger (XXE .class );
42
- private static String EXCEPT = "xxe except" ;
45
+ private static final Logger logger = LoggerFactory .getLogger (XXE .class );
46
+ private static final String EXCEPT = "xxe except" ;
43
47
44
48
@ PostMapping ("/xmlReader/vuln" )
45
49
public String xmlReaderVuln (HttpServletRequest request ) {
@@ -226,16 +230,15 @@ public String DigesterSec(HttpServletRequest request) {
226
230
}
227
231
228
232
229
- // 有回显
230
- @ RequestMapping (value = "/DocumentBuilder/vuln01" , method = RequestMethod .POST )
233
+ /**
234
+ * Use request.getInputStream to support UTF16 encoding.
235
+ */
236
+ @ RequestMapping (value = "/DocumentBuilder/vuln" , method = RequestMethod .POST )
231
237
public String DocumentBuilderVuln01 (HttpServletRequest request ) {
232
238
try {
233
- String body = WebUtils .getRequestBody (request );
234
- logger .info (body );
235
239
DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
236
240
DocumentBuilder db = dbf .newDocumentBuilder ();
237
- StringReader sr = new StringReader (body );
238
- InputSource is = new InputSource (sr );
241
+ InputSource is = new InputSource (request .getInputStream ());
239
242
Document document = db .parse (is ); // parse xml
240
243
241
244
// 遍历xml节点name和value
@@ -249,7 +252,6 @@ public String DocumentBuilderVuln01(HttpServletRequest request) {
249
252
buf .append (String .format ("%s: %s\n " , node .getNodeName (), node .getTextContent ()));
250
253
}
251
254
}
252
- sr .close ();
253
255
return buf .toString ();
254
256
} catch (Exception e ) {
255
257
e .printStackTrace ();
@@ -258,43 +260,6 @@ public String DocumentBuilderVuln01(HttpServletRequest request) {
258
260
}
259
261
}
260
262
261
-
262
- // 有回显
263
- @ RequestMapping (value = "/DocumentBuilder/vuln02" , method = RequestMethod .POST )
264
- public String DocumentBuilderVuln02 (HttpServletRequest request ) {
265
- try {
266
- String body = WebUtils .getRequestBody (request );
267
- logger .info (body );
268
-
269
- DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
270
- DocumentBuilder db = dbf .newDocumentBuilder ();
271
- StringReader sr = new StringReader (body );
272
- InputSource is = new InputSource (sr );
273
- Document document = db .parse (is ); // parse xml
274
-
275
- // 遍历xml节点name和value
276
- StringBuilder result = new StringBuilder ();
277
- NodeList rootNodeList = document .getChildNodes ();
278
- for (int i = 0 ; i < rootNodeList .getLength (); i ++) {
279
- Node rootNode = rootNodeList .item (i );
280
- NodeList child = rootNode .getChildNodes ();
281
- for (int j = 0 ; j < child .getLength (); j ++) {
282
- Node node = child .item (j );
283
- // 正常解析XML,需要判断是否是ELEMENT_NODE类型。否则会出现多余的的节点。
284
- if (child .item (j ).getNodeType () == Node .ELEMENT_NODE ) {
285
- result .append (String .format ("%s: %s\n " , node .getNodeName (), node .getFirstChild ()));
286
- }
287
- }
288
- }
289
- sr .close ();
290
- return result .toString ();
291
- } catch (Exception e ) {
292
- logger .error (e .toString ());
293
- return EXCEPT ;
294
- }
295
- }
296
-
297
-
298
263
@ RequestMapping (value = "/DocumentBuilder/Sec" , method = RequestMethod .POST )
299
264
public String DocumentBuilderSec (HttpServletRequest request ) {
300
265
try {
@@ -447,6 +412,31 @@ private static void response(NodeList rootNodeList){
447
412
}
448
413
}
449
414
415
+ /**
416
+ * Receiving POST requests supporting both JSON and XML.
417
+ * CVE-2018-1259
418
+ */
419
+ @ PostMapping (value = "/xmlbeam/vuln" )
420
+ HttpEntity <String > post (@ RequestBody UserPayload user ) {
421
+ try {
422
+ logger .info (user .toString ());
423
+ return ResponseEntity .ok (String .format ("hello, %s!" , user .getUserName ()));
424
+ }catch (Exception e ){
425
+ e .printStackTrace ();
426
+ return ResponseEntity .ok ("error" );
427
+ }
428
+ }
429
+
430
+ /**
431
+ * The projection interface using XPath and JSON Path expression to selectively pick elements from the payload.
432
+ */
433
+ @ ProjectedPayload
434
+ public interface UserPayload {
435
+ @ XBRead ("//userName" )
436
+ String getUserName ();
437
+ }
438
+
439
+
450
440
public static void main (String [] args ) {
451
441
}
452
442
0 commit comments