Skip to content

Commit

Permalink
Added validate_parameters macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Retr0-code committed Nov 7, 2023
1 parent f403770 commit f8f72cf
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 197 deletions.
24 changes: 4 additions & 20 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,7 @@ static int openssl_evp_wrapper(
uint8_t* get_md5(const char* data, size_t data_size)
{
// Validating parameters
if (data == NULL || data_size == 0)
{
errno = EINVAL;
return NULL;
}
validate_parameters(data == NULL || data_size == 0, NULL);

uint8_t* hash = malloc_check(hash, 16, NULL);

Expand All @@ -176,11 +172,7 @@ uint8_t* get_md5(const char* data, size_t data_size)
int rc4_encrypt(const uint8_t* data, int data_len, uint8_t* key, uint8_t* enc_data)
{
// Validating parameters
if (data == NULL || data_len == 0 || key == NULL || enc_data == NULL)
{
errno = EINVAL;
return NULL;
}
validate_parameters(data == NULL || data_len == 0 || key == NULL || enc_data == NULL, -1);

RC4_KEY rc4_key;
RC4_set_key(&rc4_key, 16, key);
Expand All @@ -197,11 +189,7 @@ int aes_128_cbc_decrypt(
)
{
// Validating parameters
if (enc_data == NULL || data_len == 0 || key == NULL || iv == NULL || dec_data == NULL)
{
errno = EINVAL;
return NULL;
}
validate_parameters(enc_data == NULL || data_len == 0 || key == NULL || iv == NULL || dec_data == NULL, -1);

AES_KEY dec_key;
AES_set_decrypt_key(key, 128, &dec_key);
Expand All @@ -212,11 +200,7 @@ int aes_128_cbc_decrypt(
int des_ecb_decrypt(const uint8_t* enc_data, int data_len, const uint8_t* key, uint8_t* dec_data)
{
// Validating parameters
if (enc_data == NULL || data_len == 0 || key == NULL || dec_data == NULL)
{
errno = EINVAL;
return NULL;
}
validate_parameters(enc_data == NULL || data_len == 0 || key == NULL || dec_data == NULL, -1);

DES_cblock key_block;
memcpy(&key_block, key, sizeof(uint64_t));
Expand Down
32 changes: 6 additions & 26 deletions src/dump_bootkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
int dump_bootkey(FILE* sys_hive, char16_t* out_bootkey)
{
// Validating parameters
if (sys_hive == NULL || out_bootkey == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(sys_hive == NULL || out_bootkey == NULL, -1);

// Allocating hive header
hive_header_t* hive_header_ptr = malloc_check(hive_header_ptr, sizeof(hive_header_t), -2);
Expand Down Expand Up @@ -123,11 +119,7 @@ int dump_bootkey(FILE* sys_hive, char16_t* out_bootkey)
int get_hashed_bootkey(const char16_t* u16_bootkey, FILE* sam_hive, uint8_t* hashed_bootkey)
{
// Validating parameters
if (u16_bootkey == NULL || hashed_bootkey == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(u16_bootkey == NULL || hashed_bootkey == NULL, -1);

// Decoding hex string to raw values
uint8_t* raw_bootkey = bootkey_from_u16(u16_bootkey);
Expand Down Expand Up @@ -264,14 +256,10 @@ int get_hashed_bootkey(const char16_t* u16_bootkey, FILE* sam_hive, uint8_t* has
return 0;
}

uint8_t* bootkey_from_u16(const char16_t* wstr)
static uint8_t* bootkey_from_u16(const char16_t* wstr)
{
// Validating parameter
if (wstr == NULL)
{
errno = EINVAL;
return NULL;
}
validate_parameters(wstr == NULL, NULL);

// Checking a bootkey length
size_t wstr_length = 0;
Expand Down Expand Up @@ -305,11 +293,7 @@ uint8_t* bootkey_from_u16(const char16_t* wstr)
int ntlmv1_hash_bootkey(uint8_t* permutated_bootkey, uint8_t* f_value, uint8_t* hashed_bootkey)
{
// Validating parameters
if (permutated_bootkey == NULL || f_value == NULL || hashed_bootkey == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(permutated_bootkey == NULL || f_value == NULL || hashed_bootkey == NULL, -1);

// Constants for hashed bootkey construction
const char* aqwerty = "!@#$%^&*()qwertyUIOPAzxcvbnmQQQQQQQQQQQQ)(*@&%\0";
Expand Down Expand Up @@ -352,11 +336,7 @@ int ntlmv1_hash_bootkey(uint8_t* permutated_bootkey, uint8_t* f_value, uint8_t*
int ntlmv2_hash_bootkey(uint8_t* permutated_bootkey, uint8_t* f_value, uint8_t* hashed_bootkey)
{
// Validating parameters
if (permutated_bootkey == NULL || f_value == NULL || hashed_bootkey == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(permutated_bootkey == NULL || f_value == NULL || hashed_bootkey == NULL, -1);

// Allocating space for IV taken from F[0x78:0x88] and encrypted bootkey taken from F[0x88:0xA8]
uint8_t* iv = malloc_check(iv, AES_BLOCK_SIZE, -3);
Expand Down
4 changes: 2 additions & 2 deletions src/dump_bootkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ int dump_bootkey(FILE* sys_hive, char16_t* out_bootkey);
*/
int get_hashed_bootkey(const char16_t* u16_bootkey, FILE* sam_hive, uint8_t* hashed_bootkey);

/*! \fn uint8_t* bootkey_from_u16(const char16_t* wstr)
/*! \fn static uint8_t* bootkey_from_u16(const char16_t* wstr)
* \brief Converst bootkey wide char string to array of size 16 of one byte integers
* \param[in] wstr UTF-16 bootkey uppercase string.
* \return hashed bootkey bytes array of size \a RAW_BOOTKEY_LENGTH
*/
uint8_t* bootkey_from_u16(const char16_t* wstr);
static uint8_t* bootkey_from_u16(const char16_t* wstr);

/*! \fn static int ntlmv1_hash_bootkey(uint8_t* permutated_bootkey, uint8_t* f_value, uint8_t* hashed_bootkey)
* \brief Generates NTLMv1 hashed bootkey
Expand Down
71 changes: 13 additions & 58 deletions src/dump_hashes.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
int ntlm_user_init(ntlm_user_t* user_info_ptr)
{
// Validating parameters
if (user_info_ptr == NULL)
{
errno = EINVAL;
return -1;
}

validate_parameters(user_info_ptr == NULL, -1);

user_info_ptr->lmhash = malloc_check(user_info_ptr->lmhash, 16, -2);
user_info_ptr->nthash = malloc_check_clean(user_info_ptr->nthash, 16, -3, 1, user_info_ptr->lmhash);

Expand All @@ -33,11 +29,7 @@ int ntlm_user_init(ntlm_user_t* user_info_ptr)
int ntlm_user_destroy(ntlm_user_t* user_info_ptr)
{
// Validating parameters
if (user_info_ptr == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(user_info_ptr == NULL, -1);

free(user_info_ptr->lmhash);
free(user_info_ptr->nthash);
Expand All @@ -49,11 +41,7 @@ int ntlm_user_destroy(ntlm_user_t* user_info_ptr)
int dump_users_keys(FILE* sam_hive, named_key_t** users_keys_array, size_t* users_amount)
{
// Validating parameters
if (sam_hive == NULL || users_amount == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(sam_hive == NULL || users_amount == NULL || users_amount == 0, -1);

// Allocating hive header
hive_header_t* hive_header_ptr = malloc_check(hive_header_ptr, sizeof(hive_header_t), -2);
Expand Down Expand Up @@ -145,11 +133,7 @@ int dump_users_keys(FILE* sam_hive, named_key_t** users_keys_array, size_t* user
int dump_v_value(FILE* sam_hive, named_key_t* user_key_ptr, ntlm_user_t* user_info_ptr)
{
// Validating parameters
if (sam_hive == NULL || user_key_ptr == NULL || user_info_ptr == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(sam_hive == NULL || user_key_ptr == NULL || user_info_ptr == NULL, -1);

// Reading value key
value_key_t* v_key_ptr = malloc_check(v_key_ptr, sizeof(value_key_t), -2);
Expand Down Expand Up @@ -178,11 +162,7 @@ int dump_v_value(FILE* sam_hive, named_key_t* user_key_ptr, ntlm_user_t* user_in
int dump_user_name(ntlm_user_t* user_info_ptr)
{
// Validating parameters
if (user_info_ptr == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(user_info_ptr == NULL, -1);

uint32_t name_offset = 0;
uint32_t name_length = 0;
Expand All @@ -200,11 +180,7 @@ int dump_user_name(ntlm_user_t* user_info_ptr)
int dump_user_ntlm(ntlm_user_t* user_info_ptr, const uint8_t* hashed_bootkey)
{
// Validating parameters
if (user_info_ptr == NULL || hashed_bootkey == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(user_info_ptr == NULL || hashed_bootkey == NULL, -1);

// Decrypt LM hash
int result = decrypt_ntlm_hash(user_info_ptr, hashed_bootkey, hash_lm);
Expand All @@ -222,11 +198,7 @@ int dump_user_ntlm(ntlm_user_t* user_info_ptr, const uint8_t* hashed_bootkey)
int decrypt_ntlm_hash(ntlm_user_t* user_info_ptr, const uint8_t* hashed_bootkey, const hash_type_e hash_type)
{
// Validating parameters
if (user_info_ptr == NULL || hashed_bootkey == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(user_info_ptr == NULL || hashed_bootkey == NULL, -1);

// Retrieving hash offset from V value
uint32_t hash_offset = 0;
Expand Down Expand Up @@ -293,15 +265,10 @@ int decrypt_ntlm_hash_wrapper(
)
{
// Validating parameters
if (enc_hash == NULL || hashed_bootkey == NULL ||
validate_parameters(enc_hash == NULL || hashed_bootkey == NULL ||
salt == NULL || user_info_ptr == NULL ||
decrypted_hash == NULL || ntlm_version == NULL ||
user_info_ptr == NULL
)
{
errno = EINVAL;
return -1;
}
user_info_ptr == NULL, -1);

uint64_t des_key1 = 0;
uint64_t des_key2 = 0;
Expand Down Expand Up @@ -344,11 +311,7 @@ int decrypt_ntlmv1_callback(
)
{
// Validating parameters
if (encrypted_hash == NULL || hashed_bootkey == NULL || salt == NULL || output == NULL || user_info_ptr == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(encrypted_hash == NULL || hashed_bootkey == NULL || salt == NULL || output == NULL || user_info_ptr == NULL, -1);

// Constructing full data for RC4 key
size_t ntlmphrase_len = strlen(salt) + 1;
Expand Down Expand Up @@ -383,11 +346,7 @@ int decrypt_ntlmv2_callback(
)
{
// Validating parameters
if (encrypted_hash == NULL || hashed_bootkey == NULL || salt == NULL || output == NULL || user_info_ptr == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(encrypted_hash == NULL || hashed_bootkey == NULL || salt == NULL || output == NULL || user_info_ptr == NULL, -1);

if (aes_128_cbc_decrypt(encrypted_hash, 32, hashed_bootkey, salt, output) == 0)
return -2;
Expand All @@ -398,11 +357,7 @@ int decrypt_ntlmv2_callback(
int sid_to_des_keys(uint32_t sid, uint64_t* key1, uint64_t* key2)
{
// Validating parameters
if (key1 == NULL || key2 == NULL)
{
errno = EINVAL;
return -1;
}
validate_parameters(key1 == NULL || key2 == NULL, -1);

// Creating pointers to use uint64_t pointer as a byte array
uint8_t* key1_array = key1;
Expand Down
20 changes: 14 additions & 6 deletions src/dump_hives.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,30 @@ int resolve_temp_paths()

int reg_save_key(const char* key_name, const char* save_to)
{
validate_parameters(key_name == NULL || save_to == NULL, -1);

HANDLE token_handle = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token_handle) == 0)
{
CloseHandle(token_handle);
return -1;
return -2;
}

// Enabling requered priviles
if (enable_privilege(token_handle, SE_BACKUP_NAME, TRUE) != 0)
{
CloseHandle(token_handle);
return -1;
return -3;
}

// Opening registry hive
HKEY hive_handle = NULL;
if (RegOpenKeyA(HKEY_LOCAL_MACHINE, key_name, &hive_handle) != ERROR_SUCCESS)
return -2;
return -4;

// Saving hive to file
if (RegSaveKeyA(hive_handle, save_to, NULL) != ERROR_SUCCESS)
return -3;
return -5;

// Cleanup
RegCloseKey(hive_handle);
Expand Down Expand Up @@ -158,22 +160,28 @@ void set_paths(const char* sys_hive_path, const char* sam_hive_path)

int open_hives(FILE** system_hive, FILE** sam_hive)
{
// Validating parameters
validate_parameters(system_hive == NULL || sam_hive == NULL, -1);

*system_hive = fopen(system_hive_filepath, "rb");
if (*system_hive == NULL)
return -1;
return -2;

*sam_hive = fopen(sam_hive_filepath, "rb");
if (*sam_hive == NULL)
{
fclose(*system_hive);
return -2;
return -3;
}

return 0;
}

void close_hives(FILE** system_hive, FILE** sam_hive, int delete_hives)
{
// Validating parameters
validate_parameters(system_hive == NULL || sam_hive == NULL, 0);

fclose(*system_hive);
fclose(*sam_hive);

Expand Down
19 changes: 9 additions & 10 deletions src/functional.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

void cleanup_pointers(size_t amount, ...)
{
// Validating parameters
validate_parameters(amount == 0, 0);

va_list pointers;
va_start(pointers, amount);

Expand All @@ -28,11 +31,7 @@ void cleanup_pointers(size_t amount, ...)
char* get_random_string(size_t length)
{
// Validating parameter
if (length == 0)
{
errno = EINVAL;
return NULL;
}
validate_parameters(length == 0, NULL);

// Allocating string
char* str = malloc_check(str, length + 1, NULL);
Expand All @@ -48,6 +47,9 @@ char* get_random_string(size_t length)

void bytes_to_hex(uint8_t* input, size_t length, char* output)
{
// Validating parameters
validate_parameters(input == NULL || length == 0 || output == NULL, 0);

for (size_t i = 0; i < length; i++)
output += sprintf(output, "%02x", input[i]);
}
Expand All @@ -56,11 +58,8 @@ void bytes_to_hex(uint8_t* input, size_t length, char* output)

wchar_t* u16_to_u32(const char16_t* u16_input_str)
{
if (u16_input_str == NULL)
{
errno = EINVAL;
return NULL;
}
// Validating parameters
validate_parameters(u16_input_str == NULL, NULL);

size_t u16_length = 0;
while (u16_input_str[u16_length++]);
Expand Down
Loading

0 comments on commit f8f72cf

Please sign in to comment.