Skip to content

Commit

Permalink
bytes payload parsing with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
yehoudie committed Sep 10, 2024
1 parent 11e3ab8 commit 77dbd4d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
77 changes: 68 additions & 9 deletions src/Writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -276,31 +276,37 @@ 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 )
{
printf("Error: Payload string has no value!\n");
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
uint8_t* p = (uint8_t*) malloc(outlen);
if ( p == NULL )
{
printf("ERROR: Allocating memory failed!\n");
return 0;
outlen = 0;
goto clean;
}

for ( i = 0; i < outlen; i++ )
Expand All @@ -310,6 +316,10 @@ uint32_t payloadParseUtf16(const char* arg, uint8_t** payload)

*payload = p;

clean:
if ( outb )
free(outb);

return (uint32_t)outlen;
}

Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/hexter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ('/')
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/utils/Strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 77dbd4d

Please sign in to comment.