Class: Rex::MachParsey::Fat

Inherits:
FatBase
  • Object
show all
Defined in:
lib/rex/machparsey/mach.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(isource, offset = 0) ⇒ Fat

Returns a new instance of Fat.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rex/machparsey/mach.rb', line 107

def initialize(isource, offset = 0)
  self.fat_archs = []
  self.machos = []
  self.isource = isource
  self.fat_header = FatHeader.new(isource.read(offset, FAT_HEADER_SIZE))

  if !self.fat_header
    raise FatHeaderError, "Could not parse FAT header"
  end

  print "Detected " +  self.fat_header.nfat_arch.to_s +  " archs in binary.\n"

  offset += FAT_HEADER_SIZE

  self.fat_header.nfat_arch.times do
    fat_arch = FatArch.new(isource.read(offset, FAT_ARCH_SIZE), self.fat_header.endian)
    self.fat_archs << fat_arch
    self.machos << Mach.new(isource, fat_arch.offset, true)
    offset += FAT_ARCH_SIZE
  end


end

Instance Attribute Details

#fat_archsObject

Returns the value of attribute fat_archs.



105
106
107
# File 'lib/rex/machparsey/mach.rb', line 105

def fat_archs
  @fat_archs
end

#fat_headerObject

Returns the value of attribute fat_header.



105
106
107
# File 'lib/rex/machparsey/mach.rb', line 105

def fat_header
  @fat_header
end

#isourceObject

Returns the value of attribute isource.



105
106
107
# File 'lib/rex/machparsey/mach.rb', line 105

def isource
  @isource
end

#machosObject

Returns the value of attribute machos.



105
106
107
# File 'lib/rex/machparsey/mach.rb', line 105

def machos
  @machos
end

Class Method Details

.new_from_file(filename, disk_backed = false) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rex/machparsey/mach.rb', line 163

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) ⇒ Object



177
178
179
# File 'lib/rex/machparsey/mach.rb', line 177

def self.new_from_string(data)
  return self.new(ImageSource::Memory.new(data))
end

Instance Method Details

#_parse_fat_header(isource, offset) ⇒ Object

this is useful for debugging but we don’t use it for anything.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rex/machparsey/mach.rb', line 132

def _parse_fat_header(isource, offset)
  archs = []
  nfat_arch = self.fat_header.nfat_arch

  print "Number of archs in binary: " + nfat_arch.to_s + "\n"

  nfat_arch.times do
    arch = FatArch.new(isource.read(offset, FAT_ARCH_SIZE), self.endian)

    case arch.cpu_type

    when CPU_TYPE_I386
      print "i386\n"

    when CPU_TYPE_X86_64
      print "x86_64\n"

    when CPU_TYPE_ARM
      print "Arm\n"

    when CPU_TYPE_POWERPC
      print "Power PC\n"

    when CPU_TYPE_POWERPC64
      print "Power PC 64\n"
    end

    offset += FAT_ARCH_SIZE
  end
end

#closeObject



201
202
203
# File 'lib/rex/machparsey/mach.rb', line 201

def close
  isource.close
end

#index(*args) ⇒ Object



197
198
199
# File 'lib/rex/machparsey/mach.rb', line 197

def index(*args)
  isource.index(*args)
end

#ptr_32?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/rex/machparsey/mach.rb', line 185

def ptr_32?
  ptr_64? == false
end

#ptr_64?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/rex/machparsey/mach.rb', line 181

def ptr_64?
  mach_header.bits == BITS_64
end

#ptr_s(vaddr) ⇒ Object



189
190
191
# File 'lib/rex/machparsey/mach.rb', line 189

def ptr_s(vaddr)
  (ptr_32?) ? ("0x%.8x" % vaddr) : ("0x%.16x" % vaddr)
end

#read(offset, len) ⇒ Object



193
194
195
# File 'lib/rex/machparsey/mach.rb', line 193

def read(offset, len)
  isource.read(offset, len)
end