-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDecrypt.cpp
404 lines (393 loc) · 12.6 KB
/
Decrypt.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/************************************************************************
* decrypt.cpp *
* This class adds some simple block decryption/encryption with file *
* patching to Borg. By storing decryptors in blocks it is possible to *
* reconstruct the file when it is saved to database and reloaded, even *
* if some patches were applied to the file and some were not. *
* Added in Borg 2.19 *
* NB Any future general file patching will need to be included in this *
* class and saved in a similar way. This opens up the way to decrypting *
* patching and reencrypting within Borg :) *
* Current decryptors are fairly simple, but when used in combination *
* they are very powerful. The Xadd is a bit obscure, but could be *
* simply modified as needed, and recompiled for some powerful routines. *
************************************************************************/
#include <windows.h>
#include <stdio.h>
#include "decrypt.h"
#include "data.h"
#include "disio.h"
#include "disasm.h"
#include "exeload.h"
#include "debug.h"
/************************************************************************
* constructor function *
* - resets a few global variables *
************************************************************************/
decrypt::decrypt()
{ nextitemnum=1;
loading_db=false;
}
/************************************************************************
* destructor function *
* - currently null *
************************************************************************/
decrypt::~decrypt()
{
}
/************************************************************************
* compare function for decryptor *
* - these are simply stored in uid order, the uid being increased each *
* time a new one is applied *
************************************************************************/
int decrypt::compare(declist *i,declist *j)
{ if(i->uid == j->uid)
return 0;
if(i->uid > j->uid)
return 1;
return -1;
}
/************************************************************************
* add_decrypted *
* - just adds another item to the decrypt list *
* - the decrypt list is simply a list of blocks and how they were *
* changed and whether the exe was patched *
* - the list is just to enable reconstruction of the state of the file *
* on saving and loading databases with decryptors which may or may *
* not have been saved to the exe file *
************************************************************************/
dword decrypt::add_decrypted(lptr dstart,lptr dend,dectype t,ditemtype ditem,dword val,lptr adr,bool patchedexe)
{ declist *ndec;
ndec=new struct declist;
ndec->dec_start=dstart;
ndec->dec_end=dend;
ndec->typ=t;
ndec->dlength=ditem;
ndec->value=val;
ndec->arrayaddr=adr;
ndec->patch=patchedexe;
ndec->uid=nextitemnum;
nextitemnum++;
addto(ndec);
return ndec->uid;
}
/************************************************************************
* process_dec *
* - this processes a decryptor given the uid, actually applying it to *
* the file in memory. If a block contains any disassembly then this *
* is also deleted. *
************************************************************************/
void decrypt::process_dec(dword dec_id)
{ declist fnd,*patch;
dsegitem *pseg,*aseg;
lptr cpos;
unsigned int plen,ctr;
dword doitval,lval,tval;
fnd.uid=dec_id;
patch=find(&fnd);
if(patch==NULL)
return;
if(patch->uid!=dec_id)
return;
pseg=dta.findseg(patch->dec_start);
if(pseg==NULL)
return;
ctr=0;
switch(patch->dlength)
{ case decbyte:
plen=1;
break;
case decword:
plen=2;
break;
case decdword:
plen=4;
break;
case decarray:
plen=1;
aseg=dta.findseg(patch->arrayaddr);
if(aseg==NULL)
return;
ctr=patch->arrayaddr-aseg->addr;
break;
default:
plen=1;
break;
}
cpos=patch->dec_start;
lval=patch->value;
while(cpos<=patch->dec_end)
{ // check within seg, and move to the next seg if we arent
while(cpos>pseg->addr+(pseg->size-plen))
{ cpos=pseg->addr+(pseg->size-1);
dta.nextseg(&cpos);
if(!cpos.segm)
break;
if(cpos>patch->dec_end)
break;
pseg=dta.findseg(cpos);
}
if(!cpos.segm)
break;
if(cpos>patch->dec_end)
break;
switch(plen)
{ case 1:
doitval=((byte *)(pseg->data+(cpos-pseg->addr)))[0];
break;
case 2:
doitval=((word *)(pseg->data+(cpos-pseg->addr)))[0];
break;
case 4:
doitval=((dword *)(pseg->data+(cpos-pseg->addr)))[0];
break;
}
if(patch->dlength==decarray)
{ if(ctr+plen>aseg->size)
break;
switch(plen)
{ case 1:
patch->value=((byte *)(aseg->data+ctr))[0];
break;
case 2:
patch->value=((word *)(aseg->data+ctr))[0];
break;
case 4:
patch->value=((dword *)(aseg->data+ctr))[0];
break;
}
}
switch(patch->typ)
{ case decxor:
doitval=doitval^patch->value;
break;
case decmul:
doitval=doitval*patch->value;
break;
case decadd:
doitval=doitval+patch->value;
break;
case decsub:
doitval=doitval-patch->value;
break;
case decxadd:
tval=doitval;
doitval=lval;
lval=tval;
doitval=doitval+lval;
break;
case decrot:
switch(plen)
{ case 1:
doitval=(doitval<<(patch->value&0x07))+(doitval>>(8-(patch->value&0x07)));
break;
case 2:
doitval=(doitval<<(patch->value&0x0f))+(doitval>>(16-(patch->value&0x0f)));
break;
case 4:
doitval=(doitval<<(patch->value&0x1f))+(doitval>>(32-(patch->value&0x1f)));
break;
}
break;
default:
break;
}
switch(plen)
{ case 1:
((byte *)(pseg->data+(cpos-pseg->addr)))[0]=(byte)doitval;
break;
case 2:
((word *)(pseg->data+(cpos-pseg->addr)))[0]=(word)doitval;
break;
case 4:
((dword *)(pseg->data+(cpos-pseg->addr)))[0]=doitval;
break;
}
cpos+=plen;
ctr+=plen;
}
if(!loading_db)
dsm.undefineblock(patch->dec_start,patch->dec_end);
dio.updatewindowifwithinrange(patch->dec_start,patch->dec_end);
}
/************************************************************************
* exepatch *
* - given a uid this steps through a decryptor and writes the patch to *
* the exe file. *
************************************************************************/
void decrypt::exepatch(dword dec_id)
{ declist fnd,*patch;
dsegitem *pseg;
lptr cpos;
int plen;
dword doitval;
fnd.uid=dec_id;
patch=find(&fnd);
if(patch==NULL)
return;
if(patch->uid!=dec_id)
return;
pseg=dta.findseg(patch->dec_start);
switch(patch->dlength)
{ case decbyte:
plen=1;
break;
case decword:
plen=2;
break;
case decdword:
plen=4;
break;
case decarray:
plen=1;
break;
default:
plen=1;
break;
}
cpos=patch->dec_start;
while(cpos<=patch->dec_end)
{ // check within seg, and move to the next seg if we arent
while(cpos>pseg->addr+(pseg->size-plen))
{ cpos=pseg->addr+(pseg->size-1);
dta.nextseg(&cpos);
if(!cpos.segm)
break;
if(cpos>patch->dec_end)
break;
pseg=dta.findseg(cpos);
}
if(!cpos.segm)
break;
if(cpos>patch->dec_end)
break;
doitval=floader.fileoffset(cpos);
// write patch
switch(plen)
{ case 1:
floader.patchfile(doitval,1,pseg->data+(cpos-pseg->addr));
break;
case 2:
floader.patchfile(doitval,2,pseg->data+(cpos-pseg->addr));
break;
case 4:
floader.patchfile(doitval,4,pseg->data+(cpos-pseg->addr));
break;
default:
floader.patchfile(doitval,1,pseg->data+(cpos-pseg->addr));
break;
}
cpos+=plen;
}
}
/************************************************************************
* process_reload *
* - given a uid this steps through a patch and re-reads the bytes in *
* that would have been changed. This is used in file reconstruction *
************************************************************************/
void decrypt::process_reload(dword dec_id)
{ declist fnd,*patch;
dsegitem *pseg;
lptr cpos;
int plen;
dword doitval;
fnd.uid=dec_id;
patch=find(&fnd);
if(patch==NULL)
return;
if(patch->uid!=dec_id)
return;
pseg=dta.findseg(patch->dec_start);
switch(patch->dlength)
{ case decbyte:
plen=1;
break;
case decword:
plen=2;
break;
case decdword:
plen=4;
break;
case decarray:
plen=1;
break;
default:
plen=1;
break;
}
cpos=patch->dec_start;
while(cpos<=patch->dec_end)
{ // check within seg, and move to the next seg if we arent
while(cpos>pseg->addr+(pseg->size-plen))
{ cpos=pseg->addr+(pseg->size-1);
dta.nextseg(&cpos);
if(!cpos.segm)
break;
if(cpos>patch->dec_end)
break;
pseg=dta.findseg(cpos);
}
if(!cpos.segm)
break;
if(cpos>patch->dec_end)
break;
doitval=floader.fileoffset(cpos);
// write patch
switch(plen)
{ case 1:
floader.reloadfile(doitval,1,pseg->data+(cpos-pseg->addr));
break;
case 2:
floader.reloadfile(doitval,2,pseg->data+(cpos-pseg->addr));
break;
case 4:
floader.reloadfile(doitval,4,pseg->data+(cpos-pseg->addr));
break;
default:
floader.reloadfile(doitval,1,pseg->data+(cpos-pseg->addr));
break;
}
cpos+=plen;
}
}
/************************************************************************
* write_item *
* - writes a decrypt item to the savefile specified *
* uses the current item, and moves the iterator on *
************************************************************************/
bool decrypt::write_item(savefile *sf)
{ declist *currdec;
currdec=nextiterator();
if(!sf->swrite(currdec,sizeof(declist)))
return false;
return true;
}
/************************************************************************
* read_item *
* - read a decrypt item from the savefile specified *
* adds it to the list and restores any patch *
* If we find a decryptor which was saved to disk then we *
* reload that block from the exe file. In this way after all of the *
* decryptors have been loaded we have synchronised the file in memory *
* to the file on disk, plus any further patches made but not written *
* to disk. [Any byte in the file was synchronised to the file at the *
* time of the last patch which was written to file. Subsequent *
* patches have been made to memory only, and are just redone. So the *
* loaded file is in the same state as when the database was saved] *
************************************************************************/
bool decrypt::read_item(savefile *sf)
{ dword num;
declist *currdec;
currdec=new declist;
if(!sf->sread(currdec,sizeof(declist),&num))
return false;
addto(currdec);
nextitemnum=currdec->uid+1;
loading_db=true;
if(!currdec->patch)
process_dec(currdec->uid);
else
process_reload(currdec->uid);
loading_db=false;
return true;
}