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/gss/kerberos/message_encryptor.rb
Views: 11766
module Rex1module Proto2module Gss3module Kerberos4#5# Encrypt messages according to RFC4121 (Kerberos with GSS)6# Performs wrapping of tokens in the GSS structure, filler bytes, rotation7# and sequence number tracking and verification.8#9class MessageEncryptor1011# @param [Rex::Proto::Kerberos::Model::EncryptionKey] key The encryption key used to perform encryption and decryption12# @param [Integer] encrypt_sequence_number The starting sequence number used to encrypt messages13# @param [Integer] decrypt_sequence_number The starting sequence number we expect to see when we decrypt messages14# @param [Boolean] is_initiator Are we the initiator in this communication (used for setting flags and key usage values)15# @param [Boolean] use_acceptor_subkey Are we using the subkey provided by the acceptor? (used for setting appropriate flags)16# @param [Boolean] dce_style Is the format of the encrypted blob DCE-style?17def initialize(key, encrypt_sequence_number, decrypt_sequence_number, is_initiator: true, use_acceptor_subkey: true, dce_style: false, rc4_pad_style: :single_byte)18@key = key19@encrypt_sequence_number = encrypt_sequence_number20@decrypt_sequence_number = decrypt_sequence_number21@is_initiator = is_initiator22@use_acceptor_subkey = use_acceptor_subkey23@dce_style = dce_style24@rc4_pad_style = rc4_pad_style25@encryptor = Rex::Proto::Kerberos::Crypto::Encryption::from_etype(key.type)26end2728#29# Encrypt the message, wrapping it in GSS structures, and increment the sequence number30# @return [String, Integer, Integer] The encrypted data, the length of its header, and the length of padding added to it prior to encryption31#32def encrypt_and_increment(data)33result = encryptor.gss_wrap(data, @key, @encrypt_sequence_number, @is_initiator, use_acceptor_subkey: @use_acceptor_subkey, dce_style: @dce_style, rc4_pad_style: @rc4_pad_style)34@encrypt_sequence_number += 13536result37end3839#40# Decrypt a ciphertext, and verify its validity41#42def decrypt_and_verify(data)43result = encryptor.gss_unwrap(data, @key, @decrypt_sequence_number, @is_initiator, use_acceptor_subkey: @use_acceptor_subkey)44@decrypt_sequence_number += 1 unless @decrypt_sequence_number.nil?4546result47end4849def calculate_encrypted_length(plaintext_len)50encryptor.calculate_encrypted_length(plaintext_len)51end5253#54# The sequence number to use when we are encrypting, which should be incremented for each message55#56attr_accessor :encrypt_sequence_number5758#59# The sequence number we expect to see after decrypting, which is expected to be incremented for each message60#61attr_accessor :decrypt_sequence_number6263#64# [Rex::Proto::Kerberos::Model::EncryptionKey] The encryption key to use for encryption and decryption65#66attr_accessor :key6768#69# Are we (the encryptor) also the initiator in this interaction (vs being the Acceptor)70# This refers to the term used in RFC2743/RFC412171#72attr_accessor :is_initiator7374#75# [Boolean] Whether the acceptor subkey is used for these operations76#77attr_accessor :use_acceptor_subkey7879#80# [Boolean] Whether this encryptor will be used for DCERPC purposes (since the behaviour is subtly different)81# See MS-KILE 3.4.5.4.1 for details about the exception to the rule:82# https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-kile/e94b3acd-8415-4d0d-9786-749d0c39d55083#84# "For [MS-RPCE], the length field in the above pseudo ASN.1 header does not include the length of the concatenated data if [RFC1964] is used."85#86attr_accessor :dce_style8788#89# [Symbol] The RC4 spec (RFC4757) section 7.3 implies that RC4-HMAC only needs one byte of padding,90# although it doesn't come straight out and say it. Some protocols (LDAP, at least on a DC) complain91# if you give it more than a single byte of paddding.92# Other protocols (DRSR) complain if you don't align it perfectly with an 8-byte boundary.93# The MS-RPCE spec is a little vague on why exactly that might be, but we can at least94# show empirically that it is happy if you just give it an 8-byte aligned encrypted stub.95# Yet other protocols are happy whatever the padding (WinRM).96# Here, we allow customising the behaviour of the RC4-HMAC GSSAPI crypto scheme by providing either:97# - :single_byte -> Puts a single '\x01' byte of padding at the end98# - :eight_byte_aligned -> Puts between 1 and 8 bytes of PKCS#5 padding99attr_accessor :rc4_pad_style100101#102# [Rex::Proto::Kerberos::Crypto::*] Encryption class for encrypting/decrypting messages103#104attr_accessor :encryptor105end106end107end108end109end110111112