Page 1 of 1

ESP32 Wi-Fi Sniffer Mode

Posted: Mon Jan 03, 2022 2:38 pm
by yesgenius
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?

Code: Untitled.c Select all



/*
===========================================
Copyright (c) 2017 Stefan Kremser
github.com/spacehuhn
===========================================
*/


/* include all necessary libraries */
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
//#include "esp_wifi_internal.h"
#include "lwip/err.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"

#include <Arduino.h>
#include "TimeLib.h"
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "PCAP.h"


//===== SETTINGS =====//
#define CHANNEL 5
#define BAUD_RATE 921600
#define CHANNEL_HOPPING false //if true it will scan on all channels
#define MAX_CHANNEL 11 //(only necessary if channelHopping is true)
#define HOP_INTERVAL 214 //in ms (only necessary if channelHopping is true)

//===== Run-Time variables =====//
PCAP pcap = PCAP();
int ch = CHANNEL;
unsigned long lastChannelChange = 0;

wifi_promiscuous_filter_t filter = {
//.filter_mask = WIFI_PROMIS_FILTER_MASK_ALL
//.filter_mask = WIFI_PROMIS_CTRL_FILTER_MASK_ALL
.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT | WIFI_PROMIS_FILTER_MASK_CTRL | WIFI_PROMIS_FILTER_MASK_DATA
};

//===== FUNCTIONS =====//

/* will be executed on every packet the ESP32 gets while beeing in promiscuous mode */
void sniffer(void *buf, wifi_promiscuous_pkt_type_t type)
{
wifi_promiscuous_pkt_t* pkt = (wifi_promiscuous_pkt_t*)buf;
wifi_pkt_rx_ctrl_t ctrl = (wifi_pkt_rx_ctrl_t)pkt->rx_ctrl;

uint32_t timestamp = now(); //current timestamp
uint32_t microseconds = (unsigned int)(micros() - millis() * 1000); //micro seconds offset (0 - 999)

pcap.newPacketSerial(timestamp, microseconds, ctrl.sig_len, pkt->payload); //send packet via Serial
}

esp_err_t event_handler(void *ctx, system_event_t *event){ return ESP_OK; }


//===== SETUP =====//
void setup()
{
/* start Serial */
Serial.begin(BAUD_RATE);
delay(2000);
Serial.println();

Serial.println("<<START>>");
pcap.startSerial();

/* setup wifi */
nvs_flash_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
//ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
//ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() );
esp_wifi_set_promiscuous(false);
esp_wifi_set_promiscuous_filter(&filter);
esp_wifi_set_promiscuous_rx_cb(sniffer);
esp_wifi_set_promiscuous(true);
wifi_second_chan_t secondCh = (wifi_second_chan_t)NULL;
esp_wifi_set_channel(ch,secondCh);
}

//===== LOOP =====//
void loop()
{
/* Channel Hopping */
if(CHANNEL_HOPPING)
{
unsigned long currentTime = millis();
if(currentTime - lastChannelChange >= HOP_INTERVAL)
{
lastChannelChange = currentTime;
ch++; //increase channel
if(ch > MAX_CHANNEL) ch = 1;
wifi_second_chan_t secondCh = (wifi_second_chan_t)NULL;
esp_wifi_set_channel(ch,secondCh);
}
}
}

Re: ESP32 Wi-Fi Sniffer Mode

Posted: Sun Feb 05, 2023 3:14 pm
by cyberman54
To get control packets, use
esp_err_t esp_wifi_set_promiscuous_ctrl_filter(const wifi_promiscuous_filter_t *filter);