-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxfscan.py
executable file
·388 lines (256 loc) · 10.1 KB
/
xfscan.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# -*- coding: utf-8 -*-
# Author: zhanglong@μredteam, yankai@μredteam
# from asyncore import file_dispatcher
import os
import shutil
from sys import stderr, stdout
import time
import asyncio
from aiomultiprocess import Pool
import logging
from fscanOutput2Csv import OpenFile, OpenPort
'''
===== Global Variable Start =====
'''
ip_1_list = [
"10.10.0.1/19",
"10.20.0.1/25"
]
ip_2_list = [
"10.3.0.1/24",
"10.3.2.1/24",
"10.3.4.1/24",
"10.3.11.1/24",
]
# all ip segments that ready to scan
ip_list = ip_1_list + ip_2_list
# fscan
basicPath = os.getcwd()
fs_exe = basicPath+"\\fscan.exe"
# scan result path
scanResult_path = basicPath + "\\scanResult"
# every fscan process output the temp scan result to this directory
scanResult_tmp_path = scanResult_path + "\\tmp"
banner = r"""
__ __ ____ ____ ____ ____ __ _
\ \/ /| ===|(_ (_`/ (__` / () \ | \| |
/_/\_\|__| .__)__)\____)/__/\__\|_|\__|
zhanglong@μredteam, yankai@μredteam
Built upon fscan/fscanoutput project.
shadow1ng@fscan [https://github.com/shadow1ng/fscan]
ZororoZ@fscanoutput [https://github.com/ZororoZ/fscanOutput]
"""
"""
===== Global Variable End =====
"""
def mkdir(path):
"""
input:
path: directory name
func: make directory.
output:
"""
folder_exist = os.path.exists(path)
if not folder_exist:
os.makedirs(path)
print(f"[+] Now we get folder: {path}")
return
def init_dir():
"""
input:
func: invoke mkdir() to init directories which are defined in "styleList".
output:
"""
styleList = [
"\\Bug_ExpList",
"\\Bug_PocList",
"\\Finger",
"\\OpenPort",
"\\OsList",
"\\Title",
"\\WeakPasswd",
"\\ScanLogs"
]
mkdir(scanResult_path)
mkdir(scanResult_tmp_path)
for i in styleList:
mkdir(scanResult_path + i)
return
def handle_ip4Scan(ips):
"""
input:
ips: ip lists that contain various netmask.
func: Process the incoming IP lists. If the mask is less than 24, it will be divided into multiple C segments for scanning. If it is equal to 24, it will be scanned directly.
output:
ip_to_scan: an ip list that all netmasks are /24.
"""
ip_to_scan = []
for i in ips:
netmask = int(i.split('/')[1])
if netmask < 24:
ip_prefix = f"{i.split('.')[0]}.{i.split('.')[1]}"
ip_var = int(i.split('.')[2])
# print(ip_prefix)
c_num = pow(2, (32 - netmask))//256
for a in range(0, c_num):
# get new IP/24
ip_to_scan.append(f"{ip_prefix}.{ip_var + a}.1/24")
# print(f"{ip_prefix}.{ip_var + a}.1/24")
elif netmask > 24:
ip_to_scan.append(i)
# print("[+] it can scan directly")
else:
ip_to_scan.append(i)
# print("[+] it is normal")
print(
f"[+] Now we get \033[31;1m{len(ip_to_scan)}\033[0m network segments to scan.")
# print(ip_to_scan)
# for i in ip_to_scan:
# print(i)
return ip_to_scan
async def run_fs(ips):
"""
input:
ips: ip lists that netmasks all are /24.
func: main function that invoke fscan, using asyncio.
output:
"""
ips = ips.strip()
# print(ips)
if ips == "10.75.0.1/24":
print(f"[!] Damn, it is printer ip segments: {ips} , we need ignore it...")
else:
# res = asyncio.subprocess.create_subprocess_exec()
# you can customize the command here.
# scan_cmd = "ping" + f" -w 1 -n 1 {ips.replace('/24','')} | findstr /i 'ttl='"
scan_cmd = fs_exe + f" -np -nobr -h {ips} -o {scanResult_tmp_path}\\{ips.split('/')[0].replace('.','_')}_24.txt"
# scan_cmd = fs_exe + \
# f" -np -nopoc -nobr -p 22,135,445,5985,80,443,8443,8080,3389 -h {ips} -o {scanResult_tmp_path}\\{ips.split('/')[0].replace('.','_')}_24.txt"
scan_start_time = time.time()
proc = await asyncio.subprocess.create_subprocess_shell(
scan_cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if not stdout:
print(f"[-] No stdout, check the fscan process, task_id is {ips}")
return
if stdout:
try:
resultList = []
# print(f"[!] Now {ips}")
singleResult = str(stdout.decode())
for i in singleResult.split("\n"):
resultList.append(i)
# print(f"[+] Task {ips} is completely: {resultList[-3]}")
print(f"{resultList[-1]} ==> \033[31;1m{ips}\033[0m ==> Count: \033[31;1m{resultList[-2]}\033[0m")
# logging.info(f"{resultList[-1]} ==> {ips} ==> Count: {resultList[-2]}")
loginfo = f"{resultList[-1]} ==> {ips} ==> Count: {resultList[-2]}"
return loginfo
# pass
# print(str(stdout))
# except Exception as e:
# print(e)
except UnicodeDecodeError as u:
scan_end_time = time.time()
scan_costs_time = scan_end_time - scan_start_time
# print(f"[-] Decoding error on \033[31;1m{ips}\033[0m, but scan works and it costs \033[31;1m{scan_costs_time}\033[0m")
print(f"[*] 扫描结束,耗时: {round(scan_costs_time, 7)}s ==> \033[31;1m{ips}\033[0m ==> Count: Decode error!")
# logging.info(f"[*] 扫描结束,耗时: {round(scan_costs_time, 7)}s ==> {ips} ==> Count: Decode error!")
loginfo2 = f"[*] 扫描结束,耗时: {round(scan_costs_time, 7)}s ==> {ips} ==> Count: Decode error!"
return loginfo2
# print(u)
pass
if stderr:
pass
# scan_end_time = time.time()
# scan_costs_time = scan_end_time - scan_start_time
# print(f"[+] Task \033[31;1m{ips}\033[0m is completely, it costs \033[31;1m{scan_costs_time}s\033[0m")
return
def merge_result2csv():
"""
input:
func: merge all result files in tmp
output:
"""
txtName = f"fscan_result_{time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())}.txt"
txtList = os.listdir(scanResult_tmp_path)
with open(f"{scanResult_path}\\{txtName}", 'w', encoding='utf-8-sig', errors='ignore') as f:
for i in txtList:
txtPath = f"{scanResult_tmp_path}\\{i}"
for line in open(txtPath, encoding='utf-8-sig', errors='ignore'):
f.writelines(line)
# os.system(
# f"python {os.getcwd()}\\fscanOutput2Csv.py {scanResult_path}\\{txtName}")
#os.remove(f"{scanResult_path}\\{txtName}")
#shutil.rmtree(scanResult_tmp_path)
return txtName
def outputCsv(csv_savepath, scan_result):
"""
input:
csv_savepath: the directory that output the csv report.
scan_result: scan result that merge all ip segments.
func: output csv scan report.
output:
"""
import fscanOutput2Csv
list1, str1 = fscanOutput2Csv.OpenFile(csv_savepath, scan_result)
filepath = csv_savepath
fscanOutput2Csv.OpenPort(list1, filepath)
fscanOutput2Csv.Bug_ExpList(list1, filepath)
fscanOutput2Csv.Bug_PocList(list1, filepath)
fscanOutput2Csv.OsList(list1, filepath)
fscanOutput2Csv.GetTitle(list1, filepath)
fscanOutput2Csv.GetPassword(list1, filepath)
fscanOutput2Csv.FingerOut(list1, filepath)
print(f"[+] Now we get final scan result, check it in \033[31;1m{scanResult_path}\033[0m.")
return
async def entry(ips):
async with Pool(os.cpu_count()) as pool:
result = await pool.map(run_fs, ips)
if result:
for i in result:
logging.info(i)
# print("[+] Scan complete.")
return result
if __name__ == "__main__":
print(banner)
# init the directory
init_dir()
# logging
if not os.path.exists(f"{scanResult_path}\\ScanLogs\\info.log"):
fo = open(f"{scanResult_path}\\ScanLogs\\info.log", "a")
fo.close()
logging.basicConfig(
level = logging.INFO,
filename = f"{scanResult_path}\\ScanLogs\\info.log",
filemode = 'a',
format = '%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
)
logging.info("[+] Now start to scan!")
# get ip lists that netmask is /24
iplist = handle_ip4Scan(ip_list)
# 生成的 iplist_2 是一个嵌套二维数组,里面有 8 个元素,每个元素含 32 个 C 段
step = 32
iplist_2 = [iplist[i:i+step] for i in range(0, len(iplist),step)]
# print(iplist_2[0])
# print(len(iplist_2[0]))
# print(os.cpu_count())
start_time = time.time()
# task_1 = asyncio.ensure_future(entry(iplist))
for ip_segments_2_scan in iplist_2:
print(f"[+] Now scan {ip_segments_2_scan}")
print(f"[+] the segments length is {len(ip_segments_2_scan)} !!!!")
task_1 = asyncio.ensure_future(entry(ip_segments_2_scan))
loop = asyncio.get_event_loop()
loop.run_until_complete(task_1)
# output the result.
print("[+] Now output the csv report...")
logging.info("[+] Now output the csv report...")
outputCsv(scanResult_path, merge_result2csv())
end_time = time.time()
print("[+] Task complete.")
print(f"[+] All tasks cost \033[31;1m{end_time - start_time}s\033[0m.")
print(f"[+] It costs \033[31;1m{end_time - start_time}s\033[0m!")
logging.info(f"[+] All task complete. It costs {end_time - start_time}s!")