def __retrive_active_services(self): """ Parse the .services file generated by the scanner an retrieve active services. """ services = {} try: with open(self.root_service_log) as fd_services_log: with open(self.root_service_cfg, "r+") as fd_service_config: active = load(fd_service_config) fd_service_config.seek(0) fd_service_config.truncate() for info in fd_services_log.readlines(): proto, status, port = info.split("||") if status.strip() == "open": print("Writing active protocol : %s" % proto.strip()) active["active"].append(proto.strip()) # prepares the services to be activated to the for service in active["servers"]: if service["type"] in active["active"]: services["servers"] = [{ "type": service["type"], "config": service["config"] }] # save in active field which services are currently activated on this machine dump(active, fd_service_config, indent=4) return services except: message(message="Fatal error when editing the configuration file", sym="[!]", color="red") sys.exit(1)
def shell(self): """ Command and Controll system """ while True: cmd = input("shell>") if cmd == "exit": # close honeypot break elif cmd == "start": """ This command allows you to start a certain service or a list of services """ pass elif cmd == "logs": """ This command allows you to view a real time log associated to a running service """ pass elif cmd == "kill": """ This command allows you kill a runnig service or a list of running services """ pass else: message("Invalid command", "[!]", "red")
def check_service_cfg(file): """ Verify the file @param file: JSON config file """ services_cfg_root = os.path.join("services-cfg", file) if not os.path.exists(services_cfg_root) and file[-4:] is not "json": msg = "%s is not a json file, or file doesn't exists yet" % file message(sym="[!]", message=msg, color="red") sys.exit(1)
def check_machine_services(params): """ Verify if active.services file is been created @param file: .services log file """ if len(params) == 1: machine_services_root = os.path.join("machine-services", params[0]) if not os.path.exists(machine_services_root): msg = "%s doesn't not exists yet, use --createnow command to start scan now" % machine_services_root message(sym="[!]", message=msg, color="red") help(parser) sys.exit(1) else: if not machine_services_root.split(".")[1] == "services": msg = "%s is not a 'services' file" % machine_services_root message(sym="[!]", message=msg, color="red") help(parser) sys.exit(1) elif len(params) == 2: # need to create now the file of services machine_services_root = os.path.join("machine-services", params[0]) cmd = params[1] if not os.path.exists(machine_services_root): if cmd == "createnow": # start nmap default scan here pass else: msg = "'%s' services file already exists" % cmd message(sym="[!]", message=msg, color="red") help(parser) sys.exit(1) else: msg = "services file already exists don't use 'createnow' option" message(sym="[!]", message=msg, color="red") help(parser) sys.exit(1) else: msg = "invalid arguments" message(sym="[!]", message=msg, color="red") help(parser) sys.exit(1)
sys.exit(1) if __name__ == "__main__": parser = argparse.ArgumentParser( description= "start different fake services to research hacker's attack pattern to network services" ) parser.add_argument( "scan_log_file", help= "scan the machine searching for active services. Scan type depens on the list of command passed", nargs="+") parser.add_argument( "service_config_file", help="start services from the configuration file passed") # get all values args = parser.parse_args() if is_root(): # check parsed value check_machine_services(args.scan_log_file) check_service_cfg(args.service_config_file) # start honeypot pass else: message(sym="[#]", message="root privileges are required", color="yellow")