Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IAT Reconstruction #528

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/LIEF/PE/enums.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ enum class PE_SECTION_TYPES : uint8_t {
EXPORT = 7,
DEBUG = 8,
LOAD_CONFIG = 9,
UNKNOWN = 10
UNKNOWN = 10,
OLD_IMPORT = 100
};

enum class PE_TYPE : uint16_t {
Expand Down
20 changes: 16 additions & 4 deletions src/PE/Builder.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,32 @@ void Builder::build_import_table(void) {
// Remove 'import' type from the original section
if (it_import_section != std::end(this->binary_->sections_)) {
(*it_import_section)->remove_type(PE_SECTION_TYPES::IMPORT);
(*it_import_section)->add_type(PE_SECTION_TYPES::OLD_IMPORT);
}

// As add_section will change DATA_DIRECTORY::IMPORT_TABLE we have to save it before
uint32_t offset_imports = this->binary_->rva_to_offset(this->binary_->data_directory(DATA_DIRECTORY::IMPORT_TABLE).RVA());
auto offset_imports_in_section = offset_imports - this->binary_->section_from_offset(offset_imports).offset();
Section& import_section = this->binary_->add_section(new_import_section, PE_SECTION_TYPES::IMPORT);


// Patch the original IAT with the address of the associated trampoline
if (this->patch_imports_) {
Section& original_import = this->binary_->section_from_offset(offset_imports);
auto&& original_import_section = std::find_if(
std::begin(this->binary_->sections_),
std::end(this->binary_->sections_),
[] (const Section* section) {
return section != nullptr and section->is_type(PE_SECTION_TYPES::OLD_IMPORT);
});
// fallback mechanism
const auto is_found = original_import_section != std::end(this->binary_->sections_);
if (is_found) {
(*original_import_section)->remove_type(PE_SECTION_TYPES::OLD_IMPORT);
}
auto original_import_offset = is_found ? (*original_import_section)->pointerto_raw_data() : 0;
Section& original_import = this->binary_->section_from_offset(is_found ? original_import_offset: offset_imports);
std::vector<uint8_t> import_content = original_import.content();
uint32_t roffset_import = offset_imports - original_import.offset();

pe_import *import_header = reinterpret_cast<pe_import*>(import_content.data() + roffset_import);
pe_import *import_header = reinterpret_cast<pe_import*>(import_content.data() + offset_imports_in_section);
uint32_t jumpOffsetTmp = trampolines_offset;
while (import_header->ImportAddressTableRVA != 0) {
uint32_t offsetTable = this->binary_->rva_to_offset(import_header->ImportLookupTableRVA) - original_import.pointerto_raw_data();
Expand Down