Skip to content

Commit

Permalink
Agent: Fix min_range in get_free_tcp_port()
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mssalvatore committed Jan 12, 2022
1 parent 16219b7 commit 8f53a5c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions monkey/infection_monkey/network/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ def get_routes(): # based on scapy implementation for route parsing
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
Expand Down

0 comments on commit 8f53a5c

Please sign in to comment.