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