CommandLine.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "CommandLine.h"
  2. CommandLine::CommandLine() {
  3. }
  4. void CommandLine::RunSetup() {
  5. Serial.println(this->ascii_art);
  6. Serial.println(F("\n\n--------------------------------\n"));
  7. Serial.println(F(" ESP32 Marauder \n"));
  8. Serial.println(" " + version_number + "\n");
  9. Serial.println(F(" By: justcallmekoko\n"));
  10. Serial.println(F("--------------------------------\n\n"));
  11. Serial.print("> ");
  12. }
  13. String CommandLine::getSerialInput() {
  14. String input = "";
  15. if (Serial.available() > 0)
  16. input = Serial.readStringUntil('\n');
  17. input.trim();
  18. return input;
  19. }
  20. void CommandLine::main(uint32_t currentTime) {
  21. String input = this->getSerialInput();
  22. this->runCommand(input);
  23. if (input != "")
  24. Serial.print("> ");
  25. }
  26. LinkedList<String> CommandLine::parseCommand(String input, char* delim) {
  27. LinkedList<String> cmd_args;
  28. if (input != "") {
  29. //char delim[] = " ";
  30. char fancy[input.length() + 1] = {};
  31. input.toCharArray(fancy, input.length() + 1);
  32. char* ptr = strtok(fancy, delim);
  33. while (ptr != NULL) {
  34. cmd_args.add(String(ptr));
  35. ptr = strtok(NULL, delim);
  36. }
  37. //memset(fancy, 0, sizeof(fancy));
  38. // Display the segmented cmd
  39. //for (int i = 0; i < cmd_args.size(); i++) {
  40. // Serial.println(cmd_args.get(i));
  41. //}
  42. }
  43. return cmd_args;
  44. }
  45. int CommandLine::argSearch(LinkedList<String>* cmd_args_list, String key) {
  46. for (int i = 0; i < cmd_args_list->size(); i++) {
  47. if (cmd_args_list->get(i) == key)
  48. return i;
  49. }
  50. return -1;
  51. }
  52. void CommandLine::runCommand(String input) {
  53. if (input != "")
  54. Serial.println("#" + input);
  55. else
  56. return;
  57. LinkedList<String> cmd_args = this->parseCommand(input, " ");
  58. //// Admin commands
  59. // Stop Scan
  60. if (cmd_args.get(0) == STOPSCAN_CMD) {
  61. wifi_scan_obj.StartScan(WIFI_SCAN_OFF);
  62. // If we don't do this, the text and button coordinates will be off
  63. #ifdef HAS_SCREEN
  64. display_obj.tft.init();
  65. menu_function_obj.changeMenu(menu_function_obj.current_menu);
  66. #endif
  67. }
  68. // Channel command
  69. else if (cmd_args.get(0) == CH_CMD) {
  70. // Search for channel set arg
  71. int ch_set = this->argSearch(&cmd_args, "-s");
  72. if (cmd_args.size() == 1) {
  73. Serial.println("Current channel: " + (String)wifi_scan_obj.set_channel);
  74. }
  75. else if (ch_set != -1) {
  76. wifi_scan_obj.set_channel = cmd_args.get(ch_set + 1).toInt();
  77. wifi_scan_obj.changeChannel();
  78. Serial.println("Set channel: " + (String)wifi_scan_obj.set_channel);
  79. }
  80. }
  81. // Clear APs
  82. else if (cmd_args.get(0) == CLEARAP_CMD) {
  83. wifi_scan_obj.RunClearAPs();
  84. }
  85. else if (cmd_args.get(0) == REBOOT_CMD) {
  86. ESP.restart();
  87. }
  88. //// WiFi Scan commands
  89. if (!wifi_scan_obj.scanning()) {
  90. // AP Scan
  91. if (cmd_args.get(0) == SCANAP_CMD) {
  92. #ifdef HAS_SCREEN
  93. display_obj.clearScreen();
  94. menu_function_obj.drawStatusBar();
  95. #endif
  96. wifi_scan_obj.StartScan(WIFI_SCAN_TARGET_AP, TFT_MAGENTA);
  97. }
  98. // Beacon sniff
  99. else if (cmd_args.get(0) == SNIFF_BEACON_CMD) {
  100. #ifdef HAS_SCREEN
  101. display_obj.clearScreen();
  102. menu_function_obj.drawStatusBar();
  103. #endif
  104. wifi_scan_obj.StartScan(WIFI_SCAN_AP, TFT_MAGENTA);
  105. }
  106. // Deauth sniff
  107. else if (cmd_args.get(0) == SNIFF_DEAUTH_CMD) {
  108. #ifdef HAS_SCREEN
  109. display_obj.clearScreen();
  110. menu_function_obj.drawStatusBar();
  111. #endif
  112. wifi_scan_obj.StartScan(WIFI_SCAN_DEAUTH, TFT_RED);
  113. }
  114. // PMKID sniff
  115. else if (cmd_args.get(0) == SNIFF_PMKID_CMD) {
  116. wifi_scan_obj.StartScan(WIFI_SCAN_EAPOL, TFT_VIOLET);
  117. }
  118. }
  119. //// WiFi aux commands
  120. // List access points
  121. if (cmd_args.get(0) == LIST_AP_CMD) {
  122. for (int i = 0; i < access_points->size(); i++) {
  123. if (access_points->get(i).selected)
  124. Serial.println("[" + (String)i + "] " + access_points->get(i).essid + " (selected)");
  125. else
  126. Serial.println("[" + (String)i + "] " + access_points->get(i).essid);
  127. }
  128. }
  129. // Select access points or stations
  130. else if (cmd_args.get(0) == SEL_CMD) {
  131. // Get switches
  132. int ap_sw = this->argSearch(&cmd_args, "-a");
  133. int st_sw = this->argSearch(&cmd_args, "-s");
  134. // Access points
  135. if (ap_sw != -1) {
  136. // Get list of indices
  137. LinkedList<String> ap_index = this->parseCommand(cmd_args.get(ap_sw + 1), ",");
  138. // Mark APs as selected
  139. for (int i = 0; i < ap_index.size(); i++) {
  140. int index = ap_index.get(i).toInt();
  141. if (access_points->get(index).selected) {
  142. // Unselect "selected" ap
  143. AccessPoint new_ap = access_points->get(index);
  144. new_ap.selected = false;
  145. access_points->set(index, new_ap);
  146. }
  147. else {
  148. // Select "unselected" ap
  149. AccessPoint new_ap = access_points->get(index);
  150. new_ap.selected = true;
  151. access_points->set(index, new_ap);
  152. }
  153. }
  154. }
  155. // Stations
  156. else if (st_sw != -1) {
  157. }
  158. }
  159. }