diff --git a/nselib/smb.lua b/nselib/smb.lua index c9d499eb0..2574abfd3 100644 --- a/nselib/smb.lua +++ b/nselib/smb.lua @@ -130,7 +130,6 @@ local math = require "math" local match = require "match" local netbios = require "netbios" local nmap = require "nmap" -local os = require "os" local smbauth = require "smbauth" local stdnse = require "stdnse" local string = require "string" @@ -1050,7 +1049,7 @@ function negotiate_v1(smb, overrides) -- Convert the time and timezone to more useful values smb['time'] = (smb['time'] // 10000000) - 11644473600 - smb['date'] = os.date("%Y-%m-%d %H:%M:%S", smb['time']) + smb['date'] = stdnse.format_timestamp(smb['time']) smb['timezone'] = -(smb['timezone'] / 60) if(smb['timezone'] == 0) then smb['timezone_str'] = "UTC+0" @@ -2862,7 +2861,7 @@ function find_files(smbstate, fname, options) local time = fe.created time = (time // 10000000) - 11644473600 - fe.created = os.date("%Y-%m-%d %H:%M:%S", time) + fe.created = stdnse.format_timestamp(time) -- TODO: cleanup fe.s_fname pos, fe.fname = bin.unpack("A" .. f_len, response.data, pos) diff --git a/nselib/smb2.lua b/nselib/smb2.lua index abf4ebd7d..5a166efa5 100644 --- a/nselib/smb2.lua +++ b/nselib/smb2.lua @@ -16,7 +16,6 @@ local stdnse = require "stdnse" local nmap = require "nmap" local table = require "table" local match = require "match" -local os = require "os" _ENV = stdnse.module("smb2", stdnse.seeall) @@ -389,12 +388,12 @@ function negotiate_v2(smb, overrides) -- Convert the time and timezone to human readable values (taken from smb.lua) smb['time'] = (smb['time'] // 10000000) - 11644473600 - smb['date'] = os.date("%Y-%m-%d %H:%M:%S", smb['time']) + smb['date'] = stdnse.format_timestamp(smb['time']) -- Samba does not report the boot time if smb['start_time'] ~= 0 then smb['start_time'] = (smb['start_time'] // 10000000) - 11644473600 - smb['start_date'] = os.date("%Y-%m-%d %H:%M:%S", smb['start_time']) + smb['start_date'] = stdnse.format_timestamp(smb['start_time']) else smb['start_date'] = "N/A" end diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 4949e3927..fe9cf8462 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -27,6 +27,7 @@ local tonumber = tonumber; local tostring = tostring; local print = print; local type = type +local pcall = pcall local ceil = math.ceil local floor = math.floor @@ -578,7 +579,25 @@ function format_timestamp(t, offset) else local tz_string = format_tz(offset) offset = offset or 0 - return date("!%Y-%m-%dT%H:%M:%S", floor(t + offset)) .. tz_string + local status, result = pcall(date, "!%Y-%m-%dT%H:%M:%S", floor(t + offset)) + if not status then + local tmp = floor(t + offset) + if tmp > 0xffffffff then + -- Maybe too far in the future? + local seconds_in_year = 31556926 + local extra_years = (tmp - 0xffffffff) // seconds_in_year + 1 + tmp = tmp - extra_years * seconds_in_year + status, result = pcall(date, "!*t", tmp) + if status then + -- seconds_in_year is imprecise, so we truncate to date only + result = format("%d-%02d-%02d", result.year + extra_years, result.month, result.day) + end + end + end + if not status then + return ("Invalid timestamp: %s"):format(t) + end + return result .. tz_string end end