diff --git a/CHANGELOG b/CHANGELOG index d85e8fd52..17835c923 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,12 @@ # Nmap Changelog ($Id$); -*-text-*- +o [NSE] Added the datetime library for performing date and time calculations, + and as a helper to the clock-skew script. + +o [NSE] Added clock-skew for analyzing and reporting clock skew between Nmap + and services that report timestamps. Reports groups of hosts with similar + skews. [Daniel Miller] + o [Ncat][GH#444] Added a -z option to Ncat. Just like the -z option in traditional netcat, it can be used to quicky check the status of a port. Port ranges are not supported. [Abhishek Singh] diff --git a/nselib/datetime.lua b/nselib/datetime.lua new file mode 100644 index 000000000..b93086a3b --- /dev/null +++ b/nselib/datetime.lua @@ -0,0 +1,33 @@ +--- Functions for dealing with dates and timestamps +-- +-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html +-- @class module +-- @name datetime +-- @author Daniel Miller + +local stdnse = require "stdnse" +local os = require "os" +local math = require "math" +_ENV = stdnse.module("datetime", stdnse.seeall) + +--- Record a time difference between the scanner and the target +-- +-- The skew will be recorded in the host's registry for later retrieval and +-- analysis. Adjusts for network distance by subtracting half the smoothed +-- round-trip time. +-- +--@param host The host being scanned +--@param timestamp The target timestamp, in seconds. +--@param received The local time the stamp was received, in seconds. +function record_skew(host, timestamp, received) + local skew_tab = host.registry.datetime_skew + skew_tab = skew_tab or {} + -- No srtt? I suppose we'll ignore it, but this could cause problems + local srtt = host.times and host.times.srtt or 0 + local adjusted = os.difftime(math.floor(timestamp), math.floor(received)) - srtt / 2.0 + skew_tab[#skew_tab + 1] = adjusted + stdnse.debug2("record_skew: %s", adjusted) + host.registry.datetime_skew = skew_tab +end + +return _ENV diff --git a/scripts/http-date.nse b/scripts/http-date.nse index 5f5032861..67eb04ac1 100644 --- a/scripts/http-date.nse +++ b/scripts/http-date.nse @@ -3,6 +3,7 @@ local os = require "os" local shortport = require "shortport" local stdnse = require "stdnse" local string = require "string" +local datetime = require "datetime" description = [[ Gets the date from HTTP-like services. Also prints how much the date @@ -31,8 +32,8 @@ categories = {"discovery", "safe"} portrule = shortport.http action = function(host, port) - local request_time = os.time() local response = http.get(host, port, "/") + local request_time = os.time() if not response.status or not response.header["date"] then return end @@ -47,6 +48,8 @@ action = function(host, port) output_tab.date = stdnse.format_timestamp(response_time, 0) output_tab.delta = os.difftime(response_time, request_time) + datetime.record_skew(host, response_time, request_time) + local output_str = string.format("%s; %s from local time.", response.header["date"], stdnse.format_difftime(os.date("!*t", response_time), os.date("!*t", request_time))) diff --git a/scripts/http-ntlm-info.nse b/scripts/http-ntlm-info.nse index acb59e486..89e68cd78 100644 --- a/scripts/http-ntlm-info.nse +++ b/scripts/http-ntlm-info.nse @@ -1,4 +1,6 @@ local bin = require "bin" +local os = require "os" +local datetime = require "datetime" local http = require "http" local shortport = require "shortport" local stdnse = require "stdnse" @@ -74,6 +76,7 @@ action = function(host, port) local opts = { header = { Authorization = "NTLM " .. auth_blob } } local response = http.get( host, port, root, opts ) + local recvtime = os.time() -- Continue only if correct header (www-authenticate) and NTLM response are included if response.header["www-authenticate"] and string.match(response.header["www-authenticate"], "NTLM (.*)") then @@ -84,6 +87,12 @@ action = function(host, port) -- Leverage smbauth.get_host_info_from_security_blob() for decoding local ntlm_decoded = smbauth.get_host_info_from_security_blob(data) + if ntlm_decoded.timestamp then + -- 64-bit number of 100ns clicks since 1/1/1601 + local unixstamp = ntlm_decoded.timestamp // 10000000 - 11644473600 + datetime.record_skew(host, unixstamp, recvtime) + end + -- Target Name will always be returned under any implementation output.Target_Name = ntlm_decoded.target_realm diff --git a/scripts/imap-ntlm-info.nse b/scripts/imap-ntlm-info.nse index 460bdecaf..cc721ef91 100644 --- a/scripts/imap-ntlm-info.nse +++ b/scripts/imap-ntlm-info.nse @@ -1,4 +1,6 @@ local comm = require "comm" +local os = require "os" +local datetime = require "datetime" local bin = require "bin" local shortport = require "shortport" local sslcert = require "sslcert" @@ -109,6 +111,7 @@ action = function(host, port) return nil end + local recvtime = os.time() socket:close() if string.match(response, "^A%d%d%d%d ") then @@ -134,6 +137,12 @@ action = function(host, port) -- Leverage smbauth.get_host_info_from_security_blob() for decoding local ntlm_decoded = smbauth.get_host_info_from_security_blob(response_decoded) + if ntlm_decoded.timestamp then + -- 64-bit number of 100ns clicks since 1/1/1601 + local unixstamp = ntlm_decoded.timestamp // 10000000 - 11644473600 + datetime.record_skew(host, unixstamp, recvtime) + end + -- Target Name will always be returned under any implementation output.Target_Name = ntlm_decoded.target_realm diff --git a/scripts/ms-sql-ntlm-info.nse b/scripts/ms-sql-ntlm-info.nse index af8ae4131..52bfd370f 100644 --- a/scripts/ms-sql-ntlm-info.nse +++ b/scripts/ms-sql-ntlm-info.nse @@ -1,4 +1,6 @@ local bin = require "bin" +local os = require "os" +local datetime = require "datetime" local mssql = require "mssql" local shortport = require "shortport" local stdnse = require "stdnse" @@ -72,6 +74,7 @@ action = function(host, port) end local status, response, errorDetail = tdsstream:Receive() + local recvtime = os.time() tdsstream:Disconnect() local pos, ttype = bin.unpack("C", response) @@ -87,6 +90,12 @@ action = function(host, port) -- Leverage smbauth.get_host_info_from_security_blob() for decoding local ntlm_decoded = smbauth.get_host_info_from_security_blob(data) + if ntlm_decoded.timestamp then + -- 64-bit number of 100ns clicks since 1/1/1601 + local unixstamp = ntlm_decoded.timestamp // 10000000 - 11644473600 + datetime.record_skew(host, unixstamp, recvtime) + end + -- Target Name will always be returned under any implementation output.Target_Name = ntlm_decoded.target_realm diff --git a/scripts/nntp-ntlm-info.nse b/scripts/nntp-ntlm-info.nse index 131c60ca7..a8640c938 100644 --- a/scripts/nntp-ntlm-info.nse +++ b/scripts/nntp-ntlm-info.nse @@ -1,4 +1,6 @@ local comm = require "comm" +local os = require "os" +local datetime = require "datetime" local shortport = require "shortport" local stdnse = require "stdnse" local base64 = require "base64" @@ -101,6 +103,7 @@ action = function(host, port) end end + local recvtime = os.time() socket:close() -- Continue only if a 381 response is returned @@ -119,6 +122,12 @@ action = function(host, port) -- Leverage smbauth.get_host_info_from_security_blob() for decoding local ntlm_decoded = smbauth.get_host_info_from_security_blob(response_decoded) + if ntlm_decoded.timestamp then + -- 64-bit number of 100ns clicks since 1/1/1601 + local unixstamp = ntlm_decoded.timestamp // 10000000 - 11644473600 + datetime.record_skew(host, unixstamp, recvtime) + end + -- Target Name will always be returned under any implementation output.Target_Name = ntlm_decoded.target_realm diff --git a/scripts/ntp-info.nse b/scripts/ntp-info.nse index 930902997..cc77bb01d 100644 --- a/scripts/ntp-info.nse +++ b/scripts/ntp-info.nse @@ -1,5 +1,8 @@ local bin = require "bin" local comm = require "comm" +local datetime = require "datetime" +local os = require "os" +local math = require "math" local nmap = require "nmap" local shortport = require "shortport" local stdnse = require "stdnse" @@ -100,13 +103,15 @@ action = function(host, port) status, buftres = comm.exchange(host, port, treq, {timeout=TIMEOUT}) if status then - local _, sec, frac, tstamp + local recvtime = os.time() - _, sec, frac = bin.unpack(">II", buftres, 33) + local _, sec, frac = bin.unpack(">II", buftres, 33) -- The NTP epoch is 1900-01-01, so subtract 70 years to bring the date into -- the range Lua expects. The number of seconds at 1970-01-01 is taken from -- the NTP4 reference above. - tstamp = sec - 2208988800 + frac / 0x10000000 + local tstamp = sec - 2208988800 + frac / 0x10000000 + + datetime.record_skew(host, tstamp, recvtime) output["receive time stamp"] = stdnse.format_timestamp(tstamp) end diff --git a/scripts/pop3-ntlm-info.nse b/scripts/pop3-ntlm-info.nse index 9e2808b92..57660da61 100644 --- a/scripts/pop3-ntlm-info.nse +++ b/scripts/pop3-ntlm-info.nse @@ -1,4 +1,6 @@ local comm = require "comm" +local os = require "os" +local datetime = require "datetime" local bin = require "bin" local shortport = require "shortport" local stdnse = require "stdnse" @@ -102,6 +104,7 @@ action = function(host, port) return end + local recvtime = os.time() socket:close() -- Continue only if a + response is returned @@ -119,6 +122,12 @@ action = function(host, port) -- Leverage smbauth.get_host_info_from_security_blob() for decoding local ntlm_decoded = smbauth.get_host_info_from_security_blob(response_decoded) + if ntlm_decoded.timestamp then + -- 64-bit number of 100ns clicks since 1/1/1601 + local unixstamp = ntlm_decoded.timestamp // 10000000 - 11644473600 + datetime.record_skew(host, unixstamp, recvtime) + end + -- Target Name will always be returned under any implementation output.Target_Name = ntlm_decoded.target_realm diff --git a/scripts/rfc868-time.nse b/scripts/rfc868-time.nse index d838537be..a426aa4eb 100644 --- a/scripts/rfc868-time.nse +++ b/scripts/rfc868-time.nse @@ -1,4 +1,5 @@ local comm = require "comm" +local datetime = require "datetime" local shortport = require "shortport" local stdnse = require "stdnse" local bin = require "bin" @@ -46,12 +47,14 @@ action = function(host, port) -- Make sure we don't stomp a more-likely service detection. if port.version.name == "time" then - local diff = os.difftime(stamp,os.time()) + local recvtime = os.time() + local diff = os.difftime(stamp,recvtime) if diff < 0 then diff = -diff end -- confidence decreases by 1 for each year the time is off. stdnse.debug1("Time difference: %d seconds (%0.2f years)", diff, diff / 31556926) local confidence = 10 - diff / 31556926 if confidence < 0 then confidence = 0 end + datetime.record_skew(host, stamp, recvtime) port.version.name_confidence = confidence nmap.set_port_version(host, port, "hardmatched") end diff --git a/scripts/script.db b/scripts/script.db index 681433416..2639ab9b8 100644 --- a/scripts/script.db +++ b/scripts/script.db @@ -64,6 +64,7 @@ Entry { filename = "citrix-enum-apps.nse", categories = { "discovery", "safe", } Entry { filename = "citrix-enum-servers-xml.nse", categories = { "discovery", "safe", } } Entry { filename = "citrix-enum-servers.nse", categories = { "discovery", "safe", } } Entry { filename = "clamav-exec.nse", categories = { "exploit", "vuln", } } +Entry { filename = "clock-skew.nse", categories = { "default", "safe", } } Entry { filename = "couchdb-databases.nse", categories = { "discovery", "safe", } } Entry { filename = "couchdb-stats.nse", categories = { "discovery", "safe", } } Entry { filename = "creds-summary.nse", categories = { "auth", "default", "safe", } } diff --git a/scripts/smb-security-mode.nse b/scripts/smb-security-mode.nse index c679fb0a5..d6b267e30 100644 --- a/scripts/smb-security-mode.nse +++ b/scripts/smb-security-mode.nse @@ -1,4 +1,6 @@ local bit = require "bit" +local os = require "os" +local datetime = require "datetime" local smb = require "smb" local stdnse = require "stdnse" local string = require "string" @@ -101,6 +103,9 @@ action = function(host) smb.stop(state) return stdnse.format_output(false, err) end + if state.time then + datetime.record_skew(host, state.time, os.time()) + end local security_mode = state['security_mode'] diff --git a/scripts/smtp-ntlm-info.nse b/scripts/smtp-ntlm-info.nse index 83d94d548..d16fd440c 100644 --- a/scripts/smtp-ntlm-info.nse +++ b/scripts/smtp-ntlm-info.nse @@ -1,3 +1,5 @@ +local datetime = require "datetime" +local os = require "os" local smtp = require "smtp" local bin = require "bin" local shortport = require "shortport" @@ -125,6 +127,7 @@ action = function(host, port) if not response then return end + local recvtime = os.time() socket:close() @@ -143,6 +146,13 @@ action = function(host, port) local ntlm_decoded = smbauth.get_host_info_from_security_blob(response_decoded) + if ntlm_decoded.timestamp and ntlm_decoded.timestamp > 0 then + stdnse.debug1("timestamp is %s", ntlm_decoded.timestamp) + -- 64-bit number of 100ns clicks since 1/1/1601 + local unixstamp = ntlm_decoded.timestamp // 10000000 - 11644473600 + datetime.record_skew(host, unixstamp, recvtime) + end + -- Target Name will always be returned under any implementation output.Target_Name = ntlm_decoded.target_realm diff --git a/scripts/ssl-date.nse b/scripts/ssl-date.nse index cb62c1504..c09eaf0c3 100644 --- a/scripts/ssl-date.nse +++ b/scripts/ssl-date.nse @@ -7,6 +7,7 @@ local os = require "os" local string = require "string" local sslcert = require "sslcert" local tls = require "tls" +local datetime = require "datetime" description = [[ Retrieves a target host's time and date from its TLS ServerHello response. @@ -201,6 +202,7 @@ action = function(host, port) end end + datetime.record_skew(host, tm.target, tm.scanner) local output = { date = stdnse.format_timestamp(tm.target, 0), delta = tm.delta, diff --git a/scripts/telnet-ntlm-info.nse b/scripts/telnet-ntlm-info.nse index 04ec56fb7..4b838b2d4 100644 --- a/scripts/telnet-ntlm-info.nse +++ b/scripts/telnet-ntlm-info.nse @@ -1,3 +1,5 @@ +local datetime = require "datetime" +local os = require "os" local bin = require "bin" local comm = require "comm" local shortport = require "shortport" @@ -87,6 +89,7 @@ action = function(host, port) return nil end + local recvtime = os.time() socket:close() -- Continue only if NTLMSSP response is returned. @@ -100,6 +103,12 @@ action = function(host, port) -- Leverage smbauth.get_host_info_from_security_blob() for decoding local ntlm_decoded = smbauth.get_host_info_from_security_blob(data) + if ntlm_decoded.timestamp then + -- 64-bit number of 100ns clicks since 1/1/1601 + local unixstamp = ntlm_decoded.timestamp // 10000000 - 11644473600 + datetime.record_skew(host, unixstamp, recvtime) + end + -- Target Name will always be returned under any implementation output.Target_Name = ntlm_decoded.target_realm