-
Notifications
You must be signed in to change notification settings - Fork 2
/
libc-offsets
executable file
·68 lines (58 loc) · 1.37 KB
/
libc-offsets
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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw/:config posix_default no_ignore_case bundling permute/;
my %opt = (
func => '',
str => '',
unique => 0,
);
GetOptions(
'func=s' => \$opt{func},
'str=s' => \$opt{str},
'u|unique!' => \$opt{unique},
) or usage();
my $file = shift or usage();
my $syms = `readelf -s $file`;
my $offset = `strings -tx $file`;
my (%func, %str);
offset('__libc_start_main');
offset('system');
offset_str('/bin/sh');
offset($_) for split /,/, $opt{func};
offset_str($_) for split /,/, $opt{str};
print "offset = {\n";
if (!$opt{unique}) {
%func = invert_hash(invert_hash(%func));
%str = invert_hash(invert_hash(%str));
}
for my $k (sort { $func{$a} cmp $func{$b} or $a <=> $b } keys %func) {
printf " '%s': 0x%x,\n", $func{$k}, $k;
}
for my $k (sort { $str{$a} cmp $str{$b} or $a <=> $b } keys %str) {
printf " '%s': 0x%x, # str\n", $str{$k}, $k;
}
print "}\n";
sub invert_hash {
my (%a) = @_;
my %ret;
for my $k (keys %a) {
$ret{$a{$k}} = $k;
}
return %ret;
}
sub offset {
my ($func) = @_;
while ($syms =~ /\d+:\s+([0-9a-f]+).+FUNC.+\s+$func@@/g) {
$func{hex $1} = $func;
}
}
sub offset_str {
my ($str) = @_;
while ($offset =~ /\s+([0-9a-f]+)\s+$str/g) {
$str{hex $1} = $str;
}
}
sub usage {
die "Usage: $0 FILE\n";
}