44import com .squareup .okhttp .OkHttpClient ;
55import org .apache .commons .httpclient .HttpClient ;
66import org .apache .commons .httpclient .methods .GetMethod ;
7+ import org .apache .commons .io .IOUtils ;
78import org .apache .http .HttpResponse ;
89import org .apache .http .HttpStatus ;
910import org .apache .http .client .fluent .Request ;
1011import org .apache .http .client .methods .HttpGet ;
1112import org .apache .http .impl .client .CloseableHttpClient ;
1213import org .apache .http .impl .client .HttpClients ;
1314import org .joychou .security .SecurityUtil ;
14- import org .springframework .stereotype .Controller ;
15+ import org .jsoup .Jsoup ;
16+ import org .jsoup .nodes .Document ;
17+ import org .slf4j .Logger ;
18+ import org .slf4j .LoggerFactory ;
1519import org .springframework .web .bind .annotation .RequestMapping ;
20+ import org .springframework .web .bind .annotation .RequestParam ;
1621import org .springframework .web .bind .annotation .ResponseBody ;
22+ import org .springframework .web .bind .annotation .RestController ;
1723
1824
1925import javax .imageio .ImageIO ;
2026import javax .servlet .http .HttpServletRequest ;
2127import javax .servlet .http .HttpServletResponse ;
2228import java .io .*;
23- import java .net .URL ;
24- import java .net .URLConnection ;
25- import java .net .HttpURLConnection ;
29+ import java .net .*;
2630
2731
2832/**
3135 * @desc Java ssrf vuls code.
3236 */
3337
34- @ Controller
38+ @ RestController
3539@ RequestMapping ("/ssrf" )
3640public class SSRF {
3741
42+ private static Logger logger = LoggerFactory .getLogger (SSRF .class );
43+
3844 @ RequestMapping ("/urlConnection" )
39- @ ResponseBody
4045 public static String ssrf_URLConnection (HttpServletRequest request )
4146 {
4247 try {
@@ -169,9 +174,7 @@ public static void ssrf_okhttp(HttpServletRequest request) throws IOException {
169174 */
170175 @ RequestMapping ("/HttpClient" )
171176 @ ResponseBody
172- public static String ssrf_HttpClient (HttpServletRequest request ) {
173-
174- String url = request .getParameter ("url" );
177+ public static String ssrf_HttpClient (@ RequestParam String url ) {
175178 CloseableHttpClient client = HttpClients .createDefault ();
176179 HttpGet httpGet = new HttpGet (url );
177180 try {
@@ -193,26 +196,18 @@ public static String ssrf_HttpClient(HttpServletRequest request) {
193196
194197 /**
195198 * Safe code.
196- * http://localhost:8080/ssrf/commonsHttpClient?url=http://www.baidu.com
199+ * http://localhost:8080/ssrf/commonsHttpClient/sec ?url=http://www.baidu.com
197200 *
198201 */
199- @ RequestMapping ("/commonsHttpClient" )
202+ @ RequestMapping ("/commonsHttpClient/sec " )
200203 @ ResponseBody
201- public static String commonsHttpClient (HttpServletRequest request ) {
202-
203- String url = request .getParameter ("url" );
204-
205- // Security check
204+ public static String commonsHttpClient (@ RequestParam String url ) {
206205 if (!SecurityUtil .checkSSRFWithoutRedirect (url )) {
207206 return "Bad man. I got u." ;
208207 }
209- // Create an instance of HttpClient.
210- HttpClient client = new HttpClient ();
211208
212- // Create a method instance.
209+ HttpClient client = new HttpClient ();
213210 GetMethod method = new GetMethod (url );
214-
215- // forbid 302 redirection
216211 method .setFollowRedirects (false );
217212
218213 try {
@@ -238,19 +233,63 @@ public static String commonsHttpClient(HttpServletRequest request) {
238233
239234 }
240235
236+ /**
237+ * jsoup是一款Java的HTML解析器,可直接解析某个URL地址、HTML文本内容。
238+ * http://localhost:8080/ssrf/Jsoup?url=http://www.baidu.com
239+ *
240+ */
241+ @ RequestMapping ("/Jsoup" )
242+ @ ResponseBody
243+ public static String Jsoup (@ RequestParam String url ) {
244+ try {
245+ Document doc = Jsoup .connect (url )
246+ .userAgent (
247+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) "
248+ + "Chrome/64.0.3282.167 Safari/537.36" )
249+ .timeout (3000 )
250+ .cookie ("name" , "joychou" ) // request请求带的cookie
251+ .followRedirects (false )
252+ .execute ().parse ();
253+ } catch (MalformedURLException e ) {
254+ return "exception: " + e .toString ();
255+ } catch (Exception e ) {
256+ return "exception: " + e .toString ();
257+ }
258+
259+ return "Jsoup ssrf" ;
260+ }
261+
262+
263+ /**
264+ * 用途:IOUtils可远程获取URL图片
265+ * 默认重定向:是
266+ * 封装类:URLConnection
267+ * http://localhost:8080/ssrf/IOUtils?url=http://www.baidu.com
268+ */
269+ @ RequestMapping ("/IOUtils" )
270+ public static String IOUtils (@ RequestParam String url ) {
271+ try {
272+ // IOUtils.toByteArray内部用URLConnection进行了封装
273+ byte [] b = IOUtils .toByteArray (URI .create (url ));
274+ } catch (Exception e ) {
275+ return "exception: " + e .toString ();
276+ }
277+
278+ return "IOUtils ssrf" ;
279+ }
280+
241281
242282 /**
243283 * Safe code.
244- * http://localhost:8080/ssrf/ImageIO_safe ?url=http://www.baidu.com
284+ * http://localhost:8080/ssrf/ImageIO/sec ?url=http://www.baidu.com
245285 *
246286 */
247- @ RequestMapping ("/ImageIO_safe" )
248- @ ResponseBody
249- public static String ssrf_ImageIO_safecode (HttpServletRequest request ) {
250- String url = request .getParameter ("url" );
287+ @ RequestMapping ("/ImageIO/sec" )
288+ public static String ImageIOSec (@ RequestParam String url ) {
251289 try {
252290 URL u = new URL (url );
253291 if (!SecurityUtil .checkSSRF (url )) {
292+ logger .error ("[-] SSRF check failed. Original Url: " + url );
254293 return "SSRF check failed." ;
255294 }
256295 ImageIO .read (u ); // send request
0 commit comments