-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
112 lines (104 loc) · 3.6 KB
/
main.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
#Code uses python language, by Lucas, email: [email protected]
#This code scans the network is 3 different modes,the 4th option is using all 3 modes at once.
import nmap3
import nmap
import datetime
nmScan = nmap.PortScanner()
nm = nmap3.Nmap()
now = datetime.datetime.now()
x = input("Enter network/device you want to scan for: ")
def istrue(x): #checks if input is a valid ip address
"""_summary_
Args:
x (): ip address of device/network
"""
try:
ip_object = ipaddress.ip_address(x)
except ValueError:
print("Not valid ip address")
x = input("Pls input again: ")
istrue(x)
istrue(x)
time = (now.strftime("%Y-%m-%d %H%M"))
bdict={
}
o = ("-------------------------------------------------------------------\n")
separate=("---------------------\n")
nmScan.scan(x)
results = nm.nmap_os_detection(x) #output of os scan of network/device
def os(scantype,writingtype): # scans network and returns os name and cpe with device name
"""_summary_
Args:
scantype (): type of scan (e.g. ping, os or all)
writingtype (): 'w' or 'a' to write to a file
"""
filename = scantype+'_'+time+'.txt'
file1 = open(filename, writingtype)
for host in nmScan.all_hosts():
L=('Device :'+host+'\n')
file1.writelines(o)
file1.writelines(L)
file1.writelines(separate)
osstart = ("--OS Matches--\n")
file1.writelines(osstart)
osstats = results[host]["osmatch"]
for i in list(osstats):
a = i["accuracy"]
q=i["name"]
if "cpe" in i:
v = i["cpe"]
else:
v="cpe:[NONE]"
file1.writelines("os name:"+q+" (accuracy:"+a+")\n")
file1.writelines(v+"\n")
print("The output is in "+filename)
def ports(scantype, writingtype): #scans network and returns ports with device name
"""_summary_
Args:
scantype (): type of scan(os,ports or all)
writingtype ( ): 'w' or 'a' to write to file
"""
filename = scantype+'_'+time+'.txt'
print(filename)
file1 = open(filename, writingtype)
for host in nmScan.all_hosts():
file1.writelines(o)
d=("Service:Port")
L=('Device :'+host+'\n')
file1.writelines(L)
file1.writelines(separate)
file1.writelines(d+"\n")
file1.writelines(separate)
for proto in nmScan[host].all_protocols():
lport = nmScan[host][proto].keys()
nport = nmScan[host][proto]
for i in nport:
a=nport[i]['name']
bdict[i]=a
for port in lport:
p=(bdict[port]+':'+ str(port)+'\n' )
file1.writelines(p)
file1.writelines(separate)
print("The output is in "+filename)
def choice():
y = input("Ping,scan OS, scan ports, or all: ")
if y == "os" or y== "OS" or y == "scan os" or y == "scan OS":
os("OS",'w')
elif y == "ports" or y == "Port" or y== "scan port" or y== "Scan Port":
ports("Ports",'w')
elif y == "all" or y == "All":
ports("All",'a')
os("All",'a')
elif y == "ping" or y == "Ping":
scantype = "Ping"
filename = scantype+'_'+time+'.txt'
file1 = open(filename, 'w')
for host in nmScan.all_hosts():
L=('Device :'+host+'\n')
file1.writelines(L)
print("The output is in "+filename)
else:
print("-Please input again-")
choice()
choice()
print("--DONE--")