- 访问内部资源
假设有一个服务器端的应用程序,允许用户传入一个 URL,然后使用 RestTemplate 发起 HTTP GET 请求来获取数据。如果没有对用户传入的 URL 进行有效的验证和限制,可能会导致访问到服务器本不应该访问的内部资源,比如数据库。
java
复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ApiController {
private final RestTemplate restTemplate;
public ApiController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/fetchData")
public String fetchData(@RequestParam("url") String url) {
// 假设用户传入了一个恶意构造的 URL,例如:http://localhost:3306/dump_all_data
// 如果服务器未对 URL 进行适当的验证和限制,这可能导致访问内部数据库或其他敏感资源
String result = restTemplate.getForObject(url, String.class);
return "Fetched data: " + result;
}
}
在上述示例中,如果用户传入 http://localhost:3306/dump_all_data,服务器将尝试访问本地主机(localhost)的 3306 端口,这通常是 MySQL 数据库的默认端口。如果服务器上的应用程序没有对 URL 进行充分验证,那么可能会导致该应用程序尝试从数据库中获取所有数据,这是非常危险的行为,因为这可能泄露敏感信息。
- 访问外部服务
另一个例子是,用户可以通过 SSRF 漏洞,使服务器发起请求访问外部的恶意服务或网站。
java
复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ApiController {
private final RestTemplate restTemplate;
public ApiController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/fetchExternal")
public String fetchExternal(@RequestParam("url") String url) {
// 假设用户传入了一个恶意构造的 URL,例如:http://malicious-website.com/malware_payload
// 如果服务器未对 URL 进行适当的验证和限制,这可能导致服务器发起恶意请求
String result = restTemplate.getForObject(url, String.class);
return "External response: " + result;
}
}
在这个示例中,如果用户传入了 http://malicious-website.com/malware_payload,服务器将尝试访问名为 malicious-website.com 的网站,并下载可能包含恶意软件的 payload。这种行为对服务器和其他网络上的系统都是危险的,因为恶意服务可能用来进行进一步的攻击,例如分发恶意软件或者窃取用户信息。
- 绕过防火墙和安全控制
通过 SSRF 漏洞,攻击者可以绕过企业内部的防火墙和其他安全控制。
java
复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ApiController {
private final RestTemplate restTemplate;
public ApiController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/bypassFirewall")
public String bypassFirewall(@RequestParam("url") String url) {
// 假设用户传入了一个恶意构造的 URL,例如:http://internal-service/api/admin/deleteUser
// 如果服务器未对 URL 进行适当的验证和限制,这可能导致绕过企业的防火墙和安全控制
String result = restTemplate.getForObject(url, String.class);
return "Bypassed firewall: " + result;
}
}
在这个示例中,如果用户传入 http://internal-service/api/admin/deleteUser,服务器可能会试图访问内部服务的管理员接口,并执行删除用户的操作。如果服务器上的应用程序没有正确验证 URL,并且未实施适当的访问控制,攻击者可能成功绕过企业防火墙和安全策略,导致未经授权的访问和操作。
- 利用内部资源和应用程序
攻击者还可以利用 SSRF 漏洞来消耗服务器的资源或者执行未经授权的操作。
java
复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ApiController {
private final RestTemplate restTemplate;
public ApiController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/exploitInternal")
public String exploitInternal(@RequestParam("url") String url) {
// 假设用户传入了一个恶意构造的 URL,例如:http://localhost:8080/admin/configure?param=evil_value
// 如果服务器未对 URL 进行适当的验证和限制,这可能导致未经授权的操作
String result = restTemplate.getForObject(url, String.class);
return "Exploited internal resource: " + result;
}
}
在这个示例中,如果用户传入了 http://localhost:8080/admin/configure?param=evil_value,服务器可能会试图访问本地主机的 8080 端口,并尝试以管理员权限配置系统。如果服务器未对 URL 进行适当验证和限制,攻击者可能成功地执行未经授权的操作,例如修改系统配置或执行系统命令。