forked from k8gege/K8tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
K8PortScan.py
132 lines (132 loc) · 3.71 KB
/
K8PortScan.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
import socket, sys
import threading
import argparse
import time
##Code: https://github.com/k8gege/K8PortScan
##K8portScan 1.0
##Date: 20190530
##Author: K8gege
##Usage:
##IP (IP IP/24 IP/16 IP/8)
##
##python K8PortScan.py -ip 192.11.22.29
##python K8PortScan.py -ip 192.11.22.29 -p 80-89
##python K8PortScan.py -ip 192.11.22.29/24 -p 80,445,3306
##
##IPlist (ip.txt ip24.txt ip16.txt ip8.txt)
##python K8PortScan.py -f ip.txt
##python K8PortScan.py -f ip.txt -p 80-89
##python K8PortScan.py -f ip24.txt -p 80,445,3306
def getPortBanner(ip, p):
try:
port=int(p)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if port==3306 or port==22 or port==23 or port==1521:
s.settimeout(5)
else:
s.settimeout(0.2)
s.connect((ip, port))
s.send('HELLO\r\n')
#print ip+"\t"+p+" Open"
print ip+"\t"+p+" Open\t"+s.recv(1024).split('\r\n')[0].strip('\r\n')
except Exception as e:
#print e
pass
finally:
s.close()
def GetPortsBanner(ip,ports):
for p in ports:
#print p
banner=getPortBanner(ip,str(p))
# if banner!=None:
# print ip+"\t"+banner
def CscanPortBanner(ip,ports):
if '/24' in ip:
print 'ip/24: '+ip
ipc = (ip.split('.')[:-1])
for i in range(1,256):
ip = ('.'.join(ipc)+'.'+str(i))
threading._start_new_thread(GetPortsBanner,(ip,ports,))
time.sleep(0.1)
else:
GetPortsBanner(ip,ports)
def BscanPortBanner(ip,ports):
if '/16' in ip:
print 'ip/16: '+ip
ipc = (ip.split('.')[:-2])
for i in range(1,256):
ip = ('.'.join(ipc)+'.'+str(i)+'.0/24')
CscanPortBanner(ip,ports)
def AscanPortBanner(ip,ports):
if '/8' in ip:
print 'ip/8: '+ip
ipc = (ip.split('.')[:-3])
for i in range(1,256):
ip = ('.'.join(ipc)+'.'+str(i)+'.0/16')
BscanPortBanner(ip,ports)
if __name__ == '__main__':
print('K8PortScan 1.0')
parser = argparse.ArgumentParser()
parser.add_argument('-ip',help='IP or IP/24')
parser.add_argument('-f', dest="ip_file", help="ip.txt ip24.txt ip16.txt ip8.txt")
parser.add_argument('-p', dest='port', type=str, help="Example: 80 80-89 80,443,3306,8080")
args = parser.parse_args()
ip=args.ip
tmpPorts = args.port
ipfile=args.ip_file
if ip==None and ipfile==None:
print 'Error: ip or ipfile is Null!'
print 'Help: -h or --help'
sys.exit(1)
if tmpPorts:
if ',' in tmpPorts:
ports = tmpPorts.split(',')
elif '-' in tmpPorts:
ports = tmpPorts.split('-')
tmpports = []
[tmpports.append(i) for i in range(int(ports[0]), int(ports[1]) + 1)]
ports = tmpports
else:
ports = [tmpPorts]
else:
print 'Default Ports'
ports = [21, 22, 23, 53, 80, 111, 139, 161, 389, 443, 445, 512, 513, 514,
873, 1025, 1433, 1521, 3128, 3306, 3311, 3312, 3389, 5432, 5900,
5984, 6082, 6379, 7001, 7002, 8000, 8080, 8081, 8090, 9000, 9090,
8888, 9200, 9300, 10000, 11211, 27017, 27018, 50000, 50030, 50070]
if ipfile!=None:
iplist = []
with open(str(ipfile)) as f:
while True:
line = str(f.readline()).strip()
if line:
iplist.append(line)
else:
break
if ipfile=='ip24.txt':
print 'Scan iplist/24'
for ip in iplist:
CscanPortBanner(ip+'/24',ports)
elif ipfile=='ip16.txt':
print 'Scan iplist/16'
for ip in iplist:
BscanPortBanner(ip+'/16',ports)
elif ipfile=='ip8.txt':
print 'Scan iplist/8'
for ip in iplist:
AscanPortBanner(ip+'/8',ports)
# elif ipfile=='ip.txt':
# print 'iplist'
else:
print 'Scan iplist (any txt file)'
for ip in iplist:
CscanPortBanner(ip,ports)
elif ip!=None:
if '/16' in ip:
BscanPortBanner(ip,ports)
elif '/8' in ip:
AscanPortBanner(ip,ports)
elif '/24' in ip:
CscanPortBanner(ip,ports)
else:
CscanPortBanner(ip,ports)