From 68b59404256a2a374c2d1dc7777fca4e0f393fd5 Mon Sep 17 00:00:00 2001 From: Simon Gurney Date: Sun, 10 Mar 2024 13:58:04 +0000 Subject: [PATCH 1/2] feat: add custom uri on body checks --- signatures/checks/WEB.py | 74 +++++++++---------- signatures/teamwork.py | 16 ++-- .../cname_found_but_string_in_body.py | 6 +- .../cname_or_ip_found_but_string_in_body.py | 6 +- .../templates/ip_found_but_string_in_body.py | 6 +- 5 files changed, 58 insertions(+), 50 deletions(-) diff --git a/signatures/checks/WEB.py b/signatures/checks/WEB.py index d8169f0..589d3ad 100644 --- a/signatures/checks/WEB.py +++ b/signatures/checks/WEB.py @@ -1,37 +1,37 @@ -from domain import Domain -from math import floor -import logging - - -def string_in_body(domain: Domain, string: str, https: bool) -> bool: - if string in domain.fetch_web(https=https).body: - logging.info(f"Message observed in response for '{domain}'") - return True - logging.debug(f"Message not found in response for '{domain}'") - return False - - -def string_in_body_http(domain: Domain, string: str) -> bool: - return string_in_body(domain, string, False) - - -def string_in_body_https(domain: Domain, string: str) -> bool: - return string_in_body(domain, string, True) - - -def status_code_match(domain: Domain, status_code: int, https: bool) -> bool: - response_code = domain.fetch_web(https=https).status_code - if status_code < 10: # match the first int - if floor(response_code / 100) == status_code: - logging.info(f"Response code {response_code} observed for '{domain}'") - return True - else: - if response_code == status_code: - logging.info(f"Response code {response_code} observed for '{domain}'") - return True - logging.debug(f"Response code {response_code} observed for '{domain}'") - return False - - -def status_code_404(domain: Domain, https: bool) -> bool: - return status_code_match(domain, 404, https) +from domain import Domain +from math import floor +import logging + + +def string_in_body(domain: Domain, string: str, https: bool, custom_uri: str = "") -> bool: + if string in domain.fetch_web(https=https, uri=custom_uri).body: + logging.info(f"Message observed in response for '{domain}'") + return True + logging.debug(f"Message not found in response for '{domain}'") + return False + + +def string_in_body_http(domain: Domain, string: str, custom_uri: str = "") -> bool: + return string_in_body(domain, string, False, custom_uri) + + +def string_in_body_https(domain: Domain, string: str, custom_uri: str = "") -> bool: + return string_in_body(domain, string, True, custom_uri) + + +def status_code_match(domain: Domain, status_code: int, https: bool) -> bool: + response_code = domain.fetch_web(https=https).status_code + if status_code < 10: # match the first int + if floor(response_code / 100) == status_code: + logging.info(f"Response code {response_code} observed for '{domain}'") + return True + else: + if response_code == status_code: + logging.info(f"Response code {response_code} observed for '{domain}'") + return True + logging.debug(f"Response code {response_code} observed for '{domain}'") + return False + + +def status_code_404(domain: Domain, https: bool) -> bool: + return status_code_match(domain, 404, https) diff --git a/signatures/teamwork.py b/signatures/teamwork.py index ddb0ea7..1912afa 100644 --- a/signatures/teamwork.py +++ b/signatures/teamwork.py @@ -1,7 +1,9 @@ -from .templates.cname_found_but_string_in_body import cname_found_but_string_in_body - -test = cname_found_but_string_in_body( - cname=".teamwork.com", - domain_not_configured_message="""Teamwork Projects""", - service="teamwork", -) +from .templates.cname_found_but_string_in_body import cname_found_but_string_in_body + +test = cname_found_but_string_in_body( + cname=".teamwork.com", + domain_not_configured_message="""Unable to determine installationID from domain""", + service="teamwork", + custom_uri="launchpad/v1/info.json", + https=True +) diff --git a/signatures/templates/cname_found_but_string_in_body.py b/signatures/templates/cname_found_but_string_in_body.py index 1af1994..1380234 100644 --- a/signatures/templates/cname_found_but_string_in_body.py +++ b/signatures/templates/cname_found_but_string_in_body.py @@ -17,10 +17,10 @@ def potential(self, domain, **kwargs) -> bool: def check(self, domain, **kwargs) -> bool: if self.https: return signatures.checks.WEB.string_in_body_https( - domain, self.domain_not_configured_message + domain, self.domain_not_configured_message, custom_uri=self.custom_uri ) return signatures.checks.WEB.string_in_body_http( - domain, self.domain_not_configured_message + domain, self.domain_not_configured_message, custom_uri=self.custom_uri ) def __init__( @@ -30,10 +30,12 @@ def __init__( service, info=None, https=False, + custom_uri="", **kwargs, ): self.cname = cname self.domain_not_configured_message = domain_not_configured_message self.https = https + self.custom_uri = custom_uri info = info if info else INFO super().__init__(info.format(service=service), **kwargs) diff --git a/signatures/templates/cname_or_ip_found_but_string_in_body.py b/signatures/templates/cname_or_ip_found_but_string_in_body.py index b5d7eba..ca9d29e 100644 --- a/signatures/templates/cname_or_ip_found_but_string_in_body.py +++ b/signatures/templates/cname_or_ip_found_but_string_in_body.py @@ -19,10 +19,10 @@ def potential(self, domain, **kwargs) -> bool: def check(self, domain, **kwargs) -> bool: if self.https: return signatures.checks.WEB.string_in_body_https( - domain, self.domain_not_configured_message + domain, self.domain_not_configured_message, custom_uri=self.custom_uri ) return signatures.checks.WEB.string_in_body_http( - domain, self.domain_not_configured_message + domain, self.domain_not_configured_message, custom_uri=self.custom_uri ) def __init__( @@ -33,11 +33,13 @@ def __init__( service, info=None, https=False, + custom_uri="", **kwargs ): self.cname = cname self.ips = ips self.domain_not_configured_message = domain_not_configured_message self.https = https + self.custom_uri = custom_uri info = info if info else INFO super().__init__(info.format(service=service), **kwargs) diff --git a/signatures/templates/ip_found_but_string_in_body.py b/signatures/templates/ip_found_but_string_in_body.py index dc9f857..d3f9564 100644 --- a/signatures/templates/ip_found_but_string_in_body.py +++ b/signatures/templates/ip_found_but_string_in_body.py @@ -19,10 +19,10 @@ def potential(self, domain, **kwargs) -> bool: def check(self, domain, **kwargs) -> bool: if self.https: return signatures.checks.WEB.string_in_body_https( - domain, self.domain_not_configured_message + domain, self.domain_not_configured_message, custom_uri=self.custom_uri ) return signatures.checks.WEB.string_in_body_http( - domain, self.domain_not_configured_message + domain, self.domain_not_configured_message, custom_uri=self.custom_uri ) def __init__( @@ -32,10 +32,12 @@ def __init__( service, info=None, https=False, + custom_uri="", **kwargs ): self.ips = ips self.domain_not_configured_message = domain_not_configured_message self.https = https + self.custom_uri = custom_uri info = info if info else INFO super().__init__(info.format(service=service), **kwargs) From e206c6e1fffc99c8cdc6658f836b470cb7e69e15 Mon Sep 17 00:00:00 2001 From: Simon Gurney Date: Sun, 10 Mar 2024 14:00:45 +0000 Subject: [PATCH 2/2] chore: black --- signatures/checks/WEB.py | 4 +++- signatures/teamwork.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/signatures/checks/WEB.py b/signatures/checks/WEB.py index 589d3ad..afd4a27 100644 --- a/signatures/checks/WEB.py +++ b/signatures/checks/WEB.py @@ -3,7 +3,9 @@ import logging -def string_in_body(domain: Domain, string: str, https: bool, custom_uri: str = "") -> bool: +def string_in_body( + domain: Domain, string: str, https: bool, custom_uri: str = "" +) -> bool: if string in domain.fetch_web(https=https, uri=custom_uri).body: logging.info(f"Message observed in response for '{domain}'") return True diff --git a/signatures/teamwork.py b/signatures/teamwork.py index 1912afa..fbc7fc8 100644 --- a/signatures/teamwork.py +++ b/signatures/teamwork.py @@ -5,5 +5,5 @@ domain_not_configured_message="""Unable to determine installationID from domain""", service="teamwork", custom_uri="launchpad/v1/info.json", - https=True + https=True, )