diff options
Diffstat (limited to 'extract_decoded_executable/encrypter.c')
| -rw-r--r-- | extract_decoded_executable/encrypter.c | 70 |
1 files changed, 70 insertions, 0 deletions
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; +} |
