Facebook
From asd, 4 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 215
  1. def capture_packets(interface, num_packets, output_file):
  2.     try:
  3.         set_monitor_mode(interface)
  4.         capture = pyshark.LiveCapture(interface=interface)
  5.  
  6.         with open(output_file, 'w', newline='') as csvfile:
  7.             fieldnames = ['frame.len', 'radiotap.length', 'radiotap.dbm_antsignal', 'wlan.duration',
  8.                           'radiotap.channel.freq', 'radiotap.channel.flags.cck',
  9.                           'radiotap.channel.flags.ofdm', 'wlan.fc.type', 'wlan.fc.subtype', 'wlan.fc.ds',
  10.                           'wlan.fc.frag', 'wlan.fc.retry', 'wlan.fc.pwrmgt', 'wlan.fc.moredata', 'wlan.fc.protected']
  11.             writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  12.             writer.writeheader()
  13.  
  14.             for packet in capture.sniff_continuously(packet_count=num_packets):
  15.                 data = extract_packet_data(packet)
  16.                 writer.writerow(data)
  17.  
  18.     except KeyboardInterrupt:
  19.         print("Paket yakalama işlemi durduruldu.")
  20.     except Exception as e:
  21.         print(f"Bir hata oluştu: {e}")
  22.     finally:
  23.         unset_monitor_mode(interface)
  24.         capture.close()
  25.  
  26. def extract_packet_data(packet):
  27.     features = {
  28.         'frame.len': packet.frame_info.len,
  29.         'radiotap.length': getattr(packet.radiotap, 'length', 'None') if 'radiotap' in packet else 'None',
  30.         'radiotap.dbm_antsignal': getattr(packet.radiotap, 'dbm_antsignal', 'None') if 'radiotap' in packet else 'None',
  31.         'wlan.duration': getattr(packet.wlan, 'duration', 'None') if 'wlan' in packet else 'None',
  32.         # 'tsft': getattr(packet.radiotap, 'tsft', 'None') if 'radiotap' in packet else 'None',
  33.         'radiotap.channel.freq': getattr(packet.radiotap, 'channel_freq', 'None') if 'radiotap' in packet else 'None',
  34.         'radiotap.channel.flags.cck': 1 if hasattr(packet.radiotap.present.fields, 'cck') else 0,
  35.         'radiotap.channel.flags.ofdm': 1 if hasattr(packet.radiotap.present.fields, 'ofdm') else 0,
  36.         'wlan.fc.type': getattr(packet.wlan, 'fc_type', 'None') if 'wlan' in packet else 'None',
  37.         'wlan.fc.subtype': getattr(packet.wlan, 'fc_subtype', 'None') if 'wlan' in packet else 'None',
  38.         'wlan.fc.ds': format_ds_value(packet.wlan.fc_ds) if 'wlan' in packet and hasattr(packet.wlan, 'fc_ds') else 'None',
  39.         'wlan.fc.frag': getattr(packet.wlan, 'fc_frag', 'None') if 'wlan' in packet else 'None',
  40.         'wlan.fc.retry': getattr(packet.wlan, 'fc_retry', 'None') if 'wlan' in packet else 'None',
  41.         'wlan.fc.pwrmgt': getattr(packet.wlan, 'fc_pwrmgt', 'None') if 'wlan' in packet else 'None',
  42.         'wlan.fc.moredata': getattr(packet.wlan, 'fc_more_data', '0') if 'wlan' in packet else '0',
  43.         'wlan.fc.protected': getattr(packet.wlan, 'fc_protected', 'None') if 'wlan' in packet else 'None',
  44.     }
  45.     return features