wifi_marauder_script.c 36 KB

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