Repository navigation
Expand file tree
/
Copy pathapi.cpp
More file actions
457 lines (433 loc) · 17.5 KB
/
Copy pathapi.cpp
File metadata and controls
457 lines (433 loc) · 17.5 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Request dispatcher: translates JSON requests into x64dbg bridge calls.
//
// Request: {"id": 1, "method": "mem_read", "params": {"addr": "rsp", "size": 16}}
// Response: {"id": 1, "result": ...} or {"id": 1, "error": "message"}
//
// Address/value parameters accept an integer or a string evaluated as an x64dbg expression
// (numbers in expressions are hexadecimal by default, "kernel32.CreateFileW" and "rsp+8" work).
// Addresses and register values in results are hexadecimal strings ("0x7FF6...") so that
// 64-bit values survive JSON number limits.
#include "server.h"
#include "_plugins.h"
#include "jansson/jansson.h"
#include <chrono>
#include <functional>
#include <unordered_map>
#include <vector>
namespace
{
constexpr int PluginApiVersion = 1;
constexpr long long MaxMemoryRead = 64 * 1024 * 1024;
constexpr long long MaxDisasmCount = 10000;
#ifdef _WIN64
const char* const GprNames[] =
{
"rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"rip", "rflags",
};
#else
const char* const GprNames[] =
{
"eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
"eip", "eflags",
};
#endif //_WIN64
struct ApiError
{
std::string message;
};
[[noreturn]] void Fail(const std::string & message)
{
throw ApiError{ message };
}
// RAII owner for json_t references
struct JsonRef
{
json_t* ptr;
explicit JsonRef(json_t* p) : ptr(p) { }
~JsonRef()
{
if(ptr)
json_decref(ptr);
}
JsonRef(const JsonRef &) = delete;
JsonRef & operator=(const JsonRef &) = delete;
};
json_t* Hex(duint value)
{
char text[32];
sprintf_s(text, "0x%llX", (unsigned long long)value);
return json_string(text);
}
json_t* GetParam(json_t* params, const char* name, bool required)
{
json_t* value = params ? json_object_get(params, name) : nullptr;
if(json_is_null(value))
value = nullptr;
if(!value && required)
Fail(std::string("missing parameter '") + name + "'");
return value;
}
duint ValueParam(json_t* params, const char* name)
{
json_t* value = GetParam(params, name, true);
if(json_is_integer(value))
return (duint)json_integer_value(value);
if(json_is_string(value))
{
bool success = false;
duint result = DbgEval(json_string_value(value), &success);
if(!success)
Fail(std::string("cannot evaluate expression '") + json_string_value(value) + "'");
return result;
}
Fail(std::string("parameter '") + name + "' must be an integer or an expression string");
}
std::string StringParam(json_t* params, const char* name)
{
json_t* value = GetParam(params, name, true);
if(!json_is_string(value))
Fail(std::string("parameter '") + name + "' must be a string");
return json_string_value(value);
}
long long IntParam(json_t* params, const char* name, long long defaultValue)
{
json_t* value = GetParam(params, name, false);
if(!value)
return defaultValue;
if(!json_is_integer(value))
Fail(std::string("parameter '") + name + "' must be an integer");
return json_integer_value(value);
}
bool BoolParam(json_t* params, const char* name, bool defaultValue)
{
json_t* value = GetParam(params, name, false);
if(!value)
return defaultValue;
if(!json_is_boolean(value))
Fail(std::string("parameter '") + name + "' must be a boolean");
return json_is_true(value);
}
json_t* State()
{
json_t* state = json_object();
bool debugging = DbgIsDebugging();
bool running = debugging && DbgIsRunning();
json_object_set_new(state, "debugging", json_boolean(debugging));
json_object_set_new(state, "running", json_boolean(running));
if(debugging && !running)
json_object_set_new(state, "cip", Hex(DbgValFromString("cip")));
return state;
}
// Waits until the debuggee is paused. timeoutMs < 0 waits forever.
bool WaitPaused(long long timeoutMs, const std::atomic<bool> & serverRunning)
{
auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs < 0 ? 0 : timeoutMs);
while(serverRunning)
{
if(DbgIsDebugging() && !DbgIsRunning())
return true;
if(timeoutMs >= 0 && std::chrono::steady_clock::now() >= deadline)
return false;
Sleep(5);
}
return false;
}
json_t* ExecAndWait(const char* command, json_t* params, bool waitByDefault, const std::atomic<bool> & serverRunning)
{
if(!DbgCmdExecDirect(command))
Fail(std::string("command '") + command + "' failed");
bool paused = false;
if(BoolParam(params, "wait", waitByDefault))
paused = WaitPaused(IntParam(params, "timeout_ms", 10000), serverRunning);
json_t* state = State();
json_object_set_new(state, "paused", json_boolean(paused));
return state;
}
std::vector<unsigned char> FromHex(const std::string & text)
{
auto nibble = [](char ch) -> int
{
if(ch >= '0' && ch <= '9')
return ch - '0';
if(ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
if(ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
return -1;
};
std::vector<unsigned char> data;
int high = -1;
for(char ch : text)
{
if(ch == ' ')
continue;
int value = nibble(ch);
if(value < 0)
Fail("invalid hex data");
if(high < 0)
high = value;
else
{
data.push_back((unsigned char)(high << 4 | value));
high = -1;
}
}
if(high >= 0)
Fail("hex data has an odd number of digits");
return data;
}
std::string ToHex(const unsigned char* data, size_t size)
{
static const char digits[] = "0123456789ABCDEF";
std::string text(size * 2, '\0');
for(size_t i = 0; i < size; i++)
{
text[i * 2] = digits[data[i] >> 4];
text[i * 2 + 1] = digits[data[i] & 0xF];
}
return text;
}
using Method = std::function<json_t*(json_t* params, const std::atomic<bool> & serverRunning)>;
const std::unordered_map<std::string, Method> & Methods()
{
static const std::unordered_map<std::string, Method> methods =
{
{
"ping", [](json_t*, const std::atomic<bool> &)
{
json_t* result = json_object();
json_object_set_new(result, "name", json_string("pybridge"));
json_object_set_new(result, "version", json_integer(PluginApiVersion));
#ifdef _WIN64
json_object_set_new(result, "arch", json_string("x64"));
#else
json_object_set_new(result, "arch", json_string("x32"));
#endif //_WIN64
// Lets a client check that it reached the process it started
json_object_set_new(result, "pid", json_integer(GetCurrentProcessId()));
return result;
}
},
{
"quit", [](json_t*, const std::atomic<bool> &)
{
// Asynchronous: x64dbg (GUI or headless) closes after this response is sent
GuiCloseApplication();
return json_true();
}
},
{
"cmd", [](json_t* params, const std::atomic<bool> &)
{
return json_boolean(DbgCmdExecDirect(StringParam(params, "command").c_str()));
}
},
{
"eval", [](json_t* params, const std::atomic<bool> &)
{
auto expression = StringParam(params, "expr");
bool success = false;
duint value = DbgEval(expression.c_str(), &success);
if(!success)
Fail("cannot evaluate expression '" + expression + "'");
return Hex(value);
}
},
{
"state", [](json_t*, const std::atomic<bool> &)
{
return State();
}
},
{
"wait", [](json_t* params, const std::atomic<bool> & serverRunning)
{
bool paused = WaitPaused(IntParam(params, "timeout_ms", 10000), serverRunning);
json_t* state = State();
json_object_set_new(state, "paused", json_boolean(paused));
return state;
}
},
{
"run", [](json_t* params, const std::atomic<bool> & serverRunning)
{
// erun passes first chance exceptions to the debuggee
return ExecAndWait(BoolParam(params, "pass_exceptions", false) ? "erun" : "run", params, false, serverRunning);
}
},
{
"pause", [](json_t* params, const std::atomic<bool> & serverRunning)
{
return ExecAndWait("pause", params, true, serverRunning);
}
},
{
"stop", [](json_t* params, const std::atomic<bool> & serverRunning)
{
// Queued like the GUI's stop button: StopDebug waits for the debug loop to finish,
// so running it directly here could block this server thread for a long time.
if(DbgIsDebugging() && !DbgCmdExec("StopDebug"))
Fail("cannot queue StopDebug");
long long timeoutMs = IntParam(params, "timeout_ms", 15000);
auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
while(serverRunning && DbgIsDebugging() && std::chrono::steady_clock::now() < deadline)
Sleep(10);
json_t* state = State();
json_object_set_new(state, "stopped", json_boolean(!DbgIsDebugging()));
return state;
}
},
{
"step_into", [](json_t* params, const std::atomic<bool> & serverRunning)
{
return ExecAndWait("StepInto", params, true, serverRunning);
}
},
{
"step_over", [](json_t* params, const std::atomic<bool> & serverRunning)
{
return ExecAndWait("StepOver", params, true, serverRunning);
}
},
{
"step_out", [](json_t* params, const std::atomic<bool> & serverRunning)
{
return ExecAndWait("StepOut", params, true, serverRunning);
}
},
{
"regs", [](json_t*, const std::atomic<bool> &)
{
if(!DbgIsDebugging())
Fail("not debugging");
json_t* result = json_object();
for(auto name : GprNames)
json_object_set_new(result, name, Hex(DbgValFromString(name)));
return result;
}
},
{
"reg_get", [](json_t* params, const std::atomic<bool> &)
{
auto name = StringParam(params, "name");
bool success = false;
duint value = DbgEval(name.c_str(), &success);
if(!success)
Fail("unknown register '" + name + "'");
return Hex(value);
}
},
{
"reg_set", [](json_t* params, const std::atomic<bool> &)
{
auto name = StringParam(params, "name");
duint value = ValueParam(params, "value");
if(!DbgValSetScalar(name.c_str(), value))
Fail("cannot set '" + name + "'");
return json_true();
}
},
{
"mem_read", [](json_t* params, const std::atomic<bool> &)
{
duint addr = ValueParam(params, "addr");
long long size = IntParam(params, "size", -1);
if(size < 0 || size > MaxMemoryRead)
Fail("parameter 'size' must be between 0 and " + std::to_string(MaxMemoryRead));
std::vector<unsigned char> data((size_t)size);
if(size && !DbgMemRead(addr, data.data(), (duint)size))
Fail("cannot read memory");
return json_string(ToHex(data.data(), data.size()).c_str());
}
},
{
"mem_write", [](json_t* params, const std::atomic<bool> &)
{
duint addr = ValueParam(params, "addr");
auto data = FromHex(StringParam(params, "data"));
if(!data.empty() && !DbgMemWrite(addr, data.data(), data.size()))
Fail("cannot write memory");
return json_true();
}
},
{
"mem_valid", [](json_t* params, const std::atomic<bool> &)
{
return json_boolean(DbgMemIsValidReadPtr(ValueParam(params, "addr")));
}
},
{
"disasm", [](json_t* params, const std::atomic<bool> &)
{
duint addr = ValueParam(params, "addr");
long long count = IntParam(params, "count", 1);
if(count < 1 || count > MaxDisasmCount)
Fail("parameter 'count' must be between 1 and " + std::to_string(MaxDisasmCount));
json_t* result = json_array();
for(long long i = 0; i < count; i++)
{
BASIC_INSTRUCTION_INFO info = {};
DbgDisasmFastAt(addr, &info);
int size = info.size > 0 ? info.size : 1;
unsigned char bytes[16] = {};
bool readable = DbgMemRead(addr, bytes, std::min(size, 16));
json_t* entry = json_object();
json_object_set_new(entry, "address", Hex(addr));
json_object_set_new(entry, "size", json_integer(size));
json_object_set_new(entry, "text", json_string(readable ? info.instruction : "???"));
json_object_set_new(entry, "bytes", json_string(readable ? ToHex(bytes, std::min(size, 16)).c_str() : ""));
json_array_append_new(result, entry);
addr += size;
}
return result;
}
},
};
return methods;
}
std::string Dump(json_t* json)
{
// json_dump_callback avoids freeing memory allocated by jansson.dll
std::string result;
auto append = [](const char* buffer, size_t size, void* data)
{
((std::string*)data)->append(buffer, size);
return 0;
};
if(json_dump_callback(json, append, &result, JSON_COMPACT) != 0)
result = "{\"error\":\"cannot serialize response\"}";
return result;
}
}
std::string BridgeHandleRequest(const std::string & line, const std::atomic<bool> & serverRunning)
{
JsonRef response(json_object());
json_error_t parseError;
JsonRef request(json_loadb(line.data(), line.size(), 0, &parseError));
if(!json_is_object(request.ptr))
{
json_object_set_new(response.ptr, "error", json_string("request is not a JSON object"));
return Dump(response.ptr);
}
json_t* id = json_object_get(request.ptr, "id");
if(id)
json_object_set(response.ptr, "id", id);
const char* method = json_string_value(json_object_get(request.ptr, "method"));
json_t* params = json_object_get(request.ptr, "params");
try
{
if(!method)
Fail("missing 'method'");
auto found = Methods().find(method);
if(found == Methods().end())
Fail(std::string("unknown method '") + method + "'");
json_object_set_new(response.ptr, "result", found->second(params, serverRunning));
}
catch(const ApiError & error)
{
json_object_set_new(response.ptr, "error", json_string(error.message.c_str()));
}
return Dump(response.ptr);
}