@@ -23,7 +23,16 @@ def inspect_getaddrinfo_result(dns_results, hostname, port):
2323 logger .debug ("Hostname %s is actually an IP address, ignoring" , hostname )
2424 return
2525
26- if not inspect_dns_results (dns_results , hostname ):
26+ ip_addresses = extract_ip_array_from_results (dns_results )
27+ stored_ssrf_findings = resolves_to_imds_ip (ip_addresses , hostname )
28+ if stored_ssrf_findings :
29+ return {
30+ "module" : "socket" ,
31+ "operation" : "socket.getaddrinfo" ,
32+ "kind" : "ssrf" ,
33+ }
34+
35+ if not ip_addresses_contain_private_ip (ip_addresses ):
2736 return
2837
2938 context = get_current_context ()
@@ -60,18 +69,6 @@ def get_metadata_for_ssrf_attack(hostname, port):
6069 return {"hostname" : hostname }
6170
6271
63- def inspect_dns_results (dns_results , hostname ):
64- """
65- Blocks stored SSRF attack that target IMDS IP addresses and returns True
66- if a private_ip is present.
67- This function gets called by inspect_getaddrinfo_result after parsing the hostname.
68- """
69- ip_addresses = extract_ip_array_from_results (dns_results )
70- if resolves_to_imds_ip (ip_addresses , hostname ):
71- # An attacker could have stored a hostname in a database that points to an IMDS IP address
72- # We don't check if the user input contains the hostname because there's no context
73- if is_blocking_enabled ():
74- raise AikidoSSRF ()
75-
76- private_ip = next ((ip for ip in ip_addresses if is_private_ip (ip )), None )
77- return private_ip
72+ def ip_addresses_contain_private_ip (ip_addresses ) -> bool :
73+ has_private_ip = next ((ip for ip in ip_addresses if is_private_ip (ip )), None )
74+ return has_private_ip
0 commit comments