1
mirror of https://github.com/rapid7/metasploit-framework synced 2024-11-05 14:57:30 +01:00

ya ya encodin

git-svn-id: file:///home/svn/incoming/trunk@2512 4d416f70-5f16-0410-b530-b9f4589650da
This commit is contained in:
Spoon M 2005-05-24 03:58:47 +00:00
parent f8c10af0ef
commit 54daa98c67
10 changed files with 208 additions and 134 deletions

18
lib/rex/encoding/xor.rb Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/ruby
#
# make sure the namespace is created
#
module Rex
module Encoding
module Xor
end end end
#
# include the Xor encodings
#
require 'Rex/Encoding/Xor/Generic'
require 'Rex/Encoding/Xor/Byte'
require 'Rex/Encoding/Xor/Word'

View File

@ -0,0 +1,13 @@
#!/usr/bin/ruby
$:.unshift(File.join('..', '..', File.dirname(__FILE__)))
#
# Xor Encoding Test Suite
#
require 'test/unit'
require 'Rex/Encoding/Xor/Generic.ut'
require 'Rex/Encoding/Xor/Byte.ut'
require 'Rex/Encoding/Xor/Word.ut'
require 'Rex/Encoding/Xor/DWord.ut'

View File

@ -1,12 +1,14 @@
#!/usr/bin/ruby
require 'Rex/Encoding/Xor/Generic'
module Rex
module Encoding
module Xor
class Byte < Generic
def keysize
def Byte.keysize
1
end

View File

@ -0,0 +1,29 @@
#!/usr/bin/ruby
$:.unshift(File.join('..', '..', '..', File.dirname(__FILE__)))
require 'Rex/Encoding/Xor/Byte'
require 'Rex/Encoding/Xor/Generic.ut'
require 'test/unit'
require 'test/unit/ui/console/testrunner'
#
# I suck because I want to inherit a test case, but this will
# also cause it to run the test case I'm inheriting, so this runs both the
# Byte and Generic tests, oh well for now...
#
class Rex::Encoding::Xor::Byte::UnitTest < Rex::Encoding::Xor::Generic::UnitTest
def enc
Rex::Encoding::Xor::Byte
end
def hook_static_encode(data, key, expected)
if key.length != enc.keysize
assert_raise(ArgumentError) { enc.encode(data, key) }
else
enc.encode(data, key)
end
end
end

View File

@ -1,13 +1,21 @@
#!/usr/bin/ruby
require 'Rex/Encoding/Xor/Generic'
#
# Routine for xor encoding a buffer by a 2-byte (intel word) key. The perl
# version used to pad this buffer out to a 2-byte boundary, but I can't think
# of a good reason to do that anymore, so this doesn't.
#
module Rex
module Encoding
module Xor
class DWord < Generic
def keysize
def DWord.keysize
4
end
end end end end # DWord/Xor/Encoding/Rex
end end end end # Word/Xor/Encoding/Rex

View File

@ -0,0 +1,15 @@
#!/usr/bin/ruby
$:.unshift(File.join('..', '..', '..', File.dirname(__FILE__)))
require 'Rex/Encoding/Xor/DWord'
require 'Rex/Encoding/Xor/Byte.ut'
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class Rex::Encoding::Xor::DWord::UnitTest < Rex::Encoding::Xor::Byte::UnitTest
def enc
Rex::Encoding::Xor::DWord
end
end

View File

@ -13,13 +13,21 @@ class Generic
return 0
end
def Generic.find_key(*crap)
raise NotImplementedError, "We are lazy bums!", caller
end
def Generic.encode(buf, key)
if !key.kind_of?(String)
raise ArgumentError, "Key must be a string!", caller
end
len = key.length
len = key.length
if len == 0
raise ArgumentError, "Zero key length!", caller
end
if keysize != 0 && keysize != len
raise ArgumentError, "Key length #{len}, expected #{keysize}", caller
@ -30,11 +38,16 @@ class Generic
while pos < buf.length
encoded += (buf[pos] ^ key[pos % len]).chr
pos += 1
end
return encoded
end
# maybe a bit a smaller of method name?
def Generic.find_key_and_encode()
end
end end end end # Generic/Xor/Encoding/Rex

View File

@ -1,109 +1,109 @@
#!/usr/bin/ruby
$:.unshift(File.dirname(__FILE__))
$:.unshift(File.join('..', '..', '..', File.dirname(__FILE__)))
require 'test/unit'
require 'Generic'
require 'Rex/Encoding/Xor/Generic'
class Rex::Encoding::Xor::Generic::UnitTest < Test::Unit::TestCase
def test_static_byte_encode
def enc
Rex::Encoding::Xor::Generic
end
gen = Rex::Encoding::Xor::Generic
def hook_static_encode(data, key, expected)
if enc.keysize != 0 && key.length != enc.keysize
assert_raise(ArgumentError) { enc.encode(data,key) }
else
assert_equal(enc.encode(data, key), expected)
end
end
def test_static_encode
# Test key of zero length
assert_raise(ArgumentError) { enc.encode("\x00", "") }
# Test non-string key
assert_raise(ArgumentError) { enc.encode("\x00\x01", 1) }
# some simple single byte tests with 0x00
30.times {
byte = rand(256).chr
assert_equal(gen.encode("\x00" * 3, byte), byte * 3)
hook_static_encode("\x00" * 3, byte, byte * 3)
}
# misc tests, see below
misc_tests.each { |test|
hook_static_encode(test[0], test[1], test[2])
}
end
def test_key_zero_length
gen = Rex::Encoding::Xor::Generic
assert_raise(ArgumentError) { gen.encode("\x00", "") }
end
#
# Misc (mostly) autogenerated tests, we need more with
# different keysizes!
#
def test_key_not_string
gen = Rex::Encoding::Xor::Generic
assert_raise(ArgumentError) { gen.encode("\x00\x01", 1) }
end
def test_static_misc_encode
gen = Rex::Encoding::Xor::Generic
tests = [ [
def misc_tests
[
# a 3 byte key test
[
"\x54\x9a\x04\x02\x8f",
"\x6d\x4b\x3c",
"9\3218o\304"
] ]
tests.each { |test|
assert_equal(gen.encode(test[0], test[1]), test[2])
}
],
# randomly generated 2 byte key tests...
[
"\x82\x3f\xb4\x77\x55\x16\x4a\x56\x87\xad\x5b\xf5",
"\x33\xdb",
"\xb1\xe4\x87\xac\x66\xcd\x79\x8d\xb4\x76\x68\x2e"
],
[
"\x9c\xbd\xaa\x83\x8d\x7e\x76\xd9\x4b\xb2\x04\xd5\x2b\x58\x66",
"\xda\x10",
"\x46\xad\x70\x93\x57\x6e\xac\xc9\x91\xa2\xde\xc5\xf1\x48\xbc"
],
[
"\x7f\x3b\xfb\x3b\xce\x8c\xe8\x3d\x65\x40\x2d\x5a\x19",
"\x62\x28",
"\x1d\x13\x99\x13\xac\xa4\x8a\x15\x07\x68\x4f\x72\x7b"
],
[
"\xc8\xab\xa4\x56\xd5\xf0",
"\x1a\xd0",
"\xd2\x7b\xbe\x86\xcf\x20"
],
[
"\xcc\x5a\x84\xe0\x6c\x00\x7a\x20\xa0\xc9",
"\xe6\xb6",
"\x2a\xec\x62\x56\x8a\xb6\x9c\x96\x46\x7f"
],
[
"\x46\x96\x83\x1f\x6a\x79\xfe\xec\x24\xe0\xc3\x20\xe9\xa5\x3a\x76",
"\x36\x5e",
"\x70\xc8\xb5\x41\x5c\x27\xc8\xb2\x12\xbe\xf5\x7e\xdf\xfb\x0c\x28"
],
[
"\x74\x7c\xe9\x21\x30\x33\xb3\xe6\x77\x9e\x07\xbc\x6c\xee\xc5\x06",
"\x02\xa0",
"\x76\xdc\xeb\x81\x32\x93\xb1\x46\x75\x3e\x05\x1c\x6e\x4e\xc7\xa6"
],
[
"\x64\x8c\xc3\x41\x5d\xe5\x18\x36\xda\xc4\x86",
"\xe3\xb9",
"\x87\x35\x20\xf8\xbe\x5c\xfb\x8f\x39\x7d\x65"
],
[
"\xdb\xbb\xb2\x7c\xda\x1f\xd6\xa5\x34\x00\xad",
"\x20\xfc",
"\xfb\x47\x92\x80\xfa\xe3\xf6\x59\x14\xfc\x8d"
],
[
"\xc1\x2e\xfc\x7b\x98\x41\xec\xe3\x40\x98\x0b\xfd\x2c",
"\x4a\xd7",
"\x8b\xf9\xb6\xac\xd2\x96\xa6\x34\x0a\x4f\x41\x2a\x66"
]
]
end
def test_static_word_encode
gen = Rex::Encoding::Xor::Generic
tests = [
[
"\x82\x3f\xb4\x77\x55\x16\x4a\x56\x87\xad\x5b\xf5",
"\x33\xdb",
"\xb1\xe4\x87\xac\x66\xcd\x79\x8d\xb4\x76\x68\x2e"
],
[
"\x9c\xbd\xaa\x83\x8d\x7e\x76\xd9\x4b\xb2\x04\xd5\x2b\x58\x66",
"\xda\x10",
"\x46\xad\x70\x93\x57\x6e\xac\xc9\x91\xa2\xde\xc5\xf1\x48\xbc"
],
[
"\x7f\x3b\xfb\x3b\xce\x8c\xe8\x3d\x65\x40\x2d\x5a\x19",
"\x62\x28",
"\x1d\x13\x99\x13\xac\xa4\x8a\x15\x07\x68\x4f\x72\x7b"
],
[
"\xc8\xab\xa4\x56\xd5\xf0",
"\x1a\xd0",
"\xd2\x7b\xbe\x86\xcf\x20"
],
[
"\xcc\x5a\x84\xe0\x6c\x00\x7a\x20\xa0\xc9",
"\xe6\xb6",
"\x2a\xec\x62\x56\x8a\xb6\x9c\x96\x46\x7f"
],
[
"\x46\x96\x83\x1f\x6a\x79\xfe\xec\x24\xe0\xc3\x20\xe9\xa5\x3a\x76",
"\x36\x5e",
"\x70\xc8\xb5\x41\x5c\x27\xc8\xb2\x12\xbe\xf5\x7e\xdf\xfb\x0c\x28"
],
[
"\x74\x7c\xe9\x21\x30\x33\xb3\xe6\x77\x9e\x07\xbc\x6c\xee\xc5\x06",
"\x02\xa0",
"\x76\xdc\xeb\x81\x32\x93\xb1\x46\x75\x3e\x05\x1c\x6e\x4e\xc7\xa6"
],
[
"\x64\x8c\xc3\x41\x5d\xe5\x18\x36\xda\xc4\x86",
"\xe3\xb9",
"\x87\x35\x20\xf8\xbe\x5c\xfb\x8f\x39\x7d\x65"
],
[
"\xdb\xbb\xb2\x7c\xda\x1f\xd6\xa5\x34\x00\xad",
"\x20\xfc",
"\xfb\x47\x92\x80\xfa\xe3\xf6\x59\x14\xfc\x8d"
],
[
"\xc1\x2e\xfc\x7b\x98\x41\xec\xe3\x40\x98\x0b\xfd\x2c",
"\x4a\xd7",
"\x8b\xf9\xb6\xac\xd2\x96\xa6\x34\x0a\x4f\x41\x2a\x66"
] ]
tests.each { |test|
assert_equal(gen.encode(test[0], test[1]), test[2])
}
end
end

View File

@ -1,5 +1,7 @@
#!/usr/bin/ruby
require 'Rex/Encoding/Xor/Generic'
#
# Routine for xor encoding a buffer by a 2-byte (intel word) key. The perl
# version used to pad this buffer out to a 2-byte boundary, but I can't think
@ -12,49 +14,8 @@ module Xor
class Word < Generic
def Word.find_key(*crap)
raise NotImplementedError, "We are lazy bums!", caller
def Word.keysize
2
end
def Word.keylength
return 2
end
def Word.packspec
return 'v'
end
def Word.pack(num)
[ num ].pack(packspec)
end
def Word.unpack(data)
data.unpack(packspec)[0]
end
def Word.encode(buf, key)
encoded = ""
pos = 0
len = keylength()
while pos < buf.length
chunk = buf[pos, len]
short = len - length(chunk)
# temporarly pad out if we are short of a word
chunk .= "\x00" * short
# add to the result, removing any short padding
encoded += (pack(unpack(chunk) ^ key))[0, len - short]
pos += len
end
end
# maybe a bit a smaller of method name?
def Word.find_key_and_encode()
end
end end end end # Word/Xor/Encoding/Rex

View File

@ -0,0 +1,15 @@
#!/usr/bin/ruby
$:.unshift(File.join('..', '..', '..', File.dirname(__FILE__)))
require 'Rex/Encoding/Xor/Word'
require 'Rex/Encoding/Xor/Byte.ut'
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class Rex::Encoding::Xor::Word::UnitTest < Rex::Encoding::Xor::Byte::UnitTest
def enc
Rex::Encoding::Xor::Word
end
end