BLACKLIST_IPS = {"85.234.12.1"}
def parse_log_line(line: str) -> dict | None:
parts = line.split()
if len(parts) < 7:
return None
try:
ip = parts[0]
user = parts[2] if parts[2] != "-" else None
method = parts[4].strip('"')
path = parts[5]
status_code = int(parts[-1])
return {
"ip": ip,
"user": user,
"method": method,
"path": path,
"status_code": status_code
}
except (ValueError, IndexError):
return None
def is_suspicious(log_dict: dict, blacklist: set) -> bool:
if not log_dict:
return False
if log_dict["ip"] in blacklist:
return True
if log_dict["status_code"] in (401, 403):
return True
if log_dict["path"].startswith("/admin"):
return True
return False
def count_failed_logins(logs_list: list) -> dict:
failed_logins = {}
for log in logs_list:
if log and log["status_code"] == 401:
ip = log["ip"]
failed_logins[ip] = failed_logins.get(ip, 0) + 1
return failed_logins
def get_most_frequent_path(logs_list: list) -> str:
path_counter = {}
for log in logs_list:
if log:
path = log["path"]
path_counter[path] = path_counter.get(path, 0) + 1
if not path_counter:
return ""
most_frequent = max(path_counter, key=path_counter.get)
return most_frequent
if __name__ == "__main__":
# Тест функции parse_log_line
test_line = '192.168.1.10 - - [10/Oct/2023:13:55:36] "GET /index.html HTTP/1.1" 200'
print(parse_log_line(test_line))
all_logs = []
try:
with open("network_log.txt", "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
parsed = parse_log_line(line)
if parsed:
all_logs.append(parsed)
except FileNotFoundError:
print("Ошибка: Файл network_log.txt не найден!")
exit()
try:
with open("suspicious_report.txt", "w", encoding="utf-8") as report:
for log in all_logs:
if is_suspicious(log, BLACKLIST_IPS):
report.write(
f"[ВНИМАНИЕ] IP: {log['ip']} | "
f"Путь: {log['path']} | "
f"Код: {log['status_code']}\n"
)
except IOError:
print("Ошибка при записи в файл suspicious_report.txt")
failed_stats = count_failed_logins(all_logs)
for ip, count in failed_stats.items():
print(f"IP {ip}: {count} неудачных попыток входа")
most_frequent = get_most_frequent_path(all_logs)
if most_frequent:
print(f"Самый частый путь: {most_frequent}")