subbrute_device.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include "subbrute_protocols.h"
  3. #include "helpers/subbrute_radio_device_loader.h"
  4. #include <lib/subghz/protocols/base.h>
  5. #include <lib/subghz/transmitter.h>
  6. #include <lib/subghz/receiver.h>
  7. #include <lib/subghz/environment.h>
  8. #define SUBBRUTE_MAX_LEN_NAME 64
  9. #define SUBBRUTE_PATH EXT_PATH("subghz")
  10. #define SUBBRUTE_FILE_EXT ".sub"
  11. /**
  12. * @enum SubBruteFileResult
  13. * @brief Represents the possible results of a sub-brute force file operation.
  14. *
  15. * This enumeration defines a set of possible results that can occur when performing
  16. * a sub-brute force operation on a file. Each result represents a specific error or
  17. * condition that can occur during the operation.
  18. */
  19. typedef enum {
  20. SubBruteFileResultUnknown,
  21. SubBruteFileResultOk,
  22. SubBruteFileResultErrorOpenFile,
  23. SubBruteFileResultMissingOrIncorrectHeader,
  24. SubBruteFileResultFrequencyNotAllowed,
  25. SubBruteFileResultMissingOrIncorrectFrequency,
  26. SubBruteFileResultPresetInvalid,
  27. SubBruteFileResultMissingProtocol,
  28. SubBruteFileResultProtocolNotSupported,
  29. SubBruteFileResultDynamicProtocolNotValid,
  30. SubBruteFileResultProtocolNotFound,
  31. SubBruteFileResultMissingOrIncorrectBit,
  32. SubBruteFileResultMissingOrIncorrectKey,
  33. SubBruteFileResultMissingOrIncorrectTe,
  34. } SubBruteFileResult;
  35. /**
  36. * @struct SubBruteDevice
  37. * @brief Represents a device for SubBrute attack.
  38. *
  39. * This structure contains information and state variables required for performing
  40. * a SubBrute attack.
  41. *
  42. */
  43. typedef struct {
  44. const SubBruteProtocol* protocol_info; /**< Protocol info */
  45. SubBruteProtocol* file_protocol_info; /**< File protocol info */
  46. uint64_t current_step; /**< Current step */
  47. /** @see @c SubGhz service for more info */
  48. SubGhzReceiver* receiver; /**< Receiver */
  49. SubGhzProtocolDecoderBase* decoder_result; /**< Decoder result */
  50. SubGhzEnvironment* environment; /**< Environment */
  51. const SubGhzDevice* radio_device; /**< Radio device */
  52. SubBruteAttacks attack; /**< Attack state */
  53. uint64_t max_value; /**< Max step */
  54. uint8_t extra_repeats; /**< Extra repeats */
  55. /** @brief Loaded info for the attack type */
  56. uint64_t key_from_file; /**< Key from file */
  57. uint64_t current_key_from_file; /**< Current key from file */
  58. bool two_bytes; /**< Two bytes key */
  59. uint8_t opencode; /**< Opencode */
  60. uint8_t bit_index; /**< Index of a group to bruteforce in loaded file */
  61. } SubBruteDevice;
  62. /**
  63. * @brief Response messages
  64. * */
  65. static char* const error_device_ok = "OK";
  66. static char* const error_device_invalid_path = "invalid name/path";
  67. static char* const error_device_missing_header = "Missing or incorrect header";
  68. static char* const error_device_invalid_frequency = "Invalid frequency!";
  69. static char* const error_device_incorrect_frequency = "Missing or incorrect Frequency";
  70. static char* const error_device_preset_fail = "Preset FAIL";
  71. static char* const error_device_missing_protocol = "Missing Protocol";
  72. static char* const error_device_protocol_unsupported = "Protocol unsupported";
  73. static char* const error_device_dynamic_protocol_unsupported = "Dynamic protocol unsupported";
  74. static char* const error_device_protocol_not_found = "Protocol not found";
  75. static char* const error_device_missing_bit = "Missing or incorrect Bit";
  76. static char* const error_device_missing_key = "Missing or incorrect Key";
  77. static char* const error_device_missing_te = "Missing or incorrect TE";
  78. static char* const error_device_unknown = "Unknown error";
  79. /**
  80. * @brief Allocates memory for a SubBruteDevice structure.
  81. *
  82. * This function allocates memory for a SubBruteDevice structure and
  83. * initializes it using the given SubGhzDevice.
  84. *
  85. * @param[in] radio_device The SubGhzDevice used to initialize the SubBruteDevice.
  86. * @return A pointer to the allocated SubBruteDevice structure.
  87. */
  88. SubBruteDevice* subbrute_device_alloc(const SubGhzDevice* radio_device);
  89. /**
  90. * @brief Frees the memory allocated for a SubBruteDevice instance.
  91. *
  92. * This function frees the memory allocated for a SubBruteDevice instance.
  93. * After calling this function, the instance is no longer valid and should not be used.
  94. *
  95. * @param[out] instance Pointer to the SubBruteDevice instance to be freed.
  96. *
  97. */
  98. void subbrute_device_free(SubBruteDevice* instance);
  99. /**
  100. * Saves a file with the specified key name using the given SubBruteDevice instance.
  101. *
  102. * @param instance The SubBruteDevice instance to use for saving the file.
  103. * @param key_name The name of the key to be used for saving the file.
  104. * @return True if the file is successfully saved, False otherwise.
  105. */
  106. bool subbrute_device_save_file(SubBruteDevice* instance, const char* key_name);
  107. /**
  108. * @brief Retrieves the description for a specific device error.
  109. *
  110. * This function returns a string that describes the given device error ID. The error ID
  111. * should be obtained from a SubBruteFileResult value. The returned string provides
  112. * additional information about the error, such as its cause or possible solutions.
  113. *
  114. * @param error_id The device error ID for which to retrieve the description.
  115. *
  116. * @return A pointer to a constant string containing the description of the device error.
  117. */
  118. const char* subbrute_device_error_get_desc(SubBruteFileResult error_id);
  119. SubBruteFileResult subbrute_device_attack_set(
  120. SubBruteDevice* context,
  121. SubBruteAttacks type,
  122. uint8_t extra_repeats);
  123. /**
  124. * @brief Loads data from a file into a SubBruteDevice context.
  125. *
  126. * This function reads data from the specified file and populates the
  127. * SubBruteDevice context with the loaded data. The file contents are
  128. * expected to be in a specific format supported by the SubBruteDevice.
  129. * Use this function to initialize the context with pre-existing data.
  130. *
  131. * @param context Pointer to the SubBruteDevice context to be populated.
  132. * @param file_path Path of the file to load data from.
  133. * @return The status of the loading operation.
  134. * - Returns 0 if the loading is successful.
  135. * - Returns a non-zero value if an error occurs during loading.
  136. */
  137. uint8_t subbrute_device_load_from_file(SubBruteDevice* context, const char* file_path);
  138. /**
  139. * @brief Adds a step to the SubBruteDevice instance.
  140. *
  141. * This function adds a step to the given SubBruteDevice instance. It updates
  142. * the internal state of the device accordingly.
  143. *
  144. * @param[in,out] instance The SubBruteDevice instance.
  145. * @param[in] step The step to be added.
  146. *
  147. * @return The updated value of the device state after adding the step.
  148. */
  149. uint64_t subbrute_device_add_step(SubBruteDevice* instance, int8_t step);
  150. /**
  151. * @brief Frees the memory allocated for the protocol information of a SubBruteDevice instance.
  152. *
  153. * This function will deallocate the memory used by the protocol information of a SubBruteDevice instance,
  154. * including all the individual protocol objects and their data.
  155. *
  156. * @param instance Pointer to the SubBruteDevice instance.
  157. */
  158. void subbrute_device_free_protocol_info(SubBruteDevice* instance);
  159. /**
  160. * @brief Set the default values for a SubBruteDevice attack configuration.
  161. *
  162. * This function sets the default values for a SubBruteDevice attack configuration based on the specified default_attack type.
  163. *
  164. * @param context Pointer to a SubBruteDevice instance.
  165. * @param default_attack The default attack type to use.
  166. *
  167. * @see SubBruteDevice
  168. */
  169. void subbrute_device_attack_set_default_values(
  170. SubBruteDevice* context,
  171. SubBruteAttacks default_attack);