| 1 | + | #!/usr/bin/env python3 |
| 2 | + | |
| 3 | + | import argparse |
| 4 | + | import sys |
| 5 | + | import string |
| 6 | + | import unidecode |
| 7 | + | import warnings |
| 8 | + | from prettytable import PrettyTable |
| 9 | + | import urllib.parse |
| 10 | + | import itertools |
| 11 | + | |
| 12 | + | warnings.simplefilter("ignore") |
| 13 | + | |
| 14 | + | |
| 15 | + | class Recollapse: |
| 16 | + | ENCODING_URL = 1 |
| 17 | + | ENCODING_UNICODE = 2 |
| 18 | + | ENCODING_RAW = 3 |
| 19 | + | |
| 20 | + | MODE_START = 1 |
| 21 | + | MODE_SEP = 2 |
| 22 | + | MODE_NORM = 3 |
| 23 | + | MODE_TERM = 4 |
| 24 | + | |
| 25 | + | output = [] |
| 26 | + | normalization_d = {} |
| 27 | + | |
| 28 | + | def __init__(self, size, encoding, range, positions, input, file, normtable, alphanum, maxnorm): |
| 29 | + | self.build_normalization_dict() |
| 30 | + | self.size = size |
| 31 | + | self.encoding = encoding |
| 32 | + | self.range = range |
| 33 | + | self.positions = positions |
| 34 | + | self.input = input |
| 35 | + | self.file = file |
| 36 | + | self.normtable = normtable |
| 37 | + | self.alphanum = alphanum |
| 38 | + | self.maxnorm = maxnorm |
| 39 | + | |
| 40 | + | def run(self): |
| 41 | + | if self.normtable: |
| 42 | + | self.print_normalization_table() |
| 43 | + | return |
| 44 | + | |
| 45 | + | if not self.input: |
| 46 | + | if self.file: |
| 47 | + | with open(self.file) as f: |
| 48 | + | self.input = f.read() |
| 49 | + | else: |
| 50 | + | self.input = sys.stdin.read().rstrip() |
| 51 | + | |
| 52 | + | fuzzing_range = range(self.range[0], self.range[1]+1) |
| 53 | + | if not self.alphanum: |
| 54 | + | alphanum_ascii = list(map(ord, string.ascii_letters + string.digits)) |
| 55 | + | fuzzing_range = [b for b in list(fuzzing_range) if b not in alphanum_ascii] |
| 56 | + | |
| 57 | + | if self.MODE_START in self.positions: |
| 58 | + | for t in itertools.product(fuzzing_range, repeat=self.size): |
| 59 | + | self.generate(t, 0) |
| 60 | + | |
| 61 | + | if self.MODE_SEP in self.positions: |
| 62 | + | for i in range(len(self.input)): |
| 63 | + | c = self.input[i] |
| 64 | + | if c in string.punctuation: |
| 65 | + | for t in itertools.product(fuzzing_range, repeat=self.size): |
| 66 | + | self.generate(t, i) |
| 67 | + | self.generate(t, i+1) |
| 68 | + | |
| 69 | + | if self.MODE_NORM in self.positions: |
| 70 | + | for i in range(len(self.input)): |
| 71 | + | c = self.input[i] |
| 72 | + | if c in self.normalization_d: |
| 73 | + | for cc in self.normalization_d.get(c)[0:self.maxnorm]: |
| 74 | + | self.generate((ord(cc),), i, replace=True) |
| 75 | + | |
| 76 | + | if self.MODE_TERM in self.positions: |
| 77 | + | for t in itertools.product(fuzzing_range, repeat=self.size): |
| 78 | + | self.generate(t, len(self.input)) |
| 79 | + | |
| 80 | + | print("\n".join(list(sorted(set(self.output))))) |
| 81 | + | |
| 82 | + | def build_normalization_dict(self): |
| 83 | + | charset = [chr(c) for c in range(0x20,0x7f)] |
| 84 | + | for i in range(0x80): |
| 85 | + | c = chr(i) |
| 86 | + | if c in charset: |
| 87 | + | self.normalization_d[c] = [] |
| 88 | + | |
| 89 | + | for c in range(0xffff): |
| 90 | + | norm_c = unidecode.unidecode(chr(c)) |
| 91 | + | if len(norm_c) == 1 and norm_c in charset and norm_c != chr(c): |
| 92 | + | self.normalization_d[norm_c].append(chr(c)) |
| 93 | + | |
| 94 | + | def print_normalization_table(self): |
| 95 | + | table = [] |
| 96 | + | max_col = len(self.normalization_d[max(self.normalization_d, key=lambda k: len(self.normalization_d[k]))]) |
| 97 | + | |
| 98 | + | for c in self.normalization_d: |
| 99 | + | l = self.normalization_d.get(c) |
| 100 | + | l = l + [""]*(max_col-len(l)) |
| 101 | + | table.append([hex(ord(c)), c] + l) |
| 102 | + | |
| 103 | + | tab = PrettyTable() |
| 104 | + | tab.header = False |
| 105 | + | tab.border = False |
| 106 | + | tab.add_rows(table) |
| 107 | + | print(tab) |
| 108 | + | |
| 109 | + | def generate(self, bytes, index, replace=False): |
| 110 | + | s = self.input |
| 111 | + | a = s[:index] |
| 112 | + | b = s[index:] |
| 113 | + | |
| 114 | + | if replace: |
| 115 | + | a = s[:index] |
| 116 | + | b = s[index+1:] |
| 117 | + | |
| 118 | + | x = "" |
| 119 | + | if self.encoding == self.ENCODING_URL: |
| 120 | + | for byte in bytes: |
| 121 | + | if byte > 0xff: |
| 122 | + | x += urllib.parse.quote(chr(byte)) |
| 123 | + | else: |
| 124 | + | x += "%{y}".format(y=hex(byte)[2:].zfill(2)) |
| 125 | + | self.output.append("{a}{x}{b}".format(x=x, a=a, b=b)) |
| 126 | + | elif self.encoding == self.ENCODING_RAW: |
| 127 | + | for byte in bytes: |
| 128 | + | if 10 <= byte < 13 or byte == 27: |
| 129 | + | continue |
| 130 | + | x += chr(byte) |
| 131 | + | try: |
| 132 | + | self.output.append("{a}{x}{b}".format(x=x, a=a, b=b)) |
| 133 | + | except UnicodeEncodeError: |
| 134 | + | pass |
| 135 | + | elif self.encoding == self.ENCODING_UNICODE: |
| 136 | + | for byte in bytes: |
| 137 | + | x += "\\u{x}".format(x=hex(byte)[2:].zfill(4)) |
| 138 | + | self.output.append("{a}{x}{b}".format(x=x, a=a, b=b)) |
| 139 | + | |
| 140 | + | |
| 141 | + | def parse_args(): |
| 142 | + | parser = argparse.ArgumentParser(description="REcollapse is a helper tool for black-box regex fuzzing to bypass validations and discover normalizations in web applications") |
| 143 | + | |
| 144 | + | parser.add_argument("-p", "--positions", help="pivot position modes. Example: 1,2,3,4 (default). 1: starting, 2: separator, 3: normalization, 4: termination", required=False, default="1,2,3,4", type=str) |
| 145 | + | parser.add_argument("-e", "--encoding", help="1: URL-encoded format (default), 2: Unicode format, 3: Raw format", required=False, default=1, type=int, choices=range(1, 4)) |
| 146 | + | parser.add_argument("-r", "--range", help="range of bytes for fuzzing. Example: 0,0xff (default)", required=False, default="0,0xff", type=str) |
| 147 | + | parser.add_argument("-s", "--size", help="number of fuzzing bytes (default: 1)", required=False, default=1) |
| 148 | + | parser.add_argument("-f", "--file", help="read input from file", required=False) |
| 149 | + | parser.add_argument("-an", "--alphanum", help="include alphanumeric bytes in fuzzing range", required=False, default=False, action="store_true") |
| 150 | + | parser.add_argument("-mn", "--maxnorm", help="maximum number of normalizations (default: 3)", default=3, type=int) |
| 151 | + | parser.add_argument("-nt", "--normtable", help="print normalization table", required=False, default=False, action="store_true") |
| 152 | + | parser.add_argument("input", help="original input", nargs="?") |
| 153 | + | |
| 154 | + | args = parser.parse_args() |
| 155 | + | |
| 156 | + | if len(sys.argv) == 1: |
| 157 | + | parser.print_help() |
| 158 | + | exit(1) |
| 159 | + | |
| 160 | + | if args.range: |
| 161 | + | base = 0 |
| 162 | + | sep = "," |
| 163 | + | if "0x" in args.range: |
| 164 | + | base = 16 |
| 165 | + | if "-" in args.range: |
| 166 | + | sep = "-" |
| 167 | + | args.range = list(map(lambda x: int(x, base), args.range.split(sep))) |
| 168 | + | if len(args.range) == 1: |
| 169 | + | args.range.append(args.range[0]+1) |
| 170 | + | |
| 171 | + | if args.positions: |
| 172 | + | try: |
| 173 | + | args.positions = list(map(lambda x: int(x), args.positions.split(","))) |
| 174 | + | except ValueError: |
| 175 | + | print("Invalid positions provided") |
| 176 | + | exit(1) |
| 177 | + | |
| 178 | + | for p in args.positions: |
| 179 | + | if not 0 < p < 5: |
| 180 | + | print("Invalid positions provided") |
| 181 | + | exit(1) |
| 182 | + | |
| 183 | + | args.size = int(args.size) |
| 184 | + | |
| 185 | + | return args |
| 186 | + | |
| 187 | + | |
| 188 | + | if __name__ == "__main__": |
| 189 | + | args = parse_args() |
| 190 | + | recollapse = Recollapse(**vars(args)) |
| 191 | + | recollapse.run() |
| 192 | + | |