|
| 1 | +import pymongo |
| 2 | +from hawk_scanner.internals import system |
| 3 | +import re |
| 4 | +from rich.console import Console |
| 5 | +from rich.table import Table |
| 6 | + |
| 7 | +console = Console() |
| 8 | + |
| 9 | +def connect_mongodb(host, port, username, password, database, uri=None): |
| 10 | + try: |
| 11 | + if uri: |
| 12 | + client = pymongo.MongoClient(uri) |
| 13 | + else: |
| 14 | + client = pymongo.MongoClient(host=host, port=port, username=username, password=password) |
| 15 | + |
| 16 | + if database not in client.list_database_names(): |
| 17 | + system.print_error(f"Database {database} not found on MongoDB server.") |
| 18 | + return None |
| 19 | + |
| 20 | + db = client[database] |
| 21 | + system.print_info(f"Connected to MongoDB database") |
| 22 | + return db |
| 23 | + except Exception as e: |
| 24 | + system.print_error(f"Failed to connect to MongoDB database with error: {e}") |
| 25 | + return None |
| 26 | + |
| 27 | + |
| 28 | +def check_data_patterns(db, patterns, profile_name, database_name): |
| 29 | + results = [] |
| 30 | + for collection_name in db.list_collection_names(): |
| 31 | + collection = db[collection_name] |
| 32 | + for document in collection.find(): |
| 33 | + for field_name, field_value in document.items(): |
| 34 | + if field_value: |
| 35 | + value_str = str(field_value) |
| 36 | + matches = system.match_strings(value_str) |
| 37 | + if matches: |
| 38 | + for match in matches: |
| 39 | + results.append({ |
| 40 | + 'host': db.client.address[0], |
| 41 | + 'database': database_name, |
| 42 | + 'collection': collection_name, |
| 43 | + 'field': field_name, |
| 44 | + 'pattern_name': match['pattern_name'], |
| 45 | + 'matches': match['matches'], |
| 46 | + 'sample_text': match['sample_text'], |
| 47 | + 'profile': profile_name, |
| 48 | + 'data_source': 'mongodb' |
| 49 | + }) |
| 50 | + |
| 51 | + return results |
| 52 | + |
| 53 | +def execute(args): |
| 54 | + results = [] |
| 55 | + system.print_info(f"Running Checks for MongoDB Sources") |
| 56 | + connections = system.get_connection() |
| 57 | + |
| 58 | + if 'sources' in connections: |
| 59 | + sources_config = connections['sources'] |
| 60 | + mongodb_config = sources_config.get('mongodb') |
| 61 | + |
| 62 | + if mongodb_config: |
| 63 | + patterns = system.get_fingerprint_file() |
| 64 | + |
| 65 | + for key, config in mongodb_config.items(): |
| 66 | + host = config.get('host') |
| 67 | + port = config.get('port', 27017) # default MongoDB port |
| 68 | + username = config.get('username') |
| 69 | + password = config.get('password') |
| 70 | + database = config.get('database') |
| 71 | + uri = config.get('uri') # Added support for URI |
| 72 | + |
| 73 | + if uri: |
| 74 | + system.print_info(f"Checking MongoDB Profile {key} using URI") |
| 75 | + elif host and username and password and database: |
| 76 | + system.print_info(f"Checking MongoDB Profile {key} with host and authentication") |
| 77 | + else: |
| 78 | + system.print_error(f"Incomplete MongoDB configuration for key: {key}") |
| 79 | + continue |
| 80 | + |
| 81 | + db = connect_mongodb(host, port, username, password, database, uri) |
| 82 | + if db: |
| 83 | + results += check_data_patterns(db, patterns, key, database) |
| 84 | + else: |
| 85 | + system.print_error("No MongoDB connection details found in connection.yml") |
| 86 | + else: |
| 87 | + system.print_error("No 'sources' section found in connection.yml") |
| 88 | + return results |
0 commit comments