-
Notifications
You must be signed in to change notification settings - Fork 2
/
gadgets
executable file
·147 lines (136 loc) · 5.17 KB
/
gadgets
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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw/:config posix_default no_ignore_case bundling permute/;
my $RP = 'rp-lin-x64';
my $READELF = 'readelf';
my $RE_REGISTER = qr/((?:r(?:1(?:0[dlw]?|1[dlw]?|2[dlw]?|3[dlw]?|4[dlw]?|5[dlw]?)|8[dlw]?|9[dlw]?|[ac]x|b[px]|d[ix]|s[ip])|e(?:[ac]x|b[px]|d[ix]|s[ip])|b(?:[hlx]|pl?)|d(?:[hlx]|il?)|s(?:il?|pl?)|a[hlx]|c[hlx]))/;
my $RE_IS_DWORD = qr/(?^:e(?:[ac]x|b[px]|d[ix]|s[ip]|ip);)/;
my $RE_IS_QWORD = qr/(?^:r(?:[89]|1[012345]|[ac]x|b[px]|d[ix]|s[ip]|ip))/;
my %opt = (
rop => 6,
unique => 0,
);
GetOptions(
'r|rop=s' => \$opt{rop},
'u|unique!' => \$opt{unique},
'carop' => \my $carop,
's|style' => \my $style,
) or usage();
$opt{file} = shift or usage();
my @args;
for my $key (sort keys %opt) {
if (defined $opt{$key}) {
if ($key eq 'unique') {
if (!$opt{$key}) {
push @args, "--$key";
}
} else {
push @args, "--$key=$opt{$key}";
}
}
}
my $cmd_args = join ' ', @args;
my $out = `$RP $cmd_args`;
my @gadgets; # [order1, order2, gadget]
my %add_sp_gadgets;
while ($out =~ /\033\[91m0x([0-9a-f]+)\033\[0m:\s+\033\[92m(.+)\s+\(.+\)/gm) {
my $addr = $1;
my @op = split /\s+;\s+/, $2;
if ($op[-1] eq 'ret') {
if (@op == 1) {
push @gadgets, [0, 1, sprintf 'ret = 0x%x', hex($addr)];
} elsif (@op == 2) {
if ($op[0] =~ /(syscall|int 0x80)/) {
if ($1 eq 'int 0x80') {
push @gadgets, [0, 0, sprintf 'int80 = 0x%x', hex($addr)];
} else {
push @gadgets, [0, 0, sprintf 'syscall = 0x%x', hex($addr)];
}
} elsif ($op[0] eq 'leave') {
push @gadgets, [0, 2, sprintf 'leaveret = 0x%x', hex($addr)];
} elsif ($op[0] =~ /mov $RE_REGISTER, $RE_REGISTER/) {
push @gadgets, [3, 0, sprintf 'mov_%s_%s = 0x%x', $1, $2, hex($addr)];
} elsif ($op[0] =~ /xchg $RE_REGISTER, $RE_REGISTER/) {
push @gadgets, [3, 1, sprintf 'xchg_%s_%s = 0x%x', $1, $2, hex($addr)];
} elsif ($op[0] =~ /(add|sub|xor) $RE_REGISTER, $RE_REGISTER/) {
push @gadgets, [4, 0, sprintf '%s_%s_%s = 0x%x', $1, $2, $3, hex($addr)];
} elsif ($op[0] =~ /(add|sub) ([re][sb]p), [qd]word \[([re][sb]p)([+-])0x([0-9A-F]+)\]/) {
push @gadgets, [4, 1, sprintf '%s_%s_%s%s%s = 0x%x', $1, $2, $3, ($4 eq '-' ? 'N' : ''), hex($5), hex($addr)];
} elsif ($op[0] =~ /(inc|dec) $RE_REGISTER/) {
push @gadgets, [6, 0, sprintf '%s_%s = 0x%x', $1, $2, hex($addr)];
} elsif ($carop && $op[0] =~ /add [qd]word \[([re](?:[sb]p|bx))([+-])0x([0-9A-F]+)\], $RE_REGISTER/) {
push @gadgets, [10, 0, sprintf 'add_%s_%sx%s_%s = 0x%x # carop gadget', $1, ($2 eq '-' ? 'N' : ''), $3, $4, hex($addr)];
}
}
# pop xxx; pop xxx; ...
my @reg;
for my $i (0..@op-2) {
if ($op[$i] =~ /pop $RE_REGISTER/) {
push @reg, $1;
} elsif ($op[$i] =~ /leave/) {
push @reg, 'leaveret';
} else {
last;
}
if ($i == @op-2) {
if ($reg[-1] eq 'leaveret') {
if (@op > 2) {
push @gadgets, [2, scalar @reg, sprintf 'pop_%s = 0x%x', join('_', @reg), hex($addr)];
}
} else {
if ($style) {
push @gadgets, [1, 0, sprintf 'pop%sret = 0x%x', (scalar @reg > 1 ? scalar @reg : ''), hex($addr)];
} else {
push @gadgets, [1, scalar @reg, sprintf 'pop_%s = 0x%x', join('_', @reg), hex($addr)];
}
}
}
}
my $sp = 0;
for my $i (0..@op-2) {
if ($op[$i] =~ /pop $RE_REGISTER/) {
my $reg = $1;
if ($reg =~ $RE_IS_DWORD) {
$sp += 4;
} elsif ($reg =~ $RE_IS_QWORD) {
$sp += 8;
} else {
$sp = 0;
last;
}
} elsif ($op[$i] =~ /add [er]sp, 0x([0-9A-F]+)/) {
$sp += hex($1);
} else {
$sp = 0;
last;
}
}
if ($sp > 0) {
if (!$opt{unique}) {
$add_sp_gadgets{$sp} = $addr;
} else {
push @gadgets, [5, 0, sprintf 'add_sp_%s = 0x%x', $sp, hex($addr)];
}
}
}
}
for my $sp (keys %add_sp_gadgets) {
push @gadgets, [6, $sp, sprintf 'add_sp_%s = 0x%x', $sp, hex($add_sp_gadgets{$sp})];
}
print join("\n", map { $_->[2] } sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] or $a->[2] cmp $b->[2] } @gadgets) . "\n";
my ($bss) = `$READELF -S $opt{file} | grep .bss` =~ /\s*\[\d+\]\s+\.bss\s+NOBITS\s+([0-9a-f]+)\s+/;
printf "\nbss = 0x%x+0x300\n", hex $bss;
sub usage {
die <<"USAGE";
Usage: $0 FILE
-r, --rop=SIZE
specity gadget size
-u, --unique
show all gadgets
--carop
show carop gaget
-s, --style
pop_ebp -> pop2ret
USAGE
}