URLConnect
这个是一个可以向外发送请求的类,通过这个类可以造成ssrf 漏洞
支持的协议有
1
| file ftp mailto http https jar netdoc gopher
|
简单使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package org.example;
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection;
public class URLConnectionLearn { public static void main(String[] args) { try { URL url = new URL("https://www.baidu.com"); URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000);
urlConnection.connect();
urlConnection.getHeaderFields().forEach((key, value) -> { System.out.println(key + ": " + value); });
System.out.println(urlConnection.getInputStream());
StringBuilder resp = new StringBuilder();
BufferedReader reader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream())); String line; while ((line = reader.readLine()) != null) { resp.append(line); } System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } }
|
ssrf
SSRF漏洞形成的原因大部分是因为服务端提供了可以从其他服务器获取资源的功能,然而并没有对用户的输入以及发起请求的url进行过滤&限制,从而导致了ssrf的漏洞。
如果上面代码中这个时候url 是可控的那么,就将出现ssrf

如果将url.openConnection();
切换成 HttpURLConnection 那么就只支持http 协议了
1 2 3 4
| HttpURLConnection HttpClient Request okhttp
|
如果传入了携带端口的,如果端口存在就会将网页的信息输出出来
如果是非web端口的服务,则会爆出Invalid Http response
,Connection reset
,Read timed out
异常。
如果能将此异常抛出来,那么就可以对内网所有服务端口进行探测。

如果是不存在的服务的

总之会报错,或者是其他区别
其他
java 中默认对http/https 做的事情
- 默认启用了ntlm 认证
- 默认跟随跳转
java serf 的利用场景
- 利用file协议读取文件内容(仅限使用
URLConnection|URL
发起的请求)
- 利用http 进行内网web服务端口探测
- 利用http 进行内网非web服务端口探测(如果将异常抛出来的情况下)
- 利用http进行ntlmrelay攻击(仅限
HttpURLConnection
或者二次包装HttpURLConnection
并未复写AuthenticationInfo
方法的对象)