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
|
// (C) Artem Shpynov aka FYR and Igonin Vitaliy aka chaos.persei, 2007 - 2008
#include "commonheaders.h"
int wrongPass = 0;
void* key;
Cryptor* CryptoEngine = NULL;
LIST<CryptoModule> arCryptors(1);
void zero_fill(BYTE * pBuf, size_t bufSize)
{
size_t i;
for (i = 0; i < bufSize; i++)
pBuf[i] = 0;
}
void InitSecurity()
{
Cryptor* (__stdcall *GetCryptor)();
TCHAR tszPath[MAX_PATH];
GetModuleFileName(g_hInst, tszPath, SIZEOF(tszPath));
TCHAR *p = _tcsrchr(tszPath, '\\')+1; _tcscpy(p, _T("cryptors\\*.dll"));
WIN32_FIND_DATA fd;
HANDLE hFile = FindFirstFile(tszPath, &fd);
while (hFile != INVALID_HANDLE_VALUE) {
mir_sntprintf(p, MAX_PATH - (p-tszPath), _T("cryptors\\%s"), fd.cFileName);
HMODULE hLib = LoadLibrary(tszPath);
if (hLib){
GetCryptor = (Cryptor* (__stdcall *)()) GetProcAddress(hLib, "GetCryptor");
if (GetCryptor){
CryptoModule* newItem = (CryptoModule*) malloc(sizeof(CryptoModule));
newItem->cryptor = GetCryptor();
_tcsncpy(newItem->dllname, fd.cFileName, MAX_PATH);
newItem->hLib = hLib;
arCryptors.insert(newItem);
}
else FreeLibrary(hLib);
}
if (!FindNextFile(hFile, &fd))
break;
}
}
void UnloadSecurity()
{
if (CryptoEngine)
CryptoEngine->FreeKey(key);
for (int i = 0; i < arCryptors.getCount(); i++) {
FreeLibrary(arCryptors[i]->hLib);
free(arCryptors[i]);
}
arCryptors.destroy();
}
void CDbxMmapSA::EncoderInit()
{
if (!m_bEncoding) return;
encryptKey[encryptKeyLength] = 0;
key = CryptoEngine->GenerateKey(encryptKey);
}
void CDbxMmapSA::EncodeCopyMemory(void * dst, void * src, size_t size )
{
memcpy(dst, src, size);
if (m_bEncoding)
CryptoEngine->EncryptMem((BYTE *)dst, (int)size, key);
}
void CDbxMmapSA::DecodeCopyMemory(void * dst, void * src, size_t size )
{
memcpy(dst, src, size);
if (m_bEncoding)
CryptoEngine->DecryptMem((BYTE *)dst, (int)size, key);
}
void CDbxMmapSA::EncodeDBWrite(DWORD ofs, void *src, int size)
{
if (m_bEncoding) {
BYTE *buf = (BYTE*)GlobalAlloc(GPTR, sizeof(BYTE)*size);
EncodeCopyMemory(buf, src, size);
DBWrite(ofs, buf, (int)size);
GlobalFree(buf);
}
else DBWrite(ofs, src, (int)size);
}
void CDbxMmapSA::DecodeDBWrite(DWORD ofs, void *src, int size)
{
if (m_bEncoding) {
BYTE *buf = (BYTE*)GlobalAlloc(GPTR, sizeof(BYTE)*size);
DecodeCopyMemory(buf, src, size);
DBWrite(ofs, buf, (int)size);
GlobalFree(buf);
}
else DBWrite(ofs, src, (int)size);
}
int bCheckingPass = 0;
int CDbxMmapSA::CheckPassword(WORD checkWord, TCHAR *szDBName)
{
if (bCheckingPass)
return 0;
bCheckingPass = 1;
int Found = 0;
for (int i = 0; i < arCryptors.getCount(); i++) {
if ( HIWORD(m_dbHeader.version) == arCryptors[i]->cryptor->uid){
CryptoEngine = arCryptors[i]->cryptor;
Found = 1;
break;
}
}
if (!Found){
MessageBox(0, TranslateT("Sorry, but your database encrypted with unknown module"), TranslateT("Error"), MB_OK | MB_ICONERROR);
bCheckingPass = 0;
return 0;
}
while(1) {
DlgStdInProcParam param = { this, szDBName };
int res = DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_LOGIN), NULL, DlgStdInProc, (LPARAM)¶m);
if (res == IDCANCEL) {
wrongPass = 0;
bCheckingPass = 0;
return 0;
}
if (encryptKeyLength < 1)
continue;
EncoderInit();
WORD ver;
DecodeCopyMemory(&ver, &checkWord, sizeof(checkWord));
if (ver == 0x5195) {
wrongPass = 0;
bCheckingPass = 0;
return 1;
}
wrongPass++;
}
bCheckingPass = 0;
}
int SelectEncoder()
{
if (arCryptors.getCount() == 0){
MessageBox(0, TranslateT("Crypto modules not found"), TranslateT("Error"), MB_OK | MB_ICONERROR);
return 1;
}
WORD uid = db_get_w(NULL, "SecureMMAP", "CryptoModule", 0);
if (uid != 0) {
for (int i = 0; i < arCryptors.getCount(); i++) {
if (arCryptors[i]->cryptor->uid == uid) {
CryptoEngine = arCryptors[i]->cryptor;
return 0;
}
}
}
db_set_w(NULL, "SecureMMAP", "CryptoModule", arCryptors[0]->cryptor->uid);
CryptoEngine = arCryptors[0]->cryptor;
return 0;
}
void CDbxMmapSA::EncodeAll()
{
for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
EncodeContactEvents(hContact);
EncodeContactSettings(hContact);
}
EncodeContactEvents(NULL);
EncodeContactSettings(NULL);
}
void CDbxMmapSA::DecodeAll()
{
for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
DecodeContactEvents(hContact);
DecodeContactSettings(hContact);
}
DecodeContactEvents(NULL);
DecodeContactSettings(NULL);
}
void CDbxMmapSA::WritePlainHeader()
{
DWORD bytesWritten;
memcpy(m_dbHeader.signature, &dbSignatureNonSecured, sizeof(m_dbHeader.signature));
SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN);
WriteFile(m_hDbFile,m_dbHeader.signature,sizeof(m_dbHeader.signature),&bytesWritten,NULL);
m_dbHeader.version = MAKELONG(0x0700, 0x0000); //no encryption
WriteFile(m_hDbFile,&m_dbHeader.version, sizeof(m_dbHeader.version),&bytesWritten,NULL);
}
void CDbxMmapSA::WriteCryptHeader()
{
DWORD bytesWritten;
memcpy(m_dbHeader.signature, &dbSignatureSecured, sizeof(m_dbHeader.signature));
SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN);
WriteFile(m_hDbFile,m_dbHeader.signature,sizeof(m_dbHeader.signature),&bytesWritten,NULL);
WORD checkWord = 0x5195, cryptWord;
EncodeCopyMemory(&cryptWord, &checkWord, sizeof(checkWord));
m_dbHeader.version = MAKELONG(cryptWord, CryptoEngine->uid);
WriteFile(m_hDbFile,&m_dbHeader.version, sizeof(m_dbHeader.version),&bytesWritten,NULL);
}
void CDbxMmapSA::EncryptDB()
{
int action = 0;
if (bEncProcess)
return;
if (memcmp(m_dbHeader.signature, &dbSignatureSecured, sizeof(m_dbHeader.signature)) == 0){
MessageBox(0, TranslateT("DB is already secured!"), TranslateT("Error"), MB_OK | MB_ICONERROR);
return;
}
if (SelectEncoder())
return;
bEncProcess = 1;
action = DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_NEWPASS), NULL, DlgStdNewPass, (LPARAM)this);
if (action != IDOK || !strlen(encryptKey)) {
bEncProcess = 0;
db_set_b(NULL, "SecureMMAP", "CryptoModule", 0);
return;
}
EnterCriticalSection(&m_csDbAccess);
m_bEncoding = 1;
EncoderInit();
EncodeAll();
LeaveCriticalSection(&m_csDbAccess);
WriteCryptHeader();
xModifyMenu(hSetPwdMenu, 0, LPGENT("Change Password"), 0);
bEncProcess = 0;
}
void CDbxMmapSA::DecryptDB()
{
char oldKey[255];
strcpy(oldKey, encryptKey);
if ( !CheckPassword( LOWORD(m_dbHeader.version), TranslateT("current database"))) {
strcpy(encryptKey, oldKey);
encryptKeyLength = strlen(oldKey);
return;
}
WritePlainHeader();
EnterCriticalSection(&m_csDbAccess);
DecodeAll();
LeaveCriticalSection(&m_csDbAccess);
m_bEncoding = 0;
zero_fill((BYTE *)encryptKey, sizeof encryptKey);
xModifyMenu(hSetPwdMenu, 0, LPGENT("Set Password"), 0);
db_set_w(NULL, "SecureMMAP", "CryptoModule", 0);
CryptoEngine->FreeKey(key);
CryptoEngine = NULL;
}
void CDbxMmapSA::RecryptDB()
{
EnterCriticalSection(&m_csDbAccess);
DecodeAll();
CryptoEngine->FreeKey(key);
SelectEncoder();
m_bEncoding = 1;
EncoderInit();
EncodeAll();
WriteCryptHeader();
LeaveCriticalSection(&m_csDbAccess);
}
void CDbxMmapSA::ChangePwd()
{
char newpass[255] = {0};
DlgChangePassParam param = { this, newpass };
int action = DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_CHANGEPASS), NULL, DlgChangePass, (LPARAM)¶m);
if (action == IDCANCEL || (action == IDOK && !strlen(newpass)))
return;
EnterCriticalSection(&m_csDbAccess);
DecodeAll();
CryptoEngine->FreeKey(key);
if (action == IDREMOVE){
WritePlainHeader();
m_bEncoding = 0;
CryptoEngine = NULL;
db_set_w(NULL, "SecureMMAP", "CryptoModule", 0);
zero_fill((BYTE *)encryptKey, sizeof encryptKey);
xModifyMenu(hSetPwdMenu, 0, LPGENT("Set Password"), 0);
}
if (action == IDOK){
strcpy(encryptKey, newpass);
encryptKeyLength = strlen(newpass);
m_bEncoding = 1;
EncoderInit();
EncodeAll();
WriteCryptHeader();
}
zero_fill((BYTE *)newpass, sizeof newpass);
LeaveCriticalSection(&m_csDbAccess);
}
|