Created
May 31, 2026 15:05
-
-
Save horaciod/7803c585635a63d1d1a50b3bca58f766 to your computer and use it in GitHub Desktop.
parser anubis log, minimax version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| import sys | |
| import os | |
| import time | |
| import tracemalloc | |
| from collections import Counter | |
| from datetime import datetime | |
| def format_bytes(bytes_val): | |
| for unit in ['B', 'KB', 'MB', 'GB']: | |
| if bytes_val < 1024: | |
| return f"{bytes_val:.2f} {unit}" | |
| bytes_val /= 1024 | |
| return f"{bytes_val:.2f} TB" | |
| def parse_anubis_log(filepath): | |
| blocked_bots = Counter() | |
| blocked_pages = Counter() | |
| bot_block_techniques = {} | |
| first_time = None | |
| last_time = None | |
| line_count = 0 | |
| tracemalloc.start() | |
| start_time = time.time() | |
| with open(filepath, 'r') as f: | |
| for line in f: | |
| line = line.strip() | |
| if not line: | |
| continue | |
| entry = json.loads(line) | |
| line_count += 1 | |
| timestamp = entry.get('time', '') | |
| msg = entry.get('msg', '') | |
| if first_time is None: | |
| first_time = timestamp | |
| last_time = timestamp | |
| if msg == 'explicit deny': | |
| user_agent = entry.get('user_agent', 'Unknown') | |
| path = entry.get('path', 'Unknown') | |
| check_result = entry.get('check_result', {}) | |
| rule_name = check_result.get('name', 'Unknown') | |
| blocked_bots[user_agent] += 1 | |
| blocked_pages[path] += 1 | |
| if user_agent not in bot_block_techniques: | |
| bot_block_techniques[user_agent] = set() | |
| bot_block_techniques[user_agent].add(rule_name) | |
| end_time = time.time() | |
| current, peak = tracemalloc.get_traced_memory() | |
| tracemalloc.stop() | |
| return { | |
| 'first_time': first_time, | |
| 'last_time': last_time, | |
| 'blocked_bots': blocked_bots.most_common(), | |
| 'blocked_pages': blocked_pages.most_common(10), | |
| 'bot_block_techniques': bot_block_techniques, | |
| 'line_count': line_count, | |
| 'parse_time': end_time - start_time, | |
| 'peak_memory': peak, | |
| } | |
| def format_datetime(ts): | |
| if ts: | |
| dt = datetime.fromisoformat(ts.replace('Z', '+00:00')) | |
| return dt.strftime('%Y-%m-%d %H:%M:%S') | |
| return 'N/A' | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print(f"Usage: {sys.argv[0]} <log_file>") | |
| sys.exit(1) | |
| filepath = sys.argv[1] | |
| if not os.path.exists(filepath): | |
| print(f"Error: File '{filepath}' not found") | |
| sys.exit(1) | |
| file_size = os.path.getsize(filepath) | |
| results = parse_anubis_log(filepath) | |
| print("=" * 60) | |
| print("π ANUBIS LOG ANALYSIS") | |
| print("=" * 60) | |
| print(f"\nπ Log Time Range:") | |
| print(f" First Log: {format_datetime(results['first_time'])}") | |
| print(f" Last Log: {format_datetime(results['last_time'])}") | |
| print(f"\n{'=' * 60}") | |
| print("π€ BLOCKED BOTS (Top 20)") | |
| print("=" * 60) | |
| for bot, count in results['blocked_bots'][:20]: | |
| techniques = ', '.join(sorted(results['bot_block_techniques'].get(bot, ['Unknown']))) | |
| print(f" {count:>6} - {bot}") | |
| print(f" Technique: {techniques}") | |
| print(f"\n{'=' * 60}") | |
| print("π« TOP 10 BLOCKED PAGES") | |
| print("=" * 60) | |
| for page, count in results['blocked_pages']: | |
| print(f" {count:>6} - {page}") | |
| print(f"\n{'=' * 60}") | |
| print("βοΈ PARSER STATISTICS") | |
| print("=" * 60) | |
| lps = results['line_count'] / results['parse_time'] if results['parse_time'] > 0 else 0 | |
| print(f" π Lines processed: {results['line_count']:,}") | |
| print(f" πΎ File size: {format_bytes(file_size)}") | |
| print(f" β‘ Lines per second: {lps:,.2f}") | |
| print(f" π§ Peak memory used: {format_bytes(results['peak_memory'])}") | |
| if __name__ == '__main__': | |
| main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample output
`============================================================
π ANUBIS LOG ANALYSIS
π Log Time Range:
First Log: 2026-05-31 11:36:37
Last Log: 2026-05-31 13:25:50
============================================================
π€ BLOCKED BOTS (Top 20)
============================================================
π« TOP 10 BLOCKED PAGES
============================================================
β PARSER STATISTICS
π Lines processed: 21,843
πΎ File size: 13.08 MB
β‘ Lines per second: 14,515.47
π§ Peak memory used: 27.21 KB`