在 Wireshark Postdissector 中显示数字的位值

Displaying the bits values of a number in Wireshark Postdissector

我正在使用 LUA.For 这个自定义协议编写一个自定义协议的 wireshark 解析器,没有底层的 TCP 端口或 UDP 端口,因此我写了一个 postdissector。 我能够从下面的层捕获有效负载并将其转换为字符串。

local io_b = tostring(customprotocol)

在此之后,io_b有以下数据

io_b = 10:10:10:10:01:0f:00:0d:00:00:00:00:01:00:00:00:00:20:0a:00:00

起初我用 : 作为分隔符拆分这个字符串,然后将元素复制到 array/table.

datafields = {}
index = 1

for value in string.gmatch(io_b, "[^:]+") do 
   datafields[index] = value
   index = index + 1
end

然后我读取数据字段数组的每个元素作为 uint8 值并检查是否在该数据字段中设置了一个位 element.How 以确保 table 的每个元素都是 uint8?

function lshift(x, by)
    return x * 2 ^ by
end

--checks if a bit is set at a position
function IsBitSet( b, pos)
    if b ~= nil then
      return tostring(bit32.band(tonumber(b),lshift(1,pos)) ~= 0)
    else
      return "nil"
    end
end

然后我想显示wireshark.I中每一位的值,不关心前四个字节。该脚本正确显示第 5 个字节(第一个考虑的字节)的每一位,但将第 6 个字节和其他剩余字节的所有位值显示为“nil”。

 local data_in_2 = subtree:add(customprotocol,"secondbyte") 
 data_in_2:add(firstbit,(IsBitSet((datafields[6]),7)))
 data_in_2:add(secondbit,(IsBitSet((datafields[6]),6)))
 data_in_2:add(thirdbit,(IsBitSet((datafields[6]),5)))
 data_in_2:add(fourbit,(IsBitSet((datafields[6]),4)))
 data_in_2:add(fivebit,(IsBitSet((datafields[6]),3)))
 data_in_2:add(sixbit,(IsBitSet((datafields[6]),2)))
 data_in_2:add(sevenbit,(IsBitSet((datafields[6]),1)))
 data_in_2:add(eightbit,(IsBitSet((datafields[6]),0)))

我做错了什么?

也许我错了,但看起来你可以用...

io_b = '10:10:10:10:01:0f:00:0d:00:00:00:00:01:00:00:00:00:20:0a:00:00'
-- Now replace all : on the fly with nothing and convert it with @Egor' comment tip
-- Simply by using string method gsub() from within io_b
b_num = tonumber(io_b:gsub('%:', ''), 16)
print(b_num)
-- Output: 537526272

@shakingwindow - 我不能评论所以我在这里问...
你是说...

io_b = '10:10:10:10:01:0f:00:0d:00:00:00:00:01:00:00:00:00:20:0a:00:00'
-- Converting HEX to string - Replacing : with ,
io_hex = io_b:gsub('[%x]+', '"%1"'):gsub(':', ',')
-- Converting string to table
io_hex_tab = load('return {' .. io_hex .. '}')()
-- Put out key/value pairs by converting HEX value string to a number on the fly
for key, value in pairs(io_hex_tab) do
 print(key, '=', tonumber(value, 16))
end

...那就是...

1   =   16
2   =   16
3   =   16
4   =   16
5   =   1
6   =   15
7   =   0
8   =   13
9   =   0
10  =   0
11  =   0
12  =   0
13  =   1
14  =   0
15  =   0
16  =   0
17  =   0
18  =   32
19  =   10
20  =   0
21  =   0

...?