Untitled
import psutil import json def load_azure_ip_ranges_from_json(file_path): with open(file_path, 'r') as file: data = json.load(file) azure_ip_ranges_with_info = [] for value in data['values']: if value['name'].startswith('Azure'): service_name = value['name'] for address in value['properties']['addressPrefixes']: azure_ip_ranges_with_info.append((address, service_name)) return azure_ip_ranges_with_info def is_azure_address(address, azure_ip_ranges_with_info): for azure_range, service_name in azure_ip_ranges_with_info: if ip_in_range(address, azure_range): return True, service_name return False, None def ip_in_range(ip_address, ip_range): ip, cidr = ip_range.split('/') ip = ip.split('.') ip_address = ip_address.split('.') mask = int(cidr) for i in range(mask // 8): if ip[i] != ip_address[i]: return False bits = mask % 8 if bits: ip_bin = bin(int(ip_address[mask // 8]))[2:].zfill(8) range_bin = bin(int(ip[mask // 8]))[2:].zfill(8) return ip_bin[:bits] == range_bin[:bits] return True def find_azure_network_drives(azure_ip_ranges_with_info): azure_drives_info = [] for conn in psutil.net_connections(kind='inet'): if conn.raddr: remote_address = conn.raddr[0] is_azure, service_name = is_azure_address(remote_address, azure_ip_ranges_with_info) if is_azure: try: process = psutil.Process(conn.pid) process_name = process.name() process_info = f"PID: {conn.pid}, Process Name: {process_name}" except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): process_info = "Process information could not be retrieved" azure_drives_info.append((remote_address, service_name, process_info)) return azure_drives_info if __name__ == "__main__": azure_ip_ranges_with_info = load_azure_ip_ranges_from_json('ServiceTags_Public_20240212.json') azure_network_drives_info = find_azure_network_drives(azure_ip_ranges_with_info) if azure_network_drives_info: print("Azure network drives found:") for drive_info in azure_network_drives_info: print(f"IP: {drive_info[0]}, Service: {drive_info[1]}, {drive_info[2]}") else: print("No Azure network drives found.")
Leave a Comment