Skip to content

Commit 4c21c97

Browse files
committed
bypass using URL class to getHost
1 parent af76c38 commit 4c21c97

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

src/main/java/org/joychou/controller/URLWhiteList.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.web.bind.annotation.ResponseBody;
88

99
import javax.servlet.http.HttpServletRequest;
10+
import java.net.URI;
1011
import java.net.URL;
1112
import java.util.regex.Matcher;
1213
import java.util.regex.Pattern;
@@ -89,17 +90,21 @@ public String indexOf(HttpServletRequest request) throws Exception{
8990
}
9091
}
9192

92-
// 安全代码
93-
@RequestMapping("/seccode")
93+
// URL类getHost方法被绕过造成的安全问题
94+
// 绕过姿势:http://localhost:8080/url/seccode?url=http://www.taobao.com%[email protected]/, URL类getHost为joychou.com
95+
// 直接访问http://www.taobao.com#@joychou.com/,浏览器请求的是www.taobao.com
96+
@RequestMapping("/url")
9497
@ResponseBody
95-
public String seccode(HttpServletRequest request) throws Exception{
98+
public String urlVul(HttpServletRequest request) throws Exception{
9699
String url = request.getParameter("url");
100+
System.out.println("url: " + url);
97101
URL u = new URL(url);
98102
// 判断是否是http(s)协议
99103
if (!u.getProtocol().startsWith("http") && !u.getProtocol().startsWith("https")) {
100104
return "URL is not http or https";
101105
}
102106
String host = u.getHost().toLowerCase();
107+
System.out.println("host: " + host);
103108
// 如果非顶级域名后缀会报错
104109
String rootDomain = InternetDomainName.from(host).topPrivateDomain().toString();
105110

@@ -111,4 +116,29 @@ public String seccode(HttpServletRequest request) throws Exception{
111116
}
112117

113118

119+
// 安全代码
120+
@RequestMapping("/seccode")
121+
@ResponseBody
122+
public String seccode(HttpServletRequest request) throws Exception{
123+
String url = request.getParameter("url");
124+
System.out.println("url: " + url);
125+
URI uri = new URI(url);
126+
URL u = new URL(url);
127+
// 判断是否是http(s)协议
128+
if (!u.getProtocol().startsWith("http") && !u.getProtocol().startsWith("https")) {
129+
return "URL is not http or https";
130+
}
131+
// 使用uri获取host
132+
String host = uri.getHost().toLowerCase();
133+
System.out.println("host: " + host);
134+
135+
// 如果非顶级域名后缀会报错
136+
String rootDomain = InternetDomainName.from(host).topPrivateDomain().toString();
137+
138+
if (rootDomain.equals(urlwhitelist)) {
139+
return "URL is legal";
140+
} else {
141+
return "URL is illegal";
142+
}
143+
}
114144
}

src/main/java/org/joychou/utils/Security.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.joychou.utils;
22

33
import com.google.common.net.InternetDomainName;
4+
5+
import java.net.URI;
46
import java.net.URL;
57

68
public class Security {
@@ -11,12 +13,15 @@ public class Security {
1113
public static Boolean checkSafeUrl(String url, String[] urlwhitelist){
1214
try{
1315
URL u = new URL(url);
16+
URI uri = new URI(url);
1417
// 判断是否是http(s)协议
1518
if (!u.getProtocol().startsWith("http") && !u.getProtocol().startsWith("https")) {
1619
System.out.println("The protocol of url is not http or https.");
1720
return false;
1821
}
19-
String host = u.getHost().toLowerCase();
22+
// 使用uri获取host
23+
String host = uri.getHost().toLowerCase();
24+
2025
// 如果非顶级域名后缀会报错
2126
String rootDomain = InternetDomainName.from(host).topPrivateDomain().toString();
2227

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
# Spring Boot Actuator Vulnerable Config
3-
management.security.enabled=false
3+
management.security.enabled=false
4+
logging.config=classpath:logback-online.xml
File renamed without changes.

0 commit comments

Comments
 (0)