wifi_marauder_script.c 37 KB

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