-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-logparser.py
601 lines (491 loc) · 21.8 KB
/
access-logparser.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Apache Regex portion original credits to: https://leancrew.com/all-this/2013/07/parsing-my-apache-logs/
## https://gitlab.com/mikeramsey/access-log-parser
## How to use.
# Run the script from your account via manual or curl method. It autodetects the current user and defaults to the todays date if not argument for how many days ago it provided.
# For todays hits
# ./access-logparser.py
#
# For yesterdays aka 1 Days ago
# ./access-logparser.py 1
#
##python <(curl -s https://gitlab.com/mikeramsey/access-log-parser/-/raw/master/access-logparser.py || wget -qO - https://gitlab.com/mikeramsey/access-log-parser/-/raw/master/access-logparser.py) 1;
__author__ = "Michael Ramsey"
__version__ = "0.1.2"
__license__ = "GPL-3.0"
import os
import re
import sys
import time
from collections import Counter
from datetime import date, timedelta
from datetime import datetime
from os.path import join, isfile
import getpass
import glob
# import pathlib
# print('version is', sys.version)
def main():
script = sys.argv[0]
# filename = sys.argv[2]
# filenametest = "/home/example.com.access_log"
# username = 'server'
username = getpass.getuser()
# print(username)
# Define the day of interest in the Apache common log format. Default if not specified
try:
daysago = int(sys.argv[1])
# daysago = 0
except:
daysago = 0
the_day = date.today() - timedelta(daysago)
apache_day = the_day.strftime('[%d/%b/%Y:')
dcpumon_day = the_day.strftime('%Y/%b/%d')
# Set variables to empty
controlpanel = ''
domlogs_path = ''
try:
if os.path.isfile('/usr/local/cpanel/cpanel') | os.path.isfile(os.getcwd() + '/cpanel'):
controlpanel = 'Cpanel'
datetime_dcpumon = date.today().strftime('%Y/%b/%d') # 2020/Feb/10
# Current Dcpumon file
dcpumon_current_log = "/var/log/dcpumon/" + datetime_dcpumon # /var/log/dcpumon/2019/Feb/15
acesslog_sed = "-ssl_log"
if username == 'root':
domlogs_path = '/usr/local/apache/domlogs/'
else:
user_homedir = "/home/" + username
user_accesslogs = "/home/" + username + "/logs/"
domlogs_path = "/usr/local/apache/domlogs/" + username
elif os.path.isfile('/usr/bin/cyberpanel') | os.path.isfile(os.getcwd() + '/cyberpanel'):
controlpanel = 'CyberPanel'
acesslog_sed = ".access_log"
if username == 'root':
# Needs updated to glob all /home/*/logs/
domlogs_path2 = glob.glob('/home/*/logs/')
else:
# Get users homedir path
user_homedir = os.path.expanduser("~" + username)
domlogs_path = user_homedir + "/logs/"
except:
controlpanel = 'Control Panel not found'
# Define Output file
stats_output = open(os.getcwd() + '/stats.txt', "w")
if username == 'root' and controlpanel == 'CyberPanel':
# Needs updated to glob all /home/*/logs/
path = '/home/*/logs/*'
domlogs_path = glob.glob("/home/*/logs/")
print('Root CyberPanel Detected')
# Get list of dir contents
# logs_path_contents = glob.glob("/home/*/logs/*.access_log", recursive=True)
# Get list of files only from this directory
logs = glob.glob("/home/*/logs/*.access_log")
else:
# Define log path directory
path = domlogs_path
# Get list of dir contents
logs_path_contents = os.listdir(path)
# Get list of files only from this directory
logs = filter(lambda f: isfile(join(path, f)), logs_path_contents)
# Regex for the Apache common log format.
parts = [ # host %h :ip/hostname of the client 172.68.142.138
# indent %l (unused) :client identity via client's identd configuration -
# user %u :HTTP authenticated user ID -
# time %t :timestamp [09/Mar/2019:00:38:03 -0600]
# request "%r" :request method of request, resource requested, & protocol "POST /wp-login.php HTTP/1.1"
# status %>s :Apache status code 404
# size %b (careful,can be'-'):size of request in bytes, excluding headers 3767
# referrer "%{Referer}i" :Referer "https://www.google.com/"
# user agent "%{User-agent}i":User-Agent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
r'(?P<host>\S+)',
r'\S+',
r'(?P<user>\S+)',
r'\[(?P<time>.+)\]',
r'"(?P<request>.*)"',
r'(?P<status>[0-9]+)',
r'(?P<size>\S+)',
r'"(?P<referrer>.*)"',
r'"(?P<agent>.*)"',
]
pattern = re.compile(r'\s+'.join(parts) + r'\s*\Z')
# Regex for a feed request.
feed = re.compile(r'/all-this/(\d\d\d\d/\d\d/[^/]+/)?feed/(atom/)?')
# Regexes for internal and Google search referrers.
internal = re.compile(r'https?://(www\.)?example\.com.*')
google = re.compile(r'https?://(www\.)?google\..*')
# Regexes for Uptime Monitoring Robots
uptimeroboturl = re.compile(r'https?://(www\.)?uptimerobot\..*')
uptimerobot = re.compile(r'UptimeRobot')
# Change Apache log items into Python types.
def pythonized(d):
# Clean up the request.
d['request'] = d['request'].split()[1]
# Some dashes become None.
for k in ('user', 'referrer', 'agent'):
if d[k] == '-':
d[k] = None
# The size dash becomes 0.
if d['size'] == '-':
d['size'] = 0
else:
d['size'] = int(d['size'])
# Convert the timestamp into a datetime object. Accept the server's time zone.
(time, zone) = d['time'].split()
d['time'] = datetime.strptime(time, '%d/%b/%Y:%H:%M:%S')
return d
# Is this hit a page?
def ispage(hit):
# Failures and redirects.
hit['status'] = int(hit['status'])
if hit['status'] < 200 or hit['status'] >= 300:
return False
# Feed requests.
if feed.search(hit['request']):
return False
# Requests that aren't GET.
# if (hit['request'])[0:3] != 'GET':
# return False
# Images, sounds, etc.
if hit['request'].split()[1][-1] != '/':
return False
# Requests that aren't Head type. AKA uptime monitoring
if (hit['request'])[0:3] == 'HEAD':
return False
# Must be a page.
return True
# Is the referrer interesting? Internal and Google referrers are not.
def goodref(hit):
if hit['referrer']:
return not (google.search(hit['referrer'])
or internal.search(hit['referrer']))
else:
return False
# Is the user agent interesting? An uptime monitoring robot is not.
def goodagent(hit):
if hit['agent']:
return not (uptimerobot.search(hit['agent'])
or uptimeroboturl.search(hit['agent']))
else:
return False
# create a function which returns the value of a dictionary
def keyfunction(k):
return d[k]
# Initialize pages for top IP's
pages = []
# Initialize dictionaries for hit counters
post_request_dict = {}
get_request_dict = {}
wp_login_dict = {}
wp_cron_dict = {}
wp_xmlrpc_dict = {}
wp_admin_ajax_dict = {}
drupal_login_dict = {}
magento_login_dict = {}
joomla_login_dict = {}
vbulletin_login_dict = {}
opencart_login_dict = {}
prestashop_login_dict = {}
# Parse all the lines associated with the day of interest.
for log in logs:
file = os.path.join(path, log)
text = open(file, "r")
post_request_hit_count = 0
get_request_hit_count = 0
wp_login_hit_count = 0
wp_cron_hit_count = 0
wp_xmlrpc_hit_count = 0
wp_admin_ajax_hit_count = 0
drupal_hit_count = 0
magento_hit_count = 0
joomla_hit_count = 0
vbulletin_hit_count = 0
opencart_hit_count = 0
prestashop_hit_count = 0
for line in text:
if apache_day in line:
if re.match("(.*)(POST)(.*)", line):
post_request_hit_count = post_request_hit_count + 1
if re.match("(.*)(GET)(.*)", line):
get_request_hit_count = get_request_hit_count + 1
if re.match("(.*)(wp-login.php)(.*)", line):
wp_login_hit_count = wp_login_hit_count + 1
if re.match("(.*)(wp-cron.php)(.*)", line):
wp_cron_hit_count = wp_cron_hit_count + 1
if re.match("(.*)(xmlrpc.php)(.*)", line):
wp_xmlrpc_hit_count = wp_xmlrpc_hit_count + 1
if re.match("(.*)(admin-ajax.php)(.*)", line):
wp_admin_ajax_hit_count = wp_admin_ajax_hit_count + 1
if re.match("(.*)(user/login/)(.*)", line):
drupal_hit_count = drupal_hit_count + 1
if re.match("(.*)(admin_[a-zA-Z0-9_]*[/admin/index/index])(.*)", line):
magento_hit_count = magento_hit_count + 1
if re.match("(.*)(/administrator/index.php)(.*)", line):
joomla_hit_count = joomla_hit_count + 1
if re.match("(.*)(admincp)(.*)", line):
vbulletin_hit_count = vbulletin_hit_count + 1
if re.match("(.*)(/admin/index.php)(.*)", line):
opencart_hit_count = opencart_hit_count + 1
if re.match("(.*)(/admin[a-zA-Z0-9_]*$)(.*)", line):
prestashop_hit_count = prestashop_hit_count + 1
m = pattern.match(line)
if m is not None:
hit = m.groupdict()
else:
# print("re.search() returned None")
continue
# hit = m.groupdict()
if ispage(hit):
pages.append(pythonized(hit))
else:
continue
# print >> stats_output, log + "|" + line,
# print(log + "|" + line, end="", file=stats_output)
# print(wp_login_hit_count)
log = log.replace('-ssl_log', '', 1)
log = log.replace('.access_log', '', 1)
# wp_login_dict[log] = int(wp_login_hit_count)
# wp_cron_dict[log] = int(wp_cron_hit_count)
# wp_xmlrpc_dict[log] = int(wp_xmlrpc_hit_count)
# wp_admin_ajax_dict[log] = int(wp_admin_ajax_hit_count)
# Only add hit count to dictionary if not equal to '0'
if post_request_hit_count != '0':
post_request_dict[log] = int(post_request_hit_count)
if get_request_hit_count != '0':
get_request_dict[log] = int(get_request_hit_count)
if wp_login_hit_count != '0':
wp_login_dict[log] = int(wp_login_hit_count)
if wp_cron_hit_count != '0':
wp_cron_dict[log] = int(wp_cron_hit_count)
if wp_xmlrpc_hit_count != '0':
wp_xmlrpc_dict[log] = int(wp_xmlrpc_hit_count)
if wp_admin_ajax_hit_count != '0':
wp_admin_ajax_dict[log] = int(wp_admin_ajax_hit_count)
if drupal_hit_count != '0':
drupal_login_dict[log] = int(drupal_hit_count)
if magento_hit_count != '0':
magento_login_dict[log] = int(magento_hit_count)
if joomla_hit_count != '0':
joomla_login_dict[log] = int(joomla_hit_count)
if vbulletin_hit_count != '0':
vbulletin_login_dict[log] = int(vbulletin_hit_count)
if opencart_hit_count != '0':
opencart_login_dict[log] = int(opencart_hit_count)
if prestashop_hit_count != '0':
prestashop_login_dict[log] = int(prestashop_hit_count)
# print(log)
# print("Wordpress Logins => " + str(wp_login_hit_count))
# print("Wordpress wp-cron => " + str(wp_cron_hit_count))
# print("Wordpress xmlrpc => " + str(wp_xmlrpc_hit_count))
# print("Wordpress admin-ajax => " + str(wp_admin_ajax_hit_count))
# print("===============================================================")
text.close()
# print(pages, file=stats_output)
print(' ')
print('============================================')
print('Snapshot for ' + username)
print(time.strftime('%H:%M%p %Z on %b %d, %Y'))
if controlpanel == 'Cpanel' or controlpanel == 'CyberPanel':
print(controlpanel + " detected")
else:
print('No control Panel detected')
print('Accesslog path used: ' + path)
# print(dcpumon_current_log)
print('============================================')
d = post_request_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print(' ')
print('''Top POST requests for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
d = get_request_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Top GET requests for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
# Show the top 10 pages and the total.
print('''
Show top 10 pages %s''' % the_day.strftime('%b %d, %Y'))
pageviews = Counter(x['request'] for x in pages if goodagent(x))
pagestop10 = pageviews.most_common(10)
for p in pagestop10:
print(' %5d %s' % p[::-1])
print(' %5d total' % len(pages))
print('============================================')
# Show the top five referrers.
print('''
Show top 10 referrers %s''' % the_day.strftime('%b %d, %Y'))
referrers = Counter(x['referrer'] for x in pages if goodref(x))
referrerstop10 = referrers.most_common(10)
for r in referrerstop10:
print(' %5d %s' % r[::-1])
print(' %5d total' % sum(referrers.values()))
print('============================================')
# Show the top 10 IPs.
print('''
Show Top 10 IPs %s''' % the_day.strftime('%b %d, %Y'))
iphits = Counter(x['host'] for x in pages if goodagent(x))
iptop10 = iphits.most_common(10)
for p in iptop10:
print(' %5d %s' % p[::-1])
print(' %5d total hits' % sum(iphits.values()))
print('============================================')
# CMS Checks
print(' ')
print('CMS Checks')
print(' ')
print('Wordpress Checks')
print('============================================')
d = wp_login_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
# print(d)
print('''Wordpress Bruteforce Logins for wp-login.php %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print(' ')
d = wp_cron_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Wordpress Cron wp-cron.php(virtual cron) checks for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print(' ')
d = wp_xmlrpc_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Wordpress XMLRPC Attacks checks for xmlrpc.php for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print(' ')
d = wp_admin_ajax_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Wordpress Heartbeat API checks for admin-ajax.php for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
d = drupal_login_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Drupal Login Bruteforcing checks for user/login/ for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
d = magento_login_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print(
'''Magento Login Bruteforcing checks for admin pages /admin_xxxxx/admin/index/index for %s''' % the_day.strftime(
'%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
d = joomla_login_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Joomla Login Bruteforcing checks for admin pages /administrator/index.php for %s''' % the_day.strftime(
'%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
d = vbulletin_login_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''vBulletin Login Bruteforcing checks for admin pages admincp for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
d = opencart_login_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Opencart Login Bruteforcing checks for admin pages /admin/index.php for %s''' % the_day.strftime(
'%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
d = prestashop_login_dict
# Using dictionary comprehension to find list
# keys having value in 0 will be removed from results
delete = [key for key in d if d[key] == 0]
# delete the key
for key in delete: del d[key]
print('''Prestashop Login Bruteforcing checks for admin pages /adminxxxx for %s''' % the_day.strftime('%b %d, %Y'))
print(' ')
# sort by dictionary by the values and print top 10 {key, value} pairs
for key in sorted(d, key=keyfunction, reverse=True)[:10]:
print(' %5d %s' % (d[key], key))
print(' %5d total hits' % sum(dict.values(d)))
print('============================================')
if __name__ == '__main__':
main()