def test_inventory(self): clients_dict = test_dummy() config = get_all_config() # Generate burp_reports object to use for reports. reports = BurpReports(clients_dict, days_outdated=int(config['common']['days_outdated']), config=config) reports.save_compared_inventory(__inventory__, __output__) compared_clients = csv_as_dict(__output__, config['inventory_columns']['client_name'], delimiter=config['common']['csv_delimiter']) return compared_clients
def test_inventory(self): clients_dict = test_dummy() config = get_all_config() # Generate burp_reports object to use for reports. reports = BurpReports(clients_dict, days_outdated=int( config['common']['days_outdated']), config=config) reports.save_compared_inventory(__inventory__, __output__) compared_clients = csv_as_dict( __output__, config['inventory_columns']['client_name'], delimiter=config['common']['csv_delimiter']) return compared_clients
def compare_inventory(self, csv_inventory): """ :param csv_inventory: Input filename to compare from (also can be an url to download it) :return: list csv_rows_inventory_status (status of each client) in nested list one row per client """ # Set dict of status from config all_status = dict(self.config['inventory_status']) # Set variables of status as lists, also enable possibility to have more than one status: spare_status = all_status['spare'].split(',') active_status = all_status['active'].split(',') # Set dict of status from config all_columns = dict(self.config['inventory_columns']) client_column = all_columns['client_name'] delimiter = self.common_config['csv_delimiter'] # Download the csv if it is in a url if validators.url(csv_inventory): get_csv_from_url(csv_inventory, csv_output=self.tempfile, delimiter=delimiter) csv_inventory = self.tempfile # Get inventory from CSV file inventory = csv_as_dict(csv_inventory, client_column, delimiter=delimiter) # Get a list of clients outdated in burp: outdated_clients = self._get_outdated() csv_rows_inventory_status = [] headers = [] headers.insert(0, client_column) # Change First header column as client headers.insert(1, all_columns['status']) # Change Second header column as status headers.insert(2, all_columns['server']) # Change Third header column as server headers.insert(3, all_columns['sub_status']) # Change Fourth header column as status detailed first_client = next(iter(inventory.keys())) # Gets first key of dict for column in sorted(inventory[first_client].keys()): # Get headers from input inventory sorted if column.lower() not in all_columns.values(): # Add to the list only if not in the all_columns defined headers.append(column) # Prepare the header list to return: csv_rows_inventory_status.append(headers) # First row as headers # Verify inventory and compare with clients in burp for k in sorted(inventory): client = k server_name = '' # Do not check excluded clients if client in self.excluded_clients: continue # Columns required to verify the status, lowercase and without spaces at end and beginning status = inventory[client].get(all_columns['status'], '').lower().strip() sub_status = inventory[client].get(all_columns['sub_status'], '').lower().strip() # If the client is in burp servers if client in self.clients: if sub_status in spare_status: burp_status = all_status['spare_in_burp'] elif status not in active_status: burp_status = all_status['inactive_in_burp'] else: burp_status = all_status['in_inventory_updated'] # Set never/outdated if the client is outdated if client in outdated_clients: burp_status = outdated_clients[client]['b_status'] # If the client is not in burp servers: --- # Define the status as ignored for clients spare elif sub_status in spare_status: burp_status = all_status['spare_not_in_burp'] # Define ignored to client not active: elif status not in active_status: burp_status = all_status['inactive_not_in_burp'] else: # Define absent in burp burp_status = all_status['in_inventory_not_in_burp'] # Add server_name information if self.clients[client].get('server', None): server_name = self.clients[client]['server'] # Mark the status of the client as duplicated if there is more than one server on it. if len(server_name) > 1: burp_status = all_status['in_many_servers'] # Generate list row with client's status and other data row = [client, burp_status, server_name, sub_status] # Add all other columns, starting from fourth column in headers for i in range(4, len(headers)): row.append(inventory[client].get(headers[i])) # Insert each value of client to the row csv_rows_inventory_status.append(row) # Check if there is some client in burp but not in the inventory for burp_client in sorted(self.clients): # Do not check excluded clients if burp_client in self.excluded_clients: continue if burp_client not in inventory: burp_status = all_status['not_inventory_in_burp'] if self.clients[burp_client].get('server', None): server_name = self.clients[burp_client]['server'] else: server_name = [] sub_status = '' # Generate list row with client's status and other data row = [burp_client, burp_status, server_name, sub_status] csv_rows_inventory_status.append(row) return csv_rows_inventory_status
def compare_inventory(self, csv_inventory): """ :param csv_inventory: Input filename to compare from (also can be an url to download it) :return: list csv_rows_inventory_status (status of each client) in nested list one row per client """ # Set dict of status from config all_status = dict(self.config['inventory_status']) # Set variables of status as lists, also enable possibility to have more than one status: spare_status = all_status['spare'].split(',') active_status = all_status['active'].split(',') # Set dict of status from config all_columns = self.inventory_columns client_column = all_columns['client_name'] delimiter = self.common_config['csv_delimiter'] # Download the csv if it is in a url if validators.url(csv_inventory): get_csv_from_url(csv_inventory, csv_output=self.tempfile, delimiter=delimiter) csv_inventory = self.tempfile elif csv_inventory == 'email_inventory': if self.config.has_section('email_inventory'): email_inventory_config = dict(self.config['email_inventory']) email_inventory_obj = ImapReceive(config=email_inventory_config) email_inventory_obj.download_attachment() # Read email and download csv_inventory = email_inventory_obj.save_file else: print("you must setup the section [email_inventory]" "in configuration to use email_inventory") exit(2) # Get inventory from CSV file inventory = csv_as_dict(csv_inventory, client_column, delimiter=delimiter, encoding=self.common_config.get('csv_encoding')) # Get a list of clients outdated in burp: outdated_clients = self._get_outdated() csv_rows_inventory_status = [] headers = [] headers.insert(0, client_column) # Change First header column as client headers.insert(1, all_columns['status']) # Change Second header column as status headers.insert(2, all_columns['server']) # Change Third header column as server # Change Fourth header column as status detailed headers.insert(3, all_columns['sub_status']) first_client = next(iter(inventory.keys())) # Gets first key of dict for column in sorted(inventory[first_client].keys()): # Get headers from input inventory sorted if column.lower() not in all_columns.values(): # Add to the list only if not in the all_columns defined headers.append(column) # Prepare the header list to return: csv_rows_inventory_status.append(headers) # First row as headers # Verify inventory and compare with clients in burp for k in sorted(inventory): client = k server_name = '' # Do not check excluded clients if client in self.excluded_clients: continue # Columns required to verify the status, lowercase and without spaces at end and beginning status = inventory[client].get(all_columns['status'], '').lower().strip() sub_status = inventory[client].get(all_columns['sub_status'], '').lower().strip() # If the client is in burp servers if client in self.clients: if sub_status in spare_status: burp_status = all_status['spare_in_burp'] elif status not in active_status: burp_status = all_status['inactive_in_burp'] else: burp_status = all_status['in_inventory_updated'] # Set never/outdated if the client is outdated if client in outdated_clients: burp_status = outdated_clients[client]['b_status'] # If the client is not in burp servers: --- # Define the status as ignored for clients spare elif sub_status in spare_status: burp_status = all_status['spare_not_in_burp'] # Define ignored to client not active: elif status not in active_status: burp_status = all_status['inactive_not_in_burp'] else: # Define absent in burp burp_status = all_status['in_inventory_not_in_burp'] # Add server_name information if self.clients[client].get('server', None): server_name = self.clients[client]['server'] # Mark the status of the client as duplicated if there is more than one server on it. if len(server_name) > 1: burp_status = all_status['in_many_servers'] # Generate list row with client's status and other data row = [client, burp_status, server_name, sub_status] # Add all other columns, starting from fourth column in headers for i in range(4, len(headers)): row.append(inventory[client].get(headers[i])) # Insert each value of client to the row csv_rows_inventory_status.append(row) # Check if there is some client in burp but not in the inventory for burp_client in sorted(self.clients): # Do not check excluded clients if burp_client in self.excluded_clients: continue if burp_client not in inventory: burp_status = all_status['not_inventory_in_burp'] if self.clients[burp_client].get('server', None): server_name = self.clients[burp_client]['server'] else: server_name = [] sub_status = '' # Generate list row with client's status and other data row = [burp_client, burp_status, server_name, sub_status] csv_rows_inventory_status.append(row) return csv_rows_inventory_status