Guest User

Untitled

a guest
Oct 30th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. extern "C" {
  4. #include <user_interface.h>
  5. }
  6.  
  7. #define DATA_LENGTH 112
  8.  
  9. #define TYPE_MANAGEMENT 0x00
  10. #define TYPE_CONTROL 0x01
  11. #define TYPE_DATA 0x02
  12. #define SUBTYPE_PROBE_REQUEST 0x04
  13.  
  14. struct RxControl {
  15. signed rssi:8; // signal intensity of packet
  16. unsigned rate:4;
  17. unsigned is_group:1;
  18. unsigned:1;
  19. unsigned sig_mode:2; // 0:is 11n packet; 1:is not 11n packet;
  20. unsigned legacy_length:12; // if not 11n packet, shows length of packet.
  21. unsigned damatch0:1;
  22. unsigned damatch1:1;
  23. unsigned bssidmatch0:1;
  24. unsigned bssidmatch1:1;
  25. unsigned MCS:7; // if is 11n packet, shows the modulation and code used (range from 0 to 76)
  26. unsigned CWB:1; // if is 11n packet, shows if is HT40 packet or not
  27. unsigned HT_length:16;// if is 11n packet, shows length of packet.
  28. unsigned Smoothing:1;
  29. unsigned Not_Sounding:1;
  30. unsigned:1;
  31. unsigned Aggregation:1;
  32. unsigned STBC:2;
  33. unsigned FEC_CODING:1; // if is 11n packet, shows if is LDPC packet or not.
  34. unsigned SGI:1;
  35. unsigned rxend_state:8;
  36. unsigned ampdu_cnt:8;
  37. unsigned channel:4; //which channel this packet in.
  38. unsigned:12;
  39. };
  40.  
  41. struct SnifferPacket{
  42. struct RxControl rx_ctrl;
  43. uint8_t data[DATA_LENGTH];
  44. uint16_t cnt;
  45. uint16_t len;
  46. };
  47.  
  48. std::vector<SnifferPacket*> myList;
  49.  
  50. // Declare each custom function (excluding built-in, such as setup and loop) before it will be called.
  51. // https://docs.platformio.org/en/latest/faq.html#convert-arduino-file-to-c-manually
  52. static void showMetadata(SnifferPacket *snifferPacket);
  53. static void ICACHE_FLASH_ATTR sniffer_callback(uint8_t *buffer, uint16_t length);
  54. static void printDataSpan(uint16_t start, uint16_t size, uint8_t* data);
  55. static void getMAC(char *addr, uint8_t* data, uint16_t offset);
  56. void channelHop();
  57.  
  58. static void showMetadata(SnifferPacket *snifferPacket) {
  59.  
  60. unsigned int frameControl = ((unsigned int)snifferPacket->data[1] << 8) + snifferPacket->data[0];
  61.  
  62. uint8_t version = (frameControl & 0b0000000000000011) >> 0;
  63. uint8_t frameType = (frameControl & 0b0000000000001100) >> 2;
  64. uint8_t frameSubType = (frameControl & 0b0000000011110000) >> 4;
  65. uint8_t toDS = (frameControl & 0b0000000100000000) >> 8;
  66. uint8_t fromDS = (frameControl & 0b0000001000000000) >> 9;
  67.  
  68. // Only look for probe request packets
  69. if (frameType != TYPE_MANAGEMENT ||
  70. frameSubType != SUBTYPE_PROBE_REQUEST)
  71. return;
  72.  
  73. myList.push_back(snifferPacket);
  74.  
  75. //Serial.print("RSSI: ");
  76. Serial.print(snifferPacket->rx_ctrl.rssi, DEC);
  77. Serial.print(";");
  78.  
  79. //Serial.print(" Ch: ");
  80. Serial.print(wifi_get_channel());
  81. Serial.print(";");
  82.  
  83. char addr[] = "00:00:00:00:00:00";
  84. getMAC(addr, snifferPacket->data, 10);
  85. //Serial.print(" Peer MAC: ");
  86. Serial.print(addr);
  87. Serial.print(";");
  88.  
  89. uint8_t SSID_length = snifferPacket->data[25];
  90. //Serial.print(" SSID: ");
  91. printDataSpan(26, SSID_length, snifferPacket->data);
  92.  
  93. Serial.println();
  94. }
  95.  
  96. /**
  97. * Callback for promiscuous mode
  98. */
  99. static void ICACHE_FLASH_ATTR sniffer_callback(uint8_t *buffer, uint16_t length) {
  100. struct SnifferPacket *snifferPacket = (struct SnifferPacket*) buffer;
  101. showMetadata(snifferPacket);
  102. }
  103.  
  104. static void printDataSpan(uint16_t start, uint16_t size, uint8_t* data) {
  105. for(uint16_t i = start; i < DATA_LENGTH && i < start+size; i++) {
  106. Serial.write(data[i]);
  107. }
  108. }
  109.  
  110. static void getMAC(char *addr, uint8_t* data, uint16_t offset) {
  111. sprintf(addr, "%02x:%02x:%02x:%02x:%02x:%02x", data[offset+0], data[offset+1], data[offset+2], data[offset+3], data[offset+4], data[offset+5]);
  112. }
  113.  
  114. #define CHANNEL_HOP_INTERVAL_MS 1000
  115. static os_timer_t channelHop_timer;
  116.  
  117. /**
  118. * Callback for channel hoping
  119. */
  120. void channelHop()
  121. {
  122. // hoping channels 1-13
  123. uint8 new_channel = wifi_get_channel() + 1;
  124. if (new_channel > 13) {
  125. new_channel = 1;
  126. }
  127. wifi_set_channel(new_channel);
  128. }
  129.  
  130. #define DISABLE 0
  131. #define ENABLE 1
  132.  
  133. void setup() {
  134. // set the WiFi chip to "promiscuous" mode aka monitor mode
  135. Serial.begin(115200);
  136. delay(10);
  137. wifi_set_opmode(STATION_MODE);
  138. wifi_set_channel(1);
  139. wifi_promiscuous_enable(DISABLE);
  140. delay(10);
  141. wifi_set_promiscuous_rx_cb(sniffer_callback);
  142. delay(10);
  143. wifi_promiscuous_enable(ENABLE);
  144.  
  145. // setup the channel hoping callback timer
  146. os_timer_disarm(&channelHop_timer);
  147. os_timer_setfn(&channelHop_timer, (os_timer_func_t *) channelHop, NULL);
  148. os_timer_arm(&channelHop_timer, CHANNEL_HOP_INTERVAL_MS, 1);
  149. }
  150.  
  151. void loop() {
  152. delay(10);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment