Skip to content

Commit

Permalink
added sniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
pr3y committed May 8, 2024
1 parent d53a9b8 commit 0efaa13
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 1 deletion.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lib_deps =
esphome/ESPAsyncWebServer-esphome
WireGuard-ESP32
IRremoteESP8266
Time
; bodmer/TFT_eSPI@^2.5.43 ; Esta biblioteca deve estar na pasta lib, devido as alterações no drive do StickC


Expand Down
145 changes: 145 additions & 0 deletions src/PCAP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
===========================================
Copyright (c) 2017 Stefan Kremser
github.com/spacehuhn
===========================================
*/

#ifndef PCAP_h
#define PCAP_h

#include <Arduino.h>
#include "SPI.h"
#include "mykeyboard.h"
#include "sniffer.h"
#if defined(ESP32)
#include "FS.h"
//#include "SD.h"
#else
#include <SPI.h>
#include <SdFat.h>
#endif


//String filename;
File file;

/* converts a 32 bit integer into 4 bytes */
void escape32(uint32_t n, uint8_t* buf){
buf[0] = n;
buf[1] = n >> 8;
buf[2] = n >> 16;
buf[3] = n >> 24;
}

/* converts a 16 bit integer into 2 bytes */
void escape16(uint16_t n, uint8_t* buf){
buf[0] = n;
buf[1] = n >> 8;
}

/* writes a 32 bit integer onto the SD card */
void filewrite_32(uint32_t n){
uint8_t _buf[4];
escape32(n, _buf);
file.write(_buf, 4);
}

/* writes a 16 bit integer onto the SD card */
void filewrite_16(uint16_t n){
uint8_t _buf[2];
escape16(n, _buf);
file.write(_buf, 2);
}

/* writes a 32 bit integer to Serial */
void serialwrite_32(uint32_t n){
uint8_t _buf[4];
escape32(n, _buf);
Serial.write(_buf, 4);
}

/* writes a 16 bit integer to Serial */
void serialwrite_16(uint16_t n){
uint8_t _buf[2];
escape16(n, _buf);
Serial.write(_buf, 2);
}


/* write packet to file */
void newPacketSD(uint32_t ts_sec, uint32_t ts_usec, uint32_t len, uint8_t* buf){
if(file){

uint32_t orig_len = len;
uint32_t incl_len = len;
//if(incl_len > snaplen) incl_len = snaplen; /* safty check that the packet isn't too big (I ran into problems here) */

filewrite_32(ts_sec);
filewrite_32(ts_usec);
filewrite_32(incl_len);
filewrite_32(orig_len);

file.write(buf, incl_len);
}
}


/* write packet to Serial */
void newPacketSerial(uint32_t ts_sec, uint32_t ts_usec, uint32_t len, uint8_t* buf){
uint32_t orig_len = len;
uint32_t incl_len = len;
uint32_t snaplen = 2500;
#if defined(ESP32)
if(incl_len > snaplen) incl_len = snaplen; /* safty check that the packet isn't too big (I ran into problems with the ESP8266 here) */
#endif
serialwrite_32(ts_sec);
serialwrite_32(ts_usec);
serialwrite_32(incl_len);
serialwrite_32(orig_len);

Serial.write(buf, incl_len);
}

class PCAP
{

void startSerial();
#if defined(ESP32)
bool openFile(fs::FS &fs);
// bool removeFile(fs::FS &fs);
#else
bool openFile(SdFat &SD);
// bool removeFile(SdFat &SD);
#endif

void flushFile();
void closeFile();

void newPacketSerial(uint32_t ts_sec, uint32_t ts_usec, uint32_t len, uint8_t* buf);

String filename = "/raw.cap";

uint32_t magic_number = 0xa1b2c3d4;
uint16_t version_major = 2;
uint16_t version_minor = 4;
uint32_t thiszone = 0;
uint32_t sigfigs = 0;
uint32_t snaplen = 2500;
uint32_t network = 105;

private:
//File file;

void escape32(uint32_t n, uint8_t* buf);
void escape16(uint16_t n, uint8_t* buf);

void filewrite_16(uint16_t n);
void filewrite_32(uint32_t n);

void serialwrite_16(uint16_t n);
void serialwrite_32(uint32_t n);
};

#endif

3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ TFT_eSprite draw = TFT_eSprite(&tft);
#include "Wire.h"
#include "mfrc522_i2c.h"
#include "TV-B-Gone.h"
#include "sniffer.h"


#ifdef CARDPUTER
Expand Down Expand Up @@ -167,7 +168,7 @@ void loop() {
options.push_back({"Wifi Atks", [=]() { displayRedStripe("Wifi Atks"); }});
options.push_back({"TelNET", [=]() { displayRedStripe("TelNET"); }});
options.push_back({"SSH", [=]() { displayRedStripe("SSH"); }});
options.push_back({"Raw Sniffer", [=]() { displayRedStripe("Raw Sniffer"); }});
options.push_back({"Raw Sniffer", [=]() { sniffer_setup(); }});
options.push_back({"DPWO-ESP32", [=]() { dpwo_setup(); }});
options.push_back({"Evil Portal", [=]() { displayRedStripe("Evil Portal"); }});
options.push_back({"ARP Scan", [=]() { displayRedStripe("ARP Scan"); }});
Expand Down
213 changes: 213 additions & 0 deletions src/sniffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*
===========================================
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 "PCAP.h"
#include "display.h"
#include "globals.h"
#include "sd_functions.h"



//===== SETTINGS =====//
#define CHANNEL 1
#define FILENAME "raw"
#define SAVE_INTERVAL 10 //save new file every 30s
#define CHANNEL_HOPPING true //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 =====//
unsigned long lastTime = 0;
unsigned long lastChannelChange = 0;
int counter = 0;
int ch = CHANNEL;
bool fileOpen = false;

//PCAP pcap = PCAP();
PCAP pcap;
String filename = "/" + (String)FILENAME + ".pcap";

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

bool openFile(){
//String filename = "capture.cap";


uint32_t magic_number = 0xa1b2c3d4;
uint16_t version_major = 2;
uint16_t version_minor = 4;
uint32_t thiszone = 0;
uint32_t sigfigs = 0;
uint32_t snaplen = 2500;
uint32_t network = 105;

//if(SD.exists(filename.c_str())) removeFile(SD);
file = SD.open(filename, FILE_WRITE);
if(file) {

filewrite_32(magic_number);
filewrite_16(version_major);
filewrite_16(version_minor);
filewrite_32(thiszone);
filewrite_32(sigfigs);
filewrite_32(snaplen);
filewrite_32(network);
return true;
}
return false;
}


/* will be executed on every packet the ESP32 gets while beeing in promiscuous mode */
void sniffer(void *buf, wifi_promiscuous_pkt_type_t type){

if(fileOpen){
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)
newPacketSD(timestamp, microseconds, ctrl.sig_len, pkt->payload); //write packet to file

}

}

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


/* opens a new file */
void openFile2(){

//searches for the next non-existent file name
int c = 0;
while(SD.open(filename)){
filename = "/" + (String)FILENAME + "_" + (String)c + ".pcap";
c++;
}

fileOpen = openFile();

Serial.println("opened: "+filename);

//reset counter (counter for saving every X seconds)
counter = 0;
}


//===== SETUP =====//
void sniffer_setup() {
tft.fillScreen(BGCOLOR);
tft.setCursor(0, 0);
Serial.begin(115200);
//delay(2000);
Serial.println();

uint8_t cardType = SD.cardType();

if(cardType == CARD_NONE){
Serial.println("No SD card attached");
displayRedStripe("No SD card");
return;
}

Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}

int64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
openFile2();

/* 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_start() );
esp_wifi_set_promiscuous(true);
esp_wifi_set_promiscuous_rx_cb(sniffer);
wifi_second_chan_t secondCh = (wifi_second_chan_t)NULL;
esp_wifi_set_channel(ch,secondCh);

Serial.println("Sniffer started!");

displayRedStripe("Sniffer started!", TFT_WHITE, TFT_DARKGREEN );

sniffer_loop();

}

void sniffer_loop() {
for(;;) {
// if ((checkSelPress())) {
unsigned long currentTime = millis();

/* Channel Hopping */
if(CHANNEL_HOPPING){
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);
}
}

if(fileOpen && currentTime - lastTime > 1000){
file.flush(); //save file
lastTime = currentTime; //update time
counter++; //add 1 to counter
}

/* when counter > 30s interval */
if(counter > SAVE_INTERVAL){
//closeFile(); //save & close the file
file.close();
fileOpen = false; //update flag
Serial.println("==================");
Serial.println(filename + " saved!");
Serial.println("==================");
tft.setCursor(0, 20);
tft.setTextColor(TFT_WHITE, BGCOLOR);
tft.setTextSize(2);
tft.println("Saved to file in SD card, filename:");
tft.setTextSize(2);
displayRedStripe(filename, TFT_WHITE, FGCOLOR);
// tft.println(filename);
tft.setTextColor(FGCOLOR, BGCOLOR);
openFile2(); //open new file
}
// }
}
}
10 changes: 10 additions & 0 deletions src/sniffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

void sniffer_loop();

void sniffer_setup();

void openFile2();

void sniffer(void *buf, wifi_promiscuous_pkt_type_t type);

bool openFile();

0 comments on commit 0efaa13

Please sign in to comment.