-
Notifications
You must be signed in to change notification settings - Fork 32
/
chntpw-presplit.c
2312 lines (1776 loc) · 63.1 KB
/
chntpw-presplit.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
/*
* chntpw.c - Offline Password Edit Utility for Windows SAM database
*
* This program uses the "ntreg" library to load and access the registry,
* it's main purpose is to reset password based information.
* It can also call the registry editor etc
* 2011-apr: Command line options added for hive expansion safe mode
* 2010-jun: Syskey not visible in menu, but is selectable (2)
* 2010-apr: Interactive menu adapts to show most relevant
* selections based on what is loaded
* 2008-mar: Minor other tweaks
* 2008-mar: Interactive reg ed moved out of this file, into edlib.c
* 2008-mar: 64 bit compatible patch by Mike Doty, via Alon Bar-Lev
* http://bugs.gentoo.org/show_bug.cgi?id=185411
* 2007-sep: Group handling extended, promotion now public
* 2007-sep: User edit menu, some changes to user info edit
* 2007-apr-may: Get and display users group memberships
* 2007-apr: GNU license. Some bugfixes. Cleaned up some output.
* 2004-aug: More stuff in regedit. Stringinput bugfixes.
* 2004-jan: Changed some of the verbose/debug stuff
* 2003-jan: Changed to use more of struct based V + some small stuff
* 2003-jan: Support in ntreg for adding keys etc. Editor updated.
* 2002-dec: New option: Specify user using RID
* 2002-dec: New option: blank the pass (zero hash lengths).
* 2001-jul: extra blank password logic (when NT or LANMAN hash missing)
* 2001-jan: patched & changed to use OpenSSL. Thanks to Denis Ducamp
* 2000-jun: changing passwords regardless of syskey.
* 2000-jun: syskey disable works on NT4. Not properly on NT5.
* 2000-jan: Attempt to detect and disable syskey
* 1999-feb: Now able to browse registry hives. (write support to come)
* See HISTORY.txt for more detailed info on history.
*
*****
*
* Copyright (c) 1997-2012 Petter Nordahl-Hagen.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program 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 General Public License for more details.
*
* See file GPL.txt for the full license.
*
*****
*
* Information and ideas taken from pwdump by Jeremy Allison.
*
* More info from NTCrack by Jonathan Wilkins.
*
*/
/* TODO: This is getting ugly. Most likely best to split up into different programs
* and put commun stuff in library
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <inttypes.h>
/* Define DOCRYPTO in makefile to include cryptostuff to be able to change passwords to
* a new one.
* Changing passwords is seems not to be working reliably on XP and newer anyway.
* When not defined, only reset (nulling) of passwords available.
*/
#ifdef DOCRYPTO
#include <openssl/des.h>
#include <openssl/md4.h>
#endif
#define uchar u_char
#define MD4Init MD4_Init
#define MD4Update MD4_Update
#define MD4Final MD4_Final
#include "ntreg.h"
#include "sam.h"
const char chntpw_version[] = "chntpw version 0.99.6 110511 , (c) Petter N Hagen";
extern char *val_types[REG_MAX+1];
/* Global verbosity */
int gverbose = 0;
#define MAX_HIVES 10
/* Array of loaded hives */
struct hive *hive[MAX_HIVES+1];
int no_hives = 0;
/* Icky icky... globals used to refer to hives, will be
* set when loading, so that hives can be loaded in any order
*/
int H_SAM = -1;
int H_SYS = -1;
int H_SEC = -1;
int H_SOF = -1;
int syskeyreset = 0;
int dirty = 0;
int max_sam_lock = 0;
/*
* of user with RID 500, because silly MS decided
* to localize the bloody admin-username!! AAAGHH!
*/
char admuser[129]="Administrator";
/* ============================================================== */
#ifdef DOCRYPTO
/* Crypto-stuff & support for what we'll do in the V-value */
/* Zero out string for lanman passwd, then uppercase
* the supplied password and put it in here */
void make_lanmpw(char *p, char *lm, int len)
{
int i;
for (i=0; i < 15; i++) lm[i] = 0;
for (i=0; i < len; i++) lm[i] = toupper(p[i]);
}
/*
* Convert a 7 byte array into an 8 byte des key with odd parity.
*/
void str_to_key(unsigned char *str,unsigned char *key)
{
int i;
key[0] = str[0]>>1;
key[1] = ((str[0]&0x01)<<6) | (str[1]>>2);
key[2] = ((str[1]&0x03)<<5) | (str[2]>>3);
key[3] = ((str[2]&0x07)<<4) | (str[3]>>4);
key[4] = ((str[3]&0x0F)<<3) | (str[4]>>5);
key[5] = ((str[4]&0x1F)<<2) | (str[5]>>6);
key[6] = ((str[5]&0x3F)<<1) | (str[6]>>7);
key[7] = str[6]&0x7F;
for (i=0;i<8;i++) {
key[i] = (key[i]<<1);
}
DES_set_odd_parity((des_cblock *)key);
}
/*
* Function to convert the RID to the first decrypt key.
*/
void sid_to_key1(uint32_t sid,unsigned char deskey[8])
{
unsigned char s[7];
s[0] = (unsigned char)(sid & 0xFF);
s[1] = (unsigned char)((sid>>8) & 0xFF);
s[2] = (unsigned char)((sid>>16) & 0xFF);
s[3] = (unsigned char)((sid>>24) & 0xFF);
s[4] = s[0];
s[5] = s[1];
s[6] = s[2];
str_to_key(s,deskey);
}
/*
* Function to convert the RID to the second decrypt key.
*/
void sid_to_key2(uint32_t sid,unsigned char deskey[8])
{
unsigned char s[7];
s[0] = (unsigned char)((sid>>24) & 0xFF);
s[1] = (unsigned char)(sid & 0xFF);
s[2] = (unsigned char)((sid>>8) & 0xFF);
s[3] = (unsigned char)((sid>>16) & 0xFF);
s[4] = s[0];
s[5] = s[1];
s[6] = s[2];
str_to_key(s,deskey);
}
/* DES encrypt, for LANMAN */
void E1(uchar *k, uchar *d, uchar *out)
{
des_key_schedule ks;
des_cblock deskey;
str_to_key(k,(uchar *)deskey);
#ifdef __FreeBSD__
des_set_key(&deskey,ks);
#else /* __FreeBsd__ */
des_set_key((des_cblock *)deskey,ks);
#endif /* __FreeBsd__ */
des_ecb_encrypt((des_cblock *)d,(des_cblock *)out, ks, DES_ENCRYPT);
}
#endif /* DOCRYPTO */
/* Get machines SID as binary (raw data)
* str = pointer to buffer, first 20 bytes will be filled in
* returns true if found, else 0
*/
int get_machine_sid(char *sidbuf)
{
struct accountdb_V *v;
struct keyval *kv;
uint32_t ofs;
uint32_t len;
if (H_SAM >= 0) {
/* Get accoundb V value */
kv = get_val2buf(hive[H_SAM], NULL, 0, ACCOUNTDB_V_PATH, REG_BINARY, TPF_VK);
if (!kv) {
fprintf(stderr,"get_machine_sid: Machine SID not found in SAM\n");
return(0);
}
// hexdump(&(kv->data), 0, kv->len,1);
v = (struct accountdb_V *)&kv->data;
ofs = v->sid_ofs;
len = v->sid_len + 4;
ofs += 0x40;
if (len != SID_BIN_LEN) {
fprintf(stderr,"get_machine_sid: WARNING: SID found, but it has len=%d instead of expected %d bytes\n",len,SID_BIN_LEN);
}
// printf("get_machine_sid: adjusted ofs = %x, len = %x (%d)\n",ofs,len,len);
memcpy(sidbuf, (char *)v+ofs, len);
// hexdump(sidbuf, 0, len, 1);
return(1);
}
return(0);
}
/* Make string out of SID, in S-1-5 authority (NT authority)
* like S-1-5-21-516312364-151943033-2698651
* Will allocate return string (which can be of variable lenght)
* NOTE: caller must free it
* sidbuf = the SID binary data structure with it's type+counter first
*
* str = string buffer to fill, be sure to have at least space:
* 6 chars athority prefix (S-1-5-)
* 4 * 10 digits (the 4 32 bit groups)
* 3 for the - between the groups
* 1 for null termination
* 50 chars
*/
char *sid_to_string(struct sid_binary *sidbuf)
{
int cnt, i;
int *array;
char *str = NULL;
// hexdump(sidbuf, 0, 24, 1);
array = (int *)&sidbuf->array;
if (sidbuf->unknown0 != 1) {
fprintf(stderr,"sid_to_string: DEBUG: first byte unexpected: %d\n",sidbuf->unknown0);
}
cnt = sidbuf->sections;
// printf("sid_to_string: DEBUG: sections = %d\n",cnt);
str = str_dup("S-1-5");
for (i = 0; i < cnt; i++) {
str = str_catf(str,"-%u",sidbuf->array[i]);
}
// printf("sid_to_string: returning <%s>\n",str);
return(str);
}
/* Check if hive is SAM, and if it is, extract some
* global policy information from it, like lockout counts etc
*/
void check_get_samdata(int show)
{
struct accountdb_F *f;
struct keyval *v;
if (H_SAM >= 0) {
/* Get accoundb F value */
v = get_val2buf(hive[H_SAM], NULL, 0, ACCOUNTDB_F_PATH, REG_BINARY, TPF_VK);
if (!v) {
fprintf(stderr,"WARNING: Login counts data not found in SAM\n");
return;
}
f = (struct accountdb_F *)&v->data;
max_sam_lock = f->locklimit;
if (show) {
printf("\n* SAM policy limits:\n");
printf("Failed logins before lockout is: %d\n",max_sam_lock);
printf("Minimum password length : %d\n",f->minpwlen);
printf("Password history count : %d\n",f->minpwlen);
}
}
}
/* Try to decode and possibly change account lockout etc
* This is \SAM\Domains\Account\Users\<RID>\F
* It's size seems to always be 0x50.
* Params: RID - user ID, mode - 0 silent, 1 silent, 2 edit.
* Returns: ACB bits with high bit set if lockout count is >0
*/
short handle_F(int rid, int mode)
{
struct user_F *f;
char s[200];
struct keyval *v;
unsigned short acb;
int b;
if (H_SAM < 0) return(0);
/* Get users F value */
snprintf(s,180,"\\SAM\\Domains\\Account\\Users\\%08X\\F",rid);
v = get_val2buf(hive[H_SAM], NULL, 0, s, REG_BINARY, TPF_VK_EXACT);
if (!v) {
printf("Cannot find value <%s>\n",s);
return(0);
}
if (v->len < 0x48) {
printf("handle_F: F value is 0x%x bytes, need >= 0x48, unable to check account flags!\n",v->len);
FREE(v);
return(0);
}
f = (struct user_F *)&v->data;
acb = f->ACB_bits;
if (mode == 1) {
printf("Account bits: 0x%04x =\n",acb);
for (b=0; b < 15; b++) {
printf("[%s] %-15.15s | ",
(acb & (1<<b)) ? "X" : " ", acb_fields[b] );
if (b%3 == 2) printf("\n");
}
printf("\nFailed login count: %u, while max tries is: %u\n",f->failedcnt,max_sam_lock);
printf("Total login count: %u\n",f->logins);
}
if (mode == 2) {
acb |= ACB_PWNOEXP;
acb &= ~ACB_DISABLED;
acb &= ~ACB_AUTOLOCK;
f->ACB_bits = acb;
f->failedcnt = 0;
put_buf2val(hive[H_SAM], v, 0, s, REG_BINARY,TPF_VK_EXACT);
printf("Unlocked!\n");
}
return (acb | ( (f->failedcnt > 0 && f->failedcnt >= max_sam_lock)<<15 ) | (acb & ACB_AUTOLOCK)<<15 | (acb & ACB_DISABLED)<<15);
}
/* Stuff SID binary list into more easily handled arrays
* sidbuf = binary list buffer (not changed, may point into value structure)
* size = number of bytes of raw data
* returns pointer to array, terminated with NULL pointer.
* Keeps full binary data from each SID
* All array space is allocated, call free_sid_array() to free it.
*/
struct sid_array *make_sid_array(struct sid_binary *sidbuf, int size)
{
int num = 0;
int sidlen;
struct sid_binary *sb;
struct sid_array *array;
CREATE(array, struct sid_array, 1);
array[0].len = 0;
array[0].sidptr = NULL;
while (size > 0) {
sidlen = sidbuf->sections * 4 + 8;
// printf("make_sid_array: sidlen = %d\n",sidlen);
ALLOC(sb, 1, sidlen);
memcpy(sb, sidbuf, sidlen);
array[num].len = sidlen;
array[num].sidptr = sb;
sidbuf = (void *)sidbuf + sidlen;
size -= sidlen;
num++;
array = realloc(array, (num + 1) * sizeof(struct sid_array));
array[num].len = 0;
array[num].sidptr = NULL;
}
return(array);
}
/* Free the sid array (from the function above) */
void free_sid_array(struct sid_array *array)
{
int num = 0;
while (array[num].sidptr) {
free(array[num].sidptr);
num++;
}
free(array);
}
/* Compare two SIDs, and return like strcmp */
int sid_cmp(struct sid_binary *s1, struct sid_binary *s2)
{
int p;
if (!s1 && !s2) return(0);
if (!s1) return(-1);
if (!s2) return(1);
if (s1->sections < s2->sections) return(-1); /* s1 has shorter len, always smaller */
if (s1->sections > s2->sections) return(1); /* s1 has longer len, always larger */
/* Run compare since same length */
for (p = 0; p < s1->sections; p++) {
if (s1->array[p] < s2->array[p]) return (-1);
if (s1->array[p] > s2->array[p]) return (1);
}
/* At end. Thus equal */
return(0);
}
/* Get list of group members for a group
* Will get the SID list (as binary) into a buffer that will be allocated
* according to the neccessary size (based on member count)
* NOTE: Caller must free the buffer when not needed any more
* grp = group ID
* sidarray = pointer to pointer to sid array which will be allocated
* Returns number of members in the group
*/
int get_grp_members_sid(int grp, struct sid_array **sarray)
{
char g[200];
// char groupname[128];
struct sid_array *marray;
struct keyval *c = NULL;
struct group_C *cd;
// int grpnamoffs, grpnamlen;
int mofs, mlen;
snprintf(g,180,"\\SAM\\Domains\\Builtin\\Aliases\\%08X\\C",grp);
c = get_val2buf(hive[H_SAM], NULL, 0, g, 0, TPF_VK_EXACT);
if (c) {
cd = (struct group_C *)&c->data;
// grpnamoffs = cd->grpname_ofs + 0x34;
// grpnamlen = cd->grpname_len;
// cheap_uni2ascii((char *)cd + grpnamoffs, groupname, grpnamlen);
// printf("get_grp_members_sid: group %x named %s has %d members\n",grp,groupname,cd->grp_members);
mofs = cd->members_ofs;
mlen = cd->members_len;
// printf("get_grp_members_sid: mofs = %x, mlen = %x (%d)\n", mofs,mlen,mlen);
// printf("get_grp_members_sid: ajusted: mofs = %x, mlen = %x (%d)\n", mofs + 0x34 ,mlen,mlen);
// hexdump(&c->data, 0, c->len, 1);
// hexdump(&cd->data[mofs], 0, mlen, 1);
marray = make_sid_array((struct sid_binary *)&cd->data[mofs], mlen);
*sarray = marray;
// free_sid_array(marray);
free(c);
} else {
printf("Group info for %x not found!\n",grp);
*sarray = NULL;
return(0);
}
return(cd->grp_members);
}
/* Put list of group members back into group C structure
* grp = group ID
* sidarray = pointer to sid array
* Returns true if success
*/
int put_grp_members_sid(int grp, struct sid_array *sarray)
{
char g[200];
char groupname[128];
struct keyval *c = NULL;
struct group_C *cd;
int grpnamoffs, grpnamlen;
int mofs, mlen;
int sidlen = 0;
void *sidptr;
int i;
char *str;
snprintf(g,180,"\\SAM\\Domains\\Builtin\\Aliases\\%08X\\C",grp);
c = get_val2buf(hive[H_SAM], NULL, 0, g, 0, TPF_VK_EXACT);
if (c) {
cd = (struct group_C *)&c->data;
grpnamoffs = cd->grpname_ofs + 0x34;
grpnamlen = cd->grpname_len;
cheap_uni2ascii((char *)cd + grpnamoffs, groupname, grpnamlen);
if (gverbose) printf("put_grp_members_sid: group %x named %s has %d members\n",grp,groupname,cd->grp_members);
mofs = cd->members_ofs;
mlen = cd->members_len;
if (gverbose) printf("put_grp_members_sid: ajusted: mofs = %x, mlen = %x (%d)\n", mofs + 0x34 ,mlen,mlen);
if (gverbose) hexdump(&c->data, 0, c->len, 1);
/* Get total size of new SID data */
for (i = 0; sarray[i].sidptr; i++) sidlen += sarray[i].len;
if (gverbose) printf("put_grp_members_sid: new count : %d, new sidlen: %x\n",i,sidlen);
/* Resize buffer with C structure */
c = realloc(c, 4 + mofs + sidlen + 0x34); /* offset of SIDs + sids lenght + pointer list at start */
c->len = 0x34 + mofs + sidlen;
cd = (struct group_C *)&c->data;
mofs = cd->members_ofs;
sidptr = &cd->data[mofs];
for (i = 0; sarray[i].sidptr; i++) {
if (gverbose) printf(" copying : %d len %x, at %x\n",i,sarray[i].len, sidptr);
str = sid_to_string(sarray[i].sidptr);
if (gverbose) printf(" Member # %d = <%s>\n", i, str);
FREE(str);
memcpy(sidptr, sarray[i].sidptr, sarray[i].len);
sidptr += sarray[i].len;
}
cd->members_len = sidlen; /* Update member count in C struct */
cd->grp_members = i;
if (gverbose) hexdump(&c->data, 0, c->len, 1);
if (!put_buf2val(hive[H_SAM], c, 0, g, 0, TPF_VK_EXACT)) {
fprintf(stderr,"put_grp_members_sid: could not write back group info in value %s\n",g);
free(c);
return(0);
}
free(c);
} else {
printf("Group info for %x not found!\n",grp);
return(0);
}
return(1);
}
/* List groups, optionally with members */
void list_groups(int listmembers) {
struct ex_data ex;
struct sid_array *sids = NULL;
int nkofs;
unsigned int grp;
int count = 0, countri = 0;
struct keyval *c = NULL;
struct group_C *cd;
int grpnamoffs, grpnamlen, i;
char groupname[200];
char *str;
if (H_SAM < 0) return;
nkofs = trav_path(hive[H_SAM], 0,"\\SAM\\Domains\\Builtin\\Aliases",0);
if (!nkofs) {
printf("list_groups: Cannot find group list in registry! (is this a SAM-hive?)\n");
return;
}
/* Pick up all subkeys here, they are local groups */
while ((ex_next_n(hive[H_SAM], nkofs+4, &count, &countri, &ex) > 0)) {
// printf("Group ID %s\n",ex.name);
sscanf(ex.name,"%x",&grp);
/* Groups keys have a C value, get it and pick up the name etc */
/* Some other keys also exists (Members, Names at least), but we skip them */
c = get_val2buf(hive[H_SAM], NULL, ex.nkoffs+4, "C", 0, TPF_VK_EXACT);
if (c) {
cd = (struct group_C *)&c->data;
grpnamoffs = cd->grpname_ofs + 0x34;
grpnamlen = cd->grpname_len;
cheap_uni2ascii((char *)cd + grpnamoffs, groupname, grpnamlen);
printf("Group #%x named <%s> has %d members\n",grp,groupname,cd->grp_members);
if (listmembers) {
get_grp_members_sid(grp, &sids);
for (i = 0; sids[i].sidptr; i++) {
str = sid_to_string(sids[i].sidptr);
printf(" Member # %d = <%s>\n", i, str);
FREE(str);
}
free_sid_array(sids);
}
}
}
}
/* Get group IDs a user is member of
* rid = user ID
* returns: since value data is just an array of grp ids (4 bytes each),
* just return the keyval structure (size + data)
* caller must free() keyval
*/
struct keyval *get_user_grpids(int rid)
{
char s[200];
struct sid_binary sid;
char *sidstr;
int nk = 0;
struct keyval *m = NULL;
int count = 0;
int size;
if (!rid || (H_SAM < 0)) return(NULL);
if (!get_machine_sid((char *)&sid)) {
fprintf(stderr,"get_user_grpids: Could not find machine SID\n");
return(0);
}
sidstr = sid_to_string(&sid);
/* Get member list for user on this machine */
snprintf(s,180,"\\SAM\\Domains\\Builtin\\Aliases\\Members\\%s\\%08X",sidstr,rid);
free(sidstr);
/* Now, the TYPE field is the number of groups the user is member of */
/* Don't we just love the inconsistent use of fields!! */
nk = trav_path(hive[H_SAM], 0, s, 0);
if (!nk) {
/* This probably means user is not in any group. Seems to be the case
for a couple of XPs built in support / guest users. So just return */
if (gverbose) printf("get_user_grpids: Cannot find RID under computer SID <%s>\n",s);
return(NULL);
}
nk += 4;
count = get_val_type(hive[H_SAM],nk,"@",TPF_VK_EXACT);
if (count == -1) {
printf("get_user_grpids: Cannot find value <%s\\@>\n",s);
return(NULL);
}
// printf("get_user_grpids: User is member of %d groups:\n",count);
/* This is the data size */
size = get_val_len(hive[H_SAM],nk,"@",TPF_VK_EXACT);
/* It should be 4 bytes for each group */
if (gverbose) printf("Data size %d bytes.\n",size);
if (size != count * 4) {
printf("get_user_grpids: DEBUG: Size is not 4 * count! May not matter anyway. Continuing..\n");
}
m = get_val2buf(hive[H_SAM], NULL, nk, "@", 0, TPF_VK_EXACT);
if (!m) {
printf("get_user_grpids: Could not get value data! Giving up.\n");
return(NULL);
}
return(m);
}
/* Put/set group IDs a user is member of
* rid = user ID
* val = keyval structure of data, actual value data is a list
* of ints, one per group
* returns true if successful setting the value
*/
int put_user_grpids(int rid, struct keyval *val)
{
char s[200];
struct sid_binary sid;
char *sidstr;
int newcount = 0;
int nk = 0;
int count = 0;
if (!rid || (H_SAM < 0)) return(0);
if (!val || !val->len) return(0);
if (!get_machine_sid((char *)&sid)) {
fprintf(stderr,"put_user_grpids: Could not find machine SID\n");
return(0);
}
sidstr = sid_to_string(&sid);
/* Get member list for user on this machine */
snprintf(s,180,"\\SAM\\Domains\\Builtin\\Aliases\\Members\\%s\\%08X",sidstr,rid);
free(sidstr);
/* Now, the TYPE field is the number of groups the user is member of */
nk = trav_path(hive[H_SAM], 0, s, 0);
if (!nk) {
/* This probably means user is not in any group. Seems to be the case
for a couple of XPs built in support / guest users. So just return */
if (gverbose) printf("put_user_grpids: Cannot find RID under computer SID <%s>\n",s);
return(0);
}
nk += 4;
count = get_val_type(hive[H_SAM],nk,"@",TPF_VK_EXACT);
if (count == -1) {
printf("put_user_grpids: Cannot find value <%s\\@>\n",s);
return(1);
}
if (gverbose) printf("put_user_grpids: User was member of %d groups:\n",count);
/* This is the data size */
/* It should be 4 bytes for each group */
newcount = val->len >> 2;
if (gverbose) printf("Data size %d bytes.\n",val->len);
if (val->len != newcount << 2) {
printf("set_user_grpids: DEBUG: Size is not 4 * count! May not matter anyway. Continuing..\n");
}
if (gverbose) printf("put_user_grpids: User is NOW member of %d groups:\n",newcount);
set_val_type(hive[H_SAM],nk,"@",TPF_VK_EXACT,newcount);
if (!put_buf2val(hive[H_SAM], val, nk, "@", 0, TPF_VK_EXACT) ) {
printf("put_user_grpids: Could not set reg value data!\n");
return(0);
}
return(1);
}
/* List users membership or check if admin (is in admin group)
* rid - users rid
* check - if 1 just check if admin, do not list
* returns true if user is admin
*/
int list_user_groups(int rid, int check)
{
char g[200];
char groupname[128];
struct keyval *m = NULL, *c = NULL;
struct group_C *cd;
unsigned int *grps;
int count = 0, isadmin = 0;
int i, grp, grpnamoffs, grpnamlen;
if (!rid || (H_SAM < 0)) return(0);
m = get_user_grpids(rid);
if (!m) return(0);
grps = (unsigned int *)&m->data;
count = m->len >> 2;
for (i = 0; i < count; i++) {
grp = grps[i];
if (!check) printf("%08x ",grp);
if (grp == 0x220) isadmin = 1;
if (!check) {
snprintf(g,180,"\\SAM\\Domains\\Builtin\\Aliases\\%08X\\C",grp);
c = get_val2buf(hive[H_SAM], NULL, 0, g, 0, TPF_VK_EXACT);
if (c) {
cd = (struct group_C *)&c->data;
grpnamoffs = cd->grpname_ofs + 0x34;
grpnamlen = cd->grpname_len;
cheap_uni2ascii((char *)cd + grpnamoffs, groupname, grpnamlen);
printf("= %s (which has %d members)\n",groupname,cd->grp_members);
// get_grp_members_sid(grp, &sidbuf);
} else {
printf("Group info for %x not found!\n",grp);
}
}
}
free(m);
return(isadmin);
}
/* Add user to a group
* rid = user RID
* grp = group ID
* return true if success
*/
int add_user_to_grp(int rid, int grp)
{
struct keyvala *usrgrplist, *newusrgrplist;
struct sid_array *sarray, *narray;
struct sid_binary *usid;
struct sid_binary msid;
int members, newmembers;
char *str;
int ugcnt;
int o,n,hit,c;
unsigned int *og, *ng;
if (!rid || !grp || (H_SAM < 0)) return(0);
/* Build user SID (add RID to machine SID) */
if (!get_machine_sid((char *)&msid)) {
fprintf(stderr,"get_user_grpids: Could not find machine SID\n");
return(0);
}
/* well, and hope that machine SID is always same size here too */
ALLOC(usid, sizeof(struct sid_binary) +4, 1);
memcpy(usid, &msid, sizeof(struct sid_binary));
usid->array[4] = rid; /* Tack RID on at end */
usid->sections = 5;
str = sid_to_string(usid);
if (gverbose) printf("add_user_to_grp: user SID is <%s>\n", str);
free(str);
/* With all of the above functions, it should now just be to get
* the list of groups the user account has listed under it
* and the list of users the group has listed under it
*/
usrgrplist = (struct keyvala *)get_user_grpids(rid);
if (!usrgrplist) {
printf("add_user_to_grp: user # %x not found!\n",rid);
return(0);
}
members = get_grp_members_sid(grp, &sarray);
if (!sarray) {
printf("add_user_to_grp: group # %x not found!\n",grp);
FREE(usrgrplist);
return(0);
}
/* Add the group to the users list of groups it is member of */
ugcnt = usrgrplist->len >> 2; /* Count of groups already on user */
/* Allocate new larger usrgrplist for one more entry */
ALLOC(newusrgrplist, usrgrplist->len + 4 + 4, 1);
bzero(newusrgrplist, usrgrplist->len + 4 + 4); /* for sanity.. */
newusrgrplist->len = usrgrplist->len + 4;
og = (unsigned int *)&usrgrplist->data;
ng = (unsigned int *)&newusrgrplist->data;
if (gverbose) printf("usrgrplist-len = %d\n", usrgrplist->len);
#if 0 /* If list should be sorted, but seems windows does not do that? */
/* Copy over users group list, adding in where needed */
hit = 0;
for (o = 0, n = 0; o < ugcnt; o++, n++) {
printf(":: %d %d : %x\n",o,n,og[o]);
if (og[o] == grp) { /* Was already in there, so just don't increase size.. */
newusrgrplist->len = usrgrplist->len;
hit = 1;
}
if (og[o] > grp && !hit) {
ng[n++] = grp; /* Next is higher, so insert out rid */
hit = 1;
printf(" -- insert\n");
}
ng[n] = og[o];
}
printf("n = %d\n",n);