-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analyzer.py
More file actions
50 lines (33 loc) · 1.35 KB
/
Copy pathlog_analyzer.py
File metadata and controls
50 lines (33 loc) · 1.35 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
import re
from collections import Counter
def analyze_auth_log(filepath, threshold=10):
"""Parse auth.log for failed SSH attempts and flag suspicious IPs."""
failed_pattern = re.compile(
r"Failed password for (?:invalid user )?(\S+) from (\d+\.\d+\.\d+\.\d+)"
)
attempts = []
usernames_targeted = {}
with open(filepath, "r") as log_file:
for line in log_file:
match = failed_pattern.search(line)
if match:
username = match.group(1)
ip_address = match.group(2)
attempts.append(ip_address)
if ip_address not in usernames_targeted:
usernames_targeted[ip_address] = set()
usernames_targeted[ip_address].add(username)
ip_counts = Counter(attempts)
print(f"Total failed attempts: {len(attempts)})")
print(f"Unique source IPs: {len(ip_counts)}")
print(f"\nIPs exceeding {threshold} failed attempts:)")
print("-" * 55)
flagged = []
for ip, count in ip_counts.most_common():
if count >= threshold:
users = usernames_targeted.get(ip, set())
print(f" {ip:18s} {count:4d} attempts {len(users)} usernames")
flagged.append(ip)
return flagged
if __name__ == "__main__":
flagged_ips = analyze_auth_log("test_auth.log", threshold=2)