This repository has been archived by the owner on Jun 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnipscan.py
executable file
·204 lines (189 loc) · 7.04 KB
/
nipscan.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python3
from sys import argv, exit, stdin
import socket
import re
import argparse
# make sure library is installed
try:
import nmap
except:
print("Error: cannot find nmap library on platform.")
print("Please install nmap library from pip")
print("Please run either \"pip3 install python-nmap\"")
print("or \"sudo apt install python3-nmap\"")
print("Exiting now")
exit(1)
#[InitConfig]#
nm = nmap.PortScanner() # the NMap scanning object
ip = []
opts = ["-sL"]
#[/InitConfig]#
#[Help]#
parser = argparse.ArgumentParser(
prefix_chars="-+/", description="""this is a portscanner that takes in ip addresses
and can do multiple things, including displaying the hostnames of each ip address,
as well as filtering out dead ip addresses and only displaying currently alive ips.""")
parser.add_argument("ips", nargs=argparse.REMAINDER, type=str,
metavar="ip_address", help="The IP Addresses to be scanned.")
parser.add_argument("-a", "--alive", type=bool, nargs="?", default=False,
const=True, help="Filters only alive ips into list")
parser.add_argument("-vi", "--visual", type=bool, nargs="?", default=True,
const=True, help="Gives the visual desplay of results (defualt)")
parser.add_argument("-r", type=bool, default=False, nargs="?", dest="brute", const=True,
help="Reads ips and assumes hosts are all alive. for incase some ips block ping.")
parser.add_argument("-f", "--file", type=argparse.FileType("r"),
metavar="input_file", help="Imports hosts from file, fan only be used once")
parser.add_argument("-e", "--extra", nargs="+", metavar="options",
help="Adds extra options to nmap scanner")
parser.add_argument("-ln", "--local", type=bool, nargs="?", default=False,
const=True, help="Adds local network addresses to scanner")
parser.add_argument("-t", "--text", type=bool, nargs="?", default=False, const=True,
help="Changes the scripts result so that it only displays the ips given. -a and -hn will change these from defualt input")
parser.add_argument("-hn", "--hostname", type=bool, nargs="?", default=False,
const=True, help="Addition to -t that includes hostname to raw result")
#[/Help]#
#[Config]#
if len(argv) <= 1 and stdin.isatty():
parser.print_help()
parse = parser.parse_args()
if parse.alive:
opts.append("-sn")
opts.remove("-sL")
if parse.visual:
parse.text = False
elif parse.text:
parse.visual = False
if parse.brute:
opts.append("-Pn")
if (parse.extra != None):
opts.extend(parse.extra)
if (parse.ips != None):
for i in range(len(parse.ips)):
if (re.search(r"\d{1,3}.\d{1,3}.\d{1,3}.(\d{1,3}/\d{2}|(\d{1,3}-\d{1,3}|\d{1,3}))", parse.ips[i]) == None):
try:
socket.gethostbyname(parse.ips[i])
except socket.gaierror:
parse.ips.pop(i)
ip.extend(parse.ips)
# elif(re.search(r"\d{1,3}.\d{1,3}.\d{1,3}.(\d{1,3}/\d{2}|(\d{1,3}-\d{1,3}|\d{1,3}))", i) != None):
# ip.append(i)
# else:
# try:
# socket.gethostbyname(i)
# except socket.gaierror:
# pass
# else:
# ip.append(i)
# [/Config]
#[STDIN]#
if not stdin.isatty():
addin = str(stdin.read()).split()
for term in addin:
reg = re.search(
r"\d{1,3}.\d{1,3}.\d{1,3}.(\d{1,3}/\d{2}|(\d{1,3}-\d{1,3}|\d{1,3}))", term)
if (reg != None):
ip.append(str(reg.group()))
else:
try:
socket.gethostbyname(term)
except socket.gaierror:
pass
else:
ip.append(term)
#[/STDIN]#
#[LocalHosts]#
if parse.local: # Local Network option
# opens a socket on computer to connect to internet
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # Talks to dns provider from google
localip = s.getsockname()[0] # this will get the local ip
s.close() # Turns off socket for possible later use
sets = localip.split(".") # splits 4 sections for use in next line
ip.append(str(sets[0] + "." + sets[1] + "." +
sets[2] + ".0-255")) # 192.168.1.0-255
#[/LocalHosts]#
#[Files]#
if parse.file != None: # this will grab ip addresses from an inputed file
doc = parse.file.read().split()
for term in doc:
reg = re.search(
r"\d{1,3}.\d{1,3}.\d{1,3}.(\d{1,3}/\d{2}|(\d{1,3}-\d{1,3}|\d{1,3}))", term)
if (reg != None):
ip.append(str(reg.group()))
else:
try:
socket.gethostbyname(term)
except socket.gaierror:
pass
else:
ip.append(term)
# [/Files]
#[Generator]#
opts.sort()
# org to filter non ip addresses
for i in range(len(ip)-1, 0, -1):
reg = re.search(
r"\d{1,3}.\d{1,3}.\d{1,3}.(\d{1,3}-\d{1,3}|\d{1,3})", ip[i])
if (reg != None):
ranges = str(reg.group()).split(".")
for p in ranges[:2]:
if int(p) < 0 or int(p) > 255:
print("Pop: %s. Not a real ipv4 address" % ip[i])
ip.pop(i)
break
else:
if "-" in ranges[3]:
ipr = ranges[3].split("-")
if int(ipr[0]) < 0 or int(ipr[1]) > 255:
print("Pop: %s. Not a real ipv4 address" % ip[i])
ip.pop(i)
elif int(ranges[3]) < 0 or int(ranges[3]) > 255:
print("Pop: %s. Not a real ipv4 address" % ip[i])
ip.pop(i)
if len(ip) == 0:
print("Error: No valid targets given\n")
parser.print_help()
exit()
count = 0
while count < len(opts) - 1: # This whole section if to remove duplicate options
if opts[count] == opts[count + 1]:
opts.pop(count)
else:
count += 1
sopts = opts[0]
sips = ip[0]
for i in opts[1:]:
sopts += (" " + i) # organizes all string options with a space separation
for i in ip[1:]:
sips += (" " + i) # organizes all ip addresses with a space as separation
nm.scan(arguments=sopts, hosts=sips)
#[/Generator]#
#[Visual]#
if parse.visual:
print("Hosts:")
print("state | hostname (ipaddress)")
for host in nm.all_hosts():
if parse.alive and parse.brute:
try:
if (nm[host] > 0 and nm[host].hostname() != ""):
print(nm[host].state() + "\t| " +
nm[host].hostname() + " ("+host+")")
except:
continue
elif parse.alive:
# prints as [true/false] | hostname (ip address)
print(nm[host].state() + "\t| " +
nm[host].hostname() + " (" + host + ")")
else:
if nm[host].hostname() != "":
print(nm[host].hostname() + " (" + host + ")")
#[/Visual]#
#[Text]#
if parse.text:
for host in nm.all_hosts():
if parse.hostname: # Hostname
if nm[host].hostname() != "":
print(host + ":" + nm[host].hostname())
else:
print(host)
#[/Text]#