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
|
/** dumpsam.c
Dumpsam file (only for Windows) for samdump2 3.x
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; either version 2
of the License, or (at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
This program is released under the GPL with the additional exemption
that compiling, linking, and/or using OpenSSL is allowed.
Copyright (C) 2009 Cedric Tissieres <http://www.objectif-securite.ch>
*/
#ifdef WIN32
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <stdint.h>
#include <tchar.h>
#include "ntdll.h"
/***********************************************************/
/* code from http://www.ivanlef0u.tuxfamily.org/?p=77 */
ULONG EnablePrivilege(char *Privilege)
{
HANDLE hToken;
ULONG Ret=1;
TOKEN_PRIVILEGES TP;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
printf("Error with OpenProcessToken: %d\n", (int)GetLastError());
Ret=0;
goto bye;
}
if(!LookupPrivilegeValue(NULL, Privilege, &TP.Privileges[0].Luid))
{
printf("Error with LookupPrivilegeValue: %d\n", (int)GetLastError());
Ret=0;
goto bye;
}
TP.PrivilegeCount=1;
TP.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(hToken,
0,
&TP,
0,
NULL,
NULL))
{
printf("Error with AdjustTokenPrivileges: %d\n", (int)GetLastError());
Ret=0;
goto bye;
}
bye:
if(hToken)
CloseHandle(hToken);
return Ret;
}
/***********************************************************/
/* code from http://www.ivanlef0u.tuxfamily.org/?p=77 */
void hexdump(unsigned char *data, unsigned int amount) {
unsigned int dp, p;
const char trans[] =
"................................ !\"#$%&'()*+,-./0123456789"
":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
"nopqrstuvwxyz{|}~...................................."
"....................................................."
"........................................";
for (dp = 1; dp <= amount; dp++) {
printf ("%02x ", data[dp-1]);
if ((dp % 8) == 0)
printf (" ");
if ((dp % 16) == 0) {
printf ("| ");
p = dp;
for (dp -= 16; dp < p; dp++)
printf ("%c", trans[data[dp]]);
printf ("\n");
}
}
if ((amount % 16) != 0) {
p = dp = 16 - (amount % 16);
for (dp = p; dp > 0; dp--) {
printf (" ");
if (((dp % 8) == 0) && (p != 8))
printf (" ");
}
printf (" | ");
for (dp = (amount - (16 - p)); dp < amount; dp++)
printf ("%c", trans[data[dp]]);
}
printf ("\n");
return ;
}
/***********************************************************/
/* code from http://www.ivanlef0u.tuxfamily.org/?p=77 */
int GetFileLCN(HANDLE hFile, int64_t *location, char *error)
{
ULONG Status;
IO_STATUS_BLOCK IoBlock;
unsigned char *temp;
STARTING_VCN_INPUT_BUFFER VCN;
RETRIEVAL_POINTERS_BUFFER Buff;
memset(&VCN, 0, sizeof(STARTING_VCN_INPUT_BUFFER));
temp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0x10000);
Status=ZwFsControlFile(hFile,
NULL,
NULL,
NULL,
&IoBlock,
FSCTL_GET_RETRIEVAL_POINTERS,
&VCN,
sizeof(STARTING_VCN_INPUT_BUFFER),
&Buff,
sizeof(RETRIEVAL_POINTERS_BUFFER));
if(!NT_SUCCESS(Status))
{
sprintf(error,"Error with ZwFsControlFile : 0x%x\n", (int)Status);
return -1;
}
// printf("ExtentCount : %d\n", Buff.ExtentCount);
*location = Buff.Extents[0].Lcn.QuadPart;
/* for(i=0; i<Buff.ExtentCount; i++) */
/* { */
/* printf("LCN : 0x%x\n", Buff.Extents[i].Lcn); */
/* printf("VCN : 0x%x\n", Buff.Extents[i].NextVcn); */
/* } */
HeapFree(GetProcessHeap(), 0, temp);
return 0;
}
/***********************************************************/
int get_handle(unsigned char *path, PHANDLE outHandle, char *error, int debug) {
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK status_block;
UNICODE_STRING unicode_string;
int status;
unsigned char *fullpath;
wchar_t *fullpathunicode;
fullpath = (unsigned char *) malloc(5+strlen(path));
memset(fullpath, 0, 5+strlen(path));
fullpathunicode = (wchar_t *) malloc((5+strlen(path))*sizeof(wchar_t));
strncpy(fullpath, "\\??\\", 4);
strncpy(fullpath+4, path, strlen(path));
if (debug)
printf("Full path: %s\n", fullpath);
mbstowcs(fullpathunicode, fullpath, strlen(fullpath));
fullpathunicode[strlen(fullpath)] = 0;
RtlInitUnicodeString(&unicode_string, fullpathunicode);
InitializeObjectAttributes(&ObjectAttributes, &unicode_string, 0, NULL, NULL);
status = ZwOpenFile(outHandle, FILE_READ_ATTRIBUTES|SYNCHRONIZE, &ObjectAttributes, &status_block, 3, FILE_SYNCHRONOUS_IO_NONALERT);
if(status!=STATUS_SUCCESS)
{
sprintf(error, "Error with ZwOpenFile : 0x%x : %d \n", status, (int)RtlNtStatusToDosError(status));
return -1;
}
if (debug)
printf("Got handle : %d\n", (int)*outHandle);
return 0;
}
/***********************************************************/
int get_live_syskey(unsigned char *pkey, char *error, int debug) {
int i;
HKEY hk;
HKEY hkt;
char *kn[] = {"JD", "Skew1", "GBG", "Data"};
char kv[9];
unsigned long kvsize;
unsigned char key[16];
unsigned char pkeystring[16*2+1] = {0};
#if BYTE_ORDER == LITTLE_ENDIAN
int p[] = { 0xb, 0x6, 0x7, 0x1, 0x8, 0xa, 0xe, 0x0, 0x3, 0x5, 0x2, 0xf, 0xd, 0x9, 0xc, 0x4 };
#elif BYTE_ORDER == BIG_ENDIAN
int p[] = { 0x8, 0x5, 0x4, 0x2, 0xb, 0x9, 0xd, 0x3, 0x0, 0x6, 0x1, 0xc, 0xe, 0xa, 0xf, 0x7 };
#endif
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Lsa", 0, KEY_READ, &hk)==0) {
for (i=0; i<4; i++) {
if (RegOpenKeyEx(hk, kn[i], 0, KEY_READ, &hkt)) {
sprintf(error, "Error reading %s key\n", kn[i]);
return -1;
}
kvsize = sizeof(kv);
RegQueryInfoKey(hkt, kv, &kvsize, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
RegCloseKey(hkt);
sscanf(kv, "%x", (int*) (&key[i*4]));
}
RegCloseKey(hk);
for(i=0; i<16; i++) {
pkey[i] = key[p[i]];
if (debug)
sprintf(pkeystring+i*2, "%.2x", pkey[i]);
}
} else {
sprintf(error, "Error reading LSA key\n");
return -1;
}
if (debug)
printf("key = %s\n", pkeystring);
return 0;
}
/***********************************************************/
int get_sam(unsigned char **buff_sam, int *buff_sam_size, char *error, int debug) {
uint32_t bytesread, filesize, sectorsize, clustersize;
int64_t location;
HANDLE hDevice, hSamFile;
LARGE_INTEGER tmp;
char *windir;
char drive[] = "C:\\";
char fs[16] ={0};
char filename[MAX_PATH] ={0};
char devicename[] = "\\\\.\\C:";
char boot_sector[512]={0};
char ntfs = 0;
if(!EnablePrivilege("SeDebugPrivilege")) {
sprintf(error, "Unable to set Debug Privilege. You must be admin to run this program.\n");
return -1;
}
windir = getenv("windir");
strncpy(drive, windir, 1);
strncpy(devicename+4, windir, 1);
strncpy(filename, windir, strlen(windir));
strncat(filename,"\\SYSTEM32\\CONFIG\\SAM", 20);
if (debug)
printf("Filename: %s\n", filename);
if (!GetVolumeInformation(drive, NULL, 0, NULL, NULL, NULL, fs, sizeof(fs)))
{
sprintf(error, "Error with GetVolumeInformationByHandleW : %d \n", (int)GetLastError());
return -1;
}
if (debug)
printf("Partition type: %s\n", fs); //FAT32 //NTFS
if (strncmp(fs, "NTFS", 4))
ntfs = 1;
else if (strncmp(fs, "FAT32", 5))
ntfs = 0;
else {
sprintf(error, "This tool works only on NTFS or FAT32 volumes\n");
return -1;
}
// Creating a handle to disk drive using CreateFile () function ..
hDevice = CreateFile(devicename,
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
sprintf(error, "Error with CreateFile: %d\n", (int)GetLastError());
return -1;
}
SetFilePointer (hDevice, 0, NULL, FILE_BEGIN);
if (!ReadFile (hDevice, boot_sector, 0x200, (PDWORD)&bytesread, NULL) ) {
sprintf(error, "Error with ReadFile: %d\n", (int)GetLastError());
CloseHandle(hDevice);
return -1;
}
sectorsize = *(uint16_t*)(boot_sector+0xb);
clustersize = boot_sector[0xd];
if (debug)
printf("Sector size: %d, Number of sectors by cluster: %d\n", sectorsize, clustersize);
//go on from here in fat32
if (get_handle(filename, &hSamFile, error, debug)) {
CloseHandle(hDevice);
return -1;
}
if (debug)
printf("%d\n", (int)hSamFile);
if (GetFileLCN(hSamFile, &location, error)) {
CloseHandle(hDevice);
return -1;
}
location *=clustersize;
filesize = GetFileSize(hSamFile, NULL);
if (filesize % sectorsize != 0)
filesize = (filesize/sectorsize+1)*sectorsize;
*buff_sam = (char *)malloc(filesize);
*buff_sam_size = filesize;
if (debug)
printf("File size: %d, Location = %lld\n", filesize, location);
tmp.QuadPart = location*sectorsize;
SetFilePointerEx(hDevice, tmp, NULL, FILE_BEGIN);
if (!ReadFile (hDevice, *buff_sam, filesize, (PDWORD)&bytesread, NULL) ) {
sprintf(error, "Error with ReadFile: %d\n", (int)GetLastError());
CloseHandle(hDevice);
return -1;
}
if (debug)
hexdump(*buff_sam, 0x200);
CloseHandle(hDevice);
CloseHandle(hSamFile);
return 0;
}
#endif
|