-
Notifications
You must be signed in to change notification settings - Fork 5
/
bootstrap.pl
executable file
·54 lines (44 loc) · 1.2 KB
/
bootstrap.pl
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
#!/usr/bin/perl
use strict;
use Getopt::Long;
use File::Temp qw/tempfile/;
my $_ELF;
my $_TEMPLATE;
my $_OUTPUT;
GetOptions(
"elf=s" => \$_ELF
, "template=s" => \$_TEMPLATE
, "output=s" => \$_OUTPUT
);
if( !$_ELF or !$_TEMPLATE or !$_OUTPUT){
die("usage: $0 -e elfFile -t bootstrap.s -o payload\n");
}
#read the entire elf and template into memory
my $elfData;
my $template;
{
local $/ = undef;
open FILE, "$_ELF" or die "Couldn't open file: $!";
binmode FILE;
$elfData = <FILE>;
close FILE;
open FILE, "$_TEMPLATE" or die "Couldn't open file: $!";
binmode FILE;
$template = <FILE>;
close FILE;
}
#encode the elf into something gas can understand
my $elfSize = length $elfData;
#my @encodedElf = ;
my $encodedElf = ".byte " . join(",", map {ord($_)+""} split(//,$elfData));
$template =~ s/\<_elf_size\>/.int $elfSize/g;
$template =~ s/\<_elf_data\>/$encodedElf/g;
my ($asmFH,$asmFilePath) = tempfile("keebler-bootstrapXXXXXXXX", DIR=>"/tmp");
my ($o,$oFilePath) = tempfile("keebler-bootstrapXXXXXXXX", DIR=>"/tmp");
close($o);
print $asmFH $template;
close($asmFH);
print `as $asmFilePath -o $oFilePath`;
unlink($asmFilePath);
print `objcopy -O binary $oFilePath $_OUTPUT`;
unlink($oFilePath);