def inventory_db(): """Sets up a test inventory database and returns the database name""" config.parse_config() fake = Faker() dbname = '_'.join([ 'fakedb', fake.word(), fake.word(), fake.word(), ]) config.cc.inventory.name = dbname # Create the database db = io_sql.sql_database() db.create_database(dbname) assert db.database_exists(dbname) del (db) print('Inventroy_db: ', dbname) # Pass the database to the test functions yield print('Done with inventory_db: ', dbname) # Delete the database after use db = io_sql.sql_database() db.delete_database(dbname) assert not db.database_exists(dbname)
def run_audit(csv_path): ''' Given a CSV of subnets and MAC addresses, search the database for all MACs on subnets which match those in the CSV. Compare each MAC and output a new csv with any matching MAC's listed by confidence (number of matching characters, starting from the OUI. This can be used, for example, for a Wireless Rogue SSID audit, for which the MAC address of the radios is known and you want to find out which rogue AP's are physically connected to your network. ''' if config.cc.modified is False: config.parse_config() # Open the input CSV entries= _open_csv(csv_path) csv_subnets= sort_csv_by_subnet(entries) print ('CSV Len: ', len(csv_subnets)) device_db = io_sql.device_db() results=[] mp = MacParser(update=True) # Iterate over each subnet where a rogue was detected for subnet in sorted(csv_subnets): print('Subnet: ', subnet) # Iterate over each mac in the subnet for mac in device_db.macs_on_subnet(subnet): # Iterate over each mac in the CSV subnet and # find matches for csv_row in csv_subnets[subnet]: x= evaluate_mac(mac, csv_row['mac']) if x > 50: csv_row= dict(csv_row) csv_row['confidence'] = x csv_row['wired_mac'] = mac csv_row['Manufacturer'] = mp.search(mac) results.append(csv_row) results= sorted(results, key=lambda x: x['confidence'], reverse=True) if len(results) == 0: return False write_csv(results) write_report(results)
def main(argv=None): # IGNORE:C0111 '''Command line options.''' if argv is None: argv = sys.argv else: sys.argv.extend(argv) config.parse_config() program_name = os.path.basename(sys.argv[0]) program_version = "v%s" % __version__ program_build_date = str(__updated__) program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date) program_shortdesc = __import__('__main__').__doc__.split("\n")[1] program_license = textwrap.dedent('''\ %s Created by Wyko ter Haar on %s. Licensed under the MIT License Distributed on an "AS IS" basis without warranties or conditions of any kind, either express or implied. ''' % (program_shortdesc, str(__date__))) try: # Setup argument parser parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter) parser.add_argument("-v", "--verbose", dest="verbose", action="count", help="set verbosity level [default: %(default)s]") parser.add_argument(dest="macs", help="MAC addresses to locate", metavar="MACs", nargs='+') # Process arguments args = parser.parse_args() config.cc.verbosity= args.verbose locate(args.macs) except KeyboardInterrupt: ### handle keyboard interrupt ### return 0 except Exception as e: if DEBUG: raise(e) indent = len(program_name) * " " sys.stderr.write(program_name + ": " + repr(e) + "\n") sys.stderr.write(indent + " for help use --help") return 2
# Break at the first bad match else: break #=========================================================================== # # Use this to return the exact number of characters matched # return count #=========================================================================== # Returns a percentage match if count==0: return 0 return int((count / len(mac1)) * 100) if __name__ == '__main__': import argparse config.parse_config() parser = argparse.ArgumentParser(description='Perform an audit of MACs on the network') parser.add_argument('csv', help='A csv file to audit.') args = parser.parse_args() run_audit(args.csv)
def setup_module(module): config.parse_config()