Skip to content

Commit c8cc1ac

Browse files
committed
Added pe-builder submodule
1 parent b290b63 commit c8cc1ac

8 files changed

+36
-255
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "extern/zydis"]
22
path = extern/zydis
33
url = https://github.com/zyantific/zydis
4+
[submodule "extern/pe-builder"]
5+
path = extern/pe-builder
6+
url = https://github.com/jonomango/pe-builder

chum/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ add_executable(chum
1111
"source/symbol.h"
1212
"source/disassembler.h"
1313
"source/disassembler.cpp"
14-
"source/pe-builder.h"
15-
"source/pe-builder.cpp"
1614
"source/util.h"
1715
"source/util.cpp"
1816
)
@@ -26,4 +24,5 @@ target_compile_features(chum PRIVATE
2624
# dependencies
2725
target_link_libraries(chum PRIVATE
2826
Zydis
27+
pe-builder
2928
)

chum/source/binary.cpp

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "binary.h"
2-
#include "pe-builder.h"
32

43
#include <cassert>
54
#include <algorithm>
65
#include <fstream>
76

87
#include <Windows.h>
98
#include <zycore/Format.h>
9+
#include <pe-builder/pe-builder.h>
1010

1111
namespace chum {
1212

@@ -221,7 +221,32 @@ void binary::print(bool const verbose) {
221221

222222
// Create a new PE file from this binary.
223223
bool binary::create(char const* const path) const {
224-
return pe_builder(*this).create(path);
224+
pb::pe_builder pe;
225+
pe.file_characteristics(IMAGE_FILE_DLL);
226+
227+
// We don't want to resize in the middle of adding sections.
228+
if (pe.sections_until_resize() < 1 + data_blocks_.size())
229+
return false;
230+
231+
// Create the .text section for holding code.
232+
auto& text_sec = pe.section()
233+
.name(".text")
234+
.characteristics(IMAGE_SCN_MEM_EXECUTE);
235+
auto& text_sec_data = text_sec.data();
236+
237+
// Write every instruction to the text section (first pass).
238+
for (auto const& bb : basic_blocks_) {
239+
for (auto const& instr : bb->instructions) {
240+
// This is ENTIRELY wrong, but its a good start...
241+
text_sec_data.insert(end(text_sec_data),
242+
instr.bytes, instr.bytes + instr.length);
243+
}
244+
}
245+
246+
// Set the entrypoint to the start of the text section.
247+
pe.entrypoint(pe.virtual_address(text_sec));
248+
249+
return pe.write(path);
225250
}
226251

227252
// Get the entrypoint of this binary, if it exists.

chum/source/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int main() {
6262

6363
bin->print(true);
6464

65-
if (!bin->create("C:\\Users\\realj\\Desktop\\chum-output.exe")) {
65+
if (!bin->create("C:\\Users\\realj\\Desktop\\chum-output.dll")) {
6666
std::printf("[!] Failed to create output binary.\n");
6767
return 0;
6868
}

chum/source/pe-builder.cpp

-203
This file was deleted.

chum/source/pe-builder.h

-47
This file was deleted.

extern/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
option(ZYDIS_BUILD_EXAMPLES "" OFF)
33
option(ZYDIS_BUILD_TOOLS "" OFF)
44
add_subdirectory(zydis)
5+
6+
# pe-builder
7+
add_subdirectory(pe-builder)

extern/pe-builder

Submodule pe-builder added at 92bd662

0 commit comments

Comments
 (0)