KC9UZR

ESP8266 Advanced Deauth Detection

Nov 30th, 2025
105
0
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.62 KB | Cybersecurity | 0 0
  1. /*
  2.  * PROJECT: WATCHTOWER v10 (STABLE)
  3.  * SUBJECT: ASYNC LOGGING & STICKY LOCK
  4.  * FIX: Prevents Serial Overflow crashes during high-volume attacks.
  5.  */
  6.  
  7. #include <ESP8266WiFi.h>
  8.  
  9. // --- Configuration ---
  10. #define LED_PIN 2          
  11. #define SERIAL_BAUD 115200
  12. #define LOCK_TIMEOUT 5000   // Stay on channel for 5s after last packet
  13.  
  14. // --- Globals ---
  15. unsigned long lastHopTime = 0;
  16. unsigned long lockTimer = 0;
  17. unsigned long lastLedToggle = 0;
  18. int currentChannel = 1;
  19.  
  20. // --- State Flags ---
  21. volatile bool hasNewThreat = false;  // Flag to tell Loop to print
  22. volatile int threatRSSI = 0;
  23. volatile int threatChannel = 0;
  24. volatile bool isDeauth = false;
  25. volatile bool isBeacon = false;
  26. String threatSrc = ""; // String isn't volatile-safe usually, but we manage via flag
  27. String threatDst = "";
  28. String threatSSID = "";
  29.  
  30. // --- Hardware Structs ---
  31. struct RxControl {
  32.     signed char rssi;
  33.     unsigned char rate;
  34.     unsigned int legacy_length:12;
  35.     unsigned int damatch0:1;
  36.     unsigned int damatch1:1;
  37.     unsigned int bssidmatch0:1;
  38.     unsigned int bssidmatch1:1;
  39.     unsigned int MCS:7;
  40.     unsigned int CWB:1;
  41.     unsigned int HT_length:16;
  42.     unsigned int Smoothing:1;
  43.     unsigned int Not_Sounding:1;
  44.     unsigned int Aggregation:1;
  45.     unsigned int STBC:2;
  46.     unsigned int FEC_CODING:1;
  47.     unsigned int SGI:1;
  48.     unsigned int rxend_state:8;
  49.     unsigned int ampdu_cnt:8;
  50.     unsigned int channel:4;
  51.     unsigned int :12;
  52. };
  53.  
  54. struct SnifferPacket {
  55.     struct RxControl rx_ctrl;
  56.     uint8_t data[128];
  57.     uint16_t cnt;
  58.     uint16_t len;
  59. };
  60.  
  61. // Helper to format MAC safely
  62. String formatMac(uint8_t* mac) {
  63.   char buf[18];
  64.   snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
  65.            mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  66.   return String(buf);
  67. }
  68.  
  69. // --- Sniffer Callback (INTERRUPT CONTEXT - KEEP FAST) ---
  70. void sniffer(uint8_t *buf, uint16_t len) {
  71.   struct SnifferPacket *snifferPacket = (struct SnifferPacket*) buf;
  72.  
  73.   uint8_t frame_control = snifferPacket->data[0];
  74.  
  75.   // Filter: Deauth (0xC0), Disassoc (0xA0), Beacon (0x80)
  76.   bool d = (frame_control == 0xC0 || frame_control == 0xA0);
  77.   bool b = (frame_control == 0x80);
  78.  
  79.   if (!d && !b) return; // Ignore everything else
  80.  
  81.   // RSSI Filter (Ignore weak noise)
  82.   int rssi = snifferPacket->rx_ctrl.rssi;
  83.   if (rssi < -85) return;
  84.  
  85.   // --- LOGIC: Only process Beacons if they look like SPAM (optional check) ---
  86.   // For now, we capture them to show output as requested.
  87.  
  88.   // Update Global Shared Variables (If loop has finished printing previous)
  89.   if (!hasNewThreat) {
  90.     threatRSSI = rssi;
  91.     threatChannel = currentChannel; // Use global current channel
  92.     isDeauth = d;
  93.     isBeacon = b;
  94.    
  95.     // Extract MACs
  96.     // 802.11 Header: [FC][Dur][Dst][Src]
  97.     threatDst = formatMac(&snifferPacket->data[4]);
  98.     threatSrc = formatMac(&snifferPacket->data[10]);
  99.  
  100.     // Extract SSID if Beacon
  101.     if (b && len > 38) {
  102.        int pos = 36;
  103.        uint8_t tag_id = snifferPacket->data[pos];
  104.        uint8_t tag_len = snifferPacket->data[pos+1];
  105.        if (tag_id == 0 && pos + tag_len + 2 < len) {
  106.           if (tag_len > 32) tag_len = 32;
  107.           char ssidBuf[33];
  108.           memset(ssidBuf, 0, 33);
  109.           memcpy(ssidBuf, &snifferPacket->data[pos+2], tag_len);
  110.           threatSSID = String(ssidBuf);
  111.        } else {
  112.          threatSSID = "";
  113.        }
  114.     } else {
  115.       threatSSID = "";
  116.     }
  117.  
  118.     // Set Flag
  119.     hasNewThreat = true;
  120.   }
  121. }
  122.  
  123. void setup() {
  124.   Serial.begin(SERIAL_BAUD);
  125.   pinMode(LED_PIN, OUTPUT);
  126.   digitalWrite(LED_PIN, HIGH);
  127.  
  128.   WiFi.mode(WIFI_STA);
  129.   WiFi.disconnect();
  130.  
  131.   Serial.println("\n--- WATCHTOWER v10 STABLE ---");
  132.   Serial.println("Mode: Async Logging (No Crashes)");
  133.   Serial.println("Lock: 5 Seconds per detection");
  134.  
  135.   wifi_set_opmode(STATION_MODE);
  136.   wifi_set_promiscuous_rx_cb(sniffer);
  137.   wifi_promiscuous_enable(1);
  138. }
  139.  
  140. void loop() {
  141.   unsigned long currentMillis = millis();
  142.  
  143.   // --- 1. PRINTING ENGINE (Main Loop Context) ---
  144.   if (hasNewThreat) {
  145.     // Extend Lock Timer
  146.     lockTimer = currentMillis + LOCK_TIMEOUT;
  147.  
  148.     // Visual Strobe (Toggle LED)
  149.     digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  150.  
  151.     // Print Data
  152.     if (isDeauth) {
  153.       Serial.print(">>> DEAUTH | CH:");
  154.       Serial.print(threatChannel);
  155.       Serial.print(" | RSSI:");
  156.       Serial.print(threatRSSI);
  157.       Serial.print(" | SRC: ");
  158.       Serial.println(threatSrc);
  159.     }
  160.     else if (isBeacon) {
  161.       // Only print if it looks suspicious (simple noise filter)
  162.       // or print all if you want to see the spam flow
  163.       Serial.print("--- BEACON | CH:");
  164.       Serial.print(threatChannel);
  165.       Serial.print(" | RSSI:");
  166.       Serial.print(threatRSSI);
  167.       Serial.print(" | SSID: ");
  168.       Serial.println(threatSSID);
  169.     }
  170.  
  171.     // Clear Flag (Ready for next packet)
  172.     hasNewThreat = false;
  173.   }
  174.  
  175.   // --- 2. LED MANAGEMENT ---
  176.   // If locked, we rely on the packet arrival to toggle the LED (above).
  177.   // If idle (no packets for > 5s), we do the slow heartbeat.
  178.   if (currentMillis > lockTimer) {
  179.      // Idle Mode
  180.      if (currentMillis - lastLedToggle > 2000) {
  181.        lastLedToggle = currentMillis;
  182.        digitalWrite(LED_PIN, LOW); // Blink
  183.        delay(20);
  184.        digitalWrite(LED_PIN, HIGH);
  185.      }
  186.   }
  187.  
  188.   // --- 3. CHANNEL HOPPING ---
  189.   // CRITICAL: Do NOT hop if locked
  190.   if (currentMillis > lockTimer) {
  191.     if (currentMillis - lastHopTime > 200) {
  192.       lastHopTime = currentMillis;
  193.       currentChannel++;
  194.       if (currentChannel > 14) currentChannel = 1;
  195.       wifi_set_channel(currentChannel);
  196.     }
  197.   }
  198. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment