-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.py
executable file
·98 lines (78 loc) · 2.46 KB
/
discovery.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
#!/usr/bin/python3
# All software written by Tomas. (https://github.com/shelbenheimer)
from scapy.all import ARP, srp, Ether, get_if_addr, conf
from json import load
import time
import sys
import os
BANNER = "Software written by Tomas. Available on GitHub. (https://github.com/shelbenheimer)"
VENDOR_PATH = "Resources/manuf.json"
class Discovery:
def __init__(self):
self.target = self.FormatAddress(get_if_addr(conf.iface))
self.path = f'{os.path.dirname(os.path.abspath(__file__))}/{VENDOR_PATH}'
self.vendors = self.PopulateVendors()
def GetHosts(self):
packet = Ether(dst="FF:FF:FF:FF:FF:FF") / ARP(pdst=self.target)
replies = srp(packet, timeout=1, verbose=False)[0]
if not replies:
return []
hosts = []
for reply in range(0, len(replies)):
information = (replies[reply][1].psrc, replies[reply][1].hwsrc)
hosts.append(information)
return hosts
def FormatAddress(self, ip):
mask = "255.255.255.0"
split_mask = mask.split('.')
split_addr = ip.split('.')
counted = 0
for octet in range(0, len(split_mask)):
if split_mask[octet] == "0":
split_addr[octet] = "0"
binary = bin(int(split_mask[octet]))[2:]
for digit in binary:
if digit == '1': counted += 1
result = f'{".".join(split_addr)}/{counted}'
return result
def PopulateVendors(self):
try:
with open(self.path, 'r', encoding="utf8") as file:
vendors = load(file)
return vendors
except Exception as error:
print(error)
return {}
def FormatOUI(self, address):
formatted = address[:8].replace("-", ":")
return formatted
def GetVendor(self, address):
if not self.vendors:
return "Unobtainable"
try:
formatted = self.FormatOUI(address).upper()
return self.vendors[formatted]
except:
return "Unknown Vendor"
def Main():
try:
print(BANNER)
discovery = Discovery()
if not discovery.PopulateVendors():
print("Failed to populate manufacturer database.")
print(f'Commencing scan on {discovery.target} on {time.ctime()}.')
start_time = time.time()
hosts = discovery.GetHosts()
if not hosts:
print("There was an error whilst attempting to scan the network.")
return
for host in hosts:
vendor = discovery.GetVendor(host[1])
print(f'{host[0]:<18} {host[1]:<20} {vendor:<20}')
elapsed = time.time() - start_time
formatted_elapsed = "".join(list(str(elapsed))[:5])
print(f'Finished scan in {formatted_elapsed}s.')
except Exception as error:
print(error)
return
if __name__ == "__main__": Main()