LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
PE/Binary.hpp
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_PE_BINARY_H
17#define LIEF_PE_BINARY_H
18
19#include <map>
20
21#include "LIEF/PE/Header.hpp"
22#include "LIEF/PE/OptionalHeader.hpp"
23#include "LIEF/PE/DosHeader.hpp"
24#include "LIEF/PE/Import.hpp"
25#include "LIEF/PE/DelayImport.hpp"
26#include "LIEF/PE/Symbol.hpp"
27#include "LIEF/PE/DataDirectory.hpp"
28#include "LIEF/PE/ResourcesManager.hpp"
29#include "LIEF/PE/signature/Signature.hpp"
30
31#include "LIEF/Abstract/Binary.hpp"
32
33#include "LIEF/visibility.h"
34
35namespace LIEF {
36
38namespace PE {
39class Builder;
40class CodeViewPDB;
41class Debug;
42class Export;
43class LoadConfiguration;
44class Parser;
45class Relocation;
46class ResourceData;
47class ResourceDirectory;
48class ResourceNode;
49class RichHeader;
50class TLS;
51
54class LIEF_API Binary : public LIEF::Binary {
55 friend class Parser;
56 friend class Builder;
57
58 public:
60 using sections_t = std::vector<std::unique_ptr<Section>>;
61
64
67
69 using data_directories_t = std::vector<std::unique_ptr<DataDirectory>>;
70
73
76
78 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
79
82
85
87 using imports_t = std::vector<Import>;
88
91
94
96 using delay_imports_t = std::vector<DelayImport>;
97
100
103
105 using debug_entries_t = std::vector<std::unique_ptr<Debug>>;
106
109
112
114 using symbols_t = std::vector<Symbol>;
115
118
121
123 using strings_table_t = std::vector<std::string>;
124
127
130
132 using signatures_t = std::vector<Signature>;
133
136
139
140 Binary(PE_TYPE type);
141
142 ~Binary() override;
143
145 PE_TYPE type() const {
146 return type_;
147 }
148
153 uint64_t rva_to_offset(uint64_t RVA);
154
157 uint64_t va_to_offset(uint64_t VA);
158
163 result<uint64_t> offset_to_virtual_address(uint64_t offset, uint64_t slide = 0) const override;
164
168 uint64_t imagebase() const override {
169 return optional_header().imagebase();
170 }
171
175 Section* section_from_offset(uint64_t offset);
176 const Section* section_from_offset(uint64_t offset) const;
177
181 Section* section_from_rva(uint64_t virtual_address);
182 const Section* section_from_rva(uint64_t virtual_address) const;
183
186 return sections_;
187 }
188
189 it_const_sections sections() const {
190 return sections_;
191 }
192
195 return dos_header_;
196 }
197
198 const DosHeader& dos_header() const {
199 return dos_header_;
200 }
201
204 return header_;
205 }
206
207 const Header& header() const {
208 return header_;
209 }
210
213 return optional_header_;
214 }
215
216 const OptionalHeader& optional_header() const {
217 return optional_header_;
218 }
219
222 uint64_t virtual_size() const;
223
225 uint32_t sizeof_headers() const;
226
228 TLS* tls() {
229 return tls_.get();
230 }
231
232 const TLS* tls() const {
233 return tls_.get();
234 }
235
237 void tls(const TLS& tls);
238
240 bool has_tls() const {
241 return tls_ != nullptr;
242 }
243
247 bool has_imports() const {
248 return !imports_.empty();
249 }
250
254 bool has_signatures() const {
255 return !signatures_.empty();
256 }
257
261 bool has_exports() const {
262 return export_ != nullptr;
263 }
264
266 bool has_resources() const {
267 return resources_ != nullptr;
268 }
269
271 bool has_exceptions() const;
272
276 bool has_relocations() const {
277 return !relocations_.empty();
278 }
279
281 bool has_debug() const {
282 return !debug_.empty();
283 }
284
286 bool has_configuration() const {
287 return load_configuration_ != nullptr;
288 }
289
294
297 return signatures_;
298 }
299
300 it_signatures signatures() {
301 return signatures_;
302 }
303
312 Signature::VERIFICATION_CHECKS checks = Signature::VERIFICATION_CHECKS::DEFAULT) const;
313
324 Signature::VERIFICATION_CHECKS checks = Signature::VERIFICATION_CHECKS::DEFAULT) const;
325
328 std::vector<uint8_t> authentihash(ALGORITHMS algo) const;
329
342 uint32_t predict_function_rva(const std::string& library, const std::string& function);
343
346 return export_.get();
347 }
348
349 const Export* get_export() const {
350 return export_.get();
351 }
352
354 std::vector<Symbol>& symbols();
355 const std::vector<Symbol>& symbols() const;
356
359 return resources_.get();
360 }
361
362 const ResourceNode* resources() const {
363 return resources_.get();
364 }
365
367 void set_resources(const ResourceDirectory& resource);
368
370 void set_resources(const ResourceData& resource);
371
374
379 Section* get_section(const std::string& name);
380 const Section* get_section(const std::string& name) const;
381
384 const Section* import_section() const;
385 Section* import_section();
386
392 void remove_section(const std::string& name, bool clear = false) override;
393
397 void remove(const Section& section, bool clear = false);
398
400 Section* add_section(const Section& section,
401 PE_SECTION_TYPES type = PE_SECTION_TYPES::UNKNOWN);
402
405 return relocations_;
406 }
407
408 it_const_relocations relocations() const {
409 return relocations_;
410 }
411
414
417
420 return data_directories_;
421 }
422
423 it_const_data_directories data_directories() const {
424 return data_directories_;
425 }
426
428 DataDirectory* data_directory(DataDirectory::TYPES type);
429 const DataDirectory* data_directory(DataDirectory::TYPES type) const;
430
432 bool has(DataDirectory::TYPES type) const {
433 return data_directory(type) != nullptr;
434 }
435
438 return debug_;
439 }
440
441 it_const_debug_entries debug() const {
442 return debug_;
443 }
444
446 const CodeViewPDB* codeview_pdb() const;
447
451 return load_configuration_.get();
452 }
453
454 LoadConfiguration* load_configuration() {
455 return load_configuration_.get();
456 }
457
459 span<const uint8_t> overlay() const {
460 return overlay_;
461 }
462
463 span<uint8_t> overlay() {
464 return overlay_;
465 }
466
468 uint64_t overlay_offset() const {
469 return overlay_offset_;
470 }
471
473 span<const uint8_t> dos_stub() const {
474 return dos_stub_;
475 }
476
477 span<uint8_t> dos_stub() {
478 return dos_stub_;
479 }
480
482 void dos_stub(const std::vector<uint8_t>& content);
483
484 // Rich Header
485 // -----------
486
489 return rich_header_.get();
490 }
491
492 const RichHeader* rich_header() const {
493 return rich_header_.get();
494 }
495
497 void rich_header(const RichHeader& rich_header);
498
500 bool has_rich_header() const {
501 return rich_header_ != nullptr;
502 }
503
506 return imports_;
507 }
508
509 it_const_imports imports() const {
510 return imports_;
511 }
512
517 Import* get_import(const std::string& import_name);
518 const Import* get_import(const std::string& import_name) const;
519
523 bool has_import(const std::string& import_name) const {
524 return get_import(import_name) != nullptr;
525 }
526
531 bool has_delay_imports() const {
532 return !delay_imports_.empty();
533 }
534
537 return delay_imports_;
538 }
539
540 it_const_delay_imports delay_imports() const {
541 return delay_imports_;
542 }
543
548 DelayImport* get_delay_import(const std::string& import_name);
549 const DelayImport* get_delay_import(const std::string& import_name) const;
550
551
555 bool has_delay_import(const std::string& import_name) const {
556 return get_delay_import(import_name) != nullptr;
557 }
558
559
565 ImportEntry* add_import_function(const std::string& library, const std::string& function);
566
568 Import& add_library(const std::string& name);
569
571 void remove_library(const std::string& name);
572
575
580 void write(const std::string& filename) override;
581
586 void write(std::ostream& os) override;
587
588 void accept(Visitor& visitor) const override;
589
595 void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
597
598
605 void patch_address(uint64_t address, uint64_t patch_value, size_t size = sizeof(uint64_t),
607
614 uint64_t virtual_address, uint64_t size,
615 Binary::VA_TYPES addr_type = Binary::VA_TYPES::AUTO) const override;
616
618 uint64_t entrypoint() const override;
619
621 bool is_pie() const override;
622
624 bool has_nx() const override;
625
629 LIEF::Binary::functions_t ctor_functions() const override;
630
632 LIEF::Binary::functions_t functions() const;
633
635 LIEF::Binary::functions_t exception_functions() const;
636
637 static bool classof(const LIEF::Binary* bin) {
638 return bin->format() == Binary::FORMATS::PE;
639 }
640
641 std::ostream& print(std::ostream& os) const override;
642
643 private:
644 Binary();
645
648 void make_space_for_new_section();
649
651 LIEF::Binary::symbols_t get_abstract_symbols() override;
652
653 LIEF::Header get_abstract_header() const override;
654
656 LIEF::Binary::sections_t get_abstract_sections() override;
657
658 LIEF::Binary::relocations_t get_abstract_relocations() override;
659
660 LIEF::Binary::functions_t get_abstract_exported_functions() const override;
661 LIEF::Binary::functions_t get_abstract_imported_functions() const override;
662 std::vector<std::string> get_abstract_imported_libraries() const override;
663
664 void update_lookup_address_table_offset();
665 void update_iat();
666
667 PE_TYPE type_ = PE_TYPE::PE32_PLUS;
668 DosHeader dos_header_;
669 Header header_;
670 OptionalHeader optional_header_;
671
672 int32_t available_sections_space_ = 0;
673
674 signatures_t signatures_;
675 sections_t sections_;
676 data_directories_t data_directories_;
677 symbols_t symbols_;
678 strings_table_t strings_table_;
679 relocations_t relocations_;
680 imports_t imports_;
681 delay_imports_t delay_imports_;
682 debug_entries_t debug_;
683 uint64_t overlay_offset_ = 0;
684 std::vector<uint8_t> overlay_;
685 std::vector<uint8_t> dos_stub_;
686 std::vector<uint8_t> section_offset_padding_;
687
688 std::unique_ptr<RichHeader> rich_header_;
689 std::unique_ptr<Export> export_;
690 std::unique_ptr<ResourceNode> resources_;
691 std::unique_ptr<TLS> tls_;
692 std::unique_ptr<LoadConfiguration> load_configuration_;
693};
694
695}
696}
697#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:39
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:98
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:69
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:43
@ AUTO
Try to guess if it's relative or not.
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:60
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:78
Definition Abstract/Header.hpp:29
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:54
OptionalHeader & optional_header()
Return a reference to the OptionalHeader object.
Definition PE/Binary.hpp:212
void remove_all_relocations()
Remove all the relocations.
std::vector< DelayImport > delay_imports_t
Internal container for storing PE's DelayImport.
Definition PE/Binary.hpp:96
std::vector< uint8_t > authentihash(ALGORITHMS algo) const
Compute the authentihash according to the algorithm provided in the first parameter.
const LoadConfiguration * load_configuration() const
Retrun the LoadConfiguration object or a nullptr if the binary does not use the LoadConfiguration.
Definition PE/Binary.hpp:450
void remove_section(const std::string &name, bool clear=false) override
Delete the section with the given name.
bool is_pie() const override
Check if the binary is position independent.
LIEF::Binary::functions_t ctor_functions() const override
Return the list of the binary constructors.
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing PE's Section.
Definition PE/Binary.hpp:60
void set_resources(const ResourceDirectory &resource)
Set a new resource tree.
uint64_t va_to_offset(uint64_t VA)
Convert the absolute virtual address into an offset.
it_imports imports()
Return an iterator over the binary imports.
Definition PE/Binary.hpp:505
bool has_resources() const
Check if the current binary has resources.
Definition PE/Binary.hpp:266
uint32_t predict_function_rva(const std::string &library, const std::string &function)
Try to predict the RVA of the function function in the import library library
std::vector< Symbol > & symbols()
Return binary Symbols.
bool has_debug() const
Check if the current binary contains debug information.
Definition PE/Binary.hpp:281
DelayImport * get_delay_import(const std::string &import_name)
Returns the PE::DelayImport from the given name. If it can't be found, return a nullptr.
bool has_relocations() const
Check if the current binary has relocations.
Definition PE/Binary.hpp:276
ImportEntry * add_import_function(const std::string &library, const std::string &function)
Add the function function of the library library. If the function fails, it returns a nullptr.
bool has_exports() const
Check if the current binary has exports.
Definition PE/Binary.hpp:261
uint64_t virtual_size() const
Compute the binary's virtual size. It should match OptionalHeader::sizeof_image.
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing PE's Relocation.
Definition PE/Binary.hpp:78
result< ResourcesManager > resources_manager() const
Return the ResourcesManager (class to manage resources more easily than the tree one)
result< uint64_t > offset_to_virtual_address(uint64_t offset, uint64_t slide=0) const override
Convert the given offset into a virtual address.
void patch_address(uint64_t address, uint64_t patch_value, size_t size=sizeof(uint64_t), LIEF::Binary::VA_TYPES addr_type=LIEF::Binary::VA_TYPES::AUTO) override
Patch the address with the given value.
Signature::VERIFICATION_FLAGS verify_signature(const Signature &sig, Signature::VERIFICATION_CHECKS checks=Signature::VERIFICATION_CHECKS::DEFAULT) const
Verify the binary with the Signature object provided in the first parameter It can be used to verify ...
span< const uint8_t > overlay() const
Return the overlay content.
Definition PE/Binary.hpp:459
span< const uint8_t > get_content_from_virtual_address(uint64_t virtual_address, uint64_t size, Binary::VA_TYPES addr_type=Binary::VA_TYPES::AUTO) const override
Return the content located at the provided virtual address.
std::vector< Symbol > symbols_t
Internal container for storing COFF Symbols.
Definition PE/Binary.hpp:114
Section * add_section(const Section &section, PE_SECTION_TYPES type=PE_SECTION_TYPES::UNKNOWN)
Add a section to the binary and return the section added.
bool has_rich_header() const
Check if the current binary has a RichHeader object.
Definition PE/Binary.hpp:500
bool has_signatures() const
Check if the current binary contains signatures.
Definition PE/Binary.hpp:254
bool has_delay_imports() const
Check if the current binary contains delay imports.
Definition PE/Binary.hpp:531
void set_resources(const ResourceData &resource)
Set a new resource tree.
void write(std::ostream &os) override
Reconstruct the binary object and write the raw PE in os stream.
void rich_header(const RichHeader &rich_header)
Set a RichHeader object in the current Binary.
TLS * tls()
Return a reference to the TLS object.
Definition PE/Binary.hpp:228
uint64_t entrypoint() const override
Return the binary's entrypoint (It is the same value as OptionalHeader::addressof_entrypoint.
std::vector< Signature > signatures_t
Internal container for storing PE's authenticode Signature.
Definition PE/Binary.hpp:132
Signature::VERIFICATION_FLAGS verify_signature(Signature::VERIFICATION_CHECKS checks=Signature::VERIFICATION_CHECKS::DEFAULT) const
Verify the binary against the embedded signature(s) (if any) First, it checks that the embedded signa...
void remove_all_libraries()
Remove all libraries in the binary.
void accept(Visitor &visitor) const override
Method so that a visitor can visit us.
LIEF::Binary::functions_t functions() const
All functions found in the binary
LIEF::Binary::functions_t exception_functions() const
Functions found in the Exception table directory.
Import & add_library(const std::string &name)
Add an imported library (i.e. DLL) to the binary.
uint64_t overlay_offset() const
Return the original overlay offset.
Definition PE/Binary.hpp:468
Section * section_from_rva(uint64_t virtual_address)
Find the section associated that encompasses the given RVA.
const Section * import_section() const
Return the section associated with import table or a nullptr if the binary does not have an import ta...
PE_TYPE type() const
Return PE32 or PE32+
Definition PE/Binary.hpp:145
bool has_tls() const
Check if the current binary has a TLS object.
Definition PE/Binary.hpp:240
DataDirectory * data_directory(DataDirectory::TYPES type)
Return the DataDirectory with the given type (or index)
void remove_library(const std::string &name)
Remove the library with the given name
Section * section_from_offset(uint64_t offset)
Find the section associated that encompasses the given offset.
bool has_exceptions() const
Check if the current binary has exceptions.
it_delay_imports delay_imports()
Return an iterator over the binary's delay imports.
Definition PE/Binary.hpp:536
bool is_reproducible_build() const
Check if the current binary is reproducible build, replacing timestamps by a compile hash.
std::vector< std::unique_ptr< DataDirectory > > data_directories_t
Internal container for storing PE's DataDirectory.
Definition PE/Binary.hpp:69
DosHeader & dos_header()
Return a reference to the PE::DosHeader object.
Definition PE/Binary.hpp:194
std::vector< Import > imports_t
Internal container for storing PE's Import.
Definition PE/Binary.hpp:87
bool has_nx() const override
Check if the binary uses NX protection.
span< const uint8_t > dos_stub() const
Return the DOS stub content.
Definition PE/Binary.hpp:473
Header & header()
Return a reference to the PE::Header object.
Definition PE/Binary.hpp:203
it_relocations relocations()
Return an iterator over the PE's Relocation.
Definition PE/Binary.hpp:404
uint64_t imagebase() const override
Return binary's imagebase. 0 if not relevant.
Definition PE/Binary.hpp:168
bool has_configuration() const
Check if the current binary has a load configuration.
Definition PE/Binary.hpp:286
const CodeViewPDB * codeview_pdb() const
Return the CodeViewPDB object if present.
ResourceNode * resources()
Return resources as a tree or a nullptr if there is no resources.
Definition PE/Binary.hpp:358
bool has(DataDirectory::TYPES type) const
Check if the current binary has the given DataDirectory::TYPES.
Definition PE/Binary.hpp:432
uint32_t sizeof_headers() const
Compute the size of all the headers.
void tls(const TLS &tls)
Set a TLS object in the current Binary.
std::vector< std::string > strings_table_t
Internal container for storing strings.
Definition PE/Binary.hpp:123
std::vector< std::unique_ptr< Debug > > debug_entries_t
Internal container for storing Debug information.
Definition PE/Binary.hpp:105
Import * get_import(const std::string &import_name)
Returns the PE::Import from the given name. If it can't be found, return a nullptr.
it_const_signatures signatures() const
Return an iterator over the Signature object(s) if the binary is signed.
Definition PE/Binary.hpp:296
it_debug_entries debug()
Return an iterator over the Debug entries.
Definition PE/Binary.hpp:437
void patch_address(uint64_t address, const std::vector< uint8_t > &patch_value, LIEF::Binary::VA_TYPES addr_type=LIEF::Binary::VA_TYPES::AUTO) override
Patch the content at virtual address address with patch_value.
void dos_stub(const std::vector< uint8_t > &content)
Update the DOS stub content.
bool has_delay_import(const std::string &import_name) const
True if the binary delay-imports the given library name
Definition PE/Binary.hpp:555
bool has_imports() const
Check if the current binary contains imports.
Definition PE/Binary.hpp:247
void remove(const Section &section, bool clear=false)
Remove the given section.
it_data_directories data_directories()
Return an iterator over the DataDirectory present in the Binary.
Definition PE/Binary.hpp:419
it_sections sections()
Return an iterator over the PE's Section.
Definition PE/Binary.hpp:185
Export * get_export()
Return the Export object.
Definition PE/Binary.hpp:345
void write(const std::string &filename) override
Reconstruct the binary object and write the raw PE in filename
bool has_import(const std::string &import_name) const
True if the binary imports the given library name
Definition PE/Binary.hpp:523
Relocation & add_relocation(const Relocation &relocation)
Add a PE::Relocation.
Section * get_section(const std::string &name)
Return binary's section from its name. If the secion can't be found, return a nullptr.
RichHeader * rich_header()
Return a reference to the RichHeader object.
Definition PE/Binary.hpp:488
uint64_t rva_to_offset(uint64_t RVA)
Convert a Relative Virtual Address into an offset.
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:45
CodeView PDB specialization.
Definition CodeViewPDB.hpp:34
Class that represents a PE data directory entry.
Definition DataDirectory.hpp:38
Class that represents a PE delayed import.
Definition DelayImport.hpp:37
Class which represents the DosHeader, the first structure presents at the beginning of a PE file.
Definition DosHeader.hpp:38
Class which represents a PE Export.
Definition Export.hpp:38
Class that represents the PE header (which follows the DosHeader)
Definition PE/Header.hpp:36
Class that represents an entry (i.e. an import) in the import table (Import).
Definition ImportEntry.hpp:36
Class that represents a PE import.
Definition Import.hpp:40
Class that represents the default PE's LoadConfiguration
Definition LoadConfiguration.hpp:35
Class which represents the PE OptionalHeader structure.
Definition OptionalHeader.hpp:38
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:47
Class which represents the Base Relocation Block We usually find this structure in the ....
Definition PE/Relocation.hpp:37
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:32
Definition ResourceDirectory.hpp:37
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:38
Class which represents the not-so-documented rich header.
Definition RichHeader.hpp:38
Class which represents a PE section.
Definition PE/Section.hpp:41
Main interface for the PKCS #7 signature scheme.
Definition Signature.hpp:40
VERIFICATION_CHECKS
Flags to tweak the verification process of the signature.
Definition Signature.hpp:91
VERIFICATION_FLAGS
Flags returned by the verification functions.
Definition Signature.hpp:68
Class which represents the PE Thread Local Storage.
Definition TLS.hpp:45
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
PE_SECTION_TYPES
Common section type.
Definition PE/enums.hpp:666
ALGORITHMS
Cryptography algorithms.
Definition PE/enums.hpp:686
PE_TYPE
Definition PE/enums.hpp:680
LIEF namespace.
Definition Abstract/Binary.hpp:32
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:72