Skip to content

Commit

Permalink
use raw strings for regex (fix SyntaxWarning: invalid escape sequence…
Browse files Browse the repository at this point in the history
… '\d')
  • Loading branch information
rvyhnal authored and e3rd committed Oct 4, 2024
1 parent 118feea commit bd96b1c
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion convey/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion convey/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_domains(mail: str):
""" mail = [email protected];[email protected] -> [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 []

Expand Down
12 changes: 6 additions & 6 deletions convey/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}(?<!-)\.)+[a-zA-Z]{2,63}$)") # Xtoo long, infinite loop: ^(((([A-Za-z0-9]+){1,63}\.)|(([A-Za-z0-9]+(\-)+[A-Za-z0-9]+){1,63}\.))+){1,255}$
reUrl = re.compile('[htps]*://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
r"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-_]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)") # Xtoo long, infinite loop: ^(((([A-Za-z0-9]+){1,63}\.)|(([A-Za-z0-9]+(\-)+[A-Za-z0-9]+){1,63}\.))+){1,255}$
reUrl = re.compile(r'[htps]*://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
# reBase64 = re.compile('^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$')


Expand Down Expand Up @@ -43,7 +43,7 @@ def port_ip_port(s):

def url_port(s):
s = s.split(":")[1]
return re.match("^(\d*)", s).group(1)
return re.match(r"^(\d*)", s).group(1)


def url_hostname(url):
Expand Down Expand Up @@ -103,7 +103,7 @@ def nmap(val, port=""):
if Config.get("multiple_nmap_ports", "FIELDS"):
l = []
for row in text.split("\n"):
l.append(int(re.match("(\d+)", row).group(1)))
l.append(int(re.match(r"(\d+)", row).group(1)))
return l

return text
2 changes: 1 addition & 1 deletion convey/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def reg_s_method(s):
return search.sub("", s)
try:
# we convert "str{0}" → "\g<0>" (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():
Expand Down
2 changes: 1 addition & 1 deletion convey/infodicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions convey/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion convey/whois.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand Down
2 changes: 1 addition & 1 deletion convey/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down

0 comments on commit bd96b1c

Please sign in to comment.