summaryrefslogtreecommitdiff
path: root/extract_decoded_executable
diff options
context:
space:
mode:
Diffstat (limited to 'extract_decoded_executable')
-rwxr-xr-xextract_decoded_executable/build.sh9
-rw-r--r--extract_decoded_executable/encrypter.c70
-rw-r--r--extract_decoded_executable/extractor.c18
-rw-r--r--extract_decoded_executable/rng.h51
-rw-r--r--extract_decoded_executable/test_binary.c2
5 files changed, 149 insertions, 1 deletions
diff --git a/extract_decoded_executable/build.sh b/extract_decoded_executable/build.sh
index 6af8971..7d0bc0d 100755
--- a/extract_decoded_executable/build.sh
+++ b/extract_decoded_executable/build.sh
@@ -1,7 +1,14 @@
#!/usr/bin/env bash
+SEED=$(seq 1 10 | shuf --random-source=/dev/urandom | sed -z 's/\n//g')
+
+echo "Encryption seed: ${SEED}"
+
gcc -Wall -Werror -pedantic -O2 -mavx2 -march=x86-64-v3 -std=gnu23 ./test_binary.c -o ./test_binary
+gcc -Wall -Werror -pedantic -O2 -mavx2 -march=x86-64-v3 -std=gnu23 -DSEED_VAL=${SEED} ./encrypter.c -o ./encrypter
+./encrypter
+rm ./encrypter
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
+gcc -O2 -mavx2 -march=x86-64-v3 -std=gnu23 -DSEED_VAL=${SEED} -DBIN_HEX=$(cat ./test_binary_hex.dat) ./extractor.c -o ./extractor
rm ./test_binary_hex.dat
diff --git a/extract_decoded_executable/encrypter.c b/extract_decoded_executable/encrypter.c
new file mode 100644
index 0000000..34732cb
--- /dev/null
+++ b/extract_decoded_executable/encrypter.c
@@ -0,0 +1,70 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <math.h>
+#include <sys/stat.h>
+#include "rng.h"
+
+void encrypt_test_binary(){
+ init_rng();
+ unsigned char enc_key[256];
+ memset( (void*)&enc_key, 0, sizeof(enc_key) );
+ size_t k_size = 0;
+ for ( ; k_size < 256; k_size++ )
+ enc_key[k_size] = (unsigned char)get_random_int( 1, 255 );
+ struct stat test_bin_info;
+ memset( (void*)&test_bin_info, 0, sizeof(struct stat) );
+ if ( stat("./test_binary", &test_bin_info) == -1 ){
+ fprintf( stderr, "Failed to stat: %s\n", strerror(errno) );
+ return;
+ }
+ if ( !((test_bin_info.st_mode & S_IFMT) == S_IFREG) ){
+ fputs( "Error: test binary is not regular file\n", stderr );
+ return;
+ }
+ unsigned char *test_bin_bytes = (unsigned char*)calloc( (size_t)test_bin_info.st_size, sizeof(unsigned char) );
+ if ( test_bin_bytes == NULL ){
+ fprintf( stderr, "Error: failed to allocate memory for reading test binary: %s\n", strerror(errno) );
+ return;
+ }
+ int test_bin_fd = open( "./test_binary", O_RDONLY, 0755 );
+ if ( test_bin_fd == -1 ){
+ fprintf( stderr, "Error: failed to open test binary for encryption: %s\n", strerror(errno) );
+ free( test_bin_bytes );
+ return;
+ }
+ if ( read(test_bin_fd, (void*)test_bin_bytes, (size_t)test_bin_info.st_size) == -1 ){
+ fprintf( stderr, "Error: failed to read test binary bytes: %s\n", strerror(errno) );
+ close( test_bin_fd );
+ free( test_bin_bytes );
+ return;
+ }
+ close( test_bin_fd );
+ size_t encrypted_bytes = 0;
+ for ( ; encrypted_bytes < (size_t)test_bin_info.st_size; encrypted_bytes++ )
+ test_bin_bytes[encrypted_bytes] = test_bin_bytes[encrypted_bytes] ^ enc_key[(size_t)test_bin_info.st_size % 255];
+ test_bin_fd = open( "./test_binary", O_RDWR | O_CREAT | O_TRUNC );
+ if ( test_bin_fd == -1 ){
+ fprintf( stderr, "Error: failed to open test binary for writing: %s\n", strerror(errno) );
+ free( test_bin_bytes );
+ return;
+ }
+ if ( write(test_bin_fd, (const void*)test_bin_bytes, (size_t)test_bin_info.st_size) == -1 ){
+ fprintf( stderr, "Error: failed to write encrypted test binary: %s\n", strerror(errno) );
+ close( test_bin_fd );
+ free( test_bin_bytes );
+ return;
+ }
+ close( test_bin_fd );
+ free( test_bin_bytes );
+ return;
+}
+
+int main( int argc, char *const *args ){
+ encrypt_test_binary();
+ return 0;
+}
diff --git a/extract_decoded_executable/extractor.c b/extract_decoded_executable/extractor.c
index 3a53536..5bda48d 100644
--- a/extract_decoded_executable/extractor.c
+++ b/extract_decoded_executable/extractor.c
@@ -1,9 +1,25 @@
#include <stdlib.h>
#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
+#include <math.h>
#include <fcntl.h>
#include <string.h>
+#include "rng.h"
+
+void decrypt_decoded_bytes( unsigned char *encrypted_bytes, size_t byte_count ){
+ unsigned char dec_key[256];
+ memset( (void*)&dec_key, 0, sizeof(dec_key) );
+ size_t recv_key_size = 0;
+ for ( ; recv_key_size < 256; recv_key_size++ )
+ dec_key[recv_key_size] = (unsigned char)get_random_int( 1, 255 );
+ size_t c_byte = 0;
+ for ( ; c_byte < byte_count; c_byte++ )
+ encrypted_bytes[c_byte] = encrypted_bytes[c_byte] ^ dec_key[byte_count % 255];
+ return;
+}
void extract_contained_binary(){
double bin_hex_div = (double)strlen((const char*)BIN_HEX) / 2;
@@ -33,6 +49,7 @@ void extract_contained_binary(){
}
decoded_bytes[dec_byte_pos] = decoded_byte;
}
+ decrypt_decoded_bytes( decoded_bytes, hex_byte_count );
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) );
@@ -52,6 +69,7 @@ void extract_contained_binary(){
}
int main( int argc, char *const *args ){
+ init_rng();
extract_contained_binary();
return 0;
}
diff --git a/extract_decoded_executable/rng.h b/extract_decoded_executable/rng.h
new file mode 100644
index 0000000..93fd317
--- /dev/null
+++ b/extract_decoded_executable/rng.h
@@ -0,0 +1,51 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+struct rngstate{
+ uint64_t state[16];
+ int idx;
+};
+
+static struct rngstate global_rng_state;
+
+void init_rng(){
+ uint64_t seed_val = (uint64_t)SEED_VAL;
+ size_t c_idx = 0;
+ for ( ; c_idx < 16; c_idx++ ){
+ seed_val = seed_val * 0x13371337;
+ seed_val = seed_val ^ (seed_val >> 16);
+ global_rng_state.state[c_idx] = seed_val;
+ }
+ return;
+}
+
+uint64_t get_rng_output(){
+ int c_idx = global_rng_state.idx;
+ const uint64_t next_state = global_rng_state.state[c_idx++];
+ uint64_t use_state = global_rng_state.state[c_idx &= 15];
+ use_state ^= use_state << 31;
+ use_state ^= use_state >> 16;
+ use_state ^= next_state ^ (next_state >> 31);
+ global_rng_state.state[c_idx] = use_state;
+ global_rng_state.idx = c_idx;
+ return use_state * 0x1337;
+}
+
+unsigned int get_random_int( unsigned int min, unsigned int max ){
+ if ( min >= max )
+ return min;
+ uint64_t nmax = 0xffffffffffffffff;
+ uint64_t ncount = (max - min) + 1;
+ uint64_t upper_limit = nmax - (nmax % ncount);
+ uint64_t rand_int = get_rng_output();
+ if ( rand_int > upper_limit ){
+ while ( rand_int > upper_limit )
+ rand_int = get_rng_output();
+ }
+ return (unsigned int)(rand_int % max) + min;
+}
diff --git a/extract_decoded_executable/test_binary.c b/extract_decoded_executable/test_binary.c
index 0ac5c4c..6e1b72f 100644
--- a/extract_decoded_executable/test_binary.c
+++ b/extract_decoded_executable/test_binary.c
@@ -1,5 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>