Path: blob/master/lib/rex/proto/kerberos/crypto.rb
33084 views
# -*- coding: binary -*-1# frozen_string_literal: true23require 'rex/proto/kerberos/crypto/rc4_hmac'4require 'rex/proto/kerberos/crypto/rsa_md5'56module Rex7module Proto8module Kerberos9module Crypto10# https://datatracker.ietf.org/doc/html/rfc4120#section-7.5.1 - A unique number used as part of encryption to make certain types of11# cryptographic attacks harder12module KeyUsage13AS_REQ_PA_ENC_TIMESTAMP = 114KDC_REP_TICKET = 215AS_REP_ENCPART = 316TGS_REQ_KDC_REQ_BODY_AUTHDATA_SESSION_KEY = 417TGS_REQ_KDC_REQ_BODY_AUTHDATA_SUB_KEY = 518TGS_REQ_PA_TGS_REQ_AP_REQ_AUTHENTICATOR_CHKSUM = 619TGS_REQ_PA_TGS_REQ_AP_REQ_AUTHENTICATOR = 720TGS_REP_ENCPART_SESSION_KEY = 821TGS_REP_ENCPART_AUTHENTICATOR_SUB_KEY = 922AP_REQ_AUTHENTICATOR_CHKSUM = 1023AP_REQ_AUTHENTICATOR = 1124AP_REP_ENCPART = 1225KRB_PRIV_ENCPART = 1326KRB_CRED_ENCPART = 1427KRB_SAFE_CHKSUM = 1528KERB_NON_KERB_SALT = 1629KERB_NON_KERB_CKSUM_SALT = 1730GSS_ACCEPTOR_SEAL = 2231GSS_ACCEPTOR_SIGN = 2332GSS_INITIATOR_SEAL = 2433GSS_INITIATOR_SIGN = 2534PA_S4U_X509_USER = 2635end3637module Checksum38RSA_MD5 = 739MD5_DES = 840SHA1_DES3 = 1241SHA1_AES128 = 1542SHA1_AES256 = 1643HMAC_MD5 = -1384445def self.from_checksum_type(ctype)46checksummers = {47RSA_MD5 => Rex::Proto::Kerberos::Crypto::RsaMd5,48MD5_DES => Rex::Proto::Kerberos::Crypto::DesCbcMd5,49SHA1_DES3 => Rex::Proto::Kerberos::Crypto::Des3CbcSha1,50SHA1_AES128 => Rex::Proto::Kerberos::Crypto::Aes128CtsSha1,51SHA1_AES256 => Rex::Proto::Kerberos::Crypto::Aes256CtsSha1,52HMAC_MD5 => Rex::Proto::Kerberos::Crypto::Rc4Hmac,530xffffff76 => Rex::Proto::Kerberos::Crypto::Rc4Hmac, # Negative 138 two's complement54}55result = checksummers[ctype]56raise ::NotImplementedError, 'Checksum type is not supported' if result == nil5758result.new59end6061end6263# https://www.iana.org/assignments/kerberos-parameters/kerberos-parameters.xhtml64module Encryption65DES_CBC_CRC = 166DES_CBC_MD4 = 267DES_CBC_MD5 = 368DES3_CBC_SHA1 = 1669AES128 = 1770AES256 = 1871RC4_HMAC = 237273# Mapping of encryption type numbers to the human readable text description by IANA74# https://www.iana.org/assignments/kerberos-parameters/kerberos-parameters.xhtml75IANA_NAMES = {76DES_CBC_CRC => 'des-cbc-crc',77DES_CBC_MD4 => 'des-cbc-md4',78DES_CBC_MD5 => 'des-cbc-md5',79DES3_CBC_SHA1 => 'des3-cbc-sha1-kd',80AES128 => 'aes128-cts-hmac-sha1-96',81AES256 => 'aes256-cts-hmac-sha1-96',82RC4_HMAC => 'rc4-hmac'83}8485# The default etypes to offer to the Kerberos server when none is provided86DefaultOfferedEtypes = [AES256, AES128, RC4_HMAC, DES_CBC_MD5, DES3_CBC_SHA1]87PkinitEtypes = [AES256, AES128]8889# The individual etype used by an encryptor when none is provided90DefaultEncryptionType = RC4_HMAC9192ENCRYPTORS = {93DES_CBC_MD5 => Rex::Proto::Kerberos::Crypto::DesCbcMd5,94DES3_CBC_SHA1 => Rex::Proto::Kerberos::Crypto::Des3CbcSha1,95RC4_HMAC => Rex::Proto::Kerberos::Crypto::Rc4Hmac,96AES128 => Rex::Proto::Kerberos::Crypto::Aes128CtsSha1,97AES256 => Rex::Proto::Kerberos::Crypto::Aes256CtsSha1,98}99private_constant :ENCRYPTORS100101SUPPORTED_ENCRYPTIONS = ENCRYPTORS.keys102103#104# Return a string representation of the constant for a number105#106# @param [Integer] code107def self.const_name(code)108(self.constants - [:DefaultEncryptionType]).each do |c|109return c.to_s if self.const_get(c) == code110end111return nil112end113114# Return a integer value for the given encryption const name115#116# @param [String] const_name117def self.value_for(const_name)118self.const_get(const_name)119end120121# @param [Integer] etype122# @return [Rex::Proto::Kerberos::Crypto::BlockCipherBase]123def self.from_etype(etype)124result = ENCRYPTORS[etype]125raise ::NotImplementedError, "EncryptedData schema #{etype.inspect} is not supported" if result == nil126127result.new128end129end130end131end132end133end134135136