mirror of
https://github.com/nmap/nmap.git
synced 2026-12-01 03:50:57 +00:00
fix(nse): update http-google-malware to Safe Browsing API v4
Google deprecated the v3 API endpoint (sb-ssl.google.com). Update the
script to use the v4 Lookup API (safebrowsing.googleapis.com/v4).
Key changes:
- Switch from GET to POST with JSON body (required by v4 API)
- Use http.post() with correct host/port/path/options signature
- Set platformTypes to ANY_PLATFORM (was WINDOWS in the broken attempt)
- Include UNWANTED_SOFTWARE threat type alongside MALWARE and
SOCIAL_ENGINEERING
- Parse the JSON response with json.parse() instead of string-searching
the body (the old approach could false-positive on request echoes)
- Deduplicate matched threat types in output
- Detect the safe case via empty body / bare {} rather than status 204
- Update documentation URLs from deprecated code.google.com to
developers.google.com
Fixes #1474
This commit is contained in:
parent
a2b2ee9e31
commit
3b00eb82ee
1 changed files with 89 additions and 46 deletions
|
|
@ -1,4 +1,5 @@
|
|||
local http = require "http"
|
||||
local json = require "json"
|
||||
local nmap = require "nmap"
|
||||
local shortport = require "shortport"
|
||||
local stdnse = require "stdnse"
|
||||
|
|
@ -6,39 +7,34 @@ local string = require "string"
|
|||
local table = require "table"
|
||||
|
||||
description = [[
|
||||
Checks if hosts are on Google's blacklist of suspected malware and phishing
|
||||
servers. These lists are constantly updated and are part of Google's Safe
|
||||
Browsing service.
|
||||
Checks if hosts are on Google's blacklist of suspected malware, phishing,
|
||||
and unwanted software. These lists are constantly updated and are part of
|
||||
Google's Safe Browsing service.
|
||||
|
||||
To do this the script queries the Google's Safe Browsing service and you need
|
||||
to have your own API key to access Google's Safe Browsing Lookup services. Sign
|
||||
up for yours at http://code.google.com/apis/safebrowsing/key_signup.html
|
||||
To use this script you need a Google API key with the Safe Browsing API v4
|
||||
enabled. Get yours at https://developers.google.com/safe-browsing/v4/get-started
|
||||
|
||||
* To learn more about Google's Safe Browsing:
|
||||
http://code.google.com/apis/safebrowsing/
|
||||
|
||||
* To register and get your personal API key:
|
||||
http://code.google.com/apis/safebrowsing/key_signup.html
|
||||
* Safe Browsing API v4 documentation:
|
||||
https://developers.google.com/safe-browsing/v4/lookup-api
|
||||
]]
|
||||
|
||||
---
|
||||
-- @usage
|
||||
-- nmap -p80 --script http-google-malware <host>
|
||||
-- nmap -p80 --script http-google-malware --script-args http-google-malware.api=<API_KEY> <target>
|
||||
--
|
||||
-- @output
|
||||
-- PORT STATE SERVICE
|
||||
-- 80/tcp open http
|
||||
-- |_http-google-malware.nse: Host is known for distributing malware.
|
||||
-- |_http-google-malware: Host is known for distributing malware.
|
||||
--
|
||||
-- @args http-google-malware.url URL to check. Default: <code>http/https</code>://<code>host</code>
|
||||
-- @args http-google-malware.api API key for Google's Safe Browsing Lookup service
|
||||
-- @args http-google-malware.api Google Safe Browsing API v4 key
|
||||
---
|
||||
|
||||
author = "Paulino Calderon <calderon@websec.mx>"
|
||||
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
|
||||
categories = {"malware", "discovery", "safe", "external"}
|
||||
|
||||
|
||||
portrule = shortport.http
|
||||
|
||||
---#########################
|
||||
|
|
@ -47,60 +43,107 @@ portrule = shortport.http
|
|||
local APIKEY = ""
|
||||
---#########################
|
||||
|
||||
--Builds Google Safe Browsing query
|
||||
--@param apikey Api key
|
||||
--@return Url
|
||||
local function build_qry(apikey, url)
|
||||
return string.format("https://sb-ssl.google.com/safebrowsing/api/lookup?client=%s&apikey=%s&appver=1.5.2&pver=3.0&url=%s", SCRIPT_NAME, apikey, url)
|
||||
local API_HOST = "safebrowsing.googleapis.com"
|
||||
local API_PORT = {number = 443, service = "https", protocol = "tcp"}
|
||||
local API_PATH = "/v4/threatMatches:find"
|
||||
|
||||
-- Builds the JSON POST body for the Safe Browsing v4 Lookup API.
|
||||
local function build_request_body(target_url)
|
||||
return json.generate({
|
||||
client = {
|
||||
clientId = SCRIPT_NAME,
|
||||
clientVersion = "1.0"
|
||||
},
|
||||
threatInfo = {
|
||||
threatTypes = {"MALWARE", "SOCIAL_ENGINEERING", "UNWANTED_SOFTWARE"},
|
||||
platformTypes = {"ANY_PLATFORM"},
|
||||
threatEntryTypes = {"URL"},
|
||||
threatEntries = {{url = target_url}}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
local function fail (err) return stdnse.format_output(false, err) end
|
||||
local function fail(err)
|
||||
return stdnse.format_output(false, err)
|
||||
end
|
||||
|
||||
---
|
||||
--MAIN
|
||||
-- MAIN
|
||||
---
|
||||
action = function(host, port)
|
||||
local apikey = stdnse.get_script_args("http-google-malware.api") or APIKEY
|
||||
local malware_found = false
|
||||
local target
|
||||
local output_lns = {}
|
||||
|
||||
--Use the host IP if a hostname isn't available
|
||||
if not(host.targetname) then
|
||||
local target
|
||||
if not host.targetname then
|
||||
target = host.ip
|
||||
else
|
||||
target = host.targetname
|
||||
end
|
||||
|
||||
local target_url = stdnse.get_script_args("http-google-malware.url") or string.format("%s://%s", port.service, target)
|
||||
local target_url = stdnse.get_script_args("http-google-malware.url")
|
||||
or string.format("%s://%s", port.service, target)
|
||||
|
||||
if string.len(apikey) < 25 then
|
||||
if #apikey < 25 then
|
||||
return fail(("No API key found. Use the %s.api argument"):format(SCRIPT_NAME))
|
||||
end
|
||||
|
||||
stdnse.debug1("Checking host %s", target_url)
|
||||
local qry = build_qry(apikey, target_url)
|
||||
local req = http.get_url(qry, {any_af=true})
|
||||
stdnse.debug2("%s", qry)
|
||||
stdnse.debug1("Checking URL: %s", target_url)
|
||||
|
||||
if ( req.status > 400 ) then
|
||||
return fail("Request failed (invalid API key?)")
|
||||
local body = build_request_body(target_url)
|
||||
local path = API_PATH .. "?key=" .. apikey
|
||||
|
||||
stdnse.debug2("POST https://%s%s", API_HOST, API_PATH)
|
||||
stdnse.debug2("Payload: %s", body)
|
||||
|
||||
local response = http.post(API_HOST, API_PORT, path,
|
||||
{scheme = "https", header = {["Content-Type"] = "application/json"}, any_af = true},
|
||||
nil, body)
|
||||
|
||||
if not response then
|
||||
return fail("HTTP request failed (no response)")
|
||||
end
|
||||
|
||||
--The Safe Lookup API responds with a type when site is on the lists
|
||||
if req.body then
|
||||
if http.response_contains(req, "malware") then
|
||||
output_lns[#output_lns+1] = "Host is known for distributing malware."
|
||||
malware_found = true
|
||||
end
|
||||
if http.response_contains(req, "phishing") then
|
||||
output_lns[#output_lns+1] = "Host is known for being used in phishing attacks."
|
||||
malware_found = true
|
||||
end
|
||||
if response.status and response.status >= 400 then
|
||||
return fail(("Request failed with status %d (invalid API key?)"):format(response.status))
|
||||
end
|
||||
--For the verbose lovers
|
||||
if req.status == 204 and nmap.verbosity() >= 2 and not(malware_found) then
|
||||
output_lns[#output_lns+1] = "Host is safe to browse."
|
||||
|
||||
-- An empty body or bare "{}" means no threats found.
|
||||
if not response.body or response.body:match("^%s*{%s*}%s*$") or #response.body == 0 then
|
||||
if nmap.verbosity() >= 2 then
|
||||
output_lns[#output_lns + 1] = "Host is safe to browse."
|
||||
end
|
||||
else
|
||||
local ok, parsed = json.parse(response.body)
|
||||
if not ok or type(parsed) ~= "table" then
|
||||
stdnse.debug1("Failed to parse API response: %s", response.body)
|
||||
return fail("Failed to parse API response")
|
||||
end
|
||||
|
||||
if parsed.matches then
|
||||
local seen = {}
|
||||
for _, match in ipairs(parsed.matches) do
|
||||
local threat = match.threatType
|
||||
if threat and not seen[threat] then
|
||||
seen[threat] = true
|
||||
if threat == "MALWARE" then
|
||||
output_lns[#output_lns + 1] = "Host is known for distributing malware."
|
||||
malware_found = true
|
||||
elseif threat == "SOCIAL_ENGINEERING" then
|
||||
output_lns[#output_lns + 1] = "Host is known for being used in phishing attacks."
|
||||
malware_found = true
|
||||
elseif threat == "UNWANTED_SOFTWARE" then
|
||||
output_lns[#output_lns + 1] = "Host is known for distributing unwanted software."
|
||||
malware_found = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if nmap.verbosity() >= 2 and not malware_found then
|
||||
output_lns[#output_lns + 1] = "Host is safe to browse."
|
||||
end
|
||||
end
|
||||
|
||||
if #output_lns > 0 then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue