File tree Expand file tree Collapse file tree 8 files changed +36
-255
lines changed Expand file tree Collapse file tree 8 files changed +36
-255
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
2725target_link_libraries (chum PRIVATE
2826 Zydis
27+ pe-builder
2928)
Original file line number Diff line number Diff line change 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
1111namespace chum {
1212
@@ -221,7 +221,32 @@ void binary::print(bool const verbose) {
221221
222222// Create a new PE file from this binary.
223223bool 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.
Original file line number Diff line number Diff 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 }
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 22option (ZYDIS_BUILD_EXAMPLES "" OFF )
33option (ZYDIS_BUILD_TOOLS "" OFF )
44add_subdirectory (zydis)
5+
6+ # pe-builder
7+ add_subdirectory (pe-builder)
You can’t perform that action at this time.
0 commit comments