1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| class SubNameBrute(object): def __init__(self, *params): self.domain, self.options, self.process_num, self.dns_servers, self.next_subs, \ self.scan_count, self.found_count, self.queue_size_array, tmp_dir = params self.dns_count = len(self.dns_servers) self.scan_count_local = 0 self.found_count_local = 0 self.resolvers = [dns.asyncresolver.Resolver(configure=False) for _ in range(self.options.threads)] for r in self.resolvers: r.lifetime = 6.0 r.timeout = 10.0 self.queue = PriorityQueue() self.ip_dict = {} self.found_subs = set() self.cert_subs = set() self.timeout_subs = {} self.no_server_subs = {} self.count_time = time.time() self.outfile = open('%s/%s_part_%s.txt' % (tmp_dir, self.domain, self.process_num), 'w') self.normal_names_set = set() self.lock = asyncio.Lock() self.threads_status = ['1'] * self.options.threads
async def load_sub_names(self): normal_lines = [] wildcard_lines = [] wildcard_set = set() regex_list = [] lines = set() with open(self.options.file) as inFile: for line in inFile.readlines(): sub = line.strip() if not sub or sub in lines: continue lines.add(sub)
brace_count = sub.count('{') if brace_count > 0: wildcard_lines.append((brace_count, sub)) sub = sub.replace('{alphnum}', '[a-z0-9]') sub = sub.replace('{alpha}', '[a-z]') sub = sub.replace('{num}', '[0-9]') if sub not in wildcard_set: wildcard_set.add(sub) regex_list.append('^' + sub + '$') else: normal_lines.append(sub) self.normal_names_set.add(sub)
if regex_list: pattern = '|'.join(regex_list) _regex = re.compile(pattern) for line in normal_lines: if _regex.search(line): normal_lines.remove(line) for _ in normal_lines[self.process_num::self.options.process]: await self.queue.put((0, _)) for _ in wildcard_lines[self.process_num::self.options.process]: await self.queue.put(_)
async def update_counter(self): while True: if '1' not in self.threads_status: return self.scan_count.value += self.scan_count_local self.scan_count_local = 0 self.queue_size_array[self.process_num] = self.queue.qsize() if self.found_count_local: self.found_count.value += self.found_count_local self.found_count_local = 0 self.count_time = time.time() await asyncio.sleep(0.5)
async def check_https_alt_names(self, domain): try: reader, _ = await asyncio.open_connection( host=domain, port=443, ssl=True, server_hostname=domain, ) for item in reader._transport.get_extra_info('peercert')['subjectAltName']: if item[0].upper() == 'DNS': name = item[1].lower() if name.endswith(self.domain): sub = name[:len(name) - len(self.domain) - 1] sub = sub.replace('*', '') sub = sub.strip('.') if sub and sub not in self.found_subs and \ sub not in self.normal_names_set and sub not in self.cert_subs: self.cert_subs.add(sub) await self.queue.put((0, sub)) except Exception as e: pass
async def do_query(self, j, cur_domain): async with timeout(10.2): return await self.resolvers[j].resolve(cur_domain, 'A')
async def scan(self, j): self.resolvers[j].nameservers = [self.dns_servers[j % self.dns_count]] if self.dns_count > 1: while True: s = random.choice(self.dns_servers) if s != self.dns_servers[j % self.dns_count]: self.resolvers[j].nameservers.append(s) break empty_counter = 0 while True: try: brace_count, sub = self.queue.get_nowait() self.threads_status[j] = '1' empty_counter = 0 except asyncio.queues.QueueEmpty as e: empty_counter += 1 if empty_counter > 10: self.threads_status[j] = '0' if '1' not in self.threads_status: break else: await asyncio.sleep(0.1) continue
if brace_count > 0: brace_count -= 1 if sub.find('{next_sub}') >= 0: for _ in self.next_subs: await self.queue.put((0, sub.replace('{next_sub}', _))) if sub.find('{alphnum}') >= 0: for _ in 'abcdefghijklmnopqrstuvwxyz0123456789': await self.queue.put((brace_count, sub.replace('{alphnum}', _, 1))) elif sub.find('{alpha}') >= 0: for _ in 'abcdefghijklmnopqrstuvwxyz': await self.queue.put((brace_count, sub.replace('{alpha}', _, 1))) elif sub.find('{num}') >= 0: for _ in '0123456789': await self.queue.put((brace_count, sub.replace('{num}', _, 1))) continue
try: if sub in self.found_subs: continue
self.scan_count_local += 1 cur_domain = sub + '.' + self.domain
answers = await self.do_query(j, cur_domain) if answers: self.found_subs.add(sub) ips = ', '.join(sorted([answer.address for answer in answers])) invalid_ip_found = False for answer in answers: if answer.address in ['1.1.1.1', '127.0.0.1', '0.0.0.0', '0.0.0.1']: invalid_ip_found = True if invalid_ip_found: continue if self.options.i and is_intranet(answers[0].host): continue
try: cname = str(answers.canonical_name)[:-1] if cname != cur_domain and cname.endswith(self.domain): cname_sub = cname[:len(cname) - len(self.domain) - 1] if cname_sub not in self.found_subs and cname_sub not in self.normal_names_set: await self.queue.put((0, cname_sub)) except Exception as e: pass
first_level_sub = sub.split('.')[-1] max_found = 20
if self.options.w: first_level_sub = '' max_found = 3
if (first_level_sub, ips) not in self.ip_dict: self.ip_dict[(first_level_sub, ips)] = 1 else: self.ip_dict[(first_level_sub, ips)] += 1 if self.ip_dict[(first_level_sub, ips)] > max_found: continue
self.found_count_local += 1
self.outfile.write(cur_domain.ljust(30) + '\t' + ips + '\n') self.outfile.flush()
if not self.options.no_cert_check: async with timeout(10.0): await self.check_https_alt_names(cur_domain)
try: self.scan_count_local += 1 await self.do_query(j, 'lijiejie-test-not-existed.' + cur_domain)
except dns.resolver.NXDOMAIN as e: if self.queue.qsize() < 20000: for _ in self.next_subs: await self.queue.put((0, _ + '.' + sub)) else: await self.queue.put((1, '{next_sub}.' + sub)) except Exception as e: continue
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer) as e: pass except dns.resolver.NoNameservers as e: self.no_server_subs[sub] = self.no_server_subs.get(sub, 0) + 1 if self.no_server_subs[sub] <= 3: await self.queue.put((0, sub)) except (dns.exception.Timeout, dns.resolver.LifetimeTimeout) as e: self.timeout_subs[sub] = self.timeout_subs.get(sub, 0) + 1 if self.timeout_subs[sub] <= 3: await self.queue.put((0, sub)) except Exception as e: if str(type(e)).find('asyncio.exceptions.TimeoutError') < 0: with open('errors.log', 'a') as errFile: errFile.write('[%s] %s\n' % (type(e), str(e)))
async def async_run(self): await self.load_sub_names() tasks = [self.scan(i) for i in range(self.options.threads)] tasks.insert(0, self.update_counter()) await asyncio.gather(*tasks)
def run(self): loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(self.async_run())
def run_process(*params): signal.signal(signal.SIGINT, user_abort) s = SubNameBrute(*params) s.run()
for process_num in range(options.process): p = multiprocessing.Process( target=run_process, args=(domain, options, process_num, dns_servers, next_subs, scan_count, found_count, queue_size_array, tmp_dir) ) all_process.append(p) p.start()
|