From 3e53646d7fc3247b72585ac686eaa5bc1e154a4d Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 13 Feb 2020 02:58:24 +0000 Subject: lua/doh-get.lua: fix base64url decoding Avoids malformed packet exception with certain unpadded values. See also https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=16386 --- lua/doh-get.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lua/doh-get.lua b/lua/doh-get.lua index e36ca1c..d95b542 100644 --- a/lua/doh-get.lua +++ b/lua/doh-get.lua @@ -11,6 +11,18 @@ local media_type = DissectorTable.get("media_type") local http_path = Field.new("http.request.uri") local http2_path = Field.new("http2.headers.path") +-- Converts "base64url" to standard "base64" encoding. +local function from_base64url(b64url) + local lastlen = string.len(b64url) % 4 + local b64 = string.gsub(string.gsub(b64url, "-", "+"), "_", "/") + if lastlen == 3 then + b64 = b64 .. "=" + elseif lastlen == 2 then + b64 = b64 .. "==" + end + return b64 +end + function doh_get.dissector(tvb, pinfo, tree) local path = http2_path() or http_path() if not path then @@ -26,6 +38,9 @@ function doh_get.dissector(tvb, pinfo, tree) return end + -- Convert base64url to standard base64 with +/ and padding + dns_b64 = from_base64url(dns_b64) + local dns_tvb = ByteArray.new(dns_b64, true):base64_decode():tvb("Base64-decoded DNS") -- Allow HTTP GET line to be replaced with the DNS one in the Info column. -- cgit v1.2.1