Created
April 9, 2017 19:51
-
-
Save agustingianni/5b8e7acddf68189d46f2c073f1fab65f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <libdebug/Debugger.h> | |
| #include <string> | |
| using namespace std; | |
| using namespace lldb; | |
| int main(int argc, char** argv) | |
| { | |
| if (argc < 1) { | |
| cerr << "Usage: ./%s binary_file" << endl; | |
| return -1; | |
| } | |
| string binary_file{ argv[1] }; | |
| Debugger debugger{}; | |
| debugger.disable_aslr(); | |
| debugger.stop_at_entrypoint(); | |
| debugger.process_execute(binary_file); | |
| #ifndef DEBUG | |
| // Enable lldb's internal debug log channel. | |
| // debugger.enable_debug_log("lldb", { "api", "break", "module", "platform", "process" }); | |
| // debugger.enable_debug_log("lldb", { "all" }); | |
| #endif | |
| auto regions = debugger.memory_regions(); | |
| if (!regions) { | |
| printf("Could not get memory regions\n"); | |
| return false; | |
| } | |
| for (auto i = 0; i < regions->GetSize(); i++) { | |
| SBMemoryRegionInfo info; | |
| if (!regions->GetMemoryRegionAtIndex(i, info)) | |
| continue; | |
| printf("Name: %s mapped=%3s %c%c%c 0x%p 0x%p\n", | |
| info.GetName() ? info.GetName() : "no-name", | |
| info.IsMapped() ? "yes" : "no", | |
| info.IsReadable() ? 'R' : '-', | |
| info.IsWritable() ? 'W' : '-', | |
| info.IsExecutable() ? 'X' : '-', | |
| (void*)info.GetRegionBase(), | |
| (void*)info.GetRegionEnd()); | |
| } | |
| debugger.process_continue(); | |
| // debugger.process_stop(); | |
| // debugger.main_loop(); | |
| for (auto i = 0; i < 16; i++) { | |
| auto memory = debugger.evaluate_expression<void*>("(char *) malloc(32)"); | |
| if (!memory) | |
| continue; | |
| printf("Allocated memory at %p\n", *memory); | |
| } | |
| std::string library_path = "/usr/local/lib/libunicorn.dylib"; | |
| if (!debugger.library_load(library_path)) { | |
| printf("Could not load library\n"); | |
| return -1; | |
| } | |
| printf("Loaded library\n"); | |
| // getchar(); | |
| if (!debugger.library_unload(library_path)) { | |
| printf("Could not unload library\n"); | |
| return -1; | |
| } | |
| printf("Unloaded library\n"); | |
| // getchar(); | |
| debugger.registers_set({ { "rax", "0x44444444" }, | |
| { "rbx", "0x88888888" } }); | |
| if (auto reg_value = debugger.register_get("rax")) { | |
| printf("Register value: name=%s value=%s\n", reg_value->GetName(), reg_value->GetValue()); | |
| } | |
| if (auto reg_value = debugger.register_get("rbx")) { | |
| printf("Register value: name=%s value=%s\n", reg_value->GetName(), reg_value->GetValue()); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment