Updated ELF stuff

git-svn-id: file:///home/svn/framework3/trunk@5413 4d416f70-5f16-0410-b530-b9f4589650da
This commit is contained in:
Ramon de C Valle 2008-02-13 02:43:56 +00:00
parent ffe2fa80d9
commit e39c053f76
12 changed files with 75 additions and 45 deletions

View File

@ -8,5 +8,4 @@ module ElfParsey
end end
end end
require 'rex/elfparsey/exceptions'
require 'rex/elfparsey/elf' require 'rex/elfparsey/elf'

View File

@ -19,7 +19,9 @@ class Elf < ElfBase
# ELF Header # ELF Header
elf_header = ElfHeader.new(isource.read(offset, ELF_HEADER_SIZE)) elf_header = ElfHeader.new(isource.read(offset, ELF_HEADER_SIZE))
# Data encoding
ei_data = elf_header.e_ident[EI_DATA] ei_data = elf_header.e_ident[EI_DATA]
e_phoff = elf_header.e_phoff e_phoff = elf_header.e_phoff
e_phentsize = elf_header.e_phentsize e_phentsize = elf_header.e_phentsize
e_phnum = elf_header.e_phnum e_phnum = elf_header.e_phnum
@ -46,8 +48,6 @@ class Elf < ElfBase
self.isource = isource self.isource = isource
end end
# Stolen from lib/rex/peparsey/pebase.rb
def self.new_from_file(filename, disk_backed = false) def self.new_from_file(filename, disk_backed = false)
file = ::File.new(filename) file = ::File.new(filename)
@ -62,21 +62,36 @@ class Elf < ElfBase
end end
end end
# Stolen from lib/rex/peparsey/pebase.rb
def self.new_from_string(data) def self.new_from_string(data)
return self.new(ImageSource::Memory.new(data)) return self.new(ImageSource::Memory.new(data))
end end
# Stolen from lib/rex/peparsey/pe.rb #
# Returns true if this binary is for a 64-bit architecture.
#
def ptr_64?
unless [ ELFCLASS32, ELFCLASS64 ].include?(
elf_header.e_ident[EI_CLASS])
raise ElfHeaderError, 'Invalid class', caller
end
elf_header.e_ident[EI_CLASS] == ELFCLASS64
end
#
# Returns true if this binary is for a 32-bit architecture.
# This check does not take into account 16-bit binaries at the moment.
#
def ptr_32?
ptr_64? == false
end
# #
# Converts a virtual address to a string representation based on the # Converts a virtual address to a string representation based on the
# underlying architecture. # underlying architecture.
# #
def ptr_s(va) def ptr_s(rva)
#(ptr_32?) ? ("0x%.8x" % va) : ("0x%.16x" % va) (ptr_32?) ? ("0x%.8x" % rva) : ("0x%.16x" % rva)
"0x%.8x" % va
end end
def offset_to_rva(offset) def offset_to_rva(offset)
@ -95,14 +110,14 @@ class Elf < ElfBase
isource.read(rva_to_offset(rva), len) isource.read(rva_to_offset(rva), len)
end end
def close
isource.close
end
def index(*args) def index(*args)
isource.index(*args) isource.index(*args)
end end
def close
isource.close
end
end end
end end
end end

View File

@ -99,9 +99,11 @@ class ElfBase
# #
ELFMAG0 = 0x7f # e_ident[EI_MAG0] ELFMAG0 = 0x7f # e_ident[EI_MAG0]
ELFMAG1 = 'E' # e_ident[EI_MAG1] ELFMAG1 = ?E # e_ident[EI_MAG1]
ELFMAG2 = 'L' # e_ident[EI_MAG2] ELFMAG2 = ?L # e_ident[EI_MAG2]
ELFMAG3 = 'F' # e_ident[EI_MAG3] ELFMAG3 = ?F # e_ident[EI_MAG3]
ELFMAG = ELFMAG0.chr + ELFMAG1.chr + ELFMAG2.chr + ELFMAG3.chr
# EI_CLASS Identifies the file's class, or capacity # EI_CLASS Identifies the file's class, or capacity
@ -118,8 +120,6 @@ class ElfBase
ELFDATA2LSB = 1 # Least significant byte first ELFDATA2LSB = 1 # Least significant byte first
ELFDATA2MSB = 2 # Most significant byte first ELFDATA2MSB = 2 # Most significant byte first
# Stolen from lib/rex/peparsey/pebase.rb
class GenericStruct class GenericStruct
attr_accessor :struct attr_accessor :struct
def initialize(_struct) def initialize(_struct)
@ -154,6 +154,8 @@ class ElfBase
class ElfHeader < GenericHeader class ElfHeader < GenericHeader
def initialize(rawdata) def initialize(rawdata)
# Identify the data encoding and parse ELF Header
elf_header = ELF32_EHDR_LSB.make_struct elf_header = ELF32_EHDR_LSB.make_struct
if !elf_header.from_s(rawdata) if !elf_header.from_s(rawdata)
@ -168,16 +170,14 @@ class ElfBase
end end
end end
unless elf_header.v['e_ident'][EI_DATA] == ELFDATA2LSB || unless [ ELFDATA2LSB, ELFDATA2MSB ].include?(
elf_header.v['e_ident'][EI_DATA] == ELFDATA2MSB elf_header.v['e_ident'][EI_DATA])
raise ElfHeaderError, 'Invalid data encoding', caller raise ElfHeaderError, 'Invalid data encoding', caller
end end
unless elf_header.v['e_ident'][EI_MAG0].to_i == ELFMAG0 && # Identify the file as an ELF object file
elf_header.v['e_ident'][EI_MAG1] == ELFMAG1 && unless elf_header.v['e_ident'][EI_MAG0, 4] == ELFMAG
elf_header.v['e_ident'][EI_MAG2] == ELFMAG2 && raise ElfHeaderError, 'Invalid magic number', caller
elf_header.v['e_ident'][EI_MAG3] == ELFMAG3
#raise ElfHeaderError, 'Invalid magic number', caller
end end
self.struct = elf_header self.struct = elf_header
@ -237,6 +237,8 @@ class ElfBase
class ProgramHeader < GenericHeader class ProgramHeader < GenericHeader
def initialize(rawdata, ei_data) def initialize(rawdata, ei_data)
# Identify the data encoding and parse Program Header
if ei_data == ELFDATA2LSB if ei_data == ELFDATA2LSB
program_header = ELF32_PHDR_LSB.make_struct program_header = ELF32_PHDR_LSB.make_struct
elsif ei_data == ELFDATA2MSB elsif ei_data == ELFDATA2MSB
@ -256,4 +258,4 @@ class ElfBase
end end
end end
end end

View File

@ -24,5 +24,4 @@ class WtfError < ElfError
end end
end end
end end

View File

@ -204,4 +204,4 @@ end
end end
end end
end end

View File

@ -43,4 +43,4 @@ module Search
end end
end end
end end
end end

View File

@ -2,6 +2,11 @@
# $Id$ # $Id$
require 'rex/image_source/image_source.rb' module Rex
require 'rex/image_source/memory.rb' module ImageSource
require 'rex/image_source/disk.rb'
end
end
require 'rex/image_source/disk'
require 'rex/image_source/memory'

View File

@ -1,5 +1,12 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'rex/peparsey/exceptions' # $Id$
module Rex
module PeParsey
end
end
require 'rex/peparsey/pe' require 'rex/peparsey/pe'
require 'rex/peparsey/pe_memdump' require 'rex/peparsey/pe_memdump'

View File

@ -2,10 +2,10 @@
# $Id$ # $Id$
require 'rex/peparsey/pebase'
require 'rex/peparsey/exceptions'
require 'rex/peparsey/section'
require 'rex/image_source' require 'rex/image_source'
require 'rex/peparsey/exceptions'
require 'rex/peparsey/pebase'
require 'rex/peparsey/section'
require 'rex/struct2' require 'rex/struct2'
module Rex module Rex

View File

@ -2,10 +2,10 @@
# $Id$ # $Id$
require 'rex/peparsey/pebase'
require 'rex/peparsey/exceptions'
require 'rex/peparsey/section'
require 'rex/image_source' require 'rex/image_source'
require 'rex/peparsey/exceptions'
require 'rex/peparsey/pebase'
require 'rex/peparsey/section'
require 'rex/struct2' require 'rex/struct2'
# #

View File

@ -2,8 +2,8 @@
# $Id$ # $Id$
require 'rex/peparsey/pebase'
require 'rex/peparsey/exceptions' require 'rex/peparsey/exceptions'
require 'rex/peparsey/pebase'
require 'rex/struct2' require 'rex/struct2'
module Rex module Rex

View File

@ -1,3 +1,6 @@
#!/usr/bin/env ruby
# $Id$
module Rex module Rex
module PeScan module PeScan
@ -5,6 +8,6 @@ module PeScan
end end
end end
require 'rex/pescan/scanner'
require 'rex/pescan/search'
require 'rex/pescan/analyze' require 'rex/pescan/analyze'
require 'rex/pescan/scanner'
require 'rex/pescan/search'