-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsystem.cpp
319 lines (236 loc) · 7.26 KB
/
system.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
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
/*
Patchdiff2
Portions (C) 2011 Alexander Pick
Portions (C) 2010 Nicolas Pouvesle
Portions (C) 2007 - 2009 Tenable Network Security, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pro.h>
#include <ida.hpp>
#include <fpro.h>
#include <diskio.hpp>
#include <kernwin.hpp>
#include "sig.hpp"
#include "system.hpp"
#include "options.hpp"
#include "os.hpp"
// global variable to keep IPC state
ipc_config_t ipcc;
void gen_random(char *s, const int len) {
for (int i = 0; i < len; ++i) {
int randomChar = rand()%(26+26+10);
if (randomChar < 26)
s[i] = 'a' + randomChar;
else if (randomChar < 26+26)
s[i] = 'A' + randomChar - 26;
else
s[i] = '0' + randomChar - 26 - 26;
}
s[len] = 0;
}
void system_temp_name(char * data, size_t size)
{
char name[QMAXPATH];
char * str;
gen_random(name, 10);
qsnprintf(data, size, "/tmp/%s.idc", name);
data[strlen(data)] = '\0';
}
/*------------------------------------------------*/
/* function : generate_idc_file */
/* description: generates an idc file to launch */
/* second plugin instance in batch */
/* mode */
/*------------------------------------------------*/
int generate_idc_file(char * file)
{
FILE * fp;
fp = qfopen(file, "w+");
if (!fp) return -1;
qfwrite(fp, PATCHDIFF_IDC, strlen(PATCHDIFF_IDC));
qfclose(fp);
return 0;
}
/*------------------------------------------------*/
/* function : system_execute_second_instance */
/* description: Executes another IDA instance */
/*------------------------------------------------*/
int system_execute_second_instance(char * idc, ea_t ea, char * file, bool close, long id, void * data)
{
char path[QMAXPATH*4];
char cmd[QMAXPATH*4];
if (!getsysfile(path, sizeof(path), IDA_EXEC, NULL))
return -1;
if (generate_idc_file(idc))
return -1;
qsnprintf(cmd, sizeof(cmd), "%s -A -S\"%s\" -Opatchdiff:%lu:%"FMT_EA"u:%u:\"%s\" \"%s\"",
path,
idc,
id,
ea,
dto.graph.s_showpref,
idc,
file
);
return os_execute_command(cmd, close, data);
}
/*------------------------------------------------*/
/* function : ipc_init */
/* description: Inits interprocess communication */
/* between 2 IDA instances */
/*------------------------------------------------*/
bool ipc_init(char * file, int type, long id)
{
bool ret;
long pid;
char tmpname[QMAXPATH];
if (type == 0)
{
ipcc.init = false;
ipcc.data = NULL;
}
else if (!ipcc.init)
{
if (type == 1)
{
pid = os_get_pid();
ret = os_ipc_init(&ipcc.data, pid, IPC_SERVER);
if (!ret)
return false;
system_temp_name(tmpname, sizeof(tmpname));
if (system_execute_second_instance(tmpname, BADADDR, file, false, pid, ipcc.data) != 0)
{
ipc_close();
return false;
}
}
else
{
ret = os_ipc_init(&ipcc.data, id, IPC_CLIENT);
if (!ret)
return false;
}
ipcc.init = true;
}
return true;
}
/*------------------------------------------------*/
/* function : ipc_close */
/* description: Closes interprocess communication */
/* between 2 IDA instances */
/*------------------------------------------------*/
void ipc_close()
{
if (!ipcc.init) return;
os_ipc_close(ipcc.data);
ipcc.data = NULL;
ipcc.init = false;
}
/*------------------------------------------------*/
/* function : ipc_send_cmd */
/* description: Executes command on the remote */
/* IDA instance */
/*------------------------------------------------*/
bool ipc_send_cmd(char * cmd)
{
idata_t d;
d.cmd = IPC_DATA;
qstrncpy(d.data, cmd, sizeof(d.data));
msg("IPC call\n");
if (!os_ipc_send(ipcc.data, IPC_SERVER, &d))
return false;
if (!os_ipc_recv(ipcc.data, IPC_SERVER, &d) || d.cmd != IPC_DONE)
return false;
msg("IPC call successful\n");
return true;
}
/*------------------------------------------------*/
/* function : ipc_recv_cmd */
/* description: Receives command to execute */
/*------------------------------------------------*/
bool ipc_recv_cmd(char * buf, size_t blen)
{
bool ret;
idata_t d;
size_t len;
msg("IPC recv\n");
memset(d.data, '\0', sizeof(d.data));
ret = os_ipc_recv(ipcc.data, IPC_CLIENT, &d);
if (!ret)
return false;
if (d.cmd != IPC_DATA)
return false;
msg("IPC recv successful\n");
len = strlen(d.data) + 1;
if (len > blen)
len = blen;
memcpy(buf, d.data, len);
return true;
}
/*------------------------------------------------*/
/* function : ipc_recv_cmd_end */
/* description: Acknowledges end of command */
/*------------------------------------------------*/
bool ipc_recv_cmd_end()
{
idata_t d;
d.cmd = IPC_DONE;
return os_ipc_send(ipcc.data, IPC_CLIENT, &d);
}
/*------------------------------------------------*/
/* function : ipc_execute_second_instance */
/* description: Sends command to the second IDA */
/* instance */
/*------------------------------------------------*/
void ipc_execute_second_instance(char * idc, ea_t ea, char * file)
{
char cmd[QMAXPATH*4];
if (!ipc_init(file, 1, 0))
return;
qsnprintf(cmd, sizeof(cmd), "%u:%"FMT_EA"u:%u:%s",
0,
ea,
dto.graph.s_showpref,
idc
);
ipc_send_cmd(cmd);
}
/*------------------------------------------------*/
/* function : system_parse_idb */
/* description: generates a list of signatures for*/
/* another idb */
/*------------------------------------------------*/
slist_t * system_parse_idb(ea_t ea, char * file, options_t * opt)
{
slist_t * sl = NULL;
char tmpname[QMAXPATH];
system_temp_name(tmpname, sizeof(tmpname));
//printf("system_parse_idb()\n");
//if (!options_use_ipc(opt))
system_execute_second_instance(tmpname, ea, file, true, 0, NULL);
//else
// ipc_execute_second_instance(tmpname, ea, file);
sl = siglist_load(tmpname);
os_unlink(tmpname);
return sl;
}
/*------------------------------------------------*/
/* function : system_get_pref */
/* description: Gets global system preference */
/*------------------------------------------------*/
bool system_get_pref(char * name, void * data, int type)
{
if (type == SPREF_INT)
{
return os_get_pref_int(name, (int *)data);
}
return false;
}