#!/usr/bin/env python3
"""
SIEM مبسط - نظام جمع وتحليل السجلات
يجمع ويحلل logs من 3 مصادر ويكتشف الأنماط المشبوهة
المصادر:
1. Nginx Access Logs
2. Auth/SSH Logs
3. Application Logs
"""
import re
import json
import os
from datetime import datetime, timedelta
from collections import defaultdict
from typing import Dict, List, Tuple
import hashlib
# ═══════════════════════════════════════════════════════════════
# إعدادات المصادر
# ═══════════════════════════════════════════════════════════════
LOG_SOURCES = {
'nginx': '/var/log/nginx/access.log',
'auth': '/var/log/auth.log',
'app': '/var/log/app/application.log'
}
# للتجربة - ملفات محلية
SAMPLE_LOG_SOURCES = {
'nginx': 'sample_logs/nginx_access.log',
'auth': 'sample_logs/auth.log',
'app': 'sample_logs/app.log'
}
# ═══════════════════════════════════════════════════════════════
# أنماط الكشف عن التهديدات
# ═══════════════════════════════════════════════════════════════
THREAT_PATTERNS = {
'brute_force': {
'description': 'محاولات تسجيل دخول فاشلة متكررة',
'threshold': 5,
'window_minutes': 5,
'severity': 'critical'
},
'sql_injection': {
'description': 'محاولة SQL Injection',
'patterns': [
r"(\%27)|(\')|(\-\-)|(\%23)|(#)",
r"((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;))",
r"\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))",
r"union.*select",
r"select.*from",
r"insert.*into",
r"drop.*table"
],
'severity': 'critical'
},
'xss_attempt': {
'description': 'محاولة XSS',
'patterns': [
r" HTTP/1.1" 400 80 "-" "Mozilla/5.0"
172.16.0.25 - - [26/Jul/2026:14:15:02 +0000] "GET /../../etc/passwd HTTP/1.1" 403 50 "-" "Mozilla/5.0"
8.8.8.8 - - [26/Jul/2026:14:20:01 +0000] "GET / HTTP/1.1" 200 1024 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
8.8.8.8 - - [26/Jul/2026:14:20:02 +0000] "GET /about HTTP/1.1" 200 2048 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
"""
# Auth sample logs
auth_logs = """Jul 26 14:00:01 server sshd[1234]: Failed password for invalid user admin from 192.168.1.200 port 22 ssh2
Jul 26 14:00:02 server sshd[1234]: Failed password for invalid user admin from 192.168.1.200 port 22 ssh2
Jul 26 14:00:03 server sshd[1234]: Failed password for invalid user root from 192.168.1.200 port 22 ssh2
Jul 26 14:00:04 server sshd[1234]: Failed password for invalid user test from 192.168.1.200 port 22 ssh2
Jul 26 14:00:05 server sshd[1234]: Failed password for invalid user user from 192.168.1.200 port 22 ssh2
Jul 26 14:05:01 server sshd[1235]: Accepted password for developer from 10.0.0.10 port 22 ssh2
"""
# App sample logs
app_logs = """[2026-07-26 14:00:01] [ERROR] [192.168.1.100] Login failed for user: admin
[2026-07-26 14:00:02] [ERROR] [192.168.1.100] Login failed for user: admin
[2026-07-26 14:00:03] [WARN] [10.0.0.50] Suspicious query detected: SELECT * FROM users
[2026-07-26 14:00:04] [INFO] [8.8.8.8] User logged in successfully
[2026-07-26 14:00:05] [ERROR] [172.16.0.25] Invalid input:
"""
with open('sample_logs/nginx_access.log', 'w') as f:
f.write(nginx_logs)
with open('sample_logs/auth.log', 'w') as f:
f.write(auth_logs)
with open('sample_logs/app.log', 'w') as f:
f.write(app_logs)
print("✅ تم إنشاء ملفات السجلات التجريبية في sample_logs/")
# ═══════════════════════════════════════════════════════════════
# التشغيل الرئيسي
# ═══════════════════════════════════════════════════════════════
if __name__ == '__main__':
import sys
# إنشاء ملفات تجريبية إذا لم تكن موجودة
if not os.path.exists('sample_logs'):
create_sample_logs()
# تحليل السجلات
analyzer = LogAnalyzer(use_sample=True)
analyzer.analyze_all()
analyzer.print_summary()
# تصدير التقرير
analyzer.export_json('siem_report.json')
print("\n" + "=" * 60)
print("✅ اكتمل التحليل - راجع siem_report.json للتفاصيل")
print("=" * 60)