-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathend_to_end.cpp
More file actions
182 lines (161 loc) · 5.91 KB
/
Copy pathend_to_end.cpp
File metadata and controls
182 lines (161 loc) · 5.91 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
// Copyright (c) 2024-2026 Elias Bachaalany
// SPDX-License-Identifier: LicenseRef-Human-Origin-Source-1.0
//
// This file is licensed under the Human-Origin Source License v1.0.
// See LICENSE.
//
// end_to_end: Launch headless Ghidra, analyze a binary, enumerate functions
// with basic blocks and decompilation, save the project, and shut down.
//
// Usage:
// end_to_end --ghidra <ghidra_dist> --binary <target.exe> [--port <port>]
//
// Prerequisites:
// - Ghidra distribution with the LibGhidraHost extension installed
// (install via: gradle installExtension -PGHIDRA_INSTALL_DIR=<dist>)
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include "libghidra/ghidra.hpp"
// ---------------------------------------------------------------------------
// Analysis: enumerate functions with blocks, edges, and decompilation
// ---------------------------------------------------------------------------
static void analyze(ghidra::Client& client) {
auto funcs_resp = client.ListFunctions(0, UINT64_MAX, 0, 0);
if (!funcs_resp.ok()) {
fprintf(stderr, "ListFunctions failed: %s\n",
funcs_resp.status.message.c_str());
return;
}
const auto& functions = funcs_resp.value->functions;
printf("\n%s\n %zu functions found\n%s\n\n",
std::string(70, '=').c_str(), functions.size(),
std::string(70, '=').c_str());
for (const auto& func : functions) {
printf("--- %s @ 0x%llx (%llu bytes, %u params) ---\n",
func.name.c_str(), (unsigned long long)func.entry_address,
(unsigned long long)func.size, func.parameter_count);
// Basic blocks
auto bb =
client.ListBasicBlocks(func.start_address, func.end_address, 0, 0);
if (bb.ok() && !bb.value->blocks.empty()) {
printf(" Basic blocks (%zu):\n", bb.value->blocks.size());
for (const auto& b : bb.value->blocks) {
printf(" 0x%llx..0x%llx in_degree=%u out_degree=%u\n",
(unsigned long long)b.start_address,
(unsigned long long)b.end_address, b.in_degree, b.out_degree);
}
} else {
printf(" Basic blocks: (none)\n");
}
// CFG edges
auto edges =
client.ListCFGEdges(func.start_address, func.end_address, 0, 0);
if (edges.ok() && !edges.value->edges.empty()) {
printf(" CFG edges (%zu):\n", edges.value->edges.size());
for (const auto& e : edges.value->edges) {
printf(" 0x%llx -> 0x%llx (%s)\n",
(unsigned long long)e.src_block_start,
(unsigned long long)e.dst_block_start, e.edge_kind.c_str());
}
}
// Decompilation
auto dec = client.GetDecompilation(func.entry_address, 30000);
if (dec.ok() && dec.value->decompilation &&
dec.value->decompilation->completed) {
const auto& code = dec.value->decompilation->pseudocode;
int line_count = 1;
for (char c : code)
if (c == '\n') ++line_count;
printf(" Decompilation (%d lines):\n", line_count);
printf(" ");
for (char c : code) {
putchar(c);
if (c == '\n') printf(" ");
}
printf("\n");
} else if (dec.ok() && dec.value->decompilation &&
!dec.value->decompilation->error_message.empty()) {
printf(" Decompilation error: %s\n",
dec.value->decompilation->error_message.c_str());
} else {
printf(" Decompilation: %s\n",
dec.ok() ? "(empty)" : dec.status.message.c_str());
}
printf("\n");
}
}
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
int main(int argc, char* argv[]) {
std::string ghidra_dir, binary_path;
int port = 18080;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--ghidra") == 0 && i + 1 < argc)
ghidra_dir = argv[++i];
else if (std::strcmp(argv[i], "--binary") == 0 && i + 1 < argc)
binary_path = argv[++i];
else if (std::strcmp(argv[i], "--port") == 0 && i + 1 < argc)
port = std::atoi(argv[++i]);
else {
fprintf(stderr,
"Usage: %s --ghidra <ghidra_dist> --binary <target> [--port N]\n",
argv[0]);
return 1;
}
}
if (ghidra_dir.empty() || binary_path.empty()) {
fprintf(stderr, "ERROR: --ghidra and --binary are required\n");
return 1;
}
try {
ghidra::HeadlessProjectOptions opts;
opts.ghidra_dir = ghidra_dir;
opts.port = port;
opts.on_output = [](const std::string& line) {
printf(" [ghidra] %s\n", line.c_str());
};
auto h = ghidra::launch_headless_project(std::move(opts));
ghidra::ImportProgramRequest import;
import.source_path = binary_path;
import.overwrite = true;
import.analyze = true;
auto imported = h->ImportProgram(import);
if (!imported.ok()) {
fprintf(stderr, "Import failed: %s\n", imported.status.message.c_str());
return 1;
}
ghidra::OpenProgramRequest open;
open.program_path = imported.value->primary_program_path;
open.analyze = false;
auto opened = h->OpenProgram(open);
if (!opened.ok()) {
fprintf(stderr, "Open failed: %s\n", opened.status.message.c_str());
return 1;
}
auto status = h->GetStatus();
if (!status.ok()) {
fprintf(stderr, "Cannot reach host: %s\n",
status.status.message.c_str());
return 1;
}
printf("\nConnected: %s v%s (mode: %s)\n\n",
status.value->service_name.c_str(),
status.value->service_version.c_str(),
status.value->host_mode.c_str());
analyze(*h);
printf("Saving project...\n");
auto save = h->SaveProgram();
printf(" saved=%s\n", save.ok() ? "true" : "false");
int code = h.close();
printf(" Ghidra exited with code %d\n", code);
} catch (const std::exception& e) {
fprintf(stderr, "\nERROR: %s\n", e.what());
return 1;
}
printf("\nDone.\n");
return 0;
}