ESP32 Wi-Fi Sniffer Mode

yesgenius
Posts: 1
Joined: Mon Jan 03, 2022 2:04 pm

ESP32 Wi-Fi Sniffer Mode

Postby yesgenius » Mon Jan 03, 2022 2:38 pm

Colleagues, hello!
As a beginner, I ask for your support :)

According to the documentation, ESP32 can receive 3 types of frames: Control, Management, Data.

My program receives frames: Management, Data and does not receives Control.

What am I doing wrong?

  1.  
  2. /*
  3.   ===========================================
  4.        Copyright (c) 2017 Stefan Kremser
  5.               github.com/spacehuhn
  6.   ===========================================
  7. */
  8.  
  9.  
  10. /* include all necessary libraries */
  11. #include "freertos/FreeRTOS.h"
  12. #include "esp_wifi.h"
  13. //#include "esp_wifi_internal.h"
  14. #include "lwip/err.h"
  15. #include "esp_system.h"
  16. #include "esp_event.h"
  17. #include "esp_event_loop.h"
  18. #include "nvs_flash.h"
  19. #include "driver/gpio.h"
  20.  
  21. #include <Arduino.h>
  22. #include "TimeLib.h"
  23. #include "FS.h"
  24. #include "SD.h"
  25. #include "SPI.h"
  26. #include "PCAP.h"
  27.  
  28.  
  29. //===== SETTINGS =====//
  30. #define CHANNEL           5
  31. #define BAUD_RATE         921600
  32. #define CHANNEL_HOPPING   false     //if true it will scan on all channels
  33. #define MAX_CHANNEL       11        //(only necessary if channelHopping is true)
  34. #define HOP_INTERVAL      214       //in ms (only necessary if channelHopping is true)
  35.  
  36. //===== Run-Time variables =====//
  37. PCAP pcap = PCAP();
  38. int ch = CHANNEL;
  39. unsigned long lastChannelChange = 0;
  40.  
  41. wifi_promiscuous_filter_t filter = {
  42.   //.filter_mask = WIFI_PROMIS_FILTER_MASK_ALL
  43.   //.filter_mask = WIFI_PROMIS_CTRL_FILTER_MASK_ALL
  44.   .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT | WIFI_PROMIS_FILTER_MASK_CTRL | WIFI_PROMIS_FILTER_MASK_DATA
  45. };
  46.  
  47. //===== FUNCTIONS =====//
  48.  
  49. /* will be executed on every packet the ESP32 gets while beeing in promiscuous mode */
  50. void sniffer(void *buf, wifi_promiscuous_pkt_type_t type)
  51. {
  52.   wifi_promiscuous_pkt_t* pkt = (wifi_promiscuous_pkt_t*)buf;
  53.   wifi_pkt_rx_ctrl_t ctrl = (wifi_pkt_rx_ctrl_t)pkt->rx_ctrl;
  54.  
  55.   uint32_t timestamp = now(); //current timestamp
  56.   uint32_t microseconds = (unsigned int)(micros() - millis() * 1000); //micro seconds offset (0 - 999)
  57.  
  58.   pcap.newPacketSerial(timestamp, microseconds, ctrl.sig_len, pkt->payload); //send packet via Serial  
  59. }
  60.  
  61. esp_err_t event_handler(void *ctx, system_event_t *event){ return ESP_OK; }
  62.  
  63.  
  64. //===== SETUP =====//
  65. void setup()
  66. {
  67.   /* start Serial */
  68.   Serial.begin(BAUD_RATE);
  69.   delay(2000);
  70.   Serial.println();
  71.  
  72.   Serial.println("<<START>>");
  73.   pcap.startSerial();
  74.  
  75.   /* setup wifi */
  76.   nvs_flash_init();
  77.   tcpip_adapter_init();
  78.   ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
  79.   wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  80.   ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  81.   ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  82.   //ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );  
  83.   ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );  
  84.   //ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );  
  85.   ESP_ERROR_CHECK( esp_wifi_start() );
  86.   esp_wifi_set_promiscuous(false);
  87.   esp_wifi_set_promiscuous_filter(&filter);
  88.   esp_wifi_set_promiscuous_rx_cb(sniffer);
  89.   esp_wifi_set_promiscuous(true);
  90.   wifi_second_chan_t secondCh = (wifi_second_chan_t)NULL;
  91.   esp_wifi_set_channel(ch,secondCh);
  92. }
  93.  
  94. //===== LOOP =====//
  95. void loop()
  96. {
  97.     /* Channel Hopping */
  98.   if(CHANNEL_HOPPING)
  99.   {
  100.     unsigned long currentTime = millis();
  101.     if(currentTime - lastChannelChange >= HOP_INTERVAL)
  102.     {
  103.       lastChannelChange = currentTime;
  104.       ch++; //increase channel
  105.       if(ch > MAX_CHANNEL) ch = 1;
  106.       wifi_second_chan_t secondCh = (wifi_second_chan_t)NULL;
  107.       esp_wifi_set_channel(ch,secondCh);
  108.     }
  109.   }
  110. }
  111.  

cyberman54
Posts: 27
Joined: Sun Jan 14, 2018 7:47 pm

Re: ESP32 Wi-Fi Sniffer Mode

Postby cyberman54 » Sun Feb 05, 2023 3:14 pm

To get control packets, use
esp_err_t esp_wifi_set_promiscuous_ctrl_filter(const wifi_promiscuous_filter_t *filter);

Who is online

Users browsing this forum: No registered users and 138 guests