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 build_imports #530

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/LIEF/PE/ImportEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ class LIEF_API ImportEntry : public Object {
//! @brief **Original** address of the entry in the Import Address Table
uint64_t iat_address(void) const;

bool manually_added(void) const;

void name(const std::string& name);
void data(uint64_t data);
void manually_added(bool manually_added);

virtual void accept(Visitor& visitor) const override;

Expand All @@ -84,6 +86,8 @@ class LIEF_API ImportEntry : public Object {
uint64_t iat_value_;
uint64_t rva_;
PE_TYPE type_;

bool manually_added_;
};

}
Expand Down
7 changes: 6 additions & 1 deletion src/PE/Builder.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void Builder::build_import_table(void) {
for (const ImportEntry& entry : import.entries()) {

// If patch is enabled, we have to create a trampoline for this function
if (this->patch_imports_) {
if (this->patch_imports_ && (not entry.manually_added())) {
std::vector<uint8_t> instructions;
uint64_t address = this->binary_->optional_header().imagebase() + import_section.virtual_address() + iat_offset;
if (this->binary_->hooks_.count(import_name) > 0 and this->binary_->hooks_[import_name].count(entry.name())) {
Expand Down Expand Up @@ -342,6 +342,11 @@ void Builder::build_import_table(void) {
const uint32_t rva = static_cast<uint32_t>(import_section.virtual_address() + iat_offset);
this->binary_->data_directory(DATA_DIRECTORY::IAT).RVA(rva);
this->binary_->data_directory(DATA_DIRECTORY::IAT).size(functions_name_offset - iat_offset + 1);

// Finaly disable ASLR
const auto old_dll_chars = this->binary_->optional_header().dll_characteristics();
const auto new_dll_chars = old_dll_chars & (~static_cast<uint32_t>(LIEF::PE::DLL_CHARACTERISTICS::IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE));
this->binary_->optional_header().dll_characteristics(new_dll_chars);
}

template<typename PE_T>
Expand Down
2 changes: 2 additions & 0 deletions src/PE/Import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,14 @@ void Import::import_address_table_rva(uint32_t rva) {

ImportEntry& Import::add_entry(const ImportEntry& entry) {
this->entries_.push_back(entry);
this->entries_.back().manually_added(true);
return this->entries_.back();
}


ImportEntry& Import::add_entry(const std::string& name) {
this->entries_.emplace_back(name);
this->entries_.back().manually_added(true);
return this->entries_.back();
}

Expand Down
14 changes: 12 additions & 2 deletions src/PE/ImportEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ ImportEntry::ImportEntry(void) :
hint_{0},
iat_value_{0},
rva_{0},
type_{PE_TYPE::PE32}
type_{PE_TYPE::PE32},
manually_added_(false)
{}

ImportEntry::ImportEntry(uint64_t data, const std::string& name) :
Expand All @@ -42,7 +43,8 @@ ImportEntry::ImportEntry(uint64_t data, const std::string& name) :
hint_{0},
iat_value_{0},
rva_{0},
type_{PE_TYPE::PE32}
type_{PE_TYPE::PE32},
manually_added_(false)
{}


Expand Down Expand Up @@ -101,6 +103,10 @@ uint64_t ImportEntry::iat_address(void) const {
return this->rva_;
}

bool ImportEntry::manually_added(void) const {
return this->manually_added_;
}

void ImportEntry::name(const std::string& name) {
this->name_ = name;
}
Expand All @@ -109,6 +115,9 @@ void ImportEntry::data(uint64_t data) {
this->data_ = data;
}

void ImportEntry::manually_added(bool manually_added) {
this->manually_added_ = manually_added;
}

void ImportEntry::accept(LIEF::Visitor& visitor) const {
visitor.visit(*this);
Expand All @@ -134,6 +143,7 @@ std::ostream& operator<<(std::ostream& os, const ImportEntry& entry) {
os << std::setw(20) << entry.data();
os << std::setw(20) << entry.iat_value();
os << std::setw(20) << entry.hint();
os << std::setw(20) << entry.manually_added();
return os;
}

Expand Down
1 change: 1 addition & 0 deletions src/PE/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ void Hash::visit(const ImportEntry& import_entry) {
this->process(import_entry.iat_value());
this->process(import_entry.name());
this->process(import_entry.data());
this->process(import_entry.manually_added());
}

void Hash::visit(const ResourceNode& resource_node) {
Expand Down
1 change: 1 addition & 0 deletions src/PE/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ void JsonVisitor::visit(const ImportEntry& import_entry) {
this->node_["iat_address"] = import_entry.iat_address();
this->node_["data"] = import_entry.data();
this->node_["hint"] = import_entry.hint();
this->node_["manually_added"] = import_entry.manually_added();
}

void JsonVisitor::visit(const ResourceData& resource_data) {
Expand Down