-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynscan.py
52 lines (43 loc) · 1.56 KB
/
synscan.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
from scapy.layers.inet import ICMP, IP, TCP, sr1
import socket
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
def icmp_probe(ip):
icmp_packet = IP(dst=ip) / ICMP()
resp_packet = sr1(icmp_packet, timeout=10)
return resp_packet is not None
def scan_port(ip, port, open_tcp_ports):
syn_packet = IP(dst=ip) / TCP(dport=port, flags='S')
resp_packet = sr1(syn_packet, timeout=10)
if resp_packet is not None:
if resp_packet.haslayer(TCP) and (resp_packet[TCP].flags & 0x12 == 0x12):
open_tcp_ports.append(port)
print(f'{ip}:{port} is open/{resp_packet.sport}')
else:
print(f'{ip}:{port} is closed')
else:
print(f'{ip}:{port} is filtered or no response')
def syn_scan(ip, start_port, end_port):
open_tcp_ports = []
with ThreadPoolExecutor(max_workers=10) as executor: #실행 속도 증가
futures = []
for port in range(start_port, end_port + 1):
futures.append(executor.submit(scan_port, ip, port, open_tcp_ports))
for future in futures:
future.result()
return open_tcp_ports
def scan(ip):
ip = socket.gethostbyname(ip)
start_port = 0
end_port = 65535
try:
if icmp_probe(ip):
open_ports = syn_scan(ip, start_port, end_port)
print('Syn Scan completed!')
return open_ports
else:
print('Failed to send ICMP packet')
return False
except Exception as e:
print('Error:', e)
return False