1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-26 01:19:03 +00:00

Move service fingerprint unwrapping code from scripts to lpeg-utility

This commit is contained in:
dmiller
2015-02-05 04:17:54 +00:00
parent 29174ba87d
commit bf58512bec
3 changed files with 36 additions and 66 deletions

View File

@@ -10,6 +10,8 @@ local assert = assert
local lpeg = require "lpeg"
local stdnse = require "stdnse"
local pairs = pairs
local string = require "string"
local tonumber = tonumber
_ENV = {}
@@ -118,4 +120,34 @@ function debug (grammar, printer)
return grammar
end
do
-- Cache the returned pattern
local getquote = escaped_quote()
-- Substitution pattern to unescape a string
local unescape = lpeg.P {
-- Substitute captures
lpeg.Cs((lpeg.V "simple_char" + lpeg.V "unesc")^0),
-- Escape char is '\'
esc = lpeg.P "\\",
-- Simple char is anything but escape char
simple_char = lpeg.P(1) - lpeg.V "esc",
-- If we hit an escape, process specials or hex code, otherwise remove the escape
unesc = (lpeg.V "esc" * lpeg.Cs( lpeg.V "specials" + lpeg.V "code" + lpeg.P(1) ))/"%1",
-- single-char escapes. These are the only ones service_scan uses
specials = lpeg.S "trn0" / {t="\t", r="\r", n="\n", ["0"]="\0"},
-- hex escape: convert to char
code = (lpeg.P "x" * lpeg.C(lpeg.S "0123456789abcdefABCDEF"^-2))/function(c)
return string.char(tonumber(c,16)) end,
}
--- Turn the service fingerprint reply to a probe into a binary blob
function get_response (fp, probe)
fp = string.gsub(fp, "\nSF:", "")
local i, e = string.find(fp, string.format("%s,%%x+,", probe))
if i == nil then return nil end
return unescape:match(getquote:match(fp, e+1))
end
end
return _ENV