Skip to content

Instantly share code, notes, and snippets.

@TheFlash2k
Created December 18, 2023 14:05
Show Gist options
  • Save TheFlash2k/f67e46892818664cd72ddb8036f7361b to your computer and use it in GitHub Desktop.
Save TheFlash2k/f67e46892818664cd72ddb8036f7361b to your computer and use it in GitHub Desktop.
Brute force a regular expression and match it against a flag format
#!/usr/bin/env python3
import argparse
from rstr import xeger as generate_pattern
def brute_regex(
regex: str,
count: int = 1000,
match: str = "",
show: bool = False
):
''' Test-Case '''
try:
_ = generate_pattern(regex)
except Exception as E:
print(f"Error: {E}")
print("[!] Please verify if your regex is a valid regex.")
exit(1)
uniqs = []
print("[*] Matches::")
for i in range(count):
pp = False
gen = generate_pattern(regex)
if match.lower() not in gen.lower():
continue
if show:
print(gen)
continue
if gen not in uniqs:
uniqs.append(gen)
print(gen)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Brute Force Regular Expression based on a certain flag.')
parser.add_argument('-r', '--regex', type=str, help='Regex to brute force', required=True)
parser.add_argument('-m', '--match', type=str, help="Match a specific string in flag", default="")
parser.add_argument('-c', '--count', type=int, help="Maximum number of patterns to be generated (if the regex allows)", default=1000)
parser.add_argument('--show-all', action='store_true', dest='show', help="Shows all the found matches (Default: Show only unique)")
args = parser.parse_args()
brute_regex(regex=args.regex, count=args.count, match=args.match, show=args.show)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment