Skip to content

Commit 3162f63

Browse files
committed
Change socket.py to @before so we can block outbound
1 parent 7d72f58 commit 3162f63

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

aikido_zen/sinks/socket.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,37 @@
44

55
from aikido_zen.helpers.get_argument import get_argument
66
from aikido_zen.helpers.register_call import register_call
7-
from aikido_zen.sinks import on_import, patch_function, after
7+
from aikido_zen.sinks import on_import, patch_function, before
88
from aikido_zen.vulnerabilities import run_vulnerability_scan
9+
from aikido_zen.thread.thread_cache import get_cache
10+
from aikido_zen.errors import AikidoSSRF
911

1012

11-
@after
12-
def _getaddrinfo(func, instance, args, kwargs, return_value):
13+
@before
14+
def _getaddrinfo(func, instance, args, kwargs):
1315
host = get_argument(args, kwargs, 0, "host")
1416
port = get_argument(args, kwargs, 1, "port")
1517

18+
# Check if we should block this outgoing request based on configuration
19+
cache = get_cache()
20+
if cache and cache.config:
21+
if cache.config.should_block_outgoing_request(host):
22+
raise AikidoSSRF(
23+
f"Zen has blocked an outbound connection: socket.getaddrinfo to {host}"
24+
)
25+
1626
op = "socket.getaddrinfo"
1727
register_call(op, "outgoing_http_op")
1828

29+
# Call the original function to get the return value for vulnerability scanning
30+
return_value = func(*args, **kwargs)
31+
32+
# Run vulnerability scan after getting the result
1933
arguments = (return_value, host, port) # return_value = dns response
2034
run_vulnerability_scan(kind="ssrf", op=op, args=arguments)
2135

36+
return return_value
37+
2238

2339
@on_import("socket")
2440
def patch(m):

aikido_zen/sinks/tests/socket_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_patched_socket_module():
1818
import aikido_zen.sinks.socket
1919

2020
# Manually apply the patch
21-
patch_function(socket, "getaddrinfo", aikido_zen.sinks.socket._getaddrinfo_wrapper)
21+
patch_function(socket, "getaddrinfo", aikido_zen.sinks.socket._getaddrinfo)
2222

2323
return socket
2424

0 commit comments

Comments
 (0)