Debian Codegraph @

dumpsam.c

raw symbols SCIP
1 /** dumpsam.c
2 Dumpsam file (only for Windows) for samdump2 3.x
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 This program is released under the GPL with the additional exemption
19 that compiling, linking, and/or using OpenSSL is allowed.
20
21 Copyright (C) 2009 Cedric Tissieres <http://www.objectif-securite.ch>
22 */
23
24 #ifdef WIN32
25
26 #include <windows.h>
27 #include <winioctl.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <tchar.h>
31 #include "ntdll.h"
32
33
34 /***********************************************************/
35 /* code from http://www.ivanlef0u.tuxfamily.org/?p=77 */
36 ULONG EnablePrivilege(char *Privilege)
37 {
38 HANDLE hToken;
39 ULONG Ret=1;
40 TOKEN_PRIVILEGES TP;
41
42 if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
43 {
44 printf("Error with OpenProcessToken: %d\n", (int)GetLastError());
45 Ret=0;
46 goto bye;
47 }
48
49 if(!LookupPrivilegeValue(NULL, Privilege, &TP.Privileges[0].Luid))
50 {
51 printf("Error with LookupPrivilegeValue: %d\n", (int)GetLastError());
52 Ret=0;
53 goto bye;
54
55 }
56
57 TP.PrivilegeCount=1;
58 TP.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
59
60 if(!AdjustTokenPrivileges(hToken,
61 0,
62 &TP,
63 0,
64 NULL,
65 NULL))
66 {
67 printf("Error with AdjustTokenPrivileges: %d\n", (int)GetLastError());
68 Ret=0;
69 goto bye;
70
71 }
72
73 bye:
74 if(hToken)
75 CloseHandle(hToken);
76
77 return Ret;
78 }
79
80
81 /***********************************************************/
82 /* code from http://www.ivanlef0u.tuxfamily.org/?p=77 */
83 void hexdump(unsigned char *data, unsigned int amount) {
84 unsigned int dp, p;
85 const char trans[] =
86 "................................ !\"#$%&'()*+,-./0123456789"
87 ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
88 "nopqrstuvwxyz{|}~...................................."
89 "....................................................."
90 "........................................";
91
92 for (dp = 1; dp <= amount; dp++) {
93 printf ("%02x ", data[dp-1]);
94 if ((dp % 8) == 0)
95 printf (" ");
96 if ((dp % 16) == 0) {
97 printf ("| ");
98 p = dp;
99 for (dp -= 16; dp < p; dp++)
100 printf ("%c", trans[data[dp]]);
101 printf ("\n");
102 }
103 }
104 if ((amount % 16) != 0) {
105 p = dp = 16 - (amount % 16);
106 for (dp = p; dp > 0; dp--) {
107 printf (" ");
108 if (((dp % 8) == 0) && (p != 8))
109 printf (" ");
110 }
111 printf (" | ");
112 for (dp = (amount - (16 - p)); dp < amount; dp++)
113 printf ("%c", trans[data[dp]]);
114 }
115 printf ("\n");
116 return ;
117 }
118
119 /***********************************************************/
120 /* code from http://www.ivanlef0u.tuxfamily.org/?p=77 */
121 int GetFileLCN(HANDLE hFile, int64_t *location, char *error)
122 {
123 ULONG Status;
124 IO_STATUS_BLOCK IoBlock;
125 unsigned char *temp;
126
127 STARTING_VCN_INPUT_BUFFER VCN;
128 RETRIEVAL_POINTERS_BUFFER Buff;
129
130 memset(&VCN, 0, sizeof(STARTING_VCN_INPUT_BUFFER));
131
132 temp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0x10000);
133 Status=ZwFsControlFile(hFile,
134 NULL,
135 NULL,
136 NULL,
137 &IoBlock,
138 FSCTL_GET_RETRIEVAL_POINTERS,
139 &VCN,
140 sizeof(STARTING_VCN_INPUT_BUFFER),
141 &Buff,
142 sizeof(RETRIEVAL_POINTERS_BUFFER));
143 if(!NT_SUCCESS(Status))
144 {
145 sprintf(error,"Error with ZwFsControlFile : 0x%x\n", (int)Status);
146 return -1;
147 }
148
149 // printf("ExtentCount : %d\n", Buff.ExtentCount);
150 *location = Buff.Extents[0].Lcn.QuadPart;
151
152 /* for(i=0; i<Buff.ExtentCount; i++) */
153 /* { */
154
155 /* printf("LCN : 0x%x\n", Buff.Extents[i].Lcn); */
156 /* printf("VCN : 0x%x\n", Buff.Extents[i].NextVcn); */
157
158 /* } */
159
160 HeapFree(GetProcessHeap(), 0, temp);
161 return 0;
162 }
163 /***********************************************************/
164 int get_handle(unsigned char *path, PHANDLE outHandle, char *error, int debug) {
165
166 OBJECT_ATTRIBUTES ObjectAttributes;
167 IO_STATUS_BLOCK status_block;
168 UNICODE_STRING unicode_string;
169 int status;
170 unsigned char *fullpath;
171 wchar_t *fullpathunicode;
172
173 fullpath = (unsigned char *) malloc(5+strlen(path));
174 memset(fullpath, 0, 5+strlen(path));
175 fullpathunicode = (wchar_t *) malloc((5+strlen(path))*sizeof(wchar_t));
176 strncpy(fullpath, "\\??\\", 4);
177 strncpy(fullpath+4, path, strlen(path));
178 if (debug)
179 printf("Full path: %s\n", fullpath);
180 mbstowcs(fullpathunicode, fullpath, strlen(fullpath));
181 fullpathunicode[strlen(fullpath)] = 0;
182
183 RtlInitUnicodeString(&unicode_string, fullpathunicode);
184
185 InitializeObjectAttributes(&ObjectAttributes, &unicode_string, 0, NULL, NULL);
186
187 status = ZwOpenFile(outHandle, FILE_READ_ATTRIBUTES|SYNCHRONIZE, &ObjectAttributes, &status_block, 3, FILE_SYNCHRONOUS_IO_NONALERT);
188
189 if(status!=STATUS_SUCCESS)
190 {
191 sprintf(error, "Error with ZwOpenFile : 0x%x : %d \n", status, (int)RtlNtStatusToDosError(status));
192 return -1;
193 }
194
195 if (debug)
196 printf("Got handle : %d\n", (int)*outHandle);
197
198 return 0;
199 }
200
201 /***********************************************************/
202
203 int get_live_syskey(unsigned char *pkey, char *error, int debug) {
204 int i;
205 HKEY hk;
206 HKEY hkt;
207 char *kn[] = {"JD", "Skew1", "GBG", "Data"};
208 char kv[9];
209 unsigned long kvsize;
210 unsigned char key[16];
211 unsigned char pkeystring[16*2+1] = {0};
212
213 #if BYTE_ORDER == LITTLE_ENDIAN
214 int p[] = { 0xb, 0x6, 0x7, 0x1, 0x8, 0xa, 0xe, 0x0, 0x3, 0x5, 0x2, 0xf, 0xd, 0x9, 0xc, 0x4 };
215 #elif BYTE_ORDER == BIG_ENDIAN
216 int p[] = { 0x8, 0x5, 0x4, 0x2, 0xb, 0x9, 0xd, 0x3, 0x0, 0x6, 0x1, 0xc, 0xe, 0xa, 0xf, 0x7 };
217 #endif
218
219 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Lsa", 0, KEY_READ, &hk)==0) {
220
221 for (i=0; i<4; i++) {
222 if (RegOpenKeyEx(hk, kn[i], 0, KEY_READ, &hkt)) {
223 sprintf(error, "Error reading %s key\n", kn[i]);
224 return -1;
225 }
226
227 kvsize = sizeof(kv);
228
229 RegQueryInfoKey(hkt, kv, &kvsize, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
230 RegCloseKey(hkt);
231
232 sscanf(kv, "%x", (int*) (&key[i*4]));
233 }
234 RegCloseKey(hk);
235
236 for(i=0; i<16; i++) {
237 pkey[i] = key[p[i]];
238 if (debug)
239 sprintf(pkeystring+i*2, "%.2x", pkey[i]);
240 }
241 } else {
242 sprintf(error, "Error reading LSA key\n");
243 return -1;
244 }
245 if (debug)
246 printf("key = %s\n", pkeystring);
247 return 0;
248 }
249
250
251
252 /***********************************************************/
253 int get_sam(unsigned char **buff_sam, int *buff_sam_size, char *error, int debug) {
254
255 uint32_t bytesread, filesize, sectorsize, clustersize;
256 int64_t location;
257 HANDLE hDevice, hSamFile;
258 LARGE_INTEGER tmp;
259 char *windir;
260 char drive[] = "C:\\";
261 char fs[16] ={0};
262 char filename[MAX_PATH] ={0};
263 char devicename[] = "\\\\.\\C:";
264 char boot_sector[512]={0};
265 char ntfs = 0;
266
267 if(!EnablePrivilege("SeDebugPrivilege")) {
268 sprintf(error, "Unable to set Debug Privilege. You must be admin to run this program.\n");
269 return -1;
270 }
271
272 windir = getenv("windir");
273 strncpy(drive, windir, 1);
274 strncpy(devicename+4, windir, 1);
275 strncpy(filename, windir, strlen(windir));
276 strncat(filename,"\\SYSTEM32\\CONFIG\\SAM", 20);
277 if (debug)
278 printf("Filename: %s\n", filename);
279
280 if (!GetVolumeInformation(drive, NULL, 0, NULL, NULL, NULL, fs, sizeof(fs)))
281 {
282 sprintf(error, "Error with GetVolumeInformationByHandleW : %d \n", (int)GetLastError());
283 return -1;
284 }
285
286 if (debug)
287 printf("Partition type: %s\n", fs); //FAT32 //NTFS
288
289 if (strncmp(fs, "NTFS", 4))
290 ntfs = 1;
291 else if (strncmp(fs, "FAT32", 5))
292 ntfs = 0;
293 else {
294 sprintf(error, "This tool works only on NTFS or FAT32 volumes\n");
295 return -1;
296 }
297
298 // Creating a handle to disk drive using CreateFile () function ..
299 hDevice = CreateFile(devicename,
300 GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
301 NULL, OPEN_EXISTING, 0, NULL);
302
303 if (hDevice == INVALID_HANDLE_VALUE) {
304 sprintf(error, "Error with CreateFile: %d\n", (int)GetLastError());
305 return -1;
306 }
307
308 SetFilePointer (hDevice, 0, NULL, FILE_BEGIN);
309
310 if (!ReadFile (hDevice, boot_sector, 0x200, (PDWORD)&bytesread, NULL) ) {
311 sprintf(error, "Error with ReadFile: %d\n", (int)GetLastError());
312 CloseHandle(hDevice);
313 return -1;
314 }
315
316 sectorsize = *(uint16_t*)(boot_sector+0xb);
317 clustersize = boot_sector[0xd];
318 if (debug)
319 printf("Sector size: %d, Number of sectors by cluster: %d\n", sectorsize, clustersize);
320
321 //go on from here in fat32
322
323
324 if (get_handle(filename, &hSamFile, error, debug)) {
325 CloseHandle(hDevice);
326 return -1;
327 }
328 if (debug)
329 printf("%d\n", (int)hSamFile);
330
331 if (GetFileLCN(hSamFile, &location, error)) {
332 CloseHandle(hDevice);
333 return -1;
334 }
335
336
337 location *=clustersize;
338 filesize = GetFileSize(hSamFile, NULL);
339 if (filesize % sectorsize != 0)
340 filesize = (filesize/sectorsize+1)*sectorsize;
341
342 *buff_sam = (char *)malloc(filesize);
343
344 *buff_sam_size = filesize;
345 if (debug)
346 printf("File size: %d, Location = %lld\n", filesize, location);
347
348 tmp.QuadPart = location*sectorsize;
349 SetFilePointerEx(hDevice, tmp, NULL, FILE_BEGIN);
350
351 if (!ReadFile (hDevice, *buff_sam, filesize, (PDWORD)&bytesread, NULL) ) {
352 sprintf(error, "Error with ReadFile: %d\n", (int)GetLastError());
353 CloseHandle(hDevice);
354 return -1;
355 }
356
357 if (debug)
358 hexdump(*buff_sam, 0x200);
359
360 CloseHandle(hDevice);
361 CloseHandle(hSamFile);
362
363 return 0;
364 }
365
366 #endif