Add support for decoding KrbError

bug/bundler_fix
jvazquez-r7 2014-12-14 16:26:18 -06:00
parent 704781d0ce
commit c5dc065fde
4 changed files with 391 additions and 2 deletions

View File

@ -12,4 +12,5 @@ module Rex
end
require 'rex/proto/kerberos/model/message/kdc_request'
require 'rex/proto/kerberos/model/message/krb_error'
require 'rex/proto/kerberos/model/message/kdc_response'

View File

@ -101,8 +101,7 @@ module Rex
decode_asn1(asn1)
end
# Decodes a Rex::Proto::Kerberos::Model::Message::KdcRequest from an
# OpenSSL::ASN1::Sequence
# Decodes a Rex::Proto::Kerberos::Model::Message::KdcRequest
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @raise [RuntimeError] if decoding doesn't succeed

View File

@ -0,0 +1,203 @@
# -*- coding: binary -*-
module Rex
module Proto
module Kerberos
module Model
module Message
class KrbError < Element
# @!attribute pvno
# @return [Fixnum] The protocol version number
attr_accessor :pvno
# @!attribute msg_type
# @return [Fixnum] The type of a protocol message
attr_accessor :msg_type
# @!attribute ctime
# @return [Time] The current time of the client's host
attr_accessor :ctime
# @!attribute cusec
# @return [Fixnum] The microseconds part of the client timestamp
attr_accessor :cusec
# @!attribute stime
# @return [Time] The current time of the server
attr_accessor :stime
# @!attribute susec
# @return [Fixnum] The microseconds part of the server timestamp
attr_accessor :susec
# @!attribute error_code
# @return [Fixnum] The error request returned by kerberos or the server when a request fails
attr_accessor :error_code
# @!attribute crealm
# @return [String] The realm part of the client's principal identifier
attr_accessor :crealm
# @!attribute cname
# @return [Rex::Proto::Kerberos::Type::PrincipalName] The name part of the client's principal identifier
attr_accessor :cname
# @!attribute realm
# @return [String] The realm part of the server's principal identifier
attr_accessor :realm
# @!attribute sname
# @return [Rex::Proto::Kerberos::Type::PrincipalName] The name part of the server's identity
attr_accessor :sname
# Decodes the Rex::Proto::Kerberos::Model::Message::KrbError from an input
#
# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [self] if decoding succeeds
# @raise [RuntimeError] if decoding doesn't succeed
def decode(input)
case input
when String
decode_string(input)
when OpenSSL::ASN1::ASN1Data
decode_asn1(input)
else
raise ::RuntimeError, 'Failed to decode KRB Error, invalid input'
end
self
end
def encode
raise ::RuntimeError, 'KrbError encoding not supported'
end
private
# Decodes a Rex::Proto::Kerberos::Model::Message::KrbError from an String
#
# @param input [String] the input to decode from
def decode_string(input)
asn1 = OpenSSL::ASN1.decode(input)
decode_asn1(asn1)
end
# Decodes a Rex::Proto::Kerberos::Model::Message::KrbError
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @raise [RuntimeError] if decoding doesn't succeed
def decode_asn1(input)
input.value[0].value.each do |val|
case val.tag
when 0
self.pvno = decode_pvno(val)
when 1
self.msg_type = decode_msg_type(val)
when 2
self.ctime = decode_ctime(val)
when 3
self.cusec = decode_cusec(val)
when 4
self.stime = decode_stime(val)
when 5
self.susec = decode_susec(val)
when 6
self.error_code = decode_error_code(val)
when 7
self.crealm = decode_crealm(val)
when 8
self.cname = decode_cname(val)
when 9
self.realm = decode_realm(val)
when 10
self.sname = decode_sname(val)
else
raise ::RuntimeError, 'Failed to decode KRB-ERROR SEQUENCE'
end
end
end
# Decodes the pvno from an OpenSSL::ASN1::ASN1Data
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Fixnum]
def decode_pvno(input)
input.value[0].value.to_i
end
# Decodes the msg_type from an OpenSSL::ASN1::ASN1Data
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Fixnum]
def decode_msg_type(input)
input.value[0].value.to_i
end
# Decodes the ctime field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Time]
def decode_ctime(input)
input.value[0].value
end
# Decodes the cusec field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Fixnum]
def decode_cusec(input)
input.value[0].value
end
# Decodes the stime field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Time]
def decode_stime(input)
input.value[0].value
end
# Decodes the susec field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Fixnum]
def decode_susec(input)
input.value[0].value.to_i
end
# Decodes the error_code field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Fixnum]
def decode_error_code(input)
input.value[0].value.to_i
end
# Decodes the crealm field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [String]
def decode_crealm(input)
input.value[0].value
end
# Decodes the cname field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Rex::Proto::Kerberos::Type::PrincipalName]
def decode_cname(input)
Rex::Proto::Kerberos::Model::Type::PrincipalName.decode(input.value[0])
end
# Decodes the realm field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [String]
def decode_realm(input)
input.value[0].value
end
# Decodes the sname field
#
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
# @return [Rex::Proto::Kerberos::Type::PrincipalName]
def decode_sname(input)
Rex::Proto::Kerberos::Model::Type::PrincipalName.decode(input.value[0])
end
end
end
end
end
end
end

View File

@ -0,0 +1,186 @@
# -*- coding:binary -*-
require 'spec_helper'
require 'rex/proto/kerberos'
describe Rex::Proto::Kerberos::Model::Message::KrbError do
subject(:krb_error) do
described_class.new
end
let(:msg_type) { 30 }
let(:error_code_generic) { 60 }
=begin
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbc370
@infinite_length=false,
@tag=30,
@tag_class=:APPLICATION,
@value=
[#<OpenSSL::ASN1::Sequence:0x007ff1f7cbc4b0
@infinite_length=false,
@tag=16,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=
[#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbd770
@infinite_length=false,
@tag=0,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::Integer:0x007ff1f7cbd810
@infinite_length=false,
@tag=2,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=#<OpenSSL::BN:0x007ff1f7cbd838>>]>,
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbd568
@infinite_length=false,
@tag=1,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::Integer:0x007ff1f7cbd5e0
@infinite_length=false,
@tag=2,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=#<OpenSSL::BN:0x007ff1f7cbd6a8>>]>,
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbd338
@infinite_length=false,
@tag=4,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::GeneralizedTime:0x007ff1f7cbd360
@infinite_length=false,
@tag=24,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=2014-12-14 06:54:01 UTC>]>,
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbd0b8
@infinite_length=false,
@tag=5,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::Integer:0x007ff1f7cbd1a8
@infinite_length=false,
@tag=2,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=#<OpenSSL::BN:0x007ff1f7cbd248>>]>,
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbcd70
@infinite_length=false,
@tag=6,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::Integer:0x007ff1f7cbce10
@infinite_length=false,
@tag=2,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=#<OpenSSL::BN:0x007ff1f7cbd018>>]>,
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbcb18
@infinite_length=false,
@tag=9,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::GeneralString:0x007ff1f7cbcb40
@infinite_length=false,
@tag=27,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value="DEMO.LOCAL">]>,
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbc4d8
@infinite_length=false,
@tag=10,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::Sequence:0x007ff1f7cbc550
@infinite_length=false,
@tag=16,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=
[#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbc910
@infinite_length=false,
@tag=0,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::Integer:0x007ff1f7cbc960
@infinite_length=false,
@tag=2,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=#<OpenSSL::BN:0x007ff1f7cbc988>>]>,
#<OpenSSL::ASN1::ASN1Data:0x007ff1f7cbc578
@infinite_length=false,
@tag=1,
@tag_class=:CONTEXT_SPECIFIC,
@value=
[#<OpenSSL::ASN1::Sequence:0x007ff1f7cbc5f0
@infinite_length=false,
@tag=16,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value=
[#<OpenSSL::ASN1::GeneralString:0x007ff1f7cbc730
@infinite_length=false,
@tag=27,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value="krbtgt">,
#<OpenSSL::ASN1::GeneralString:0x007ff1f7cbc618
@infinite_length=false,
@tag=27,
@tag_class=:UNIVERSAL,
@tagging=nil,
@value="DEMO.LOCAL">]>]>]>]>]>]>
=end
let(:generic_error) do
"\x7e\x5a\x30\x58\xa0\x03\x02\x01\x05\xa1\x03\x02" +
"\x01\x1e\xa4\x11\x18\x0f\x32\x30\x31\x34\x31\x32\x31\x34\x30\x36" +
"\x35\x34\x30\x31\x5a\xa5\x05\x02\x03\x0b\x0d\x5b\xa6\x03\x02\x01" +
"\x3c\xa9\x0c\x1b\x0a\x44\x45\x4d\x4f\x2e\x4c\x4f\x43\x41\x4c\xaa" +
"\x1f\x30\x1d\xa0\x03\x02\x01\x02\xa1\x16\x30\x14\x1b\x06\x6b\x72" +
"\x62\x74\x67\x74\x1b\x0a\x44\x45\x4d\x4f\x2e\x4c\x4f\x43\x41\x4c"
end
describe "#decode" do
context "generic error" do
it "returns the Rex::Proto::Kerberos::Model::Message::KrbError decoded" do
expect(krb_error.decode(generic_error)).to eq(krb_error)
end
it "decodes msg_type correctly" do
krb_error.decode(generic_error)
expect(krb_error.msg_type).to eq(msg_type)
end
it "decodes stime correctly" do
krb_error.decode(generic_error)
expect(krb_error.stime.to_s).to eq('2014-12-14 06:54:01 UTC')
end
it "decodes susec correctly" do
krb_error.decode(generic_error)
expect(krb_error.susec).to eq(724315)
end
it "decodes error_code correctly" do
krb_error.decode(generic_error)
expect(krb_error.error_code).to eq(error_code_generic)
end
it "decodes realm correctly" do
krb_error.decode(generic_error)
expect(krb_error.realm).to eq('DEMO.LOCAL')
end
it "decodes sname correctly" do
krb_error.decode(generic_error)
expect(krb_error.sname.name_string).to eq(['krbtgt', 'DEMO.LOCAL'])
end
end
end
end