1
1
from scapy .all import *
2
+ import socket
2
3
3
- SYNACK_FLAG = 0x12
4
- RSTACK_FLAG = 0x14
4
+ # Silence scapy debug
5
+ conf . verb = 0
5
6
6
- # Config
7
- ports = range ( int (1 ), int ( 100 ) )
7
+ ip = "107.152.41.214"
8
+ port = int (22 )
8
9
9
10
10
- def is_online (ip ):
11
+ def is_online ():
12
+ reply = sr1 (IP (dst = ip , ttl = 20 ) / ICMP (), timeout = 2 )
13
+ return False if reply is None else True
14
+
15
+
16
+ def is_port_open (port ):
17
+ # Send SYN packet
18
+ response = sr1 (IP (dst = ip ) / TCP (dport = port , flags = "S" ), timeout = 2 , verbose = 0 )
19
+
11
20
try :
12
- print ("Pining... " , ip )
13
- ping = sr1 (IP (dst = ip ) / ICMP ())
14
- print (ping )
15
- return True
16
- except Exception :
17
- return False
18
-
19
-
20
- def scan (target , port ):
21
- src_port = RandShort ()
22
-
23
- print ("Checking Port: " , port )
24
- response = sr (IP (dst = target ) / TCP (sport = src_port , dport = port , flags = "S" ))
25
- # Extract flags of recived packet
26
- pktflags = response .getlayer (TCP ).flags
27
-
28
- if pktflags == SYNACK_FLAG :
29
- RSTpkt = IP (dst = target ) / TCP (sport = src_port , dport = port , flags = "R" )
30
- send (RSTpkt )
31
-
32
- print ("Port is OPEN: " , port )
33
- return True
34
- else :
35
- return False
21
+ # Check for response, if available check for ACK
22
+ if response .getlayer (TCP ).flags == "SA" :
23
+ return True
24
+ except AttributeError :
25
+ pass
26
+ return False
36
27
37
28
38
29
# Main()
39
30
if __name__ == "__main__" :
40
- target = "107.152.41.214"
41
-
42
- # Start
43
- # if not is_online(target):
44
- # print("@FAILED: Unable to to reach server, is it online?")
45
- # else:
46
- print ("Scanning ports..." )
31
+ print ("Checking if host is online..." )
47
32
48
- try :
49
- for port in ports :
50
- status = scan (target , port )
51
- if status == True :
52
- print ("Port " + str (port ) + ": Open" )
53
- except KeyboardInterrupt :
54
- print ("\n [*] User Requested Shutdown..." )
33
+ # Check if host is online
34
+ for i in range (5 ):
35
+ online = is_online ()
36
+
37
+ if online :
38
+ break
39
+
40
+ if online :
41
+ print ("Scanning ports..." )
42
+ open_ports = []
43
+
44
+ # Scan ports, cache index if open
45
+ for i in range (1 , 100 ):
46
+ if is_port_open (i ):
47
+ open_ports .append (i )
48
+
49
+ # Print open ports with related service
50
+ for port in open_ports :
51
+ print ("OPEN: %s => %s" % (port , socket .getservbyport (port , "TCP" )))
52
+ else :
53
+ print (f"Unable to ping host, is it online? ({ ip } )" )
0 commit comments