Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "MAC Capture"
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-12-11 15:01:30
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* I want my esp32 to detect mac addresses of other */
- /* devices and I want the mac addresses that I */
- /* indicate to be detected and recorded as PRESENT in */
- /* the serial monitor. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <WiFi.h> // Include the WiFi library for ESP32
- #include <set> // Include the set library for unique MAC storage
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Define the promiscuous mode filter
- wifi_promiscuous_filter_t filter = {
- .filter_mask = WIFI_PROMIS_FILTER_MASK_DATA | WIFI_PROMIS_FILTER_MASK_MGMT,
- }; // This filter is used to capture data and management frames
- // Set to store unique MAC addresses
- std::set<String> uniqueMACs;
- // Target MAC address (converted to uppercase for comparison)
- const char targetMAC[] = "B8:7E:39:E7:9E:FF";
- // Callback to process captured packets
- void sniffer_callback(void* buf, wifi_promiscuous_pkt_type_t type) {
- const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buf;
- // Extract the source MAC address from the packet
- const uint8_t *mac = ppkt->payload + 10; // Offset to get the source MAC
- char macAddr[18];
- sprintf(macAddr, "%02X:%02X:%02X:%02X:%02X:%02X",
- mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
- String macString = String(macAddr);
- // Check if the MAC has already been detected
- if (uniqueMACs.find(macString) == uniqueMACs.end()) {
- uniqueMACs.insert(macString); // Add MAC to the set
- // Show message if it is the target MAC
- if (strcmp(macAddr, targetMAC) == 0) {
- Serial.println("DIRECCIÓN ENCONTRADA: ASISTIÓ"); // Target MAC found
- } else {
- // Show the unique detected MAC address
- Serial.print("Nueva dirección MAC detectada: ");
- Serial.println(macAddr);
- }
- }
- }
- void setup(void) {
- Serial.begin(115200); // Initialize serial monitor
- delay(1000);
- // Configure Wi-Fi in station mode
- WiFi.mode(WIFI_MODE_STA);
- esp_wifi_set_promiscuous(true); // Enable promiscuous mode
- esp_wifi_set_promiscuous_filter(&filter); // Apply filter
- esp_wifi_set_promiscuous_rx_cb(&sniffer_callback); // Register callback function
- esp_wifi_start(); // Start Wi-Fi in promiscuous mode
- Serial.println("Modo promiscuo activado. Detectando direcciones MAC únicas...");
- }
- void loop(void) {
- // No need for anything in the loop, as everything happens in the callback
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement