Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/kerberos/model/krb_cred.rb
Views: 11766
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of a Kerberos KRB-CRED7# message definition.8class KrbCred < Element9# @!attribute pvno10# @return [Integer] The protocol version number11attr_accessor :pvno12# @!attribute msg_type13# @return [Integer] The type of a protocol message14attr_accessor :msg_type15# @!attribute tickets16# @return [Array<Rex::Proto::Kerberos::Model::Ticket>] Tickets encapsulated in this message17attr_accessor :tickets18# @!attribute enc_part19# @return [Rex::Proto::Kerberos::Model::EncryptedData] Encrypted KRB-CRED blob20attr_accessor :enc_part2122def ==(other)23pvno == other.pvno &&24msg_type == other.msg_type &&25tickets == other.tickets &&26enc_part == other.enc_part27end2829# Decodes the Rex::Proto::Kerberos::Model::KrbCred from an input30#31# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from32# @return [self] if decoding succeeds33# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed34def decode(input)35case input36when String37decode_string(input)38when OpenSSL::ASN1::Sequence39decode_asn1(input)40else41raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode KrbCred, invalid input'42end4344self45end4647# Rex::Proto::Kerberos::Model::KrbCred encoding isn't supported48#49# @raise [NotImplementedError]50def encode51elems = []52elems << OpenSSL::ASN1::ASN1Data.new([encode_pvno], 0, :CONTEXT_SPECIFIC)53elems << OpenSSL::ASN1::ASN1Data.new([encode_msg_type], 1, :CONTEXT_SPECIFIC)54elems << OpenSSL::ASN1::ASN1Data.new([encode_tickets], 2, :CONTEXT_SPECIFIC)55elems << OpenSSL::ASN1::ASN1Data.new([encode_enc_part], 3, :CONTEXT_SPECIFIC)5657seq = OpenSSL::ASN1::Sequence.new(elems)58seq_asn1 = OpenSSL::ASN1::ASN1Data.new([seq], KRB_CRED, :APPLICATION)5960seq_asn1.to_der61end6263# Loads a KrbCred from a kirbi file64# @param [String] file_path the path to load the file from65# @return [Rex::Proto::Kerberos::Model::KrbCred]66def self.load_credential_from_file(file_path)67unless File.readable?(file_path.to_s)68raise ::ArgumentError, "Failed to load kirbi file '#{file_path}'"69end7071decode(File.binread(file_path))72end7374# Saves a KrbCred to a kirbi file75# @param [String] file_path the path to save the file to76# @return [Integer] The length written77def save_credential_to_file(file_path)78File.binwrite(file_path, encode)79end8081private8283# Encodes the pvno84#85# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError]86def encode_pvno87bn = OpenSSL::BN.new(pvno.to_s)88int = OpenSSL::ASN1::Integer.new(bn)8990int91rescue OpenSSL::ASN1::ASN1Error92raise Rex::Proto::Kerberos::Model::Error::KerberosDecodingError93end9495# Encodes the msg_type field96#97# @return [OpenSSL::ASN1::Integer]98def encode_msg_type99bn = OpenSSL::BN.new(msg_type.to_s)100int = OpenSSL::ASN1::Integer.new(bn)101102int103end104105# Encodes the ticket field106#107# @return [OpenSSL::ASN1::Sequence]108def encode_tickets109encoded = tickets.map(&:encode)110seq = OpenSSL::ASN1::Sequence.new(encoded)111end112113# Encodes the enc_part field114#115# @return [String]116def encode_enc_part117encoded = enc_part.encode118end119120# Decodes a Rex::Proto::Kerberos::Model::KrbCred121#122# @param input [String] the input to decode from123def decode_string(input)124asn1 = OpenSSL::ASN1.decode(input)125126decode_asn1(asn1)127end128129# Decodes a Rex::Proto::Kerberos::Model::KrbCred from an130# OpenSSL::ASN1Data131#132# @param input [OpenSSL::ASN1Data] the input to decode from133def decode_asn1(input)134input.value[0].value.each do |val|135case val.tag136when 0137self.pvno = decode_pvno(val)138when 1139self.msg_type = decode_msg_type(val)140when 2141self.tickets = decode_tickets(val)142when 3143self.enc_part = decode_enc_part(val)144else145raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, "Failed to decode KrbCred (#{val.tag})"146end147end148end149150# Decodes the pvno from an OpenSSL::ASN1::ASN1Data151#152# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from153# @return [Integer]154def decode_pvno(input)155input.value[0].value.to_i156end157158# Decodes the msg_type from an OpenSSL::ASN1::ASN1Data159#160# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from161# @return [Integer]162def decode_msg_type(input)163input.value[0].value.to_i164end165166# Decodes the tickets from an OpenSSL::ASN1::Sequence167#168# @param input [OpenSSL::ASN1::Sequence] the input to decode from169# @return [Array<Rex::Proto::Kerberos::Model::Tickets>]170def decode_tickets(input)171tickets = []172input.value[0].value.each do |val|173tickets << Rex::Proto::Kerberos::Model::Ticket.decode(val)174end175tickets176end177178# Decodes the enc_part179#180# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from181# @return [Rex::Proto::Kerberos::Model::EncryptedData]182def decode_enc_part(input)183Rex::Proto::Kerberos::Model::EncryptedData.decode(input.value[0])184end185end186end187end188end189end190191192