Completed test coverage for pointer_util.rb and fixed the bugs I found

This commit is contained in:
chao-mu 2012-01-08 11:05:24 -05:00
parent f9d123a8c8
commit 6591bd3a45
2 changed files with 67 additions and 23 deletions

View File

@ -20,17 +20,15 @@ module PointerUtil
end
def self.pack_pointer(pointer, platform)
# TODO: Ensure the correct size
unless pointer.kind_of?(Fixnum)
return nil
if pointer.nil?
return pack_pointer(0, platform)
end
case platform
when X86_64
when PlatformUtil::X86_64
# XXX: Only works if attacker and victim are like-endianed
[pointer].pack('Q')
when X86_32
when PlatformUtil::X86_32
[pointer].pack('V')
else
raise "platform symbol #{platform.to_s} not supported"
@ -69,28 +67,28 @@ module PointerUtil
return pointer.nil? || pointer == 0
end
def self.is_unpacked_pointer?(pointer, platform)
# TODO also check that the integer size is appropriate for the platform
unless pointer.kind_of?(Fixnum) and pointer > 0 # and pointer <
return false
end
packed_pointer = pack_pointer(pointer, platform)
if !packed_pointer.nil? and packed_pointer.length == pointer_size(platform)
return true
end
return false
end
#
# def self.is_unpacked_pointer?(pointer, platform)
# # TODO also check that the integer size is appropriate for the platform
# unless pointer.kind_of?(Fixnum) and pointer > 0 # and pointer <
# return false
# end
#
# packed_pointer = pack_pointer(pointer, platform)
# if !packed_pointer.nil? and packed_pointer.length == pointer_size(platform)
# return true
# end
#
# return false
# end
#
# Returns true if the data type is a pointer, false otherwise
def self.is_pointer_type?(type)
if type == :pointer
return true
end
if type.kind_of?(String) && type =~ /^P/
if type.kind_of?(String) && type =~ /^L?P/
return true
end

View File

@ -14,7 +14,7 @@ module Extensions
module Stdapi
module Railgun
module Type
class PointerUtil::UnitTest < Test::Unit::TestCase
class PlatformUtil::UnitTest < Test::Unit::TestCase
include Rex::Post::Meterpreter::Extensions::Stdapi::Railgun::MockMagic
@ -43,6 +43,30 @@ class PointerUtil::UnitTest < Test::Unit::TestCase
X86_64 = PlatformUtil::X86_64
X86_32 = PlatformUtil::X86_32
def test_pack_pointer
X86_64_POINTERS.invert.each_pair do |unpacked, packed|
assert_equal(packed, PointerUtil.pack_pointer(unpacked.to_i, X86_64),
"pack_pointer should pack 64-bit numberic pointers")
end
X86_32_POINTERS.invert.each_pair do |unpacked, packed|
assert_equal(packed, PointerUtil.pack_pointer(unpacked.to_i, X86_32),
"pack_pointer should pack 32-bit numberic pointers")
end
assert_equal(X86_64_NULL_POINTER, PointerUtil.pack_pointer(nil, X86_64),
'pack_pointer should pack "nil" as a null pointer for x86_64')
assert_equal(X86_32_NULL_POINTER, PointerUtil.pack_pointer(nil, X86_32),
'pack_pointer should pack "nil" as a null pointer for x86_32')
assert_equal(X86_64_NULL_POINTER, PointerUtil.pack_pointer(0, X86_64),
'pack_pointer should pack numeric 0 as a null pointer for x86_64')
assert_equal(X86_32_NULL_POINTER, PointerUtil.pack_pointer(0, X86_32),
'pack_pointer should pack numeric 9 as a null pointer for x86_32')
end
def test_unpack_pointer
X86_64_POINTERS.each_pair do |packed, unpacked|
assert_equal(unpacked, PointerUtil.unpack_pointer(packed, X86_64),
@ -53,6 +77,8 @@ class PointerUtil::UnitTest < Test::Unit::TestCase
assert_equal(unpacked, PointerUtil.unpack_pointer(packed, X86_32),
"unpack_pointer should unpack 32-bit pointers")
end
end
def test_is_null_pointer
@ -70,6 +96,26 @@ class PointerUtil::UnitTest < Test::Unit::TestCase
end
def test_pointer_size
assert_equal(8, PointerUtil.pointer_size(X86_64),
'pointer_size should report X86_64 arch as 8 (bytes)')
assert_equal(4, PointerUtil.pointer_size(X86_32),
'pointer_size should report X86_32 arch as 4 (bytes)')
end
def test_is_pointer_type
assert_equal(true, PointerUtil.is_pointer_type?(:pointer),
'pointer_type should return true for the symbol :pointer')
assert_equal(true, PointerUtil.is_pointer_type?('LPVOID'),
'pointer_type should return true if string begins with LP')
assert_equal(true, PointerUtil.is_pointer_type?('PDWORD'),
'pointer_type should return true if string begins with P')
assert_equal(false, PointerUtil.is_pointer_type?('LOLZ'),
'pointer_type should return false if not a pointer type')
end
end
end