- def capture_packets(interface, num_packets, output_file):
- try:
- set_monitor_mode(interface)
- capture = pyshark.LiveCapture(interface=interface)
- with open(output_file, 'w', newline='') as csvfile:
- fieldnames = ['frame.len', 'radiotap.length', 'radiotap.dbm_antsignal', 'wlan.duration',
- 'radiotap.channel.freq', 'radiotap.channel.flags.cck',
- 'radiotap.channel.flags.ofdm', 'wlan.fc.type', 'wlan.fc.subtype', 'wlan.fc.ds',
- 'wlan.fc.frag', 'wlan.fc.retry', 'wlan.fc.pwrmgt', 'wlan.fc.moredata', 'wlan.fc.protected']
- writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
- writer.writeheader()
- for packet in capture.sniff_continuously(packet_count=num_packets):
- data = extract_packet_data(packet)
- writer.writerow(data)
- except KeyboardInterrupt:
- print("Paket yakalama işlemi durduruldu.")
- except Exception as e:
- print(f"Bir hata oluştu: {e}")
- finally:
- unset_monitor_mode(interface)
- capture.close()
- def extract_packet_data(packet):
- features = {
- 'frame.len': packet.frame_info.len,
- 'radiotap.length': getattr(packet.radiotap, 'length', 'None') if 'radiotap' in packet else 'None',
- 'radiotap.dbm_antsignal': getattr(packet.radiotap, 'dbm_antsignal', 'None') if 'radiotap' in packet else 'None',
- 'wlan.duration': getattr(packet.wlan, 'duration', 'None') if 'wlan' in packet else 'None',
- # 'tsft': getattr(packet.radiotap, 'tsft', 'None') if 'radiotap' in packet else 'None',
- 'radiotap.channel.freq': getattr(packet.radiotap, 'channel_freq', 'None') if 'radiotap' in packet else 'None',
- 'radiotap.channel.flags.cck': 1 if hasattr(packet.radiotap.present.fields, 'cck') else 0,
- 'radiotap.channel.flags.ofdm': 1 if hasattr(packet.radiotap.present.fields, 'ofdm') else 0,
- 'wlan.fc.type': getattr(packet.wlan, 'fc_type', 'None') if 'wlan' in packet else 'None',
- 'wlan.fc.subtype': getattr(packet.wlan, 'fc_subtype', 'None') if 'wlan' in packet else 'None',
- 'wlan.fc.ds': format_ds_value(packet.wlan.fc_ds) if 'wlan' in packet and hasattr(packet.wlan, 'fc_ds') else 'None',
- 'wlan.fc.frag': getattr(packet.wlan, 'fc_frag', 'None') if 'wlan' in packet else 'None',
- 'wlan.fc.retry': getattr(packet.wlan, 'fc_retry', 'None') if 'wlan' in packet else 'None',
- 'wlan.fc.pwrmgt': getattr(packet.wlan, 'fc_pwrmgt', 'None') if 'wlan' in packet else 'None',
- 'wlan.fc.moredata': getattr(packet.wlan, 'fc_more_data', '0') if 'wlan' in packet else '0',
- 'wlan.fc.protected': getattr(packet.wlan, 'fc_protected', 'None') if 'wlan' in packet else 'None',
- }
- return features