-
Notifications
You must be signed in to change notification settings - Fork 0
/
netscan.py
185 lines (163 loc) · 8.58 KB
/
netscan.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
# Author: Eugenio Pastoral
# Course: Advanced and Offensive Security
#!/usr/bin/env python3
import argparse, time, scanner, ipv4, logging, sys
from threading import Thread
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
logging.getLogger("scappy.interactive").setLevel(logging.ERROR)
logging.getLogger("scappy.loading").setLevel(logging.ERROR)
try:
from scapy.all import *
except ImportError:
print("Scapy library for Python is not installed on your system. Run 'pip install --pre scapy[basic]' to install the library.")
print("For more information, visit https://scapy.readthedocs.io/en/latest/installation.html to isntall Scapy.")
exit(0)
def main():
start = time.time()
# Arg Parsing
parser = argparse.ArgumentParser(description = "To perform a Ping Sweep, the syntax is as follows:\n'sudo ptyhon3 netscan.py -i [host] -c [number of packets]'\n\nTo perform a TCP Port Scan, the syntax is as follows:\n'sudo ptyhon3 netscan.py -p [port] -[scaning mode] [host]'", formatter_class = argparse.RawTextHelpFormatter)
THO = parser.add_argument_group('TARGET HOST OPTIONS')
THO.add_argument("H", nargs="?", help = "Target Host IP or URL")
THO.add_argument("-p", "--p", help = "Target Port")
PSO = parser.add_argument_group('PORT SCANNING OPTIONS')
PSO.add_argument("-t", "--t", action = 'store_true', help = "Perform TCP Connect Scan")
PSO.add_argument("-s", "--s", action = 'store_true', help = "Perform TCP SYN Scan")
PSO.add_argument("-x", "--x", action = 'store_true', help = "Perform TCP XMAS Scan")
PSO.add_argument("-f", "--f", action = 'store_true', help = "Perform TCP FIN Scan")
PSO.add_argument("-n", "--n", action = 'store_true', help = "Perform TCP NULL Scan")
PSO.add_argument("-a", "--a", action = 'store_true', help = "Perform TCP ACK Scan")
PSO.add_argument("-ALL", "--ALL", action = 'store_true', help = "Perform ALL TCP Port Scans")
IEO = parser.add_argument_group('PING SWEEPING OPTIONS')
IEO.add_argument("-i", "--i", action = 'store_true', help = "Perform Ping Sweep")
IEO.add_argument("-c", "--c", help = "Number of ICMP ECHO Requests to be sent for Ping Sweep", type = int)
IEO = parser.add_argument_group('PROGRAM OPTIONS')
IEO.add_argument("-T", "--T", action = 'store_true', help = "Show time spent to compelete the scan")
IEO.add_argument("-v", "--v", action = 'store_true', help = "Show program description")
args = parser.parse_args()
if args.v:
print("""
███╗░░██╗███████╗████████╗░██████╗░█████╗░░█████╗░███╗░░██╗
████╗░██║██╔════╝╚══██╔══╝██╔════╝██╔══██╗██╔══██╗████╗░██║
██╔██╗██║█████╗░░░░░██║░░░╚█████╗░██║░░╚═╝███████║██╔██╗██║
██║╚████║██╔══╝░░░░░██║░░░░╚═══██╗██║░░██╗██╔══██║██║╚████║
██║░╚███║███████╗░░░██║░░░██████╔╝╚█████╔╝██║░░██║██║░╚███║
╚═╝░░╚══╝╚══════╝░░░╚═╝░░░╚═════╝░░╚════╝░╚═╝░░╚═╝╚═╝░░╚══╝
""")
print("netscan by Eugenio Pastoral\n")
print("netscan is a TCP port scanning and ping sweep tool that uses Scapy to craft and send out appropriate packets. It can detect open, closed, filtered, and unfiltered ports. It can also detect live hosts.\n")
print("NOTE: In order to ensure that the program is going to work correctly, please install the latest version of Scapy.")
else:
ip = args.H
type = None
end = None
result = ""
results = []
try:
if args.t:
if type != None:
raise Exception()
type = 1
if args.s:
if type != None:
raise Exception()
type = 2
if args.x:
if type != None:
raise Exception()
type = 3
if args.f:
if type != None:
raise Exception()
type = 4
if args.n:
if type != None:
raise Exception()
type = 5
if args.a:
if type != None:
raise Exception()
type = 6
if args.ALL:
if type != None:
raise Exception()
type = 0
end = 7
if args.i:
type = 0
if type != None and end != None:
raise Exception()
except Exception as e:
print('\nInvalid argument combination supplied. Try again.')
parser.print_help()
exit(0)
port = args.p
if type == None or ip == None:
print('\nInsufficient arguments supplied. Try again.')
parser.print_help()
exit(0)
# Check args present and set variables
if not args.i:
if (port == None):
print('\nPlease specify a valid port to perform a port scan.')
parser.print_help()
exit(0)
if not ipv4.validate_ip(ip):
print('\nIP address is missing or invalid. Please try again.')
parser.print_help()
exit(0)
if args.i:
if args.c == None:
scanner.ping(ip, 10, True)
else:
scanner.ping(ip, args.c, True)
if args.ALL:
for type in range (1, 7):
result = scanner.portscan(ip, port, type, True)
print('\n=============================================')
# print("""
# ███╗░░██╗███████╗████████╗░██████╗░█████╗░░█████╗░███╗░░██╗
# ████╗░██║██╔════╝╚══██╔══╝██╔════╝██╔══██╗██╔══██╗████╗░██║
# ██╔██╗██║█████╗░░░░░██║░░░╚█████╗░██║░░╚═╝███████║██╔██╗██║
# ██║╚████║██╔══╝░░░░░██║░░░░╚═══██╗██║░░██╗██╔══██║██║╚████║
# ██║░╚███║███████╗░░░██║░░░██████╔╝╚█████╔╝██║░░██║██║░╚███║
# ╚═╝░░╚══╝╚══════╝░░░╚═╝░░░╚═════╝░░╚════╝░╚═╝░░╚═╝╚═╝░░╚══╝
# """)
#
# print("Performing scan...\n")
# headers = ["IP Address", "Port", "ICMP", "Connect", "SYN", "XMAS", "FIN", "NULL", "ACK"]
# results.append(ip)
# results.append(port)
#
# if scanner.ping(ip, 3, False):
# results.append("↑")
# else:
# results.append("↓")
#
# for type in range (1, 7):
# result = scanner.portscan(ip, port, type, False)
# results.append(result)
#
# data = [headers] + [results]
#
# for i, d in enumerate(data):
# line = '|'.join(str(x).ljust(15) for x in d)
# print(line)
# if i == 0:
# print('-' * len(line))
#
# print('\nLegend:\nICMP\n↑ = Host is up\n↓ = Host is down\n\nTCP\nO = Port is open\nC = Port is closed\nF = Port is filtered\nO|F = Port is possibly open or filtered\nUF = Port is unfiltered')
elif args.ALL != True and args.i != True:
result = scanner.portscan(ip, port, type, True)
end = time.time()
if args.T:
print('\nCompleted scan in ' + str(round((end - start), 4)) + 's.')
else:
print('\nScan complete.')
if __name__ == '__main__':
try:
t = Thread(target=main)
t.daemon = True
t.start()
t.join()
except KeyboardInterrupt as e:
sys.exit(0)