Implement more robust IEC61850 TPKT parser. Close #3494
Some checks failed
nmap multiplatform autobuilds / build (arm64, gcc, ubuntu-latest-gcc-arm64, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, freebsd-15-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-15-clang, macos-15) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-26-clang, macos-26) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, netbsd-10-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, openbsd-7-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, solaris-11-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, ubuntu-latest-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (egcc, openbsd-7-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, freebsd-15-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, netbsd-10-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, solaris-11-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, ubuntu-latest-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (msvc, windows-latest-msvc, windows-latest) (push) Has been cancelled

This commit is contained in:
nnposter 2026-09-27 02:53:52 +00:00
parent 774760ab08
commit a885c55e2f
2 changed files with 24 additions and 19 deletions

View file

@ -33,6 +33,10 @@ o [Zenmap][GH#3431] Fix "KeyError: 'vl_6_logo'" crash in Zenmap 7.991.
o [NSE] Added DHX2 authentication method to afp.lua [Daniel Miller]
o [NSE][GH#3494] Malformed TPKT header could have caused parser infinite loop
in library iec61850mms. Truncated data were silently ignored.
[Zasnit1, nnposter]
o [NSE][GH#3447] IP options parsing by the packet library no longer crashes
on incomplete options. IP header data after EOOL are no longer parsed as
options. [Ibrahim AlJaafreh, nnposter]

View file

@ -30,29 +30,30 @@ MMSDecoder = {
unpackmmsFromTPKT = function(self, tpktStr)
-- unpack TPKT and COTP
local TPKT_pos = 1
local COTP_pos = 5
local COTP_last = false
local TPKT_ver, TPKT_res, TPKT_len
local COTP_len, COTP_type, COTP_tpdu
local HDRFMT = ">xxI2xxI1"
local HDRLEN = HDRFMT:packsize()
local OSI_Session = {}
while not COTP_last do
TPKT_ver, TPKT_res, TPKT_len = string.unpack("i1c1>i2", tpktStr, TPKT_pos)
COTP_len, COTP_type, COTP_tpdu = string.unpack("i1c1c1", tpktStr, COTP_pos)
COTP_last = COTP_tpdu == "\x80"
OSI_Session[#OSI_Session+1] = string.sub(tpktStr, TPKT_pos + 7, TPKT_pos + TPKT_len - 1)
if not COTP_last then
TPKT_pos = TPKT_pos + TPKT_len
COTP_pos = TPKT_pos + 4
local pos = 1
repeat
if pos + HDRLEN - 1 > #tpktStr then
stdnse.debug1("truncated TPKT/COTP data")
return nil
end
end
local TPKT_len, COTP_tpdu = HDRFMT:unpack(tpktStr, pos)
if TPKT_len < HDRLEN then
stdnse.debug1("invalid TPKT/COTP header")
return nil
end
local eodpos = pos + TPKT_len - 1
if eodpos > #tpktStr then
stdnse.debug1("truncated TPKT/COTP data")
return nil
end
table.insert(OSI_Session, tpktStr:sub(pos + HDRLEN, eodpos))
pos = pos + TPKT_len
until COTP_tpdu == 0x80
OSI_Session = table.concat(OSI_Session)
local newpos = 5 -- start of ISO 8823
local type, len, dummy