|
34 | 34 | else: |
35 | 35 | sys.exit("This script requires *nix.") |
36 | 36 |
|
37 | | -unresolvable_ips = [] # List to keep track of unresolvable IP addresses |
| 37 | +unresolvable_ips = set() # keep track of unresolvable IP addresses |
38 | 38 |
|
39 | 39 | def main(): |
40 | 40 | # Parse the command line arguments |
@@ -108,22 +108,22 @@ def update_ip_hostname_mappings(df, ip_shelve): |
108 | 108 | for _, row in sni_df.iterrows(): |
109 | 109 | ip_shelve[row['ip.dst']] = row['tls.handshake.extensions_server_name'] |
110 | 110 |
|
111 | | - df['src_hostname'] = df['ip.src'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x)) if x else '')) |
112 | | - df['dst_hostname'] = df['ip.dst'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x)) if x else '')) |
| 111 | + df['src_hostname'] = df['ip.src'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x))) if pd.notna(x) else '') |
| 112 | + df['dst_hostname'] = df['ip.dst'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x))) if pd.notna(x) else '') |
113 | 113 | df.drop(['dns.qry.name', 'dns.a', 'tls.handshake.extensions_server_name'], axis=1, inplace=True) |
114 | 114 |
|
115 | 115 | def reverse_dns(ip_address): |
116 | 116 | """ |
117 | 117 | Attempts to resolve an IP address to a hostname using a reverse DNS lookup; |
118 | 118 | This function is used as a fallback mechanism in the event that an IP address does not have a corresponding hostname entry in the shelve database. |
119 | 119 | """ |
120 | | - if not ip_address or not isinstance(ip_address, str): |
| 120 | + if not ip_address or not isinstance(ip_address, str) or ip_address.lower() == 'nan': |
121 | 121 | return '' |
122 | 122 | try: |
123 | 123 | hostname = socket.gethostbyaddr(ip_address)[0] |
124 | 124 | return hostname |
125 | 125 | except (socket.herror, socket.gaierror): |
126 | | - unresolvable_ips.append(ip_address) |
| 126 | + unresolvable_ips.add(ip_address) |
127 | 127 | return '' |
128 | 128 |
|
129 | 129 | if __name__ == "__main__": |
|
0 commit comments