From bd96b1c98770c49483afbf7833d1b7bd44f5a89e Mon Sep 17 00:00:00 2001 From: Radek Vyhnal Date: Fri, 4 Oct 2024 14:56:39 +0200 Subject: [PATCH] use raw strings for regex (fix SyntaxWarning: invalid escape sequence '\d') --- convey/config.py | 2 +- convey/contacts.py | 2 +- convey/convert.py | 12 ++++++------ convey/identifier.py | 2 +- convey/infodicts.py | 2 +- convey/types.py | 4 ++-- convey/whois.py | 2 +- convey/wrapper.py | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/convey/config.py b/convey/config.py index 4dda74c..6690085 100644 --- a/convey/config.py +++ b/convey/config.py @@ -176,7 +176,7 @@ def integrity_check(): section_lines = {} # [section name] = section start line number for i, line in enumerate(config_lines): try: - section = re.match("\[([^]]*)\]", line).group(1) + section = re.match(r"\[([^]]*)\]", line).group(1) except AttributeError: pass else: diff --git a/convey/contacts.py b/convey/contacts.py index 0ed6b46..716ab8b 100644 --- a/convey/contacts.py +++ b/convey/contacts.py @@ -28,7 +28,7 @@ def get_domains(mail: str): """ mail = mail@example.com;mail2@example2.com -> [example.com, example2.com] """ try: # return set(re.findall("@([\w.]+)", mail)) - return set([x[0] for x in re.findall("@(([A-Za-z0-9-]{1,63}\.)+[A-Za-z]{2,6})", mail)]) + return set([x[0] for x in re.findall(r"@(([A-Za-z0-9-]{1,63}\.)+[A-Za-z]{2,6})", mail)]) except AttributeError: return [] diff --git a/convey/convert.py b/convey/convert.py index 3dfccf8..5e9ab49 100644 --- a/convey/convert.py +++ b/convey/convert.py @@ -8,11 +8,11 @@ logger = logging.getLogger(__name__) -reIpWithPort = re.compile("((\d{1,3}\.){4})(\d+)") -reAnyIp = re.compile("\"?((\d{1,3}\.){3}(\d{1,3}))") +reIpWithPort = re.compile(r"((\d{1,3}\.){4})(\d+)") +reAnyIp = re.compile(r"\"?((\d{1,3}\.){3}(\d{1,3}))") reFqdn = re.compile( - "(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-_]{1,63}(?" (works better than conversion to a mere "\0" that may result to ambiguity - return search.sub(re.sub("{(\d+)}", r"\\g<\1>", replace), s) + return search.sub(re.sub(r"{(\d+)}", r"\\g<\1>", replace), s) except re.error: logger.error(f"RegExp failed: `{replace}` cannot be used to substitute `{s}` with `{search}`") if not Config.error_caught(): diff --git a/convey/infodicts.py b/convey/infodicts.py index 0b41b5d..9dcfadf 100644 --- a/convey/infodicts.py +++ b/convey/infodicts.py @@ -3,7 +3,7 @@ idds = ('00', '+') phone_reg = re.compile(r"(\+|00)?\d[\d\-()]{7,12}\d$") -phone_dial = re.compile("[+(]?(\d{0,6})") # any prefix like `+420` or `(089)`, then up to 6 digits (there is no longer prefix) +phone_dial = re.compile(r"[+(]?(\d{0,6})") # any prefix like `+420` or `(089)`, then up to 6 digits (there is no longer prefix) def phone_regex_match(val): diff --git a/convey/types.py b/convey/types.py index dd4246b..df4e406 100644 --- a/convey/types.py +++ b/convey/types.py @@ -349,7 +349,7 @@ def get_method(start: Type, target: Type): ["sourceipaddress", "source", "src"], Checker.is_ip, usual_must_match=True) destination_ip = Type("destination_ip", TypeGroup.general, "valid destination IP address", ["destinationipaddress", "destination", "dest", "dst"], Checker.is_ip, usual_must_match=True) - port = Type("port", TypeGroup.general, "port", ["port", "prt"], lambda x: re.match("\d{1,5}", x), + port = Type("port", TypeGroup.general, "port", ["port", "prt"], lambda x: re.match(r"\d{1,5}", x), usual_must_match=True) cidr = Type("cidr", TypeGroup.general, "CIDR 127.0.0.1/32", ["cidr"], Checker.check_cidr) port_ip = Type("port_ip", TypeGroup.general, "IP in the form 1.2.3.4.port", [], reIpWithPort.match) @@ -362,7 +362,7 @@ def get_method(start: Type, target: Type): url = Type("url", TypeGroup.general, "URL starting with http/https", ["url", "uri", "location"], lambda s: reUrl.match(s) and "[.]" not in s) # input "example[.]com" would be admitted as a valid URL) asn = Type("asn", TypeGroup.whois, "Autonomous system number", ["as", "asn", "asnumber"], - lambda x: re.search('AS\d+', x) is not None) + lambda x: re.search(r'AS\d+', x) is not None) base64 = Type("base64", TypeGroup.general, "Text encoded with Base64", ["base64"], Checker.is_base64) quoted_printable = Type("quoted_printable", TypeGroup.general, "Text encoded as quotedprintable", [], Checker.is_quopri) diff --git a/convey/whois.py b/convey/whois.py index 0dcdd31..bc673ef 100644 --- a/convey/whois.py +++ b/convey/whois.py @@ -373,7 +373,7 @@ def _load_country_from_addresses(self): return c return "" - reAbuse = re.compile('[a-z0-9._%+-]{1,64}@(?:[a-z0-9-]{1,63}\.){1,125}[a-z]{2,63}') + reAbuse = re.compile(r'[a-z0-9._%+-]{1,64}@(?:[a-z0-9-]{1,63}\.){1,125}[a-z]{2,63}') def get_abusemail(self): """ Loads abusemail from last whois response OR from whois json api. """ diff --git a/convey/wrapper.py b/convey/wrapper.py index 9e4c7d0..2b2042c 100644 --- a/convey/wrapper.py +++ b/convey/wrapper.py @@ -307,7 +307,7 @@ def check_log(self): """ Check if the contents is a CSV and not just a log ex: "06:25:13.378767 IP 142.234.39.36.51354 > 195.250.148.86.80: Flags [S], seq 1852455482, win 29200, length 0" """ - re_ip_with_port = re.compile("((\d{1,3}\.){4})(\d+)") + re_ip_with_port = re.compile(r"((\d{1,3}\.){4})(\d+)") re_log_line = re.compile(r"([^\s]*)\sIP\s([^\s]*)\s>\s([^\s:]*)") _, sample, _ = Identifier(None).get_sample(self.file) if sample and re_log_line.match(sample[0]):