-
Notifications
You must be signed in to change notification settings - Fork 3
/
carphunter.py
345 lines (312 loc) · 12.4 KB
/
carphunter.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
# Written by Jonathan Kelley, March 7, 2018
# March 12, Added config support from yaml..
#pip2.7 install netmiko
#pip2.7 install --upgrade paramiko
#pip2.7 install cryptography
#pip2.7 uninstall gssapi
#pip2.7 install unidecode
###pip2.7 install yamlcfg
#pip2.7 install prettytable
###pip2.7 install configloader
#pip2.7 install loadconfig
import sys
from netmiko import ConnectHandler
from datetime import datetime
from unidecode import unidecode
import re
import json
from pprint import pprint
import prettytable
import argparse
from loadconfig import Config
parser = argparse.ArgumentParser(description="Cisco Arp Hunter v0.1", epilog="This Python based tool uses Netmiko to search your Cisco® branded routers and switches. This tool creates an ARP cache in a local json file (using --poll). --mac and -ip can be used to find MAC, IP, PORT and VLAN associations immediately against this json cache.")
parser.add_argument('-m', '--mac', action="store", dest="mac", help="Search json cache for MAC to port/vlan mappings")
parser.add_argument('-i', '--ip', action="store", dest="ip", help="Search json cache for IP address to MAC mapping")
parser.add_argument('--poll', action="store_true", default=False, dest="pollmode", help="Poll devices now and save to local json-cache-file")
parser.add_argument('--json', action="store_true", default=False, dest="json", help="Print output in JSON format")
parser.add_argument('--all-ports', action="store_true", default=False, dest="allports", help="List all ports in cache search including port channels")
parser.add_argument('--config', action="store", default="/etc/carphunter.yml", dest="configfile", help="Non-default config file location")
parser.add_argument('--json-cache-file', action="store", default="/mnt/data/arp.json", dest="cachedbfile", help="Non-default json-cache-file location")
args = parser.parse_args()
class ConfigHelper(object):
"""
helper class to load configuration properties from local file
"""
def __init__(self, path):
conf = "!include {}".format(path)
config = Config(conf)
self.configuration = Config(conf)
@property
def raw(self):
"""
returns raw configuration as python object, not really for external use.
"""
return self.configuration
@property
def global_logins(self):
"""
return tuple of user/password combintation for global switch login privileges.
"""
return (self.configuration['global']['user'], self.configuration['global']['password'])
@property
def switches(self):
"""
return a list of configuration properties for switching devices.
"""
return self.configuration['devices']['switches']
@property
def routers(self):
"""
return a list of configuration properties for routing devices.
"""
return self.configuration['devices']['routers']
conf = ConfigHelper(path=args.configfile)
# netmiko device constants
a_device = {
'device_type': 'cisco_ios',
'verbose': False,
}
class DateTimeEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime):
return o.isoformat()
return json.JSONEncoder.default(self, o)
class Cache():
"""
updates or loads a network json database object
"""
def __init__(self):
"""
basic minimum datastructure of json class db
"""
self.db = {}
self.db['routers'] = {}
self.db['switches'] = {}
def add_switch(self, switchname):
"""
add a new switch dictionary
"""
self.db['switches'][switchname] = {}
def add_switch_entry(self, switchname, mac, mode, interface, vlan):
"""
adds a new switch entry to class db
"""
self.db['switches'][switchname][mac] = {}
self.db['switches'][switchname][mac]['mode'] = mode
self.db['switches'][switchname][mac]['interface'] = interface
self.db['switches'][switchname][mac]['vlan'] = vlan
def add_router(self, routername):
"""
add a new router dictionary
"""
self.db['routers'][routername] = {}
def add_router_entry(self, routername, ip, mac, proto, age, vlan):
"""
adds a new router entry to class db
"""
self.db['routers'][routername][ip] = {}
self.db['routers'][routername][ip]['ip'] = ip
self.db['routers'][routername][ip]['mac'] = mac
self.db['routers'][routername][ip]['proto'] = proto
self.db['routers'][routername][ip]['age'] = age
self.db['routers'][routername][ip]['vlan'] = vlan
def time_clock(self, timetaken):
"""
records time taken for poll into database
"""
self.runtime = str(timetaken)
@property
def timetaken(self):
"""
return last poller run time
"""
return self.runtime
def write_to_file(self):
"""
writes the database to a file
"""
db = {
'db': self.db,
'lastUpdate': datetime.now(),
'pollTime': self.runtime
}
with open(args.cachedbfile, 'w') as outfile:
json.dump(db, outfile, cls=DateTimeEncoder)
@property
def load_from_file(self):
"""
loads the database from file
"""
return ""
def device_command(device, command):
"""
connects to a network device and issues a command, returns decoded split output
"""
net_connect = ConnectHandler(**device)
output = net_connect.send_command(command)
return unidecode(output).split("\n")
def poll_devices():
"""
initiates a polling process and saves all device data to disk for cached searches in json
"""
cache = Cache()
start_benchmark = datetime.now()
for router, v in conf.routers.iteritems():
print("Collecting ARP from router {} ({})".format(router, v.get("name", "No name defined")))
a_device['ip'] = router
a_device['username'] = conf.global_logins[0]
a_device['password'] = conf.global_logins[1]
output = device_command(a_device, "show ip arp")
cache.add_router(routername=v.get("name", router))
for line in output:
if "Internet" in line:
line=re.split(" +",line)
line = [x.strip() for x in line]
dict = {}
cache.add_router_entry(
routername=v.get("name", router),
ip=line[1],
age=line[2],
mac=line[3],
proto=line[4],
vlan=line[5],
)
for switch, v in conf.switches.iteritems():
print("Collecting ARP from switch {} ({})".format(switch, v.get("name", "No name defined")))
a_device['ip'] = switch
a_device['username'] = v.get("user", conf.global_logins[0])
a_device['password'] = v.get("password",conf.global_logins[1])
output = device_command(a_device, "show mac address-table")
cache.add_switch(switchname=v.get("name", switch))
for line in output:
line = line.strip() # strip unwanted spaces
if ("STATIC" in line) or ("DYNAMIC" in line):
line = line.split(" ")
line = [x.strip() for x in line]
dict = {}
dict['vlan'] = line[0]
dict['mac'] = line[1]
dict['mode'] = line[2]
dict['interface'] = line[3]
cache.add_switch_entry(
switchname=v.get("name", switch),
vlan=line[0],
mac=line[1],
mode=line[2],
interface=line[3]
)
end_benchmark = datetime.now()
total_time = end_benchmark - start_benchmark
dbs.time_clock(total_time)
dbs.write_to_file()
print("Time taken: {}".format(dbs.timetaken))
def print_statistics(data, total_time):
print("\nStatistics:")
print(" * Cache contains {} routers, {} switches".format(len(data['db']['routers']),len(data['db']['switches'])))
print(" * Cache updated {}\n * Last poller took {}".format(data['lastUpdate'], data['pollTime']))
print(" * This lookup took {}".format(total_time))
print("Tool written by: Jonathan Kelley")
def search_arp_table(search, jsondata=False):
"""
searches the json cache for an arp entry
"""
start_benchmark = datetime.now()
print("Checking network for `{}`...".format(search))
data = json.load(open(args.cachedbfile))
for router, arpitem in data['db']['routers'].iteritems():
print_results = 0
#print router, arp
table = prettytable.PrettyTable(["hwaddr", "ip", "vlan", "age", "proto"])
for ip, values in arpitem.iteritems():
if not values['vlan']:
values['vlan'] = "N/A"
if search in values['mac']:
table.add_row([values['mac'], ip, values['vlan'], values['age'], values['proto']])
print_results = 1
if print_results:
print("\nRouting Device: {} found match!".format(router))
print("{}".format(table))
for switch, arpitem in data['db']['switches'].iteritems():
print_results = 0
table = prettytable.PrettyTable(["hwaddr", "interface", "vlan", "mode"])
for mac, values in arpitem.iteritems():
if search in mac:
if not args.allports:
if "/" in values['interface']:
table.add_row([mac, values['interface'], values['vlan'], values['mode']])
print_results = 1
else:
table.add_row([mac, values['interface'], values['vlan'], values['mode']])
print_results = 1
if print_results:
print("\nSwitching Device: {} found match!".format(switch))
print("{}".format(table))
end_benchmark = datetime.now()
total_time = end_benchmark - start_benchmark
print_statistics(data, total_time)
def ip2arp(searchip,jsondata=False):
"""
find arpa in json cache based on ip address
"""
start_benchmark= datetime.now()
if not jsondata:
print("Checking network for `{}` ARPA entry...".format(searchip))
data = json.load(open(args.cachedbfile))
jsondata = {}
for router, arpitem in data['db']['routers'].iteritems():
print_results = 0
exact_match = 0 # if exact match is found, hide ambiguous results
#print router, arp
jsondata[router] = {}
table = prettytable.PrettyTable(["ip", "hwaddr", "vlan", "age", "proto"])
for ip, values in arpitem.iteritems():
if values['vlan'] == '':
values['vlan'] = "?"
if searchip == values['ip']:
table.add_row([ip, values['mac'], values['vlan'], values['age'], values['proto']])
print_results = 1
exact_match = 1
elif searchip in values['ip']:
if not exact_match:
table.add_row([ip, values['mac'], values['vlan'], values['age'], values['proto']])
print_results = 1
if print_results:
print("\nRouting Device: {} found a matching IP!".format(router))
print("{}\n".format(table))
print("")
end_benchmark = datetime.now()
total_time = end_benchmark - start_benchmark
if not jsondata:
print_statistics(data, total_time)
if "__main__" == __name__:
# arg parse
if len(sys.argv) == 1:
# Ask for y/n if displaying ALL NETWORK DATA
yes = {'yes','y', 'ye'}
no = {'no','n', ''}
sys.stdout.write("WARNING : This will display ALL NETWORK INFORMATION, are you sure?\n")
sys.stdout.write(" Type 'yes' or 'no': ")
choice = raw_input().lower()
if choice in yes:
pass
elif choice in no:
print("Exiting...")
exit(1)
else:
sys.stdout.write("Please respond with 'yes' or 'no'")
if args.pollmode:
# --poll flag
print("Collection polling started. Do not terminate or data will not be saved.")
poll_devices()
else:
# not --poll flag
if args.ip:
# --ip flag
ip2arp(args.ip, args.json)
else:
# --mac flag
if not args.mac:
args.mac = "."
search_arp_table(args.mac)