Skip to content

Commit

Permalink
include the app password id in audit
Browse files Browse the repository at this point in the history
  • Loading branch information
bennet0496 committed Oct 1, 2024
1 parent 8ce205b commit 4d82cf0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions lookup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import socket
from functools import lru_cache
from typing import Any
from typing import Any, Optional

from pydantic import BaseModel

Expand All @@ -15,6 +15,7 @@

class LookupResult(BaseModel):
user: str | None = None
password: int | None = None
service: str | None = None
ip: str | None = None
rev_host: str | None = None
Expand All @@ -30,8 +31,8 @@ def __str__(self):
e = ", entity=".join(self.whois_result.entities)
else:
e = "<>"
val = "user=<{}>, service={}, ip={}, host={}, asn={}, as_cc={}, as_desc=<{}>, as_org=<{}>, net_name=<{}>, net_cc={}, entity={}".format(
self.user, self.service, self.ip, self.rev_host, self.whois_result.asn, self.whois_result.as_cc,
val = "user=<{}>, password={}, service={}, ip={}, host={}, asn={}, as_cc={}, as_desc=<{}>, as_org=<{}>, net_name=<{}>, net_cc={}, entity={}".format(
self.user, self.password, self.service, self.ip, self.rev_host, self.whois_result.asn, self.whois_result.as_cc,
self.whois_result.as_desc, self.maxmind_result and self.maxmind_result.as_org, self.whois_result.net_name,
self.whois_result.net_cc, e
)
Expand Down Expand Up @@ -75,6 +76,7 @@ def __str__(self):

def __cmp__(self, other):
return (self.user == other.user and
self.password == other.password and
self.service == other.service and
self.ip == other.ip and
self.rev_host == other.rev_host and
Expand All @@ -86,19 +88,19 @@ def __cmp__(self, other):
self.reserved == other.reserved)

def __hash__(self):
return hash((self.user, self.service, self.ip, self.rev_host, self.whois_result, self.maxmind_result,
return hash((self.user, self.password, self.service, self.ip, self.rev_host, self.whois_result, self.maxmind_result,
self.blocked, self.matched, self.log))


@lru_cache(maxsize=16)
def lookup(ip: str, service: str, user: str) -> LookupResult:
def lookup(ip: str, service: str, user: str, password_id: Optional[int] = None) -> LookupResult:
try:
rdns = socket.gethostbyaddr(ip)[0]
except socket.herror:
logger.debug("lookup for %s returned socket.herror/NXDOMAIN", ip)
rdns = "<>"

result = LookupResult(user=user, service=service, ip=ip, rev_host=rdns)
result = LookupResult(user=user, service=service, ip=ip, rev_host=rdns, password=password_id)
local_net = find_net(ip, get_settings().audit.local_networks.keys())
if local_net is not None:
logger.debug("%s is in local network %s, synthesizing WhoisResult", ip, local_net)
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def post_auth(
response.status_code = status.HTTP_401_UNAUTHORIZED
return {"status": "app passwords not allowed"}

result = lookup(request.remote_ip, request.service, request.username)
result = lookup(request.remote_ip, request.service, request.username, app_password.id)
audit_result = audit(result)

if result.maxmind_result is not None:
Expand Down

0 comments on commit 4d82cf0

Please sign in to comment.