1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-21 15:09:02 +00:00

Add a cache for geoip lookups.

This commit is contained in:
dmiller
2016-08-19 18:33:02 +00:00
parent 907ea3793f
commit df4ef0cb91

View File

@@ -80,14 +80,25 @@ end
-- --
-- GeoPlugin requires no API key and has no limitations on lookups -- GeoPlugin requires no API key and has no limitations on lookups
-- --
local function geoLookup(ip) local function geoLookup(ip, no_cache)
local output = stdnse.registry_get({SCRIPT_NAME, ip})
if output then return output end
local response = http.get("www.geoplugin.net", 80, "/json.gp?ip="..ip, {any_af=true}) local response = http.get("www.geoplugin.net", 80, "/json.gp?ip="..ip, {any_af=true})
local stat, loc = json.parse(response.body) local stat, loc = json.parse(response.body)
if not stat then return nil end if not stat then return nil end
local output = {}
local regionName = (loc.geoplugin_regionName == json.NULL) and "Unknown" or loc.geoplugin_regionName local regionName = (loc.geoplugin_regionName == json.NULL) and "Unknown" or loc.geoplugin_regionName
return loc.geoplugin_latitude, loc.geoplugin_longitude, regionName, loc.geoplugin_countryName output = {
lat = loc.geoplugin_latitude,
lon = loc.geoplugin_longitude,
reg = regionName,
ctry = loc.geoplugin_countryName
}
if not no_cache then
stdnse.registry_add_table({SCRIPT_NAME}, ip, output)
end
return output
end end
local function createKMLFile(filename, coords) local function createKMLFile(filename, coords)
@@ -114,7 +125,7 @@ local output_structured = {}
local output = tab.new(4) local output = tab.new(4)
local coordinates = {} local coordinates = {}
local function output_hop(count, ip, name, rtt, lat, lon, ctry, reg) local function output_hop(count, ip, name, rtt, geo)
if ip then if ip then
local label local label
if name then if name then
@@ -122,10 +133,10 @@ local function output_hop(count, ip, name, rtt, lat, lon, ctry, reg)
else else
label = ("%s"):format(ip) label = ("%s"):format(ip)
end end
if lat then if geo then
table.insert(output_structured, { hop = count, ip = ip, hostname = name, rtt = ("%.2f"):format(rtt), lat = lat, lon = lon }) table.insert(output_structured, { hop = count, ip = ip, hostname = name, rtt = ("%.2f"):format(rtt), lat = geo.lat, lon = geo.lon })
tab.addrow(output, count, ("%.2f"):format(rtt), label, ("%.3f,%.3f %s (%s)"):format(lat, lon, ctry, reg)) tab.addrow(output, count, ("%.2f"):format(rtt), label, ("%.3f,%.3f %s (%s)"):format(geo.lat, geo.lon, geo.ctry, geo.reg))
table.insert(coordinates, { hop = count, lat = lat, lon = lon }) table.insert(coordinates, { hop = count, lat = geo.lat, lon = geo.lon })
else else
table.insert(output_structured, { hop = count, ip = ip, hostname = name, rtt = ("%.2f"):format(rtt) }) table.insert(output_structured, { hop = count, ip = ip, hostname = name, rtt = ("%.2f"):format(rtt) })
tab.addrow(output, count, ("%.2f"):format(rtt), label, ("%s,%s"):format("- ", "- ")) tab.addrow(output, count, ("%.2f"):format(rtt), label, ("%s,%s"):format("- ", "- "))
@@ -144,12 +155,13 @@ action = function(host)
-- do not add the current scanned host.ip -- do not add the current scanned host.ip
if hop.ip then if hop.ip then
local rtt = tonumber(hop.times.srtt) * 1000 local rtt = tonumber(hop.times.srtt) * 1000
if ( not(ipOps.isPrivate(hop.ip) ) ) then local geo
local lat, lon, reg, ctry = geoLookup(hop.ip) if not ipOps.isPrivate(hop.ip) then
output_hop(count, hop.ip, hop.name, rtt, lat, lon, ctry, reg) -- be sure not to cache the target address, since it's not likely to be
else -- a hop for something else.
output_hop(count, hop.ip, hop.name, rtt) geo = geoLookup(hop.ip, ipOps.compare_ip(hop.ip, "eq", host.ip) )
end end
output_hop(count, hop.ip, hop.name, rtt, geo)
else else
output_hop(count) output_hop(count)
end end