def start_existing_listeners(self): """ Startup any listeners that are current in the database. """ cur = self.conn.cursor() cur.execute("SELECT id,name,host,port,cert_path,staging_key,default_delay,default_jitter,default_profile,kill_date,working_hours,listener_type,redirect_target,default_lost_limit FROM listeners") results = cur.fetchall() cur.close() # for each listener in the database, add it to the cache for result in results: # don't start the listener unless it's a native one if result[11] != "native": self.listeners[result[0]] = None else: lhost = http.host2lhost(result[2]) port = result[3] # if cert_path is empty, no ssl is used cert_path = result[4] # build the handler server and kick if off server = http.EmpireServer(self.agents, lhost=lhost, port=port, cert=cert_path) # check if the listener started correctly if server.success: server.start() if (server.base_server()): # store off this servers in the "[id] : server" object array # only if the server starts up correctly self.listeners[result[0]] = server
def add_listener_from_config(self): """ Start up a new listener with the internal config information. """ name = self.options['Name']['Value'] host = self.options['Host']['Value'] port = self.options['Port']['Value'] certPath = self.options['CertPath']['Value'] stagingKey = self.options['StagingKey']['Value'] defaultDelay = self.options['DefaultDelay']['Value'] defaultJitter = self.options['DefaultJitter']['Value'] defaultProfile = self.options['DefaultProfile']['Value'] killDate = self.options['KillDate']['Value'] workingHours = self.options['WorkingHours']['Value'] listenerType = self.options['Type']['Value'] redirectTarget = self.options['RedirectTarget']['Value'] defaultLostLimit = self.options['DefaultLostLimit']['Value'] # validate all of the options if self.validate_listener_options(): # if the listener name already exists, iterate the name # until we have a valid one if self.is_listener_valid(name): baseName = name for x in xrange(1,20): name = str(baseName) + str(x) if not self.is_listener_valid(name): break if self.is_listener_valid(name): return (False, "Listener name already used.") # don't actually start a pivot/hop listener, foreign listeners, or meter listeners if listenerType == "pivot" or listenerType == "hop" or listenerType == "foreign" or listenerType == "meter": # double-check that the host ends in .php for hop listeners if listenerType == "hop" and not host.endswith(".php"): choice = raw_input(helpers.color("[!] Host does not end with .php continue? [y/N] ")) if choice.lower() == "" or choice.lower()[0] == "n": return (False, "") cur = self.conn.cursor() results = cur.execute("INSERT INTO listeners (name, host, port, cert_path, staging_key, default_delay, default_jitter, default_profile, kill_date, working_hours, listener_type, redirect_target,default_lost_limit) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", [name, host, port, certPath, stagingKey, defaultDelay, defaultJitter, defaultProfile, killDate, workingHours, listenerType, redirectTarget,defaultLostLimit] ) # get the ID for the listener cur.execute("SELECT id FROM listeners where name=?", [name]) result = cur.fetchone() cur.close() self.listeners[result[0]] = None return (True, name) else: lhost = http.host2lhost(host) # start up the server object server = http.EmpireServer(self.agents, lhost=lhost, port=port, cert=certPath) # check if the listener started correctly if server.success: server.start() if (server.base_server()): # add the listener to the database if start up cur = self.conn.cursor() results = cur.execute("INSERT INTO listeners (name, host, port, cert_path, staging_key, default_delay, default_jitter, default_profile, kill_date, working_hours, listener_type, redirect_target, default_lost_limit) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", [name, host, port, certPath, stagingKey, defaultDelay, defaultJitter, defaultProfile, killDate, workingHours, listenerType, redirectTarget,defaultLostLimit] ) # get the ID for the listener cur.execute("SELECT id FROM listeners where name=?", [name]) result = cur.fetchone() cur.close() # store off this server in the "[id] : server" object array # only if the server starts up correctly self.listeners[result[0]] = server return (True, name) else: return (False, "Misc. error starting listener") else: return (False, "Error starting listener on port %s, port likely already in use." %(port)) else: return (False, "Required listener option missing.")