-
Notifications
You must be signed in to change notification settings - Fork 1
/
first.c
322 lines (276 loc) · 9.42 KB
/
first.c
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
#include <stdbool.h>
#include <r_core.h>
#include <r_socket.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "utils.h"
#define SETDESC(x, y) r_config_node_desc (x, y)
#define SETPREF(x, y, z) SETDESC (r_config_set (core->config, x, y), z)
bool init_fst = false;
bool init_fcn = false;
static const char *help_msg_Fst[] = {
"Usage:", "Fst[?asug]", "FIRST plugin",
"Fst", "", "test connection to server",
"Fst?", "", "show this help",
"Fsta", " [func]", "add function to FIRST",
"Fstaa", "", "add all functions to FIRST",
"Fstaac", " [comment]", "add all functions to FIRST with a comment",
"Fstd", " [addr]", "delete annotation from FIRST",
"Fstdd", " [id]", "delete annotation from FIRST of a function that don't exist in this file (you can see all created annotations using Fstgc)",
"Fstg", "", "get annotations saved in FIRST",
"Fstgc", "", "get all created annotations saved in FIRST (this does not depend on the opened file)",
"Fsth", " [addr]", "get annotation history of a function",
"Fsthh", " [id]", "get annotation history of a function that don't exist in this file (you can see all created annotations using Fstgc)",
"Fst+", " [addr] [id]", "apply annotations",
"Fsts", " [func]", "scan for similar functions in FIRST",
"Fstsa", "", "scan all functions for similar functions in FIRST",
NULL
};
int cmd_fst(RCore* core, const char *input) {
if (!input) {
return true;
}
switch (input[0]) {
case 'a':
switch(input[1]){
case 'a':
{
RList* fcns = core->anal->fcns;
if(fcns){
eprintf("Adding %d functions to FIRST\n", fcns->length);
if (input[2] == 'c' && input[3] == ' '){
do_add_all(core,fcns, input + 4 );
break;
}
do_add_all(core,fcns,NULL);
}
else{
eprintf("Cant find functions\n");
}
break;
}
case ' ':
case '\0':
{
RAnalFunction* fcn;
if (input[1] == ' '){
fcn = r_anal_fcn_find_name(core->anal, input+2);
if (!fcn){
ut64 addr = r_num_math(core->num, input + 2);
if (core->num->nc.errors){
eprintf("Unknown function address or name\n");
break;
}
fcn = r_anal_get_fcn_at(core->anal, (ut64)r_num_math(core->num, input + 2),0);
if(!fcn){
eprintf("Cant find function\n");
break;
}
}
}
else{
ut64 addr = r_num_math(core->num, "$FB");
if (core->num->nc.errors){
eprintf("Cant find function\n");
break;
}
fcn = r_anal_get_fcn_at(core->anal, addr,0);
if(!fcn){
eprintf("Cant find function\n");
break;
}
}
eprintf("Adding function %s of address 0x%08llx\n",fcn->name, fcn->addr);
do_add(core, fcn);
break;
}
default:
r_core_cmd_help(core,help_msg_Fst);
}
break;
case 'd':
switch (input[1]){
case ' ':
{
int addr = (int)r_num_math(core->num, input + 2);
do_delete(core, addr );
break;
}
case 'd':
if(input[2] == ' '){
do_delete_id(input +3 );
break;
}
default:
r_core_cmd_help(core,help_msg_Fst);
}
break;
case 's':
switch(input[1]){
case 'a':
{
RList* fcns = core->anal->fcns;
if(fcns){
eprintf("Scanning all %d functions in FIRST (Use the ID to apply the annotations using Fst+)\n", fcns->length);
do_scan_all(core,fcns);
}
else
eprintf("Cant find functions\n");
break;
}
case ' ':
case '\0':
{
RAnalFunction* fcn;
if (input[1] == ' '){
fcn = r_anal_fcn_find_name(core->anal, input+2);
if (!fcn){
ut64 addr = r_num_math(core->num, input + 2);
if (core->num->nc.errors){
eprintf("Unknown function address or name\n");
break;
}
fcn = r_anal_get_fcn_at(core->anal, (ut64)r_num_math(core->num, input + 2),0);
if(!fcn){
eprintf("Cant find function\n");
break;
}
}
}
else{
ut64 addr = r_num_math(core->num, "$FB");
if (core->num->nc.errors){
eprintf("Cant find function\n");
break;
}
fcn = r_anal_get_fcn_at(core->anal, addr,0);
if(!fcn){
eprintf("Cant find function\n");
break;
}
}
eprintf("Scanning for similar functions of %s of address 0x%08llx\n",fcn->name, fcn->addr);
do_scan(core, fcn);
break;
}
default:
r_core_cmd_help(core,help_msg_Fst);
}
break;
case '+':
{
RAnalFunction* fcn;
if (input[1] == ' '){
char addresss[11];
strncpy(addresss, input + 2, 10);
ut64 addr = r_num_math(core->num,addresss);
if (core->num->nc.errors){
eprintf("Unknown function address\n");
break;
}
fcn = r_anal_get_fcn_at(core->anal, (ut64)r_num_math(core->num, input + 2),0);
if(!fcn){
eprintf("Cant find function\n");
break;
}
int i=2;
while(input[i] != ' '){
i++;
if (i > 15){
r_core_cmd_help(core,help_msg_Fst);
break;
}
}
while(input[i] == ' ')
i++;
do_apply(core, input+i, addr);
}
break;
}
// case 'r':
// read_db();
break;
case 'g':
switch(input[1]){
case 'c':
do_created();
break;
case '\0':
do_get();
break;
default:
r_core_cmd_help(core,help_msg_Fst);
}
break;
case 'h':
switch (input[1]){
case ' ':
{
int addr = (int)r_num_math(core->num, input + 2);
do_history(addr );
break;
}
case 'h':
if(input[2] == ' '){
do_history_id(input +3 );
break;
}
default:
r_core_cmd_help(core,help_msg_Fst);
}
break;
case '\0':
if(s_test_connection())
eprintf("You are connected to FIRST server...\n");
else{
char *homedir = NULL;
homedir = (char*)malloc(strlen(getenv("HOME"))+ strlen("/root/.config/first/first.config"));
strcpy(homedir,getenv("HOME"));
eprintf("Problem connecting to FIRST server... Check the configuration file at %s\n", strcat(homedir,"/.config/first/first.config"));
}
break;
case '?':
default:
r_core_cmd_help(core,help_msg_Fst);
}
return true;
}
int cmd(void *user, const char *input) {
RCore *core = (RCore *) user;
if (strncmp ("Fst", input, 3)) {
return false;
}
if(!init_fst)
if(f_set_config()){
set_hashes(core);
init_fst = true;
}
if (init_fst)
cmd_fst(core,input+3);
return true;
}
int init(void *user, const char *_input) {
RCmd *rcmd = (RCmd*) user;
RCore *core = (RCore *) rcmd->data;
RCoreAutocomplete *Fst = r_core_autocomplete_add (core->autocomplete, "Fst", R_CORE_AUTOCMPLT_DFLT, true);
// r_core_autocomplete_add (Fst, "Fsta", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fstaa", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fstap", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fstg", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fstgh", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fstgc", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fsts", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fstsa", R_CORE_AUTOCMPLT_OPTN, true);
// r_core_autocomplete_add (Fst, "Fstu", R_CORE_AUTOCMPLT_OPTN, true);
return true; };
RCorePlugin r_core_plugin_test = {.name = "First",
.desc = "r2 plugin for FIRST",
.license = "",
.call = cmd,
.init = init};
#ifndef CORELIB
RLibStruct radare_plugin = {.type = R_LIB_TYPE_CORE,
.data = &r_core_plugin_test,
.version = R2_VERSION};
#endif