-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile_Operations.cpp
294 lines (212 loc) · 7.66 KB
/
File_Operations.cpp
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
#include <stdio.h>
#include <Windows.h>
#include <Wincrypt.h>
#include <string.h>
#include "File_Operations.h"
//Defines
#define SHA256LEN 32
// Printing System error's
void Error(const char* msg)
{
printf("\n### ERROR ### %s (%d)", msg, ::GetLastError());
exit(EXIT_FAILURE);
}
// Find_file_hash() - Calculates the Hash of the ransomware
void Find_file_hash(char* full_hash_value, char child_process_path[])
{
int result;
OFSTRUCT file_info_struct = { sizeof(file_info_struct) };
HFILE hfile;
hfile = ::OpenFile(child_process_path, &file_info_struct, OF_SHARE_COMPAT);
if (hfile == HFILE_ERROR)
{
Error("OpenFile failed");
}
HCRYPTPROV hProv = 0;
result = ::CryptAcquireContext(
&hProv, // This variable will recive a pointer to a key container within a particular cryptographic service provider (CSP)
NULL, // Name of the container
NULL, // Name of CSP we want. NULL says just use the default
PROV_RSA_AES, // type of provider to acquire. Read about this specific https://docs.microsoft.com/en-us/windows/win32/seccrypto/prov-rsa-full
CRYPT_VERIFYCONTEXT); // Used by applications that perform only hashing.
if (!result)
{
Error("CryptAcquireContext failed");
}
/* ### CryptCreateHash() ### - create a CSP hash object
// Handle to a CSP we created with CryptAcquireContext
// Algorithm ID we want to use
// Must be zero for non-keyed apps
// zero. This parameters isn't really used
// pointer to rhe object that will recive the new hash object
*/
HCRYPTPROV hHash = 0;
result = ::CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash);
if (!result)
{
Error("CryptCreateHash failed");
}
/* ### ReadFile() parameters ###
// Handle to the file. I covert it from HFILE -> HANDLE
// Destination of the bytes we read
// Amount of bytes we want to read
// Number of bytes we were able to read successfully
// Only relavent if the file was opened with FILE_FLAG_OVERLAPPED
*/
BYTE file_output_buffer[1024];
DWORD num_of_bytes_we_read = 0;
while (result = ::ReadFile((HANDLE)hfile, file_output_buffer, 1024, &num_of_bytes_we_read, NULL))
{
// We didn't read any bytes. Probably finished all bytes
if (num_of_bytes_we_read == 0)
{
break;
}
//Inserting data (bytes we read from the file) to the hash object. We can insert as much as we want
result = ::CryptHashData(hHash, file_output_buffer, num_of_bytes_we_read, NULL);
if (!result)
{
Error("CryptHashData failed");
}
}
if (!result)
{
Error("ReadFile failed");
}
/* ### CryptGetHashParam() ### - hash the data in the hash object
// Handle to the hash object
// What we are quering for. We can query for: algorithm ID, HASH size in bytes, Hash value. HP_HASHVAL = hash value.
// Buffer that will recive the actual hash.
// Pointer to a hash lenth variable
// Must be zero
*/
BYTE buffer_of_hash_itself[SHA256LEN];
DWORD hash_lenth = SHA256LEN;
char chars_possible_to_use[] = "0123456789abcdef";
char full_hash_val;
int counter = 0;
result = ::CryptGetHashParam(hHash, HP_HASHVAL, buffer_of_hash_itself, &hash_lenth, 0);
if (result)
{
for (int i = 0; i < SHA256LEN; i++)
{
full_hash_value[counter] = chars_possible_to_use[buffer_of_hash_itself[i] >> 4];
counter += 1;
full_hash_value[counter] = chars_possible_to_use[buffer_of_hash_itself[i] & 0xf];
counter += 1;
//printf("%c%c", chars_possible_to_use[buffer_of_hash_itself[i] >> 4], chars_possible_to_use[buffer_of_hash_itself[i] & 0xf]); ### This will print the hash
}
full_hash_value[counter] = '\0';
}
else
{
Error("CryptGetHashParam failed");
}
//Close all the handles
::CryptDestroyHash(hHash);
::CryptReleaseContext(hProv, 0);
::CloseHandle((HANDLE)hfile);
}
//num_of_lines_in_file() - calculate number of lines in keywords.txt . Presumes there is at least 1 line.
int num_of_lines_in_file(FILE* fp)
{
int lines = 1;
char ch;
while (!feof(fp))
{
ch = fgetc(fp);
if (ch == '\n')
{
lines++;
}
}
fseek(fp, 0, SEEK_SET);
return lines;
}
//get_key_words() - creates a an array of "strings". Each string represents a line in the kewords.txt file.
//Those strings are searched in the process memory.
char** get_key_words(keywords_data* kd)
{
FILE* fp;
fopen_s(&fp, "Keywords.txt", "r");
if (fp == NULL)
{
printf("Unable to open keywords.txt, make sure that file exists");
exit(1);
}
kd->number_of_lines = num_of_lines_in_file(fp);
kd->keywords = (char**)calloc(kd->number_of_lines, sizeof(char*));
if (kd->keywords == NULL)
{
printf("Can't allocate memory from the heap for WCHAR ** keywords");
exit(1);
}
for (int j = 0; j < kd->number_of_lines; j++)
{
kd->keywords[j] = (char*)calloc(60, sizeof(char));
if (kd->keywords[j] == NULL)
{
printf("Can't allocate memory from the heap for kd->keywords[j]");
exit(1);
}
}
char temp_buffer[60];
for (int i = 0; i < kd->number_of_lines; i++)
{
//Replacing the \n of fgets with NULL
memset(temp_buffer, 0, 30);
fgets(temp_buffer, 59, fp);
for (int k = 0; k < strlen(temp_buffer); k++)
{
if (temp_buffer[k] == '\n')
{
temp_buffer[k] = '\0';
break;
}
}
strcpy_s(kd->keywords[i], 59, temp_buffer);
}
fclose(fp);
return kd->keywords;
}
void write_ransom_note(char* buffer_that_holds_only_text_that_is_extracted_from_memory_page, char* lp_strstr_result, keywords_data* kd)
{
//Get Current folder path
char path[MAX_PATH];
if (!::GetCurrentDirectoryA(MAX_PATH, path))
{
Error("GetCurrentDirectoryA failed");
}
//Create an output folder
if (!::CreateDirectoryA("Output_Folder", NULL))
{
if (::GetLastError() != ERROR_ALREADY_EXISTS)
{
Error("Couldn't Create the output folder");
}
}
//Create the "ransom note" file path
FILE* fp;
char dst_relative_file_path[95] = "Output_Folder/";
strcat_s(dst_relative_file_path, 95, kd->dst_filename);
fopen_s(&fp, dst_relative_file_path, "w");
if (fp == NULL)
{
printf("Unable to open the destination file");
exit(1);
}
//Get 800 bytes from the buffer_that_holds_only_text_that_is_extracted_from_memory_page which are likely to hold the ransom note.
int index = lp_strstr_result - buffer_that_holds_only_text_that_is_extracted_from_memory_page;
int start = 0, end = index + 400;
if (index > 401)
{
start = index - 400;
}
int counter = start;
while (buffer_that_holds_only_text_that_is_extracted_from_memory_page[counter] != '\0' && counter != end)
{
fputc(buffer_that_holds_only_text_that_is_extracted_from_memory_page[counter], fp);
counter += 1;
}
fclose(fp);
}