forked from hackthepart/HawkEye
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
121 lines (110 loc) · 4.09 KB
/
scanner.py
File metadata and controls
121 lines (110 loc) · 4.09 KB
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
from scapy.all import *
import os, sys
import traceback
import urllib2 as urllib
BLUE, RED, WHITE, YELLOW, MAGENTA, GREEN, END = '\33[94m', '\033[91m', '\33[97m', '\33[93m', '\033[1;35m', '\033[1;32m', '\033[0m'
#The function returns your current working interface (wlan or wifi)
def getDefaultInterface(returnNet=False):
def long2net(arg):
if(arg <= 0 or arg >= 0xFFFFFFFF):
raise ValueError("Illegal Netmask Value", hex(arg))
return 32 - int(round(math.log(0xFFFFFFFF - arg, 2)))
def to_CIDR_notation(bytes_network, bytes_netmask):
network = scapy.utils.ltoa(bytes_network)
netmask = long2net(bytes_netmask)
net = "%s/%s" %(network, netmask)
if netmask < 16:
return None
return net
iface_routes = [route for route in scapy.config.conf.route.routes if route[3] == scapy.config.conf.iface and route[1] != 0xFFFFFFFF]
network, netmask, _, interface, address = max(iface_routes, key=lambda item:item[1])
net = to_CIDR_notation(network, netmask)
if net:
if returnNet:
return net
else:
return interface
#This returns your MAC address
def getDefaultInterfaceMAC():
try:
defaultInterfaceMac = get_if_hwaddr(defaultInterface)
if defaultInterfaceMac == "" or not defaultInterfaceMac:
print("Error")
defaultInterfaceMac = raw_input(header)
return defaultInterfaceMac
else:
return defaultInterfaceMac
except:
print("Ex. Error")
#This returns your Gateway IP
def getGatewayIP():
try:
getGateway_p = sr1(IP(dst="google.com", ttl=0) / ICMP() / "XXXXXXXXXXX", verbose=False)
return getGateway_p.src
except:
# request gateway IP address (after failed detection by scapy)
print("\n{0}ERROR: Gateway IP could not be obtained. Please enter IP manually.{1}\n").format(RED, END)
header = ('{0}Scanner {1}> {2}Enter Gateway IP {3}(e.g. 192.168.1.1): '.format(BLUE, WHITE, RED, END))
gatewayIP = raw_input(header)
return gatewayIP
#Uses API to fingerprint your device using MAC address
def resolveMac(mac):
try:
url = "https://macvendors.co/api/vendorname/"
request = urllib.Request(url + mac, headers={'User-Agent': "API Browser"})
response = urllib.urlopen(request)
vendor = response.read()
vendor = vendor.decode("utf-8")
vendor = vendor[:25]
return vendor
except:
return "N/A"
#Scans the network, in given interface, to generate list of live IPs
def scanNetwork(network):
returnList = []
import nmap
nm = nmap.PortScanner()
a = nm.scan(hosts=network, arguments='-sP')
for k, v in a['scan'].iteritems():
if str(v['status']['state']) == 'up':
try:
returnList.append([str(v['addresses']['ipv4']), str(v['addresses']['mac'])])
except:
pass
return returnList
def getNodes():
global nodelist
try:
nodelist = scanNetwork(getDefaultInterface(True))
except KeyboardInterrupt:
printf("Terminated.")
except:
print("Error.")
generateIPs()
#Create list of IPs that were found live
def generateIPs():
global liveIPs
liveIPs = []
for host in nodelist:
liveIPs.append(host[0])
print("Running")
defaultInterface = getDefaultInterface()
defaultGatewayIP = getGatewayIP()
defaultInterfaceMac = getDefaultInterfaceMAC()
print("Network Details: ")
print("Default Network Interface: " + defaultInterface)
print("Your Gateway IP: " + defaultGatewayIP)
print("Your MAC Address: " + defaultInterfaceMac)
getNodes()
print(nodelist) #This list contains both IP and MAC addresses
print("IP thinggy")
print(liveIPs) #This list only contains their IP addresses
print("Real Thinggy")
for i in range(len(liveIPs)):
mac = ""
for host in nodelist:
if host[0] == liveIPs[i]:
mac = host[1]
vendor = resolveMac(mac)
#print(mac)
print(" [{0}" + str(i) + "{1}] {2}" + str(liveIPs[i]) + "{3}\t" + mac + "{4}\t" + vendor + "{5}").format(YELLOW, WHITE, RED, BLUE, GREEN, END)