New CLI commands for getting and setting settings

This commit is contained in:
Stefan Kremser
2019-05-28 20:33:31 +02:00
parent bb8ec1f6f8
commit fba18d7c66
10 changed files with 227 additions and 46 deletions

View File

@@ -15,7 +15,7 @@ extern Names names;
extern String searchVendor(uint8_t* mac);
extern String leftRight(String a, String b, int len);
extern String fixUtf8(String str);
extern String bytesToStr(uint8_t* b, uint32_t size);
extern String bytesToStr(const uint8_t* b, uint32_t size);
struct AP {
uint8_t id;

View File

@@ -26,8 +26,8 @@ extern uint32_t currentTime;
extern bool macBroadcast(uint8_t* mac);
extern void getRandomMac(uint8_t* mac);
extern void setOutputPower(float dBm);
extern String macToStr(uint8_t* mac);
extern String bytesToStr(uint8_t* b, uint32_t size);
extern String macToStr(const uint8_t* mac);
extern String bytesToStr(const uint8_t* b, uint32_t size);
extern void setWifiChannel(uint8_t ch);
extern bool writeFile(String path, String& buf);
extern int8_t free80211_send(uint8_t* buffer, uint16_t len);

View File

@@ -673,14 +673,133 @@ void CLI::runCommand(String input) {
// ===== GET/SET ===== //
// get <setting>
else if (eqlsCMD(0, CLI_GET) && (list->size() == 2)) {
settings.print();
// prntln(settings.get(list->get(1).c_str()));
else if (eqlsCMD(0, CLI_GET) /*&& (list->size() == 2)*/) {
String _tmp = list->get(1);
const char* str = _tmp.c_str();
if (eqls(str, "settings")) settings.print();
// Version
else if (eqls(str, S_JSON_VERSION)) prntln(DEAUTHER_VERSION);
else if (eqls(str, S_JSON_AUTOSAVE)) prntln(settings.getAutosaveSettings().enabled);
else if (eqls(str, S_JSON_AUTOSAVETIME)) prntln(settings.getAutosaveSettings().time);
// Attack
else if (eqls(str, S_JSON_BEACONCHANNEL)) prntln((int)settings.getAttackSettings().attack_all_ch);
else if (eqls(str, S_JSON_RANDOMTX)) prntln(settings.getAttackSettings().random_tx);
else if (eqls(str, S_JSON_ATTACKTIMEOUT)) prntln(settings.getAttackSettings().timeout);
else if (eqls(str, S_JSON_DEAUTHSPERTARGET)) prntln(settings.getAttackSettings().deauths_per_target);
else if (eqls(str, S_JSON_DEAUTHREASON)) prntln(settings.getAttackSettings().deauth_reason);
else if (eqls(str, S_JSON_BEACONINTERVAL)) prntln((bool)settings.getAttackSettings().beacon_interval);
else if (eqls(str, S_JSON_PROBESPERSSID)) prntln(settings.getAttackSettings().probe_frames_per_ssid);
// WiFi
else if (eqls(str, S_JSON_CHANNEL)) prntln(settings.getWifiSettings().channel);
else if (eqls(str, S_JSON_MACST)) prntln(macToStr(settings.getWifiSettings().mac_st));
else if (eqls(str, S_JSON_MACAP)) prntln(macToStr(settings.getWifiSettings().mac_ap));
// Sniffer
else if (eqls(str, S_JSON_CHTIME)) prntln(settings.getSnifferSettings().channel_time);
else if (eqls(str, S_JSON_MIN_DEAUTHS)) prntln(settings.getSnifferSettings().min_deauth_frames);
// AP
else if (eqls(str, S_JSON_SSID)) prntln(settings.getAccessPointSettings().ssid);
else if (eqls(str, S_JSON_PASSWORD)) prntln(settings.getAccessPointSettings().password);
else if (eqls(str, S_JSON_HIDDEN)) prntln(settings.getAccessPointSettings().hidden);
else if (eqls(str, S_JSON_IP)) prntln(settings.getAccessPointSettings().ip);
// Web
else if (eqls(str, S_JSON_WEBINTERFACE)) prntln(settings.getWebSettings().enabled);
else if (eqls(str, S_JSON_CAPTIVEPORTAL)) prntln(settings.getWebSettings().captive_portal);
else if (eqls(str, S_JSON_WEB_SPIFFS)) prntln(settings.getWebSettings().use_spiffs);
else if (eqls(str, S_JSON_LANG)) prntln(settings.getWebSettings().lang, 3);
// CLI
else if (eqls(str, S_JSON_SERIALINTERFACE)) prntln(settings.getCLISettings().enabled);
else if (eqls(str, S_JSON_SERIAL_ECHO)) prntln(settings.getCLISettings().serial_echo);
// LED
else if (eqls(str, S_JSON_LEDENABLED)) prntln(settings.getLEDSettings().enabled);
// Display
else if (eqls(str, S_JSON_DISPLAYINTERFACE)) prntln(settings.getDisplaySettings().enabled);
else if (eqls(str, S_JSON_DISPLAY_TIMEOUT)) prntln(settings.getDisplaySettings().timeout);
else {
prnt(_tmp);
prntln(" settings not found");
}
}
// set <setting> <value>
else if (eqlsCMD(0, CLI_SET) && (list->size() == 3)) {
// settings.set(list->get(1).c_str(), list->get(2));
String _tmp = list->get(1);
const char* str = _tmp.c_str();
String strVal = list->get(2);
bool boolVal = s2b(strVal);
int intVal = strVal.toInt();
uint32_t unsignedVal = intVal < 0 ? 0 : (uint32_t)intVal;
settings_t newSettings = settings.getAllSettings();
// Autosave
if (eqls(str, S_JSON_AUTOSAVE)) newSettings.autosave.enabled = boolVal;
else if (eqls(str, S_JSON_AUTOSAVETIME)) newSettings.autosave.time = unsignedVal;
// Attack
else if (eqls(str, S_JSON_BEACONCHANNEL)) newSettings.attack.attack_all_ch = boolVal;
else if (eqls(str, S_JSON_RANDOMTX)) newSettings.attack.random_tx = boolVal;
else if (eqls(str, S_JSON_ATTACKTIMEOUT)) newSettings.attack.timeout = unsignedVal;
else if (eqls(str, S_JSON_DEAUTHSPERTARGET)) newSettings.attack.deauths_per_target = unsignedVal;
else if (eqls(str, S_JSON_DEAUTHREASON)) newSettings.attack.deauth_reason = unsignedVal;
else if (eqls(str, S_JSON_BEACONINTERVAL)) newSettings.attack.beacon_interval = (beacon_interval_t)boolVal;
else if (eqls(str, S_JSON_PROBESPERSSID)) newSettings.attack.probe_frames_per_ssid = unsignedVal;
// WiFi
else if (eqls(str, S_JSON_CHANNEL)) newSettings.wifi.channel = unsignedVal;
else if (eqls(str, S_JSON_MACST)) strToMac(strVal, newSettings.wifi.mac_st);
else if (eqls(str, S_JSON_MACAP)) strToMac(strVal, newSettings.wifi.mac_ap);
// Sniffer
else if (eqls(str, S_JSON_CHTIME)) newSettings.sniffer.channel_time = unsignedVal;
else if (eqls(str, S_JSON_MIN_DEAUTHS)) newSettings.sniffer.min_deauth_frames = unsignedVal;
// AP
else if (eqls(str, S_JSON_SSID)) strncpy(newSettings.ap.ssid, strVal.c_str(), 32);
else if (eqls(str, S_JSON_PASSWORD)) strncpy(newSettings.ap.password, strVal.c_str(), 64);
else if (eqls(str, S_JSON_HIDDEN)) newSettings.ap.hidden = boolVal;
else if (eqls(str, S_JSON_IP)) strToIP(strVal, newSettings.ap.ip);
// Web
else if (eqls(str, S_JSON_WEBINTERFACE)) newSettings.web.enabled = boolVal;
else if (eqls(str, S_JSON_CAPTIVEPORTAL)) newSettings.web.captive_portal = boolVal;
else if (eqls(str, S_JSON_WEB_SPIFFS)) newSettings.web.use_spiffs = boolVal;
else if (eqls(str, S_JSON_LANG)) strncpy(newSettings.web.lang, strVal.c_str(), 3);
// CLI
else if (eqls(str, S_JSON_SERIALINTERFACE)) newSettings.cli.enabled = boolVal;
else if (eqls(str, S_JSON_SERIAL_ECHO)) newSettings.cli.serial_echo = boolVal;
// LED
else if (eqls(str, S_JSON_LEDENABLED)) newSettings.led.enabled = boolVal;
// Display
else if (eqls(str, S_JSON_DISPLAYINTERFACE)) newSettings.display.enabled = boolVal;
else if (eqls(str, S_JSON_DISPLAY_TIMEOUT)) newSettings.display.timeout = unsignedVal;
else {
prnt(str);
prntln(" not found");
return;
}
prnt("Set ");
prnt(str);
prnt(" = ");
prntln(strVal);
settings.setAllSettings(newSettings);
}
// ====== CHICKEN ===== //

View File

@@ -30,7 +30,9 @@ extern DisplayUI displayUI;
extern uint32_t currentTime;
extern uint32_t autosaveTime;
extern String macToStr(uint8_t* mac);
extern String macToStr(const uint8_t* mac);
extern bool strToMac(String macStr, uint8_t* mac);
extern bool strToIP(String ipStr, uint8_t* ip);
extern void strToColor(String str, uint8_t* buf);
extern void readFileToSerial(String path, bool showLineNum);
extern bool readFile(String path, String& buf);
@@ -69,8 +71,8 @@ class CLI {
private:
bool enabled = false;
SimpleList<String>* list;
SimpleList<String>* queue;
SimpleList<String>*list;
SimpleList<String>*queue;
bool delayed = false;
uint32_t delayTime = 0;

View File

@@ -23,7 +23,7 @@ extern String searchVendor(uint8_t* mac);
extern String fixUtf8(String str);
extern String leftRight(String a, String b, int len);
extern String escape(String str);
extern String bytesToStr(uint8_t* b, uint32_t size);
extern String bytesToStr(const uint8_t* b, uint32_t size);
class Names {
public:

View File

@@ -25,15 +25,15 @@ void jsonStr(String& str, const char* name, const char* value) {
str += ',';
}
void jsonFlag(String& str, const char* name, bool value) {
/*
void jsonFlag(String& str, const char* name, bool value) {
str += '"';
str += String(name);
str += '"';
str += ':';
str += String(value ? S_JSON_TRUE : S_JSON_FALSE);
str += ',';
}
}*/
void jsonValue(String& str, const char* name, int value) {
str += '"';
str += String(name);
@@ -91,16 +91,16 @@ String Settings::getJsonStr() {
jsonStr(str, S_JSON_VERSION, DEAUTHER_VERSION);
// Autosave
jsonFlag(str, S_JSON_AUTOSAVE, data.autosave.enabled);
/*jsonFlag*/ jsonValue(str, S_JSON_AUTOSAVE, data.autosave.enabled);
jsonValue(str, S_JSON_AUTOSAVETIME, data.autosave.time);
// Attack
jsonFlag(str, S_JSON_BEACONCHANNEL, data.attack.attack_all_ch);
jsonFlag(str, S_JSON_RANDOMTX, data.attack.random_tx);
/*jsonFlag*/ jsonValue(str, S_JSON_BEACONCHANNEL, data.attack.attack_all_ch);
/*jsonFlag*/ jsonValue(str, S_JSON_RANDOMTX, data.attack.random_tx);
jsonValue(str, S_JSON_ATTACKTIMEOUT, data.attack.timeout);
jsonValue(str, S_JSON_DEAUTHSPERTARGET, data.attack.deauths_per_target);
jsonValue(str, S_JSON_DEAUTHREASON, data.attack.deauth_reason);
jsonFlag(str, S_JSON_BEACONINTERVAL, data.attack.beacon_interval == INTERVAL_1S);
/*jsonFlag*/ jsonValue(str, S_JSON_BEACONINTERVAL, data.attack.beacon_interval == INTERVAL_1S);
jsonValue(str, S_JSON_PROBESPERSSID, data.attack.probe_frames_per_ssid);
// WiFi
@@ -115,24 +115,24 @@ String Settings::getJsonStr() {
// Access Point
jsonStr(str, S_JSON_SSID, data.ap.ssid);
jsonStr(str, S_JSON_PASSWORD, data.ap.password);
jsonFlag(str, S_JSON_HIDDEN, data.ap.hidden);
/*jsonFlag*/ jsonValue(str, S_JSON_HIDDEN, data.ap.hidden);
jsonDec(str, S_JSON_IP, data.ap.ip, 4);
// Web Interface
jsonFlag(str, S_JSON_WEBINTERFACE, data.web.enabled);
jsonFlag(str, S_JSON_CAPTIVEPORTAL, data.web.captive_portal);
jsonFlag(str, S_JSON_WEB_SPIFFS, data.web.use_spiffs);
/*jsonFlag*/ jsonValue(str, S_JSON_WEBINTERFACE, data.web.enabled);
/*jsonFlag*/ jsonValue(str, S_JSON_CAPTIVEPORTAL, data.web.captive_portal);
/*jsonFlag*/ jsonValue(str, S_JSON_WEB_SPIFFS, data.web.use_spiffs);
jsonStr(str, S_JSON_LANG, data.web.lang);
// CLI
jsonFlag(str, S_JSON_SERIALINTERFACE, data.cli.enabled);
jsonFlag(str, S_JSON_SERIAL_ECHO, data.cli.serial_echo);
/*jsonFlag*/ jsonValue(str, S_JSON_SERIALINTERFACE, data.cli.enabled);
/*jsonFlag*/ jsonValue(str, S_JSON_SERIAL_ECHO, data.cli.serial_echo);
// LED
jsonFlag(str, S_JSON_LEDENABLED, data.led.enabled);
/*jsonFlag*/ jsonValue(str, S_JSON_LEDENABLED, data.led.enabled);
// Display
jsonFlag(str, S_JSON_DISPLAYINTERFACE, data.display.enabled);
/*jsonFlag*/ jsonValue(str, S_JSON_DISPLAYINTERFACE, data.display.enabled);
jsonValue(str, S_JSON_DISPLAY_TIMEOUT, data.display.timeout);
str[str.length()-1] = '}';
@@ -197,6 +197,8 @@ void Settings::print() {
String settingsJson = getJsonStr();
settingsJson.replace("\":", " = ");
settingsJson.replace("= 0\r\n", "= false\r\n");
settingsJson.replace("= 1\r\n", "= true\r\n");
settingsJson.replace("\"", "");
settingsJson.replace("{", "");
settingsJson.replace("}", "");
@@ -208,6 +210,10 @@ void Settings::print() {
// ===== GETTERS ===== //
const settings_t& Settings::getAllSettings() {
return data;
}
const version_t& Settings::getVersion() {
return data.version;
}
@@ -250,6 +256,12 @@ const display_settings_t& Settings::getDisplaySettings() {
// ===== SETTERS ===== //
void Settings::setAllSettings(settings_t& newSettings) {
newSettings.version = this->data.version;
data = newSettings;
changed = true;
}
void Settings::setAutosaveSettings(const autosave_settings_t& autosave) {
data.autosave = autosave;
changed = true;

View File

@@ -35,6 +35,8 @@ const char S_JSON_FALSE[] PROGMEM = "true";
// Version
const char S_JSON_VERSION[] PROGMEM = "version";
// Autosave
const char S_JSON_AUTOSAVE[] PROGMEM = "autosave";
const char S_JSON_AUTOSAVETIME[] PROGMEM = "autosavetime";
@@ -198,7 +200,8 @@ class Settings {
void reset();
void print();
const version_t& getVersion();
const settings_t& getAllSettings();
const version_t & getVersion();
const autosave_settings_t& getAutosaveSettings();
const attack_settings_t & getAttackSettings();
const wifi_settings_t & getWifiSettings();
@@ -209,6 +212,7 @@ class Settings {
const led_settings_t& getLEDSettings();
const display_settings_t& getDisplaySettings();
void setAllSettings(settings_t& settings);
void setAutosaveSettings(const autosave_settings_t& autosave);
void setAttackSettings(const attack_settings_t& attack);
void setWifiSettings(const wifi_settings_t& wifi);

View File

@@ -20,7 +20,7 @@ extern String searchVendor(uint8_t* mac);
extern bool macMulticast(uint8_t* mac);
extern bool macValid(uint8_t* mac);
extern bool macBroadcast(uint8_t* mac);
extern String bytesToStr(uint8_t* b, uint32_t size);
extern String bytesToStr(const uint8_t* b, uint32_t size);
class Stations {
public:

View File

@@ -247,15 +247,15 @@ bool s2b(String input) {
}
// ===== PRINT FUNCTIONS ===== //
void prnt(String s) {
void prnt(const String s) {
Serial.print(s);
}
void prnt(bool b) {
void prnt(const bool b) {
Serial.print(b2s(b));
}
void prnt(char c) {
void prnt(const char c) {
Serial.print(c);
}
@@ -263,23 +263,31 @@ void prnt(const char* ptr) {
Serial.print(FPSTR(ptr));
}
void prnt(int i) {
void prnt(const char* ptr, int len) {
for (int i = 0; i<len; i++) prnt(ptr[i]);
}
void prnt(const int i) {
Serial.print((String)i);
}
void prnt(const uint32_t i) {
Serial.printf("%u", i);
}
void prntln() {
Serial.println();
}
void prntln(String s) {
void prntln(const String s) {
Serial.println(s);
}
void prntln(bool b) {
void prntln(const bool b) {
Serial.println(b2s(b));
}
void prntln(char c) {
void prntln(const char c) {
Serial.println(c);
}
@@ -287,10 +295,19 @@ void prntln(const char* ptr) {
Serial.println(FPSTR(ptr));
}
void prntln(int i) {
void prntln(const char* ptr, int len) {
for (int i = 0; i<len; i++) prnt(ptr[i]);
prntln();
}
void prntln(const int i) {
Serial.println((String)i);
}
void prntln(const uint32_t i) {
Serial.printf("%u\r\n", i);
}
/* ===== WiFi ===== */
void setWifiChannel(uint8_t ch) {
if ((ch != wifi_channel) && (ch > 0) && (ch < 15)) {
@@ -402,7 +419,7 @@ String searchVendor(uint8_t* mac) {
}
/* ===== STRING ===== */
String bytesToStr(uint8_t* b, uint32_t size) {
String bytesToStr(const uint8_t* b, uint32_t size) {
String str;
for (uint32_t i = 0; i < size; i++) {
@@ -414,7 +431,7 @@ String bytesToStr(uint8_t* b, uint32_t size) {
return str;
}
String macToStr(uint8_t* mac) {
String macToStr(const uint8_t* mac) {
return bytesToStr(mac, 6);
}
@@ -435,6 +452,29 @@ bool strToMac(String macStr, uint8_t* mac) {
return true;
}
bool strToIP(String ipStr, uint8_t* ip) {
String parts[4] = { "0", "0", "0", "0" };
int ipAddr[4] = { -1, -1, -1, -1 };
int j = 0;
for (int i = 0; i<ipStr.length(); i++) {
if (ipStr[i] == '.') j++;
else parts[j] += ipStr[i];
}
for (int i = 0; i<4; i++) {
ipAddr[i] = parts[i].toInt();
if ((ipAddr[i] < 0) || (ipAddr[i] > 255)) return false;
}
for (int i = 0; i<4; i++) {
ip[i] = (uint8_t)ipAddr[i];
}
return true;
}
void strToColor(String str, uint8_t* buf) {
str.replace(":", "");
str.replace("0x", "");

View File

@@ -10,17 +10,21 @@ extern bool eqls(String str, const char* keywordPtr);
extern String b2s(bool input);
extern String b2a(bool input);
extern bool s2b(String input);
extern void prnt(String s);
extern void prnt(bool b);
extern void prnt(char c);
extern void prnt(const String s);
extern void prnt(const bool b);
extern void prnt(const char c);
extern void prnt(const char* ptr);
extern void prnt(int i);
extern void prnt(const char* ptr, int len);
extern void prnt(const int i);
extern void prnt(const uint32_t i);
extern void prntln();
extern void prntln(String s);
extern void prntln(bool b);
extern void prntln(char c);
extern void prntln(const String s);
extern void prntln(const bool b);
extern void prntln(const char c);
extern void prntln(const char* ptr);
extern void prntln(int i);
extern void prntln(const char* ptr, int len);
extern void prntln(const int i);
extern void prntln(const uint32_t i);
/*
The following variables are the strings used for the serial interface, display interface and settings.