Class: Msf::Payload::MachO

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/payload/macho.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ MachO

Returns a new instance of MachO.



6
7
8
# File 'lib/msf/core/payload/macho.rb', line 6

def initialize(data)
  @macho = MachO::MachOFile.new_from_bin(data)
end

Instance Method Details

#entrypointObject



10
11
12
13
# File 'lib/msf/core/payload/macho.rb', line 10

def entrypoint
  main_func = @macho[:LC_MAIN].first
  main_func.entryoff
end

#flattenObject

Return the VM respresentation of a macho file



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
# File 'lib/msf/core/payload/macho.rb', line 18

def flatten
  raw_data = @macho.serialize
  min = -1
  max = 0
  for segment in @macho.segments
    next if segment.segname == MachO::LoadCommands::SEGMENT_NAMES[:SEG_PAGEZERO]
    if min == -1 or min > segment.vmaddr
      min = segment.vmaddr
    end
    if max < segment.vmaddr + segment.vmsize
      max = segment.vmaddr + segment.vmsize
    end
  end

  output_data = "\x00" * (max - min)
  for segment in @macho.segments
    for section in segment.sections
      flat_addr = section.addr - min
      section_data = raw_data[section.offset, section.size]
      if section_data
        output_data[flat_addr, section_data.size] = section_data
      end
    end
  end

  output_data
end

#rawObject



56
57
58
# File 'lib/msf/core/payload/macho.rb', line 56

def raw
  @macho.serialize
end

#to_dylib(name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/msf/core/payload/macho.rb', line 46

def to_dylib(name)
  new_lc = MachO::LoadCommands::LoadCommand.create(:LC_ID_DYLIB, "@executable_path/#{name}.dylib", 0, 0, 0)
  @macho.add_command(new_lc)

  raw_data = @macho.serialize
  raw_data[12] = MachO::Headers::MH_DYLIB.chr
  raw_data[36,7] = "__ZERO\x00"
  raw_data
end