diff --git a/README.md b/README.md index 3dc5b86..b3c7c65 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ Compiles and runs under ## Version ## -1.8.0 -Last changed: 13.06.2024 +1.8.1 +Last changed: 10.09.2024 ## REQUIREMENTS ## @@ -105,7 +105,7 @@ Optional Parameters: * q: quad word. Expect for the string types, all values have to be passed as hex values, omitting `0x`. * Find options: - * -ci: case independed (for ascii search only). + * -ci: case insensitive (for ascii search only). * -pid only: * -lpx List entire process memory layout. * -lpm List all process modules. diff --git a/src/Globals.h b/src/Globals.h index 683f149..6ebac9b 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -54,7 +54,7 @@ #define QUIT ('q') #define CONTINUE ('c') -#define MAX_PAYLOAD_LN (0x200) +#define MAX_PAYLOAD_LN (0xFFFFFFFF) #define FIND_FAILURE SIZE_MAX extern size_t file_size; diff --git a/src/Writer.c b/src/Writer.c index 324fa93..ded08bf 100644 --- a/src/Writer.c +++ b/src/Writer.c @@ -35,7 +35,7 @@ static void truncateFile(FILE* fp, size_t file_size, size_t ln); uint32_t payloadParseByte(const char* arg, uint8_t** payload) { int s; - uint32_t arg_ln = (uint32_t)strnlen(arg, MAX_PAYLOAD_LN); + uint32_t arg_ln = (uint32_t)strnlen(arg, 4); if ( arg_ln < 1 ) { printf("Error: Payload byte has no value!\n"); @@ -276,10 +276,9 @@ uint32_t payloadParseUtf16(const char* arg, uint8_t** payload) { uint32_t i; size_t arg_ln = (uint32_t)strnlen(arg, MAX_PAYLOAD_LN); - - // fill buffer to get the real size - uint8_t outb[MAX_PAYLOAD_LN*2] = {0}; - size_t outlen = MAX_PAYLOAD_LN*2; + + size_t outlen = 0; + uint8_t* outb = NULL; if ( arg_ln < 1 ) { @@ -287,12 +286,18 @@ uint32_t payloadParseUtf16(const char* arg, uint8_t** payload) return 0; } - int s = UTF8ToUTF16LE(outb, &outlen, (uint8_t*)arg, &arg_ln); + // fill buffer to get the real size + outlen = (size_t)MAX_PAYLOAD_LN * 2; + outb = (uint8_t*)malloc(outlen); + if ( !outb ) + return 0; + int s = UTF8ToUTF16LE(outb, &outlen, (uint8_t*)arg, &arg_ln); if ( s != 0 ) { printf("Error (0x%x): Converting to utf16.\n", s); - return 0; + outlen = 0; + goto clean; } // alloc payload with real size @@ -300,7 +305,8 @@ uint32_t payloadParseUtf16(const char* arg, uint8_t** payload) if ( p == NULL ) { printf("ERROR: Allocating memory failed!\n"); - return 0; + outlen = 0; + goto clean; } for ( i = 0; i < outlen; i++ ) @@ -310,6 +316,10 @@ uint32_t payloadParseUtf16(const char* arg, uint8_t** payload) *payload = p; +clean: + if ( outb ) + free(outb); + return (uint32_t)outlen; } @@ -340,6 +350,55 @@ uint32_t payloadParseReversedPlainBytes(const char* arg, uint8_t** payload) return payload_ln; } +/** + * Clean byte string of spaces or \x format tags + */ +int cleanBytes(const char* input, char** output) +{ + // get max size of data + size_t input_ln = strlen(input); + + // alloc output buffer + terminating zero + char* local = (char*)malloc(input_ln+1); + if ( !local ) + return -1; + size_t local_cb = 0; + + const char* end_ptr = input + input_ln; + char* local_ptr = local; + for ( const char* input_ptr = input; input_ptr < end_ptr; input_ptr++ ) + { + // skip spaces + if ( *input_ptr == ' ' + || *input_ptr == '|' + || *input_ptr == '-' ) + continue; + // skip "\x" marker + if (*input_ptr == '\\' + && input_ptr < end_ptr - 1 + && *(input_ptr + 1) == 'x') + { + input_ptr++; + continue; + } + + *local_ptr = *input_ptr; + local_ptr++; + } + + local_cb = local_ptr - local; + if ( local_cb > MAX_PAYLOAD_LN ) + { + free(local); + return -2; + } + local[local_cb] = 0; + + *output = local; + + return 0; +} + /** * Parse the arg as plain bytes. * Allocates payload. Caller has to free it. @@ -351,7 +410,7 @@ uint32_t payloadParseReversedPlainBytes(const char* arg, uint8_t** payload) uint32_t payloadParsePlainBytes(const char* arg, uint8_t** payload) { uint32_t i, j; - uint16_t arg_ln = (uint16_t)strnlen(arg, MAX_PAYLOAD_LN); + uint32_t arg_ln = (uint32_t)strnlen(arg, MAX_PAYLOAD_LN); uint8_t* p; char byte[3] = {0}; uint32_t payload_ln; diff --git a/src/Writer.h b/src/Writer.h index 4269b0a..7b08128 100644 --- a/src/Writer.h +++ b/src/Writer.h @@ -42,6 +42,11 @@ uint32_t payloadParseReversedPlainBytes( uint8_t** payload ); +int cleanBytes( + const char* input, + char** output +); + uint32_t payloadParsePlainBytes( const char* arg, uint8_t** payload diff --git a/src/hexter.c b/src/hexter.c index 5275f56..ee9b790 100644 --- a/src/hexter.c +++ b/src/hexter.c @@ -35,8 +35,8 @@ #include "utils/Strings.h" #define BIN_NAME ("hexter") -#define BIN_VS "1.8.0" -#define BIN_LAST_CHANGED "13.06.2024" +#define BIN_VS "1.8.1" +#define BIN_LAST_CHANGED "10.09.2024" #define LIN_PARAM_IDENTIFIER ('-') #define WIN_PARAM_IDENTIFIER ('/') @@ -182,7 +182,7 @@ int run(const char payload_format, const char* raw_payload) if ( ((mode_flags & (MODE_FLAG_FIND|MODE_FLAG_CASE_INSENSITIVE)) == (MODE_FLAG_FIND|MODE_FLAG_CASE_INSENSITIVE)) && payload_format == FORMAT_ASCII ) { - toUpperCaseA(payload, payload_ln); + toUpperCaseA((char*)payload, payload_ln); } } @@ -763,8 +763,15 @@ uint32_t parsePayload(const char format, const char* value, uint8_t** payload) ln = payloadParseUtf16(value, payload); // else if ( format == 'r' ) // ln = payloadParseReversedPlainBytes(arg, payload); - else if ( format == FORMAT_PLAIN_HEX_1 || format == FORMAT_PLAIN_HEX_2 ) - ln = payloadParsePlainBytes(value, payload); + else if (format == FORMAT_PLAIN_HEX_1 || format == FORMAT_PLAIN_HEX_2) + { + char* cleaned_value = NULL; + int s = cleanBytes(value, &cleaned_value); + if ( s != 0 ) + return 0; + ln = payloadParsePlainBytes(cleaned_value, payload); + free(cleaned_value); + } else { printf("ERROR: %c is not a supported format!\n", format); diff --git a/src/utils/Strings.c b/src/utils/Strings.c index 9955d96..27b04b7 100644 --- a/src/utils/Strings.c +++ b/src/utils/Strings.c @@ -184,7 +184,6 @@ int toUpperCaseCA(char* c) int toUpperCaseA(char* buffer, size_t size) { - size_t i; char* end = buffer + size; char* ptr = buffer;