-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNscan.py
157 lines (125 loc) · 3.77 KB
/
Nscan.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
import requests
import IPy
import threading
import socket
import sys
import time
import re
import queue
def getTitle(url, port = 80):
"""
getTitle
Fix: May can get title from response message .
"""
res = requests.get("http://"+url+":"+str(port))
if(res.status_code == 200):
try:
title = re.findall("<title>(.*?)</title>", res.text)[0]
except:
title = ""
return title
def portScan(q):
"""
portScan use tcp socket connect .
"""
global datas
while True:
(ip, port) = q.get()
# print("Scaning "+ip+":"+str(port))
if port>65535:
print("Error: The port "+str(port)+"filed !")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex((ip, port))
if result == 0:
if port == 80: # fix :May can from response message judge http
title = getTitle(ip, port)
data = "[*] " + ip + ":"+str(port) + " OPNE " + title + "\n"
print(data)
datas.append(data)
q.task_done()
def scan(IPs, ports):
for i in IPs:
for p in ports:
i = i.strNormal()
p = int(p)
q.put((i,p))
def threadPool(thread_num):
print("[*] Start "+str(thread_num)+" threads scan !")
threads = []
for i in range(thread_num):
t = threading.Thread(target=portScan, args = (q,))
t.start()
threads.append(t)
# for thread in threads:
# thread.join()
def readfile():
with open(file, "r") as f:
pass
def getParaments():
"""
"""
pms = sys.argv[1:]
IPs = []
ports = [80, 8080, 3389]
file = "test_"+str(time.ctime())+".txt"
thread_num = 100
if len(pms) < 1:
print("[-] Please input paraments !")
help()
exit(0)
for pm in pms:
if (pm == "-h"):
help()
exit(0)
if pm == "-t":
IPs = IPy.IP(pms[pms.index(pm)+1])
if pm == "-r":
IPs = readfile(pm+1) //Fix
if pm == "-p":
pList = []
ports = pms[pms.index(pm)+1]
ports = ports.split(",")
for p in ports:
if "-" in p:
p2p = p.split("-")
for i in range(int(p2p[0]), int(p2p[1])+1):
pList.append(i)
else:
pList.append(p)
if pm == "-o":
file = pms[pms.index(pm)+1]
if pm == "--t":
tread_num = pms[pms.index(pm)+1]
return IPs, pList, file, thread_num
def output(file, datas = []):
try:
with open(file, "w") as f:
for data in datas:
print(data)
f.write(data)
f.close()
print("[*] The data successful saved "+file)
except:
pass
def help():
print("------------------------------------------------------------------------\n")
print("-h help")
print("-t target IP address")
print("-p port")
print(" -p 80,8000-9000")
print("-o output file ")
print("--t thread number default 100")
print()
print("Usage: python3 Nscan.py -t 192.168.100.0/24 -p 80,8080-10000 -o test.txt")
print("----------------------------------------------------------------------\n\n")
if __name__ == "__main__":
datas = []
stime = time.time()
[IPs, ports, file, thread_num] = getParaments()
lock = threading.Lock()
q = queue.Queue()
threadPool(thread_num)
scan(IPs, ports)
print("[*] Runing ...")
# output(file, datas) # FIX
# print("[*] This scan speed about "+str(time.time()-stime)+"s !")