Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/shellcode/windows/build.sh
Views: 11779
#!/usr/bin/perl1###############23##4# Name: build5# Author: H D Moore <hdm [at] metasploit.com>6# Description: Command-line tool for building/extracting asm payloads7# License: GPL / Perl Artistic8##91011my $name = shift();1213if (! $name || $name =~ /\./) { print STDERR "Usage: $0 <name>\n"; exit(0); }1415if ($name eq 'clean') {16system("rm -f *.bin *.exe *.c *.elf");17exit(0);18}192021# Compile the asm22unlink("$name.bin");23system("nasm -f bin -O3 -o $name.bin $name.asm");2425if (! -f "$name.bin") {26exit(0);27}2829# Load binary30my $bindata;31open(X, "<$name.bin") || exit(0);32$bindata = join('',<X>);33close(X);3435print "# Length: " . length($bindata) . " bytes\n";363738# Print out common offsets into the payload data39my $suffix;40my $port = index($bindata, pack("n", 8721));41if ($port != -1) {42print "# Port: $port\n";43}4445my $host = index($bindata, gethostbyname("127.0.0.1"));46if ($host != -1) {47print "# Host: $host\n";48}4950my $psize = index($bindata, pack("L", 0x12345678));51if ($psize != -1) {52print "# Size: $psize\n";53}5455my $pstart = index($bindata, pack("L", 0x13370000));56if ($pstart != -1) {57print "# Start: $pstart\n";58}5960my $pstart = index($bindata, pack("L", 0x11223344));61if ($pstart != -1) {62print "# Alloc: $pstart\n";63}6465my $pstart = index($bindata, pack("L", 0x73e2d87e));66if ($pstart != -1) {67print "# ExitProcess: $pstart\n";68}6970my $pstart = index($bindata, pack("L", 0x4cf079fa));71if ($pstart != -1) {72print "# PayloadLen: $pstart\n";73}7475my $pstart = index($bindata, "\x8d\x77\x15");76if ($pstart != -1) {77$pstart+=2;78print "# FileStart: $pstart\n";79}8081my $pstart = index($bindata, "\x88\x4f\x1a");82if ($pstart != -1) {83$pstart+=2;84print "# FileEnd: $pstart\n";85}8687my $pstart = index($bindata, "http");88if ($pstart != -1) {89print "# URL Start: $pstart\n";90}919293$x = BufferPerl($bindata);94print $x;9596$x = BufferC($bindata);97my $cfile;98while(<DATA>) { $cfile .= $_; }99100$cfile =~ s/::SHELLCODE::/$x/g;101102open(C, ">$name.c");103print C $cfile;104close (C);105106# Build PE107open (X, ">templates/payload.bin") || die "payload.bin: $!";108print X $bindata;109close (X);110111chdir("templates") || die "chdir(templates): $!";112unlink("../$name.exe");113system("nasm -I inc/ -f bin -o ../$name.exe win32_template.asm");114115# Build ELF116unlink("linux_template.o");117system("nasm -f elf -o linux_template.o linux_template.asm");118if (-f "linux_template.o")119{120system("ld -o ../$name.elf linux_template.o");121unlink("linux_template.o");122}123124unlink("payload.bin");125system("chmod 755 *.exe *.elf");126127sub BufferPerl128{129my ($data, $width) = @_;130my ($res, $count);131132if (! $data) { return }133if (! $width) { $width = 16 }134135$res = '"';136137$count = 0;138foreach my $char (split(//, $data))139{140if ($count == $width)141{142$res .= '" + ' . "\n" . '"';143$count = 0;144}145$res .= sprintf("\\x%.2x", ord($char));146$count++;147}148if ($count) { $res .= '"' . "\n"; }149return $res;150}151152sub BufferC153{154my ($data, $width) = @_;155my $res = BufferPerl($data, $width);156if (! $res) { return }157158$res =~ s/\.//g;159return $res;160}161162__DATA__163164char code[] =165::SHELLCODE::166167int main(int argc, char **argv)168{169int (*funct)();170funct = (int (*)()) code;171(int)(*funct)();172}173174175