Skip to content

Commit 3f06397

Browse files
committed
Enable DOS stub rebuilding
Python API: * lief.PE.Binary.dos_stub property * lief.PE.Builder.build_dos_stub method C++ API: * LIEF::PE::Binary::dos_stub setter/getter * LIEF::PE::Builder::build_dos_stub method
1 parent 95bc670 commit 3f06397

File tree

9 files changed

+85
-8
lines changed

9 files changed

+85
-8
lines changed

api/python/PE/objects/pyBinary.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ using no_const_func = T (Binary::*)(P);
2727
template<class T>
2828
using no_const_getter = T (Binary::*)(void);
2929

30+
template<class T>
31+
using getter_t = T (Binary::*)(void) const;
32+
33+
template<class T>
34+
using setter_t = void (Binary::*)(T);
35+
3036
void init_PE_Binary_class(py::module& m) {
3137
py::class_<Binary, LIEF::Binary>(m, "Binary")
3238
.def(py::init<const std::string &, PE_TYPE>())
@@ -204,6 +210,11 @@ void init_PE_Binary_class(py::module& m) {
204210
"Return the overlay content",
205211
py::return_value_policy::reference)
206212

213+
.def_property("dos_stub",
214+
static_cast<getter_t<const std::vector<uint8_t>&>>(&Binary::dos_stub),
215+
static_cast<setter_t<const std::vector<uint8_t>&>>(&Binary::dos_stub),
216+
"DOS stub content")
217+
207218
.def("add_import_function",
208219
&Binary::add_import_function,
209220
"Add a function to the given " RST_CLASS_REF(lief.PE.Import) " name",

api/python/PE/objects/pyBuilder.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ void init_PE_Builder_class(py::module& m) {
6363
"Rebuild the binary's overlay",
6464
py::return_value_policy::reference)
6565

66+
.def("build_dos_stub",
67+
static_cast<Builder& (Builder::*)(bool)>(&Builder::build_dos_stub),
68+
"Rebuild the DOS stub",
69+
py::return_value_policy::reference)
70+
6671
.def("write",
6772
&Builder::write,
6873
"Write the build result into the ``output`` file",

include/LIEF/PE/Binary.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ class DLL_PUBLIC Binary : public LIEF::Binary {
240240
const std::vector<uint8_t>& overlay(void) const;
241241
std::vector<uint8_t>& overlay(void);
242242

243+
// ========
244+
// DOS Stub
245+
// ========
246+
247+
//! @brief Return the DOS stub content
248+
const std::vector<uint8_t>& dos_stub(void) const;
249+
std::vector<uint8_t>& dos_stub(void);
250+
251+
//! @brief Update the DOS stub content
252+
void dos_stub(const std::vector<uint8_t>& content);
243253

244254
// =========================
245255
// Methods to manage Imports
@@ -377,6 +387,7 @@ class DLL_PUBLIC Binary : public LIEF::Binary {
377387
Export export_;
378388
Debug debug_;
379389
std::vector<uint8_t> overlay_;
390+
std::vector<uint8_t> dos_stub_;
380391

381392
std::map<std::string, std::map<std::string, uint64_t>> hooks_;
382393
};

include/LIEF/PE/Builder.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class DLL_PUBLIC Builder
8181
//! @brief Rebuild the binary's overlay
8282
Builder& build_overlay(bool flag);
8383

84+
//! @brief Rebuild the DOS stub content
85+
Builder& build_dos_stub(bool flag);
86+
8487
//! @brief Return the build result
8588
const std::vector<uint8_t>& get_build(void);
8689

@@ -113,6 +116,7 @@ class DLL_PUBLIC Builder
113116
void build_relocation(void);
114117
void build_resources(void);
115118
void build_overlay(void);
119+
void build_dos_stub(void);
116120

117121
void compute_resources_size(
118122
ResourceNode *node,
@@ -139,6 +143,7 @@ class DLL_PUBLIC Builder
139143
bool build_tls_;
140144
bool build_resources_;
141145
bool build_overlay_;
146+
bool build_dos_stub_;
142147

143148
};
144149

include/LIEF/PE/Parser.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ class DLL_PUBLIC Parser : public LIEF::Parser {
8484
void build_symbols(void);
8585
void build_signature(void);
8686
void build_overlay(void);
87+
void build_dos_stub(void);
8788

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

9192

9293
std::unique_ptr<VectorStream> stream_;
9394
Binary* binary_;
94-
PE_TYPE type_;
95+
PE_TYPE type_;
9596
};
9697

9798

src/PE/Binary.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,22 @@ std::vector<uint8_t>& Binary::overlay(void) {
994994
return const_cast<std::vector<uint8_t>&>(static_cast<const Binary*>(this)->overlay());
995995
}
996996

997+
// Dos stub
998+
// ========
999+
1000+
const std::vector<uint8_t>& Binary::dos_stub(void) const {
1001+
return this->dos_stub_;
1002+
}
1003+
1004+
std::vector<uint8_t>& Binary::dos_stub(void) {
1005+
return const_cast<std::vector<uint8_t>&>(static_cast<const Binary*>(this)->dos_stub());
1006+
}
1007+
1008+
1009+
void Binary::dos_stub(const std::vector<uint8_t>& content) {
1010+
this->dos_stub_ = content;
1011+
}
1012+
9971013
// Resource manager
9981014
// ===============
9991015

src/PE/Builder.cpp

+22-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Builder::Builder(Binary* binary) :
4343
build_relocations_{false},
4444
build_tls_{false},
4545
build_resources_{false},
46-
build_overlay_{true}
46+
build_overlay_{true},
47+
build_dos_stub_{true}
4748
{}
4849

4950

@@ -76,6 +77,11 @@ Builder& Builder::build_overlay(bool flag) {
7677
return *this;
7778
}
7879

80+
Builder& Builder::build_dos_stub(bool flag) {
81+
this->build_dos_stub_ = flag;
82+
return *this;
83+
}
84+
7985

8086
void Builder::write(const std::string& filename) const {
8187
std::ofstream output_file{filename, std::ios::out | std::ios::binary | std::ios::trunc};
@@ -459,6 +465,14 @@ Builder& Builder::operator<<(const DosHeader& dos_header) {
459465

460466
this->ios_.seekp(0);
461467
this->ios_.write(reinterpret_cast<const uint8_t*>(&dosHeader), sizeof(pe_dos_header));
468+
if (this->binary_->dos_stub().size() > 0 and this->build_dos_stub_) {
469+
470+
if (sizeof(pe_dos_header) + this->binary_->dos_stub().size() > dos_header.addressof_new_exeheader()) {
471+
LOG(WARNING) << "Inconsistent 'addressof_new_exeheader' (0x" << std::hex << dos_header.addressof_new_exeheader();
472+
}
473+
this->ios_.write(this->binary_->dos_stub());
474+
}
475+
462476
return *this;
463477
}
464478

@@ -542,12 +556,13 @@ Builder& Builder::operator<<(const Section& section) {
542556
std::ostream& operator<<(std::ostream& os, const Builder& b) {
543557
os << std::left;
544558
os << std::boolalpha;
545-
os << std::setw(20) << "Builde imports:" << b.build_imports_ << std::endl;
546-
os << std::setw(20) << "Patch imports:" << b.patch_imports_ << std::endl;
547-
os << std::setw(20) << "Builde relocations:" << b.build_relocations_ << std::endl;
548-
os << std::setw(20) << "Builde TLS:" << b.build_tls_ << std::endl;
549-
os << std::setw(20) << "Builder resources:" << b.build_resources_ << std::endl;
550-
os << std::setw(20) << "Builder overlay:" << b.build_overlay_ << std::endl;
559+
os << std::setw(20) << "Build imports:" << b.build_imports_ << std::endl;
560+
os << std::setw(20) << "Patch imports:" << b.patch_imports_ << std::endl;
561+
os << std::setw(20) << "Build relocations:" << b.build_relocations_ << std::endl;
562+
os << std::setw(20) << "Build TLS:" << b.build_tls_ << std::endl;
563+
os << std::setw(20) << "Build resources:" << b.build_resources_ << std::endl;
564+
os << std::setw(20) << "Build overlay:" << b.build_overlay_ << std::endl;
565+
os << std::setw(20) << "Build dos stub:" << b.build_dos_stub_ << std::endl;
551566
return os;
552567
}
553568

src/PE/Parser.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ void Parser::init(const std::string& name) {
8383

8484
}
8585

86+
void Parser::build_dos_stub(void) {
87+
const DosHeader& dos_header = this->binary_->dos_header();
88+
const uint64_t sizeof_dos_stub = dos_header.addressof_new_exeheader() - sizeof(pe_dos_header);
89+
90+
const uint8_t* ptr_to_dos_stub = reinterpret_cast<const uint8_t*>(this->stream_->read(
91+
sizeof(pe_dos_header),
92+
sizeof_dos_stub));
93+
this->binary_->dos_stub_ = {ptr_to_dos_stub, ptr_to_dos_stub + sizeof_dos_stub};
94+
}
8695

8796

8897

src/PE/Parser.tcc

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ void Parser::build(void) {
2727
LOG(WARNING) << e.what();
2828
}
2929

30+
LOG(DEBUG) << "[+] Retreive Dos stub";
31+
32+
this->build_dos_stub();
33+
3034
LOG(DEBUG) << "[+] Decomposing Sections";
3135

3236
try {

0 commit comments

Comments
 (0)