From 8f53a5ccd008cfaf7238e8eb7db21df2e77b27f2 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 12 Jan 2022 12:20:32 -0500 Subject: [PATCH] Agent: Fix min_range in get_free_tcp_port() get_free_tcp_port() effectively ignored the min_range parameter by using min(1, min_range). This meant that min_range was always 1 (unless a negative value was passed). As ports lower than 1024 are privileged on Linux, this lead to the agent trying to bind to ports that it did not have permission to. By using max(1, min_range), We insure that min_range is always at least 1, but will still use the provided parameter (1024 by default). --- monkey/infection_monkey/network/info.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/infection_monkey/network/info.py b/monkey/infection_monkey/network/info.py index 7f740eeb2..e262feb19 100644 --- a/monkey/infection_monkey/network/info.py +++ b/monkey/infection_monkey/network/info.py @@ -108,14 +108,14 @@ else: return routes -def get_free_tcp_port(min_range=1000, max_range=65535): - start_range = min(1, min_range) +def get_free_tcp_port(min_range=1024, max_range=65535): + min_range = max(1, min_range) max_range = min(65535, max_range) in_use = [conn.laddr[1] for conn in psutil.net_connections()] for i in range(min_range, max_range): - port = randint(start_range, max_range) + port = randint(min_range, max_range) if port not in in_use: return port