-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
323 lines (245 loc) · 9.52 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* This software is licensed under the MIT License: https://github.com/spacehuhntech/esp8266_deauther */
#include "settings.h"
#include "A_config.h" // Default Settings
#include "language.h" // prnt and prntln
#include "EEPROMHelper.h" // To load and save settings_t
#include "debug.h"
#define MAGIC_NUM 3416245
extern bool writeFile(String path, String& buf);
extern void getRandomMac(uint8_t* mac);
extern bool macValid(uint8_t* mac);
#define JSON_FLAG(_NAME, _VALUE)\
str += String('"') + String(FPSTR(_NAME)) + String(F("\":")) + String(_VALUE ? "true" : "false") + String(',');
#define JSON_VALUE(_NAME, _VALUE)\
str += String('"') + String(FPSTR(_NAME)) + String(F("\":\"")) + String(_VALUE) + String(F("\","));
#define JSON_INT(_NAME, _VALUE)\
str += String('"') + String(FPSTR(_NAME)) + String(F("\":")) + String(_VALUE) + String(',');
#define JSON_HEX(_NAME, _BYTES, _LEN)\
str += String('"') + String(FPSTR(_NAME)) + String(F("\":\""));\
for (int i = 0; i<_LEN; i++) {\
if (i > 0) str += ':';\
if (_BYTES[i] < 0x10) str += '0';\
str += String(_BYTES[i], HEX);\
}\
str += String(F("\","));
#define JSON_DEC(_NAME, _BYTES, _LEN)\
str += String(F("\"")) + String(FPSTR(_NAME)) + String(F("\":\""));\
for (int i = 0; i<_LEN; i++) {\
if (i > 0) str += '.';\
str += String(_BYTES[i]);\
}\
str += String(F("\","));
namespace settings {
// ========== PRIVATE ========== //
const char* SETTINGS_PATH = "/settings.json";
settings_t data;
bool changed = false;
void get_json(String& str) {
str = String();
str.reserve(600);
str += '{';
// Version
JSON_VALUE(S_JSON_VERSION, DEAUTHER_VERSION);
// Autosave
JSON_FLAG(S_JSON_AUTOSAVE, data.autosave.enabled);
JSON_INT(S_JSON_AUTOSAVETIME, data.autosave.time);
// Attack
JSON_FLAG(S_JSON_BEACONCHANNEL, data.attack.attack_all_ch);
JSON_FLAG(S_JSON_RANDOMTX, data.attack.random_tx);
JSON_INT(S_JSON_ATTACKTIMEOUT, data.attack.timeout);
JSON_INT(S_JSON_DEAUTHSPERTARGET, data.attack.deauths_per_target);
JSON_INT(S_JSON_DEAUTHREASON, data.attack.deauth_reason);
JSON_FLAG(S_JSON_BEACONINTERVAL, data.attack.beacon_interval == INTERVAL_1S);
JSON_INT(S_JSON_PROBESPERSSID, data.attack.probe_frames_per_ssid);
// WiFi
JSON_INT(S_JSON_CHANNEL, data.wifi.channel);
JSON_HEX(S_JSON_MACST, data.wifi.mac_st, 6);
JSON_HEX(S_JSON_MACAP, data.wifi.mac_ap, 6);
// Sniffer
JSON_INT(S_JSON_CHTIME, data.sniffer.channel_time);
JSON_INT(S_JSON_MIN_DEAUTHS, data.sniffer.min_deauth_frames);
// Access Point
JSON_VALUE(S_JSON_SSID, data.ap.ssid);
JSON_VALUE(S_JSON_PASSWORD, data.ap.password);
JSON_FLAG(S_JSON_HIDDEN, data.ap.hidden);
JSON_DEC(S_JSON_IP, data.ap.ip, 4);
// Web Interface
JSON_FLAG(S_JSON_WEBINTERFACE, data.web.enabled);
JSON_FLAG(S_JSON_CAPTIVEPORTAL, data.web.captive_portal);
JSON_FLAG(S_JSON_WEB_SPIFFS, data.web.use_spiffs);
JSON_VALUE(S_JSON_LANG, data.web.lang);
// CLI
JSON_FLAG(S_JSON_SERIALINTERFACE, data.cli.enabled);
JSON_FLAG(S_JSON_SERIAL_ECHO, data.cli.serial_echo);
// LED
JSON_FLAG(S_JSON_LEDENABLED, data.led.enabled);
// Display
JSON_FLAG(S_JSON_DISPLAYINTERFACE, data.display.enabled);
JSON_INT(S_JSON_DISPLAY_TIMEOUT, data.display.timeout);
str.setCharAt(str.length()-1, '}');
}
// ========== PUBLIC ========== //
void load() {
debugF("Loading settings...");
// read data from eeproms
settings_t newData;
EEPROMHelper::getObject(SETTINGS_ADDR, newData);
// calc and check hash
if (newData.magic_num == MAGIC_NUM) {
data = newData;
data.version.major = DEAUTHER_VERSION_MAJOR;
data.version.minor = DEAUTHER_VERSION_MINOR;
data.version.revision = DEAUTHER_VERSION_REVISION;
debuglnF("OK");
save();
} else {
debuglnF("Invalid Hash");
/*debug(data.magic_num);
debugF(" != ");
debugln(MAGIC_NUM);*/
reset();
save();
}
// check and fix mac
if (!macValid(data.wifi.mac_st)) getRandomMac(data.wifi.mac_st);
if (!macValid(data.wifi.mac_ap)) getRandomMac(data.wifi.mac_ap);
changed = true;
}
void reset() {
data.magic_num = MAGIC_NUM;
data.version.major = DEAUTHER_VERSION_MAJOR;
data.version.minor = DEAUTHER_VERSION_MINOR;
data.version.revision = DEAUTHER_VERSION_REVISION;
data.attack.attack_all_ch = ATTACK_ALL_CH;
data.attack.random_tx = RANDOM_TX;
data.attack.timeout = ATTACK_TIMEOUT;
data.attack.deauths_per_target = DEAUTHS_PER_TARGET;
data.attack.deauth_reason = DEAUTH_REASON;
data.attack.beacon_interval = beacon_interval_t::INTERVAL_100MS;
data.attack.probe_frames_per_ssid = PROBE_FRAMES_PER_SSID;
data.wifi.channel = 1;
getRandomMac(data.wifi.mac_st);
getRandomMac(data.wifi.mac_ap);
data.sniffer.channel_time = CH_TIME;
data.sniffer.min_deauth_frames = MIN_DEAUTH_FRAMES;
strncpy(data.ap.ssid, AP_SSID, 32);
strncpy(data.ap.password, AP_PASSWD, 64);
data.ap.hidden = AP_HIDDEN;
uint8_t ip[4] = AP_IP_ADDR;
memcpy(data.ap.ip, ip, 4);
data.web.enabled = WEB_ENABLED;
data.web.captive_portal = WEB_CAPTIVE_PORTAL;
data.web.use_spiffs = WEB_USE_SPIFFS;
memcpy(data.web.lang, DEFAULT_LANG, 3);
data.cli.enabled = CLI_ENABLED;
data.cli.serial_echo = CLI_ECHO;
data.led.enabled = USE_LED;
data.display.enabled = USE_DISPLAY;
data.display.timeout = DISPLAY_TIMEOUT;
changed = true;
debuglnF("Settings reset to default");
}
void save(bool force) {
if (force || changed) {
EEPROMHelper::saveObject(SETTINGS_ADDR, data);
changed = false;
String json_buffer;
get_json(json_buffer);
if (writeFile(SETTINGS_PATH, json_buffer)) {
debugF("Settings saved in ");
} else {
debugF("ERROR: saving ");
}
debugln(SETTINGS_PATH);
}
}
void print() {
String json_buffer;
get_json(json_buffer);
json_buffer.replace("\":", ": ");
json_buffer.replace(": 0\r\n", ": false\r\n");
json_buffer.replace(": 1\r\n", ": true\r\n");
json_buffer.replace("\"", "");
json_buffer.replace("{", "");
json_buffer.replace("}", "");
json_buffer.replace(",", "\r\n");
debuglnF("[========== Settings ==========]");
debugln(json_buffer);
}
// ===== GETTERS ===== //
const settings_t& getAllSettings() {
return data;
}
const version_t& getVersion() {
return data.version;
}
const autosave_settings_t& getAutosaveSettings() {
return data.autosave;
}
const attack_settings_t& getAttackSettings() {
return data.attack;
}
const wifi_settings_t& getWifiSettings() {
return data.wifi;
}
const sniffer_settings_t& getSnifferSettings() {
return data.sniffer;
}
const access_point_settings_t& getAccessPointSettings() {
return data.ap;
}
const web_settings_t& getWebSettings() {
return data.web;
}
const cli_settings_t& getCLISettings() {
return data.cli;
}
const led_settings_t& getLEDSettings() {
return data.led;
}
const display_settings_t& getDisplaySettings() {
return data.display;
}
// ===== SETTERS ===== //
void setAllSettings(settings_t& newSettings) {
newSettings.version = data.version;
data = newSettings;
changed = true;
}
void setAutosaveSettings(const autosave_settings_t& autosave) {
data.autosave = autosave;
changed = true;
}
void setAttackSettings(const attack_settings_t& attack) {
data.attack = attack;
changed = true;
}
void setWifiSettings(const wifi_settings_t& wifi) {
data.wifi = wifi;
changed = true;
}
void setSnifferSettings(const sniffer_settings_t& sniffer) {
data.sniffer = sniffer;
changed = true;
}
void setAccessPointSettings(const access_point_settings_t& ap) {
data.ap = ap;
changed = true;
}
void setWebSettings(const web_settings_t& web) {
data.web = web;
changed = true;
}
void setCLISettings(const cli_settings_t& cli) {
data.cli = cli;
changed = true;
}
void setLEDSettings(const led_settings_t& led) {
data.led = led;
changed = true;
}
void setDisplaySettings(const display_settings_t& display) {
data.display = display;
changed = true;
}
}