From 510b411ab707d7bec9115226dc7a1ba51b74dadc Mon Sep 17 00:00:00 2001 From: Clinton Thomas <1033162+KernelClint@users.noreply.github.com> Date: Tue, 1 Sep 2026 13:29:02 -0400 Subject: [PATCH] utils: delete the TLS key-log scratch file after loading it AI-Assisted: yes (GPT-5.6-Cyber) --- scapy/utils.py | 13 +++++++------ test/scapy/layers/tls/tls.uts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/scapy/utils.py b/scapy/utils.py index 0879f3322bf..b72224009fd 100644 --- a/scapy/utils.py +++ b/scapy/utils.py @@ -2005,12 +2005,13 @@ def _read_block_dsb(self, block, size): from scapy.layers.tls.session import load_nss_keys # Write Key Log to a file and parse it - filename = get_temp_file() - with open(filename, "wb") as fd: - fd.write(secrets_data) - fd.close() - - keys = load_nss_keys(filename) + filename = get_temp_file(keep=True) + try: + with open(filename, "wb") as fd: + fd.write(secrets_data) + keys = load_nss_keys(filename) + finally: + os.unlink(filename) if not keys: warning("PcapNg: invalid TLS Key Log in DSB!") else: diff --git a/test/scapy/layers/tls/tls.uts b/test/scapy/layers/tls/tls.uts index e3d194b2763..64409b96e54 100644 --- a/test/scapy/layers/tls/tls.uts +++ b/test/scapy/layers/tls/tls.uts @@ -1633,6 +1633,38 @@ if shutil.which("editcap"): assert b"BEGIN PRIVATE KEY" in packets[28].inner.msg[0].data conf = bck_conf += pcapng Decryption Secrets Block does not retain a temporary file + +import struct +import scapy.layers.tls.session + +def pcapng_block(block_type, body): + body += b"\x00" * (-len(body) % 4) + length = 12 + len(body) + return ( + struct.pack("