-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
50 lines (42 loc) · 1.25 KB
/
utils.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
#Author: Dustin Grady
#Purpose: Module to support common scan functions
#Status: In development
import nmap
from netaddr import EUI, NotRegisteredError
'''Return index of a given IP address within record_list'''
def get_record_index(ip, record_list):
for i, rec in enumerate(record_list):
if rec.ip == ip:
return i
return 'No record found'
'''Collect OUI (vendor)'''
def retrieve_oui(record):
mac = EUI(record.mac)
try:
org = mac.oui.registration().org
except NotRegisteredError:
org = None
return org
'''Collect Operating System (uses best guess)'''
def retrieve_os(record):
nm = nmap.PortScanner()
try:
nm.scan(record.ip, arguments='-O')
if 'osmatch' in nm[record.ip]:
os = nm[record.ip]['osmatch'][0]['name']
conf = nm[record.ip]['osmatch'][0]['accuracy']
except:
os = 'None detected'
conf = '0'
return [os, conf]
'''Collect visible ports'''
def retrieve_port_status(record):
nm = nmap.PortScanner()
ports = []
nm.scan(record.ip, '0-1023')
try:
for port in nm[record.ip]['tcp'].keys():
ports.append([port, nm[record.ip]['tcp'][port]['state']])
except KeyError:
return None
return ports