-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
279 lines (202 loc) · 7.27 KB
/
main.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
# -*- coding: utf-8 -*-
import boto3
import os
import sys
import subprocess
import urllib
import urlparse
import traceback
import copy
from time import sleep
import re
import requests
from multiprocessing import Pool
from threading import Thread
from Queue import Queue
from multiprocessing.pool import ThreadPool
from urlparse import urlparse
# You must specify cloudfront id.
cf_id = ""
# You must specify cloudfront hostname.
TARGET_HOST = ""
# You must specify cloudfront port.
TARGET_PORT =
PROTO = 'http'
input_data_folder = './in_data'
thread_num = 10
session = boto3.Session(profile_name="dev")
waf = session.client('waf') # waf = session.client('waf-regional')
cf = session.client('cloudfront')
def get_out_file_path(acl_name):
acl_name = re.sub(r'[-_ ]','',acl_name)
out_file_path = './out_data/out_' + acl_name + '.csv'
return out_file_path
def host_cant_resolve(method, req_sequence, request, in_data):
return {
"id": req_sequence,
"request": 'Host name can\'t resolved' + request,
"status": '404',
"method": method,
"in_data":in_data
}
def unknown_error(method, req_sequence, request, in_data, e, res):
return {
"id": req_sequence,
"request": str(e) + " " + request,
"status": '-1',
"method": method,
"in_data":in_data
}
def valid_response(method, req_sequence, request, test_data_file_name, status_code):
return {
"id": req_sequence,
"request": request,
"status": str(status_code),
"method": method,
"in_data": test_data_file_name
}
def build_out_put(res):
return ','.join( [str(res['id']), res['method'], '"'+res['request'].replace('"',"'")+'"', res['status'], res['in_data']+'\n'])
def do_request(req_sequence, req_raw_list, test_data_file_name):
# debug
if req_sequence % 100 == 0:
print(req_sequence)
request = ""
req_raw_list = map(lambda x: x.replace('localhost:8080', TARGET_HOST), req_raw_list)
# リクエストの中身を文字列化する。var_formatsはバイト文字列でバグるので無視する
if test_data_file_name.find('var_formats')<0:
request = '\\n'.join(req_raw_list)
res = req_raw_list.pop(0).split()
method = res[0]
url = res[1]
url = url.replace('http://', PROTO+'//')
# hack code. To skip unknown hostname(=cant resolve hostname).
try :
url_parsed = urlparse(url)
if url_parsed.hostname != TARGET_HOST:
return host_cant_resolve(method, req_sequence, request, test_data_file_name)
except Exception as e:
print(e)
# header
headers = {}
while True:
try :
header = req_raw_list.pop(0)
if not header:
break;
header = header.replace('localhost:8080', TARGET_HOST)
header = header.replace(' ','')
tmp_headers = header.split(':')
key = tmp_headers.pop(0)
val = ':'.join(tmp_headers)
headers[key] = val
except Exception as e:
print("Get header error.", e)
try:
data = '\n'.join(req_raw_list)
if method == 'GET':
res = requests.get(url, data=data, headers=headers, verify = False)
elif method == 'POST':
res = requests.post(url, data=data, headers=headers, verify = False)
return valid_response(method, req_sequence, request, test_data_file_name, res.status_code)
except Exception as e:
return unknown_error(method, req_sequence, request, test_data_file_name, e, res)
def do_test(input_name_list, out_data):
req_sequence = 1
req_list = []
input_name_list = sorted(input_name_list)
for input_file_path in input_name_list:
# Mac環境においては、憎き「.DS_Store」ファイルが勝手に作られるので無視する。
if input_file_path.find('DS_Store')>=0:
continue
with open(input_file_path) as input_file:
req_raw_list = []
lines = input_file.readlines()
lines = list(map(lambda x:x.replace('\n','').replace('\r',''), lines))
for line in lines:
if len(req_raw_list)>0 and ('GET' in line or 'POST' in line) :
req_list.append((req_sequence,req_raw_list, input_file_path))
req_raw_list = []
req_raw_list.append(line)
req_sequence = req_sequence + 1
continue
req_raw_list.append(line)
#最後のリクエストがループ内では処理されないので、ここで再度処理する。
req_list.append( (req_sequence, req_raw_list, input_file_path) )
req_sequence = req_sequence + 1
def do_request_wrapper(args):
return do_request(*args)
pool = ThreadPool(thread_num)
response_list = pool.map(do_request_wrapper, req_list)
pool.close()
response_list = sorted(response_list, key=lambda x: x['id'])
with open(out_data,'w') as out_file:
for res in response_list:
out_file.write(build_out_put(res))
#### メイン処理の開始地点 ####
# search input files.
input_name_list = []
for file_name in os.listdir(input_data_folder):
file_path = os.path.join(input_data_folder, file_name)
if os.path.isfile(file_path):
input_name_list.append(file_path)
acl_list = waf.list_web_acls()['WebACLs']
acl_list = sorted(acl_list, key=lambda x:x['Name'])
for acl in acl_list:
# Web ACLを1個ずつ切替えながら検査していく。
new_cf_conf = copy.deepcopy(cf_conf)
new_cf_conf['WebACLId'] = acl['WebACLId']
cf_dist = cf.get_distribution_config(Id=cf_id)
cf_conf = cf_dist['DistributionConfig']
cf_etag = cf_dist['ETag']
cf.update_distribution(
DistributionConfig=new_cf_conf,
Id=cf_id,
IfMatch=cf_etag
)
#浸透するのを念のためまつ
sleep(10)
# 結果の出力先をクリーンして(後に)新規作成。
out_data = get_out_file_path(acl['Name'])
if os.path.isfile(out_data):
os.remove(out_data)
# 検査実行
do_test(input_name_list, out_data)
print("end : " + acl['Name'])
#### 出力結果を1つのファイルにまとめる ####
out_file_path = get_out_file_path('all')
# ここではアウトプットファイルの枠だけ作成する。
with open(get_out_file_path(acl_list[0]['Name'])) as input_file:
# 結果の出力先をクリーンして新規作成
if os.path.isfile(out_file_path):
os.remove(out_file_path)
with open(out_file_path,'w') as out_file:
# write headers.
out_file.write('No.,method,request,data\n')
for line in input_file.readlines():
tmp = line.split(',')
del tmp[len(tmp)-2]
out_file.write(','.join(tmp))
# 前の処理で作ったアウトプットファイルの枠に、中身を突っ込んでいく。
for acl in acl_list:
# debug code.
print acl['Name']
in_lines1 = []
with open(out_file_path) as file:
in_lines1 = file.readlines()
in_lines2 = []
with open(get_out_file_path(acl['Name'])) as file:
in_lines2 = file.readlines()
with open(out_file_path,'w') as out_file:
for i in range(-1,len(in_lines2)):
line1 = in_lines1[i+1]
if i == -1:
out_file.write(','.join([line1.rstrip(),acl['Name']])+'\n')
else:
try:
line2 = in_lines2[i]
tmp_line2 = line2[line2.index('"')+1:] if line2.index('"') >= 0 else line2
tmp_line2 = tmp_line2[tmp_line2.index('"')+1:] if tmp_line2.index('"') >= 0 else tmp_line2
out_file.write(','.join([line1.rstrip(), tmp_line2.split(',')[1]])+'\n')
except Exception as e:
print(traceback.format_exc())