-
Notifications
You must be signed in to change notification settings - Fork 14
/
ssl-date.nse
167 lines (146 loc) · 4.68 KB
/
ssl-date.nse
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
local shortport = require "shortport"
local stdnse = require "stdnse"
local table = require "table"
local bin = require "bin"
local nmap = require "nmap"
local os = require "os"
local string = require "string"
local sslcert = require "sslcert"
description = [[
Gets the remote host's time from its TLS ServerHello response.
In many TLS implementations, the first four bytes of server randomness
are a Unix timestamp.
Original idea by Jacob Appelbaum and his TeaTime and tlsdate tools:
* https://github.com/ioerror/TeaTime
* https://github.com/ioerror/tlsdate
]]
author = "Aleksandar Nikolic"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"discovery", "safe", "default"}
portrule = function(host, port)
return shortport.ssl(host, port) or sslcert.isPortSupported(port)
end
---
-- @usage
-- nmap <target> --script=ssl-date
--
-- @output
-- PORT STATE SERVICE REASON
-- 5222/tcp open xmpp-client syn-ack
-- |_ssl-date: Server time 2012-08-02 18:29:31 GMT; +4s from the local time.
--
--
-- most of the code snatched from tls-nextprotoneg until we decide if we want a separate library
--
--- Function that sends a client hello packet
-- target host and returns the response
--@args host The target host table.
--@args port The target port table.
--@return status true if response, false else.
--@return response if status is true.
local client_hello = function(host, port)
local sock, status, response, err, cli_h
-- Craft Client Hello
-- Content Type: Client Handshake
cli_h = bin.pack(">C", 0x16)
-- Version: TLS 1.0
cli_h = cli_h .. bin.pack(">S", 0x0301)
-- Length, fixed
cli_h = cli_h .. bin.pack(">S", 0x0031)
-- Handshake protocol
-- Handshake Type: Client Hello
cli_h = cli_h .. bin.pack(">C", 0x01)
-- Length, fixed
cli_h = cli_h .. bin.pack(">CS", 0x00, 0x002d)
-- Version: TLS 1.0
cli_h = cli_h .. bin.pack(">S", 0x0301)
-- Random: epoch time
cli_h = cli_h .. bin.pack(">I", os.time())
-- Random: random 28 bytes
cli_h = cli_h .. stdnse.generate_random_string(28)
-- Session ID length
cli_h = cli_h .. bin.pack(">C", 0x00)
-- Cipher Suites length
cli_h = cli_h .. bin.pack(">S", 0x0006)
-- Ciphers
cli_h = cli_h .. bin.pack(">S", 0xc011)
cli_h = cli_h .. bin.pack(">S", 0x0039)
cli_h = cli_h .. bin.pack(">S", 0x0004)
-- Compression Methods length
cli_h = cli_h .. bin.pack(">C", 0x01)
-- Compression Methods: null
cli_h = cli_h .. bin.pack(">C", 0x00)
-- Connect to the target server
local specialized_function = sslcert.getPrepareTLSWithoutReconnect(port)
if not specialized_function then
sock = nmap.new_socket()
sock:set_timeout(5000)
status, err = sock:connect(host, port)
if not status then
sock:close()
stdnse.print_debug("Can't send: %s", err)
return false
end
else
status,sock = specialized_function(host,port)
if not status then
return false
end
end
-- Send Client Hello to the target server
status, err = sock:send(cli_h)
if not status then
stdnse.print_debug("Couldn't send: %s", err)
sock:close()
return false
end
-- Read response
status, response = sock:receive()
if not status then
stdnse.print_debug("Couldn't receive: %s", err)
sock:close()
return false
end
return true, response
end
-- extract time from ServerHello response
local extract_time = function(response)
local result
local shlength, npndata, protocol, _
if not response then
stdnse.print_debug(SCRIPT_NAME .. ": Didn't get response.")
return false,result
end
-- If content type not handshake
if string.sub(response,1,1) ~= string.char(22) then
stdnse.print_debug(SCRIPT_NAME .. ": Response type not handshake.")
return false,result
end
-- If handshake protocol not server hello
if string.sub(response, 6, 6) ~= string.char(02) then
stdnse.print_debug(SCRIPT_NAME .. ": Handshake response not server hello.")
return false,result
end
-- Get the server hello length
_, shlength = bin.unpack(">S", response, 4)
local serverhello = string.sub(response, 6, 6 + shlength)
local bin_res = string.sub(serverhello,7,10)
_,result = bin.unpack(">I",bin_res)
stdnse.print_debug("HERE: " ..result)
return true,result
end
action = function(host, port)
local status, response
-- Send crafted client hello
status, response = client_hello(host, port)
if status and response then
-- extract time from response
local result
status, result = extract_time(response)
if status then
return string.format("%s GMT; %s from local time.",
os.date("%Y-%m-%d %H:%M:%S",result),
stdnse.format_difftime(os.date("!*t",result),os.date("!*t")))
end
end
end