GetSectionAddress - Get the address of a named section of the module with the named `moduleName` returns a memory address of the section if found, and 0 if no section is found or an error occurs */
| 322 | returns a memory address of the section if found, and 0 if no section is found or an error occurs |
| 323 | */ |
| 324 | uintptr_t Process::GetSectionAddress(__in const char* moduleName, __in const char* sectionName) |
| 325 | { |
| 326 | if (moduleName == nullptr || sectionName == nullptr) |
| 327 | { |
| 328 | Logger::logf(Err, "module or section name pointers were null @ Process::GetSectionAddress"); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | HMODULE hModule = GetModuleHandleA(moduleName); |
| 333 | |
| 334 | if (hModule == NULL) |
| 335 | { |
| 336 | Logger::logf(Err, " Failed to get module handle: %d @ GetSectionAddress\n", GetLastError()); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | uintptr_t baseAddress = (uintptr_t)hModule; |
| 341 | |
| 342 | PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)baseAddress; |
| 343 | if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) |
| 344 | { |
| 345 | Logger::logf(Err, " Invalid DOS header @ GetSectionAddress.\n"); |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)(baseAddress + pDosHeader->e_lfanew); |
| 350 | if (pNtHeaders->Signature != IMAGE_NT_SIGNATURE) |
| 351 | { |
| 352 | Logger::logf(Err, " Invalid NT header @ GetSectionAddress.\n"); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | PIMAGE_SECTION_HEADER pSectionHeader = IMAGE_FIRST_SECTION(pNtHeaders); |
| 357 | |
| 358 | const int nSections = Process::GetNumSections(); |
| 359 | |
| 360 | for (int i = 0; i < nSections; i++) //we are modifying # of sections at runtime to throw attackers off |
| 361 | { |
| 362 | if ((const char*)pSectionHeader->Name != nullptr) |
| 363 | { |
| 364 | if (strcmp((const char*)pSectionHeader->Name, sectionName) == 0) |
| 365 | { |
| 366 | return (uintptr_t)(baseAddress + pSectionHeader->VirtualAddress); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | pSectionHeader++; |
| 371 | } |
| 372 | |
| 373 | Logger::logf(Warning, ".text section not found.\n"); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | GetBytesAtAddress - return bytes from an address given `size`. |
nothing calls this directly
no outgoing calls
no test coverage detected