LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
PE/Builder.hpp
Go to the documentation of this file.
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_BUILDER_H
17#define LIEF_PE_BUILDER_H
18
19#include <cstring>
20#include <string>
21#include <vector>
22#include <iterator>
23#include <ostream>
24#include <sstream>
25#include <algorithm>
26#include <iomanip>
27
28#include "LIEF/visibility.h"
29#include "LIEF/utils.hpp"
30#include "LIEF/iostream.hpp"
31
32#include "LIEF/errors.hpp"
33
34namespace LIEF {
35namespace PE {
36class Binary;
37class ResourceNode;
38class DosHeader;
39class Header;
40class OptionalHeader;
41class DataDirectory;
42class Section;
43class LIEF_API Builder {
46 public:
47
48 Builder() = delete;
49 Builder(Binary& binary);
54 template<typename PE_T>
59 static std::vector<uint8_t> build_jmp(uint64_t from, uint64_t address);
60
61 template<typename PE_T>
66 static std::vector<uint8_t> build_jmp_hook(uint64_t from, uint64_t address);
67 Builder& build_imports(bool flag = true);
70 Builder& patch_imports(bool flag = true);
76 Builder& build_relocations(bool flag = true);
79 Builder& build_tls(bool flag = true);
82 Builder& build_resources(bool flag);
85 Builder& build_overlay(bool flag);
88 Builder& build_dos_stub(bool flag);
91 const std::vector<uint8_t>& get_build();
94 void write(const std::string& filename) const;
97 void write(std::ostream& os) const;
100
101 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Builder& b);
102
103 ok_error_t build(const DosHeader& dos_header);
104 ok_error_t build(const Header& bHeader);
105 ok_error_t build(const OptionalHeader& optional_header);
106 ok_error_t build(const DataDirectory& data_directory);
107 ok_error_t build(const Section& section);
108
109 protected:
110 template<typename PE_T>
111 ok_error_t build_optional_header(const OptionalHeader& optional_header);
112 // TODO: Bug with x86
115 template<typename PE_T>
116 void build_import_table();
117
118 template<typename PE_T>
119 ok_error_t build_tls();
120
121 ok_error_t build_relocation();
122 ok_error_t build_resources();
123 ok_error_t build_overlay();
124 ok_error_t build_dos_stub();
125
126 ok_error_t compute_resources_size(ResourceNode& node, uint32_t *header_size,
127 uint32_t *data_size, uint32_t *name_size);
128
129 ok_error_t construct_resources(ResourceNode& node, std::vector<uint8_t>* content,
130 uint32_t* offset_header, uint32_t* offset_data, uint32_t* offset_name,
131 uint32_t base_rva, uint32_t depth);
132
133
134 mutable vector_iostream ios_;
135 Binary* binary_ = nullptr;
136
137 bool build_imports_ = false;
138 bool patch_imports_ = false;
139 bool build_relocations_ = false;
140 bool build_tls_ = false;
141 bool build_resources_ = false;
142 bool build_overlay_ = true;
143 bool build_dos_stub_ = true;
144
145};
146
147}
148}
149#endif
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:52
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:45
friend std::ostream & operator<<(std::ostream &os, const Builder &b)
Builder & build_overlay(bool flag)
Rebuild the binary's overlay.
Builder(Binary &binary)
Builder & build_dos_stub(bool flag)
Rebuild the DOS stub content.
static std::vector< uint8_t > build_jmp_hook(uint64_t from, uint64_t address)
Construct a jmp far address @ from.
Builder & build_imports(bool flag=true)
Rebuild the import table in new section.
ok_error_t build(const DataDirectory &data_directory)
ok_error_t build(const Section &section)
ok_error_t build(const OptionalHeader &optional_header)
ok_error_t build()
Perform the build process.
static std::vector< uint8_t > build_jmp(uint64_t from, uint64_t address)
Construct a jmp [address] @ from.
void write(std::ostream &os) const
Write the build result into the os stream.
void write(const std::string &filename) const
Write the build result into the output file.
ok_error_t build(const Header &bHeader)
ok_error_t build(const DosHeader &dos_header)
Builder & build_resources(bool flag)
Rebuid the resources in another section.
Builder & build_tls(bool flag=true)
Rebuild TLS object in another section.
Builder & build_relocations(bool flag=true)
Rebuild the relocation table in another section.
const std::vector< uint8_t > & get_build()
Return the build result.
Builder & patch_imports(bool flag=true)
Patch the original import table in order to redirect functions to the new import table.
Class that represents a PE data directory entry.
Definition DataDirectory.hpp:38
Class which represents the DosHeader, the first structure presents at the beginning of a PE file.
Definition DosHeader.hpp:38
Class that represents the PE header (which follows the DosHeader)
Definition PE/Header.hpp:36
Class which represents the PE OptionalHeader structure.
Definition OptionalHeader.hpp:42
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:36
Class which represents a PE section.
Definition PE/Section.hpp:43
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
#define LIEF_API
Definition visibility.h:41