-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cc
More file actions
99 lines (81 loc) · 2.07 KB
/
main.cc
File metadata and controls
99 lines (81 loc) · 2.07 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
#include <iostream>
#include <getopt.h>
#include "elf_file.hh"
struct usage_exception : public std::exception {
usage_exception() { }
};
struct protector_cfg {
std::string in_fname;
std::string out_fname;
};
static void
usage()
{
std::cout << std::endl;
std::cout << "Usage: ./elfprotect [OPTIONS]" << std::endl;
std::cout << std::endl;
std::cout << "Options" << std::endl;
std::cout << " -i input executable name" << std::endl;
std::cout << " -o output file name " << std::endl;
std::cout << std::endl;
}
static protector_cfg
parse_args(int argc, char **argv)
{
protector_cfg config {};
int c;
while (c = getopt(argc, argv, "i:o:"), c != -1)
switch (c) {
case 'i':
config.in_fname = optarg;
break;
case 'o':
config.out_fname = optarg;
break;
default:
throw std::invalid_argument("ERROR: Unknown command line options");
}
if (config.in_fname.empty())
throw usage_exception();
if (config.out_fname.empty())
config.out_fname = config.in_fname + "_protected";
return config;
}
static int
run_protector(struct protector_cfg &config) {
elf_file *exec = new elf_file(config.in_fname);
if (!exec || !exec->is_loaded()) {
std::cerr << "ERROR: Failed to load executable.\n";
return -1;
}
std::cout << "Executable [" << config.in_fname << "] loaded successfully\n\n";
elf_file *elf_payload = new elf_file("protector_payload");
if (!elf_payload || !elf_payload->is_loaded()) {
std::cerr << "ERROR: Failed to load payload\n";
return -1;
}
if (exec->insert_payload(elf_payload) < 0) {
std::cerr << "ERROR: Failed to insert payload\n";
return -1;
}
exec->apply_flags_to_all_segments(PF_R | PF_W | PF_X);
if (!exec->save_to_file(config.out_fname)) {
std::cout << "Failed to save executable.\n";
return -1;
}
std::cout << "Output executable saved to [" << config.out_fname << "]\n";
return 0;
}
int
main(int argc, char **argv)
{
srand(time(NULL));
try {
auto config {parse_args(argc, argv)};
return run_protector(config);
} catch (usage_exception &) {
usage();
return 0;
}
return 0;
}