Skip to content

Commit e0634c1

Browse files
committed
Fix #20
1 parent eb1c0d3 commit e0634c1

File tree

9 files changed

+123
-30
lines changed

9 files changed

+123
-30
lines changed

api/python/PE/objects/pyBinary.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ void init_PE_Binary_class(py::module& m) {
171171
.def_property_readonly("resources_manager",
172172
static_cast<no_const_getter<ResourcesManager>>(&Binary::get_resources_manager))
173173

174+
.def_property_readonly("overlay",
175+
static_cast<no_const_getter<std::vector<uint8_t>&>>(&Binary::overlay),
176+
"Return the overlay content",
177+
py::return_value_policy::reference)
178+
174179
.def("add_import_function",
175180
&Binary::add_import_function,
176181
py::return_value_policy::reference)

api/python/PE/objects/pyBuilder.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ void init_PE_Builder_class(py::module& m) {
5050
&Builder::build_imports,
5151
py::return_value_policy::reference)
5252

53+
.def("build_overlay",
54+
static_cast<Builder& (Builder::*)(bool)>(&Builder::build_overlay),
55+
py::return_value_policy::reference)
56+
5357
.def("write",
5458
&Builder::write)
5559

include/LIEF/PE/Binary.hpp

+36-26
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ class DLL_PUBLIC Binary : public LIEF::Binary {
215215
Debug& get_debug(void);
216216
const Debug& get_debug(void) const;
217217

218+
// =======
219+
// Overlay
220+
// =======
221+
222+
//! @brief Return the overlay content
223+
const std::vector<uint8_t>& overlay(void) const;
224+
std::vector<uint8_t>& overlay(void);
225+
226+
218227
// =========================
219228
// Methods to manage Imports
220229
// =========================
@@ -293,32 +302,33 @@ class DLL_PUBLIC Binary : public LIEF::Binary {
293302
void update_lookup_address_table_offset(void);
294303
void update_iat(void);
295304

296-
PE_TYPE type_;
297-
DosHeader dos_header_;
298-
Header header_;
299-
OptionalHeader optional_header_;
300-
301-
bool has_tls_;
302-
bool has_imports_;
303-
bool has_signature_;
304-
bool has_exports_;
305-
bool has_resources_;
306-
bool has_exceptions_;
307-
bool has_relocations_;
308-
bool has_debug_;
309-
bool has_configuration_;
310-
311-
Signature signature_;
312-
TLS tls_;
313-
sections_t sections_;
314-
data_directories_t data_directories_;
315-
symbols_t symbols_;
316-
strings_table_t strings_table_;
317-
relocations_t relocations_;
318-
ResourceNode* resources_;
319-
imports_t imports_;
320-
Export export_;
321-
Debug debug_;
305+
PE_TYPE type_;
306+
DosHeader dos_header_;
307+
Header header_;
308+
OptionalHeader optional_header_;
309+
310+
bool has_tls_;
311+
bool has_imports_;
312+
bool has_signature_;
313+
bool has_exports_;
314+
bool has_resources_;
315+
bool has_exceptions_;
316+
bool has_relocations_;
317+
bool has_debug_;
318+
bool has_configuration_;
319+
320+
Signature signature_;
321+
TLS tls_;
322+
sections_t sections_;
323+
data_directories_t data_directories_;
324+
symbols_t symbols_;
325+
strings_table_t strings_table_;
326+
relocations_t relocations_;
327+
ResourceNode* resources_;
328+
imports_t imports_;
329+
Export export_;
330+
Debug debug_;
331+
std::vector<uint8_t> overlay_;
322332

323333
};
324334

include/LIEF/PE/Builder.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class DLL_PUBLIC Builder
5454
Builder& build_relocations(bool flag = true);
5555
Builder& build_tls(bool flag = true);
5656
Builder& build_resources(bool flag);
57+
Builder& build_overlay(bool flag);
5758

5859
const std::vector<uint8_t>& get_build(void);
5960
void write(const std::string& filename) const;
@@ -83,6 +84,7 @@ class DLL_PUBLIC Builder
8384
void build_string_table(void);
8485
void build_relocation(void);
8586
void build_resources(void);
87+
void build_overlay(void);
8688

8789
void compute_resources_size(
8890
ResourceNode *node,
@@ -108,6 +110,7 @@ class DLL_PUBLIC Builder
108110
bool build_relocations_;
109111
bool build_tls_;
110112
bool build_resources_;
113+
bool build_overlay_;
111114

112115
};
113116

include/LIEF/PE/Parser.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class DLL_PUBLIC Parser : public LIEF::Parser {
8383
void build_string_table(void);
8484
void build_symbols(void);
8585
void build_signature(void);
86+
void build_overlay(void);
8687

8788
ResourceNode* build_resource_node(
8889
const pe_resource_directory_table *directoryTable, uint32_t baseOffset);

src/PE/Binary.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,17 @@ std::vector<uint8_t> Binary::get_content_from_virtual_address(uint64_t virtual_a
925925

926926
}
927927

928+
// Overlay
929+
// =======
930+
931+
const std::vector<uint8_t>& Binary::overlay(void) const {
932+
return this->overlay_;
933+
}
934+
935+
std::vector<uint8_t>& Binary::overlay(void) {
936+
return const_cast<std::vector<uint8_t>&>(static_cast<const Binary*>(this)->overlay());
937+
}
938+
928939
// Resource manager
929940
// ===============
930941

src/PE/Builder.cpp

+33-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <algorithm>
1818
#include <fstream>
1919
#include <iterator>
20+
#include <numeric>
2021

2122
#include "easylogging++.h"
2223

@@ -41,7 +42,8 @@ Builder::Builder(Binary* binary) :
4142
patch_imports_{false},
4243
build_relocations_{false},
4344
build_tls_{false},
44-
build_resources_{false}
45+
build_resources_{false},
46+
build_overlay_{true}
4547
{}
4648

4749

@@ -69,7 +71,10 @@ Builder& Builder::build_resources(bool flag) {
6971
return *this;
7072
}
7173

72-
74+
Builder& Builder::build_overlay(bool flag) {
75+
this->build_overlay_ = flag;
76+
return *this;
77+
}
7378

7479

7580
void Builder::write(const std::string& filename) const {
@@ -85,8 +90,6 @@ void Builder::write(const std::string& filename) const {
8590
}
8691

8792

88-
89-
9093
void Builder::build(void) {
9194

9295
LOG(DEBUG) << "Rebuilding" << std::endl;
@@ -151,6 +154,12 @@ void Builder::build(void) {
151154

152155
LOG(DEBUG) << "[+] Rebuilding string table" << std::endl;
153156
//this->build_string_table();
157+
158+
if (this->binary_->overlay().size() > 0 and this->build_overlay_) {
159+
this->build_overlay();
160+
}
161+
162+
154163
}
155164

156165
const std::vector<uint8_t>& Builder::get_build(void) {
@@ -402,6 +411,25 @@ void Builder::build_string_table(void) {
402411
//TODO
403412
}
404413

414+
void Builder::build_overlay(void) {
415+
LOG(DEBUG) << "Building overlay";
416+
417+
const uint64_t last_section_offset = std::accumulate(
418+
std::begin(this->binary_->sections_),
419+
std::end(this->binary_->sections_), 0,
420+
[] (uint64_t offset, const Section* section) {
421+
return std::max<uint64_t>(section->offset() + section->size(), offset);
422+
});
423+
424+
LOG(DEBUG) << "Overlay offset: 0x" << std::hex << last_section_offset;
425+
LOG(DEBUG) << "Overlay size: " << std::dec << this->binary_->overlay().size();
426+
427+
const size_t saved_offset = this->ios_.tellp();
428+
this->ios_.seekp(last_section_offset);
429+
this->ios_.write(this->binary_->overlay());
430+
this->ios_.seekp(saved_offset);
431+
}
432+
405433
Builder& Builder::operator<<(const DosHeader& dos_header) {
406434

407435
pe_dos_header dosHeader;
@@ -519,6 +547,7 @@ std::ostream& operator<<(std::ostream& os, const Builder& b) {
519547
os << std::setw(20) << "Builde relocations:" << b.build_relocations_ << std::endl;
520548
os << std::setw(20) << "Builde TLS:" << b.build_tls_ << std::endl;
521549
os << std::setw(20) << "Builder resources:" << b.build_resources_ << std::endl;
550+
os << std::setw(20) << "Builder overlay:" << b.build_overlay_ << std::endl;
522551
return os;
523552
}
524553

src/PE/Parser.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <iterator>
1818
#include <iostream>
1919
#include <string>
20+
#include <numeric>
2021

2122
#include <mbedtls/platform.h>
2223
#include <mbedtls/oid.h>
@@ -1018,6 +1019,33 @@ void Parser::build_signature(void) {
10181019
this->binary_->has_signature_ = true;
10191020
}
10201021

1022+
1023+
void Parser::build_overlay(void) {
1024+
LOG(DEBUG) << "Parsing Overlay";
1025+
const uint64_t last_section_offset = std::accumulate(
1026+
std::begin(this->binary_->sections_),
1027+
std::end(this->binary_->sections_), 0,
1028+
[] (uint64_t offset, const Section* section) {
1029+
return std::max<uint64_t>(section->offset() + section->size(), offset);
1030+
});
1031+
1032+
LOG(DEBUG) << "Overlay offset: 0x" << std::hex << last_section_offset;
1033+
1034+
const uint64_t overlay_size = this->stream_->size() - last_section_offset;
1035+
1036+
LOG(DEBUG) << "Overlay size: " << std::dec << overlay_size;
1037+
1038+
const uint8_t* ptr_to_overlay = reinterpret_cast<const uint8_t*>(this->stream_->read(
1039+
last_section_offset,
1040+
overlay_size));
1041+
1042+
this->binary_->overlay_ = {
1043+
ptr_to_overlay,
1044+
ptr_to_overlay + overlay_size
1045+
};
1046+
1047+
}
1048+
10211049
//
10221050
// Return the Binary constructed
10231051
//

src/PE/Parser.tcc

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ void Parser::build(void) {
4747
} catch (const corrupted& e) {
4848
LOG(WARNING) << e.what();
4949
}
50+
51+
this->build_overlay();
5052
}
5153

5254
template<typename PE_T>

0 commit comments

Comments
 (0)