File size: 3,338 Bytes
21caab1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include "curve25519-donna.h"
#include "sosemanuk.h"
#include "sha256.h"

static uint8_t basepoint[32] = { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

static uint8_t m_priv[32] = {
        'c', 'u', 'r', 'v', 'p', 'a', 't', 't', 'e', 'r', 'n', 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

#define CONST_1MB 1048576ll
#define CONST_BLOCK (CONST_1MB * 10)

void decrypt_file(char* path) {
	unsigned long wholeReaded = 0;
	size_t readed = 0;
	
	uint8_t u_publ[32];
	uint8_t u_secr[32];
	uint8_t sm_key[32];
	struct stat64 fstat;
	
	sha256_context sc;
	sosemanuk_key_context kc;
	sosemanuk_run_context rc;
	
	if(stat64(path, &fstat) == 0){
		if(fstat.st_size > 32) {
			if (FILE *fp_key = fopen(path, "r+b")) {
				if(fseek(fp_key, -32, SEEK_END) == 0) {
					fread(u_publ, 1, 32, fp_key);
					curve25519_donna(u_secr, m_priv, u_publ);
					
					sha256_init(&sc);
					sha256_hash(&sc, u_secr, 32);
					sha256_done(&sc, sm_key);
					
					sosemanuk_schedule(&kc, sm_key, 32);
					sosemanuk_init(&rc, &kc, 0, 0);
				}
				fclose(fp_key);
				truncate64(path, fstat.st_size - 32);
				
				if (FILE *fp = fopen(path, "r+b")) {
					if(uint8_t* f_data = (uint8_t*)malloc(CONST_BLOCK)) {
						do {
							wholeReaded += readed = fread(f_data, 1, CONST_BLOCK, fp);
							if(readed) {
								sosemanuk_encrypt(&rc, f_data, f_data, readed);
								fseek(fp, -readed, SEEK_CUR);
								fwrite(f_data, 1, readed, fp);
							} else break;
						} while(wholeReaded < 0x20000000 && wholeReaded < (fstat.st_size - 32));
						
						free(f_data);
					}
					fclose(fp);
					
					char unlocked_name[4096];
					strcpy(unlocked_name, path);
					for (int i = strlen(unlocked_name); i >= 0; i--) {
						if (unlocked_name[i] == '.') {
							unlocked_name[i] = 0;
							break;
						}
					}
					rename(path, unlocked_name);
				}
			}
		}
	}
}

void find_files_recursive(char* begin) {
	if(char* path = (char*)malloc(4096)) {
		strcpy(path, begin);
		if (DIR* entry = opendir(path)) {
			dirent* record = 0;
			while ((record = readdir(entry)) != NULL) {
				if(strcmp(record->d_name, "..") != 0 && strcmp(record->d_name, ".") != 0) {
					if(record->d_type == DT_DIR) {
						strcpy(path, begin);
						strcat(path, "/");
						strcat(path, record->d_name);
						find_files_recursive(path);
					} else if(record->d_type == DT_REG) {
						if(strstr(record->d_name, ".babyk") != 0) {
							strcpy(path, begin);
							strcat(path, "/");
							strcat(path, record->d_name);
							
							printf("Decrypting: %s\n", path);
							decrypt_file(path);
						}
					}
				}
			}
			closedir(entry);
		}
		
		strcpy(path, begin);
		strcat(path, "/How To Restore Your Files.txt");
		unlink(path);
		
		free(path);
	}
}

int main(int argc, char* argv[]) {
	if(argc == 2) {
		find_files_recursive(argv[1]);
	} else {
		printf("Usage: %s /begin/path\n", argv[0]);
	}
	return(0);
}