Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions impacket/krb5/pac.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Alberto Solino (@agsolino)
#
from binascii import Error as BinasciiError, unhexlify
from struct import unpack_from

from impacket.dcerpc.v5.dtypes import ULONG, RPC_UNICODE_STRING, FILETIME, PRPC_SID, USHORT, RPC_SID, SID, LONG64, LPWSTR
from impacket.dcerpc.v5.ndr import NDRSTRUCT, NDRUniConformantArray, NDRPOINTER, NDRUNION
Expand Down Expand Up @@ -223,6 +224,10 @@ class PACTYPE(Structure):
('Buffers', ':'),
)

def fromString(self, data):
validate_pac(data)
return super().fromString(data)

# 2.4 PAC_INFO_BUFFER
class PAC_INFO_BUFFER(Structure):
structure = (
Expand Down Expand Up @@ -410,6 +415,94 @@ class PAC_REQUESTOR(Structure):
)


def validate_pac(data):
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError('PAC data must be a bytes-like object')

if len(data) < 8:
raise ValueError('PAC header is truncated')

buffer_count, version = unpack_from('<II', data)
if version != 0:
raise ValueError('unsupported PAC version')

table_end = 8 + buffer_count * 16
if table_end > len(data):
raise ValueError('PAC buffer table is truncated')

buffers = []
ranges = []
for index in range(buffer_count):
buffer_type, buffer_size, offset = unpack_from(
'<IIQ', data, 8 + index * 16
)
if offset % 8 != 0:
raise ValueError('PAC buffer offset is not 8-byte aligned')
if offset < table_end:
raise ValueError('PAC buffer overlaps the descriptor table')

buffer_end = offset + buffer_size
if buffer_end > len(data):
raise ValueError('PAC buffer extends past the input')
buffers.append((buffer_type, offset, buffer_end))
if buffer_size:
ranges.append((offset, buffer_end))

ranges.sort()
for previous, current in zip(ranges, ranges[1:]):
if current[0] < previous[1]:
raise ValueError('PAC buffers overlap')

for buffer_type, offset, buffer_end in buffers:
_validate_pac_buffer(buffer_type, data[offset:buffer_end])


def _validate_pac_buffer(buffer_type, data):
if buffer_type == PAC_ATTRIBUTES_INFO:
if len(data) != 8:
raise ValueError('PAC_ATTRIBUTES_INFO has an invalid size')
flags_length, flags = unpack_from('<II', data)
if flags_length != 2:
raise ValueError('PAC_ATTRIBUTES_INFO has an invalid FlagsLength')
if flags & ~0x3:
raise ValueError('PAC_ATTRIBUTES_INFO contains unknown flags')
return

if buffer_type == PAC_REQUESTOR_INFO:
if len(data) < 8:
raise ValueError('PAC_REQUESTOR is truncated')
revision, sub_authority_count = unpack_from('<BB', data)
if revision != 1:
raise ValueError(
'PAC_REQUESTOR contains an unsupported SID revision'
)
if len(data) != 8 + sub_authority_count * 4:
raise ValueError('PAC_REQUESTOR SID length is inconsistent')
return

if buffer_type not in (PAC_SERVER_CHECKSUM, PAC_PRIVSVR_CHECKSUM):
return

if len(data) < 4:
raise ValueError('PAC signature data is truncated')
signature_type = unpack_from('<l', data)[0]
checksum_profile = _checksum_table.get(signature_type)
if checksum_profile is None:
checksum_profile = _checksum_table.get(signature_type & 0xffffffff)
if checksum_profile is None:
raise ValueError('PAC signature uses an unsupported checksum type')

signature_size = getattr(checksum_profile, 'macsize', None)
if signature_size is None:
if signature_type != constants.ChecksumTypes.hmac_md5.value:
raise ValueError('PAC signature has an unknown checksum size')
signature_size = 16

data_size = len(data) - 4
if data_size not in (signature_size, signature_size + 2):
raise ValueError('PAC signature length is inconsistent')


def get_pad_length(data_length):
return get_block_length(data_length) - data_length

Expand Down
204 changes: 204 additions & 0 deletions tests/misc/test_pac_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,216 @@
#

import unittest
from struct import pack, pack_into

from impacket.krb5 import constants, pac


class TestPacHelpers(unittest.TestCase):

@staticmethod
def _build_pac_blob(descriptors):
table_end = 8 + len(descriptors) * 16
data_end = max(
[table_end] + [offset + size for _, size, offset in descriptors]
)
data = bytearray(data_end)
pack_into('<II', data, 0, len(descriptors), 0)
for index, descriptor in enumerate(descriptors):
pack_into('<IIQ', data, 8 + index * 16, *descriptor)
return bytes(data)

@classmethod
def _build_single_buffer_pac(cls, buffer_type, payload):
data = bytearray(cls._build_pac_blob([
(buffer_type, len(payload), 24),
]))
data[24:] = payload
return bytes(data)

def test_pac_type_rejects_truncated_buffer_table(self):
data = pack('<II', 1, 0)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_truncated_header(self):
with self.assertRaises(ValueError):
pac.PACTYPE(b'\x00' * 7)

def test_pac_type_rejects_unsupported_version(self):
data = pack('<II', 0, 1)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_unaligned_buffer_offset(self):
data = self._build_pac_blob([
(pac.PAC_LOGON_INFO, 1, 25),
])

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_buffer_offset_inside_descriptor_table(self):
data = self._build_pac_blob([
(pac.PAC_LOGON_INFO, 1, 16),
])

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_buffer_past_input(self):
data = self._build_pac_blob([
(pac.PAC_LOGON_INFO, 2, 24),
])[:-1]

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_overlapping_buffers(self):
data = self._build_pac_blob([
(pac.PAC_LOGON_INFO, 8, 40),
(pac.PAC_CLIENT_INFO_TYPE, 8, 44),
])

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_invalid_attributes_flags_length(self):
data = self._build_single_buffer_pac(
pac.PAC_ATTRIBUTES_INFO,
pack('<II', 3, 1),
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_invalid_attributes_size(self):
data = self._build_single_buffer_pac(
pac.PAC_ATTRIBUTES_INFO,
b'\x00' * 7,
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_unknown_attributes_flags(self):
data = self._build_single_buffer_pac(
pac.PAC_ATTRIBUTES_INFO,
pack('<II', 2, 4),
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_requestor_sid_with_invalid_revision(self):
data = self._build_single_buffer_pac(
pac.PAC_REQUESTOR_INFO,
pack('<BB6s', 2, 0, b'\x00\x00\x00\x00\x00\x05'),
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_truncated_requestor_sid(self):
data = self._build_single_buffer_pac(
pac.PAC_REQUESTOR_INFO,
pack('<BB6s', 1, 1, b'\x00\x00\x00\x00\x00\x05'),
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_requestor_sid_trailing_data(self):
data = self._build_single_buffer_pac(
pac.PAC_REQUESTOR_INFO,
pack('<BB6sB', 1, 0, b'\x00\x00\x00\x00\x00\x05', 0),
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_unsupported_signature_type(self):
data = self._build_single_buffer_pac(
pac.PAC_SERVER_CHECKSUM,
pack('<l', -1) + b'\x00' * 12,
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_signature_without_type(self):
data = self._build_single_buffer_pac(
pac.PAC_SERVER_CHECKSUM,
b'\x00' * 3,
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_truncated_signature(self):
data = self._build_single_buffer_pac(
pac.PAC_SERVER_CHECKSUM,
pack('<l', constants.ChecksumTypes.hmac_sha1_96_aes128.value)
+ b'\x00' * 11,
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_truncated_hmac_md5_signature(self):
data = self._build_single_buffer_pac(
pac.PAC_SERVER_CHECKSUM,
pack('<l', constants.ChecksumTypes.hmac_md5.value)
+ b'\x00' * 15,
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_rejects_signature_trailing_data(self):
data = self._build_single_buffer_pac(
pac.PAC_PRIVSVR_CHECKSUM,
pack('<l', constants.ChecksumTypes.hmac_sha1_96_aes128.value)
+ b'\x00' * 15,
)

with self.assertRaises(ValueError):
pac.PACTYPE(data)

def test_pac_type_accepts_valid_fixed_buffers(self):
requestor = pack(
'<BB6sI',
1,
1,
b'\x00\x00\x00\x00\x00\x05',
21,
)
buffers = [
(pac.PAC_ATTRIBUTES_INFO, pack('<II', 2, 3)),
(pac.PAC_REQUESTOR_INFO, requestor),
(
pac.PAC_SERVER_CHECKSUM,
pack('<l', constants.ChecksumTypes.hmac_sha1_96_aes128.value)
+ b'\x00' * 12,
),
(
pac.PAC_PRIVSVR_CHECKSUM,
pack('<l', constants.ChecksumTypes.hmac_sha1_96_aes256.value)
+ b'\x00' * 14,
),
(
pac.PAC_SERVER_CHECKSUM,
pack('<l', constants.ChecksumTypes.hmac_md5.value)
+ b'\x00' * 16,
),
]

for buffer_type, payload in buffers:
with self.subTest(buffer_type=buffer_type):
pac.PACTYPE(self._build_single_buffer_pac(buffer_type, payload))

def test_build_pac_type_uses_explicit_buffer_order(self):
pac_infos = {
pac.PAC_SERVER_CHECKSUM: b'server',
Expand Down
Loading