Skip to content

Commit

Permalink
Improve output
Browse files Browse the repository at this point in the history
  • Loading branch information
b-c-ds authored and bcaller committed Dec 30, 2020
1 parent d0c9a39 commit ab186e4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions regexploit/ast/char.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def example(self) -> str:

if self.positive:
if self.literals:
if len(self.literals) > 1:
# Try to avoid \n due to false positives with the . character and flags
return chr(next(o for o in self.literals if o != 0xa))
return chr(next(iter(self.literals)))
elif self.categories:
return sorted(self.categories, key=lambda c: 0 if c.is_positive else 1)[
Expand Down
1 change: 1 addition & 0 deletions regexploit/bin/regexploit-python-env
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def main():
print("Cannot load", name)

names = tuple(sys.argv[1:]) if len(sys.argv) > 1 else None
sys.argv = sys.argv[:1]
if names:
regexploit.hook.regexes.clear()

Expand Down
2 changes: 1 addition & 1 deletion regexploit/bin/regexploit_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def main():
)
args = parser.parse_args()

output = TextOutput()
output = TextOutput(js_flavour=True)
files = (
(fname for fglob in args.files for fname in iglob(fglob, recursive=True))
if args.glob
Expand Down
5 changes: 3 additions & 2 deletions regexploit/output/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@


class TextOutput:
def __init__(self):
def __init__(self, js_flavour: bool=False):
self.first_for_regex = True
self.regexes = 0
self.js_flavour = js_flavour

def next(self):
"""Next regex being processed."""
Expand Down Expand Up @@ -47,4 +48,4 @@ def record(self, redos, pattern, *, filename=None, lineno=None, context=None):
print(f"Repeated character: {redos.repeated_character}")
if redos.killer:
print(f"Final character to cause backtracking: {redos.killer}")
print(f"Example: {redos.example()}\n")
print(f"Example: {redos.example(self.js_flavour)}\n")
8 changes: 6 additions & 2 deletions regexploit/redos.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Redos:
def example_prefix(self) -> str:
return self.prefix_sequence.example()

def example(self) -> str:
def example(self, js_flavour: bool=False) -> str:
repeated_char = self.repeated_character
killer = self.killer
# Try to find a repeating character which is also a killer
Expand All @@ -40,7 +40,11 @@ def example(self) -> str:
.replace("'", "\\'")
)
e = f"'{prefix}' + " if prefix else ""
e += f"'{repeated_char_s}' * 3456"
if js_flavour:
e += f"'{repeated_char_s}'.repeat(3456)"
else:
e += f"'{repeated_char_s}' * 3456"

if killer:
killer_s = (
killer.example().encode("unicode_escape").decode().replace("'", "\\'")
Expand Down

0 comments on commit ab186e4

Please sign in to comment.