def ignore_aiohttp_ssl_error(loop: AbstractEventLoop): original_handler = loop.get_exception_handler() def ignore_ssl_error(loop: AbstractEventLoop, context: Dict): # Ignore SSLError from `aiohttp` module. It is known as a bug. if isinstance(context["exception"], ssl.SSLError): return if original_handler is not None: original_handler(loop, context) else: loop.default_exception_handler(context) loop.set_exception_handler(ignore_ssl_error)
async def brute_basic(http_target_factory, combo_factory=None, username_factory=None, password_factory=None, pool_size: int = 100, timeout: float = 5.0, output: str = None, loop: asyncio.AbstractEventLoop = None, verbose: bool = False): loop = loop or asyncio.get_event_loop() tasks = target_tasks( http_target_factory, combo_factory, username_factory, password_factory, ) def def_handler(_loop, ctx): loop.default_exception_handler(ctx) old_handler = loop.get_exception_handler() or def_handler def new_handler(_loop, ctx): exc = ctx.get("exception") ignore = ( ssl.SSLError, asyncio.CancelledError, ) if isinstance(exc, ignore): return old_handler(_loop, ctx) loop.set_exception_handler(new_handler) try: async with AioPool(pool_size) as pool: suc_files = [sys.stdout] err_files = [sys.stderr] if output: f = open(output, "a+") suc_files.append(f) kwargs = dict(timeout=timeout*1.5, get_result=getres.pair) async for (val, err) in pool.itermap(worker, tasks, **kwargs): if err: print("ERROR") print(err) continue result, (url, username, password) = val if result is True: print(C.GRN, file=sys.stdout, end="") for file in suc_files: print("{} {}:{}".format(url, username, password), file=file, flush=True) print(C.RST, file=sys.stdout, end="", flush=True) elif verbose: print(C.RED, file=sys.stderr, end="") for file in err_files: print("Failure {} {}:{}".format(url, username, password), file=file, flush=True) print(C.RST, file=sys.stderr, end="", flush=True) finally: loop.set_exception_handler(old_handler)