This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

LUA: problem using bit.bxor

0

Hi,

I'm struggling with a problem in a LUA script. I need to XOR a byte with the value 0xa5. The code looks like this:

  tvbr = tvb:range(ptr, 1) -- set up a range
  local rop_id = tvbr:bytes() -- extract the RopId

if xor_magic_set then rop_id = bit.bxor(rop_id, 0xa5) – error occurs here end

The line that extracts the RopId works fine, but the bit.bxor line throws an error:

Lua Error: [string “C:\Program Files\Wireshark2\plugins\msmapi.lua”]:173: bad argument #1 to ‘bxor’ (number expected, got userdata)

How do I convert the extracted byte (rop_id) into a value type that I can XOR? Any advice much appreciated.

Thanks and regards…Paul

asked 26 Feb ‘16, 01:36

PaulOfford's gravatar image

PaulOfford
131283237
accept rate: 11%


One Answer:

0

It should help to use tvbr:uint() instead of tvbr:bytes() when extracting the RopId.

answered 26 Feb '16, 07:42

sindy's gravatar image

sindy
6.0k4851
accept rate: 24%

Thanks Sindy.

What I didn't say earlier was that I actually eventually needed to apply the XOR to a block of bytes. I realised that I was mixing tvbs with byte arrays and eventually converted the block of bytes to a byte array (baIn below), which meant I could then do this:

for i = 0, (baIn:len() - 1) do
  baOut:set_size(i + 1)
  baOut:set_index( i, bit.bxor(baIn:get_index(i), 0xa5) )
end

Best regards...Paul

(27 Feb '16, 08:21) PaulOfford