Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define makeptr(cast, address, offset) (cast)( (DWORD_PTR)address + (DWORD_PTR)offset )
- #define makedelta(cast, address, offset) (cast)( (DWORD_PTR)address - (DWORD_PTR)offset )
- IMAGE_SECTION_HEADER* GetEnclosingSectionHeader ( DWORD_PTR RVA, IMAGE_NT_HEADERS* NtHeader )
- {
- IMAGE_SECTION_HEADER* Section = IMAGE_FIRST_SECTION(NtHeader);
- for ( int i = 0; i < NtHeader->FileHeader.NumberOfSections; i++, Section++ )
- {
- DWORD_PTR Size = Section->Misc.VirtualSize;
- if ( !Size )
- Size = Section->SizeOfRawData;
- if ( (RVA >= Section->VirtualAddress) && (RVA < (Section->VirtualAddress + Size)) )
- return Section;
- }
- return 0;
- }
- DWORD_PTR GetMappedSection ( IMAGE_SECTION_HEADER* SectionHeader, HMODULE Module, IMAGE_NT_HEADERS* NtHeader )
- {
- IMAGE_SECTION_HEADER* Section = IMAGE_FIRST_SECTION(NtHeader);
- DWORD_PTR Offset = makedelta(DWORD_PTR, Section, Module);
- for ( int i = 0; i < NtHeader->FileHeader.NumberOfSections; i++, Section++ )
- {
- if ( Section->Name == SectionHeader->Name )
- {
- Offset = makedelta(DWORD_PTR, Section->VirtualAddress, Section->PointerToRawData);
- break;
- }
- }
- return Offset;
- }
- void* GetPtrFromRVA ( DWORD_PTR RVA, IMAGE_NT_HEADERS* NtHeader, HMODULE Module, bool Mapped )
- {
- IMAGE_SECTION_HEADER* SectionHeader = GetEnclosingSectionHeader ( RVA, NtHeader );
- DWORD_PTR Offset = 0;
- if ( Mapped )
- Offset = GetMappedSection ( SectionHeader, Module, NtHeader );
- DWORD_PTR Delta = (DWORD_PTR)( SectionHeader->VirtualAddress - SectionHeader->PointerToRawData );
- return (void*)( Module + RVA - Delta + Offset );
- }
Advertisement
Add Comment
Please, Sign In to add comment