|
21 | 21 |
|
22 | 22 |
|
23 | 23 | def get_oss_info_from_db(bin_info_list, dburl=""): |
| 24 | + import socket |
24 | 25 | _cnt_auto_identified = 0 |
25 | 26 | conn_str = get_connection_string(dburl) |
26 | | - connect_to_lge_bin_db(conn_str) |
27 | | - |
28 | | - if conn != "" and cur != "": |
29 | | - for item in bin_info_list: |
30 | | - bin_oss_items = [] |
31 | | - tlsh_value = item.tlsh |
32 | | - checksum_value = item.checksum |
33 | | - bin_file_name = item.binary_name_without_path |
34 | | - |
35 | | - df_result = get_oss_info_by_tlsh_and_filename( |
36 | | - bin_file_name, checksum_value, tlsh_value) |
37 | | - if df_result is not None and len(df_result) > 0: |
38 | | - _cnt_auto_identified += 1 |
39 | | - # Initialize the saved contents at .jar analyzing only once |
40 | | - if not item.found_in_owasp and item.oss_items: |
41 | | - item.oss_items = [] |
42 | | - |
43 | | - for idx, row in df_result.iterrows(): |
44 | | - if not item.found_in_owasp: |
45 | | - oss_from_db = OssItem(row['ossname'], row['ossversion'], row['license']) |
46 | | - |
47 | | - if bin_oss_items: |
48 | | - if not any(oss_item.name == oss_from_db.name |
49 | | - and oss_item.version == oss_from_db.version |
50 | | - and oss_item.license == oss_from_db.license |
51 | | - for oss_item in bin_oss_items): |
| 27 | + # DB URL에서 host 추출 |
| 28 | + try: |
| 29 | + dbc = urlparse( dburl if dburl else "postgresql://bin_analysis_script_user:[email protected]:5432/bat") |
| 30 | + db_host = dbc.hostname |
| 31 | + except Exception as ex: |
| 32 | + logger.warning(f"Failed to parse DB URL for host: {ex}") |
| 33 | + db_host = None |
| 34 | + |
| 35 | + is_internal = False |
| 36 | + if db_host: |
| 37 | + try: |
| 38 | + # DNS lookup 시도 |
| 39 | + socket.gethostbyname(db_host) |
| 40 | + is_internal = True |
| 41 | + except Exception: |
| 42 | + is_internal = False |
| 43 | + |
| 44 | + if is_internal: |
| 45 | + connect_to_lge_bin_db(conn_str) |
| 46 | + if conn != "" and cur != "": |
| 47 | + for item in bin_info_list: |
| 48 | + bin_oss_items = [] |
| 49 | + tlsh_value = item.tlsh |
| 50 | + checksum_value = item.checksum |
| 51 | + bin_file_name = item.binary_name_without_path |
| 52 | + |
| 53 | + df_result = get_oss_info_by_tlsh_and_filename( |
| 54 | + bin_file_name, checksum_value, tlsh_value) |
| 55 | + if df_result is not None and len(df_result) > 0: |
| 56 | + _cnt_auto_identified += 1 |
| 57 | + # Initialize the saved contents at .jar analyzing only once |
| 58 | + if not item.found_in_owasp and item.oss_items: |
| 59 | + item.oss_items = [] |
| 60 | + |
| 61 | + for idx, row in df_result.iterrows(): |
| 62 | + if not item.found_in_owasp: |
| 63 | + oss_from_db = OssItem(row['ossname'], row['ossversion'], row['license']) |
| 64 | + |
| 65 | + if bin_oss_items: |
| 66 | + if not any(oss_item.name == oss_from_db.name |
| 67 | + and oss_item.version == oss_from_db.version |
| 68 | + and oss_item.license == oss_from_db.license |
| 69 | + for oss_item in bin_oss_items): |
| 70 | + bin_oss_items.append(oss_from_db) |
| 71 | + else: |
52 | 72 | bin_oss_items.append(oss_from_db) |
53 | | - else: |
54 | | - bin_oss_items.append(oss_from_db) |
55 | | - |
56 | | - if bin_oss_items: |
57 | | - item.set_oss_items(bin_oss_items) |
58 | | - item.comment = "Binary DB result" |
59 | | - item.found_in_binary = True |
60 | 73 |
|
61 | | - disconnect_lge_bin_db() |
| 74 | + if bin_oss_items: |
| 75 | + item.set_oss_items(bin_oss_items) |
| 76 | + item.comment = "Binary DB result" |
| 77 | + item.found_in_binary = True |
| 78 | + else: |
| 79 | + logger.warning(f"Internal network detected, but DB connection to '{db_host}' failed. Skipping DB query.") |
| 80 | + disconnect_lge_bin_db() |
| 81 | + else: |
| 82 | + logger.info(f"Binary DB host '{db_host}' is not reachable. Skipping DB query (likely external network).") |
62 | 83 | return bin_info_list, _cnt_auto_identified |
63 | 84 |
|
64 | 85 |
|
|
0 commit comments