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/crypto/asn1_utils.rb
Views: 11766
module Rex1module Proto2module Kerberos3module Crypto4module Asn1Utils5# Some crypto schemes just decide to add a bunch of null bytes as padding, and6# leave it up to the application to decide how many of those null bytes to remove.7# We can't just remove all zeroes from the end of the data, because some of them8# may actually be part of the data. The assumption here is that the information9# about how many bytes to use comes from the ASN1 data structure. So here we ask10# the ASN1 parser's enclosing (first) element "How many bytes do you take up?"11def truncate_nulls_after_asn1(input)12valid_until = 013OpenSSL::ASN1.traverse(input) do | depth, offset, header_len, length, constructed, tag_class, tag|14valid_until = offset + length + header_len15break16end1718# For this to be a valid result, we expect this byte, and all following it, to be zeroes. Alternatively, there could be no padding at all (e.g. block multiple)19suffix = input[valid_until, input.length]20expected_result = suffix == "" || suffix.unpack('C*').all? {|char| char == 0}21raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to truncate decrypted data' unless expected_result2223return input[0,valid_until]24end25end26end27end28end29end30313233