-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.py
446 lines (369 loc) · 13.7 KB
/
log.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import sys
import os
import re
import subprocess
import time
from functools import reduce
if sys.version.startswith("3"):
import io
io_method = io.BytesIO
else:
import cStringIO
io_method = cStringIO.StringIO
def parse_files(files, fields, filters):
tree, lines_total, filtered_total, start = {}, 0, 0, time.time()
for filename in files:
start_parse, count_lines, count_filtered = time.time(), 0, 0
with open(filename) as fp:
for line in fp:
count_lines += 1
data = parse_apache_line(line)
if filters_pass(data, filters) == False:
count_filtered += 1
continue
tmp = tree
for i, field in enumerate(fields):
isLast = True if i == len(fields) - 1 else False
val = ""
if type(field) is list:
for fi in field:
val += format_value(fi, data)
else:
val = format_value(field, data)
if tmp.get(val) == None:
tmp[val] = 0 if isLast else {}
if isLast:
tmp[val] += 1
tmp = tmp[val]
end_parse = time.time()
lines_total += count_lines
filtered_total += count_filtered
_total = (end_parse - start_parse)
print("[{:6.2f} s, {:10d} : (+{:10d}, -{:10d}) lines, {:9.2f} l/s] {}".format(
_total,
count_lines,
(count_lines - count_filtered),
count_filtered,
(count_lines / _total) if _total > 0 else 0,
filename,
))
sys.stdout.flush()
end = time.time()
return {
'time': (end - start),
'total': lines_total,
'filtered': filtered_total,
'tree': tree,
}
def filters_pass(data, filters):
def rules_pass(rules, currField=''):
if type(rules) is str:
return re.search(rules, data[currField], re.I) != None
if type(rules) is list:
for rule in rules:
if rules_pass(rule) == True:
return True
if type(rules) is dict:
for k, v in rules.items():
if (
type(v) is dict and rules_pass(v) == False
or type(v) is list and rules_pass(v) == False
or type(v) is str and rules_pass(v, k) == False
):
return False
return True
return False
if bool(filters.get('exclude')) and rules_pass(filters['exclude']) == True:
return False
if bool(filters.get('include')) and rules_pass(filters['include']) == False:
return False
return True
def parse_apache_line(line, sep=' '):
chunk = line.split(sep)
return {
'ip': (chunk[0]).strip(),
'date': (chunk[3][1:12]).strip(),
'code': (chunk[8] if len(chunk) >= 8 else "").strip(),
'method': (chunk[5][1:] if len(chunk) >= 5 else "").strip(),
'uri': (chunk[6] if len(chunk) >= 6 else "").strip(),
'protocol': (chunk[7][:-1] if len(chunk) >= 7 else "").strip(),
'request': (chunk[5][1:]+" "+chunk[6]+" "+chunk[7][:-1] if len(chunk) >= 7 else "").strip(),
'ua': (' '.join(chunk[11:]).strip()[1:-1] if len(chunk) >= 11 else ""),
'ref': (chunk[10][1:-1] if len(chunk) >= 11 else "").strip(),
}
def field_param(field):
chunk = field.split(':')
return {
'name': chunk[0],
'format': (chunk[1] if len(chunk) > 1 else ""),
}
def norm_date(date_str):
def replace_date(str, re):
return str.replace(re[1], '{}'.format(re[0]).zfill(2))
mounths = enumerate(['Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], 1)
replaced_date = reduce(replace_date, mounths, date_str.replace('\t', ''))
return '-'.join(replaced_date.split('/')[::-1])
def format_value(field, data):
f_param = field_param(field)
value = ('{0:' + f_param['format'] + '}').format(
data[f_param['name']]
)
return value + "\t"
def find_and_replace(tree, fields, field, find_name, fn):
new_tree = {}
col_separator = '\t'
field_names = []
if type(field) is list:
for fi in field:
field_names.append(field_param(fi)['name'])
else:
field_names.append(field_param(field)['name'])
try:
field_names.index(find_name)
for k, v in tree.items():
if type(field) is list:
ch = k.split(col_separator)
for i, name in enumerate(field_names):
if name == find_name:
ch[i] = fn(ch[i])
new_tree[col_separator.join(ch)] = v
else:
new_tree[fn(k)] = v
except Exception as err:
# print("Unexpected error:", err)
new_tree = tree
pass
return new_tree
def print_tree(tree, fields, pad="", level=0):
# Replace the date from '10/May/2018' to '2018-05-10' for sorting reason.
tree = find_and_replace(
tree,
fields,
fields[level],
'date',
lambda value: norm_date(value)
)
sum = 0
for item in sorted(tree):
val = tree[item]
if type(val) is dict:
print(pad + item)
sum += print_tree(val, fields, pad + "\t", level + 1)
else:
print("{}{}\t{}".format(pad, val, item))
sum += val
print(pad + "... total: " + str(sum) + "\n")
print()
return sum
def print_report(files, fields, filters={}):
print('\n---------\nGroup by : ', fields, '\n---------\n')
print('---------\nFilters : ', filters, '\n---------\n')
print()
sys.stdout.flush()
if len(files) == 0:
print("Files not found: {:s}".format(*sys.argv))
sys.exit(1)
res = parse_files(files, fields, filters)
print("\nTotal\n[{:6.2f} s, {:10d} : (+{:10d}, -{:10d}) lines, {:.2f} l/s]\n\n".format(
res['time'],
res['total'],
(res['total'] - res['filtered']),
res['filtered'],
(res['total'] / res['time']) if res['time'] > 0 else 0,
))
print_tree(res['tree'], fields)
def get_choice(choice_str, list_of_choices, default_num=None):
choices = list(enumerate(list_of_choices, 1))
include, exclude = [], []
def parse_choice(inpt):
user_unput = inpt.split('!')
index = int(user_unput[1]) if len(user_unput) == 2 else int(user_unput[0])
if index == '':
raise Exception('Enter was pressed')
if index <= len(choices) and index > -len(choices):
if index > 0:
index -= 1
num, setlected_set = choices[index]
# choice = setlected_set
if len(user_unput) == 2:
exclude.append(setlected_set)
else:
include.append(setlected_set)
else:
return False
return True
while True:
print(choice_str)
print("\n\t(Default num is:", default_num, ")")
for num, name in choices:
print("\t{}) {}".format(num, name))
print("Press Enter for use default value...")
try:
if parse_choice(input()) == True:
break
except:
print("Default setting")
if default_num != None:
parse_choice(str(default_num))
break
print("\nValues is: exclude = ", exclude, "include=", include)
print("\n\n")
def list_cases(arr):
if type(list_of_choices) is dict and len(arr) > 0:
for i, v in enumerate(arr):
arr[i] = list_of_choices[v]
return arr
return {
'include': list_cases(include),
'exclude': list_cases(exclude),
}
if __name__ == '__main__':
"""
'">-----X--S--S-------------------------S--Q--L----I--N--J-E--C-T-----
Hello friend!
This is a simple Apache log parser with a flexibly ability to group
entries by column and|or filter it. Set up printing as you like!
--------m--a--l--i--c--i--o--u--s----r--e--q--u--e--s--t--s--------<"'
"""
files = []
for filename in sys.argv[1:]:
if os.path.isfile(filename):
files.append(filename)
# FILTERS
# Common patterns for Wordpress cms
COMMON_PATTERN = {
# Wordpress static files and openstat label
'uri:openstat': {
'uri': r'/\?_openstat=',
},
'uri:wp_cron': {
'uri': r'^/wp-cron\.php\?'
},
'uri:wp_static_files': {
'uri': r'^/(?:wp-content|wp-includes)/[^\?#]+\.(css|js|woff|ttf|eot|svg|jpg|png|gif)+(\?|\?ve?r?=[\da-f\.]+)?$',
},
# Hackers shells
'hacker:uri:scan:shells': [
{'uri': r'\.(aasf|index\.php)'},
{'uri': r'xo='},
{'uri': r'L3dwLWNvbnRlbnQvcGx1Z2lucy8|cabee63d9736600fd19e3a724f9ddaeb'},
],
'hacker:uri:scan:dev_mistakes': {
'uri': r'\/\.(ftpconfig|env|svn|vscode|remote-sync|wp-config)',
},
}
skip_my_ip = {
'ip': r'(?:127.0.0.1|192.168.0.1)',
}
# Example 3 set of filters for 3 sites
site = {
'site1': [
# Common requests
COMMON_PATTERN['uri:openstat'],
COMMON_PATTERN['uri:wp_cron'],
COMMON_PATTERN['uri:wp_static_files'],
# Site Pages
{'uri': r'^/(\?utm_source=[^/?#.\\]+)?$'},
{'uri': r'(\?|&)yclid=[\d]+$'},
{'uri': r'^/favicon\.ico$'},
{'uri': r'^/sitemap_index\.xml$'},
{'uri': r'^/page-sitemap\.xml$'},
{'uri': r'^/sale/?(\?utm_source=[^/?#.\\]+)?$'},
{'uri': r'^/contacts?/?(\?utm_source=[^/?#.\\]+)?$'},
{'uri': r'^/(?:ge[a-z]+s|st[a-z]+s|tu[a-z]+s)/?$'},
{'uri': r'^/price-(?:ge[a-z]+r|st[a-z]+r|tu[a-z]+e)/?$'},
{'uri': r'^/remont-(?:ge[a-z]+v|st[a-z]+v|tu[a-z]+n)/?(\?utm_source=[^/?#.\\]+)?$'},
{'uri': r'^/dis[a-z]+t-(?:10|25|each-5)/?$'},
{'uri': r'^/gu[a-z]+e(?:-promo)?/?$'},
{'uri': r'^/tabs/price-tabs/?$'},
{'uri': r'^/za[a-z-]+t/?$'},
{'uri': r'^/wp-content/uploads/[^?#.\\]+\.webp$'},
],
'site2': [
# Common requests
COMMON_PATTERN['uri:openstat'],
COMMON_PATTERN['uri:wp_cron'],
COMMON_PATTERN['uri:wp_static_files'],
# Site Pages
{'uri': r'^/robots\.txt$'},
{'uri': r'^/favicon\.ico$'},
{'uri': r'^/apple[^/?#\\]+\.png$'},
# sitemaps
{'uri': r'^/(?:attachment|author|category|page|post|bwg_(?:album|gallery))-sitemap\.xml$'},
{'uri': r'^/sitemap_index\.xml$'},
# mix up pages
{'uri': r'^/(?:bio|music)/[^?#\\]+\.html$'},
# general pages
{'uri': r'^/(\?utm_source=[^/?#.\\]+)?$'},
{'uri': r'^/wp-content/uploads/[^?#.\\]+\.pdf$'},
{'uri': r'^/(?:our-objects|contact|about|galery|pr[a-z-]+)/?$'},
{'uri': r'^/(?:partners|bwg_(?:album|gallery)|accessory|installation|products)/?([^.?#\\]+(?:\.html(\?download=[\d]+|[^?#.\\]+)?|\/?))?$'},
],
'site3': [
# Site Pages
{'uri': r'^/$'},
{'uri': r'^/robots\.txt$'},
{'uri': r'^/favicon\.ico$'},
{'uri': r'^/css/style\.css$'},
{'uri': r'^/poisk[^/\\#?.]+te\.html.+'},
{'uri': r'^/(support|radio|music|song)(?:\.html|\/)?$'},
{'uri': r'^/js/[^\/?#\\]+\.js$'},
{'uri': r'^/(?:bio|music|song|short_story)/[^\/?#\\]+(?:\.html|\/)?$'},
{'uri': r'^/img/[a-z\d\-\_]+\.(?:png|jpg|gif)$'},
],
'hide bot': [
{'ua': r'bot|scan'},
],
}
group_set = [
['ip'],
['date'],
['uri'],
['ua'],
['ref'],
#
['date', 'ip'],
['date', 'uri'],
['date', 'ua'],
['date', 'uri', 'ua'],
['date', 'uri', ['ua:50', 'ip:20']],
['date', 'ref'],
#
[['code', 'method', 'uri:100']],
# Grouping for precise find normal requests
[['code', 'method', 'uri:100'], 'ip:20'],
['ip:20', ['code', 'method', 'uri:100']],
['ref:50', ['code', 'method', 'uri:100']],
['ref:50', ['code', 'method', 'uri:100'], 'ip:20'],
['ua', ['code', 'method', 'uri:100']],
['ua', ['ip']],
# Grouping for daily view
['date', ['code', 'method', 'uri:100'], 'ip:20'],
['date', 'ip:20', ['code', 'method', 'uri:100']],
['date', 'ip:20', ['code', 'method', 'uri:100', 'ref']],
]
# Select groups
groups = get_choice("=== Select columns for grouping:", group_set, 10)
# sys.exit(0)
# Select filters
filters = get_choice(
"=== Select set of exclude filters (!num for include):", site)
print_report(files, groups['include'][0], {
'exclude': ([skip_my_ip] + filters['include']),
'include': filters['exclude'],
})
# Extract only important
# print_report(files, ['date', ['code', 'method', 'uri:100'], 'ip:20'], {
# print_report(files, [['code', 'method', 'uri:100'], 'ip:20'], {
# 'exclude': [skip_my_ip],
# 'include': {'uri': r'^/wp-admin/admin-ajax.php'},
# })
# print_report(files, ['uri:100', ['code', 'method', 'protocol'], 'ip:20'], {
# 'include': {'ip': r'^141.8.132.\d{1,3}$'}, # yandex bot
# })
# Suppress an error in the terminal. (How to do better?)
sys.stdout.flush()
sys.stdout.close()
sys.stderr.flush()
sys.stderr.close()