diff options
author | Trevor Bergeron <mal@sec.gd> | 2021-04-13 04:34:23 -0400 |
---|---|---|
committer | AndersBroman <a.broman58@gmail.com> | 2023-06-12 22:53:46 +0000 |
commit | ce476f79b45a75591240e9055606c96b28c85f74 (patch) | |
tree | 4f9239f787069be0e0017ba8a280f219f5d9c474 /test/lua | |
parent | 15013ab13626d86b6fa3559eff49a768092f1b7f (diff) |
Lua: Fix root zone queries in DNS example
Queries for the root zone (e.g. `dig -t NS`) produce a name of only `\x00`,
which would cause the query parser to fail.
Diffstat (limited to 'test/lua')
-rw-r--r-- | test/lua/dissector.lua | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/test/lua/dissector.lua b/test/lua/dissector.lua index 9ea14b8dc8..95ad5b2157 100644 --- a/test/lua/dissector.lua +++ b/test/lua/dissector.lua @@ -317,8 +317,8 @@ dprint2("MyDNS Prefs registered") local DNS_HDR_LEN = 12 -- the smallest possible DNS query field size --- has to be at least a label length octet, label character, label null terminator, 2-bytes type and 2-bytes class -local MIN_QUERY_LEN = 7 +-- has to be at least a label null terminator, 2-bytes type and 2-bytes class +local MIN_QUERY_LEN = 5 ---------------------------------------- -- some forward "declarations" of helper functions we use in the dissector @@ -452,13 +452,13 @@ function dns.dissector(tvbuf,pktinfo,root) -- adding it to the tree, because we want to identify the correct bytes it covers local label_count, name, name_len = getQueryName(tvbuf:range(pos,pktlen_remaining)) if not label_count then - q_tree:add_expert_info(PI_MALFORMED, PI_ERROR, name) + queries_tree:add_expert_info(PI_MALFORMED, PI_ERROR, name) return end -- now add the first query to the 'Queries' child tree we just created -- we're going to change the string generated by this later, after we figure out the subsequent fields. - -- the whole query field is the query name field length we just got, plus the 20 byte type and 2-byte class + -- the whole query field is the query name field length we just got, plus 2-byte type and 2-byte class. local q_tree = queries_tree:add(pf_query, tvbuf:range(pos, name_len + 4)) q_tree:add(pf_query_name, tvbuf:range(pos, name_len), name) @@ -608,11 +608,12 @@ end -- This function is given the TvbRange object from the dissector() function, and needs to -- parse it. -- On success, it returns three things: the number of labels, the name string, and how --- many bytes it covered of the buffer (which is always 2 more than the name length in this case). +-- many bytes it covered of the buffer. -- On failure, it returns nil and the error message. getQueryName = function (tvbr) local label_count = 0 local name = "" + local name_len = 0 local len_remaining = tvbr:len() if len_remaining < 2 then @@ -623,28 +624,30 @@ getQueryName = function (tvbr) local barray = tvbr:bytes() -- gets a ByteArray of the TvbRange local pos = 0 -- unlike Lua, ByteArray uses 0-based indexing - -- get the first octet/label-length - local label_len = barray:get_index(pos) - if label_len == 0 then - return nil, "invalid initial label length of 0" - end - - while label_len > 0 do + repeat + local label_len = barray:get_index(pos) if label_len >= len_remaining then return nil, "invalid label length of "..label_len end pos = pos + 1 -- move past label length octet - -- append the label and a dot to name string - -- note: this uses the new method of ByteArray:raw(), added in 1.11.3 - name = name .. barray:raw(pos, label_len) .. "." + if label_len > 0 then + -- append the label and a dot to name string + -- note: this uses the new method of ByteArray:raw(), added in 1.11.3 + name = name .. barray:raw(pos, label_len) .. "." + label_count = label_count + 1 + pos = pos + label_len -- move past label + end + name_len = name_len + label_len + 1 len_remaining = len_remaining - (label_len + 1) -- subtract label and its length octet - label_count = label_count + 1 - pos = pos + label_len -- move past label - label_len = barray:get_index(pos) - end + until label_len == 0 -- we appended an extra dot, so get rid of it name = name:sub(1, -2) - return label_count, name, name:len() + 2 + if name == "" then + -- this is the root zone (.) + name = "<Root>" + end + + return label_count, name, name_len end |