subbrute_device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "subbrute_device.h"
  2. #include <stdint.h>
  3. #include <storage/storage.h>
  4. #include <lib/toolbox/stream/stream.h>
  5. #include <lib/flipper_format/flipper_format.h>
  6. #include <lib/flipper_format/flipper_format_i.h>
  7. #define TAG "SubBruteDevice"
  8. SubBruteDevice* subbrute_device_alloc() {
  9. SubBruteDevice* instance = malloc(sizeof(SubBruteDevice));
  10. instance->key_index = 0;
  11. instance->protocol_info = NULL;
  12. instance->file_protocol_info = NULL;
  13. instance->decoder_result = NULL;
  14. instance->receiver = NULL;
  15. instance->environment = subghz_environment_alloc();
  16. subbrute_device_attack_set_default_values(instance, SubBruteAttackCAME12bit433);
  17. return instance;
  18. }
  19. void subbrute_device_free(SubBruteDevice* instance) {
  20. furi_assert(instance);
  21. // I don't know how to free this
  22. instance->decoder_result = NULL;
  23. if(instance->receiver != NULL) {
  24. subghz_receiver_free(instance->receiver);
  25. instance->receiver = NULL;
  26. }
  27. subghz_environment_free(instance->environment);
  28. instance->environment = NULL;
  29. subbrute_device_free_protocol_info(instance);
  30. free(instance);
  31. }
  32. uint64_t subbrute_device_add_step(SubBruteDevice* instance, int8_t step) {
  33. if(step > 0) {
  34. if((instance->key_index + step) - instance->max_value == 1) {
  35. instance->key_index = 0x00;
  36. } else {
  37. uint64_t value = instance->key_index + step;
  38. if(value == instance->max_value) {
  39. instance->key_index = value;
  40. } else {
  41. instance->key_index = value % instance->max_value;
  42. }
  43. }
  44. } else {
  45. if(instance->key_index + step == 0) {
  46. instance->key_index = 0x00;
  47. } else if(instance->key_index == 0) {
  48. instance->key_index = instance->max_value;
  49. } else {
  50. uint64_t value = ((instance->key_index - step) + instance->max_value);
  51. if(value == instance->max_value) {
  52. instance->key_index = value;
  53. } else {
  54. instance->key_index = value % instance->max_value;
  55. }
  56. }
  57. }
  58. return instance->key_index;
  59. }
  60. bool subbrute_device_save_file(SubBruteDevice* instance, const char* dev_file_name) {
  61. furi_assert(instance);
  62. #ifdef FURI_DEBUG
  63. FURI_LOG_D(TAG, "subbrute_device_save_file: %s", dev_file_name);
  64. #endif
  65. Storage* storage = furi_record_open(RECORD_STORAGE);
  66. FlipperFormat* file = flipper_format_file_alloc(storage);
  67. FuriString* file_content = furi_string_alloc();
  68. bool result = false;
  69. do {
  70. if(!flipper_format_file_open_always(file, dev_file_name)) {
  71. break;
  72. }
  73. if(instance->attack == SubBruteAttackLoadFile) {
  74. file_content = subbrute_protocol_file_generate_file(
  75. instance->file_protocol_info->frequency,
  76. instance->file_protocol_info->preset,
  77. instance->file_protocol_info->file,
  78. instance->key_index,
  79. instance->file_protocol_info->bits,
  80. instance->file_protocol_info->te,
  81. instance->file_protocol_info->repeat,
  82. instance->load_index,
  83. instance->file_key);
  84. } else {
  85. file_content = subbrute_protocol_default_generate_file(
  86. instance->protocol_info->frequency,
  87. instance->protocol_info->preset,
  88. instance->protocol_info->file,
  89. instance->key_index,
  90. instance->protocol_info->bits,
  91. instance->protocol_info->te,
  92. instance->protocol_info->repeat);
  93. }
  94. Stream* stream = flipper_format_get_raw_stream(file);
  95. stream_clean(stream);
  96. size_t written = stream_write_string(stream, file_content);
  97. if(written <= 0) {
  98. FURI_LOG_E(TAG, "create_packet_parsed failed!");
  99. break;
  100. }
  101. result = true;
  102. } while(false);
  103. if(!result) {
  104. FURI_LOG_E(TAG, "subbrute_device_save_file failed!");
  105. }
  106. flipper_format_file_close(file);
  107. flipper_format_free(file);
  108. furi_record_close(RECORD_STORAGE);
  109. furi_string_free(file_content);
  110. return result;
  111. }
  112. SubBruteFileResult subbrute_device_attack_set(SubBruteDevice* instance, SubBruteAttacks type) {
  113. furi_assert(instance);
  114. #ifdef FURI_DEBUG
  115. FURI_LOG_D(TAG, "subbrute_device_attack_set: %d", type);
  116. #endif
  117. subbrute_device_attack_set_default_values(instance, type);
  118. if(type != SubBruteAttackLoadFile) {
  119. subbrute_device_free_protocol_info(instance);
  120. instance->protocol_info = subbrute_protocol(type);
  121. }
  122. // For non-file types we didn't set SubGhzProtocolDecoderBase
  123. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  124. subghz_receiver_set_filter(instance->receiver, SubGhzProtocolFlag_Decodable);
  125. furi_hal_subghz_reset();
  126. uint8_t protocol_check_result = SubBruteFileResultProtocolNotFound;
  127. if(type != SubBruteAttackLoadFile) {
  128. instance->decoder_result = subghz_receiver_search_decoder_base_by_name(
  129. instance->receiver, subbrute_protocol_file(instance->protocol_info->file));
  130. if(!instance->decoder_result ||
  131. instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  132. FURI_LOG_E(TAG, "Can't load SubGhzProtocolDecoderBase in phase non-file decoder set");
  133. } else {
  134. protocol_check_result = SubBruteFileResultOk;
  135. // Calc max value
  136. instance->max_value =
  137. subbrute_protocol_calc_max_value(instance->attack, instance->protocol_info->bits);
  138. }
  139. } else {
  140. // And here we need to set preset enum
  141. protocol_check_result = SubBruteFileResultOk;
  142. // Calc max value
  143. instance->max_value =
  144. subbrute_protocol_calc_max_value(instance->attack, instance->file_protocol_info->bits);
  145. }
  146. subghz_receiver_free(instance->receiver);
  147. instance->receiver = NULL;
  148. if(protocol_check_result != SubBruteFileResultOk) {
  149. return SubBruteFileResultProtocolNotFound;
  150. }
  151. return SubBruteFileResultOk;
  152. }
  153. uint8_t subbrute_device_load_from_file(SubBruteDevice* instance, const char* file_path) {
  154. furi_assert(instance);
  155. #ifdef FURI_DEBUG
  156. FURI_LOG_D(TAG, "subbrute_device_load_from_file: %s", file_path);
  157. #endif
  158. SubBruteFileResult result = SubBruteFileResultUnknown;
  159. Storage* storage = furi_record_open(RECORD_STORAGE);
  160. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  161. subbrute_device_free_protocol_info(instance);
  162. instance->file_protocol_info = malloc(sizeof(SubBruteProtocol));
  163. FuriString* temp_str;
  164. temp_str = furi_string_alloc();
  165. uint32_t temp_data32;
  166. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  167. subghz_receiver_set_filter(instance->receiver, SubGhzProtocolFlag_Decodable);
  168. furi_hal_subghz_reset();
  169. do {
  170. if(!flipper_format_file_open_existing(fff_data_file, file_path)) {
  171. FURI_LOG_E(TAG, "Error open file %s", file_path);
  172. result = SubBruteFileResultErrorOpenFile;
  173. break;
  174. }
  175. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  176. FURI_LOG_E(TAG, "Missing or incorrect header");
  177. result = SubBruteFileResultMissingOrIncorrectHeader;
  178. break;
  179. }
  180. // Frequency
  181. if(flipper_format_read_uint32(fff_data_file, "Frequency", &temp_data32, 1)) {
  182. instance->file_protocol_info->frequency = temp_data32;
  183. if(!furi_hal_subghz_is_tx_allowed(instance->file_protocol_info->frequency)) {
  184. result = SubBruteFileResultFrequencyNotAllowed;
  185. break;
  186. }
  187. } else {
  188. FURI_LOG_E(TAG, "Missing or incorrect Frequency");
  189. result = SubBruteFileResultMissingOrIncorrectFrequency;
  190. break;
  191. }
  192. // Preset
  193. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  194. FURI_LOG_E(TAG, "Preset FAIL");
  195. result = SubBruteFileResultPresetInvalid;
  196. } else {
  197. instance->file_protocol_info->preset = subbrute_protocol_convert_preset(temp_str);
  198. }
  199. const char* protocol_file = NULL;
  200. // Protocol
  201. if(!flipper_format_read_string(fff_data_file, "Protocol", temp_str)) {
  202. FURI_LOG_E(TAG, "Missing Protocol");
  203. result = SubBruteFileResultMissingProtocol;
  204. break;
  205. } else {
  206. instance->file_protocol_info->file = subbrute_protocol_file_protocol_name(temp_str);
  207. protocol_file = subbrute_protocol_file(instance->file_protocol_info->file);
  208. #ifdef FURI_DEBUG
  209. FURI_LOG_D(TAG, "Protocol: %s", protocol_file);
  210. #endif
  211. }
  212. instance->decoder_result =
  213. subghz_receiver_search_decoder_base_by_name(instance->receiver, protocol_file);
  214. if(!instance->decoder_result || strcmp(protocol_file, "RAW") == 0) {
  215. FURI_LOG_E(TAG, "RAW unsupported");
  216. result = SubBruteFileResultProtocolNotSupported;
  217. break;
  218. }
  219. if(instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  220. FURI_LOG_E(TAG, "Protocol is dynamic - not supported");
  221. result = SubBruteFileResultDynamicProtocolNotValid;
  222. break;
  223. }
  224. #ifdef FURI_DEBUG
  225. else {
  226. FURI_LOG_D(TAG, "Decoder: %s", instance->decoder_result->protocol->name);
  227. }
  228. #endif
  229. // Bit
  230. if(!flipper_format_read_uint32(fff_data_file, "Bit", &temp_data32, 1)) {
  231. FURI_LOG_E(TAG, "Missing or incorrect Bit");
  232. result = SubBruteFileResultMissingOrIncorrectBit;
  233. break;
  234. } else {
  235. instance->file_protocol_info->bits = temp_data32;
  236. #ifdef FURI_DEBUG
  237. FURI_LOG_D(TAG, "Bit: %d", instance->file_protocol_info->bits);
  238. #endif
  239. }
  240. // Key
  241. if(!flipper_format_read_string(fff_data_file, "Key", temp_str)) {
  242. FURI_LOG_E(TAG, "Missing or incorrect Key");
  243. result = SubBruteFileResultMissingOrIncorrectKey;
  244. break;
  245. } else {
  246. snprintf(
  247. instance->file_key,
  248. sizeof(instance->file_key),
  249. "%s",
  250. furi_string_get_cstr(temp_str));
  251. #ifdef FURI_DEBUG
  252. FURI_LOG_D(TAG, "Key: %s", instance->file_key);
  253. #endif
  254. }
  255. // TE
  256. if(!flipper_format_read_uint32(fff_data_file, "TE", &temp_data32, 1)) {
  257. FURI_LOG_E(TAG, "Missing or incorrect TE");
  258. //result = SubBruteFileResultMissingOrIncorrectTe;
  259. //break;
  260. } else {
  261. instance->file_protocol_info->te = temp_data32 != 0;
  262. }
  263. // Repeat
  264. if(flipper_format_read_uint32(fff_data_file, "Repeat", &temp_data32, 1)) {
  265. #ifdef FURI_DEBUG
  266. FURI_LOG_D(TAG, "Repeat: %ld", temp_data32);
  267. #endif
  268. instance->file_protocol_info->repeat = (uint8_t)temp_data32;
  269. } else {
  270. #ifdef FURI_DEBUG
  271. FURI_LOG_D(TAG, "Repeat: 3 (default)");
  272. #endif
  273. instance->file_protocol_info->repeat = 3;
  274. }
  275. result = SubBruteFileResultOk;
  276. } while(0);
  277. furi_string_free(temp_str);
  278. flipper_format_file_close(fff_data_file);
  279. flipper_format_free(fff_data_file);
  280. furi_record_close(RECORD_STORAGE);
  281. subghz_receiver_free(instance->receiver);
  282. instance->decoder_result = NULL;
  283. instance->receiver = NULL;
  284. if(result == SubBruteFileResultOk) {
  285. #ifdef FURI_DEBUG
  286. FURI_LOG_D(TAG, "Loaded successfully");
  287. #endif
  288. } else {
  289. subbrute_device_free_protocol_info(instance);
  290. }
  291. return result;
  292. }
  293. void subbrute_device_attack_set_default_values(
  294. SubBruteDevice* instance,
  295. SubBruteAttacks default_attack) {
  296. furi_assert(instance);
  297. #ifdef FURI_DEBUG
  298. FURI_LOG_D(TAG, "subbrute_device_attack_set_default_values");
  299. #endif
  300. instance->attack = default_attack;
  301. instance->key_index = 0x00;
  302. instance->load_index = 0x00;
  303. memset(instance->current_key, 0, sizeof(instance->current_key));
  304. if(default_attack != SubBruteAttackLoadFile) {
  305. memset(instance->file_key, 0, sizeof(instance->file_key));
  306. instance->max_value = (uint64_t)0x00;
  307. }
  308. }
  309. const char* subbrute_device_error_get_desc(SubBruteFileResult error_id) {
  310. const char* result;
  311. switch(error_id) {
  312. case(SubBruteFileResultOk):
  313. result = "OK";
  314. break;
  315. case(SubBruteFileResultErrorOpenFile):
  316. result = "invalid name/path";
  317. break;
  318. case(SubBruteFileResultMissingOrIncorrectHeader):
  319. result = "Missing or incorrect header";
  320. break;
  321. case(SubBruteFileResultFrequencyNotAllowed):
  322. result = "Invalid frequency!";
  323. break;
  324. case(SubBruteFileResultMissingOrIncorrectFrequency):
  325. result = "Missing or incorrect Frequency";
  326. break;
  327. case(SubBruteFileResultPresetInvalid):
  328. result = "Preset FAIL";
  329. break;
  330. case(SubBruteFileResultMissingProtocol):
  331. result = "Missing Protocol";
  332. break;
  333. case(SubBruteFileResultProtocolNotSupported):
  334. result = "RAW unsupported";
  335. break;
  336. case(SubBruteFileResultDynamicProtocolNotValid):
  337. result = "Dynamic protocol unsupported";
  338. break;
  339. case(SubBruteFileResultProtocolNotFound):
  340. result = "Protocol not found";
  341. break;
  342. case(SubBruteFileResultMissingOrIncorrectBit):
  343. result = "Missing or incorrect Bit";
  344. break;
  345. case(SubBruteFileResultMissingOrIncorrectKey):
  346. result = "Missing or incorrect Key";
  347. break;
  348. case(SubBruteFileResultMissingOrIncorrectTe):
  349. result = "Missing or incorrect TE";
  350. break;
  351. case SubBruteFileResultUnknown:
  352. default:
  353. result = "Unknown error";
  354. break;
  355. }
  356. return result;
  357. }
  358. void subbrute_device_free_protocol_info(SubBruteDevice* instance) {
  359. furi_assert(instance);
  360. instance->protocol_info = NULL;
  361. if(instance->file_protocol_info) {
  362. free(instance->file_protocol_info);
  363. }
  364. instance->file_protocol_info = NULL;
  365. }