-
Notifications
You must be signed in to change notification settings - Fork 1
/
winvmpatcher.cpp
170 lines (134 loc) · 4.41 KB
/
winvmpatcher.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "memflow.hpp"
#include <fstream>
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;
struct PatchData {
unsigned long offset;
char original;
char patch;
};
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s memory_diff_path module_name\n -cn - connector name\n -ca - connector arg\n -on - os name\n -oa - os arg\n -im - ignore mismatch\n -ll - log level\n", argv[0]);
return 1;
}
const char *memory_diff_path = argv[1];
const char *module_name = argv[2];
const char *connector_name = "qemu";
const char *connector_arg = "";
const char *os_name = "win32";
const char *os_arg = "";
bool ignore_mismatch = false;
int log_level = 3;
for (int i = 3; i < argc; i++) {
if (!strcmp(argv[i],"-im")) {
ignore_mismatch = true;
} else if (i + 1 >= argc) {
continue;
} else if (!strcmp(argv[i],"-cn")) {
connector_name = argv[++i];
} else if (!strcmp(argv[i],"-ca")) {
connector_arg = argv[++i];
} else if (!strcmp(argv[i],"-on")) {
os_name = argv[++i];
} else if (!strcmp(argv[i],"-oa")) {
os_arg = argv[++i];
} else if (!strcmp(argv[i],"-ll")) {
log_level = atoi(argv[++i]);
if (log_level < 0 || log_level > 5) {
printf("Log level is from 0 to 6 [Off, Error, Warn, Info, Debug, Trace]\n");
return 1;
}
}
}
fstream input_file(memory_diff_path);
if (!input_file.is_open()) {
printf("Failed to open memory diff file\n");
return 1;
}
string line;
vector<PatchData> patch_data_list;
while (getline(input_file, line)) {
istringstream iss(line);
string offset_str, original_str, patch_str;
iss >> offset_str >> original_str >> patch_str;
unsigned long offset = stoi(offset_str, nullptr, 16);
char original = stoi(original_str, nullptr, 16);
char patch = stoi(patch_str, nullptr, 16);
patch_data_list.push_back({ offset, original, patch });
}
input_file.close();
log_init((LevelFilter)log_level);
Inventory *inventory = inventory_scan();
if (!inventory) {
printf("Unable to create inventory\n");
return 1;
}
ConnectorInstance<> connector;
OsInstance<> os;
bool connector_initializing = true;
bool os_initializing = true;
while (connector_initializing || os_initializing) {
if (!(connector_initializing = inventory_create_connector(inventory, connector_name, connector_arg, &connector))) {
os_initializing = inventory_create_os(inventory, os_name, os_arg, &connector, &os);
}
}
connector_drop(&connector);
inventory_free(inventory);
ModuleInfo module_info;
CSliceRef<uint8_t> module_name_ref = CSliceRef<uint8_t>(module_name);
bool module_initializing = true;
while (module_initializing) {
module_initializing = os.module_by_name(module_name_ref, &module_info);
}
char value;
CSliceMut<uint8_t> value_mut = CSliceMut<uint8_t>(&value, sizeof(value));
CSliceRef<uint8_t> value_ref = CSliceRef<uint8_t>(&value, sizeof(value));
bool memory_initializing = true;
while (memory_initializing) {
memory_initializing = os.read_raw_into(module_info.base, value_mut);
}
bool mismatch = false;
for (auto pi = patch_data_list.begin(); pi != patch_data_list.end(); ) {
if (!os.read_raw_into(module_info.base + pi->offset, value_mut)) {
printf("Pre-patch %#010lx value %02X, patch original %02X\n", pi->offset, (unsigned char)value, (unsigned char)pi->original);
if (value == pi->original) {
pi++;
continue;
}
} else {
printf("Failed to read pre-patch value %#010lx\n", pi->offset);
}
mismatch = true;
pi = patch_data_list.erase(pi);
}
if (mismatch) {
printf("Memory and patch original value does not match\n");
if (ignore_mismatch) {
printf("Ignoring\n");
} else {
printf("Exiting\n");
os_drop(&os);
return 1;
}
}
for (const auto& patch_data : patch_data_list) {
value = patch_data.patch;
if (!os.write_raw(module_info.base + patch_data.offset, value_ref)) {
printf("Patched %#010lx to %02X\n", patch_data.offset, (unsigned char)value);
} else {
printf("Failed to patch %#010lx!\n", patch_data.offset);
}
}
for (const auto& patch_data : patch_data_list) {
if (!os.read_raw_into(module_info.base + patch_data.offset, value_mut)) {
printf("Post-patch %#010lx value %02X\n", patch_data.offset, (unsigned char)value);
} else {
printf("Failed to read post-patch value %#010lx\n", patch_data.offset);
}
}
os_drop(&os);
return 0;
}