-
Notifications
You must be signed in to change notification settings - Fork 19
/
astsu.py
413 lines (320 loc) · 14.2 KB
/
astsu.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os,sys,socket,ipaddress,argparse,textwrap,logging
from scapy.all import *
from ctypes import *
from time import sleep
from threading import Thread
from modules import service_detection,os_detection
from progress.bar import ChargingBar
from colorama import Fore
import rpycolors
old_print = print
print = rpycolors.Console().print
white = Fore.WHITE
black = Fore.BLACK
red = Fore.RED
reset = Fore.RESET
blue = Fore.BLUE
cyan = Fore.CYAN
yellow = Fore.YELLOW
green = Fore.GREEN
magenta = Fore.MAGENTA
OPEN_PORT = 80
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
clear = lambda:os.system('cls' if os.name == 'nt' else 'clear')
__version__ = "v1.1.3"
def print_figlet(sleep=True):
clear()
print(textwrap.dedent(
f'''
.d8b. .d8888. d888888b .d8888. db db
d8' `8b 88' YP `~~88~~' 88' YP 88 88
88ooo88 `8bo. 88 `8bo. 88 88
88~~~88 `Y8b. 88 `Y8b. 88 88
88 88 db 8D 88 db 8D 88b d88
YP YP `8888Y' YP `8888Y' ~Y8888P'
Github: https://github.com/ReddyyZ/astsu
By: ReddyyZ
Version: {__version__}
[[cyan]*[/cyan]]Starting...
'''
))
if sleep:
try:
time.sleep(4.5)
except KeyboardInterrupt:
pass
class Scanner:
def __init__(self,target=None,my_ip=None,protocol=None,timeout=5,interface=None):
self.target = target
self.my_ip = my_ip
self.protocol = protocol
self.timeout = timeout
self.interface = interface
def port_scan(self,stealth=None,port=80):
protocol = self.protocol if self.protocol else "TCP"
if stealth:
pkt = IP(dst=self.target)/TCP(dport=port,flags="S")
scan = sr1(pkt,timeout=self.timeout,verbose=0)
if scan == None:
return {port: 'Filtered'}
elif scan.haslayer(TCP):
if scan.getlayer(TCP).flags == 0x12: # 0x12 SYN+ACk
pkt = IP(dst=self.target)/TCP(dport=port,flags="R")
send_rst = sr(pkt,timeout=self.timeout,verbose=0)
return {port: 'Open'}
elif scan.getlayer(TCP).flags == 0x14:
return {port: 'Closed'}
elif scan.haslayer(ICMP):
if int(scan.getlayer(ICMP).type) == 3 and int(scan.getlayer(ICMP).code in [1,2,3,9,10,13]):
return {port: 'Filtered'}
else:
if protocol == "TCP":
pkt = IP(dst=self.target)/TCP(dport=port,flags="S")
scan = sr1(pkt,timeout=self.timeout,verbose=0)
if scan == None:
return {port: 'Filtered'}
elif scan.haslayer(TCP):
if scan.getlayer(TCP).flags == 0x12: # 0x12 SYN+ACk
pkt = IP(dst=self.target)/TCP(dport=port,flags="AR")
send_rst = sr(pkt,timeout=self.timeout,verbose=0)
return {port: 'Open'}
elif scan.getlayer(TCP).flags == 0x14:
return {port: 'Closed'}
elif protocol == "UDP":
pkt = IP(dst=self.target)/UDP(dport=port)
scan = sr1(pkt, timeout=self.timeout,verbose=0)
if scan == None:
return {port: 'Open/Filtered'}
elif scan.haslayer(UDP):
return {port: 'Closed'}
elif scan.haslayer(ICMP):
if int(scan.getlayer(ICMP).type) == 3 and int(scan.getlayer(ICMP).code) == 3:
return {port: 'Closed'}
elif int(scan.getlayer(ICMP).type) == 3 and int(scan.getlayer(ICMP).code) in [1,2,9,10,13]:
return {port: 'Closed'}
def handle_port_response(self,ports_saved,response,port):
"""
Handle the port_scan response
ports_saved : Dict with the variables where's located the open ports, filtered ports, open or filtered.
response : the response of port_scan function
port : port of the scan
"""
open_ports = ports_saved['open']
filtered_ports = ports_saved['filtered']
open_or_filtered = ports_saved['open/filtered']
if response[port] == "Closed":
logging.warning(f"Port: {port} - Closed")
elif response[port] == "Open":
logging.info(f"Port: {port} - Open")
open_ports.append(port)
elif response[port] == "Filtered":
logging.warning(f"Port: {port} - Filtered")
filtered_ports.append(port)
elif response[port] == "Open/Filtered":
logging.info(f"Port: {port} - Open/Filtered")
open_or_filtered.append(port)
else:
pass
return (
open_ports,
filtered_ports,
open_or_filtered
)
def common_scan(self,stealth=None,sv=None):
# print_figlet()
if not self.protocol:
protocol = "TCP"
else:
protocol = self.protocol
ports = [21,22,80,443,3306,14147,2121,8080,8000]
open_ports = []
filtered_ports = []
open_or_filtered = []
if stealth:
logging.info("Starting - Stealth TCP Port Scan\n")
else:
if protocol == "TCP":
logging.info("Starting - TCP Connect Port Scan\n")
elif protocol == "UDP":
logging.info("Starting - UDP Port Scan\n")
else:
pass
for port in ports:
scan = self.port_scan(port=port,stealth=stealth)
if scan:
ports_saved = {
"open": open_ports,
"filtered": filtered_ports,
"open/filtered": open_or_filtered
}
open_ports, filtered_ports, open_or_filtered = self.handle_port_response(ports_saved=ports_saved,response=scan,port=port)
if open_ports or filtered_ports or open_or_filtered:
total = len(open_ports) + len(filtered_ports) + len(open_or_filtered)
print("")
logging.info(f"Founded {total} ports!")
for port in open_ports:
logging.info(f"Port: {port} - Open")
for port in filtered_ports:
logging.warning(f"Port: {port} - Filtered")
for port in open_or_filtered:
logging.info(f"Port: {port} - Open/Filtered")
def range_scan(self,start,end=None,stealth=None,sv=None):
open_ports = []
filtered_ports = []
open_or_filtered = []
protocol = self.protocol
if not protocol:
protocol = "TCP"
# print_figlet()
if protocol == "TCP" and stealth:
logging.info("Starting - TCP Stealth Port Scan\n")
elif protocol == "TCP" and not stealth:
logging.info("Starting - TCP Connect Port Scan\n")
elif protocol == "UDP":
logging.info("Starting - UDP Port Scan\n")
else:
pass
if end:
for port in range(start,end):
scan = self.port_scan(stealth,port=port)
if scan:
ports_saved = {
"open": open_ports,
"filtered": filtered_ports,
"open/filtered": open_or_filtered
}
open_ports, filtered_ports, open_or_filtered = self.handle_port_response(ports_saved=ports_saved,response=scan,port=port)
if open_ports or filtered_ports or open_or_filtered:
total = len(open_ports) + len(filtered_ports) + len(open_or_filtered)
# print_figlet()
logging.info(f"Founded {total} ports!")
for port in open_ports:
logging.info(f"Port: {port} - Open")
for port in filtered_ports:
logging.warning(f"Port: {port} - Filtered")
for port in open_or_filtered:
logging.info(f"Port: {port} - Open/Filtered")
else:
scan = self.port_scan(stealth)
if scan:
ports_saved = {
"open": open_ports,
"filtered": filtered_ports,
"open/filtered": open_or_filtered
}
open_ports, filtered_ports, open_or_filtered = self.handle_port_response(ports_saved=ports_saved,response=scan,port=start)
if open_ports or filtered_ports or open_or_filtered:
total = len(open_ports) + len(filtered_ports) + len(open_or_filtered)
# print_figlet()
logging.info(f"Founded {total} ports!")
for port in open_ports:
logging.info(f"Port: {port} - Open")
for port in filtered_ports:
logging.debug(f"Port: {port} - Filtered")
for port in open_or_filtered:
logging.info(f"Port: {port} - Open/Filtered")
def os_scan(self):
target_os = os_detection.scan(self.target)
if target_os:
print("")
logging.info(f"Target OS: {target_os}")
else:
logging.warning("[[red]-[/red]]Error when scanning OS")
def send_icmp(self,target, result, index):
# print(f"[+]Sending ICMP request to {target}")
target = str(target)
host_found = []
pkg = IP(dst=target)/ICMP()
answers, unanswered = sr(pkg,timeout=3, retry=2,verbose=0,iface=self.interface if self.interface else None)
answers.summary(lambda r : host_found.append(target))
if host_found: result[index] = host_found[0]
def discover_net(self,ip_range=24):
protocol = self.protocol
base_ip = self.my_ip
# print_figlet()
if not protocol:
protocol = "ICMP"
else:
if protocol != "ICMP":
logging.warning(f"Warning: {protocol} is not supported by discover_net function! Changed to ICMP")
if protocol == "ICMP":
logging.info("Starting - Discover Hosts Scan")
base_ip = base_ip.split('.')
base_ip = f"{str(base_ip[0])}.{str(base_ip[1])}.{str(base_ip[2])}.0/{str(ip_range)}"
hosts = list(ipaddress.ip_network(base_ip))
bar = ChargingBar("Scanning...", max=len(hosts))
sys.stdout = None
bar.start()
threads = [None] * len(hosts)
results = [None] * len(hosts)
for i in range(len(threads)):
threads[i] = Thread(target=self.send_icmp,args=(hosts[i], results, i))
threads[i].start()
for i in range(len(threads)):
threads[i].join()
bar.next()
bar.finish()
sys.stdout = sys.__stdout__
hosts_found = [i for i in results if i is not None]
if not hosts_found:
logging.warn('[[red]-[/red]]Not found any host')
else:
print("")
logging.info(f'{len(hosts_found)} hosts founded')
for host in hosts_found:
logging.info(f'Host found: {host}')
return True
else:
logging.critical("[[red]-[/red]]Invalid protocol for this scan")
return False
def arguments():
parser = argparse.ArgumentParser(description="ASTSU - Network Tool",usage="\n\tastsu.py -sC 192.168.0.106\n\tastsu.py -sA 192.168.0.106")
parser.add_argument('-sC',"--scan-common",help="Scan common ports",action="count")
parser.add_argument('-sA',"--scan-all",help="Scan all ports",action="count")
parser.add_argument('-sO',"--scan-os",help="Scan OS",action="count")
parser.add_argument('-sP',"--scan-port",help="Scan defined port")
parser.add_argument('-sV',"--scan-service",help="Try to detect service running")
parser.add_argument('-d',"--discover",help="Discover hosts in the network",action="count")
parser.add_argument('-p',"--protocol",help="Protocol to use in the scans. ICMP,UDP,TCP.",type=str,choices=['ICMP','UDP','TCP'],default=None)
parser.add_argument('-i',"--interface",help="Interface to use",default=None)
parser.add_argument('-t',"--timeout",help="Timeout to each request",default=5,type=int)
parser.add_argument('-st',"--stealth",help="Use Stealth scan method (TCP)",action="count")
parser.add_argument('-v',"--verbose",action="count")
parser.add_argument('Target',nargs='?',default=None)
args = parser.parse_args()
if not args.discover and not args.Target:
sys.exit(parser.print_help())
if not args.scan_common and not args.scan_all and not args.scan_os and not args.scan_port and not args.discover:
sys.exit(parser.print_help())
return (args, parser)
if __name__ == '__main__':
args, parser = arguments()
del logging.root.handlers[:]
logging.addLevelName(logging.CRITICAL, f"[{red}!!{reset}]")
logging.addLevelName(logging.WARNING, f"[{red}!{reset}]")
logging.addLevelName(logging.INFO, f"[{cyan}*{reset}]")
logging.addLevelName(logging.DEBUG, f"[{cyan}**{reset}]")
logging.basicConfig(format="%(levelname)s%(message)s", level=logging.DEBUG if args.verbose else logging.INFO)
print_figlet()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
ip = s.getsockname()[0]
s.close()
scanner = Scanner(target=args.Target,my_ip=ip,protocol=args.protocol,timeout=args.timeout,interface=args.interface)
if args.scan_common:
scanner.common_scan(stealth=args.stealth,sv=args.scan_service)
elif args.scan_all:
scanner.range_scan(start=0,end=65535,stealth=args.stealth,sv=args.scan_service)
elif args.scan_port:
try:
scanner.range_scan(start=int(args.scan_port.split(',')[0]),end=int(args.scan_port.split(',')[1]),stealth=args.stealth,sv=args.scan_service)
except:
scanner.range_scan(start=args.scan_port,stealth=args.stealth,sv=args.scan_service)
elif args.discover:
scanner.discover_net()
else:
parser.print_help()
if args.scan_os:
scanner.os_scan()