Simple Log File Analyzer

简介: #!/usr/bin/python # Simple Log File Analyzer by Valentin Hoebel # Version 1.
#!/usr/bin/python

# Simple Log File Analyzer by Valentin Hoebel
# Version 1.0 (6th June 2010)

# Contact me at valentin@xenuser.org
# Website: I am sure you will find me! :)
# ASCII FOR BREAKFAST

# Description:
# This tool helps you to find hack attempts
# within webserver log files (e.g. Apache2 access logs).

# Features:
# - Error handling
# - Scan a log file for four different attack types
# - Display a short scan report
# - Write scan results to a new log file
# - Easy to use (everything is simple and automated

# Usage example:
# scan_log.py -file vhost_access.log

# Known issue:
# XSS attempt discovery feature can be a little bit buggy.

# Tested with:
# Apache2 log files only. Sry guys! But I am sure
# that every other webserver log file will work aswell.

# Disclaimer:
# I am not responsible if this script or you cause any damage
# to your system. The memory consumption can become
# quite large and the generated reports very huge. So be sure
# you know what you are doing. I highly recommend you
# download your log files on a separate machine and
# analyze these files there.

# I know that there are much better tools, but well, I do
# this for learning and fun =)

# Attention: Tool is far away from being perfect, so don't rely a 100 percent on it.

# A BIG "Thank you!" to all who publish their awesome Python
# scripts online and help other ppl learning this language.

# Modify, distribute, share and copy this code in any way you like!

# Power to the cows!

import sys, string, re, time
from time import strftime, localtime

def print_usage():
    print ""
    print ""
    print "________________________________________________"
    print "Simple Log File Analyzer"
    print "by Valentin Hoebel (valentin@xenuser.org)"
    print ""
    print "Version 1.0 (6th June 2010)   ^__^"
    print "                              (oo)/________"
    print "                              (__)/        )/// "
    print "                                  ||----w |"
    print "Power to teh cows!                ||     ||"
    print "________________________________________________"
    print ""
    print "[!] Use parameter --help for help!"
    print ""
    print ""
    return
    
def print_help():
    print ""
    print ""
    print "________________________________________________"
    print "Simple Log File Analyzer"
    print "by Valentin Hoebel (valentin@xenuser.org)"
    print ""
    print "Version 1.0 (6th June 2010)   ^__^"
    print "                              (oo)/________"
    print "                              (__)/        )/// "
    print "                                  ||----w |"
    print "Power to teh cows!                ||     ||"
    print "________________________________________________"
    print ""
    print "The Simple Log File Analyzer helps you to find"
    print "common hack attempts within your webserver log."
    print ""
    print "Supported attacks:"
    print " - SQL Injection"
    print " - Local File Inclusion"
    print " - Remote File Inclusion"
    print " - Cross-Site Scripting"
    print ""
    print "This scanner doesn't find everything so don't"
    print "rely on it!"
    print ""
    print "Usage example:"
    print "scan_log.py -file vhost_access.log"
    print ""
    print "Options:"
    print " -file <file>   (starts the main analyzing function"
    print " --help         (displays this text)"
    print ""
    print "Features:"
    print " - Error handling"
    print " - Scan a log file for four different attack types"
    print " - Display a short scan report"
    print " - Write scan results to a new log file"
    print " - Easy to use (everything is simple and automated)"
    print ""
    print "Additional information:"
    print "I only tested this tool with Apache2 log files (up to 2000 lines)."
    print "It may happen that the tool crashes when the provided log"
    print "file is too big or contains too many lines/characters."
    print "Scanning a 4000 lines log file only takes one second."
    print ""
    print "Hint: The XSS discovery feature is a little bit buggy."
    print ""
    print ""
    return
    
def print_banner():
    print ""
    print ""
    print "________________________________________________"
    print "Simple Log File Analyzer"
    print "by Valentin Hoebel (valentin@xenuser.org)"
    print ""
    print "Version 1.0 (6th June 2010)   ^__^"
    print "                              (oo)/________"
    print "                              (__)/        )/// "
    print "                                  ||----w |"
    print "Power to teh cows!                ||     ||"
    print "________________________________________________"
    return

# Define the function for analyzing log files
def analyze_log_file(provided_file):
    # Defining some important vars
    sqli_found_list = {}
    lfi_found_list = {}
    rfi_found_list = {}
    xss_found_list = {}
    
    # I know, there are better methods for doing/defining this...
    sql_injection_1 = "UNION"
    sql_injection_2 = "ORDER"
    sql_injection_3 = "GROUP"
        
    local_file_inclusion_1 = "/etc/passwd"
    local_file_inclusion_2 = "/etc/passwd%20"
    local_file_inclusion_3 = "=../"
    
    remote_file_inclusion_1 = "c99"
    remote_file_inclusion_2 = "=http://"
    
    cross_site_scripting_1 = "XSS"
    cross_site_scripting_2 = "alert"
    cross_site_scripting_3 = "String.fromCharCode"
    cross_site_scripting_4 = "iframe"
    cross_site_scripting_5 = "javascript"
    
    print "[i] >>",  provided_file
    print "[i] Assuming you provided a readable log file."
    print "[i] Trying to open the log file now."
    print ""

    # Opening the log file
    try:
        f = open(provided_file,  "r")
    except IOError:
        print "[!] The file doesn't exist."
        print "[!] Exiting now!"
        print ""
        sys.exit(1)
    
    print "[i] Opening the log file was successfull."
    print "[i] Moving on now..."
    print ""
    
    # Storing every single line in a list
    line_list = [line for line in f]
    max_lines = len(line_list)
    print "[i] The file contains", max_lines,  "lines."
    print "[i] Now looking for possible hack attempts..."
    
    # Viewing every single line
    for x in xrange(1, max_lines):
        current_line = line_list[x:x+1]
        
        # For some strange list behaviour we convert the list into a string
        current_line_string = "".join(current_line)
        
        # Search for SQL injections 
        find_sql_injection_1 =  re.findall(sql_injection_1, current_line_string) 
        if len(find_sql_injection_1) != 0:
            sqli_found_list[x+1] = current_line_string        
        else:
            find_sql_injection_2 = re.findall(sql_injection_2,  current_line_string)
            if len(find_sql_injection_2) != 0:
                sqli_found_list[x+1] = current_line_string
            else:
                find_sql_injection_3 = re.findall(sql_injection_3,  current_line_string)
                if len(find_sql_injection_3) != 0:
                    sqli_found_list[x+1] = current_line_string 
    
        # Search for local file inclusions
        find_local_file_inclusion_1 =  re.findall(local_file_inclusion_1, current_line_string) 
        if len(find_local_file_inclusion_1) != 0:
            lfi_found_list[x+1] = current_line_string        
        else:
            find_local_file_inclusion_2 = re.findall(local_file_inclusion_2,  current_line_string)
            if len(find_local_file_inclusion_2) != 0:
                lfi_found_list[x+1] = current_line_string
            else:
                find_local_file_inclusion_3 = re.findall(local_file_inclusion_3,  current_line_string)
                if len(find_local_file_inclusion_3) != 0:
                    lfi_found_list[x+1] = current_line_string
                    
        # Search for remote file inclusions
        find_remote_file_inclusion_1 =  re.findall(remote_file_inclusion_1, current_line_string) 
        if len(find_remote_file_inclusion_1) != 0:
            rfi_found_list[x+1] = current_line_string        
        else:
            find_remote_file_inclusion_2 = re.findall(remote_file_inclusion_2,  current_line_string)
            if len(find_remote_file_inclusion_2) != 0:
                rfi_found_list[x+1] = current_line_string

        # Search for cross-site scripting attempts
        find_cross_site_scripting_1 = re.findall(cross_site_scripting_1,  current_line_string)
        if len(find_cross_site_scripting_1) != 0:
            xss_found_list[x+1] = current_line_string
        else:
            find_cross_site_scripting_2 = re.findall(cross_site_scripting_2,  current_line_string)
            if len(find_cross_site_scripting_2) != 0:
                xss_found_list[x+1] = current_line_string
            else:
                find_cross_site_scripting_3 = re.findall(cross_site_scripting_3,  current_line_string)
                if len(find_cross_site_scripting_3) != 0:
                    xss_found_list[x+1] = current_line_string
                else:
                    find_cross_site_scripting_4= re.findall(cross_site_scripting_4,  current_line_string)
                    if len(find_cross_site_scripting_4) != 0:
                        xss_found_list[x+1] = current_line_string
                    else:
                        find_cross_site_scripting_5 = re.findall(cross_site_scripting_5,  current_line_string)
                        if len(find_cross_site_scripting_5) != 0:
                            xss_found_list[x+1] = current_line_string

    # Close the file we opened recently
    f.close() 

    # Generating a short report
    print "[i] Done."
    print ""
    print "[#] Simple report for analyzed log file"
   
    check_for_sqli_attempts = len(sqli_found_list)
    if check_for_sqli_attempts > 0:
        print "[!]", check_for_sqli_attempts,  "SQL injection attempt(s) was/were found."
    else:
        print "[+] No SQL injection attempt was found."
    
    check_for_lfi_attempts = len(lfi_found_list)
    if check_for_lfi_attempts > 0:
        print "[!]",  check_for_lfi_attempts,  "local file inclusion attempt(s) was/were found."    
    else:
        print "[+] No local file inclusion attempt was found." 
 
    check_for_rfi_attempts = len(rfi_found_list)
    if check_for_rfi_attempts > 0:
        print "[!]",  check_for_rfi_attempts,  "remote file inclusion attempt(s) was/were found."       
    else:
        print "[+] No remote file inclusion attempt was found." 
   
    check_for_xss_attempts = len(xss_found_list)
    if check_for_xss_attempts > 0:
        print "[!]",  check_for_xss_attempts,  "cross-site scripting attempt(s) was/were found."    
    else:
        print "[+] No crosse-site scripting attempt was found." 
    
    
    # Now generate the report
    print ""
    print "[i] Generating report..."
    
    # Define variables for the report name
    time_string = strftime("%a_%d_%b_%Y_%H_%M_%S", localtime())
    time_string_for_report = strftime("%a the %d %b %Y, %H:%M:%S", localtime())
    name_of_report_file = provided_file + "_scan_report_from_" + time_string
    
    # Convert the ints to strings
    sqli_numbers = str(check_for_sqli_attempts)
    lfi_numbers = str(check_for_lfi_attempts)
    rfi_numbers = str(check_for_rfi_attempts)
    xss_numbers = str(check_for_xss_attempts)
    
    # Create the file
    generated_report = open(name_of_report_file,  "w")
    
    # Write the content    
    generated_report.write("/n")
    generated_report.write("Simple Log File Analyzer/n")
    generated_report.write("by Valentin Hoebel (valentin@xenuser.org)/n")
    generated_report.write("/n")
    generated_report.write("Version 1.0 (6th June 2010)   ^__^/n")
    generated_report.write("                              (oo)/________/n")
    generated_report.write("                              (__)/        )/// /n")
    generated_report.write("                                  ||----w |/n")
    generated_report.write("Power to teh cows!                ||     ||/n")
    generated_report.write("________________________________________________/n")
    generated_report.write("/n")
    generated_report.write("Scan report for " +provided_file +  " on " + time_string_for_report + "/n")
    generated_report.write("Hint: XSS attempt discovery feature might be a little bit buggy./n")
    generated_report.write("/n")
    generated_report.write("/n")
    generated_report.write("Number of possible SQL injection attempts found: " + sqli_numbers + "/n")
    generated_report.write("Number of possible local file inclusion attempts found: " + lfi_numbers + "/n")
    generated_report.write("Number of possible remote file inclusion attempts found: " + rfi_numbers + "/n")
    generated_report.write("Number of possible cross-site scripting attempts found: " + xss_numbers + "/n")
    generated_report.write("/n")
    generated_report.write("/n")
    if len(sqli_found_list) != 0:
        sqli_found_list_string = ""
        sqli_found_list_string = "".join(str(sqli_found_list))
        generated_report.write("Details for the SQL injection attempts (line, log entry)/n")
        generated_report.write("------------------------------------------------/n")
        generated_report.write(sqli_found_list_string)
        generated_report.write("/n")
        generated_report.write("/n")
        generated_report.write("/n")
    if len(lfi_found_list) != 0:
        lfi_found_list_string = ""
        lfi_found_list_string = "".join(str(lfi_found_list))
        generated_report.write("Details for the local file inclusion attempts (line, log entry)/n")
        generated_report.write("------------------------------------------------/n")
        generated_report.write(lfi_found_list_string)  
        generated_report.write("/n")
        generated_report.write("/n")
        generated_report.write("/n")
    if len(rfi_found_list) != 0:
        rfi_found_list_string = ""
        rfi_found_list_string = "".join(str(rfi_found_list))
        generated_report.write("Details for the remote file inclusion attempts (line, log entry)/n")
        generated_report.write("------------------------------------------------/n")
        generated_report.write(rfi_found_list_string) 
        generated_report.write("/n")
        generated_report.write("/n")
        generated_report.write("/n")
    if len(xss_found_list) != 0:
        xss_found_list_string = ""
        xss_found_list_string = "".join(str(xss_found_list))
        generated_report.write("Details for the cross-site scripting attempts (line, log entry)/n")
        generated_report.write("------------------------------------------------/n")
        generated_report.write(xss_found_list_string) 
        generated_report.write("/n")
        generated_report.write("/n")
        generated_report.write("/n") 
    
    # Close the file
    generated_report.close()
    print "[i] Finished writing the report."
    print "[i] Hint: The report file can become quite large."
    print "[i] Hint: The XSS attempt discovery feature might be a little bit buggy."
    print ""
    
    print "[i] That's it, bye!"
    print ""
    print ""
    return
    # End of the log file function
    
# Checking if argument was provided
if len(sys.argv) <=1:
    print_usage()
    sys.exit(1)
    
for arg in sys.argv:
    # Checking if help was called
    if arg == "--help":
        print_help()
        sys.exit(1)
    
    # Checking if  a log file was provided, if yes -> go!
    if arg == "-file":
        provided_file = sys.argv[2]
        print_banner()
        
        #  Start the main analyze function
        analyze_log_file(provided_file)
        sys.exit(1)
        
### EOF ###
相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
目录
相关文章
如何关掉Parsed mapper file日志打印
如何关掉Parsed mapper file日志打印
652 1
|
小程序
【小程序】报错:no such file or directory, access ‘wxfile://usr/miniprogramLog/log2‘
【小程序】报错:no such file or directory, access ‘wxfile://usr/miniprogramLog/log2‘
2569 0
|
7月前
|
运维 监控 安全
EventLog Analyzer:高效的Web服务器日志监控与审计解决方案
ManageEngine EventLog Analyzer是一款企业级Web服务器日志监控与审计工具,支持Apache、IIS、Nginx等主流服务器,实现日志集中管理、实时威胁检测、合规报表生成及可视化分析,助力企业应对安全攻击与合规挑战,提升运维效率。
374 1
|
7月前
|
监控 安全 搜索推荐
使用EventLog Analyzer进行日志取证分析
EventLog Analyzer助力企业通过集中采集、归档与分析系统日志及syslog,快速构建“数字犯罪现场”,精准追溯安全事件根源。其强大搜索功能可秒级定位入侵时间、人员与路径,生成合规与取证报表,确保日志安全防篡改,大幅提升调查效率,为执法提供有力证据支持。
264 0
|
7月前
|
存储 运维 监控
EventLog Analyzer与市场上其他日志审计工具相比有哪些优势?
企业设备激增导致日志管理困难,传统审计方式效率低、难合规。ManageEngine EventLog Analyzer 提供全类型日志集中采集、实时监控与智能分析,支持等保、GDPR等合规需求,具备轻量部署、安全加固、自动化告警与SIEM能力,助力企业提升安全运营与风险响应水平。
173 0
|
7月前
|
监控 安全 数据可视化
使用EventLog Analyzer进行Apache日志监控和日志分析
Apache日志分析是监控网站访问、安全与性能的关键手段,涵盖访问日志与错误日志。通过EventLog Analyzer可实现日志的集中管理、实时监控与可视化分析,支持多版本Apache及Tomcat,助力企业合规与安全防护。
286 0
|
安全 BI 网络安全
EventLog Analyzer 如何满足等保合规要求?密码有效期、产品日志保留、配置备份三大核心问题全面解答
EventLog Analyzer(ELA)助力企业满足网络安全等级保护要求,支持配置自动/手动备份、日志180天留存及密码策略管理,提升合规性与安全运营效率。
237 0
|
监控 安全 网络安全
使用EventLog Analyzer日志分析工具监测 Windows Server 安全威胁
Windows服务器面临多重威胁,包括勒索软件、DoS攻击、内部威胁、恶意软件感染、网络钓鱼、暴力破解、漏洞利用、Web应用攻击及配置错误等。这些威胁严重威胁服务器安全与业务连续性。EventLog Analyzer通过日志管理和威胁分析,有效检测并应对上述威胁,提升服务器安全性,确保服务稳定运行。
520 2
|
安全 网络安全 数据安全/隐私保护
auth required pam_tally2.so file=/var/log/tallylog onerr=fail deny=3 unlock_time=300 even_deny_root root_unlock_time=300 什么作用?
【8月更文挑战第2天】auth required pam_tally2.so file=/var/log/tallylog onerr=fail deny=3 unlock_time=300 even_deny_root root_unlock_time=300 什么作用?
426 1
|
关系型数据库 MySQL 数据库
MySQL 启动日志报错: File /mysql-bin.index not found (Errcode: 13 - Permission denied)
MySQL 启动日志报错: File /mysql-bin.index not found (Errcode: 13 - Permission denied)
1252 2