-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (83 loc) · 2.75 KB
/
main.cpp
File metadata and controls
103 lines (83 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cassert>
#include <iostream>
#include <zasm/modules/module.hpp>
#include <zasm/x86/x86.hpp>
static zasm::Error createTestProgram64(zasm::Program& program)
{
using namespace zasm;
// Imports.
auto labelImpMessageBoxA = program.getOrCreateImportLabel("user32.dll", "MessageBoxA");
auto labelImpExitProcess = program.getOrCreateImportLabel("kernel32.dll", "ExitProcess");
// Labels.
auto labelMain = program.createLabel("main");
auto labelStrMsgTitle = program.createLabel("str_MsgTitle");
auto labelStrMsg = program.createLabel("str_Msg");
x86::Assembler a(program);
// .text
a.section(".text", Section::Attribs::Code | Section::Attribs::Read | Section::Attribs::Exec);
{
// main:
a.bind(labelMain);
{
// .stack_alloc
a.sub(x86::rsp, Imm(40));
// invoke user32.MessageBoxA, NULL, labelStrMsgTitle, labelStrMsg, MB_OK
a.mov(x86::rcx, Imm(0)); // HWND hWnd
a.lea(x86::rdx, x86::qword_ptr(labelStrMsg)); // LPCTSTR lpText
a.lea(x86::r8, x86::qword_ptr(labelStrMsgTitle)); // LPCTSTR lpCaption,
a.mov(x86::r9, Imm(0)); // MB_OK
a.call(x86::qword_ptr(labelImpMessageBoxA));
// invoke kernel32.ExitProcess
a.xor_(x86::rcx, x86::rcx);
a.call(x86::qword_ptr(labelImpExitProcess));
// .stack_free
a.add(x86::rsp, Imm(40));
a.ret();
}
}
// .rdata
a.section(".rdata", Section::Attribs::RData | Section::Attribs::Read);
{
// str_MsgTitle:
a.bind(labelStrMsgTitle);
a.embed("MessageBox Title");
// str_Msg:
a.bind(labelStrMsg);
a.embed("Hello World, this program was generated by zasm and LIEF.");
}
// Specify entrypoint.
program.setEntryPoint(labelMain);
return Error::None;
}
static zasm::Error buildTestPE64()
{
using namespace zasm;
// Generate a new program.
Program program(zasm::MachineMode::AMD64);
if (auto err = createTestProgram64(program); err != Error::None)
{
return err;
}
// Create a new module.
auto pe64Mod = modules::createModule(modules::ModuleType::PE, program, "test_pe64");
assert(pe64Mod != nullptr);
if (auto err = pe64Mod->serialize(); err != Error::None)
{
return err;
}
std::filesystem::path outputPath("test_pe64.exe");
if (auto err = pe64Mod->save(outputPath); err != Error::None)
{
return err;
}
return Error::None;
}
int main()
{
if (auto err = buildTestPE64(); err != zasm::Error::None)
{
std::cout << "Failed to create PE64\n";
return -1;
}
return 0;
}