From daa523ef707dcb006919e85c49d90efd9809bea5 Mon Sep 17 00:00:00 2001 From: HD Moore Date: Fri, 2 Dec 2005 01:20:31 +0000 Subject: [PATCH] More code from bmc, forgot to commit git-svn-id: file:///home/svn/incoming/trunk@3164 4d416f70-5f16-0410-b530-b9f4589650da --- lib/rex/proto.ts.rb | 18 +++++++++ lib/rex/proto/dcerpc.ts.rb | 21 ++++++++++ lib/rex/proto/dcerpc/ndr.rb | 64 +++++++++++++++++++++++++++++++ lib/rex/proto/dcerpc/ndr.rb.ut.rb | 41 ++++++++++++++++++++ lib/rex/proto/http.ts.rb | 29 ++++++++++++++ lib/rex/proto/smb.ts.rb | 28 ++++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 lib/rex/proto.ts.rb create mode 100644 lib/rex/proto/dcerpc.ts.rb create mode 100644 lib/rex/proto/dcerpc/ndr.rb create mode 100644 lib/rex/proto/dcerpc/ndr.rb.ut.rb create mode 100644 lib/rex/proto/http.ts.rb create mode 100644 lib/rex/proto/smb.ts.rb diff --git a/lib/rex/proto.ts.rb b/lib/rex/proto.ts.rb new file mode 100644 index 0000000000..9cc5dec69e --- /dev/null +++ b/lib/rex/proto.ts.rb @@ -0,0 +1,18 @@ +#!/usr/bin/ruby + +require 'test/unit' +require 'rex/proto/smb.ts' +require 'rex/proto/dcerpc.ts' +require 'rex/proto/http.ts' + +class Rex::Proto::DCERPC::TestSuite + def self.suite + suite = Test::Unit::TestSuite.new("Rex::Proto::DCERPC::TestSuite") + + suite << Rex::Proto::SMB::TestSuite.suite + suite << Rex::Proto::DCERPC::TestSuite.suite + suite << Rex::Proto::HTTP::TestSuite.suite + + return suite + end +end diff --git a/lib/rex/proto/dcerpc.ts.rb b/lib/rex/proto/dcerpc.ts.rb new file mode 100644 index 0000000000..32b7c215e2 --- /dev/null +++ b/lib/rex/proto/dcerpc.ts.rb @@ -0,0 +1,21 @@ +#!/usr/bin/ruby + +require 'test/unit' +require 'rex/proto/dcerpc' +require 'rex/proto/dcerpc/uuid.rb.ut' +require 'rex/proto/dcerpc/response.rb.ut' +require 'rex/proto/dcerpc/packet.rb.ut' +require 'rex/proto/dcerpc/ndr.rb.ut' + +class Rex::Proto::DCERPC::TestSuite + def self.suite + suite = Test::Unit::TestSuite.new("Rex::Proto::DCERPC::TestSuite") + + suite << Rex::Proto::DCERPC::UUID::UnitTest.suite + suite << Rex::Proto::DCERPC::Response::UnitTest.suite + suite << Rex::Proto::DCERPC::Packet::UnitTest.suite + suite << Rex::Proto::DCERPC::NDR::UnitTest.suite + + return suite + end +end diff --git a/lib/rex/proto/dcerpc/ndr.rb b/lib/rex/proto/dcerpc/ndr.rb new file mode 100644 index 0000000000..5e2c430fbb --- /dev/null +++ b/lib/rex/proto/dcerpc/ndr.rb @@ -0,0 +1,64 @@ +require "rex/text" + +module Rex +module Proto +module DCERPC +class NDR + + + # Provide padding to align the string to the 32bit boundary + def self.align(string) + return "\x00" * ((4 - (string.length & 3)) & 3) + end + + # Encode a 4 byte long + # use to encode: + # long element_1; + def self.long(string) + return [string].pack('V') + end + + # Encode a 2 byte short + # use to encode: + # short element_1; + def self.short(string) + return [string].pack('v') + end + + # Encode a single byte + # use to encode: + # byte element_1; + def self.byte(string) + return [string].pack('c') + end + + # Encode a byte array + # use to encode: + # char element_1 + def self.UniConformantArray(string) + return long(string.length) + string + align(string) + end + + # Encode a string + # use to encode: + # w_char *element_1; + def self.UnicodeConformantVaryingString(string) + string += "\x00" # null pad + return long(string.length) + long(0) + long(string.length) + Rex::Text.to_unicode(string) + align(Rex::Text.to_unicode(string)) + end + + # Encode a string that is already unicode encoded + # use to encode: + # w_char *element_1; + def self.UnicodeConformantVaryingStringPreBuilt(string) + # if the string len is odd, thats bad! + if (string.length % 2) + string += "\x00" + end + return long(string.length / 2) + long(0) + long(string.length / 2) + string + align(string) + end + +end +end +end +end diff --git a/lib/rex/proto/dcerpc/ndr.rb.ut.rb b/lib/rex/proto/dcerpc/ndr.rb.ut.rb new file mode 100644 index 0000000000..a6506a248e --- /dev/null +++ b/lib/rex/proto/dcerpc/ndr.rb.ut.rb @@ -0,0 +1,41 @@ +#!/usr/bin/ruby + +$:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..')) + +require 'test/unit' +require 'rex/exceptions' +require 'rex/proto/dcerpc/ndr' + +class Rex::Proto::DCERPC::NDR::UnitTest < Test::Unit::TestCase + + Klass = Rex::Proto::DCERPC::NDR + + def test_align + assert_equal(Klass.align('').length, 0, 'align 0') + assert_equal(Klass.align('f').length, 3, 'align 1') + assert_equal(Klass.align('fo').length, 2, 'align 2') + assert_equal(Klass.align('foo').length, 1, 'align 3') + assert_equal(Klass.align('fooo').length, 0, 'align 4') + assert_equal(Klass.align('foooo').length, 3, 'align 5') + end + + def test_numbers + assert_equal(Klass.long(10), "\x0a\x00\x00\x00", 'long') + assert_equal(Klass.short(10), "\x0a\x00", 'short') + assert_equal(Klass.byte(10), "\x0a", 'byte') + end + + def test_conformant_array + assert_equal(Klass.UniConformantArray('aaaaa').slice(0,9), "\x05\x00\x00\x00aaaaa", 'UniConformantArray') + assert_equal(Klass.UniConformantArray('aaaaa').length, 12, 'UniConformantArray length') + end + + def test_conformant_string + assert_equal(Klass.UnicodeConformantVaryingString('aaaaa').slice(0,4+4+4+12), "\x06\x00\x00\x00" + "\x00\x00\x00\x00" + "\x06\x00\x00\x00" "a\x00a\x00a\x00a\x00a\x00\x00\x00", 'UniConformantVaryingString') + assert_equal(Klass.UnicodeConformantVaryingString('aaaaa').length, 24, 'UniConformantVaryingString length') + + assert_equal(Klass.UnicodeConformantVaryingStringPreBuilt('aaaaa').slice(0,4+4+4+6), "\x03\x00\x00\x00" + "\x00\x00\x00\x00" + "\x03\x00\x00\x00" "aaaaa\x00", 'UniConformantVaryingStringPreBuilt') + assert_equal(Klass.UnicodeConformantVaryingStringPreBuilt('aaaaa').length, 20, 'UniConformantVaryingStringPreBuilt length') + end + +end diff --git a/lib/rex/proto/http.ts.rb b/lib/rex/proto/http.ts.rb new file mode 100644 index 0000000000..a3fdac0583 --- /dev/null +++ b/lib/rex/proto/http.ts.rb @@ -0,0 +1,29 @@ +#!/usr/bin/ruby + +require 'test/unit' + +require 'rex/proto/http' +require 'rex/proto/http/client.rb.ut' +require 'rex/proto/http/server.rb.ut' +require 'rex/proto/http/packet.rb.ut' +require 'rex/proto/http/request.rb.ut' +require 'rex/proto/http/response.rb.ut' +require 'rex/proto/http/handler/erb.rb.ut' +require 'rex/proto/http/handler/proc.rb.ut' + +class Rex::Proto::Http::TestSuite + def self.suite + suite = Test::Unit::TestSuite.new("Rex::Proto::Http::TestSuite") + + suite << Rex::Proto::Http::Client::UnitTest.suite + suite << Rex::Proto::Http::Server::UnitTest.suite + suite << Rex::Proto::Http::Packet::UnitTest.suite + suite << Rex::Proto::Http::Request::UnitTest.suite + suite << Rex::Proto::Http::Response::UnitTest.suite + suite << Rex::Proto::Http::Handler::ERB::UnitTest.suite + suite << Rex::Proto::Http::Handler::Proc::UnitTest.suite + + return suite + end +end + diff --git a/lib/rex/proto/smb.ts.rb b/lib/rex/proto/smb.ts.rb new file mode 100644 index 0000000000..29f4646739 --- /dev/null +++ b/lib/rex/proto/smb.ts.rb @@ -0,0 +1,28 @@ +#!/usr/bin/ruby + +require 'test/unit' +require 'rex/proto/dcerpc' +require 'rex/proto/dcerpc/uuid.rb.ut' +require 'rex/proto/dcerpc/response.rb.ut' +require 'rex/proto/dcerpc/packet.rb.ut' + +require 'rex/proto/smb/client.rb.ut.rb' +require 'rex/proto/smb/constants.rb.ut.rb' +require 'rex/proto/smb/crypt.rb.ut.rb' +require 'rex/proto/smb/simpleclient.rb.ut.rb' +require 'rex/proto/smb/utils.rb.ut.rb' + +class Rex::Proto::SMB::TestSuite + def self.suite + suite = Test::Unit::TestSuite.new("Rex::Proto::SMB::TestSuite") + + suite << Rex::Proto::SMB::Client::UnitTest.suite + suite << Rex::Proto::SMB::Constants::UnitTest.suite + suite << Rex::Proto::SMB::Crypt::UnitTest.suite + suite << Rex::Proto::SMB::SimpleClient::UnitTest.suite + suite << Rex::Proto::SMB::Utils::UnitTest.suite + + return suite + end +end +