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

New scripts for geo mapping. Closes #606

This commit is contained in:
dmiller
2016-12-17 14:37:35 +00:00
parent 916ce30d0f
commit c12c2eb1c9
9 changed files with 566 additions and 7 deletions

81
nselib/geoip.lua Normal file
View File

@@ -0,0 +1,81 @@
local nmap = require "nmap"
local stdnse = require "stdnse"
local table = require "table"
_ENV = stdnse.module("geoip", stdnse.seeall)
---
-- Consolidation of GeoIP functions.
--
-- @author "Mak Kolybabi <mak@kolybabi.com>"
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
--- Add a geolocation to the registry
-- @param ip The IP that was geolocated
-- @param lat The latitude in degrees
-- @param lon The longitude in degrees
add = function(ip, lat, lon)
if not nmap.registry.geoip then
nmap.registry.geoip = {}
end
if not nmap.registry.geoip[ip] then
nmap.registry.geoip[ip] = {}
end
local lat_n = tonumber(lat)
if lat_n < -90 or lat_n > 90 then
stdnse.debug1("Invalid latitude for %s: %s.", ip, lat)
return
end
local lon_n = tonumber(lon)
if lon_n < -180 or lon_n > 180 then
stdnse.debug1("Invalid longitude for %s: %s.", ip, lon)
return
end
nmap.registry.geoip[ip]["latitude"] = lat
nmap.registry.geoip[ip]["longitude"] = lon
end
--- Check if any coordinates have been stored in the registry
--@return True if any coordinates have been returned, false otherwise
empty = function()
return not nmap.registry.geoip
end
--- Retrieve the table of coordinates by IP
--@return A table of coordinates keyed by IP.
get_all_by_ip = function()
if empty() then
return nil
end
return nmap.registry.geoip
end
--- Retrieve a table of IPs by coordinate
--@return A table of IPs keyed by coordinate in <code>lat,lon</code> format
get_all_by_gps = function(limit)
if empty() then
return nil
end
local t = {}
for ip, coords in pairs(get_all_by_ip()) do
if limit and limit < #t then
break
end
local key = coords["latitude"] .. "," .. coords["longitude"]
if not t[key] then
t[key] = {}
end
table.insert(t[key], ip)
end
return t
end
return _ENV;