summaryrefslogtreecommitdiff
path: root/extract_decoded_executable/encrypter.c
blob: 34732cbf153e881829299d4c22089cd54c83bc1c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
}