docs/test: add tests and documentation for competition-aware patterns and commands
This commit is contained in:
130
src/ctf/utils.py
130
src/ctf/utils.py
@@ -37,7 +37,134 @@ def active_competitions(dir: str) -> dict:
|
||||
comps[item] = active_categories(item)
|
||||
print(item.name)
|
||||
print(comps[item])
|
||||
return comps
|
||||
# {{{ suggest_patterns
|
||||
def suggest_patterns(s: str) -> list[str]:
|
||||
"""Suggests a list of regex patterns from a sample flag string."""
|
||||
if not s or not s.strip():
|
||||
raise ValueError("Input string cannot be empty or whitespace only.")
|
||||
|
||||
# Group characters into types
|
||||
groups = []
|
||||
current_type = None
|
||||
current_chars = []
|
||||
|
||||
def get_type(c):
|
||||
if c.isupper():
|
||||
return 'U'
|
||||
elif c.islower():
|
||||
return 'L'
|
||||
elif c.isdigit():
|
||||
return 'D'
|
||||
else:
|
||||
return 'S'
|
||||
|
||||
for c in s:
|
||||
ctype = get_type(c)
|
||||
if ctype != current_type:
|
||||
if current_type is not None:
|
||||
groups.append((current_type, "".join(current_chars)))
|
||||
current_type = ctype
|
||||
current_chars = [c]
|
||||
else:
|
||||
current_chars.append(c)
|
||||
if current_type is not None:
|
||||
groups.append((current_type, "".join(current_chars)))
|
||||
|
||||
def escape_special(chars):
|
||||
res = []
|
||||
for c in chars:
|
||||
if c in r".+*?^$()[]{}|\/":
|
||||
res.append("\\" + c)
|
||||
else:
|
||||
res.append(c)
|
||||
return "".join(res)
|
||||
|
||||
# 1. Exact counts for group types
|
||||
opt1_parts = []
|
||||
for gtype, gchars in groups:
|
||||
if gtype == 'U':
|
||||
opt1_parts.append(f"[A-Z]{{{len(gchars)}}}")
|
||||
elif gtype == 'L':
|
||||
opt1_parts.append(f"[a-z]{{{len(gchars)}}}")
|
||||
elif gtype == 'D':
|
||||
opt1_parts.append(f"\\d{{{len(gchars)}}}")
|
||||
else:
|
||||
opt1_parts.append(escape_special(gchars))
|
||||
opt1 = "^" + "".join(opt1_parts) + "$"
|
||||
|
||||
# 2. Variable counts for group types
|
||||
opt2_parts = []
|
||||
for gtype, gchars in groups:
|
||||
if gtype == 'U':
|
||||
opt2_parts.append("[A-Z]+")
|
||||
elif gtype == 'L':
|
||||
opt2_parts.append("[a-z]+")
|
||||
elif gtype == 'D':
|
||||
opt2_parts.append("\\d+")
|
||||
else:
|
||||
opt2_parts.append(escape_special(gchars))
|
||||
opt2 = "^" + "".join(opt2_parts) + "$"
|
||||
|
||||
# 3. Case-insensitive / merged letters with exact counts
|
||||
opt3_parts = []
|
||||
for gtype, gchars in groups:
|
||||
if gtype in ('U', 'L'):
|
||||
opt3_parts.append(f"[A-Za-z]{{{len(gchars)}}}")
|
||||
elif gtype == 'D':
|
||||
opt3_parts.append(f"\\d{{{len(gchars)}}}")
|
||||
else:
|
||||
opt3_parts.append(escape_special(gchars))
|
||||
opt3 = "^" + "".join(opt3_parts) + "$"
|
||||
|
||||
# 4. Case-insensitive / merged letters with variable counts
|
||||
opt4_parts = []
|
||||
for gtype, gchars in groups:
|
||||
if gtype in ('U', 'L'):
|
||||
opt4_parts.append("[A-Za-z]+")
|
||||
elif gtype == 'D':
|
||||
opt4_parts.append("\\d+")
|
||||
else:
|
||||
opt4_parts.append(escape_special(gchars))
|
||||
opt4 = "^" + "".join(opt4_parts) + "$"
|
||||
|
||||
# 5. General alphanumeric character class plus unique special characters
|
||||
unique_specials = set()
|
||||
has_alpha = False
|
||||
has_digit = False
|
||||
for c in s:
|
||||
if c.isalnum():
|
||||
if c.isalpha():
|
||||
has_alpha = True
|
||||
if c.isdigit():
|
||||
has_digit = True
|
||||
else:
|
||||
unique_specials.add(c)
|
||||
|
||||
char_class_parts = []
|
||||
if has_alpha:
|
||||
char_class_parts.append("A-Za-z")
|
||||
if has_digit:
|
||||
char_class_parts.append("0-9")
|
||||
for spec in sorted(unique_specials):
|
||||
if spec in r"-[]\^$":
|
||||
char_class_parts.append("\\" + spec)
|
||||
else:
|
||||
char_class_parts.append(spec)
|
||||
opt5 = f"^[{''.join(char_class_parts)}]+$"
|
||||
|
||||
# 6. Literal exact escaped match
|
||||
opt6 = "^" + escape_special(s) + "$"
|
||||
|
||||
# Deduplicate while preserving order
|
||||
candidates = [opt1, opt2, opt3, opt4, opt5, opt6]
|
||||
seen = set()
|
||||
result = []
|
||||
for c in candidates:
|
||||
if c not in seen:
|
||||
seen.add(c)
|
||||
result.append(c)
|
||||
return result
|
||||
# }}}
|
||||
|
||||
# Load variables to export
|
||||
config = load_config("/home/venus/code/ctf/config.toml")
|
||||
@@ -45,3 +172,4 @@ competition = config["Competition"]
|
||||
enviroment = config["Enviroment"]
|
||||
# base_dir = config["ctf_dir"]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user