Debian Codegraph @

debian/patches/15_added_samunlock_binary

raw
1 Description: Add samunlock binary
2 The samunlock binary lets you unlock or list users.
3 This command is suited for scripts and can be run also interactively
4
5 Obtained from: https://github.com/rescatux/chntpw/
6
7 Forwarded: no
8 Origin: other
9 Author: Adrian Gibanel Lopez <[email protected]>
10
11 --- a/Makefile
12 +++ b/Makefile
13 @@ -12,7 +12,7 @@
14 LIBS=$(shell libgcrypt-config --libs)
15
16
17 -all: chntpw cpnt reged samusrgrp sampasswd
18 +all: chntpw cpnt reged samusrgrp sampasswd samunlock
19
20 chntpw: chntpw.o ntreg.o edlib.o libsam.o
21 $(CC) $(CFLAGS) -o chntpw chntpw.o ntreg.o edlib.o libsam.o $(LIBS)
22 @@ -41,6 +41,11 @@
23 sampasswd.static: sampasswd.o ntreg.o libsam.o
24 $(CC) -static $(CFLAGS) -o sampasswd.static sampasswd.o ntreg.o libsam.o
25
26 +samunlock: samunlock.o ntreg.o libsam.o
27 + $(CC) $(CFLAGS) -o samunlock samunlock.o ntreg.o libsam.o
28 +
29 +samunlock.static: samunlock.o ntreg.o libsam.o
30 + $(CC) -static $(CFLAGS) -o samunlock.static samunlock.o ntreg.o libsam.o
31
32
33 #ts: ts.o ntreg.o
34 @@ -52,5 +57,5 @@
35 $(CC) -c $(CFLAGS) $<
36
37 clean:
38 - rm -f *.o chntpw cpnt reged samusrgrp sampasswd *~
39 + rm -f *.o chntpw cpnt reged samusrgrp sampasswd samunlock *~
40
41 --- /dev/null
42 +++ b/samunlock.c
43 @@ -0,0 +1,248 @@
44 +/*
45 + * samunlock.c - SAM database, Unlock user
46 + *
47 + * Command line utility, non-interactive to unlock a user
48 + * in the SAM database
49 + *
50 + * Changes:
51 + * 2014 - oct: First version, some code from earlier sampasswd.c.
52 + *
53 + *****
54 + *
55 + * Copyright (c) 2014 Adrian Gibanel
56 + *
57 + * This program is free software; you can redistribute it and/or modify
58 + * it under the terms of the GNU General Public License as published by
59 + * the Free Software Foundation; version 2 of the License.
60 + *
61 + * This program is distributed in the hope that it will be useful,
62 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
63 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 + * GNU General Public License for more details.
65 + *
66 + * See file GPL.txt for the full license.
67 + *
68 + *****
69 + */
70 +
71 +
72 +#include <stdio.h>
73 +#include <unistd.h>
74 +#include <stdlib.h>
75 +#include <string.h>
76 +
77 +#include "ntreg.h"
78 +#include "sam.h"
79 +
80 +
81 +const char samunlock_version[] = "samunlock version 0.1 141018, (c) Adrian Gibanel";
82 +
83 +
84 +/* Global verbosity flag */
85 +int gverbose = 0;
86 +
87 +/* Array of loaded hives */
88 +#define MAX_HIVES 10
89 +struct hive *hive[MAX_HIVES+1];
90 +int no_hives = 0;
91 +
92 +int H_SAM = -1;
93 +
94 +
95 +
96 +int do_unlock(char *user, int inrid, int verb)
97 +{
98 + int rid = 0;
99 + int ret;
100 + char *resolvedname = NULL;
101 + char s[200];
102 + unsigned short acb;
103 +
104 + if ((H_SAM < 0) || (!user && !inrid) ) return(1);
105 +
106 + if (inrid) {
107 + rid = inrid;
108 + } else {
109 + if (*user == '0' && *(user+1) == 'x') sscanf(user,"%i",&rid);
110 + }
111 +
112 + if (!rid) { /* Look up username */
113 + /* Extract the unnamed value out of the username-key, value is RID */
114 + snprintf(s,180,"\\SAM\\Domains\\Account\\Users\\Names\\%s\\@",user);
115 + rid = get_dword(hive[H_SAM],0,s, TPF_VK_EXACT|TPF_VK_SHORT);
116 + if (rid == -1) {
117 + printf("ERROR: User <%s> not found\n",user);
118 + return(1);
119 + }
120 + }
121 +
122 + /* At this point we have a RID, so get the real username it maps to just to show it */
123 + resolvedname = sam_get_username(hive[H_SAM], rid);
124 +
125 + if (!resolvedname) return(1); /* RID lookup failed, no such user */
126 +
127 + if (gverbose) printf("do_unlock: Username: %s, RID = %d (0x%0x)\n",resolvedname,rid,rid);
128 +
129 + acb = sam_handle_accountbits(hive[H_SAM], rid,2);
130 +
131 + ret = acb & 0x8000; /* ret != 0 means locked ; ret == 0 means unlocked */
132 +
133 + if (!ret && verb) printf("Unlock user %s, RID = %d [0x%0x]\n",resolvedname,rid,rid);
134 +
135 + FREE(resolvedname);
136 + return(ret);
137 +
138 +}
139 +
140 +
141 +
142 +void usage(void)
143 +{
144 + printf(" [-U|-l] [-H] -u <user> <samhive>\n"
145 + "Unlock user or list users in SAM database\n"
146 + "Mode:\n"
147 + " -U = Unlock user\n"
148 + " -l = list users in sam\n"
149 + "Parameters:\n"
150 + " <user> can be given as a username or a RID in hex with 0x in front\n"
151 + " Example:\n"
152 + " -U -u theboss -> Unlocks user named 'theboss' if found\n"
153 + " -U -u 0x3ea -> Unlocks user with RID 0x3ea (hex)\n"
154 + " -U -f -> Unlocks admin user with lowest RID\n"
155 + " not counting built-in admin (0x1f4) unless it is the only admin\n"
156 + " Usernames with international characters usually fails to be found,\n"
157 + " please use RID number instead\n"
158 + " If success, there will be no output, and exit code is 0\n"
159 + "Options:\n"
160 + " -H : For list: Human readable listing (default is parsable table)\n"
161 + " -H : For unlock: Will output confirmation message if success\n"
162 + " -N : No allocate mode, only allow edit of existing values with same size\n"
163 + " -E : No expand mode, do not expand hive file (safe mode)\n"
164 + " -t : Debug trace of allocated blocks\n"
165 + " -v : Some more verbose messages/debug\n"
166 + );
167 +}
168 +
169 +
170 +int main(int argc, char **argv)
171 +{
172 +
173 + extern int optind;
174 + extern char* optarg;
175 +
176 + int what = 0;
177 + int unlock = 0;
178 + int list = 0;
179 + int mode = 0;
180 + int human = 0;
181 + int adm = 0;
182 + int first = 0;
183 + int ret, wret, il;
184 + char *hivename;
185 + char c;
186 + char *usr = NULL;
187 +
188 + char *options = "UlHu:vNEthaf";
189 +
190 + while((c=getopt(argc,argv,options)) > 0) {
191 + switch(c) {
192 + case 'U': unlock = 1; break;
193 + case 'l': list = 2; break;
194 + case 'u': usr = optarg; break;
195 + case 'f': first = 1; break;
196 + case 'H': human = 1; break;
197 + case 'v': mode |= HMODE_VERBOSE; gverbose = 1; break;
198 + case 'N': mode |= HMODE_NOALLOC; break;
199 + case 'E': mode |= HMODE_NOEXPAND; break;
200 + case 't': mode |= HMODE_TRACE; break;
201 + case 'h': printf("%s\n%s ",samunlock_version,argv[0]); usage(); exit(0); break;
202 + default: printf("%s\n%s ",samunlock_version,argv[0]); usage(); exit(1); break;
203 + }
204 + }
205 +
206 + if (!unlock && !list && !what) {
207 + fprintf(stderr,"%s: ERROR: Mode -U or -l must be specified. -h for help\n",argv[0]);
208 + exit(1);
209 + }
210 +
211 +#if 0 /* Should both be allowed at same time?? */
212 + if (list && unlock) {
213 + fprintf(stderr,"%s: ERROR: Mode -U and -l impossible at the same time. -h for help\n",argv[0]);
214 + exit(1);
215 + }
216 +#endif
217 +
218 + if (unlock && !first && (!usr || !*usr)) {
219 + fprintf(stderr,"%s: ERROR: Need a user for unlock, -u must be specified.\n",argv[0]);
220 + exit(1);
221 + }
222 +
223 +
224 + /* Load hives. Only first SAM hive will be used however */
225 +
226 + hivename = argv[optind+no_hives];
227 + if (!hivename || !*hivename) {
228 + fprintf(stderr,"%s: ERROR: You must specify a SAM registry hive filename.\n",argv[0]);
229 + exit(1);
230 + }
231 + do {
232 + if (!(hive[no_hives] = openHive(hivename,
233 + HMODE_RW|mode))) {
234 + fprintf(stderr,"%s: ERROR: Unable to open/read registry hive, cannot continue\n",argv[0]);
235 + exit(1);
236 + }
237 + switch(hive[no_hives]->type) {
238 + case HTYPE_SAM: H_SAM = no_hives; break;
239 + // case HTYPE_SOFTWARE: H_SOF = no_hives; break;
240 + // case HTYPE_SYSTEM: H_SYS = no_hives; break;
241 + // case HTYPE_SECURITY: H_SEC = no_hives; break;
242 + }
243 + no_hives++;
244 + hivename = argv[optind+no_hives];
245 + } while (hivename && *hivename && no_hives < MAX_HIVES);
246 +
247 + if (H_SAM == -1) {
248 + fprintf(stderr,"%s: WARNING: Hive file does not look like SAM, but continuing anyway in case detection was wrong\n"
249 + "%s: WARNING: If it really is not a SAM file you will get strange errors or bad results\n",argv[0],argv[0]);
250 + H_SAM = 0;
251 + }
252 +
253 +
254 + /* Do logic */
255 +
256 + if (list) {
257 + adm = sam_list_users(hive[H_SAM], human);
258 + if (gverbose) printf(" sam_list_users found admin to be 0x%x\n",adm);
259 + }
260 +
261 + if (unlock) {
262 + if (first) {
263 + adm = sam_list_users(hive[H_SAM], 2);
264 + if (!adm) {
265 + fprintf(stderr,"%s: ERROR: Unable to unlock, no admin users found\n",argv[0]);
266 + } else {
267 + // printf("Resetting password of user with RID %x\n",adm);
268 + ret = do_unlock(usr, adm, human);
269 + }
270 + } else {
271 + ret = do_unlock(usr, 0, human);
272 + if (ret) {
273 + fprintf(stderr,"%s: ERROR: Failed to unlock %s\n",argv[0],usr);
274 + }
275 + }
276 + }
277 +
278 + /* write registry hive (if needed) */
279 +
280 + wret = 0;
281 + for (il = 0; il < no_hives; il++) {
282 + wret |= writeHive(hive[il]);
283 + if (hive[il]->state & HMODE_DIDEXPAND)
284 + fprintf(stderr," WARNING: Registry file %s was expanded! Experimental! Use at own risk!\n",hive[il]->filename);
285 + while (no_hives > 0)
286 + closeHive(hive[--no_hives]);
287 + }
288 +
289 + return(ret | wret);
290 +}
291 +