|
| 1 | +import io |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +from tempfile import NamedTemporaryFile |
| 5 | +from zipfile import ZipFile |
| 6 | + |
| 7 | +import openpyxl |
| 8 | + |
| 9 | +from navv.bll import get_inventory_report_df, get_snmp_df, get_zeek_df |
| 10 | +from navv.spreadsheet_tools import ( |
| 11 | + auto_adjust_width, |
| 12 | + create_analysis_array, |
| 13 | + get_inventory_data, |
| 14 | + get_package_data, |
| 15 | + get_segments_data, |
| 16 | + get_workbook, |
| 17 | + perform_analysis, |
| 18 | + write_conn_states_sheet, |
| 19 | + write_externals_sheet, |
| 20 | + write_inventory_report_sheet, |
| 21 | + write_snmp_sheet, |
| 22 | + write_stats_sheet, |
| 23 | + write_unknown_internals_sheet, |
| 24 | +) |
| 25 | +from navv.zeek import ( |
| 26 | + get_conn_data, |
| 27 | + get_dns_data, |
| 28 | + get_snmp_data, |
| 29 | + run_zeek, |
| 30 | + perform_zeekcut, |
| 31 | +) |
| 32 | +from navv.utilities import pushd |
| 33 | + |
| 34 | + |
| 35 | +def generate(customer_name, output_dir, pcap, zeek_logs_zip, spreadsheet): |
| 36 | + """Generate excel sheet.""" |
| 37 | + with pushd(output_dir): |
| 38 | + pass |
| 39 | + |
| 40 | + if spreadsheet and spreadsheet.filename: |
| 41 | + wb = openpyxl.load_workbook(os.path.join(output_dir, spreadsheet.filename)) |
| 42 | + else: |
| 43 | + file_name = os.path.join(output_dir, customer_name + "_network_analysis.xlsx") |
| 44 | + wb = get_workbook(file_name) |
| 45 | + |
| 46 | + # Extract Zeek logs from zip file |
| 47 | + if zeek_logs_zip: |
| 48 | + with ZipFile(f"{output_dir}/{zeek_logs_zip}", "r") as zip_file: |
| 49 | + zip_file.extractall(path=output_dir) |
| 50 | + zeek_logs = os.path.join(output_dir, zeek_logs_zip[:-4]) |
| 51 | + os.remove(os.path.join(output_dir, zeek_logs_zip)) |
| 52 | + else: |
| 53 | + zeek_logs = os.path.join(output_dir, "logs") |
| 54 | + |
| 55 | + services, conn_states = get_package_data() |
| 56 | + timer_data = dict() |
| 57 | + segments = get_segments_data(wb["Segments"]) |
| 58 | + inventory = get_inventory_data(wb["Inventory Input"]) |
| 59 | + |
| 60 | + if pcap and pcap.filename: |
| 61 | + run_zeek(os.path.join(output_dir, pcap.filename), zeek_logs, timer=timer_data) |
| 62 | + else: |
| 63 | + timer_data["run_zeek"] = "NOT RAN" |
| 64 | + |
| 65 | + # Get zeek data from conn.log, dns.log and snmp.log |
| 66 | + zeek_data = get_conn_data(zeek_logs) |
| 67 | + snmp_data = get_snmp_data(zeek_logs) |
| 68 | + dns_filtered = get_dns_data(customer_name, output_dir, zeek_logs) |
| 69 | + |
| 70 | + # Get dns data for resolution |
| 71 | + json_path = os.path.join(output_dir, f"{customer_name}_dns_data.json") |
| 72 | + |
| 73 | + # Get zeek dataframes |
| 74 | + zeek_df = get_zeek_df(zeek_data, dns_filtered) |
| 75 | + snmp_df = get_snmp_df(snmp_data) |
| 76 | + |
| 77 | + # Get inventory report dataframe |
| 78 | + inventory_df = get_inventory_report_df(zeek_df) |
| 79 | + |
| 80 | + # Turn zeekcut data into rows for spreadsheet |
| 81 | + rows = create_analysis_array(zeek_data, timer=timer_data) |
| 82 | + |
| 83 | + ext_IPs = set() |
| 84 | + unk_int_IPs = set() |
| 85 | + perform_analysis( |
| 86 | + wb, |
| 87 | + rows, |
| 88 | + services, |
| 89 | + conn_states, |
| 90 | + inventory, |
| 91 | + segments, |
| 92 | + dns_filtered, |
| 93 | + json_path, |
| 94 | + ext_IPs, |
| 95 | + unk_int_IPs, |
| 96 | + timer=timer_data, |
| 97 | + ) |
| 98 | + |
| 99 | + write_inventory_report_sheet(inventory_df, wb) |
| 100 | + |
| 101 | + write_externals_sheet(ext_IPs, wb) |
| 102 | + |
| 103 | + write_unknown_internals_sheet(unk_int_IPs, wb) |
| 104 | + |
| 105 | + write_snmp_sheet(snmp_df, wb) |
| 106 | + |
| 107 | + auto_adjust_width(wb["Analysis"]) |
| 108 | + times = ( |
| 109 | + perform_zeekcut(fields=["ts"], log_file=os.path.join(zeek_logs, "conn.log")) |
| 110 | + .decode("utf-8") |
| 111 | + .split("\n")[:-1] |
| 112 | + ) |
| 113 | + forward = sorted(times) |
| 114 | + start = float(forward[0]) |
| 115 | + end = float(forward[len(forward) - 1]) |
| 116 | + cap_time = end - start |
| 117 | + timer_data[ |
| 118 | + "Length of Capture time" |
| 119 | + ] = "{} day(s) {} hour(s) {} minutes {} seconds".format( |
| 120 | + int(cap_time / 86400), |
| 121 | + int(cap_time % 86400 / 3600), |
| 122 | + int(cap_time % 3600 / 60), |
| 123 | + int(cap_time % 60), |
| 124 | + ) |
| 125 | + write_stats_sheet(wb, timer_data) |
| 126 | + write_conn_states_sheet(conn_states, wb) |
| 127 | + |
| 128 | + memfile: io.BytesIO |
| 129 | + with NamedTemporaryFile() as tmp: |
| 130 | + wb.save(tmp.name) |
| 131 | + tmp.seek(0) |
| 132 | + memfile = io.BytesIO(tmp.read()) |
| 133 | + |
| 134 | + shutil.rmtree(output_dir) |
| 135 | + return memfile |
0 commit comments