Skip to content

Commit

Permalink
Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mads256h committed Sep 28, 2023
1 parent bb0274f commit 147df8f
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 68 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(wifi-scanner)
project(wifi-sniffer)
36 changes: 36 additions & 0 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
menu "Sniffer Configuration"
config BLINK_GPIO_NUM
int "The GPIO number that is blinked"
range 0 33
default 2
help
"No help :)"

config BLINK_SPEED
int "The time between blinks in milliseconds"
range 10 100
default 25
help
"No help :)"

config PACKET_QUEUE_SIZE
int "The size of the packet queue"
range 8 128
default 32
help
"The size of the packet queue. Too small of a value may drop some packets."

config PACKET_QUEUE_TIMEOUT
int "The timeout in ms when inserting into the queue"
range 1 100
default 10
help
"The timeout in milliseconds of trying to insert a packet into the queue. A too small or too big value may drop packets."

config CHANNEL_SWITCHER_FREQUENCY
int "The time in milliseconds between channel switches"
range 10 1000
default 100
help
"No help :)"
endmenu
113 changes: 95 additions & 18 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,116 @@
#include "wifi.h"

#include "sdkconfig.h"
#include <driver/gpio.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_wifi.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <freertos/task.h>
#include <inttypes.h>
#include <string.h>

void packet_callback(void *buf, wifi_promiscuous_pkt_type_t type) {
constexpr static auto blink_port = (gpio_num_t)CONFIG_BLINK_GPIO_NUM;
constexpr static auto blink_speed = CONFIG_BLINK_SPEED; // ms

constexpr static auto sniffer_tag = "sniffer";

constexpr static auto queue_size = CONFIG_PACKET_QUEUE_SIZE;
constexpr static auto queue_timeout = CONFIG_PACKET_QUEUE_TIMEOUT; // ms

constexpr static auto channel_switcher_frequency =
CONFIG_CHANNEL_SWITCHER_FREQUENCY; // ms

static TaskHandle_t blinker_task;
static QueueHandle_t packet_queue;

struct queue_item {
wifi_pkt_rx_ctrl_t rx_ctrl;
void *payload;
};

extern "C" void app_main(void) {
packet_queue = xQueueCreate(queue_size, sizeof(queue_item));

setup_blinker();

setup_wifi();

while (true) {
queue_item item;

// Block forever until we receive a packet from the queue.
if (xQueueReceive(packet_queue, &item, portMAX_DELAY) != pdTRUE) {
// Keep trying :)
continue;
}

auto *frame = (mac_frame *)item.payload;
print_frame_control_type(frame->frame_control);

printf("rssi: %d\n"
"channel: %u\n",
item.rx_ctrl.rssi, item.rx_ctrl.channel);

printf("receiver: ");
print_mac_address(frame->receiver_address);

printf("transmitter: ");
print_mac_address(frame->transmitter_address);

printf("\n\n\n");

free(item.payload);
}
}

static void packet_callback(void *buf, wifi_promiscuous_pkt_type_t type) {
auto *pkt = (wifi_promiscuous_pkt_t *)buf;
auto *rx_ctrl = &pkt->rx_ctrl;
auto *frame = (mac_frame *)pkt->payload;
print_frame_control_type(frame->frame_control);

printf("rssi: %d\n"
"channel: %u\n"
"sig_len: %u\n",
rx_ctrl->rssi, rx_ctrl->channel, rx_ctrl->sig_len);
auto *new_payload = malloc(rx_ctrl->sig_len);
memcpy(new_payload, pkt->payload, rx_ctrl->sig_len);

printf("duration: %u\n", frame->duration);
queue_item item{pkt->rx_ctrl, new_payload};

printf("receiver: ");
print_mac_address(frame->receiver_address);
xTaskNotifyGive(blinker_task);

printf("transmitter: ");
print_mac_address(frame->transmitter_address);
if (xQueueSend(packet_queue, &item, pdMS_TO_TICKS(queue_timeout)) != pdTRUE) {
ESP_LOGE(sniffer_tag, "queue full");
free(new_payload);
}
}

static void task_blinker(void *parameters) {
while (true) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
gpio_set_level(blink_port, 1);
// new_packet = 0;
vTaskDelay(blink_speed / portTICK_PERIOD_MS);
gpio_set_level(blink_port, 0);
vTaskDelay(blink_speed / portTICK_PERIOD_MS);
}
}

printf("destination: ");
print_mac_address(frame->destination_address);
static void task_channel_hopper(void *parameters) {
auto channel = 0;
while (true) {
esp_wifi_set_channel(channel + 1, WIFI_SECOND_CHAN_NONE);
channel = (channel + 1) % 12;
vTaskDelay(channel_switcher_frequency / portTICK_PERIOD_MS);
}
}

printf("source: ");
print_mac_address(frame->source_address);
static void setup_blinker() {
gpio_reset_pin(blink_port);
gpio_set_direction(blink_port, GPIO_MODE_OUTPUT);

printf("\n\n\n");
xTaskCreate(task_blinker, "blinker", 2048, (void *)nullptr, tskIDLE_PRIORITY,
&blinker_task);
}

extern "C" void app_main(void) {
static void setup_wifi() {
esp_netif_init();
esp_event_loop_create_default();
auto *sta_netif = esp_netif_create_default_wifi_sta();
Expand All @@ -50,4 +124,7 @@ extern "C" void app_main(void) {

esp_wifi_set_promiscuous(true);
esp_wifi_set_promiscuous_rx_cb(packet_callback);

xTaskCreate(task_channel_hopper, "channel hopper", 2048, (void *)nullptr,
tskIDLE_PRIORITY, NULL);
}
12 changes: 12 additions & 0 deletions main/main.h
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
#pragma once

#include <esp_wifi.h>

static void packet_callback(void *buf, wifi_promiscuous_pkt_type_t type);

static void task_blinker(void *parameters);

static void task_channel_hopper(void *parameters);

static void setup_blinker();

static void setup_wifi();
73 changes: 24 additions & 49 deletions sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,22 @@ CONFIG_PARTITION_TABLE_OFFSET=0x8000
CONFIG_PARTITION_TABLE_MD5=y
# end of Partition Table

#
# Sniffer Configuration
#
CONFIG_BLINK_GPIO_NUM=2
CONFIG_BLINK_SPEED=25
CONFIG_PACKET_QUEUE_SIZE=32
CONFIG_PACKET_QUEUE_TIMEOUT=10
CONFIG_CHANNEL_SWITCHER_FREQUENCY=100
# end of Sniffer Configuration

#
# Compiler options
#
CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y
# CONFIG_COMPILER_OPTIMIZATION_DEFAULT is not set
# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set
# CONFIG_COMPILER_OPTIMIZATION_PERF is not set
CONFIG_COMPILER_OPTIMIZATION_PERF=y
# CONFIG_COMPILER_OPTIMIZATION_NONE is not set
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set
Expand All @@ -358,7 +368,7 @@ CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y
# CONFIG_COMPILER_STACK_CHECK_MODE_NORM is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_STRONG is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_ALL is not set
# CONFIG_COMPILER_WARN_WRITE_STRINGS is not set
CONFIG_COMPILER_WARN_WRITE_STRINGS=y
# CONFIG_COMPILER_DISABLE_GCC12_WARNINGS is not set
# CONFIG_COMPILER_DUMP_RTL_FILES is not set
# end of Compiler options
Expand Down Expand Up @@ -677,21 +687,9 @@ CONFIG_ESP_ERR_TO_NAME_LOOKUP=y
#
# Ethernet
#
CONFIG_ETH_ENABLED=y
CONFIG_ETH_USE_ESP32_EMAC=y
CONFIG_ETH_PHY_INTERFACE_RMII=y
CONFIG_ETH_RMII_CLK_INPUT=y
# CONFIG_ETH_RMII_CLK_OUTPUT is not set
CONFIG_ETH_RMII_CLK_IN_GPIO=0
CONFIG_ETH_DMA_BUFFER_SIZE=512
CONFIG_ETH_DMA_RX_BUFFER_NUM=10
CONFIG_ETH_DMA_TX_BUFFER_NUM=10
CONFIG_ETH_USE_SPI_ETHERNET=y
# CONFIG_ETH_SPI_ETHERNET_DM9051 is not set
# CONFIG_ETH_SPI_ETHERNET_W5500 is not set
# CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL is not set
# CONFIG_ETH_USE_ESP32_EMAC is not set
# CONFIG_ETH_USE_SPI_ETHERNET is not set
# CONFIG_ETH_USE_OPENETH is not set
# CONFIG_ETH_TRANSMIT_MUTEX is not set
# end of Ethernet

#
Expand All @@ -710,7 +708,7 @@ CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=y
#
# ESP HTTP client
#
CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
# CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS is not set
# CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH is not set
# CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH is not set
# end of ESP HTTP client
Expand Down Expand Up @@ -1014,14 +1012,12 @@ CONFIG_ESP_WIFI_SOFTAP_BEACON_MAX_LEN=752
CONFIG_ESP_WIFI_MGMT_SBUF_NUM=32
CONFIG_ESP_WIFI_IRAM_OPT=y
CONFIG_ESP_WIFI_RX_IRAM_OPT=y
CONFIG_ESP_WIFI_ENABLE_WPA3_SAE=y
CONFIG_ESP_WIFI_ENABLE_SAE_PK=y
CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT=y
# CONFIG_ESP_WIFI_ENABLE_WPA3_SAE is not set
CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA=y
# CONFIG_ESP_WIFI_SLP_IRAM_OPT is not set
CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE=y
# CONFIG_ESP_WIFI_GMAC_SUPPORT is not set
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y
# CONFIG_ESP_WIFI_SOFTAP_SUPPORT is not set
# CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT is not set
CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM=7
# CONFIG_ESP_WIFI_NAN_ENABLE is not set
Expand All @@ -1033,7 +1029,6 @@ CONFIG_ESP_WIFI_MBEDTLS_TLS_CLIENT=y
# CONFIG_ESP_WIFI_MBO_SUPPORT is not set
# CONFIG_ESP_WIFI_DPP_SUPPORT is not set
# CONFIG_ESP_WIFI_11R_SUPPORT is not set
# CONFIG_ESP_WIFI_WPS_SOFTAP_REGISTRAR is not set

#
# WPS Configuration Options
Expand Down Expand Up @@ -1124,7 +1119,6 @@ CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1
#
# Port
#
CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set
CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y
# CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set
Expand Down Expand Up @@ -1482,8 +1476,6 @@ CONFIG_MBEDTLS_ECP_NIST_OPTIM=y
CONFIG_MQTT_PROTOCOL_311=y
# CONFIG_MQTT_PROTOCOL_5 is not set
CONFIG_MQTT_TRANSPORT_SSL=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y
# CONFIG_MQTT_MSG_ID_INCREMENTAL is not set
# CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set
# CONFIG_MQTT_REPORT_DELETED_MESSAGES is not set
Expand Down Expand Up @@ -1648,9 +1640,7 @@ CONFIG_SPIFFS_USE_MTIME=y
#
# Websocket
#
CONFIG_WS_TRANSPORT=y
CONFIG_WS_BUFFER_SIZE=1024
# CONFIG_WS_DYNAMIC_BUFFER is not set
# CONFIG_WS_TRANSPORT is not set
# end of Websocket
# end of TCP Transport

Expand Down Expand Up @@ -1680,18 +1670,7 @@ CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y
#
# Virtual file system
#
CONFIG_VFS_SUPPORT_IO=y
CONFIG_VFS_SUPPORT_DIR=y
CONFIG_VFS_SUPPORT_SELECT=y
CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT=y
CONFIG_VFS_SUPPORT_TERMIOS=y
CONFIG_VFS_MAX_COUNT=8

#
# Host File System I/O (Semihosting)
#
CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS=1
# end of Host File System I/O (Semihosting)
# CONFIG_VFS_SUPPORT_IO is not set
# end of Virtual file system

#
Expand Down Expand Up @@ -1738,8 +1717,8 @@ CONFIG_LOG_BOOTLOADER_LEVEL=3
CONFIG_FLASHMODE_DIO=y
# CONFIG_FLASHMODE_DOUT is not set
CONFIG_MONITOR_BAUD=115200
CONFIG_OPTIMIZATION_LEVEL_DEBUG=y
CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y
# CONFIG_OPTIMIZATION_LEVEL_DEBUG is not set
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG is not set
# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
Expand All @@ -1751,7 +1730,7 @@ CONFIG_STACK_CHECK_NONE=y
# CONFIG_STACK_CHECK_NORM is not set
# CONFIG_STACK_CHECK_STRONG is not set
# CONFIG_STACK_CHECK_ALL is not set
# CONFIG_WARN_WRITE_STRINGS is not set
CONFIG_WARN_WRITE_STRINGS=y
# CONFIG_ESP32_APPTRACE_DEST_TRAX is not set
CONFIG_ESP32_APPTRACE_DEST_NONE=y
CONFIG_ESP32_APPTRACE_LOCK_ENABLE=y
Expand Down Expand Up @@ -1925,7 +1904,7 @@ CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN=752
CONFIG_ESP32_WIFI_MGMT_SBUF_NUM=32
CONFIG_ESP32_WIFI_IRAM_OPT=y
CONFIG_ESP32_WIFI_RX_IRAM_OPT=y
CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y
# CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE is not set
CONFIG_ESP32_WIFI_ENABLE_WPA3_OWE_STA=y
CONFIG_WPA_MBEDTLS_CRYPTO=y
CONFIG_WPA_MBEDTLS_TLS_CLIENT=y
Expand All @@ -1935,7 +1914,6 @@ CONFIG_WPA_MBEDTLS_TLS_CLIENT=y
# CONFIG_WPA_MBO_SUPPORT is not set
# CONFIG_WPA_DPP_SUPPORT is not set
# CONFIG_WPA_11R_SUPPORT is not set
# CONFIG_WPA_WPS_SOFTAP_REGISTRAR is not set
# CONFIG_WPA_WPS_STRICT is not set
# CONFIG_WPA_DEBUG_PRINT is not set
# CONFIG_WPA_TESTING_OPTIONS is not set
Expand Down Expand Up @@ -1987,7 +1965,4 @@ CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ABORTS=y
# CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_FAILS is not set
# CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ALLOWED is not set
# CONFIG_ESP32_ULP_COPROC_ENABLED is not set
CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y
CONFIG_SUPPORT_TERMIOS=y
CONFIG_SEMIHOSTFS_MAX_MOUNT_POINTS=1
# End of deprecated options

0 comments on commit 147df8f

Please sign in to comment.