-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (87 loc) · 2.74 KB
/
Copy pathmain.cpp
File metadata and controls
115 lines (87 loc) · 2.74 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
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
std::string get_function_start_address(const std::string& line) {
std::istringstream iss(line);
std::string part;
// Get section and offset
if (std::getline(iss, part, ':')) {
std::getline(iss, part, ' ');
}
return part;
}
int main(int argc, char* argv[]) {
std::cout << "Usage: " << argv[0] << " <map_file> <function_name> <dumped_section_file>" << std::endl;
std::string mapFilePath;
std::string targetFunction;
std::string dumpedSectionFilePath;
if (argc == 4) {
mapFilePath = argv[1];
targetFunction = argv[2];
dumpedSectionFilePath = argv[3];
}
mapFilePath = "shellcode_gen.map";
targetFunction = "_code";
dumpedSectionFilePath = "shellcode_gen.text";
std::cout << "Using Map File: " << mapFilePath << std::endl;
std::cout << "Using Function Name: " << targetFunction << std::endl;
std::cout << "Using Dump Text File: " << dumpedSectionFilePath << std::endl;
targetFunction = targetFunction + " ";
std::ifstream mapFile(mapFilePath);
if (!mapFile.is_open()) {
std::cerr << "Failed to open map file: " << mapFilePath << std::endl;
return 1;
}
unsigned long long baseAddress;
std::string line;
// Find base address
while (std::getline(mapFile, line)) {
if (line.find("Preferred load address is") != std::string::npos) {
std::string hex_string = line.substr(line.find_last_of(' ') + 1);
baseAddress = std::stoull(hex_string, nullptr, 16);
break;
}
}
// Reset file pointer to beginning
mapFile.clear();
mapFile.seekg(0, std::ios::beg);
std::string strOffset;
while (std::getline(mapFile, line)) {
if (strstr(line.c_str(), targetFunction.c_str())) {
strOffset = get_function_start_address(line);
}
}
mapFile.close();
auto offset = stoul(strOffset, 0, 16);
std::ifstream dumpFile(dumpedSectionFilePath, std::ios::binary | std::ios::ate);
if (!dumpFile) {
std::cerr << "Unable to open file: " << dumpedSectionFilePath << std::endl;
return 1;
}
int size = dumpFile.tellg();
std::cout << "#define FUNCTION_OFFSET " << "0x" << std::hex << offset << std::endl;;
std::cout << "unsigned char _code_raw[" << std::dec << size << "] = { " << std::endl;;
char byte;
int count = 0;
dumpFile.seekg(0, std::ios::beg);
while (dumpFile.get(byte)) {
if (size-1 == count) {
std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex
<< static_cast<int>(static_cast<unsigned char>(byte)) << " ";
}
else {
std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex
<< static_cast<int>(static_cast<unsigned char>(byte)) << ", ";
}
if (++count % 16 == 0) {
std::cout << std::endl;
}
}
if (count % 16 != 0) {
std::cout << std::endl;
}
std::cout << "};";
dumpFile.close();
return 0;
}