def main(args): for choice in ['netcat', 'nc', 'telnet']: if os.system('which %s &> /dev/null' % choice) == 0: prog = choice break else: raise CommandError('Could not find an installed telnet.') target = args.target if ':' in target: host, port = target.split(':', 1) else: host, port = 'localhost', target rlwrap = args.rlwrap cmd = [prog, str(host), str(port)] if prog == 'netcat': cmd.append('--close') if rlwrap is None: rlwrap = os.system('which rlwrap &> /dev/null') == 0 if rlwrap: cmd.insert(0, 'rlwrap') try: if call(cmd) != 0: raise CommandError( 'Backdoor unreachable on {}'.format(target) ) except (EOFError, KeyboardInterrupt): print() if choice == 'telnet' and rlwrap: call(['reset'])
def import_service(module_name): parts = module_name.split(":", 1) if len(parts) == 1: module_name, obj = module_name, None else: module_name, obj = parts[0], parts[1] try: __import__(module_name) except ImportError as exc: if module_name.endswith(".py") and os.path.exists(module_name): raise CommandError( "Failed to find service, did you mean '{}'?".format( module_name[:-3].replace('/', '.') ) ) missing_module_re = MISSING_MODULE_TEMPLATE.format(module_name) # is there a better way to do this? if re.match(missing_module_re, str(exc)): raise CommandError(exc) # found module, but importing it raised an import error elsewhere # let this bubble (resulting in a full stacktrace being printed) raise module = sys.modules[module_name] if obj is None: found_services = [] # find top-level objects with entrypoints for _, potential_service in inspect.getmembers(module, is_type): if inspect.getmembers(potential_service, is_entrypoint): found_services.append(potential_service) if not found_services: raise CommandError( "Failed to find anything that looks like a service in module " "{!r}".format(module_name) ) else: try: service_cls = getattr(module, obj) except AttributeError: raise CommandError( "Failed to find service class {!r} in module {!r}".format( obj, module_name) ) if not isinstance(service_cls, type): raise CommandError("Service must be a class.") found_services = [service_cls] return found_services
def register_services(r, package): found_services = [] for module_name in find_modules(package): module = import_string(module_name) print(module, module_name) for _, potential_service in inspect.getmembers(module, is_type): if inspect.getmembers(potential_service, is_entrypoint): found_services.append(potential_service) r.add_service(potential_service) if not found_services: raise CommandError( "Failed to find anything that looks like a service in package " "{!r}".format(package))