This repository was archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmsfpayload
More file actions
executable file
·177 lines (137 loc) · 3.74 KB
/
Copy pathmsfpayload
File metadata and controls
executable file
·177 lines (137 loc) · 3.74 KB
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
#!/usr/bin/perl
###############
##
# Name: msfpayload
# Author: H D Moore <hdm [at] metasploit.com>
# Version: $Revision$
# Description: Command line interface for generating Metasploit payloads
# License:
#
# This file is part of the Metasploit Exploit Framework
# and is subject to the same licenses and copyrights as
# the rest of this package.
#
##
require 5.6.0;
use strict;
use FindBin qw{$RealBin};
use lib "$RealBin/lib";
use Getopt::Std;
use POSIX;
use Msf::TextUI;
use Pex;
no utf8;
no locale;
Msf::UI::ActiveStateSucks();
Msf::UI::BrokenUTF8();
my $ui = Msf::TextUI->new($RealBin);
my $FRAMEVERSION = $ui->Version;
my $VERSION = '$Revision$';
my %opts;
getopts('hv', \%opts);
Version() if($opts{'v'});
$ui->SetTempEnv('_MsfPayload', 1);
$ui->SetTempEnv('DebugLevel', 0);
my $exploits = { };
my $payloads = { };
my $payloadsIndex = $ui->LoadPayloads;
foreach my $key (keys(%{$payloadsIndex})) {
$payloads->{$payloadsIndex->{$key}->SelfEndName} = $payloadsIndex->{$key};
}
$ui->SetTempEnv('_Payloads', $payloadsIndex);
my $sel = shift(@ARGV);
my $p = $payloads->{$sel};
Usage() if($opts{'h'});
Usage() if ! $p;
my $action = uc(pop(@ARGV));
foreach my $opt (@ARGV) {
$ui->SetTempEnv(split('=', $opt));
}
$p->_Load;
$ui->SetTempEnv('_PayloadName', $sel);
$ui->SetTempEnv('_Payload', $p);
if (! $action || $action =~ /^S/)
{
print "\n" . $ui->DumpPayloadSummary($p);
exit(0);
}
Usage() if $action !~ /^(C|P|R|X|J)/;
if ($action =~ /^R/) { print $p->Build; exit; }
if ($p->Multistage) {
print STDERR "Warning: Multistage payloads only return first stage\n";
}
if ($action =~ /^X/) {
my (%pos, %parch);
map { $pos{$_}++ } @{ $p->OS };
map { $parch{$_}++ } @{ $p->Arch };
# Generate a PE image for Windows payloads
if ($pos{'win32'} && $parch{'x86'}) {
ExportWinPE();
}
# Generate a shell script if there is no architecture
if (! scalar(keys(%parch))) {
print "#!/bin/sh\n" . $p->Build;
exit(0);
}
print STDERR "Error: No export format is implemented for this payload\n";
exit(0);
}
# Needs to
if ($action =~ /^J/) {
my $end = 'LE';
my (%pos, %parch);
map { $pos{$_}++ } @{ $p->OS };
map { $parch{$_}++ } @{ $p->Arch };
if (! scalar(keys(%parch)) || $parch{'x86'}) {
$end = 'LE';
} else {
$end = 'BE';
}
my $out = '';
$out .= "// Created by msfpayload, a component of the Metasploit Framework ($FRAMEVERSION)\n";
$out .= "// This variable contains the ".$p->SelfEndName." payload\n";
$out .= "// Options:\n";
$out .= "//\tEndian=".$end."\n";
foreach (keys %{ $ui->GetTempEnv() }) {
next if $_ =~ /^_/;
$out .= "//\t" . $_ ."=". $ui->GetTempEnv($_)."\n";
}
$out .= "var shellcode = unescape('";
$out .= Pex::Utils::JSUnescape($p->Build, $end);
$out .= "');\n";
print STDOUT $out;
exit(0);
}
my $r = $action =~ /^C/ ? Pex::Text::BufferC($p->Build) : Pex::Text::BufferPerl($p->Build);
print $r;
exit(0);
sub Usage
{
print STDERR "\n Usage: $0 <payload> [var=val] <S|C|P|R|X>\n\n";
print STDERR "Payloads: \n";
print STDERR $ui->DumpPayloads(2, $payloads);
print STDERR "\n";
exit(0);
}
sub ExportWinPE {
# Comments are limited to 512 bytes
# Payloads are limited to 8192 bytes
my $bin = $p->Build;
my $com = "Created by msfpayload, a component of the Metasploit Framework ($FRAMEVERSION). ".
"This executable contains the ".$p->SelfEndName." payload, ".
"generated with the following set of options: ";
foreach (keys %{ $ui->GetTempEnv() }) {
next if $_ =~ /^_/;
$com .= $_ ."=". $ui->GetTempEnv($_)." ";
}
print STDOUT Pex::Utils::CreateWin32PE($bin, $com);
exit(0);
}
sub Version {
my $ver = Pex::Utils::Rev2Ver($VERSION);
print STDERR qq{
Framework Version: $FRAMEVERSION
Msfpayload Version: $ver
};
exit(0);
}