Advertisement
pleasedontcode

"MAC Capture" rev_01

Dec 11th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "MAC Capture"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-11 15:01:30
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I want my esp32 to detect mac addresses of other */
  21.     /* devices and I want the mac addresses that I */
  22.     /* indicate to be detected and recorded as PRESENT in */
  23.     /* the serial monitor. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <WiFi.h> // Include the WiFi library for ESP32
  30. #include <set>    // Include the set library for unique MAC storage
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. // Define the promiscuous mode filter
  37. wifi_promiscuous_filter_t filter = {
  38.     .filter_mask = WIFI_PROMIS_FILTER_MASK_DATA | WIFI_PROMIS_FILTER_MASK_MGMT,
  39. }; // This filter is used to capture data and management frames
  40.  
  41. // Set to store unique MAC addresses
  42. std::set<String> uniqueMACs;
  43.  
  44. // Target MAC address (converted to uppercase for comparison)
  45. const char targetMAC[] = "B8:7E:39:E7:9E:FF";
  46.  
  47. // Callback to process captured packets
  48. void sniffer_callback(void* buf, wifi_promiscuous_pkt_type_t type) {
  49.     const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buf;
  50.     // Extract the source MAC address from the packet
  51.     const uint8_t *mac = ppkt->payload + 10; // Offset to get the source MAC
  52.     char macAddr[18];
  53.     sprintf(macAddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  54.             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  55.     String macString = String(macAddr);
  56.     // Check if the MAC has already been detected
  57.     if (uniqueMACs.find(macString) == uniqueMACs.end()) {
  58.         uniqueMACs.insert(macString); // Add MAC to the set
  59.         // Show message if it is the target MAC
  60.         if (strcmp(macAddr, targetMAC) == 0) {
  61.             Serial.println("DIRECCIÓN ENCONTRADA: ASISTIÓ"); // Target MAC found
  62.         } else {
  63.             // Show the unique detected MAC address
  64.             Serial.print("Nueva dirección MAC detectada: ");
  65.             Serial.println(macAddr);
  66.         }
  67.     }
  68. }
  69.  
  70. void setup(void) {
  71.     Serial.begin(115200); // Initialize serial monitor
  72.     delay(1000);
  73.     // Configure Wi-Fi in station mode
  74.     WiFi.mode(WIFI_MODE_STA);
  75.     esp_wifi_set_promiscuous(true); // Enable promiscuous mode
  76.     esp_wifi_set_promiscuous_filter(&filter); // Apply filter
  77.     esp_wifi_set_promiscuous_rx_cb(&sniffer_callback); // Register callback function
  78.     esp_wifi_start(); // Start Wi-Fi in promiscuous mode
  79.     Serial.println("Modo promiscuo activado. Detectando direcciones MAC únicas...");
  80. }
  81.  
  82. void loop(void) {
  83.     // No need for anything in the loop, as everything happens in the callback
  84. }
  85.  
  86. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement