-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_route.py
194 lines (160 loc) · 5.5 KB
/
find_route.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import csv
import os
import sys
import heapq
from collections import defaultdict
from ipaddress import ip_network, ip_address
# Known sub-1024 ports dictionary
SUB_1024_PORTS = {
20: 'FTP',
21: 'FTP',
22: 'SSH',
23: 'Telnet',
25: 'SMTP',
53: 'DNS',
67: 'DHCP',
68: 'DHCP',
69: 'TFTP',
80: 'HTTP',
110: 'POP3',
123: 'NTP',
137: 'NetBIOS',
138: 'NetBIOS',
139: 'NetBIOS',
143: 'IMAP',
161: 'SNMP',
162: 'SNMP',
179: 'BGP',
194: 'IRC',
443: 'HTTPS',
445: 'SMB',
465: 'SMTPS',
514: 'Syslog',
515: 'LPD',
587: 'Submission',
636: 'LDAPS',
873: 'Rsync',
993: 'IMAPS',
995: 'POP3S',
1080: 'SOCKS',
1194: 'OpenVPN',
1433: 'MSSQL',
1434: 'MSSQL',
1521: 'ORACLE',
1701: 'L2TP',
1723: 'PPTP',
3306: 'MySQL',
3389: 'RDP',
5060: 'SIP',
5061: 'SIP-TLS',
}
def load_graph(file_path):
graph = defaultdict(list)
with open(file_path, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
src = row['src_ip']
dst = row['dest_ip']
port = row['dest_port']
weight = float(row['dest_port_weight'])
graph[src].append((dst, port, weight))
return graph
def is_within_subnet(ip, subnet):
return ip_address(ip) in ip_network(subnet)
def dijkstra(graph, start_nodes, end_subnet=None, end_ip=None):
queue = [(0, start, [(start, None, 0)]) for start in start_nodes]
seen = set()
paths = []
while queue:
(cost, node, path) = heapq.heappop(queue)
if node in seen:
continue
seen.add(node)
if end_subnet and is_within_subnet(node, end_subnet):
paths.append(path)
continue
if end_ip and node == end_ip:
paths.append(path)
continue
for dst, port, weight in graph[node]:
if dst not in seen:
new_path = path + [(dst, port, weight)]
heapq.heappush(queue, (cost + weight, dst, new_path))
return paths
def parse_port(port, parse_port_flag):
if parse_port_flag == '1' and port and int(port) in SUB_1024_PORTS:
return SUB_1024_PORTS[int(port)]
return f"TCP:{port}" if port else ''
def main():
if len(sys.argv) < 5 or len(sys.argv) > 7:
print(
"Usage: find_route.py <delete_flag> <input_csv> <start_ip_or_subnet> <end_ip_or_subnet> [<pathColoring>] [<parsePort>]")
return
delete_flag = sys.argv[1]
file_path = sys.argv[2]
start_arg = sys.argv[3]
end_arg = sys.argv[4]
path_coloring = sys.argv[5] if len(sys.argv) >= 6 else None
parse_port_flag = sys.argv[6] if len(sys.argv) == 7 else '0'
if not os.path.isfile(file_path):
print(f"File {file_path} does not exist.")
return
graph = load_graph(file_path)
# Determine if start_arg and end_arg are IPs or subnets
start_nodes = []
start_subnet_str = start_arg
end_subnet_str = end_arg
end_subnet = None
end_ip = None
try:
start_subnet = ip_network(start_arg)
start_nodes = [ip for ip in graph.keys() if is_within_subnet(ip, start_subnet)]
start_subnet_str = str(start_subnet.network_address)
except ValueError:
start_nodes = [start_arg]
try:
end_subnet = ip_network(end_arg)
end_subnet_str = str(end_subnet.network_address)
except ValueError:
end_ip = end_arg
if not start_nodes:
print(f"No valid start IPs found in the subnet {start_arg}.")
return
if end_subnet is None and end_ip is None:
print(f"No valid end IPs or subnet found in the argument {end_arg}.")
return
paths = dijkstra(graph, start_nodes, end_subnet, end_ip)
if paths:
# Find the minimum number of nodes in paths
min_nodes = min(len(path) for path in paths)
for path in paths:
segments = []
full_path = [(start_subnet_str, None, 0)] + path + [(end_subnet_str, None, 0)]
total_nodes = len(full_path) - 2 # Subtract 2 for the start and end subnet
for i in range(len(full_path) - 1):
from_node, from_port, from_weight = full_path[i]
to_node, to_port, to_weight = full_path[i + 1]
link_text = parse_port(to_port, parse_port_flag)
from_str = from_node
to_str = to_node
if path_coloring == "byFastest":
link_color = "red" if len(path) == min_nodes else "black"
elif path_coloring == "byWeight":
link_color = "red" if to_weight == 10 else "black"
else:
link_color = "black"
value_str = from_str
segments.append(
f"from={from_str}, to={to_str}, linkColor={link_color}, value={value_str}, linkWidth=5, type=server, linkText={link_text}, weight={to_weight}, nodeCount={total_nodes}")
path_str = " ### ".join(segments)
print(f"{path_str}")
else:
print("No route found")
if delete_flag == '1':
try:
os.remove(file_path)
print(f"File {file_path} has been deleted.")
except OSError as e:
print(f"Error: {file_path} : {e.strerror}")
if __name__ == "__main__":
main()