This tool analyzes Apache/Nginx access log files to detect anomalies, generate statistics, and search for specific keywords in the logs. It is designed to help with security monitoring and log file analysis.
- Anomaly Detection: Identifies potential security issues like SQL injection, XSS attempts, and sensitive file access.
- Log Statistics: Provides a breakdown of the HTTP methods, status codes, and IP addresses accessing the server.
- Search Functionality: Allows you to search for specific IP addresses, URLs, or user-agent strings in the logs.
- Customizable Date Range: You can filter logs by start and end date.
- Anomalous Entries Filtering: Option to only show log entries with detected anomalies.
- Multiple Log Files Analysis: Analyze multiple log files at once.
- Suspicious User-Agent Report: Generate a report of suspicious User-Agents.
- Output to File: Save the analysis result to an output file.
- Graphical Representation: Display a graphical representation of attack patterns.
- Python 3.x
argparse
,re
,prettytable
,colorama
modules (all can be installed viapip
).
pip install -r requirements.txt
python log-analyzer.py -h
-
-f
,--file
: Required. Path to the log file to analyze.- Example:
--file access.log
- Example:
-
-a
,--only-anomalies
: Display only log entries with anomalies.- Example:
--only-anomalies
- Example:
-
-s
,--stats
: Display statistics about the log file (e.g., request count, IP frequencies).- Example:
--stats
- Example:
-
-sd
,--start-date
: Filter logs starting from this date (inclusive). Format:YYYY-MM-DD
.- Example:
--start-date 2023-01-01
- Example:
-
ed
,--end-date
: Filter logs up to this date (inclusive). Format:YYYY-MM-DD
.- Example:
--end-date 2023-12-31
- Example:
-
-fi
,--find
: Search for single or multiple keywords (URL, status, etc.).- Example:
--find pdf
,--find sql,200
- Example:
-
-rs
,--regex-search
: Search logs using a regex pattern.- Example:
--regex-search 'admin'
- Example:
-
--detect
{bruteforce,fileaccess,largefile,directorytraversal,sqli,xss,forbiddenaccess,csrf,ssrf,hightraffic} : Detect specific attack patterns.- Example:
--detect bruteforce
- Example:
-
-r
,--report
: Generate a summary report of suspicious IPs.- Example:
--report
- Example:
-
-ml
,--multi-log
: Analyze multiple log files.- Example:
--multi-log file1.log file2.log
- Example:
-
-ua
,--user-agent-report
: Generate a report of suspicious User-Agents.- Example: Example:
--user-agent-report
- Example: Example:
-
-o
,--output
: Output file to save the result. If not provided, the result will be displayed in the terminal.- Example:
-o output.txt
- Example:
-
-g
,--graph
: Display a graphical representation of attack patterns.- Example:
--graph
- Example:
-
Analyze logs and show anomalies:
python log_analyzer.py --file access.log --only-anomalies
-
Generate statistics for the log file:
python log_analyzer.py --file access.log --stats
-
Search for a specific IP address:
python log_analyzer.py --file access.log --find 185.160.71.3
-
Filter logs by date range:
python log_analyzer.py --file access.log --start-date 2023-01-01 --end-date 2023-12-31
-
Save the result to a file:
python log_analyzer.py --file access.log --output result.txt
-
Display a graphical representation of attack patterns:
python log_analyzer.py --file access.log --graph
When you run the tool, it will display or save a table of log entries with anomalies, including:
IP Address | Date | Method | URL | Status | Size | Anomalies | Rating |
---|---|---|---|---|---|---|---|
185.160.71.3 | 01/Jan/2023:12:00:00 | GET | /admin/config/ | 403 | 1500 | Directory traversal attempt, Sensitive file access attempt | 7 |
The statistics option will output the following:
=== Statistics ===
Total Requests: 5000
Top 5 IPs:
185.160.71.3: 300 requests
192.168.1.1: 200 requests
...
HTTP Methods:
GET: 3500
POST: 1000
...
HTTP Status Codes:
200: 4500
403: 300
500: 200
...
The graph option will output a graphical representation of attack patterns, such as:
====== Attack Graph ======
185.160.71.3 | ██████████████████████████████████████████████ 300
192.168.1.1 | ██████████████████████████████████████████ 200
This tool is released under the MIT License.