2323
2424
2525import javax .imageio .ImageIO ;
26- import javax .servlet .http .HttpServletRequest ;
2726import javax .servlet .http .HttpServletResponse ;
2827import java .io .*;
2928import java .net .*;
3029
3130
3231/**
33- * @author JoyChou ([email protected] ) 34- * @date 2017.12.28
35- * @desc Java ssrf vuls code.
32+ * Java SSRF vuln or security code.
33+ *
34+ * @author JoyChou @2017-12-28
3635 */
3736
3837@ RestController
@@ -42,62 +41,59 @@ public class SSRF {
4241 private static Logger logger = LoggerFactory .getLogger (SSRF .class );
4342
4443 @ RequestMapping ("/urlConnection" )
45- public static String ssrf_URLConnection (HttpServletRequest request )
44+ public static String ssrf_URLConnection (@ RequestParam String url )
4645 {
4746 try {
48- String url = request .getParameter ("url" );
4947 URL u = new URL (url );
5048 URLConnection urlConnection = u .openConnection ();
5149 BufferedReader in = new BufferedReader (new InputStreamReader (urlConnection .getInputStream ())); //send request
5250 String inputLine ;
53- StringBuffer html = new StringBuffer ();
51+ StringBuilder html = new StringBuilder ();
5452
5553 while ((inputLine = in .readLine ()) != null ) {
5654 html .append (inputLine );
5755 }
5856 in .close ();
5957 return html .toString ();
6058 }catch (Exception e ) {
61- e . printStackTrace ( );
59+ logger . error ( e . toString () );
6260 return "fail" ;
6361 }
6462 }
6563
6664
6765 @ RequestMapping ("/HttpURLConnection" )
6866 @ ResponseBody
69- public static String ssrf_httpURLConnection (HttpServletRequest request )
67+ public static String ssrf_httpURLConnection (@ RequestParam String url )
7068 {
7169 try {
72- String url = request .getParameter ("url" );
7370 URL u = new URL (url );
7471 URLConnection urlConnection = u .openConnection ();
7572 HttpURLConnection httpUrl = (HttpURLConnection )urlConnection ;
7673 BufferedReader in = new BufferedReader (new InputStreamReader (httpUrl .getInputStream ())); //send request
7774 String inputLine ;
78- StringBuffer html = new StringBuffer ();
75+ StringBuilder html = new StringBuilder ();
7976
8077 while ((inputLine = in .readLine ()) != null ) {
8178 html .append (inputLine );
8279 }
8380 in .close ();
8481 return html .toString ();
8582 }catch (Exception e ) {
86- e . printStackTrace ( );
83+ logger . error ( e . toString () );
8784 return "fail" ;
8885 }
8986 }
9087
9188
9289 @ RequestMapping ("/Request" )
9390 @ ResponseBody
94- public static String ssrf_Request (HttpServletRequest request )
91+ public static String ssrf_Request (@ RequestParam String url )
9592 {
9693 try {
97- String url = request .getParameter ("url" );
9894 return Request .Get (url ).execute ().returnContent ().toString ();
9995 }catch (Exception e ) {
100- e . printStackTrace ( );
96+ logger . error ( e . toString () );
10197 return "fail" ;
10298 }
10399 }
@@ -113,10 +109,9 @@ public static String ssrf_Request(HttpServletRequest request)
113109 */
114110 @ RequestMapping ("/openStream" )
115111 @ ResponseBody
116- public static void ssrf_openStream (HttpServletRequest request , HttpServletResponse response ) throws IOException {
112+ public static void ssrf_openStream (@ RequestParam String url , HttpServletResponse response ) throws IOException {
117113 InputStream inputStream = null ;
118114 OutputStream outputStream = null ;
119- String url = request .getParameter ("url" );
120115 try {
121116 String downLoadImgFileName = Files .getNameWithoutExtension (url ) + "." + Files .getFileExtension (url );
122117 // download
@@ -132,7 +127,7 @@ public static void ssrf_openStream (HttpServletRequest request, HttpServletRespo
132127 }
133128
134129 }catch (Exception e ) {
135- e . printStackTrace ( );
130+ logger . error ( e . toString () );
136131 }finally {
137132 if (inputStream != null ) {
138133 inputStream .close ();
@@ -147,20 +142,19 @@ public static void ssrf_openStream (HttpServletRequest request, HttpServletRespo
147142
148143 @ RequestMapping ("/ImageIO" )
149144 @ ResponseBody
150- public static void ssrf_ImageIO (HttpServletRequest request ) {
151- String url = request .getParameter ("url" );
145+ public static void ssrf_ImageIO (@ RequestParam String url ) {
152146 try {
153147 URL u = new URL (url );
154148 ImageIO .read (u ); // send request
155149 } catch (Exception e ) {
150+ logger .error (e .toString ());
156151 }
157152 }
158153
159154
160155 @ RequestMapping ("/okhttp" )
161156 @ ResponseBody
162- public static void ssrf_okhttp (HttpServletRequest request ) throws IOException {
163- String url = request .getParameter ("url" );
157+ public static void ssrf_okhttp (@ RequestParam String url ) throws IOException {
164158 OkHttpClient client = new OkHttpClient ();
165159 com .squareup .okhttp .Request ok_http = new com .squareup .okhttp .Request .Builder ().url (url ).build ();
166160 client .newCall (ok_http ).execute ();
@@ -180,8 +174,8 @@ public static String ssrf_HttpClient(@RequestParam String url) {
180174 try {
181175 HttpResponse httpResponse = client .execute (httpGet ); // send request
182176 BufferedReader rd = new BufferedReader (new InputStreamReader (httpResponse .getEntity ().getContent ()));
183- StringBuffer result = new StringBuffer ();
184- String line = "" ;
177+ StringBuilder result = new StringBuilder ();
178+ String line = null ;
185179 while ((line = rd .readLine ()) != null ) {
186180 result .append (line );
187181 }
@@ -236,8 +230,8 @@ public static String commonsHttpClient(@RequestParam String url) {
236230
237231 /**
238232 * jsoup是一款Java的HTML解析器,可直接解析某个URL地址、HTML文本内容。
239- * http://localhost:8080/ssrf/Jsoup?url=http://www.baidu.com
240233 *
234+ * http://localhost:8080/ssrf/Jsoup?url=http://www.baidu.com
241235 */
242236 @ RequestMapping ("/Jsoup" )
243237 @ ResponseBody
@@ -251,9 +245,11 @@ public static String Jsoup(@RequestParam String url) {
251245 .cookie ("name" , "joychou" ) // request请求带的cookie
252246 .followRedirects (false )
253247 .execute ().parse ();
248+ logger .info (doc .html ());
254249 } catch (MalformedURLException e ) {
255250 return "exception: " + e .toString ();
256- } catch (Exception e ) {
251+ } catch (IOException e ) {
252+ logger .error (e .toString ());
257253 return "exception: " + e .toString ();
258254 }
259255
@@ -271,7 +267,7 @@ public static String Jsoup(@RequestParam String url) {
271267 public static String IOUtils (@ RequestParam String url ) {
272268 try {
273269 // IOUtils.toByteArray内部用URLConnection进行了封装
274- byte [] b = IOUtils .toByteArray (URI .create (url ));
270+ IOUtils .toByteArray (URI .create (url ));
275271 } catch (Exception e ) {
276272 return "exception: " + e .toString ();
277273 }
0 commit comments