def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.nmap") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.info("Starting scan of environment") try: nmap = NmapProcess(self.config['discovery']['plugins']['nmap']['target'], options=self.config['discovery']['plugins']['nmap']['flags']) except Exception as e: raise Exception("Failed to execute nmap process: {0}".format(e.message)) up = [] while True: nmap.run() nmap_report = NmapParser.parse(nmap.stdout) for scanned_host in nmap_report.hosts: if "up" in scanned_host.status and scanned_host.address not in up: up.append(scanned_host.address) logger.debug("Found new host: {0}".format(scanned_host.address)) if self.dbc.new_discovery(ip=scanned_host.address): logger.debug("Added host {0} to discovery queue".format( scanned_host.address)) else: logger.debug("Failed to add host {0} to discovery queue".format( scanned_host.address)) logger.debug("Scanned {0} hosts, {1} found up".format( len(nmap_report.hosts), len(up))) time.sleep(self.config['discovery']['plugins']['nmap']['interval']) return True
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.nmap") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.info("Starting scan of environment") try: nmap = NmapProcess( self.config['discovery']['plugins']['nmap']['target'], options=self.config['discovery']['plugins']['nmap']['flags']) except Exception as e: raise Exception("Failed to execute nmap process: {0}".format( e.message)) up = [] while True: nmap.run() nmap_report = NmapParser.parse(nmap.stdout) for scanned_host in nmap_report.hosts: if "up" in scanned_host.status and scanned_host.address not in up: up.append(scanned_host.address) logger.debug("Found new host: {0}".format( scanned_host.address)) if self.dbc.new_discovery(ip=scanned_host.address): logger.debug( "Added host {0} to discovery queue".format( scanned_host.address)) else: logger.debug( "Failed to add host {0} to discovery queue".format( scanned_host.address)) logger.debug("Scanned {0} hosts, {1} found up".format( len(nmap_report.hosts), len(up))) time.sleep(self.config['discovery']['plugins']['nmap']['interval']) return True
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.roster") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.debug("Getting hosts from Roster") while True: found = [] try: for ip in self.config['discovery']['plugins']['roster']['hosts']: found.append(ip) if self.dbc.new_discovery(ip=ip): logger.debug("Added host {0} to discovery queue".format(ip)) else: logger.debug("Failed to add host {0} to discovery queue".format(ip)) except KeyError as e: logger.warn("Configuration syntax error: {0}".format(e.message)) logger.info("Found {0} hosts".format(len(found))) if "unit_testing" in self.config.keys(): # Break out of loop for unit testing break else: # Adding sleep() so master process doesn't exit after completion time.sleep(900) # Return true for unit testing return True
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.roster") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.debug("Getting hosts from Roster") while True: found = [] try: for ip in self.config['discovery']['plugins']['roster'][ 'hosts']: found.append(ip) if self.dbc.new_discovery(ip=ip): logger.debug( "Added host {0} to discovery queue".format(ip)) else: logger.debug( "Failed to add host {0} to discovery queue".format( ip)) except KeyError as e: logger.warn("Configuration syntax error: {0}".format( e.message)) logger.info("Found {0} hosts".format(len(found))) if "unit_testing" in self.config.keys(): # Break out of loop for unit testing break else: # Adding sleep() so master process doesn't exit after completion time.sleep(900) # Return true for unit testing return True
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.digitalocean") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.debug("Getting hosts from DigitalOcean") # Define DO information for API Request url = "{0}/droplets".format(self.config['discovery']['plugins']['digitalocean']['url']) headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer {0}'.format( self.config['discovery']['plugins']['digitalocean']['api_key']) } while True: try: # Make http GET request and timeout after 3 seconds r = requests.get(url=url, headers=headers, timeout=3.0) except Exception as e: # Warn of issue and try again later logger.warn("Unable to query DigitalOcean API: {0}".format(e.message)) if "unit_testing" in self.config.keys(): raise Exception(e.message) else: continue ip_addrs = [] if r.status_code == 200: try: response = json.loads(r.text) except ValueError: # If we got bad json assume it's empty response = {} if "droplets" in response.keys(): for droplet in response['droplets']: for ip_type in ['v4', 'v6']: for interface in droplet['networks'][ip_type]: if interface['type'] == "public": logger.debug("Found host: {0}".format(interface['ip_address'])) ip_addrs.append(interface['ip_address']) else: logger.warn("Unable to query DigitalOcean API: HTTP Response {0}".format(r.status_code)) for ip in ip_addrs: if self.dbc.new_discovery(ip=ip): logger.debug("Added host {0} to discovery queue".format(ip)) else: logger.debug("Failed to add host {0} to discovery queue".format(ip)) logger.info("Found {0} hosts".format(len(ip_addrs))) if "unit_testing" in self.config.keys(): # Break out of loop for unit testing break else: time.sleep(self.config['discovery']['plugins']['digitalocean']['interval']) # Return true for unit testing return True
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.webping") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.info("Listening for web pings") WSGIServer((self.config['discovery']['plugins']['webping']['ip'], self.config['discovery']['plugins']['webping']['port']), application=self.grab_ip, log=None).serve_forever()
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.webping") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.info("Listening for web pings") WSGIServer((self.config['discovery']['plugins']['webping']['ip'], self.config['discovery']['plugins']['webping']['port']), application=self.grab_ip, log=None).serve_forever()
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.aws") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.info("Getting hosts from AWS") while True: # Setup IP List ip_addrs = [] try: # Connect to AWS session = boto3.session.Session( aws_access_key_id=self.config['discovery']['plugins'] ['aws']['aws_access_key_id'], aws_secret_access_key=self.config['discovery']['plugins'] ['aws']['aws_secret_access_key']) # Get Regions then connect to each and list instances for region in session.get_available_regions('ec2'): ec2 = session.client("ec2", region) data = ec2.describe_instances() for reservation in data['Reservations']: for instance in reservation['Instances']: # Check if filter should be public or private IP's if 'filter' in self.config['discovery']['plugins'][ 'aws']: ip_types = self.config['discovery']['plugins'][ 'aws']['filter'] else: # Default to both ip_types = [ 'PrivateIPAddress', 'PublicIPAddress' ] # Get IP's and Append to list for ip_type in ip_types: ip_addrs.append(instance[ip_type]) except Exception as e: logger.debug("Failed to query AWS: {0}".format(e.message)) # Process found IP's for ip in ip_addrs: if self.dbc.new_discovery(ip=ip): logger.debug( "Added host {0} to discovery queue".format(ip)) else: logger.debug( "Failed to add host {0} to discovery queue".format(ip)) logger.debug("Found {0} hosts".format(len(ip_addrs))) if "unit_testing" in self.config.keys(): # Break out of loop for unit testing break else: time.sleep( self.config['discovery']['plugins']['aws']['interval']) # Return true for unit testing return True
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.linode") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.info("Getting hosts from Linode") # Define Linode information for API Request url = "{0}/".format(self.config['discovery']['plugins']['linode']['url']) params = { 'api_key': '{0}'.format(self.config['discovery']['plugins']['linode']['api_key']), 'api_action': 'linode.ip.list' } while True: try: # Make http GET request and timeout after 3 seconds r = requests.get(url=url, params=params, timeout=3.0) except Exception as e: # Warn of issue and try again later logger.warn("Unable to query Linode API: {0}".format(e.message)) if "unit_testing" in self.config.keys(): raise Exception(e.message) else: continue ip_addrs = [] if r.status_code >= 200 and r.status_code <= 300: try: response = json.loads(r.text) except ValueError: # If we got bad json assume it's empty response = {'DATA': []} for node in response['DATA']: logger.debug("Found host: {0}".format(node['IPADDRESS'])) ip_addrs.append(node['IPADDRESS']) else: logger.warn("Unable to query Linode API: HTTP Response {0}".format(r.status_code)) for ip in ip_addrs: if self.dbc.new_discovery(ip=ip): logger.debug("Added host {0} to discovery queue".format(ip)) else: logger.debug("Failed to add host {0} to discovery queue".format(ip)) logger.debug("Found {0} hosts".format(len(ip_addrs))) if "unit_testing" in self.config.keys(): # Break out of loop for unit testing break else: time.sleep(self.config['discovery']['plugins']['linode']['interval']) # Return true for unit testing return True
def start(self): ''' Start Discovery ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.aws") logger = logs.getLogger() logger = logs.clean_handlers(logger) logger.info("Getting hosts from AWS") while True: # Setup IP List ip_addrs = [] try: # Connect to AWS session = boto3.session.Session( aws_access_key_id=self.config['discovery']['plugins']['aws']['aws_access_key_id'], aws_secret_access_key=self.config['discovery']['plugins']['aws']['aws_secret_access_key']) # Get Regions then connect to each and list instances for region in session.get_available_regions('ec2'): ec2 = session.client("ec2", region) data = ec2.describe_instances() for reservation in data['Reservations']: for instance in reservation['Instances']: # Check if filter should be public or private IP's if 'filter' in self.config['discovery']['plugins']['aws']: ip_types = self.config['discovery']['plugins']['aws']['filter'] else: # Default to both ip_types = [ 'PrivateIPAddress', 'PublicIPAddress' ] # Get IP's and Append to list for ip_type in ip_types: ip_addrs.append(instance[ip_type]) except Exception as e: logger.debug("Failed to query AWS: {0}".format(e.message)) # Process found IP's for ip in ip_addrs: if self.dbc.new_discovery(ip=ip): logger.debug("Added host {0} to discovery queue".format(ip)) else: logger.debug("Failed to add host {0} to discovery queue".format(ip)) logger.debug("Found {0} hosts".format(len(ip_addrs))) if "unit_testing" in self.config.keys(): # Break out of loop for unit testing break else: time.sleep(self.config['discovery']['plugins']['aws']['interval']) # Return true for unit testing return True
def grab_ip(self, env, start_response): ''' Grab IP from Webserver event ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.webping") logger = logs.getLogger() logger = logs.clean_handlers(logger) up = [] ip = env['REMOTE_ADDR'] if ip not in up: up.append(ip) logger.debug("Found new host: {0}".format(ip)) if self.dbc.new_discovery(ip=ip): logger.debug("Added host {0} to discovery queue".format(ip)) else: logger.debug("Failed to add host {0} to discovery queue".format(ip)) start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Success']
def grab_ip(self, env, start_response): ''' Grab IP from Webserver event ''' logs = core.logs.Logger(config=self.config, proc_name="discovery.webping") logger = logs.getLogger() logger = logs.clean_handlers(logger) up = [] ip = env['REMOTE_ADDR'] if ip not in up: up.append(ip) logger.info("Found new host: {0}".format(ip)) if self.dbc.new_discovery(ip=ip): logger.debug("Added host {0} to discovery queue".format(ip)) else: logger.debug("Failed to add host {0} to discovery queue".format(ip)) start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Success']