-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CiscoMacFinder.py
294 lines (225 loc) · 7.02 KB
/
CiscoMacFinder.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
import re
import signal
import sys
import yaml
from getpass import getpass
from netmiko import ConnectHandler
def signal_handler(frame, signal):
'''
Exit gracefully on SIGINT
'''
sys.exit()
def get_input():
'''
Receive user input from terminal
check length, if correct, pass to
AnalyzeMac()
'''
user_input = sys.argv[1].strip()
if len(user_input) >= 14 and len(user_input) <= 17:
MAC, mac_format = analyze_mac(user_input)
else:
print('\033[91m', end="")
print("[❌] wrong MAC address")
print('\033[0m', end="")
exit()
username = input("[+] Username: ")
password = getpass("[+] Password: ")
return MAC, mac_format, username, password
def analyze_mac(mac):
'''
Check whether the MAC is a Cisco
formatted MAC or a normal MAC.
'''
cisco_pattern = re.compile(r"([0-9a-fA-F]{4}(?:.[0-9a-fA-F]{4}){2})")
linux_pattern = re.compile(r"([0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){5})")
windows_pattern = re.compile(r"([0-9a-fA-F]{2}(?:-[0-9a-fA-F]{2}){5})")
Cisco_MAC = re.findall(cisco_pattern, mac)
Linux_MAC = re.findall(linux_pattern, mac)
Windows_MAC = re.findall(windows_pattern, mac)
mac_format = ""
print('\033[1m', end="")
print('\033[92m', end="")
if Cisco_MAC:
MAC = Cisco_MAC[0]
print(f"[+] Cisco formatted MAC detected")
mac_format = "cisco"
elif Linux_MAC:
MAC = Linux_MAC[0]
print(f"[+] Linux formatted MAC detected")
mac_format = "linux"
elif Windows_MAC:
MAC = Windows_MAC[0]
print(f"[+] Windows formatted MAC detected")
mac_format = "windows"
else:
print('\033[91m', end="")
print(f"[❌] Invalid MAC address: {mac}")
exit()
print('\033[0m', end="")
return MAC, mac_format
def convert_cisco_mac_to_linux(mac):
'''
Gets a MAC and converts it to
Linux format
'''
char_count = 1
total_chars = 0
mac_addr = []
for char in mac.replace(".", ""):
if char_count == 2 and total_chars != 11:
mac_addr.append(char + ":")
char_count = 1
else:
mac_addr.append(char)
char_count += 1
total_chars += 1
mac = "".join(x for x in mac_addr)
return mac
def convert_linux_mac_to_cisco(mac):
'''
Gets a MAC and converts it to
Cisco format
'''
char_count = 1
total_chars = 0
mac_addr = []
for char in mac.replace(":", ""):
if char_count == 4 and total_chars != 11:
mac_addr.append(char + ".")
char_count = 1
else:
mac_addr.append(char)
char_count += 1
total_chars += 1
mac = "".join(x for x in mac_addr)
return mac
def convert_windows_mac_to_cisco(mac):
'''
Gets a MAC and converts it to
Cisco format
'''
char_count = 1
total_chars = 0
mac_addr = []
for char in mac.replace("-", ""):
if char_count == 4 and total_chars != 11:
mac_addr.append(char + ".")
char_count = 1
else:
mac_addr.append(char)
char_count += 1
total_chars += 1
mac = "".join(x for x in mac_addr)
return mac
def open_yaml_file(yamlfile):
'''
opens a YAML file and returns
its content to lookup_mac()
'''
with open(yamlfile, "r") as swfile:
switches = yaml.safe_load(swfile)
return switches
def lookup_mac(username, password, mac, yamlfile):
'''
receives MAC table and looks for
intended mac
'''
counter = 0
switches = open_yaml_file(yamlfile)
sites = switches.keys()
switch_list = []
seen_in_site = False
print('\033[1m', end="")
print('\033[92m', end="")
print(f"[+] Searching for: ", end="")
print('\033[94m', end="")
print(f"{mac}")
print('\033[92m', end="")
for site in sites:
if seen_in_site:
break
site_items = len(site)
print(f"[+] Looking up {site} site on {site_items} devices.")
print("-" * 50)
for sw in switches[site]:
seen_in_sw = False
swname = sw['name']
swip = sw['mgmt_ip']
sshport = sw['port']
swname, mac_table = SSH_to_SW(username, password,
swip, sshport,
swname)
if not mac_table:
print('\033[93m', end="")
print("[-] MAC table fetch was not successful")
print('\033[0m', end="")
continue
for line in mac_table.splitlines():
if mac in line:
counter += 1
switch_list.append(swname)
vlan = line.split()[0]
# MAC = line.split()[1]
mactype = line.split()[2]
port = line.split()[3]
seen_in_site = True
seen_in_sw = True
print('\033[1m', end="")
print('\033[92m', end="")
print(f" |---> [+] Found [{mac}] in {swname}", end="")
print(f" on port {port} and VLAN number of {vlan}")
print(f" |---> [*] MAC is learned {mactype}ALLY\n")
print('\033[0m', end="")
if not seen_in_sw:
print('\033[93m', end="")
print(f"[-] MAC not found on {swname}")
print('\033[0m', end="")
print('\033[1m', end="")
print('\033[95m')
if counter == 0:
print(f"[-] {mac} MAC address was not found")
exit()
print(f"[+] MAC was seen on {counter} switch(es)")
for sw in switch_list:
print(f"\t{sw}", flush=True)
print('\033[0m')
def SSH_to_SW(username, password, swip, sshport, swname):
'''
SSH to switches and look
for MAC address
'''
get_mac_command = "show mac address-table"
sshport = str(sshport)
device = {
'device_type': 'cisco_ios',
'ip': swip,
'port': sshport,
'username': username,
'password': password,
}
print('\033[1;36m', end="")
print(f"[*] Connecting to {swname} using {swip}")
print('\033[0m', end="")
try:
net_connect = ConnectHandler(**device)
mac_table = net_connect.send_command(get_mac_command, delay_factor=10)
net_connect.send_command("\n", delay_factor=10)
return swname, mac_table
except Exception as e:
print('\033[91m', end="")
print(f"[-] Could not connect to {swname} using {swip}")
print(e)
print('\033[0m', end="")
pass
def main():
signal.signal(signal.SIGINT, signal_handler)
mac, platform, username, password = get_input()
if platform == "linux":
mac = convert_linux_mac_to_cisco(mac)
elif platform == "windows":
mac = convert_windows_mac_to_cisco(mac)
lookup_mac(username, password, mac, "switches.yml")
if __name__ == "__main__":
main()