wifi_marauder_script.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. #include "../wifi_marauder_app_i.h"
  2. #include "wifi_marauder_script.h"
  3. #define WIFI_MARAUDER_DEFAULT_TIMEOUT_SCAN 15
  4. #define WIFI_MARAUDER_DEFAULT_TIMEOUT_DEAUTH 30
  5. #define WIFI_MARAUDER_DEFAULT_TIMEOUT_PROBE 60
  6. #define WIFI_MARAUDER_DEFAULT_TIMEOUT_SNIFF 60
  7. #define WIFI_MARAUDER_DEFAULT_TIMEOUT_BEACON 60
  8. WifiMarauderScript *wifi_marauder_script_alloc() {
  9. WifiMarauderScript *script = (WifiMarauderScript *) malloc(sizeof(WifiMarauderScript));
  10. if (script == NULL) {
  11. return NULL;
  12. }
  13. script->name = NULL;
  14. script->description = NULL;
  15. script->first_stage = NULL;
  16. script->last_stage = NULL;
  17. script->enable_led = WifiMarauderScriptBooleanUndefined;
  18. script->save_pcap = WifiMarauderScriptBooleanUndefined;
  19. script->repeat = 1;
  20. return script;
  21. }
  22. WifiMarauderScript* wifi_marauder_script_create(const char* script_name) {
  23. WifiMarauderScript *script = wifi_marauder_script_alloc();
  24. script->name = strdup(script_name);
  25. return script;
  26. }
  27. void _wifi_marauder_script_load_meta(WifiMarauderScript *script, cJSON *meta_section) {
  28. if (meta_section != NULL) {
  29. // Script description
  30. cJSON* description = cJSON_GetObjectItem(meta_section, "description");
  31. if (description != NULL) {
  32. script->description = strdup(description->valuestring);
  33. }
  34. // Enable LED
  35. cJSON* enable_led_json = cJSON_GetObjectItemCaseSensitive(meta_section, "enableLed");
  36. if (cJSON_IsBool(enable_led_json)) {
  37. script->enable_led = enable_led_json->valueint;
  38. }
  39. // Save PCAP
  40. cJSON* save_pcap_json = cJSON_GetObjectItemCaseSensitive(meta_section, "savePcap");
  41. if (cJSON_IsBool(save_pcap_json)) {
  42. script->save_pcap = save_pcap_json->valueint;
  43. }
  44. // Times the script will be repeated
  45. cJSON* repeat = cJSON_GetObjectItem(meta_section, "repeat");
  46. if (repeat != NULL) {
  47. script->repeat = repeat->valueint;
  48. }
  49. }
  50. if (script->description == NULL) {
  51. script->description = strdup("My script");
  52. }
  53. }
  54. WifiMarauderScriptStageScan* _wifi_marauder_script_get_stage_scan(cJSON *stages) {
  55. cJSON* stage_scan = cJSON_GetObjectItem(stages, "scan");
  56. if (stage_scan == NULL) {
  57. return NULL;
  58. }
  59. cJSON* type = cJSON_GetObjectItem(stage_scan, "type");
  60. if (type == NULL) {
  61. return NULL;
  62. }
  63. WifiMarauderScriptScanType scan_type;
  64. if (strcmp(type->valuestring, "ap") == 0) {
  65. scan_type = WifiMarauderScriptScanTypeAp;
  66. } else if (strcmp(type->valuestring, "station") == 0) {
  67. scan_type = WifiMarauderScriptScanTypeStation;
  68. } else {
  69. return NULL;
  70. }
  71. cJSON* channel = cJSON_GetObjectItem(stage_scan, "channel");
  72. int scan_channel = channel != NULL ? (int)cJSON_GetNumberValue(channel) : 0;
  73. cJSON* timeout = cJSON_GetObjectItem(stage_scan, "timeout");
  74. int scan_timeout = timeout != NULL ? (int)cJSON_GetNumberValue(timeout) : WIFI_MARAUDER_DEFAULT_TIMEOUT_SCAN;
  75. WifiMarauderScriptStageScan *scan_stage = (WifiMarauderScriptStageScan*) malloc(sizeof(WifiMarauderScriptStageScan));
  76. scan_stage->type = scan_type;
  77. scan_stage->channel = scan_channel;
  78. scan_stage->timeout = scan_timeout;
  79. return scan_stage;
  80. }
  81. WifiMarauderScriptStageSelect* _wifi_marauder_script_get_stage_select(cJSON *stages) {
  82. cJSON *select_stage_json = cJSON_GetObjectItemCaseSensitive(stages, "select");
  83. if (select_stage_json == NULL) {
  84. return NULL;
  85. }
  86. cJSON *type_json = cJSON_GetObjectItemCaseSensitive(select_stage_json, "type");
  87. cJSON *filter_json = cJSON_GetObjectItemCaseSensitive(select_stage_json, "filter");
  88. cJSON *indexes_json = cJSON_GetObjectItemCaseSensitive(select_stage_json, "indexes");
  89. cJSON *index_json = cJSON_GetObjectItemCaseSensitive(select_stage_json, "index");
  90. cJSON *allow_repeat_json = cJSON_GetObjectItemCaseSensitive(select_stage_json, "allow_repeat");
  91. if (!cJSON_IsString(type_json) || !cJSON_IsString(filter_json)) {
  92. return NULL;
  93. }
  94. WifiMarauderScriptSelectType select_type;
  95. if (strcmp(type_json->valuestring, "ap") == 0) {
  96. select_type = WifiMarauderScriptSelectTypeAp;
  97. } else if (strcmp(type_json->valuestring, "station") == 0) {
  98. select_type = WifiMarauderScriptSelectTypeStation;
  99. } else if (strcmp(type_json->valuestring, "ssid") == 0) {
  100. select_type = WifiMarauderScriptSelectTypeSsid;
  101. } else {
  102. return NULL;
  103. }
  104. char *filter_str = strdup(filter_json->valuestring);
  105. WifiMarauderScriptStageSelect *stage_select = (WifiMarauderScriptStageSelect*) malloc(sizeof(WifiMarauderScriptStageSelect));
  106. stage_select->type = select_type;
  107. stage_select->filter = filter_str;
  108. stage_select->allow_repeat = cJSON_IsBool(allow_repeat_json) ? allow_repeat_json->valueint : true;
  109. if (cJSON_IsNumber(index_json)) {
  110. int* indexes = (int*) malloc(sizeof(int));
  111. indexes[0] = index_json->valueint;
  112. stage_select->indexes = indexes;
  113. } else if (cJSON_IsArray(indexes_json)) {
  114. int indexes_size = cJSON_GetArraySize(indexes_json);
  115. int* indexes = (int*) malloc(indexes_size * sizeof(int));
  116. for (int i = 0; i < indexes_size; i++) {
  117. cJSON *index_item = cJSON_GetArrayItem(indexes_json, i);
  118. if (cJSON_IsNumber(index_item)) {
  119. indexes[i] = index_item->valueint;
  120. }
  121. }
  122. stage_select->indexes = indexes;
  123. } else {
  124. stage_select->indexes = NULL;
  125. }
  126. return stage_select;
  127. }
  128. WifiMarauderScriptStageDeauth* _wifi_marauder_script_get_stage_deauth(cJSON *stages) {
  129. cJSON *deauth_stage_json = cJSON_GetObjectItemCaseSensitive(stages, "deauth");
  130. if (deauth_stage_json == NULL) {
  131. return NULL;
  132. }
  133. cJSON* timeout = cJSON_GetObjectItem(deauth_stage_json, "timeout");
  134. int deauth_timeout = timeout != NULL ? (int)cJSON_GetNumberValue(timeout) : WIFI_MARAUDER_DEFAULT_TIMEOUT_DEAUTH;
  135. WifiMarauderScriptStageDeauth *deauth_stage = (WifiMarauderScriptStageDeauth*) malloc(sizeof(WifiMarauderScriptStageDeauth));
  136. deauth_stage->timeout = deauth_timeout;
  137. return deauth_stage;
  138. }
  139. WifiMarauderScriptStageProbe* _wifi_marauder_script_get_stage_probe(cJSON *stages) {
  140. cJSON *probe_stage_json = cJSON_GetObjectItemCaseSensitive(stages, "probe");
  141. if (probe_stage_json == NULL) {
  142. return NULL;
  143. }
  144. cJSON* timeout = cJSON_GetObjectItem(probe_stage_json, "timeout");
  145. int probe_timeout = timeout != NULL ? (int)cJSON_GetNumberValue(timeout) : WIFI_MARAUDER_DEFAULT_TIMEOUT_PROBE;
  146. WifiMarauderScriptStageProbe *probe_stage = (WifiMarauderScriptStageProbe*) malloc(sizeof(WifiMarauderScriptStageProbe));
  147. probe_stage->timeout = probe_timeout;
  148. return probe_stage;
  149. }
  150. WifiMarauderScriptStageSniffRaw* _wifi_marauder_script_get_stage_sniff_raw(cJSON *stages) {
  151. cJSON* sniffraw_stage_json = cJSON_GetObjectItem(stages, "sniffraw");
  152. if (sniffraw_stage_json == NULL) {
  153. return NULL;
  154. }
  155. cJSON* timeout_json = cJSON_GetObjectItem(sniffraw_stage_json, "timeout");
  156. int timeout = timeout_json != NULL ? (int)cJSON_GetNumberValue(timeout_json) : WIFI_MARAUDER_DEFAULT_TIMEOUT_SNIFF;
  157. WifiMarauderScriptStageSniffRaw *sniff_raw_stage = (WifiMarauderScriptStageSniffRaw*) malloc(sizeof(WifiMarauderScriptStageSniffRaw));
  158. sniff_raw_stage->timeout = timeout;
  159. return sniff_raw_stage;
  160. }
  161. WifiMarauderScriptStageSniffBeacon* _wifi_marauder_script_get_stage_sniff_beacon(cJSON *stages) {
  162. cJSON* sniffbeacon_stage_json = cJSON_GetObjectItem(stages, "sniffbeacon");
  163. if (sniffbeacon_stage_json == NULL) {
  164. return NULL;
  165. }
  166. cJSON* timeout_json = cJSON_GetObjectItem(sniffbeacon_stage_json, "timeout");
  167. int timeout = timeout_json != NULL ? (int)cJSON_GetNumberValue(timeout_json) : WIFI_MARAUDER_DEFAULT_TIMEOUT_SNIFF;
  168. WifiMarauderScriptStageSniffBeacon *sniff_beacon_stage = (WifiMarauderScriptStageSniffBeacon*) malloc(sizeof(WifiMarauderScriptStageSniffBeacon));
  169. sniff_beacon_stage->timeout = timeout;
  170. return sniff_beacon_stage;
  171. }
  172. WifiMarauderScriptStageSniffDeauth* _wifi_marauder_script_get_stage_sniff_deauth(cJSON *stages) {
  173. cJSON* sniffdeauth_stage_json = cJSON_GetObjectItem(stages, "sniffdeauth");
  174. if (sniffdeauth_stage_json == NULL) {
  175. return NULL;
  176. }
  177. cJSON* timeout_json = cJSON_GetObjectItem(sniffdeauth_stage_json, "timeout");
  178. int timeout = timeout_json != NULL ? (int)cJSON_GetNumberValue(timeout_json) : WIFI_MARAUDER_DEFAULT_TIMEOUT_SNIFF;
  179. WifiMarauderScriptStageSniffDeauth *sniff_deauth_stage = (WifiMarauderScriptStageSniffDeauth*) malloc(sizeof(WifiMarauderScriptStageSniffDeauth));
  180. sniff_deauth_stage->timeout = timeout;
  181. return sniff_deauth_stage;
  182. }
  183. WifiMarauderScriptStageSniffEsp* _wifi_marauder_script_get_stage_sniff_esp(cJSON *stages) {
  184. cJSON* sniffesp_stage_json = cJSON_GetObjectItem(stages, "sniffesp");
  185. if (sniffesp_stage_json == NULL) {
  186. return NULL;
  187. }
  188. cJSON* timeout_json = cJSON_GetObjectItem(sniffesp_stage_json, "timeout");
  189. int timeout = timeout_json != NULL ? (int)cJSON_GetNumberValue(timeout_json) : WIFI_MARAUDER_DEFAULT_TIMEOUT_SNIFF;
  190. WifiMarauderScriptStageSniffEsp *sniff_esp_stage = (WifiMarauderScriptStageSniffEsp*) malloc(sizeof(WifiMarauderScriptStageSniffEsp));
  191. sniff_esp_stage->timeout = timeout;
  192. return sniff_esp_stage;
  193. }
  194. WifiMarauderScriptStageSniffPmkid* _wifi_marauder_script_get_stage_sniff_pmkid(cJSON *stages) {
  195. cJSON* sniffpmkid_stage_json = cJSON_GetObjectItem(stages, "sniffpmkid");
  196. if (sniffpmkid_stage_json == NULL) {
  197. return NULL;
  198. }
  199. cJSON* channel_json = cJSON_GetObjectItem(sniffpmkid_stage_json, "channel");
  200. int channel = channel_json != NULL ? (int)cJSON_GetNumberValue(channel_json) : 0;
  201. cJSON* timeout_json = cJSON_GetObjectItem(sniffpmkid_stage_json, "timeout");
  202. int timeout = timeout_json != NULL ? (int)cJSON_GetNumberValue(timeout_json) : WIFI_MARAUDER_DEFAULT_TIMEOUT_SNIFF;
  203. cJSON* force_deauth_json = cJSON_GetObjectItemCaseSensitive(sniffpmkid_stage_json, "forceDeauth");
  204. bool force_deauth = cJSON_IsBool(force_deauth_json) ? force_deauth_json->valueint : true;
  205. WifiMarauderScriptStageSniffPmkid *sniff_pmkid_stage = (WifiMarauderScriptStageSniffPmkid*) malloc(sizeof(WifiMarauderScriptStageSniffPmkid));
  206. sniff_pmkid_stage->channel = channel;
  207. sniff_pmkid_stage->timeout = timeout;
  208. sniff_pmkid_stage->force_deauth = force_deauth;
  209. return sniff_pmkid_stage;
  210. }
  211. WifiMarauderScriptStageSniffPwn* _wifi_marauder_script_get_stage_sniff_pwn(cJSON *stages) {
  212. cJSON* sniffpwn_stage_json = cJSON_GetObjectItem(stages, "sniffpwn");
  213. if (sniffpwn_stage_json == NULL) {
  214. return NULL;
  215. }
  216. cJSON* timeout_json = cJSON_GetObjectItem(sniffpwn_stage_json, "timeout");
  217. int timeout = timeout_json != NULL ? (int)cJSON_GetNumberValue(timeout_json) : WIFI_MARAUDER_DEFAULT_TIMEOUT_SNIFF;
  218. WifiMarauderScriptStageSniffPwn *sniff_pwn_stage = (WifiMarauderScriptStageSniffPwn*) malloc(sizeof(WifiMarauderScriptStageSniffPwn));
  219. sniff_pwn_stage->timeout = timeout;
  220. return sniff_pwn_stage;
  221. }
  222. WifiMarauderScriptStageBeaconList* _wifi_marauder_script_get_stage_beacon_list(cJSON *stages) {
  223. cJSON* stage_beaconlist = cJSON_GetObjectItem(stages, "beaconList");
  224. if (stage_beaconlist == NULL) {
  225. return NULL;
  226. }
  227. WifiMarauderScriptStageBeaconList *beaconlist_stage = (WifiMarauderScriptStageBeaconList*) malloc(sizeof(WifiMarauderScriptStageBeaconList));
  228. if (beaconlist_stage == NULL) {
  229. return NULL;
  230. }
  231. cJSON* ssids = cJSON_GetObjectItem(stage_beaconlist, "ssids");
  232. if (ssids == NULL) {
  233. return NULL;
  234. }
  235. // SSID count
  236. int ssid_count = cJSON_GetArraySize(ssids);
  237. if (ssid_count == 0) {
  238. return NULL;
  239. }
  240. beaconlist_stage->ssid_count = ssid_count;
  241. // SSIDs
  242. beaconlist_stage->ssids = (char**) malloc(sizeof(char*) * ssid_count);
  243. if (beaconlist_stage->ssids == NULL) {
  244. return NULL;
  245. }
  246. for (int i = 0; i < ssid_count; i++) {
  247. cJSON* ssid = cJSON_GetArrayItem(ssids, i);
  248. if (ssid == NULL) {
  249. continue;
  250. }
  251. char* ssid_string = cJSON_GetStringValue(ssid);
  252. if (ssid_string == NULL) {
  253. continue;
  254. }
  255. beaconlist_stage->ssids[i] = (char*) malloc(sizeof(char) * (strlen(ssid_string) + 1));
  256. strcpy(beaconlist_stage->ssids[i], ssid_string);
  257. }
  258. // Timeout
  259. cJSON* timeout = cJSON_GetObjectItem(stage_beaconlist, "timeout");
  260. beaconlist_stage->timeout = timeout != NULL ? (int)cJSON_GetNumberValue(timeout) : WIFI_MARAUDER_DEFAULT_TIMEOUT_BEACON;
  261. // Random SSIDs
  262. cJSON* random_ssids = cJSON_GetObjectItem(stage_beaconlist, "generate");
  263. beaconlist_stage->random_ssids = random_ssids != NULL ? (int)cJSON_GetNumberValue(random_ssids) : 0;
  264. return beaconlist_stage;
  265. }
  266. WifiMarauderScriptStageBeaconAp* _wifi_marauder_script_get_stage_beacon_ap(cJSON *stages) {
  267. cJSON* beaconap_stage_json = cJSON_GetObjectItem(stages, "beaconAp");
  268. if (beaconap_stage_json == NULL) {
  269. return NULL;
  270. }
  271. cJSON* timeout_json = cJSON_GetObjectItem(beaconap_stage_json, "timeout");
  272. int timeout = timeout_json != NULL ? (int)cJSON_GetNumberValue(timeout_json) : WIFI_MARAUDER_DEFAULT_TIMEOUT_BEACON;
  273. WifiMarauderScriptStageBeaconAp *beacon_ap_stage = (WifiMarauderScriptStageBeaconAp*) malloc(sizeof(WifiMarauderScriptStageBeaconAp));
  274. beacon_ap_stage->timeout = timeout;
  275. return beacon_ap_stage;
  276. }
  277. WifiMarauderScriptStage* _wifi_marauder_script_create_stage(WifiMarauderScriptStageType type, void* stage_data) {
  278. WifiMarauderScriptStage* stage = (WifiMarauderScriptStage*) malloc(sizeof(WifiMarauderScriptStage));
  279. stage->type = type;
  280. stage->stage = stage_data;
  281. stage->next_stage = NULL;
  282. return stage;
  283. }
  284. void wifi_marauder_script_add_stage(WifiMarauderScript *script, WifiMarauderScriptStageType stage_type, void* stage_data) {
  285. if (script == NULL || stage_data == NULL) {
  286. return;
  287. }
  288. WifiMarauderScriptStage* stage = _wifi_marauder_script_create_stage(stage_type, stage_data);
  289. if (script->last_stage != NULL) {
  290. script->last_stage->next_stage = stage;
  291. } else {
  292. script->first_stage = stage;
  293. }
  294. script->last_stage = stage;
  295. }
  296. void _wifi_marauder_script_load_stages(WifiMarauderScript *script, cJSON *stages) {
  297. // Scan stage
  298. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeScan, _wifi_marauder_script_get_stage_scan(stages));
  299. // Select stage
  300. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeSelect, _wifi_marauder_script_get_stage_select(stages));
  301. // Deauth stage
  302. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeDeauth, _wifi_marauder_script_get_stage_deauth(stages));
  303. // Probe stage
  304. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeProbe, _wifi_marauder_script_get_stage_probe(stages));
  305. // Sniff raw stage
  306. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeSniffRaw, _wifi_marauder_script_get_stage_sniff_raw(stages));
  307. // Sniff beacon stage
  308. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeSniffBeacon, _wifi_marauder_script_get_stage_sniff_beacon(stages));
  309. // Sniff deauth stage
  310. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeSniffDeauth, _wifi_marauder_script_get_stage_sniff_deauth(stages));
  311. // Sniff esp stage
  312. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeSniffEsp, _wifi_marauder_script_get_stage_sniff_esp(stages));
  313. // Sniff PMKID stage
  314. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeSniffPmkid, _wifi_marauder_script_get_stage_sniff_pmkid(stages));
  315. // Sniff pwn stage
  316. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeSniffPwn, _wifi_marauder_script_get_stage_sniff_pwn(stages));
  317. // Beacon List stage
  318. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeBeaconList, _wifi_marauder_script_get_stage_beacon_list(stages));
  319. // Beacon Ap stage
  320. wifi_marauder_script_add_stage(script, WifiMarauderScriptStageTypeBeaconAp, _wifi_marauder_script_get_stage_beacon_ap(stages));
  321. }
  322. WifiMarauderScript *wifi_marauder_script_parse_raw(const char* json_raw) {
  323. WifiMarauderScript *script = wifi_marauder_script_alloc();
  324. if (script == NULL) {
  325. return NULL;
  326. }
  327. cJSON* json = cJSON_Parse(json_raw);
  328. if (json == NULL) {
  329. return NULL;
  330. }
  331. cJSON* meta = cJSON_GetObjectItem(json, "meta");
  332. _wifi_marauder_script_load_meta(script, meta);
  333. cJSON* stages = cJSON_GetObjectItem(json, "stages");
  334. if (cJSON_IsArray(stages)) {
  335. cJSON* stage_item = NULL;
  336. cJSON_ArrayForEach(stage_item, stages) {
  337. _wifi_marauder_script_load_stages(script, stage_item);
  338. }
  339. } else {
  340. _wifi_marauder_script_load_stages(script, stages);
  341. }
  342. return script;
  343. }
  344. WifiMarauderScript *wifi_marauder_script_parse_json(Storage* storage, const char* file_path) {
  345. WifiMarauderScript *script = NULL;
  346. File* script_file = storage_file_alloc(storage);
  347. if (storage_file_open(script_file, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  348. uint32_t file_size = storage_file_size(script_file);
  349. char* json_buffer = (char*)malloc(file_size + 1);
  350. uint16_t bytes_read = storage_file_read(script_file, json_buffer, file_size);
  351. json_buffer[bytes_read] = '\0';
  352. script = wifi_marauder_script_parse_raw(json_buffer);
  353. if (script != NULL) {
  354. // Set script name
  355. FuriString* script_name = furi_string_alloc();
  356. path_extract_filename_no_ext(file_path, script_name);
  357. script->name = strdup(furi_string_get_cstr(script_name));
  358. furi_string_free(script_name);
  359. }
  360. storage_file_close(script_file);
  361. }
  362. storage_file_free(script_file);
  363. return script;
  364. }
  365. cJSON* _wifi_marauder_script_create_json_meta(WifiMarauderScript *script) {
  366. cJSON* meta_json = cJSON_CreateObject();
  367. if (script->description != NULL) {
  368. cJSON_AddStringToObject(meta_json, "description", script->description);
  369. } else {
  370. cJSON_AddStringToObject(meta_json, "description", "My Script");
  371. }
  372. if (script->enable_led != WifiMarauderScriptBooleanUndefined) {
  373. cJSON_AddBoolToObject(meta_json, "enable_led", (script->enable_led == WifiMarauderScriptBooleanTrue));
  374. }
  375. if (script->save_pcap != WifiMarauderScriptBooleanUndefined) {
  376. cJSON_AddBoolToObject(meta_json, "save_pcap", (script->save_pcap == WifiMarauderScriptBooleanTrue));
  377. }
  378. cJSON_AddNumberToObject(meta_json, "repeat", script->repeat);
  379. return meta_json;
  380. }
  381. cJSON* _wifi_marauder_script_create_json_scan(WifiMarauderScriptStageScan* scan_stage) {
  382. cJSON* stage_json = cJSON_CreateObject();
  383. cJSON_AddItemToObject(stage_json, "scan", cJSON_CreateObject());
  384. cJSON* scan_json = cJSON_GetObjectItem(stage_json, "scan");
  385. // Scan type
  386. cJSON_AddStringToObject(scan_json, "type", scan_stage->type == WifiMarauderScriptScanTypeAp ? "ap" : "station");
  387. // Channel
  388. if (scan_stage->channel > 0) {
  389. cJSON_AddNumberToObject(scan_json, "channel", scan_stage->channel);
  390. }
  391. // Timeout
  392. if (scan_stage->timeout > 0) {
  393. cJSON_AddNumberToObject(scan_json, "timeout", scan_stage->timeout);
  394. }
  395. return stage_json;
  396. }
  397. cJSON* _wifi_marauder_script_create_json_select(WifiMarauderScriptStageSelect* select_stage) {
  398. cJSON* stage_json = cJSON_CreateObject();
  399. cJSON_AddItemToObject(stage_json, "select", cJSON_CreateObject());
  400. cJSON* select_json = cJSON_GetObjectItem(stage_json, "select");
  401. // Select type
  402. cJSON_AddStringToObject(select_json, "type", select_stage->type == WifiMarauderScriptSelectTypeAp ? "ap" : select_stage->type == WifiMarauderScriptSelectTypeStation ? "station" : "ssid");
  403. if (select_stage->filter != NULL) {
  404. cJSON_AddStringToObject(select_json, "filter", select_stage->filter);
  405. }
  406. // Indexes
  407. if (select_stage->indexes != NULL) {
  408. cJSON* indexes_json = cJSON_CreateArray();
  409. int* index_ptr = select_stage->indexes;
  410. while (*index_ptr != -1) {
  411. cJSON_AddItemToArray(indexes_json, cJSON_CreateNumber(*index_ptr));
  412. index_ptr++;
  413. }
  414. if (cJSON_GetArraySize(indexes_json) > 0) {
  415. cJSON_AddItemToObject(select_json, "indexes", indexes_json);
  416. }
  417. }
  418. return stage_json;
  419. }
  420. cJSON* _wifi_marauder_script_create_json_deauth(WifiMarauderScriptStageDeauth* deauth_stage) {
  421. cJSON* stage_json = cJSON_CreateObject();
  422. cJSON_AddItemToObject(stage_json, "deauth", cJSON_CreateObject());
  423. cJSON* deauth_json = cJSON_GetObjectItem(stage_json, "deauth");
  424. // Timeout
  425. if (deauth_stage->timeout > 0) {
  426. cJSON_AddNumberToObject(deauth_json, "timeout", deauth_stage->timeout);
  427. }
  428. return stage_json;
  429. }
  430. cJSON* _wifi_marauder_script_create_json_probe(WifiMarauderScriptStageProbe* probe_stage) {
  431. cJSON* stage_json = cJSON_CreateObject();
  432. cJSON_AddItemToObject(stage_json, "probe", cJSON_CreateObject());
  433. cJSON* probe_json = cJSON_GetObjectItem(stage_json, "probe");
  434. // Timeout
  435. if (probe_stage->timeout > 0) {
  436. cJSON_AddNumberToObject(probe_json, "timeout", probe_stage->timeout);
  437. }
  438. return stage_json;
  439. }
  440. cJSON* _wifi_marauder_script_create_json_sniffraw(WifiMarauderScriptStageSniffRaw* sniffraw_stage) {
  441. cJSON* stage_json = cJSON_CreateObject();
  442. cJSON_AddItemToObject(stage_json, "sniffRaw", cJSON_CreateObject());
  443. cJSON* sniffraw_json = cJSON_GetObjectItem(stage_json, "sniffRaw");
  444. // Timeout
  445. if (sniffraw_stage->timeout > 0) {
  446. cJSON_AddNumberToObject(sniffraw_json, "timeout", sniffraw_stage->timeout);
  447. }
  448. return stage_json;
  449. }
  450. cJSON* _wifi_marauder_script_create_json_sniffbeacon(WifiMarauderScriptStageSniffBeacon* sniffbeacon_stage) {
  451. cJSON* stage_json = cJSON_CreateObject();
  452. cJSON_AddItemToObject(stage_json, "sniffBeacon", cJSON_CreateObject());
  453. cJSON* sniffbeacon_json = cJSON_GetObjectItem(stage_json, "sniffBeacon");
  454. // Timeout
  455. if (sniffbeacon_stage->timeout > 0) {
  456. cJSON_AddNumberToObject(sniffbeacon_json, "timeout", sniffbeacon_stage->timeout);
  457. }
  458. return stage_json;
  459. }
  460. cJSON* _wifi_marauder_script_create_json_sniffdeauth(WifiMarauderScriptStageSniffDeauth* sniffdeauth_stage) {
  461. cJSON* stage_json = cJSON_CreateObject();
  462. cJSON_AddItemToObject(stage_json, "sniffDeauth", cJSON_CreateObject());
  463. cJSON* sniffdeauth_json = cJSON_GetObjectItem(stage_json, "sniffDeauth");
  464. // Timeout
  465. if (sniffdeauth_stage->timeout > 0) {
  466. cJSON_AddNumberToObject(sniffdeauth_json, "timeout", sniffdeauth_stage->timeout);
  467. }
  468. return stage_json;
  469. }
  470. cJSON* _wifi_marauder_script_create_json_sniffesp(WifiMarauderScriptStageSniffEsp* sniffesp_stage) {
  471. cJSON* stage_json = cJSON_CreateObject();
  472. cJSON_AddItemToObject(stage_json, "sniffEsp", cJSON_CreateObject());
  473. cJSON* sniffesp_json = cJSON_GetObjectItem(stage_json, "sniffEsp");
  474. // Timeout
  475. if (sniffesp_stage->timeout > 0) {
  476. cJSON_AddNumberToObject(sniffesp_json, "timeout", sniffesp_stage->timeout);
  477. }
  478. return stage_json;
  479. }
  480. cJSON* _wifi_marauder_script_create_json_sniffpmkid(WifiMarauderScriptStageSniffPmkid* sniffpmkid_stage) {
  481. cJSON* stage_json = cJSON_CreateObject();
  482. cJSON_AddItemToObject(stage_json, "sniffPmkid", cJSON_CreateObject());
  483. cJSON* sniffpmkid_json = cJSON_GetObjectItem(stage_json, "sniffPmkid");
  484. // Force deauth
  485. cJSON_AddBoolToObject(sniffpmkid_json, "forceDeauth", sniffpmkid_stage->force_deauth);
  486. // Channel
  487. if (sniffpmkid_stage->channel > 0) {
  488. cJSON_AddNumberToObject(sniffpmkid_json, "channel", sniffpmkid_stage->channel);
  489. }
  490. // Timeout
  491. if (sniffpmkid_stage->timeout > 0) {
  492. cJSON_AddNumberToObject(sniffpmkid_json, "timeout", sniffpmkid_stage->timeout);
  493. }
  494. return stage_json;
  495. }
  496. cJSON* _wifi_marauder_script_create_json_sniffpwn(WifiMarauderScriptStageSniffPwn* sniffpwn_stage) {
  497. cJSON* stage_json = cJSON_CreateObject();
  498. cJSON_AddItemToObject(stage_json, "sniffPwn", cJSON_CreateObject());
  499. cJSON* sniffpwn_json = cJSON_GetObjectItem(stage_json, "sniffPwn");
  500. // Timeout
  501. if (sniffpwn_stage->timeout > 0) {
  502. cJSON_AddNumberToObject(sniffpwn_json, "timeout", sniffpwn_stage->timeout);
  503. }
  504. return stage_json;
  505. }
  506. cJSON* _wifi_marauder_script_create_json_beaconlist(WifiMarauderScriptStageBeaconList* beaconlist_stage) {
  507. cJSON* stage_json = cJSON_CreateObject();
  508. cJSON_AddItemToObject(stage_json, "beaconList", cJSON_CreateObject());
  509. cJSON* beaconlist_json = cJSON_GetObjectItem(stage_json, "beaconList");
  510. // SSIDs
  511. if (beaconlist_stage->ssids != NULL) {
  512. cJSON* ssids_json = cJSON_CreateStringArray((const char**)beaconlist_stage->ssids, beaconlist_stage->ssid_count);
  513. cJSON_AddItemToObject(beaconlist_json, "ssids", ssids_json);
  514. }
  515. // Random SSIDs
  516. if (beaconlist_stage->random_ssids > 0) {
  517. cJSON_AddNumberToObject(beaconlist_json, "generate", beaconlist_stage->random_ssids);
  518. }
  519. // Timeout
  520. if (beaconlist_stage->timeout > 0) {
  521. cJSON_AddNumberToObject(beaconlist_json, "timeout", beaconlist_stage->timeout);
  522. }
  523. return stage_json;
  524. }
  525. cJSON* _wifi_marauder_script_create_json_beaconap(WifiMarauderScriptStageBeaconAp* beaconap_stage) {
  526. cJSON* stage_json = cJSON_CreateObject();
  527. cJSON_AddItemToObject(stage_json, "beaconAp", cJSON_CreateObject());
  528. cJSON* beaconap_json = cJSON_GetObjectItem(stage_json, "beaconAp");
  529. // Timeout
  530. if (beaconap_stage->timeout > 0) {
  531. cJSON_AddNumberToObject(beaconap_json, "timeout", beaconap_stage->timeout);
  532. }
  533. return stage_json;
  534. }
  535. void wifi_marauder_script_save_json(Storage* storage, const char* file_path, WifiMarauderScript* script) {
  536. File* script_file = storage_file_alloc(storage);
  537. if (storage_file_open(script_file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  538. cJSON* root_json = cJSON_CreateObject();
  539. // Meta info
  540. cJSON* meta_json = _wifi_marauder_script_create_json_meta(script);
  541. cJSON_AddItemToObject(root_json, "meta", meta_json);
  542. // Create array for stages
  543. cJSON* stages_array = cJSON_CreateArray();
  544. cJSON_AddItemToObject(root_json, "stages", stages_array);
  545. // Iterate over each stage and create the corresponding JSON object
  546. WifiMarauderScriptStage* stage = script->first_stage;
  547. while (stage != NULL) {
  548. cJSON* stage_json = NULL;
  549. switch (stage->type) {
  550. case WifiMarauderScriptStageTypeScan: {
  551. WifiMarauderScriptStageScan* scan_stage = (WifiMarauderScriptStageScan*) stage->stage;
  552. stage_json = _wifi_marauder_script_create_json_scan(scan_stage);
  553. break;
  554. }
  555. case WifiMarauderScriptStageTypeSelect: {
  556. WifiMarauderScriptStageSelect* select_stage = (WifiMarauderScriptStageSelect*) stage->stage;
  557. stage_json = _wifi_marauder_script_create_json_select(select_stage);
  558. break;
  559. }
  560. case WifiMarauderScriptStageTypeDeauth: {
  561. WifiMarauderScriptStageDeauth* deauth_stage = (WifiMarauderScriptStageDeauth*) stage->stage;
  562. stage_json = _wifi_marauder_script_create_json_deauth(deauth_stage);
  563. break;
  564. }
  565. case WifiMarauderScriptStageTypeProbe: {
  566. WifiMarauderScriptStageProbe* probe_stage = (WifiMarauderScriptStageProbe*) stage->stage;
  567. stage_json = _wifi_marauder_script_create_json_probe(probe_stage);
  568. break;
  569. }
  570. case WifiMarauderScriptStageTypeSniffRaw: {
  571. WifiMarauderScriptStageSniffRaw* sniffraw_stage = (WifiMarauderScriptStageSniffRaw*) stage->stage;
  572. stage_json = _wifi_marauder_script_create_json_sniffraw(sniffraw_stage);
  573. break;
  574. }
  575. case WifiMarauderScriptStageTypeSniffBeacon: {
  576. WifiMarauderScriptStageSniffBeacon* sniffbeacon_stage = (WifiMarauderScriptStageSniffBeacon*) stage->stage;
  577. stage_json = _wifi_marauder_script_create_json_sniffbeacon(sniffbeacon_stage);
  578. break;
  579. }
  580. case WifiMarauderScriptStageTypeSniffDeauth: {
  581. WifiMarauderScriptStageSniffDeauth* sniffdeauth_stage = (WifiMarauderScriptStageSniffDeauth*) stage->stage;
  582. stage_json = _wifi_marauder_script_create_json_sniffdeauth(sniffdeauth_stage);
  583. break;
  584. }
  585. case WifiMarauderScriptStageTypeSniffEsp: {
  586. WifiMarauderScriptStageSniffEsp* sniffesp_stage = (WifiMarauderScriptStageSniffEsp*) stage->stage;
  587. stage_json = _wifi_marauder_script_create_json_sniffesp(sniffesp_stage);
  588. break;
  589. }
  590. case WifiMarauderScriptStageTypeSniffPmkid: {
  591. WifiMarauderScriptStageSniffPmkid* sniffpmkid_stage = (WifiMarauderScriptStageSniffPmkid*) stage->stage;
  592. stage_json = _wifi_marauder_script_create_json_sniffpmkid(sniffpmkid_stage);
  593. break;
  594. }
  595. case WifiMarauderScriptStageTypeSniffPwn: {
  596. WifiMarauderScriptStageSniffPwn* sniffpwn_stage = (WifiMarauderScriptStageSniffPwn*) stage->stage;
  597. stage_json = _wifi_marauder_script_create_json_sniffpwn(sniffpwn_stage);
  598. break;
  599. }
  600. case WifiMarauderScriptStageTypeBeaconList: {
  601. WifiMarauderScriptStageBeaconList* beaconlist_stage = (WifiMarauderScriptStageBeaconList*) stage->stage;
  602. stage_json = _wifi_marauder_script_create_json_beaconlist(beaconlist_stage);
  603. break;
  604. }
  605. case WifiMarauderScriptStageTypeBeaconAp: {
  606. WifiMarauderScriptStageBeaconAp* beaconap_stage = (WifiMarauderScriptStageBeaconAp*) stage->stage;
  607. stage_json = _wifi_marauder_script_create_json_beaconap(beaconap_stage);
  608. break;
  609. }
  610. }
  611. // Add the stage JSON object to the "stages" array
  612. if (stage_json != NULL) {
  613. cJSON_AddItemToArray(stages_array, stage_json);
  614. }
  615. stage = stage->next_stage;
  616. }
  617. // Write JSON to file
  618. char* json_str = cJSON_Print(root_json);
  619. storage_file_write(script_file, json_str, strlen(json_str));
  620. //free(json_str);
  621. storage_file_close(script_file);
  622. }
  623. storage_file_free(script_file);
  624. }
  625. bool wifi_marauder_script_has_stage(WifiMarauderScript* script, WifiMarauderScriptStageType stage_type) {
  626. if (script == NULL) {
  627. return false;
  628. }
  629. WifiMarauderScriptStage* current_stage = script->first_stage;
  630. while (current_stage != NULL) {
  631. if (current_stage->type == stage_type) {
  632. return true;
  633. }
  634. current_stage = current_stage->next_stage;
  635. }
  636. return false;
  637. }
  638. void wifi_marauder_script_free(WifiMarauderScript *script) {
  639. if (script == NULL) {
  640. return;
  641. }
  642. WifiMarauderScriptStage *current_stage = script->first_stage;
  643. while (current_stage != NULL) {
  644. WifiMarauderScriptStage *next_stage = current_stage->next_stage;
  645. switch (current_stage->type) {
  646. case WifiMarauderScriptStageTypeScan:
  647. free(current_stage->stage);
  648. break;
  649. case WifiMarauderScriptStageTypeSelect:
  650. if (((WifiMarauderScriptStageSelect *) current_stage->stage)->filter != NULL) {
  651. free(((WifiMarauderScriptStageSelect *) current_stage->stage)->filter);
  652. }
  653. if (((WifiMarauderScriptStageSelect *) current_stage->stage)->indexes != NULL) {
  654. free(((WifiMarauderScriptStageSelect *) current_stage->stage)->indexes);
  655. }
  656. free(current_stage->stage);
  657. break;
  658. case WifiMarauderScriptStageTypeDeauth:
  659. free(current_stage->stage);
  660. break;
  661. case WifiMarauderScriptStageTypeProbe:
  662. free(current_stage->stage);
  663. break;
  664. case WifiMarauderScriptStageTypeSniffRaw:
  665. free(current_stage->stage);
  666. break;
  667. case WifiMarauderScriptStageTypeSniffBeacon:
  668. free(current_stage->stage);
  669. break;
  670. case WifiMarauderScriptStageTypeSniffDeauth:
  671. free(current_stage->stage);
  672. break;
  673. case WifiMarauderScriptStageTypeSniffEsp:
  674. free(current_stage->stage);
  675. break;
  676. case WifiMarauderScriptStageTypeSniffPmkid:
  677. free(current_stage->stage);
  678. break;
  679. case WifiMarauderScriptStageTypeSniffPwn:
  680. free(current_stage->stage);
  681. break;
  682. case WifiMarauderScriptStageTypeBeaconList:
  683. for (int i = 0; i < ((WifiMarauderScriptStageBeaconList *) current_stage->stage)->ssid_count; i++) {
  684. free(((WifiMarauderScriptStageBeaconList *) current_stage->stage)->ssids[i]);
  685. }
  686. free(((WifiMarauderScriptStageBeaconList *) current_stage->stage)->ssids);
  687. free(current_stage->stage);
  688. break;
  689. case WifiMarauderScriptStageTypeBeaconAp:
  690. free(current_stage->stage);
  691. break;
  692. }
  693. free(current_stage);
  694. current_stage = next_stage;
  695. }
  696. free(script->name);
  697. free(script->description);
  698. free(script);
  699. }