-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfingerbank.py
71 lines (52 loc) · 2.01 KB
/
fingerbank.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
import os
def parse_info(client):
dhcpf = client["dhcp_fingerprint"]
dhcpv = client["dhcp_vendor"]
ua = client["user_agent"]
mac = client["oui"] + ":00:00:00"
return dhcpf, dhcpv, ua, mac
def get_device_api(client, api_key):
import requests
BASE_URL = "https://fingerbank.inverse.ca/api/v1/combinations/interogate?"
dhcpf, dhcpv, ua, mac = parse_info(client)
data = {"dhcp_fingerprint": dhcpf, "dhcp_vendor": dhcpv, "mac": mac, "user_agent": ua}
url = BASE_URL + 'key=' + api_key
r = requests.get(url, data)
if r.status_code == 200:
data = r.json()
else:
return False
res = {"name": data["device"]["name"], "parents": {}}
return res
def get_device_db(client, c):
dhcpf, dhcpv, ua, mac = parse_info(client)
# Get dhcp fingerprint id
c.execute('SELECT id FROM dhcp_fingerprint WHERE value = "{dhcpf}"'.
format(dhcpf=dhcpf))
f_id = c.fetchone()[0]
# Get dhcp vendor id
c.execute('SELECT id FROM dhcp_vendor WHERE value = "{dhcpv}"'.
format(dhcpv=dhcpv))
v_id = c.fetchone()[0]
# Get the device with combination score
c.execute('SELECT combination.score, device.name, device.parent_id FROM combination, device \
WHERE device.id = combination.device_id \
AND combination.dhcp_fingerprint_id = {f_id} \
AND combination.dhcp_vendor_id = {v_id} \
AND user_agent_id \
IN (SELECT id FROM user_agent WHERE value LIKE "{ua}")'.
format(f_id=f_id, v_id=v_id, ua=ua))
device = c.fetchone()
res = {"name": device[1], "parents": {}}
parents = []
parent_id = device[2]
while parent_id is not None:
c.execute('SELECT device.name, device.parent_id FROM device \
WHERE device.id = {id}'.
format(id=parent_id))
p = c.fetchone()
parents.append(p[0])
parent_id = p[1]
for i, p in enumerate(parents):
res["parents"][i] = p
return res