-
Notifications
You must be signed in to change notification settings - Fork 6
/
scan.py
162 lines (131 loc) · 4.19 KB
/
scan.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
# -*- coding: UTF-8 -*-
'''
Desprition:
自动化批量nmap使用
Author:
Sp4rkW https://sp4rkw.blog.csdn.net/
Modify:2019-08-05 14:42:20
'''
# -*- coding: utf-8 -*-
import datetime,time,openpyxl,sys,getopt,nmap,threading
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
sem=threading.Semaphore(4) #限定最大线程数
'''
Desprition:
函数 ReadExcel 用于将 nmap 数据写入 xlsl 文件,并且做自适应宽度调整
Parameters:
param nmap扫描之后的结果
url 目标站点url
starttime 开始的时间
Returns:
Null
Modify:2019-08-05 15:05:01
'''
def WriteExcel(param,url,starttime):
wb = Workbook()
sheet = wb.active
sheet.title = url
len1 = len(param)
for i in range(0, len1):
demo = param[i].split(';')
len2 = len(demo)
for j in range(0, len2):
sheet.cell(row=i+1, column=j+1, value=str(demo[j]))
col_width = [] #记录每一列的宽度
i = 0
for col in sheet.columns:
for j in range(len(col)):
if j == 0:
# 数组增加一个元素
col_width.append(len(str(col[j].value)))
else:
# 获得每列中的内容的最大宽度
if col_width[i] < len(str(col[j].value)):
col_width[i] = len(str(col[j].value))
i = i + 1
#设置列宽
for j in range(len(col_width)):
# 根据列的数字返回字母
col_letter = get_column_letter(j+1)
# 当宽度大于100,宽度设置为100
if col_width[j] > 100:
sheet.column_dimensions[col_letter].width = 100
# 只有当宽度大于10,才设置列宽
elif col_width[j] > 7:
sheet.column_dimensions[col_letter].width = col_width[j] + 2
wb.save('./'+url+'.xlsx')
print('#*# 文件:'+url+'.xlsx 已经输出完毕')
endtime = datetime.datetime.now()
print ('耗时:'+str((endtime - starttime).seconds)+'s')
'''
Desprition:
函数 NmapExcel 调用 python-nmap 使用 nmap 扫描
Parameters:
Null
Returns:
Null
Modify:2019-08-05 15:05:22
'''
def NmapExcel(param1,param2,url):
with sem:
nm = nmap.PortScanner() #创建nmap接口
# try:
starttime = datetime.datetime.now()
nm.scan(hosts=url, ports=param1, arguments=' -Pn -sS -sU '+param2)
nmap_data = nm.csv().split('\n')
WriteExcel(nmap_data,url,starttime)
# except Exception:
# print('扫描出错,当前扫描进行到:'+url)
'''
Desprition:
函数 StartFunc 用于输出相关参数的意义
Parameters:
Null
Returns:
Null
Modify:2019-08-05 15:08:52
'''
def StartFunc(argv):
parameter1 = ''
parameter2 = ''
try:
opts, args = getopt.getopt(sys.argv[1:],"hp:o:",["port=","other="])
except getopt.GetoptError:
print('python ./scan.py -p <需要扫描端口> -o <其他nmap参数>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('python ./scan.py -p <需要扫描端口> -o <其他nmap参数>')
sys.exit()
elif opt in ("-p", "--port"):
parameter1 = arg
elif opt in ("-o", "--other"):
parameter2 = arg
print('''
)\____________________________|¯¯¯¯¯|__,,,,,.
)_ |||||||||||||||||||||||||||||||||||||||||||||||||||||||| | |
/ _________________ __|__|
/}}}}}}}}}}/(__\¯\¯ |____|
/}}}}}}}}}}}/}}___\|
/}}}}}}}}}}}}}}/
/}}}}}}}}}}}}}}/
/}}}}}}}}}}}}}}/
/}}}}}}}}}}}}}}/
/}}}}}}}}}}}}}}/
/}}}}}}}}}}}}}}/
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
NmapToExcel version 1.1.0
time 2019-8-5
''')
print('#*# NmapToExcel 正在运行')
data = open('url.txt','r') #url.txt存储的是目标站点ip/url
website_urls = data.readlines()
if website_urls:
for url in website_urls:
threading.Thread(target=NmapExcel, args=(parameter1,parameter2,url.replace('\n',''))).start()
else:
print("未读取到目标站点,请先添加目标")
data.close()
if __name__ == "__main__":
StartFunc(sys.argv[1:])