wifi_marauder_script.c 38 KB

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