11#!/usr/bin/python
22# -*- coding: utf-8 -*-
3- # Originally based on code from: https://leancrew.com/all-this/2013/07/parsing-my-apache-logs/
3+ # Apache Regex portion original credits to: https://leancrew.com/all-this/2013/07/parsing-my-apache-logs/
4+
5+ __author__ = "Michael Ramsey"
6+ __version__ = "0.1.0"
7+ __license__ = "GPL-3.0"
48
59import os
610import re
@@ -21,10 +25,10 @@ def main():
2125 # filenametest = "/home/example.com.access_log"
2226 # username = 'server'
2327 username = str (sys .argv [1 ])
24- # Define the day of interest in the Apache common log format.
28+ # Define the day of interest in the Apache common log format. Default if not specified
2529 try :
2630 daysago = int (sys .argv [2 ])
27- # daysago = 4
31+ # daysago = 0
2832 except :
2933 daysago = 0
3034 the_day = date .today () - timedelta (daysago )
@@ -37,7 +41,7 @@ def main():
3741
3842 try :
3943 if os .path .isfile ('/usr/local/cpanel/cpanel' ) | os .path .isfile (os .getcwd () + '/cpanel' ):
40- controlpanel = 'cpanel '
44+ controlpanel = 'Cpanel '
4145 datetime_dcpumon = date .today ().strftime ('%Y/%b/%d' ) # 2020/Feb/10
4246 # Current Dcpumon file
4347 dcpumon_current_log = "/var/log/dcpumon/" + datetime_dcpumon # /var/log/dcpumon/2019/Feb/15
@@ -50,7 +54,7 @@ def main():
5054 domlogs_path = "/usr/local/apache/domlogs/" + username
5155
5256 elif os .path .isfile ('/usr/bin/cyberpanel' ) | os .path .isfile (os .getcwd () + '/cyberpanel' ):
53- controlpanel = 'cyberpanel '
57+ controlpanel = 'CyberPanel '
5458 acesslog_sed = ".access_log"
5559 if username == 'server' :
5660 # Needs updated to glob all /home/*/logs/
@@ -228,14 +232,27 @@ def keyfunction(k):
228232 continue
229233 # print >> stats_output, log + "|" + line,
230234 # print(log + "|" + line, end="", file=stats_output)
231-
235+ # print(wp_login_hit_count)
232236 log = log .replace ('-ssl_log' , '' , 1 )
233237 log = log .replace ('.access_log' , '' , 1 )
234238
235- wp_login_dict [log ] = int (wp_login_hit_count )
236- wp_cron_dict [log ] = int (wp_cron_hit_count )
237- wp_xmlrpc_dict [log ] = int (wp_xmlrpc_hit_count )
238- wp_admin_ajax_dict [log ] = int (wp_admin_ajax_hit_count )
239+ # wp_login_dict[log] = int(wp_login_hit_count)
240+ # wp_cron_dict[log] = int(wp_cron_hit_count)
241+ # wp_xmlrpc_dict[log] = int(wp_xmlrpc_hit_count)
242+ # wp_admin_ajax_dict[log] = int(wp_admin_ajax_hit_count)
243+
244+ # Only add hit count to dictionary if not equal to '0'
245+ if wp_login_hit_count != '0' :
246+ wp_login_dict [log ] = int (wp_login_hit_count )
247+
248+ if wp_cron_hit_count != '0' :
249+ wp_cron_dict [log ] = int (wp_cron_hit_count )
250+
251+ if wp_xmlrpc_hit_count != '0' :
252+ wp_xmlrpc_dict [log ] = int (wp_xmlrpc_hit_count )
253+
254+ if wp_admin_ajax_hit_count != '0' :
255+ wp_admin_ajax_dict [log ] = int (wp_admin_ajax_hit_count )
239256
240257 # print(log)
241258 # print("Wordpress Logins => " + str(wp_login_hit_count))
@@ -250,7 +267,7 @@ def keyfunction(k):
250267 print ('============================================' )
251268 print ('Snapshot for ' + username )
252269 print (time .strftime ('%H:%M%p %Z on %b %d, %Y' ))
253- if controlpanel == 'cpanel ' or controlpanel == 'cyberpanel ' :
270+ if controlpanel == 'Cpanel ' or controlpanel == 'CyberPanel ' :
254271 print (controlpanel + " detected" )
255272 else :
256273 print ('No control Panel detected' )
@@ -297,7 +314,15 @@ def keyfunction(k):
297314 print ('============================================' )
298315
299316 d = wp_login_dict
317+ # Using dictionary comprehension to find list
318+ # keys having value in 0 will be removed from results
319+ delete = [key for key in d if d [key ] == 0 ]
320+
321+ # delete the key
322+ for key in delete : del d [key ]
323+
300324 # print(d)
325+
301326 print ('''Wordpress Bruteforce Logins for wp-login.php %s''' % the_day .strftime ('%b %d, %Y' ))
302327 print (' ' )
303328 # sort by dictionary by the values and print top 10 {key, value} pairs
@@ -307,6 +332,12 @@ def keyfunction(k):
307332 print (' ' )
308333
309334 d = wp_cron_dict
335+ # Using dictionary comprehension to find list
336+ # keys having value in 0 will be removed from results
337+ delete = [key for key in d if d [key ] == 0 ]
338+
339+ # delete the key
340+ for key in delete : del d [key ]
310341
311342 print ('''Wordpress Cron wp-cron.php(virtual cron) checks for %s''' % the_day .strftime ('%b %d, %Y' ))
312343 print (' ' )
@@ -317,6 +348,12 @@ def keyfunction(k):
317348 print (' ' )
318349
319350 d = wp_xmlrpc_dict
351+ # Using dictionary comprehension to find list
352+ # keys having value in 0 will be removed from results
353+ delete = [key for key in d if d [key ] == 0 ]
354+
355+ # delete the key
356+ for key in delete : del d [key ]
320357
321358 print ('''Wordpress XMLRPC Attacks checks for xmlrpc.php for %s''' % the_day .strftime ('%b %d, %Y' ))
322359 print (' ' )
@@ -327,6 +364,12 @@ def keyfunction(k):
327364 print (' ' )
328365
329366 d = wp_admin_ajax_dict
367+ # Using dictionary comprehension to find list
368+ # keys having value in 0 will be removed from results
369+ delete = [key for key in d if d [key ] == 0 ]
370+
371+ # delete the key
372+ for key in delete : del d [key ]
330373
331374 print ('''Wordpress Heartbeat API checks for admin-ajax.php for %s''' % the_day .strftime ('%b %d, %Y' ))
332375 print (' ' )
@@ -338,4 +381,4 @@ def keyfunction(k):
338381
339382
340383if __name__ == '__main__' :
341- main ()
384+ main ()
0 commit comments