-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathntreg.c
4363 lines (3465 loc) · 119 KB
/
ntreg.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ntreg.c - Windows (NT and up) Registry Hive access library
* should be able to handle most basic functions:
* iterate, add&delete keys and values, read stuff, change stuff etc
* no rename of keys or values yet..
* also contains some minor utility functions (string handling etc) for now
*
* 2014-jan: openhive() now more compatible with build on non-unix?
* 2013-aug: Enter buil-in buffer debugger only if in trace mode, else return error or abort()
* 2013-may-aug: Fixed critical bug in del_value which could
* thrash the hive when removing value in bottom of key.
* And a pointer not reinitialized when buffer reallocated in some cases, fixed.
* Thanks to Jacky To for reporting those two.
* Some minor adjustments for compiler. A few more utility functions.
* 2012-oct: Added set_val_type. some minor changes.
* 2011-may: Seems like large values >16k or something like that is split
* into several blocks (db), have tried to implement that.
* Vista seems to accept it. Not tested on others yet.
* 2011-may: Expansion now seems to be working, have lot of test accepted by
* vista and win7. But no warranties..
* 2011-may: Found a couple of nasty bugs inn add_key(), making Vista and newer
* reject (remove) keys on hive load.
* May have been there for a long time according to reports.
* 2011-apr: Fixed some problems with the allocator when at the end of a hbin.
* 2011-apr: .reg file import. Ugly one, but it seems to work. Found
* quite a lot of bugs in other places while testing it.
* String handling when international characters or wide (UTF-16)
* is a pain, and very ugly. May not work in some cases.
* Will keep wide (16 bit) characters in strings when importing from
* .reg file that has it, like what regedit.exe generates for example.
* 2011-apr: Added routines for hive expansion. Will add new hbin at end of file
* when needed. If using library, please read ugly warnings in "alloc_block()".
* 2010-jun: Patches from Frediano Ziglio adding support for wide characters
* and some bugfixes. Thank you!
* 2008-mar: Type QWORD (XP/Vista and newer) now recognized
* 2008-mar: Most functions accepting a path now also have a parameter specifying if
* the search should be exact or on first match basis
* 2008-mar: Fixed bug which skipped first indirect index table when deleting keys,
* usually leading to endless loop when recursive deleting.
* 2008-mar: Export to .reg file by Leo von Klenze, expanded a bit by me.
* 2008-mar: 64 bit compatible patch by Mike Doty, via Alon Bar-Lev
* http://bugs.gentoo.org/show_bug.cgi?id=185411
* 2007-sep: Verbosity/debug messages minor changes
* 2007-apr: LGPL license.
* 2004-aug: Deep indirect index support. NT351 support. Recursive delete.
* Debugged a lot in allocation routines. Still no expansion.
* 2004-jan: Verbosity updates
* 2003-jan: Allocation of new data, supports adding/deleting keys & stuff.
* Missing is expanding the file.
* 2003-jan: Seems there may be garbage pages at end of file, not zero pages
* now stops enumerating at first non 'hbin' page.
*
* NOTE: The API is not frozen. It can and will change every release.
*
*****
*
* NTREG - Window registry file reader / writer library
* Copyright (c) 1997-2014 Petter Nordahl-Hagen.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* See file LGPL.txt for the full license.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <stdarg.h>
#include "ntreg.h"
/* Set to abort() and debug on more critical errors */
#define DOCORE 1
#define ZEROFILL 1 /* Fill blocks with zeroes when allocating and deallocating */
#define ZEROFILLONLOAD 0 /* Fill blocks marked as unused/deallocated with zeroes on load. FOR DEBUG */
const char ntreg_version[] = "ntreg lib routines, v0.95 140201, (c) Petter N Hagen";
const char *val_types[REG_MAX+1] = {
"REG_NONE", "REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", "REG_DWORD", /* 0 - 4 */
"REG_DWORD_BIG_ENDIAN", "REG_LINK", /* 5 - 6 */
"REG_MULTI_SZ", "REG_RESOUCE_LIST", "REG_FULL_RES_DESC", "REG_RES_REQ", /* 7 - 10 */
"REG_QWORD", /* 11 */
};
static char * string_prog2regw(void *string, int len, int* out_len);
/* Utility routines */
/* toupper() table for registry hashing functions, so we don't have to
* dependent upon external locale lib files
*/
static const unsigned char reg_touppertable[] = {
/* ISO 8859-1 is probably not the one.. */
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 0x00-0x07 */
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 0x08-0x0f */
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 0x10-0x17 */
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 0x18-0x1f */
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 0x20-0x27 */
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 0x28-0x2f */
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 0x30-0x37 */
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 0x38-0x3f */
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 0x40-0x47 */
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 0x48-0x4f */
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 0x50-0x57 */
0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 0x58-0x5f */
0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 0x60-0x67 */
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 0x68-0x6f */
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 0x70-0x77 */
0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 0x78-0x7f */
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 0x80-0x87 */
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 0x88-0x8f */
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 0x90-0x97 */
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 0x98-0x9f */
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* 0xa0-0xa7 */
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* 0xa8-0xaf */
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0x00, 0xb6, 0xb7, /* 0xb0-0xb7 */
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* 0xb8-0xbf */
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 0xc0-0xc7 */
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* 0xc8-0xcf */
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, /* 0xd0-0xd7 */
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, /* 0xd8-0xdf */
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 0xe0-0xe7 */
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* 0xe8-0xef */
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xf7, /* 0xf0-0xf7 */
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00, /* 0xf8-0xff */
};
/* Use table above in strcasecmp else add_key may put names in wrong order
and windows actually verifies that on hive load!!
or at least it finds out in some cases..
*/
int strn_casecmp(const char *s1, const char *s2, size_t n)
{
char r;
while ( *s1 && *s2 && n ) {
r = (unsigned char)reg_touppertable[(unsigned char)*s1] - (unsigned char)reg_touppertable[(unsigned char)*s2];
if (r) return(r);
n--;
s1++;
s2++;
}
if ( (!*s1 && !*s2) || !n) return(0);
if ( !*s1 ) return(-1);
return(1);
}
char *str_dup( const char *str )
{
char *str_new;
if (!str) return(0);
CREATE( str_new, char, strlen(str) + 1 );
strcpy( str_new, str );
return str_new;
}
char *str_cat(char *str, char *add)
{
if (!add) return(str);
str = (char *) realloc(str, strlen(str) + strlen(add) + 3);
strcat (str, add);
return (str);
}
char *str_catf(char *str, const char *format, ... )
{
va_list ap;
char add[640] ;
va_start(ap, format) ;
vsprintf(add, format, ap) ;
va_end(ap) ;
str = (char *) realloc(str, strlen(str) + strlen(add) + 3);
strcat (str, add );
return(str);
}
/* Copy non-terminated string to buffer we allocate and null terminate it
* Uses length only, does not check for nulls
*/
char *mem_str( const char *str, int len )
{
char *str_new;
if (!str)
return 0 ;
CREATE( str_new, char, len + 1 );
memcpy( str_new, str, len);
*(str_new+len) = 0;
return str_new;
}
int fmyinput(char *prmpt, char *ibuf, int maxlen)
{
printf("%s",prmpt);
fgets(ibuf,maxlen+1,stdin);
ibuf[strlen(ibuf)-1] = 0;
return(strlen(ibuf));
}
/* Print len number of hexbytes */
void hexprnt(char *s, unsigned char *bytes, int len)
{
int i;
printf("%s",s);
for (i = 0; i < len; i++) {
printf("%02x ",bytes[i]);
}
printf("\n");
}
/* HexDump all or a part of some buffer */
void hexdump(char *hbuf, int start, int stop, int ascii)
{
char c;
int diff,i;
while (start < stop ) {
diff = stop - start;
if (diff > 16) diff = 16;
printf(":%05X ",start);
for (i = 0; i < diff; i++) {
printf("%02X ",(unsigned char)*(hbuf+start+i));
}
if (ascii) {
for (i = diff; i < 16; i++) printf(" ");
for (i = 0; i < diff; i++) {
c = *(hbuf+start+i);
printf("%c", isprint(c) ? c : '.');
}
}
printf("\n");
start += 16;
}
}
/* General search routine, find something in something else */
int find_in_buf(char *buf, char *what, int sz, int len, int start)
{
int i;
for (; start < sz; start++) {
for (i = 0; i < len; i++) {
if (*(buf+start+i) != *(what+i)) break;
}
if (i == len) return(start);
}
return(0);
}
/* Get INTEGER from memory. This is probably low-endian specific? */
int get_int( char *array )
{
return ((array[0]&0xff) + ((array[1]<<8)&0xff00) +
((array[2]<<16)&0xff0000) +
((array[3]<<24)&0xff000000));
}
/* Quick and dirty UNICODE to std. ascii */
void cheap_uni2ascii(char *src, char *dest, int l)
{
for (; l > 0; l -=2) {
*dest = *src;
dest++; src +=2;
}
*dest = 0;
}
/* Quick and dirty ascii to unicode */
void cheap_ascii2uni(char *src, char *dest, int l)
{
for (; l > 0; l--) {
*dest++ = *src++;
*dest++ = 0;
}
}
void skipspace(char **c)
{
while( **c == ' ' ) (*c)++;
}
int gethex(char **c)
{
int value;
skipspace(c);
if (!(**c)) return(0);
sscanf(*c,"%x",&value);
while( **c != ' ' && (**c)) (*c)++;
return(value);
}
/* Get a string of HEX bytes (space separated),
* or if first char is ' get an ASCII string instead.
*/
int gethexorstr(char **c, char *wb)
{
int l = 0;
skipspace(c);
if ( **c == '\'') {
(*c)++;
while ( **c ) {
*(wb++) = *((*c)++);
l++;
}
} else {
do {
*(wb++) = gethex(c);
l++;
skipspace(c);
} while ( **c );
}
return(l);
}
/* Simple buffer debugger, returns 1 if buffer dirty/edited */
int debugit(char *buf, int sz)
{
char inbuf[100],whatbuf[100],*bp;
int dirty=0,to,from,l,i,j,wlen,cofs = 0;
printf("Buffer debugger. '?' for help.\n");
while (1) {
l = fmyinput(".",inbuf,90);
bp = inbuf;
skipspace(&bp);
if (l > 0 && *bp) {
switch(*bp) {
case 'd' :
bp++;
if (*bp) {
from = gethex(&bp);
to = gethex(&bp);
} else {
from = cofs; to = 0;
}
if (to == 0) to = from + 0x100;
if (to > sz) to = sz;
hexdump(buf,from,to,1);
cofs = to;
break;
case 'a' :
bp++;
if (*bp) {
from = gethex(&bp);
to = gethex(&bp);
} else {
from = cofs; to = 0;
}
if (to == 0) to = from + 0x100;
if (to > sz) to = sz;
hexdump(buf,from,to,0);
cofs = to;
break;
case 'q':
return(0);
break;
case 's':
if (!dirty) printf("Buffer has not changed, no need to write..\n");
return(dirty);
break;
case 'h':
bp++;
if (*bp == 'a') {
from = 0;
to = sz;
bp++;
} else {
from = gethex(&bp);
to = gethex(&bp);
}
wlen = gethexorstr(&bp,whatbuf);
if (to > sz) to = sz;
printf("from: %x, to: %x, wlen: %d\n",from,to,wlen);
for (i = from; i < to; i++) {
for (j = 0; j < wlen; j++) {
if ( *(buf+i+j) != *(whatbuf+j)) break;
}
if (j == wlen) printf("%06x ",i);
}
printf("\n");
break;
case ':':
bp++;
if (!*bp) break;
from = gethex(&bp);
wlen = gethexorstr(&bp,whatbuf);
printf("from: %x, wlen: %d\n",from,wlen);
memcpy(buf+from,whatbuf,wlen);
dirty = 1;
break;
case '?':
printf("d [<from>] [<to>] - dump buffer within range\n");
printf("a [<from>] [<to>] - same as d, but without ascii-part (for cut'n'paste)\n");
printf(": <offset> <hexbyte> [<hexbyte> ...] - change bytes\n");
printf("h <from> <to> <hexbyte> [<hexbyte> ...] - hunt (search) for bytes\n");
printf("ha <hexbyte> [<hexbyte] - Hunt all (whole buffer)\n");
printf("s - save & quit\n");
printf("q - quit (no save)\n");
printf(" instead of <hexbyte> etc. you may give 'string to enter/search a string\n");
break;
default:
printf("?\n");
break;
}
}
}
}
/* Utility function to copy and append two values into a new one
* Will allocate new buffer, but not touch the input ones
*/
struct keyval *reg_valcat(struct keyval *a, struct keyval *b)
{
int newsize = 0;
int asize = 0;
int bsize = 0;
struct keyval *result = NULL;
if (!a && !b) return(NULL);
if (a) asize = a->len;
if (b) bsize = b->len;
newsize = asize + bsize;
// printf("asize = %d, bsize = %d, newsize = %d\n",asize,bsize,newsize);
ALLOC(result, sizeof(struct keyval) + newsize, 1);
if (asize) memcpy(&result->data, &a->data, asize);
if (bsize) memcpy(&result->data + asize / sizeof(int), &b->data, bsize);
result->len = newsize;
//printf("reg_valcat done\n");
return(result);
}
/* ========================================================================= */
/* The following routines are mostly for debugging, I used it
* much during discovery. the -t command line option uses it,
* also the 'st' and 's' from the editor & hexdebugger.
* All offsets shown in these are unadjusted (ie you must add
* headerpage (most often 0x1000) to get file offset)
*/
/* Parse the nk datablock
* vofs = offset into struct (after size linkage)
*/
void parse_nk(struct hive *hdesc, int vofs, int blen)
{
struct nk_key *key;
int i;
printf("== nk at offset %0x\n",vofs);
/* #define D_OFFS2(o) ( (void *)&(key->o)-(void *)hdesc->buffer-vofs ) */
#define D_OFFS(o) ( (void *)&(key->o)-(void *)hdesc->buffer-vofs )
key = (struct nk_key *)(hdesc->buffer + vofs);
printf("%04x type = 0x%02x %s\n", D_OFFS(type) ,key->type,
(key->type == KEY_ROOT ? "ROOT_KEY" : "") );
printf("%04x timestamp skipped\n", D_OFFS(timestamp) );
printf("%04x parent key offset = 0x%0x\n", D_OFFS(ofs_parent) ,key->ofs_parent + 0x1000);
printf("%04x number of subkeys = %d\n", D_OFFS(no_subkeys),key->no_subkeys);
printf("%04x lf-record offset = 0x%0x\n",D_OFFS(ofs_lf),key->ofs_lf + 0x1000);
printf("%04x number of values = %d\n", D_OFFS(no_values),key->no_values);
printf("%04x val-list offset = 0x%0x\n",D_OFFS(ofs_vallist),key->ofs_vallist + 0x1000);
printf("%04x sk-record offset = 0x%0x\n",D_OFFS(ofs_sk),key->ofs_sk + 0x1000);
printf("%04x classname offset = 0x%0x\n",D_OFFS(ofs_classnam),key->ofs_classnam + 0x1000);
printf("%04x dummy3 = 0x%0x (%d)\n",D_OFFS(dummy3),key->dummy3,key->dummy3);
printf("%04x dummy4 = 0x%0x (%d)\n",D_OFFS(dummy4),key->dummy4,key->dummy4);
printf("%04x dummy5 = 0x%0x (%d)\n",D_OFFS(dummy5),key->dummy5,key->dummy5);
printf("%04x dummy6 = 0x%0x (%d)\n",D_OFFS(dummy6),key->dummy6,key->dummy6);
printf("%04x dummy7 = 0x%0x (%d)\n",D_OFFS(dummy7),key->dummy7,key->dummy7);
printf("%04x name length = %d\n", D_OFFS(len_name),key->len_name);
printf("%04x classname length = %d\n", D_OFFS(len_classnam),key->len_classnam);
printf("%04x Key name: <",D_OFFS(keyname) );
for(i = 0; i < key->len_name; i++) putchar(key->keyname[i]);
printf(">\n== End of key info.\n");
}
/* Parse the vk datablock
* vofs = offset into struct (after size linkage)
*/
void parse_vk(struct hive *hdesc, int vofs, int blen)
{
struct vk_key *key;
int i;
printf("== vk at offset %0x\n",vofs);
key = (struct vk_key *)(hdesc->buffer + vofs);
printf("%04x name length = %d (0x%0x)\n", D_OFFS(len_name),
key->len_name, key->len_name );
printf("%04x length of data = %d (0x%0x)\n", D_OFFS(len_data),
key->len_data, key->len_data );
printf("%04x data offset = 0x%0x\n",D_OFFS(ofs_data),key->ofs_data + 0x1000);
printf("%04x value type = 0x%0x %s\n", D_OFFS(val_type), key->val_type,
(key->val_type <= REG_MAX ? val_types[key->val_type] : "(unknown)") ) ;
printf("%04x flag = 0x%0x\n",D_OFFS(flag),key->flag);
printf("%04x *unused?* = 0x%0x\n",D_OFFS(dummy1),key->dummy1);
printf("%04x Key name: <",D_OFFS(keyname) );
for(i = 0; i < key->len_name; i++) putchar(key->keyname[i]);
printf(">\n== End of key info.\n");
}
/* Parse the sk datablock
* Gee, this is the security info. Who cares? *evil grin*
* vofs = offset into struct (after size linkage)
*/
void parse_sk(struct hive *hdesc, int vofs, int blen)
{
struct sk_key *key;
/* int i; */
printf("== sk at offset %0x\n",vofs);
key = (struct sk_key *)(hdesc->buffer + vofs);
printf("%04x *unused?* = %d\n" , D_OFFS(dummy1), key->dummy1 );
printf("%04x Offset to prev sk = 0x%0x\n", D_OFFS(ofs_prevsk), key->ofs_prevsk + 0x1000);
printf("%04x Offset to next sk = 0x%0x\n", D_OFFS(ofs_nextsk), key->ofs_nextsk + 0x1000);
printf("%04x Usage counter = %d (0x%0x)\n", D_OFFS(no_usage),
key->no_usage,key->no_usage);
printf("%04x Security data len = %d (0x%0x)\n", D_OFFS(len_sk),
key->len_sk,key->len_sk);
printf("== End of key info.\n");
}
/* Parse the lf datablock (>4.0 'nk' offsets lookuptable)
* vofs = offset into struct (after size linkage)
*/
void parse_lf(struct hive *hdesc, int vofs, int blen)
{
struct lf_key *key;
int i;
printf("== lf at offset %0x\n",vofs);
key = (struct lf_key *)(hdesc->buffer + vofs);
printf("%04x number of keys = %d\n", D_OFFS(no_keys), key->no_keys );
for(i = 0; i < key->no_keys; i++) {
printf("%04x %3d Offset: 0x%0x - <%c%c%c%c>\n",
D_OFFS(hash[i].ofs_nk), i,
key->hash[i].ofs_nk + 0x1000,
key->hash[i].name[0],
key->hash[i].name[1],
key->hash[i].name[2],
key->hash[i].name[3] );
}
printf("== End of key info.\n");
}
/* Parse the lh datablock (WinXP offsets lookuptable)
* vofs = offset into struct (after size linkage)
* The hash is most likely a base 37 conversion of the name string
*/
void parse_lh(struct hive *hdesc, int vofs, int blen)
{
struct lf_key *key;
int i;
printf("== lh at offset %0x\n",vofs);
key = (struct lf_key *)(hdesc->buffer + vofs);
printf("%04x number of keys = %d\n", D_OFFS(no_keys), key->no_keys );
for(i = 0; i < key->no_keys; i++) {
printf("%04x %3d Offset: 0x%0x - <hash: %08x>\n",
D_OFFS(lh_hash[i].ofs_nk), i,
key->lh_hash[i].ofs_nk + 0x1000,
key->lh_hash[i].hash );
}
printf("== End of key info.\n");
}
/* Parse the li datablock (3.x 'nk' offsets list)
* vofs = offset into struct (after size linkage)
*/
void parse_li(struct hive *hdesc, int vofs, int blen)
{
struct li_key *key;
int i;
printf("== li at offset %0x\n",vofs);
/* #define D_OFFS(o) ( (void *)&(key->o)-(void *)hdesc->buffer-vofs ) */
key = (struct li_key *)(hdesc->buffer + vofs);
printf("%04x number of keys = %d\n", D_OFFS(no_keys), key->no_keys );
for(i = 0; i < key->no_keys; i++) {
printf("%04x %3d Offset: 0x%0x\n",
D_OFFS(hash[i].ofs_nk), i,
key->hash[i].ofs_nk + 0x1000);
}
printf("== End of key info.\n");
}
/* Parse the ri subindex-datablock
* (Used to list li/lf/lh's when ~>500keys)
* vofs = offset into struct (after size linkage)
*/
void parse_ri(struct hive *hdesc, int vofs, int blen)
{
struct ri_key *key;
int i;
printf("== ri at offset %0x\n",vofs);
/* #define D_OFFS(o) ( (void *)&(key->o)-(void *)hdesc->buffer-vofs ) */
key = (struct ri_key *)(hdesc->buffer + vofs);
printf("%04x number of subindices = %d\n", D_OFFS(no_lis), key->no_lis );
for(i = 0; i < key->no_lis; i++) {
printf("%04x %3d Offset: 0x%0x\n",
D_OFFS(hash[i].ofs_li), i,
key->hash[i].ofs_li + 0x1000);
}
printf("== End of key info.\n");
}
/* Parse the db block (used when value data >4k or something)
* vofs = offset into struct (after size linkage)
*/
void parse_db(struct hive *hdesc, int vofs, int blen)
{
struct db_key *key;
printf("== db at offset %0x\n",vofs);
key = (struct db_key *)(hdesc->buffer + vofs);
printf("%04x number of parts = %d\n", D_OFFS(no_part), key->no_part );
printf("%04x Data list at offset: 0x%0x\n",
D_OFFS(ofs_data),
key->ofs_data + 0x1000);
printf("== End of key info.\n");
}
/* Parse the datablock
* vofs = offset into struct (after size linkage)
*/
int parse_block(struct hive *hdesc, int vofs,int verbose)
{
unsigned short id;
int seglen;
seglen = get_int(hdesc->buffer+vofs);
// if (vofs > 0xaef000) verbose = 1;
#if 0
if (verbose || seglen == 0) {
printf("** Block at offset %0x\n",vofs);
printf("seglen: %d, %u, 0x%0x\n",seglen,seglen,seglen);
}
#endif
if (seglen == 0) {
printf("parse_block: FATAL! Zero data block size! (not registry or corrupt file?)\n");
if (verbose) debugit(hdesc->buffer,hdesc->size);
return(0);
}
if (seglen < 0) {
seglen = -seglen;
hdesc->usetot += seglen;
hdesc->useblk++;
if (verbose) {
printf("USED BLOCK @ %06x to %06x : %d, 0x%0x\n",vofs,vofs+seglen,seglen,seglen);
/* hexdump(hdesc->buffer,vofs,vofs+seglen+4,1); */
}
} else {
hdesc->unusetot += seglen;
hdesc->unuseblk++;
/* Useful to zero blocks we think are empty when debugging.. */
#if ZEROFILLONLOAD
bzero(hdesc->buffer+vofs+4,seglen-4);
#endif
if (verbose) {
printf("FREE BLOCK @ %06x to %06x : %d, 0x%0x\n",vofs,vofs+seglen,seglen,seglen);
/* hexdump(hdesc->buffer,vofs,vofs+seglen+4,1); */
}
}
vofs += 4;
id = (*(hdesc->buffer + vofs)<<8) + *(hdesc->buffer+vofs+1);
if (verbose > 1) {
switch (id) {
case 0x6e6b: /* nk */
parse_nk(hdesc, vofs, seglen);
break;
case 0x766b: /* vk */
parse_vk(hdesc, vofs, seglen);
break;
case 0x6c66: /* lf */
parse_lf(hdesc, vofs, seglen);
break;
case 0x6c68: /* lh */
parse_lh(hdesc, vofs, seglen);
break;
case 0x6c69: /* li */
parse_li(hdesc, vofs, seglen);
break;
case 0x736b: /* sk */
parse_sk(hdesc, vofs, seglen);
break;
case 0x7269: /* ri */
parse_ri(hdesc, vofs, seglen);
break;
case 0x6462: /* db */
parse_db(hdesc, vofs, seglen);
break;
default:
printf("value data, or not handeled yet!\n");
break;
}
}
return(seglen);
}
/* ================================================================ */
/* Scan and allocation routines */
/* Find start of page given a current pointer into the buffer
* hdesc = hive
* vofs = offset pointer into buffer
* returns: offset to start of page (and page header)
*/
int find_page_start(struct hive *hdesc, int vofs)
{
int r,prev;
struct hbin_page *h;
/* Again, assume start at 0x1000 */
r = 0x1000;
while (r < hdesc->size) {
prev = r;
h = (struct hbin_page *)(hdesc->buffer + r);
if (h->id != 0x6E696268) return(0);
if (h->ofs_next == 0) {
printf("find_page_start: zero len or ofs_next found in page at 0x%x\n",r);
return(0);
}
r += h->ofs_next;
if (r > vofs) return (prev);
}
return(0);
}
/* Find free space in page
* size = requested size in bytes
* pofs = offset to start of actual page header
* returns: offset to free block, or 0 for error
*/
#define FB_DEBUG 0
int find_free_blk(struct hive *hdesc, int pofs, int size)
{
int vofs = pofs + 0x20;
int seglen;
struct hbin_page *p;
p = (struct hbin_page *)(hdesc->buffer + pofs);
while (vofs-pofs < (p->ofs_next - HBIN_ENDFILL)) {
seglen = get_int(hdesc->buffer+vofs);
#if FB_DEBUG
if (vofs > 0x400000) {
printf("** Block at offset %0x\n",vofs);
printf("seglen: %d, %u, 0x%0x\n",seglen,seglen,seglen);
}
#endif
if (seglen == 0) {
printf("find_free_blk: FATAL! Zero data block size! (not registry or corrupt file?)\n");
printf(" : Block at offset %0x\n",vofs);
if ( (vofs - pofs) == (p->ofs_next - 4) ) {
printf("find_free_blk: at exact end of hbin, do not care..\n");
return(0);
}
if (hdesc->state & HMODE_TRACE) debugit(hdesc->buffer,hdesc->size);
else abort();
return(0);
}
if (seglen < 0) {
seglen = -seglen;
#if FB_DEBUG
if (vofs >0x400000) printf("USED BLOCK: %d, 0x%0x\n",seglen,seglen);
#endif
/* hexdump(hdesc->buffer,vofs,vofs+seglen+4,1); */
} else {
#if FB_DEBUG
if (vofs >0x400000) printf("FREE BLOCK!\n");
#endif
/* hexdump(hdesc->buffer,vofs,vofs+seglen+4,1); */
if (seglen >= size) {
#if FB_DEBUG
if (vofs >0x400000) printf("find_free_blk: found size %d block at 0x%x\n",seglen,vofs);
#endif
#if 0
if (vofs == 0x19fb8) {
printf("find_free_blk: vofs = %x, seglen = %x\n",vofs,seglen);
debugit(hdesc->buffer,hdesc->size);
abort();
}
#endif
return(vofs);
}
}
vofs += seglen;
}
return(0);
}
#undef FB_DEBUG
/* Search pages from start to find free block
* hdesc - hive
* size - space requested, in bytes
* returns: offset to free block, 0 if not found or error
*/
int find_free(struct hive *hdesc, int size)
{
int r,blk;
struct hbin_page *h;
/* Align to 8 byte boundary */
if (size & 7) size += (8 - (size & 7));
/* Again, assume start at 0x1000 */
r = 0x1000;
while (r < hdesc->endofs) {
h = (struct hbin_page *)(hdesc->buffer + r);
if (h->id != 0x6E696268) return(0);
if (h->ofs_next == 0) {
printf("find_free: zero len or ofs_next found in page at 0x%x\n",r);
return(0);
}
blk = find_free_blk(hdesc,r,size);
if (blk) return (blk);
r += h->ofs_next;
}
return(0);
}
/* Add new hbin to end of file. If file contains data at end
* that is not in a hbin, include that too
* hdesc - hive as usual
* size - minimum size (will be rounded up to next 0x1000 alignment)
* returns offset to first block in new hbin
*/
#define ADDBIN_DEBUG
int add_bin(struct hive *hdesc, int size)
{
int r,newsize,newbinofs;
struct hbin_page *newbin;
struct regf_header *hdr;
if (hdesc->state & HMODE_NOEXPAND) {
fprintf(stderr,"ERROR: Registry hive <%s> need to be expanded,\n"
"but that is not allowed according to selected options. Operations will fail.\n", hdesc->filename);
return(0);
}
r = ((size + 0x20 + 4) & ~0xfff) + HBIN_PAGESIZE; /* Add header and link, round up to page boundary, usually 0x1000 */
newbinofs = hdesc->endofs;
#ifdef ADDBIN_DEBUG
printf("add_bin: request size = %d [%x], rounded to %d [%x]\n",size,size,r,r);
printf("add_bin: old buffer size = %d [%x]\n",hdesc->size,hdesc->size);
printf("add_bin: firs nonbin off = %d [%x]\n",newbinofs,newbinofs);
printf("add_bin: free at end = %d [%x]\n",hdesc->size-newbinofs,hdesc->size-newbinofs);
#endif
if ( (newbinofs + r) >= hdesc->size) { /* We must allocate more buffer */
newsize = ( (newbinofs + r) & ~(REGF_FILEDIVISOR-1) ) + REGF_FILEDIVISOR; /* File normally multiple of 0x40000 bytes */