-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_ne.c
353 lines (324 loc) · 9.13 KB
/
bin_ne.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
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
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_bin.h>
#include <r_io.h>
#include <r_cons.h>
#include <r_list.h>
#include "ne.h"
static ut64 loader;
//ne.c code start
static ut64 r_bin_ne_va_to_la(const ut16 segment, const ut16 offset) {
return (segment << 4) + offset -0x10 ;
}
static ut64 r_bin_ne_la_to_pa(const struct r_bin_ne_obj_t *bin, ut64 la) {
return la + (bin->ne_header->cbenttab << 4);
}
RBinAddr *r_bin_ne_get_entrypoint (const struct r_bin_ne_obj_t *bin) {
const NEHEADER *ne;
ut64 la;
RBinAddr *entrypoint;
if (!bin || !bin->ne_header) {
return NULL;
}
ne = bin->ne_header;
la=r_bin_ne_va_to_la(ne->cs,ne->ip);
entrypoint = R_NEW0 (RBinAddr);
if (entrypoint) {
entrypoint->vaddr = la;
entrypoint->paddr = 0x00003031;
}
return entrypoint;
}
static int cmp_sections(const void *a, const void *b) {
const RBinSection *s_a, *s_b;
s_a = a;
s_b = b;
return s_a->vaddr - s_b->vaddr;
}
static RBinSection *r_bin_ne_init_section(const struct r_bin_ne_obj_t *bin,
ut64 laddr) {
RBinSection *section;
section = R_NEW0 (RBinSection);
if (section) {
section->vaddr = laddr;
}
return section;
}
// RList *r_bin_ne_get_segments (const struct r_bin_ne_obj_t *bin) {
// RList *seg_list;
// RListIter *iter;
// RBinSection *section;
// MZ_image_relocation_entry *relocs;
// int i, num_relocs, section_number;
// ut16 ss;
// if (!bin || !bin->dos_header) {
// return NULL;
// }
// seg_list = r_list_newf (free);
// if (!seg_list) {
// return NULL;
// }
// /* Add address of first segment to make sure that it is present
// * even if there are no relocations or there isn't first segment in
// * the relocations. */
// section = r_bin_mz_init_section (bin, 0);
// if (!section) {
// goto err_out;
// }
// r_list_add_sorted (seg_list, section, cmp_sections);
// relocs = bin->relocation_entries;
// num_relocs = bin->dos_header->num_relocs;
// for (i = 0; i < num_relocs; i++) {
// RBinSection c;
// ut64 laddr, paddr, section_laddr;
// ut16 *curr_seg;
// int left;
// laddr = r_bin_mz_va_to_la (relocs[i].segment, relocs[i].offset);
// if ((laddr + 2) >= bin->load_module_size) {
// continue;
// }
// paddr = r_bin_mz_la_to_pa (bin, laddr);
// curr_seg = (ut16 *)r_buf_get_at (bin->b, paddr, &left);
// if (left < 2) {
// continue;
// }
// section_laddr = r_bin_mz_va_to_la (r_read_le16 (curr_seg), 0);
// if (section_laddr > bin->load_module_size) {
// continue;
// }
// c.vaddr = section_laddr;
// if (r_list_find (seg_list, &c, cmp_sections)) {
// continue;
// }
// section = r_bin_mz_init_section (bin, section_laddr);
// if (!section) {
// goto err_out;
// }
// r_list_add_sorted (seg_list, section, cmp_sections);
// }
// /* Add address of stack segment if it's inside the load module. */
// ss = bin->dos_header->ss;
// if (r_bin_mz_va_to_la (ss, 0) < bin->load_module_size) {
// section = r_bin_mz_init_section (bin, r_bin_mz_va_to_la (ss, 0));
// if (!section) {
// goto err_out;
// }
// r_list_add_sorted (seg_list, section, cmp_sections);
// }
// /* Fixup sizes and addresses, set name, permissions and set add flag */
// section_number = 0;
// r_list_foreach (seg_list, iter, section) {
// section->name = r_str_newf ("seg_%03d", section_number);
// if (section_number) {
// RBinSection *p_section = iter->p->data;
// p_section->size = section->vaddr - p_section->vaddr;
// p_section->vsize = p_section->size;
// }
// section->vsize = section->size;
// section->paddr = r_bin_mz_la_to_pa (bin, section->vaddr);
// section->perm = r_str_rwx ("rwx");
// section->add = true;
// section_number++;
// }
// section = r_list_get_top (seg_list);
// section->size = bin->load_module_size - section->vaddr;
// section->vsize = section->size;
// return seg_list;
// err_out:
// eprintf ("Error: alloc (RBinSection)\n");
// r_list_free (seg_list);
// return NULL;
// }
void *r_bin_ne_free (struct r_bin_ne_obj_t *bin) {
if (!bin) {
return NULL;
}
free ((void *)bin->dos_header);
free ((void *)bin->ne_header);
r_buf_free (bin->b);
bin->b = NULL;
free (bin);
return NULL;
}
static int r_bin_ne_init_hdr(struct r_bin_ne_obj_t *bin) {
if (!bin) {
return false;
}
if (!(bin->dos_header = malloc (sizeof(Dos_header)))) {
r_sys_perror ("malloc (dos_header)");
return false;
}
if (r_buf_read_at (bin->b, 0, (ut8*)bin->dos_header, sizeof (Dos_header)) == -1) {
eprintf("Error: read (dos_header)\n");
return false;
}
if (!(bin->ne_header = malloc (sizeof(NEHEADER)))) {
r_sys_perror ("malloc (dos_header)");
return false;
}
if (r_buf_read_at (bin->b,bin->dos_header->e_lfanew, (ut8*)bin->ne_header, sizeof (NEHEADER)) == -1) {
eprintf("Error: read (dos_header)\n");
return false;
}
if (!bin->kv) {
eprintf("Error: sdb instance is empty\n");
return false;
}
return true;
}
static int r_bin_ne_init(struct r_bin_ne_obj_t *bin) {
bin->dos_header =NULL;
bin->ne_header = NULL;
bin->kv = sdb_new0 ();
if (!r_bin_ne_init_hdr (bin)) {
eprintf ("Warning: File is not MZ\n");
return false;
}
return true;
}
struct r_bin_ne_obj_t *r_bin_ne_new (const char *file) {
const ut8 *buf;
struct r_bin_ne_obj_t *bin = R_NEW0 (struct r_bin_ne_obj_t);
if (!bin) {
return NULL;
}
bin->file = file;
if (!(buf = (ut8 *)r_file_slurp (file, &bin->size))) {
return r_bin_ne_free (bin);
}
bin->b = r_buf_new ();
if (!r_buf_set_bytes (bin->b, buf, bin->size)) {
free ((void *)buf);
return r_bin_ne_free (bin);
}
free ((void *)buf);
if (!r_bin_ne_init (bin)) {
return r_bin_ne_free (bin);
}
return bin;
}
struct r_bin_ne_obj_t *r_bin_ne_new_buf (const RBuffer *buf) {
struct r_bin_ne_obj_t *bin = R_NEW0 (struct r_bin_ne_obj_t);
if (!bin) {
return NULL;
}
bin->b = r_buf_new ();
bin->size = r_buf_size(buf);
if (!r_buf_set_bytes (bin->b, buf->buf, bin->size)) {
return r_bin_ne_free (bin);
}
return r_bin_ne_init (bin) ? bin : r_bin_ne_free (bin);
}
/*
struct r_bin_ne_obj_t *r_bin_ne_new_buf (const RBuffer *buf) {
struct r_bin_mz_obj_t *bin = R_NEW0 (struct r_bin_mz_obj_t);
if (!bin) {
return NULL;
}
bin->b = r_buf_new ();
bin->size = r_buf_size (buf);
if (!r_buf_set_bytes (bin->b, buf->buf, bin->size)) {
return r_bin_mz_free (bin);
}
return r_bin_mz_init (bin)? bin: r_bin_mz_free (bin);
}
*/
//ne.c code end
static Sdb *get_sdb(RBinFile *bf) {
const struct r_bin_ne_obj_t *bin;
if (bf && bf->o && bf->o->bin_obj) {
bin = (struct r_bin_ne_obj_t *)bf->o->bin_obj;
if (bin && bin->kv) {
return bin->kv;
}
}
return NULL;
}
static bool check_bytes(const ut8 *buf, ut64 length) {
unsigned int idx;
if (!buf) {
return false;
}
if (length <= 0x3d) {
return false;
}
idx = (buf[0x3c] | (buf[0x3d]<<8));
if (length > idx + 0x18 + 2) {
if (!memcmp (buf, "MZ", 2)) {
if (!memcmp (buf+idx, "NE", 2)) {
return true;
}
}
}
return false;
}
static void *load(RBinFile *bf, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
struct r_bin_ne_obj_t *ne_obj;
ne_obj = r_bin_ne_new_buf (buf);
if (ne_obj) {
sdb_ns_set (sdb, "info", ne_obj->kv);
}
return ne_obj;
}
static int destroy(RBinFile *bf) {
r_bin_ne_free ((struct r_bin_ne_obj_t *)bf->o->bin_obj);
return true;
}
static RList *entries(RBinFile *bf) {
RBinAddr *ptr = NULL;
RList *res = NULL;
if (!(res = r_list_newf (free))) {
return NULL;
}
ptr = r_bin_ne_get_entrypoint (bf->o->bin_obj);
if (ptr) {
r_list_append (res, ptr);
}
return res;
}
static RBinInfo *info(RBinFile *bf) {
RBinInfo *const ret = R_NEW0 (RBinInfo);
if (!ret) {
return NULL;
}
ret->file = strdup (bf->file);
ret->bclass = strdup ("MZ");
ret->rclass = strdup ("ne");
ret->os = strdup ("DOS");
ret->arch = strdup ("x86");
ret->machine = strdup ("i386");
ret->type = strdup ("New EXEC (New Executable file)");
ret->subsystem = strdup ("DOS");
ret->bits = 16;
ret->dbg_info = 0;
ret->big_endian = false;
ret->has_crypto = false;
ret->has_canary = false;
ret->has_retguard = -1;
ret->has_nx = false;
ret->has_pi = false;
ret->has_va = true;
return ret;
}
#if !R_BIN_NE
struct r_bin_plugin_t r_bin_plugin_ne = {
.get_sdb = &get_sdb,
.check_bytes = &check_bytes,
.load_buffer = &load,
.destroy = &destroy,
.name = "ne",
.desc = "NE",
.license = "BSD",
.entries = &entries,
.info=&info,
.minstrlen = 4,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_BIN,
.data = &r_bin_plugin_ne,
.version = R2_VERSION
};
#endif
#endif