-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIDA.pm
226 lines (209 loc) · 5.57 KB
/
IDA.pm
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
package IDA;
# vim:set ts=4 sw=4:
#
# (C) 2008 Willem Hengeveld itsme@xs4all.nl
#
# IdcPerl - a perl scripting plugin for the Interactive Disassembler by hex-rays.
# see http://www.xs4all.nl/~itsme/projects/idcperl
#
use strict;
use warnings;
use IDC;
require Exporter;
our @ISA= qw(Exporter);
our @EXPORT= qw(forrefs forfunctions);
# todo:
# sub import { my ($pack, @imports)= @_; ... }
# -> check 'imports' to handle the various
# 'use IDA <params>' params.
# ... experiment, trying to put 'ea' in caller's namespace.
# see Scope::Upper for how to really do this ( in .xs )
# no strict 'refs';
# use vars qw($ea);
# my $caller=caller;
# local(*{$caller."::ea"}) = \my $ea;
# enum all functions in selection/file
sub forfunctions (&)
{
my ($code)= @_;
my ($ea, $eea);
if (SelStart()==BADADDR) {
$ea= GetFunctionAttr(FirstSeg(), FUNCATTR_START);
$ea=NextFunction(FirstSeg()) if ($ea==BADADDR);
$eea= BADADDR;
}
else {
$ea= GetFunctionAttr(SelStart(), FUNCATTR_START);
$ea=NextFunction(SelStart()) if ($ea==BADADDR);
$eea= SelEnd();
}
while (($eea==BADADDR || $ea < $eea) && $ea!=BADADDR)
{
eval { &{$code}($ea); };
last if $@;
$ea=NextFunction($ea);
}
}
# enumerate all function chunks in selection/file
sub for_chunks (&)
{
my ($code)= @_;
my ($ea, $eea);
if (SelStart()==BADADDR) {
$ea= GetFchunkAttr(FirstSeg(), FUNCATTR_START);
$ea=NextFchunk(FirstSeg()) if ($ea==BADADDR);
$eea= BADADDR;
}
else {
$ea= GetFchunkAttr(SelStart(), FUNCATTR_START);
$ea=NextFchunk(SelStart()) if ($ea==BADADDR);
$eea= SelEnd();
}
while (($eea==BADADDR || $ea < $eea) && $ea!=BADADDR)
{
eval { &{$code}($ea); };
last if $@;
$ea=NextFchunk($ea);
}
}
# enumerate all chunks belonging to specified function
sub for_fchunks($&)
{
my ($fea, $code)= @_;
my $ea= FirstFuncFchunk($fea);
while ($ea!=BADADDR) {
eval { &{$code}($ea); };
last if $@;
$ea= NextFuncFchunk($fea, $ea);
}
}
# enumerate all non-funcion blocks of code
sub for_not_func (&)
{
# my ($code)= @_;
# my ($ea_f, $ea_c, $eea);
# if (SelStart()==BADADDR) {
# $ea_f= GetFchunkAttr(FirstSeg(), FUNCATTR_START);
# $ea_f=NextFchunk(FirstSeg()) if ($ea_f==BADADDR);
# $ea_c= FirstSeg();
# $ea_c= NextCode($ea_c) if (GetFlags($ea_c)&MS_CLS)!=FF_CODE;
# $eea= BADADDR;
# }
# else {
# $ea_f= GetFchunkAttr(SelStart(), FUNCATTR_START);
# $ea_f=NextFchunk(SelStart()) if ($ea_f==BADADDR);
# $ea_c= SelStart();
# $ea_c= NextCode($ea_c) if (GetFlags($ea_c)&MS_CLS)!=FF_CODE;
# $eea= SelEnd();
# }
#
# while (($eea==BADADDR || ($ea_c < $eea && $ea_f < $eea)) && $ea_c!=BADADDR && $ea_f!=BADADDR)
# {
# if ($ea_c<$ea_f) {
# eval { &{$code}($ea_c, $ea_f); };
# last if $@;
# }
# $ea_f= NextFchunk($ea_f);
# $ea_c= NextCode(GetFchunkAttr($ea_f, FUNCATTR_END));
#
# }
# TODO: iterate over code gaps between functions
# while ($fcur!=BADADDR) {
# fcode = NextCode(endof($fcur))
# fnext= NextFchunk()
# if (fcode!=fnext) &{$code}($fcode);
# $fcur=$fnext
# }
}
# enum all addresses in selection/file
sub foraddrs (&)
{
my ($code)= @_;
my ($ea, $eea);
if (SelStart()==BADADDR) {
$ea= FirstSeg();
$eea= BADADDR;
}
else {
$ea= SelStart();
$eea= SelEnd();
}
while (($eea==BADADDR || $ea < $eea) && $ea!=BADADDR)
{
eval { &{$code}($ea); }
last if $@;
$ea=NextAddr($ea);
}
}
# enumerate Heads in the selection/file
sub forheads (&)
{
my ($code)= @_;
my ($ea, $eea);
if (SelStart()==BADADDR) {
$ea= FirstSeg();
$eea= BADADDR;
}
else {
$ea= SelStart();
$eea= SelEnd();
}
while (($eea==BADADDR || $ea < $eea) && $ea!=BADADDR)
{
eval { &{$code}($ea); }
last if $@;
$ea=NextHead($ea);
}
}
sub here ()
{
return ScreenEA();
}
# forrefs($ea, "DB", { ... })
# is like for (local $_=DfirstB($ea) ; $_!=BADADDR ; $_=DnextB($ea,$_)) { ... }
# IDA::forrefs(ScreenEA(), "DB", sub { printf("%08lx\n", $ea); });
sub forrefs ($$&)
{
my ($rea, $filter, $code)= @_;
my %reffuncs= (
"R"=>{first=>\&IDC::Rfirst, next=>\&IDC::Rnext},
"RB"=>{first=>\&IDC::RfirstB, next=>\&IDC::RnextB},
"D"=>{first=>\&IDC::Dfirst, next=>\&IDC::Dnext},
"DB"=>{first=>\&IDC::DfirstB, next=>\&IDC::DnextB},
);
my $first= $reffuncs{$filter}{first};
my $next= $reffuncs{$filter}{next};
for (my $ea=&{$first}($rea) ; $ea!=BADADDR ; $ea=&{$next}($rea,$ea)) { &{$code}($ea); }
}
# return list of refs
sub refs($$)
{
my @l;
forrefs(@_, sub { push @l, shift; });
return @l;
}
# enum names in selection/file
sub fornames(&) {
# todo - call ida api
}
# enum segments in selection/file
sub forsegments(&)
{
my ($code)= @_;
my ($ea, $eea);
if (SelStart()==BADADDR) {
$ea= FirstSeg();
$eea= BADADDR;
}
else {
$ea= SegStart(SelStart());
$eea= SelEnd();
}
while (($eea==BADADDR || $ea < $eea) && $ea!=BADADDR)
{
eval { &{$code}($ea); }
last if $@;
$ea=NextSeg($ea);
}
}
1;