Ошибка недопустимого тега: криптография при выполнении задачи декодирования данных SSLPython

Программы на Python
Anonymous
Ошибка недопустимого тега: криптография при выполнении задачи декодирования данных SSL

Сообщение Anonymous »

Я пытаюсь декодировать трафик TLSv1.3 и застрял в ошибке «Неверный тег». Я использую грубую силу, просто чтобы проверить свою логику.
Основная функция, которую я использовал:

Код: Выделить всё

if __name__ == '__main__':
# from key log secret
application_data_secret = hex_to_bytes('3d1af2497112e09478dc8b0fa993f857ab1fd25e3e8d412e79398c4809bbd6cd')
context = b""
"""
As per RFC 5116,
len(iv)=12
len(key)=32
len(tag)=16
"""
# Extracting keys,iv for application data as per RFC 8446, 7.3
client_write_iv = hkdf_expand_label(application_data_secret,
bytes("iv", encoding="utf-8"), context,
12).hex()  # len=12, RFC 5116, 5.3
client_write_key = hkdf_expand_label(application_data_secret,
bytes("key", encoding="utf-8"),
context, 32)  # len=32, RFC 5116, 5.2

# from PCAP
seq_no = "dca514d8"
aead_encrypted_hex = "15b96c01325887c0cce1920b498995ba7b2b7cd9144dab737f02e62aba4f558cef84bdb828ee5aa9ce26eabfbc9e51777e1d312c4eae110313e4acaa9e384d5000b1bdc28498144809d0a206782f60000da248707f1ea8"
# as per rfc 8446, 5116
# additional data = TLSCiphertext.opaque_type || TLSCipherText.legacy_record_version || TLSCiphertext.length
# where || denotes concatenation operation
additional_data_hex = "1703030057"

# Nonce number as per RFC 8446, 5.3
nonce_hex = xor_hex(client_write_iv, seq_no)

plaintext_hex = aead_decrypt(client_write_key, nonce_hex,
additional_data_hex, aead_encrypted_hex)
print(plaintext_hex)
Большинство деталей взяты непосредственно из файла PCAP, открытого WireShark, и я просто проверяю здесь логическую часть.
У меня есть ошибка в aaed_decrypt при запуске функции:

Код: Выделить всё

def aead_decrypt(peer_write_key_hex: bytes, nonce_hex: str,
additional_data_hex: str, aead_encrypted_hex: str) -> str:

peer_write_key = peer_write_key_hex
nonce = hex_to_bytes(nonce_hex)
additional_data = hex_to_bytes(additional_data_hex)
aead_encrypted = hex_to_bytes(aead_encrypted_hex)
print(peer_write_key)

# Perform AEAD decryption using AES-GCM
aesgcm = AESGCM(peer_write_key)
plaintext = aesgcm.decrypt(nonce, aead_encrypted, additional_data)

# Convert plaintext to hex and return
return bytes_to_hex(plaintext)
Однако я думаю, что делаю что-то не так в функции hkdf-expand-label при извлечении hkdf-label, проверьте это один раз:

Код: Выделить всё

    def hkdf_expand(prk: bytes, info: bytes, length: int) -> bytes:
"""Perform the HKDF expand step."""
hash_len = hashlib.sha256().digest_size
n = (length + hash_len - 1) // hash_len
okm = b""
output_block = b""

for i in range(n):
output_block = hmac.new(prk, output_block + info + bytes([i + 1]),
hashlib.sha256).digest()
okm += output_block

return okm[:length]

def hkdf_extract(salt: bytes, ikm: bytes) -> bytes:
"""Perform the HKDF extract step."""
return hmac.new(salt, ikm, hashlib.sha256).digest()

# Prefix the label with "tls13 "
prefix_label = "tls13 ".encode() + label.encode()

# Construct the label as per the TLS 1.3 specification
hkdf_label = pack("!H", length) + bytes(
[len(prefix_label)]) + prefix_label + bytes([len(context)]) + context

# Perform the HKDF-Expand using SHA-256
prk = hkdf_extract(b"", secret)
return hkdf_expand(prk, hkdf_label, length)
Помогите мне определить ошибку.


Подробнее здесь: https://stackoverflow.com/questions/787 ... oding-task

Вернуться в «Python»