-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
102 lines (88 loc) · 2.91 KB
/
main.cpp
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
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#define VERSION "1.0"
#define TAG_DELIM ';'
bool is_numerical(const std::string &str)
{
if (str.length() == 0) return false;
for (size_t i = 0; i < str.length(); i++) {
char c = tolower(str[i]);
if (c >= '0' && c <= '9') continue;
if (c >= 'a' && c <= 'f') continue;
return false;
}
return true;
}
bool process_file(const std::string &in_file, const std::string &out_file, const std::string &module_name)
{
std::ifstream infile(in_file);
if (!infile.is_open()) {
return false;
}
std::ofstream outfile(out_file);
if (!outfile.is_open()) {
return false;
}
std::map<DWORD, std::string> rva_to_cmt;
std::string line;
while (std::getline(infile, line)) {
if (line.length() == 0) continue;
size_t pos = line.find(TAG_DELIM, 0);
if (pos == std::string::npos || pos >= (line.length() - 1)) continue;
std::string rva_str = line.substr(0, pos);
std::string cmt_str = line.substr(pos + 1, line.length());
if (rva_str.length() == 0 || cmt_str.length() == 0) continue;
if (!is_numerical(rva_str)) continue; //skip lines that don't start from RVA
DWORD rva = std::stoi(rva_str, nullptr, 16);
rva_to_cmt[rva] = cmt_str;
}
infile.close();
if (module_name.length() == 0) {
std::cerr << "Module name is missing!\n";
return false;
}
outfile << "$base=" << "\"" << module_name << ":base\"" << std::endl;
std::map<DWORD, std::string>::iterator itr;
for (itr = rva_to_cmt.begin(); itr != rva_to_cmt.end(); itr++) {
const std::string cmt_str = itr->second;
const DWORD rva = itr->first;
outfile << "cmt $base+" << std::hex << rva << "," << "\"" << cmt_str << "\"" << std::endl;
outfile << "bookmark $base+" << std::hex << rva << std::endl;
}
outfile.close();
return true;
}
std::string fetch_module_name(std::string filename)
{
size_t pos = filename.find_last_of("\\/");
if (pos != std::string::npos) {
filename.erase(0, pos + 1);
}
pos = filename.find(".tag", 0);
if (pos == std::string::npos) return "";
return filename.substr(0, pos);
}
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cout << "Tag Converter v" << VERSION << "\n"
<< "URL: https://github.com/hasherezade/tag_converter\n";
std::cout << "Args: <in_file> [module_name] [out_file]\n";
return 0;
}
std::string module_name = fetch_module_name(argv[1]);
std::string out = std::string(argv[1]) + ".x64dbg.txt";
if (argc >= 3) {
module_name = argv[2];
}
if (argc >= 4) {
out = argv[3];
}
std::cout << "Module Name: " << module_name << "\n";
std::cout << "Output file: " << out << "\n";
process_file(argv[1], out, module_name);
return 0;
}