summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xextract_decoded_executable/build.sh7
-rw-r--r--extract_decoded_executable/extractor.c57
-rw-r--r--extract_decoded_executable/test_binary.c10
3 files changed, 74 insertions, 0 deletions
diff --git a/extract_decoded_executable/build.sh b/extract_decoded_executable/build.sh
new file mode 100755
index 0000000..6af8971
--- /dev/null
+++ b/extract_decoded_executable/build.sh
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+
+gcc -Wall -Werror -pedantic -O2 -mavx2 -march=x86-64-v3 -std=gnu23 ./test_binary.c -o ./test_binary
+xxd -g1 -p ./test_binary | sed -z 's/\n$/\"/;s/^/\"/;s/\n//g' > ./test_binary_hex.dat
+rm ./test_binary
+gcc -O2 -mavx2 -march=x86-64-v3 -std=gnu23 -DBIN_HEX=$(cat ./test_binary_hex.dat) ./extractor.c -o ./extractor
+rm ./test_binary_hex.dat
diff --git a/extract_decoded_executable/extractor.c b/extract_decoded_executable/extractor.c
new file mode 100644
index 0000000..3a53536
--- /dev/null
+++ b/extract_decoded_executable/extractor.c
@@ -0,0 +1,57 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+
+void extract_contained_binary(){
+ double bin_hex_div = (double)strlen((const char*)BIN_HEX) / 2;
+ if ( bin_hex_div == 0.5 ){
+ fputs( "Error: binary hex is uneven number of characters\n", stderr );
+ return;
+ }
+ size_t hex_byte_count = (size_t)bin_hex_div;
+ unsigned char *decoded_bytes = (unsigned char*)calloc( hex_byte_count + 1, sizeof(unsigned char) );
+ if ( decoded_bytes == NULL ){
+ fprintf( stderr, "Error: failed to allocate memory for decoding: %s\n", strerror(errno) );
+ return;
+ }
+ const char *bin_hex = (const char*)BIN_HEX;
+ size_t byte_pos = 0;
+ size_t dec_byte_pos = 0;
+ char c_byte[2];
+ memset( (void*)&c_byte, 0, sizeof(c_byte) );
+ unsigned char decoded_byte = '\0';
+ for ( ; dec_byte_pos < hex_byte_count; byte_pos += 2, dec_byte_pos++ ){
+ memmove( (void*)&c_byte, (const void*)&bin_hex[byte_pos], 2 );
+ decoded_byte = (unsigned char)strtoul( (const char*)&c_byte, NULL, 16 );
+ if ( errno != 0 ){
+ fprintf( stderr, "Error: failed to decode byte: %s\n", strerror(errno) );
+ free( decoded_bytes );
+ return;
+ }
+ decoded_bytes[dec_byte_pos] = decoded_byte;
+ }
+ int out_fd = open( "/tmp/test_output_binary", O_RDWR | O_CREAT, 0755 );
+ if ( out_fd == -1 ){
+ fprintf( stderr, "Error: failed to open /tmp/test_output_binary for writing: %s\n", strerror(errno) );
+ free( decoded_bytes );
+ return;
+ }
+ if ( write(out_fd, (const void*)decoded_bytes, hex_byte_count + 1) == -1 ){
+ fprintf( stderr, "Error: failed to write /tmp/test_output_binary: %s\n", strerror(errno) );
+ free( decoded_bytes );
+ close( out_fd );
+ return;
+ }
+ close( out_fd );
+ free( decoded_bytes );
+ fputs( "Binary extracted, check /tmp for test_output_binary\n", stdout );
+ return;
+}
+
+int main( int argc, char *const *args ){
+ extract_contained_binary();
+ return 0;
+}
diff --git a/extract_decoded_executable/test_binary.c b/extract_decoded_executable/test_binary.c
new file mode 100644
index 0000000..0ac5c4c
--- /dev/null
+++ b/extract_decoded_executable/test_binary.c
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+
+int main( int argc, char *const *args ){
+ fputs( "This is simple output from extracted test program\n", stdout );
+ return 0;
+}