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
|
/*
** The Sleuth Kit
**
** Brian Carrier [carrier <at> sleuthkit [dot] org]
** Copyright (c) 2010-2019 Brian Carrier. All Rights reserved
**
** This software is distributed under the Common Public License 1.0
**
*/
#include <iostream>
#include "RegParser.h"
#include "ReportUtil.h"
RegParser::RegParser(const RegHiveType::Enum aHiveType)
: m_registryHive(NULL), m_rootKey(NULL) {
}
RegParser::RegParser(const std::wstring &filePath) {
m_registryHive = new Rejistry::RegistryHiveFile(filePath);
m_rootKey = m_registryHive->getRoot();
}
RegParser::~RegParser() {
if (m_rootKey != NULL) {
delete m_rootKey;
m_rootKey = NULL;
}
if (m_registryHive != NULL) {
delete m_registryHive;
m_registryHive = NULL;
}
}
/**
* Load a hive
*
* @param aHiveFile TSK_FS_FILE hive file
* @param aHiveType RegHiveType::Enum hive type
* @returns 0 on success, -1 on error
*/
int RegParser::loadHive(TSK_FS_FILE *aHiveFile, RegHiveType::Enum aHiveType) {
if (aHiveFile == NULL) {
ReportUtil::consoleOutput(stderr, "Null pointer passed to RegParser::loadHive. loadHive() failed.\n");
return -1;
}
// If there already is a loaded hive, free it.
if (m_registryHive != NULL) {
delete m_registryHive;
m_registryHive = NULL;
}
// Read the contents of the TSK_FS_FILE into memory.
uint8_t *registryBuffer;
if ((registryBuffer = (uint8_t *)malloc((size_t)aHiveFile->meta->size)) == NULL) {
ReportUtil::consoleOutput(stderr, "loadHive(): Error allocating memory for hive file. tsk_fs_file_read() failed.\n");
return -1;
}
ssize_t bytesRead = tsk_fs_file_read(aHiveFile, 0, (char *)®istryBuffer[0],
(size_t)aHiveFile->meta->size, TSK_FS_FILE_READ_FLAG_NONE);
if (bytesRead != aHiveFile->meta->size) {
ReportUtil::consoleOutput(stderr, "loadHive(): Error reading content from hive file. tsk_fs_file_read() failed.\n");
free(registryBuffer);
return -1;
}
try {
m_registryHive = new Rejistry::RegistryHiveBuffer(registryBuffer, (uint32_t)aHiveFile->meta->size);
}
catch (Rejistry::RegistryParseException &) {
ReportUtil::consoleOutput(stderr, "loadHive(): Error creating RegistryHiveBuffer. Likely because of memory size.\n");
free(registryBuffer);
return -1;
}
catch (...) {
ReportUtil::consoleOutput(stderr, "loadHive(): Error creating RegistryHiveBuffer (general exception). Likely because of memory size.\n");
free(registryBuffer);
return -1;
}
m_rootKey = m_registryHive->getRoot();
free(registryBuffer);
return 0;
}
/**
* Get the root key
*
* @param output aKey RegKey to receive the root key
* @returns 0 on success
*/
int RegParser::getRootKey(RegKey &aKey) {
aKey.initialize(m_rootKey);
return 0;
}
/**
* Get the registry key for the given name.
* The key name must contain one or more path elements,
* e.g. "Setup" or "Setup\AllowStart\ProtectedStorage".
* Path elements must be separated by the backslash
* character. The key name will be evaluated relative to
* the root of the registry file. The hive type (e.g.
* HKLM\SYSTEM) must not be part of the key name.
*
* @param input keyName The name of the registry key.
* @param output aKey A key object that will be populated with
* data related to the key (if found).
* @returns
* 0 if the key was found.
* -1 if the key was not found.
* -2 if there was an error getting the key.
*/
int RegParser::getKey(const std::wstring &keyName, RegKey &aKey) {
const Rejistry::RegistryKey *key = NULL;
try {
key = findKey(keyName);
}
catch (Rejistry::NoSuchElementException&) {
return -1;
}
catch (Rejistry::RegistryParseException&) {
return -2;
}
catch (...) {
return -2;
}
aKey.initialize(key);
if (key != NULL) {
delete key;
}
return 0;
}
/**
* Get the names of the subkeys (if any) for the given registry key.
*
* @param input keyName The name of the registry key to retrieve subkeys for.
* See the RegParser::getKey documentation for key name format rules.
* @param output subKeyNamesList The returned list of subkey names. The list
* will be empty if the key contains no values.
*
* @returns
* 0 if the key is found and data is being returned.
* -1 if the key is not found.
* -2 if there was an error getting the key.
*/
int RegParser::getSubKeys(const std::wstring &keyName, std::vector<std::wstring> &subKeyNamesList) {
try {
std::auto_ptr<Rejistry::RegistryKey const> key(findKey(keyName));
Rejistry::RegistryKey::RegistryKeyPtrList subkeys = key->getSubkeyList();
subKeyNamesList.reserve(subkeys.size());
Rejistry::RegistryKey::RegistryKeyPtrList::iterator subKeyIter = subkeys.begin();
for (; subKeyIter != subkeys.end(); ++subKeyIter) {
subKeyNamesList.push_back((*subKeyIter)->getName());
}
for (subKeyIter = subkeys.begin(); subKeyIter != subkeys.end(); ++subKeyIter) {
delete *subKeyIter;
}
}
catch (Rejistry::NoSuchElementException&) {
return -1;
}
catch (Rejistry::RegistryParseException&) {
return -2;
}
catch (...) {
return -2;
}
return 0;
}
/**
* Get the subkeys (if any) for the given registry key.
*
* @param input keyName The name of the registry key to retrieve subkeys for.
* See the RegParser::getKey documentation for key name format rules.
* @param output subKeysList The returned list of subkeys. The list
* will be empty if the key contains no values.
*
* @returns
* 0 if the key is found and data is being returned.
* -1 if the key is not found.
* -2 if there was an error getting the key.
*/
int RegParser::getSubKeys(const std::wstring &keyName, std::vector<RegKey*> &subKeysList) {
try {
std::auto_ptr<Rejistry::RegistryKey const> key(findKey(keyName));
Rejistry::RegistryKey::RegistryKeyPtrList subkeys = key->getSubkeyList();
subKeysList.reserve(subkeys.size());
Rejistry::RegistryKey::RegistryKeyPtrList::iterator subKeyIter = subkeys.begin();
for (; subKeyIter != subkeys.end(); ++subKeyIter) {
RegKey * key = new RegKey((*subKeyIter)->getName());
key->initialize(*subKeyIter);
subKeysList.push_back(key);
}
for (subKeyIter = subkeys.begin(); subKeyIter != subkeys.end(); ++subKeyIter) {
delete *subKeyIter;
}
}
catch (Rejistry::NoSuchElementException&) {
return -1;
}
catch (Rejistry::RegistryParseException&) {
return -2;
}
catch (...) {
return -2;
}
return 0;
}
/**
* Get the value associated with the given key name and value name.
*
* @param input keyName The name of the registry key in which to look for
* the given value name.
* See the RegParser::getKey documentation for key name format rules.
* @param input valName The name of the value to retrieve.
* @param output val A value object that has been populated with data related
* to the value.
*
* @returns
* 0 if the key/value was found.
* -1 if the key/value was not found.
* -2 if there was an error getting the key/value.
*/
int RegParser::getValue(const std::wstring &keyName, const std::wstring &valName, RegVal &val) {
try {
std::auto_ptr<Rejistry::RegistryKey const> key(findKey(keyName));
Rejistry::RegistryValue *value = key->getValue(valName);
val.initialize(value);
delete value;
}
catch (Rejistry::NoSuchElementException&) {
return -1;
}
catch (Rejistry::RegistryParseException&) {
return -2;
}
catch (...) {
return -2;
}
return 0;
}
/**
* Get the value associated with the given value name relative to the given
* key and optional subpath.
*
* @param input startKey The key in which to look for the given subpath and
* value name.
* See the RegParser::getKey documentation for key name format rules.
* @param input subpathName An optional subpath under the given key from which
* to retrieve the given value.
* @param input valName The name of the value to retrieve.
* @param output val A value object that has been populated with data related
* to the value.
*
* @returns
* 0 if the key/value was found.
* -1 if the key/value was not found.
* -2 if there was an error getting the key/value.
*/
int RegParser::getValue(const RegKey *startKey, const std::wstring &subpathName, const std::wstring &valName, RegVal &val) {
if (NULL == startKey) {
return -1;
}
try {
std::auto_ptr<Rejistry::RegistryKey const> key(findKey(subpathName, startKey->getRegistryKey()));
Rejistry::RegistryValue *value = key->getValue(valName);
val.initialize(value);
delete value;
}
catch (Rejistry::NoSuchElementException&) {
return -1;
}
catch (Rejistry::RejistryException &) {
return -2;
}
catch (...) {
return -2;
}
return 0;
}
/**
* Get the values (if any) for the given registry key.
*
* @param input keyName The name of the registry key to retrieve values for.
* See the RegParser::getKey documentation for key name format rules.
* @param output valList The returned list of values. The list will be empty
* if the key contains no values.
*
* @returns
* 0 if the key was found.
* -1 if the key was not found.
* -2 if there was an error getting the key.
*/
int RegParser::getValues(const std::wstring &keyName, std::vector<RegVal *> &valList) {
try {
std::auto_ptr<Rejistry::RegistryKey const> key(findKey(keyName));
Rejistry::RegistryValue::RegistryValuePtrList values = key->getValueList();
valList.reserve(values.size());
Rejistry::RegistryValue::RegistryValuePtrList::iterator valueIter = values.begin();
for (; valueIter != values.end(); ++valueIter) {
valList.push_back(new RegVal((*valueIter)));
}
for (valueIter = values.begin(); valueIter != values.end(); ++valueIter) {
delete *valueIter;
}
}
catch (Rejistry::NoSuchElementException&) {
return -1;
}
catch (Rejistry::RegistryParseException&) {
return -2;
}
catch (...) {
return -2;
}
return 0;
}
/**
* Get all values (if any) for the given subpath relative to the given registry key.
*
* @param input startKey The registry key in which to look for the given subpath.
* @param input subPath The path to the key to retrieve values for.
* See the RegParser::getKey documentation for key name format rules.
* @param output valList The returned list of values. The list will be empty
* if the key contains no values.
*
* @returns
* 0 if the key was found.
* -1 if the key was not found.
* -2 if there was an error getting the key.
*/
int RegParser::getValues(const RegKey *startKey, const std::wstring &subpathName, std::vector<RegVal *> &valList) {
if (NULL == startKey) {
return -1;
}
try {
std::auto_ptr<Rejistry::RegistryKey const> key(findKey(subpathName, startKey->getRegistryKey()));
Rejistry::RegistryValue::RegistryValuePtrList values = key->getValueList();
valList.reserve(values.size());
Rejistry::RegistryValue::RegistryValuePtrList::iterator valueIter = values.begin();
for (; valueIter != values.end(); ++valueIter) {
valList.push_back(new RegVal((*valueIter)));
}
for (valueIter = values.begin(); valueIter != values.end(); ++valueIter) {
delete *valueIter;
}
}
catch (Rejistry::NoSuchElementException&) {
return -1;
}
catch (Rejistry::RegistryParseException&) {
return -2;
}
catch (...) {
return -2;
}
return 0;
}
/**
* Find the key with the given name relative to the optional starting key.
*
* @param input keyName The name of the key to find.
* @param input startingKey An optional starting point from which to search.
* If not provided, the search will start at the root of the registry.
* @returns A pointer to the registry key.
* @throws Rejistry::NoSuchElementException if the key is not found.
* @throws Rejistry::RegistryParseException if there was an error getting
* the key.
*/
const Rejistry::RegistryKey *RegParser::findKey(const std::wstring &keyName, const Rejistry::RegistryKey *startingKey) const {
if (keyName == m_rootKey->getName()) {
return new Rejistry::RegistryKey(*m_rootKey);
}
std::vector<std::wstring> keyElements = splitKeyName(keyName);
std::vector<std::wstring>::iterator keyIter = keyElements.begin();
const Rejistry::RegistryKey *currentKey = startingKey == NULL ? m_rootKey : startingKey;
// Navigate our way down the tree looking to locate the desired key.
for (; keyIter != keyElements.end(); ++keyIter) {
try {
Rejistry::RegistryKey *nextKey = currentKey->getSubkey((*keyIter));
if (currentKey != m_rootKey && currentKey != startingKey) {
// Free the key we just searched (as long as its not the root or the starting key)
delete currentKey;
}
currentKey = nextKey;
}
catch (Rejistry::NoSuchElementException&) {
// If we fail on the root element, we will continue and
// try the next element in the path. Otherwise we rethrow
// the exception.
if ((*keyIter) != m_rootKey->getName()) {
throw;
}
}
}
if (currentKey == startingKey) {
return new Rejistry::RegistryKey(*currentKey);
}
else {
return currentKey;
}
}
/**
* Splits a key into its constituent parts. The key name parts must be
* separated by the backslash character.
*
* @param input keyName The key to split.
* @returns The split key elements as a vector of strings.
*/
std::vector<std::wstring> RegParser::splitKeyName(const std::wstring &keyName) const {
std::vector<std::wstring> keys;
size_t start = 0;
size_t end = 0;
while (start < keyName.size()) {
size_t pos = keyName.find('\\', start);
if (pos == std::wstring::npos) {
end = keyName.size();
}
else {
end = pos;
}
keys.push_back(std::wstring(&keyName[start], &keyName[end]));
start = end + 1;
}
return keys;
}
|