1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-01 12:29:03 +00:00

Convert more bin.lua packings

This commit is contained in:
dmiller
2018-09-06 16:00:46 +00:00
parent 201ee75059
commit f62d39f801
7 changed files with 46 additions and 78 deletions

View File

@@ -2,7 +2,6 @@ local comm = require "comm"
local nmap = require "nmap"
local shortport = require "shortport"
local string = require "string"
local bin = require "bin"
local stdnse = require "stdnse"
description = [[
@@ -151,22 +150,22 @@ action = function(host, port)
local o = stdnse.output_table()
local pos = 5
pos, o["game"] = bin.unpack("p", data, pos)
pos, o["port"] = bin.unpack("p", data, pos)
pos, o["server name"] = bin.unpack("p", data, pos)
pos, o["game type"] = bin.unpack("p", data, pos)
pos, o["map"] = bin.unpack("p", data, pos)
pos, o["version"] = bin.unpack("p", data, pos)
pos, o["passworded"] = bin.unpack("p", data, pos)
pos, o["num players"] = bin.unpack("p", data, pos)
pos, o["max players"] = bin.unpack("p", data, pos)
o["game"],
o["port"],
o["server name"],
o["game type"],
o["map"],
o["version"],
o["passworded"],
o["num players"],
o["max players"], pos = string.unpack(("s1"):rep(9), data, pos)
-- extract the key-value pairs
local kv = stdnse.output_table()
o["settings"] = kv
while data:byte(pos) ~= 1 do
local key, value
pos, key, value = bin.unpack("pp", data, pos)
key, value, pos = string.unpack("s1s1", data, pos)
kv[key] = value
end
pos = pos + 1
@@ -181,22 +180,22 @@ action = function(host, port)
local player = stdnse.output_table()
if (flags & 1) ~= 0 then
pos, player.name = bin.unpack("p", data, pos)
player.name, pos = string.unpack("s1", data, pos)
end
if (flags & 2) ~= 0 then
pos, player.team = bin.unpack("p", data, pos)
player.team, pos = string.unpack("s1", data, pos)
end
if (flags & 4) ~= 0 then
pos, player.skin = bin.unpack("p", data, pos)
player.skin, pos = string.unpack("s1", data, pos)
end
if (flags & 8) ~= 0 then
pos, player.score = bin.unpack("p", data, pos)
player.score, pos = string.unpack("s1", data, pos)
end
if (flags & 16) ~= 0 then
pos, player.ping = bin.unpack("p", data, pos)
player.ping, pos = string.unpack("s1", data, pos)
end
if (flags & 32) ~= 0 then
pos, player.time = bin.unpack("p", data, pos)
player.time, pos = string.unpack("s1", data, pos)
end
players["player " .. playernum] = player

View File

@@ -1,4 +1,3 @@
local bin = require "bin"
local nmap = require "nmap"
local shortport = require "shortport"
local stdnse = require "stdnse"
@@ -948,7 +947,7 @@ function field_size(packet)
end
-- unpack a string of length <value>
local charset, info
offset, charset, info = bin.unpack("CA" .. tostring(value), packet, offset)
charset, info, offset = string.unpack("Bc" .. tostring(value), packet, offset)
-- return information that was found in the packet
if charset == 0 then -- UTF-8
return info
@@ -980,7 +979,7 @@ end
--- Sends a query for Property Identifier id (a number) on socket
local function send_query(socket, id)
-- Wireshark dissection:
local query = bin.pack(">C2SC7ICC",
local query = string.pack(">BB I2 BBBBBBB I4 BB",
0x81, -- Type: BACnet/IP (Annex J)
0x0a, -- Function: Original-Unicast-NPDU
0x0011, -- BVLC-Length: 4 of 17 bytes
@@ -1037,8 +1036,8 @@ function standard_query(socket, type)
end
-- validate valid BACNet Packet
if( string.byte(response, 1) == 0x81 ) then
-- Lookup byte 7 (pakcet type)
local pos, value = bin.unpack("C", response, 7)
-- Lookup byte 7 (packet type)
local value = string.byte(response, 7)
-- verify that the response packet was not an error packet
if( value ~= 0x50) then
--collect information by looping thru the packet
@@ -1081,12 +1080,12 @@ function vendornum_query(socket)
end
-- validate valid BACNet Packet
if( string.byte(response, 1) == 0x81 ) then
local pos, value = bin.unpack("C", response, 7)
local value = string.byte(response, 7)
--if the vendor query resulted in an error
if( value ~= 0x50) then
-- read values for byte 18 in the packet data
-- this value determines if vendor number is 1 or 2 bytes
pos, value = bin.unpack("C", response, 18)
value = string.byte(response, 18)
else
stdnse.debug1("Error receiving Vendor ID: BACNet Error")
return nil
@@ -1100,8 +1099,7 @@ function vendornum_query(socket)
-- if value is 22 (byte 18)
elseif( value == 0x22 ) then
-- convert hex to decimal
local vendornum
pos, vendornum = bin.unpack(">S", response, 19)
local vendornum = string.unpack(">I2", response, 19)
-- look up vendor name from table
return vendor_lookup(vendornum)
else
@@ -1157,7 +1155,7 @@ action = function(host, port)
-- if the response starts with 0x81 then its BACNet
if( string.byte(response, 1) == 0x81 ) then
local pos, value = bin.unpack("C", response, 7)
local value = string.byte(response, 7)
--if the first query resulted in an error
--
if( value == 0x50) then
@@ -1179,9 +1177,8 @@ action = function(host, port)
to_return["Vendor Name"] = standard_query(sock, "vendor")
-- Instance Number (object number)
local instance_upper, instance
pos, instance_upper, instance = bin.unpack("C>S", response, 20)
to_return["Object-identifier"] = instance_upper * 0x10000 + instance
local instance = string.unpack(">I3", response, 20)
to_return["Object-identifier"] = instance
--Firmware Verson
to_return["Firmware"] = standard_query(sock, "firmware")