1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
local http = require "http"
local geoip = require "geoip"
local io = require "io"
local oops = require "oops"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"
local url = require "url"
description = [[
This script queries the Nmap registry for the GPS coordinates of targets stored
by previous geolocation scripts and renders a Bing Map of markers representing
the targets.
The Bing Maps REST API has a limit of 100 markers, so if more coordinates are
found, only the top 100 markers by number of IPs will be shown.
Additional information for the Bing Maps REST Services API can be found at:
- https://msdn.microsoft.com/en-us/library/ff701724.aspx
]]
---
-- @usage
-- nmap -sn -Pn --script ip-geolocation-geoplugin,ip-geolocation-map-bing --script-args ip-geolocation-map-bing.api_key=[redacted],ip-geolocation-map-bing.map_path=map.png <target>
--
-- @output
-- | ip-geolocation-map-bing:
-- |_ The map has been saved at 'map.png'.
--
-- @args ip-geolocation-map-bing.api_key The required Bing Maps API key for your
-- account. An API key can be generated at https://www.bingmapsportal.com/
--
-- @args ip-geolocation-map-bing.center GPS coordinates defining the center of the
-- image. If omitted, Bing Maps will choose a center that shows all the
-- markers.
--
-- @args ip-geolocation-map-bing.format The default value is 'jpeg', 'png', and
-- 'gif' are also allowed.
--
-- @args ip-geolocation-map-bing.language The default value is 'en', but other
-- two-letter language codes are accepted.
--
-- @args ip-geolocation-map-bing.layer The default value is 'Road',
-- 'Aerial', and 'AerialWithLabels' are also allowed.
--
-- @args ip-geolocation-map-bing.map_path The path at which the rendered
-- Bing Map will be saved to the local filesystem.
--
-- @args ip-geolocation-map-bing.marker_style This argument can apply styling
-- to the markers.
-- https://msdn.microsoft.com/en-us/library/ff701719.aspx
--
-- @args ip-geolocation-map-bing.size The default value is '640x640' pixels, but
-- can be changed so long as the width is between 80 and 2000 pixels and the
-- height is between 80 and 1500 pixels.
--
-- @see ip-geolocation-geoplugin.nse
-- @see ip-geolocation-ipinfodb.nse
-- @see ip-geolocation-map-google.nse
-- @see ip-geolocation-map-kml.nse
-- @see ip-geolocation-maxmind.nse
author = "Mak Kolybabi <mak@kolybabi.com>"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"external", "safe"}
local render = function(params, options)
-- Format marker style for inclusion in parameters.
local style = ""
if options["marker_style"] then
style = ";" .. options["marker_style"]
end
-- Add in a marker for each host.
local markers = {}
for coords, ip in pairs(geoip.get_all_by_gps()) do
table.insert(markers, {#ip, "pp=" .. coords .. style})
end
if #markers > 100 then
-- API is limited to 100 markers
stdnse.verbose1("Bing Maps API limits render to 100 markers. Some results not mapped.")
-- sort by number of IPs so we map the biggest groups
table.sort(markers, function (a, b) return a[1] < b[1] end)
end
local out_markers = {}
for i=1, #markers do
if i > 100 then break end
out_markers[#out_markers+1] = markers[i][2]
end
local body = table.concat(out_markers, "&")
-- Format the parameters into a properly encoded URL.
local query = "/REST/v1/Imagery/Map/" .. options["layer"] .. "?" .. url.build_query(params)
stdnse.debug1("The query URL is: %s", query)
stdnse.debug1("The query body is: %s", body)
local headers = {
["header"] = {
["Content-Type"] = "text/plain; charset=utf-8"
}
}
local res = http.post("dev.virtualearth.net", 80, query, headers, nil, body)
if not res or res.status ~= 200 then
stdnse.debug1("Error %d from API: %s", res.status, res.body)
return false, ("Failed to receive map using query '%s'."):format(query)
end
local f = io.open(options["map_path"], "w")
if not f then
return false, ("Failed to open file '%s'."):format(options["map_path"])
end
if not f:write(res.body) then
return false, ("Failed to write file '%s'."):format(options["map_path"])
end
f:close()
local msg
return true, ("The map has been saved at '%s'."):format(options["map_path"])
end
local parse_args = function()
local options = {}
local params = {}
local api_key = stdnse.get_script_args(SCRIPT_NAME .. '.api_key')
if not api_key then
return false, "Need to specify an API key, get one at https://www.bingmapsportal.com/."
end
params["key"] = api_key
local center = stdnse.get_script_args(SCRIPT_NAME .. ".center")
if center then
params["centerPoint"] = center
end
local format = stdnse.get_script_args(SCRIPT_NAME .. ".format")
if format then
params["format"] = format
end
local language = stdnse.get_script_args(SCRIPT_NAME .. ".language")
if language then
params["language"] = language
end
local layer = stdnse.get_script_args(SCRIPT_NAME .. ".layer")
if not layer then
layer = "Road"
end
options["layer"] = layer
local map_path = stdnse.get_script_args(SCRIPT_NAME .. '.map_path')
if map_path then
options["map_path"] = map_path
else
return false, "Need to specify a path for the map."
end
local size = stdnse.get_script_args(SCRIPT_NAME .. ".size")
if not size then
-- This size is arbitrary, and is chosen to match the default that Google
-- Maps will produce.
size = "640x640"
end
size = string.gsub(size, "x", ",")
params["mapSize"] = size
return true, params, options
end
postrule = function()
-- Only run if a previous script has registered geolocation data.
return not geoip.empty()
end
action = function()
-- Parse and sanity check the command line arguments.
local status, params, options = oops.raise(
"Script argument problem",
parse_args())
if not status then
return params
end
-- Render the map.
return oops.output(render(params, options))
end
|