42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
from SSHLogEntry import (
|
|
SSHRejectedPassword,
|
|
SSHAcceptedPassword,
|
|
SSHError,
|
|
SSHOther,
|
|
SSHLogEntry
|
|
)
|
|
from typing import List
|
|
|
|
class SSHLogJournal:
|
|
_entries: List[SSHLogEntry]
|
|
|
|
|
|
def __init__(self):
|
|
self._entries = []
|
|
|
|
def __len__(self):
|
|
return len(self._entries)
|
|
|
|
def __iter__(self):
|
|
return iter(self._entries)
|
|
|
|
def __contains__(self, value):
|
|
return value in self._entries
|
|
|
|
def append(self, log: str):
|
|
if "Failed password for invalid user " in log:
|
|
entry = SSHRejectedPassword(log)
|
|
elif "Accepted password for " in log:
|
|
entry = SSHAcceptedPassword(log)
|
|
elif "error: " in log:
|
|
entry = SSHError(log)
|
|
else:
|
|
entry = SSHOther(log)
|
|
|
|
if not entry.validate():
|
|
raise Exception("entry data validation failed!")
|
|
|
|
self._entries.append(entry)
|
|
|
|
def get_logs_by_ip(self, ipv4: str) -> List[SSHLogEntry]:
|
|
return [log for log in self._entries if log.ipv4() == ipv4] |