-
Notifications
You must be signed in to change notification settings - Fork 17
/
export.cc
131 lines (113 loc) · 3.31 KB
/
export.cc
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
#include "bb.h"
#include "edge.h"
#include "insn.h"
#include "cfg.h"
#include "log.h"
#include "nucleus.h"
#include "export.h"
int
export_bin2ida(std::string &fname, Binary *bin, std::list<DisasmSection> *disasm, CFG *cfg)
{
FILE *f;
uint64_t entry;
size_t i;
f = fopen(fname.c_str(), "w");
if(!f) {
print_err("cannot open file '%s' for writing", fname.c_str());
return -1;
}
fprintf(f, "\"\"\"\n");
fprintf(f, "Script generated by %s\n", NUCLEUS_VERSION);
fprintf(f, "\"\"\"\n");
fprintf(f, "\n");
fprintf(f, "import idaapi\n");
fprintf(f, "import idautils\n");
fprintf(f, "import idc\n");
fprintf(f, "\n");
fprintf(f, "idaapi.autoWait()\n");
fprintf(f, "\n");
fprintf(f, "def mark_functions():\n");
fprintf(f, " functions = [\n");
i = 0;
for(auto &func: cfg->functions) {
if(func.entry.empty()) continue;
entry = func.entry.front()->start;
if(!(i % 5)) fprintf(f, " ");
fprintf(f, "0x%jx, ", entry);
if(!(++i % 5)) fprintf(f, "\n");
}
fprintf(f, " ]\n");
fprintf(f, " for seg in idautils.Segments():\n");
fprintf(f, " if idaapi.segtype(idc.SegStart(seg)) != idaapi.SEG_CODE:\n");
fprintf(f, " continue\n");
fprintf(f, " for f in idautils.Functions(idc.SegStart(seg), idc.SegEnd(seg)):\n");
fprintf(f, " print 'nucleus: deleting function 0x%%x' %% (f)\n");
fprintf(f, " idc.DelFunction(f)\n");
fprintf(f, " for f in functions:\n");
fprintf(f, " print 'nucleus: defining function 0x%%x' %% (f)\n");
fprintf(f, " if idc.MakeCode(f):\n");
fprintf(f, " idc.MakeFunction(f)\n");
fprintf(f, "\n");
fprintf(f, "mark_functions()\n");
fclose(f);
return 0;
}
int
export_bin2binja(std::string &fname, Binary *bin, std::list<DisasmSection> *disasm, CFG *cfg)
{
FILE *f;
uint64_t entry;
size_t i;
f = fopen(fname.c_str(), "w");
if(!f) {
print_err("cannot open file '%s' for writing", fname.c_str());
return -1;
}
fprintf(f, "\"\"\"\n");
fprintf(f, "Script generated by %s\n", NUCLEUS_VERSION);
fprintf(f, "\"\"\"\n");
fprintf(f, "\n");
fprintf(f, "import binaryninja\n");
fprintf(f, "\n");
fprintf(f, "def mark_functions():\n");
fprintf(f, " functions = [\n");
i = 0;
for(auto &func: cfg->functions) {
if(func.entry.empty()) continue;
entry = func.entry.front()->start;
if(!(i % 5)) fprintf(f, " ");
fprintf(f, "0x%jx, ", entry);
if(!(++i % 5)) fprintf(f, "\n");
}
fprintf(f, " ]\n");
fprintf(f, " for f in bv.functions:\n");
fprintf(f, " bv.remove_function(f)\n");
fprintf(f, " for f in functions:\n");
fprintf(f, " print 'nucleus: defining function 0x%%x' %% (f)\n");
fprintf(f, " bv.add_function(f)\n");
fprintf(f, "\n");
fprintf(f, "mark_functions()\n");
fclose(f);
return 0;
}
int
export_cfg2dot(std::string &fname, CFG *cfg)
{
FILE *f;
BB *bb;
f = fopen(fname.c_str(), "w");
if(!f) {
print_err("cannot open file '%s' for writing", fname.c_str());
return -1;
}
fprintf(f, "digraph G {\n\n");
for(auto &kv : cfg->start2bb) {
bb = kv.second;
for(auto &e : bb->targets) {
fprintf(f, "bb_%jx -> bb_%jx [ label=\"%s\" ];\n", e.src->start, e.dst->start, e.type2str().c_str());
}
}
fprintf(f, "}\n");
fclose(f);
return 0;
}