-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analysis.py
More file actions
71 lines (57 loc) · 2.5 KB
/
Copy pathlog_analysis.py
File metadata and controls
71 lines (57 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import re
from collections import Counter
def analyze_logs(log_file):
"""Analyzes security logs to find key statistics and anomalies."""
# Regex to extract key information: Timestamp, IP, Event, Message
log_pattern = re.compile(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) \[(.*?)\] (.*)")
events = []
ips = []
failed_logins = 0
brute_force_attempts = 0
with open(log_file, 'r') as f:
for line in f:
match = log_pattern.match(line)
if match:
timestamp, ip, event, message = match.groups()
events.append(event)
ips.append(ip)
if event == "LOGIN_FAILED":
failed_logins += 1
if event == "BRUTE_FORCE_ATTEMPT":
brute_force_attempts += 1
# 1. Count of each event type
event_counts = Counter(events)
# 2. Top 5 most active IPs
top_ips = Counter(ips).most_common(5)
# 3. IPs with high failed login attempts (potential brute force)
failed_login_ips = [ip for ip, event in zip(ips, events) if event == "LOGIN_FAILED"]
top_failed_login_ips = Counter(failed_login_ips).most_common(5)
# 4. Generate Analysis Report
report = "# 🛡️ Análisis de Logs de Seguridad - Reporte de Incidentes\n\n"
report += "## Resumen de Eventos\n"
report += "| Evento | Conteo |\n"
report += "| :--- | :--- |\n"
for event, count in event_counts.most_common():
report += f"| {event} | {count} |\n"
report += "\n"
report += "## Detección de Amenazas\n"
report += f"**Intentos de Login Fallidos:** {failed_logins}\n"
report += f"**Intentos de Fuerza Bruta Detectados:** {brute_force_attempts}\n\n"
report += "## Top 5 IPs con Mayor Actividad\n"
report += "| IP | Conteo |\n"
report += "| :--- | :--- |\n"
for ip, count in top_ips:
report += f"| {ip} | {count} |\n"
report += "\n"
report += "## Top 5 IPs con Mayor Cantidad de Logins Fallidos\n"
report += "| IP | Conteo |\n"
report += "| :--- | :--- |\n"
for ip, count in top_failed_login_ips:
report += f"| {ip} | {count} |\n"
report += "\n"
return report
if __name__ == "__main__":
report_content = analyze_logs('security_logs.txt')
with open('security_analysis_report.md', 'w') as f:
f.write(report_content)
print("Security Log Analysis complete. Report saved to security_analysis_report.md.")