mirror of
https://github.com/nmap/nmap.git
synced 2025-12-10 07:31:33 +00:00
Use lpeg parsing in ntp-info to handle escape-quoted strings
This commit is contained in:
@@ -5,6 +5,8 @@ local shortport = require "shortport"
|
|||||||
local stdnse = require "stdnse"
|
local stdnse = require "stdnse"
|
||||||
local string = require "string"
|
local string = require "string"
|
||||||
local table = require "table"
|
local table = require "table"
|
||||||
|
local lpeg = require "lpeg"
|
||||||
|
local U = require "lpeg.utility"
|
||||||
|
|
||||||
description = [[
|
description = [[
|
||||||
Gets the time and configuration variables from an NTP server. We send two
|
Gets the time and configuration variables from an NTP server. We send two
|
||||||
@@ -70,6 +72,14 @@ local TIMEOUT = 5000
|
|||||||
-- Only these fields from the response are displayed with default verbosity.
|
-- Only these fields from the response are displayed with default verbosity.
|
||||||
local DEFAULT_FIELDS = {"version", "processor", "system", "refid", "stratum"}
|
local DEFAULT_FIELDS = {"version", "processor", "system", "refid", "stratum"}
|
||||||
|
|
||||||
|
-- comma-space-separated key=value pairs with optional quotes
|
||||||
|
local kvmatch = U.localize( {
|
||||||
|
lpeg.V "space"^0 * lpeg.V "kv" * lpeg.P(",")^-1,
|
||||||
|
kv = lpeg.V "key" * "=" * lpeg.V "value",
|
||||||
|
key = lpeg.C( (lpeg.V "alnum" + "_")^1 ),
|
||||||
|
value = U.escaped_quote() + lpeg.C((lpeg.P(1) - ",")^1),
|
||||||
|
} )
|
||||||
|
|
||||||
action = function(host, port)
|
action = function(host, port)
|
||||||
local status
|
local status
|
||||||
local buftres, bufrlres
|
local buftres, bufrlres
|
||||||
@@ -112,12 +122,17 @@ action = function(host, port)
|
|||||||
-- preceded by a 2-byte length.
|
-- preceded by a 2-byte length.
|
||||||
_, data = bin.unpack(">P", bufrlres, 11)
|
_, data = bin.unpack(">P", bufrlres, 11)
|
||||||
|
|
||||||
-- This parsing is not quite right with respect to quoted strings.
|
-- loop over capture pairs which represent (key, value)
|
||||||
-- Backslash escapes should be interpreted inside strings and commas should
|
local function accumulate_output (...)
|
||||||
-- be allowed inside them.
|
local k, v = ...
|
||||||
for k, q, v in string.gmatch(data, "%s*([%w_]+)=(\"?)([^,\"\r\n]*)%2,?") do
|
if k == nil then return end
|
||||||
output[k] = v
|
output[k] = v
|
||||||
|
return accumulate_output(select(3, ...))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- do the match and accumulate the captures
|
||||||
|
local list = kvmatch^0 / accumulate_output
|
||||||
|
list:match(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
if(#output > 0) then
|
if(#output > 0) then
|
||||||
|
|||||||
Reference in New Issue
Block a user