class Rex::MachParsey::Mach

Attributes

arch[RW]
bits[RW]
endian[RW]
fat_offset[RW]
isource[RW]
mach_header[RW]
segments[RW]

Public Class Methods

new(isource, offset = 0, fat = false) click to toggle source
# File lib/rex/machparsey/mach.rb, line 13
def initialize(isource, offset = 0, fat = false)
  _parse_mach_header(isource, offset)
  if fat == true
    self.fat_offset = offset
  else
    self.fat_offset = 0
  end

  self.isource = isource
end
new_from_file(filename, disk_backed = false) click to toggle source
# File lib/rex/machparsey/mach.rb, line 60
def self.new_from_file(filename, disk_backed = false)

  file = ::File.open(filename, "rb")

  if disk_backed
    return self.new(ImageSource::Disk.new(file))
  else
    obj = new_from_string(file.read)
    file.close
    return obj
  end
end
new_from_string(data) click to toggle source
# File lib/rex/machparsey/mach.rb, line 73
def self.new_from_string(data)
  return self.new(ImageSource::Memory.new(data))
end

Public Instance Methods

_parse_mach_header(isource, offset) click to toggle source
# File lib/rex/machparsey/mach.rb, line 24
def _parse_mach_header(isource, offset)
  self.mach_header = MachHeader.new(isource.read(offset, MACH_HEADER_SIZE_64))
  bits = mach_header.bits
  endian = mach_header.endian
  ncmds = mach_header.ncmds

  if bits == BITS_32
    offset += MACH_HEADER_SIZE
  else
    offset += MACH_HEADER_SIZE_64
  end


  segments = []
  ncmds.times do
    load_command = LoadCommand.new(isource.read(offset, LOAD_COMMAND_SIZE), endian)

    case load_command.cmd
      when LC_SEGMENT
        segments << Segment.new(isource.read(offset, SEGMENT_COMMAND_SIZE), bits, endian)
      when LC_SEGMENT_64
        segments << Segment.new(isource.read(offset, SEGMENT_COMMAND_SIZE_64), bits, endian)
    end

    offset += load_command.cmdsize
  end

  self.mach_header = mach_header
  self.segments = segments
  self.isource = isource
  self.bits = bits
  self.endian = endian

  return segments
end
close() click to toggle source
# File lib/rex/machparsey/mach.rb, line 97
def close
  isource.close
end
index(*args) click to toggle source
# File lib/rex/machparsey/mach.rb, line 93
def index(*args)
  isource.index(*args)
end
ptr_32?() click to toggle source
# File lib/rex/machparsey/mach.rb, line 81
def ptr_32?
  ptr_64? == false
end
ptr_64?() click to toggle source
# File lib/rex/machparsey/mach.rb, line 77
def ptr_64?
  mach_header.bits == BITS_64
end
ptr_s(vaddr) click to toggle source
# File lib/rex/machparsey/mach.rb, line 85
def ptr_s(vaddr)
  (ptr_32?) ? ("0x%.8x" % vaddr) : ("0x%.16x" % vaddr)
end
read(offset, len) click to toggle source
# File lib/rex/machparsey/mach.rb, line 89
def read(offset, len)
  isource.read(fat_offset + offset, len)
end