-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathportscanner.py
executable file
·71 lines (54 loc) · 1.58 KB
/
portscanner.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
#!/usr/bin/python
# File : portscanner.py
# Author : Pranat Dayal
# Simple port scanner in python.
import sys
import socket
import threading
# Initiates a TCP connection with the provided port and IP
def tcp_conn(ip,port):
sock_t=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock_t.settimeout(0.5)
try:
output=sock_t.connect_ex((ip,port))
if output==0:
print "TCP Port {}: Open".format(port)
sock_t.close()
except Exception as e:
pass
# Sends a UDP datagram to a specific port
def udp_scan(ip,port):
data = "test"
sock_u=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
sock_u.sendto(data,(ip,port))
sock_u.settimeout(1.0)
output = sock_u.recvfrom(1024)
if output!=None:
print "UDP Port {}: Open".format(port)
except Exception as e:
pass
# Spawns an individual thread for the top 1024 ports
# Scans both udp and tcp
# Change 1024 to the number of ports you wanna scan
def scan_ports(ip):
threads_tcp = []
threads_udp = []
for i in range(1024):
t = threading.Thread(target=tcp_conn, args=(ip,i))
u = threading.Thread(target=udp_scan, args=(ip,i))
threads_tcp.append(t)
threads_udp.append(u)
for i in range(1024):
threads_tcp[i].start()
threads_udp[i].start()
for i in range(1024):
threads_tcp[i].join()
threads_udp[i].join()
if __name__=="__main__":
target = sys.argv[1]
print "Target IP : ", target
try:
scan_ports(target)
except Exception as e:
pass