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 bit_index; /**< Index of a group to bruteforce in loaded file */
  60. } SubBruteDevice;
  61. /**
  62. * @brief Response messages
  63. * */
  64. static char* const error_device_ok = "OK";
  65. static char* const error_device_invalid_path = "invalid name/path";
  66. static char* const error_device_missing_header = "Missing or incorrect header";
  67. static char* const error_device_invalid_frequency = "Invalid frequency!";
  68. static char* const error_device_incorrect_frequency = "Missing or incorrect Frequency";
  69. static char* const error_device_preset_fail = "Preset FAIL";
  70. static char* const error_device_missing_protocol = "Missing Protocol";
  71. static char* const error_device_protocol_unsupported = "Protocol unsupported";
  72. static char* const error_device_dynamic_protocol_unsupported = "Dynamic protocol unsupported";
  73. static char* const error_device_protocol_not_found = "Protocol not found";
  74. static char* const error_device_missing_bit = "Missing or incorrect Bit";
  75. static char* const error_device_missing_key = "Missing or incorrect Key";
  76. static char* const error_device_missing_te = "Missing or incorrect TE";
  77. static char* const error_device_unknown = "Unknown error";
  78. /**
  79. * @brief Allocates memory for a SubBruteDevice structure.
  80. *
  81. * This function allocates memory for a SubBruteDevice structure and
  82. * initializes it using the given SubGhzDevice.
  83. *
  84. * @param[in] radio_device The SubGhzDevice used to initialize the SubBruteDevice.
  85. * @return A pointer to the allocated SubBruteDevice structure.
  86. */
  87. SubBruteDevice* subbrute_device_alloc(const SubGhzDevice* radio_device);
  88. /**
  89. * @brief Frees the memory allocated for a SubBruteDevice instance.
  90. *
  91. * This function frees the memory allocated for a SubBruteDevice instance.
  92. * After calling this function, the instance is no longer valid and should not be used.
  93. *
  94. * @param[out] instance Pointer to the SubBruteDevice instance to be freed.
  95. *
  96. */
  97. void subbrute_device_free(SubBruteDevice* instance);
  98. /**
  99. * Saves a file with the specified key name using the given SubBruteDevice instance.
  100. *
  101. * @param instance The SubBruteDevice instance to use for saving the file.
  102. * @param key_name The name of the key to be used for saving the file.
  103. * @return True if the file is successfully saved, False otherwise.
  104. */
  105. bool subbrute_device_save_file(SubBruteDevice* instance, const char* key_name);
  106. /**
  107. * @brief Retrieves the description for a specific device error.
  108. *
  109. * This function returns a string that describes the given device error ID. The error ID
  110. * should be obtained from a SubBruteFileResult value. The returned string provides
  111. * additional information about the error, such as its cause or possible solutions.
  112. *
  113. * @param error_id The device error ID for which to retrieve the description.
  114. *
  115. * @return A pointer to a constant string containing the description of the device error.
  116. */
  117. const char* subbrute_device_error_get_desc(SubBruteFileResult error_id);
  118. SubBruteFileResult subbrute_device_attack_set(
  119. SubBruteDevice* context,
  120. SubBruteAttacks type,
  121. uint8_t extra_repeats);
  122. /**
  123. * @brief Loads data from a file into a SubBruteDevice context.
  124. *
  125. * This function reads data from the specified file and populates the
  126. * SubBruteDevice context with the loaded data. The file contents are
  127. * expected to be in a specific format supported by the SubBruteDevice.
  128. * Use this function to initialize the context with pre-existing data.
  129. *
  130. * @param context Pointer to the SubBruteDevice context to be populated.
  131. * @param file_path Path of the file to load data from.
  132. * @return The status of the loading operation.
  133. * - Returns 0 if the loading is successful.
  134. * - Returns a non-zero value if an error occurs during loading.
  135. */
  136. uint8_t subbrute_device_load_from_file(SubBruteDevice* context, const char* file_path);
  137. /**
  138. * @brief Adds a step to the SubBruteDevice instance.
  139. *
  140. * This function adds a step to the given SubBruteDevice instance. It updates
  141. * the internal state of the device accordingly.
  142. *
  143. * @param[in,out] instance The SubBruteDevice instance.
  144. * @param[in] step The step to be added.
  145. *
  146. * @return The updated value of the device state after adding the step.
  147. */
  148. uint64_t subbrute_device_add_step(SubBruteDevice* instance, int8_t step);
  149. /**
  150. * @brief Frees the memory allocated for the protocol information of a SubBruteDevice instance.
  151. *
  152. * This function will deallocate the memory used by the protocol information of a SubBruteDevice instance,
  153. * including all the individual protocol objects and their data.
  154. *
  155. * @param instance Pointer to the SubBruteDevice instance.
  156. */
  157. void subbrute_device_free_protocol_info(SubBruteDevice* instance);
  158. /**
  159. * @brief Set the default values for a SubBruteDevice attack configuration.
  160. *
  161. * This function sets the default values for a SubBruteDevice attack configuration based on the specified default_attack type.
  162. *
  163. * @param context Pointer to a SubBruteDevice instance.
  164. * @param default_attack The default attack type to use.
  165. *
  166. * @see SubBruteDevice
  167. */
  168. void subbrute_device_attack_set_default_values(
  169. SubBruteDevice* context,
  170. SubBruteAttacks default_attack);