-
Notifications
You must be signed in to change notification settings - Fork 1
/
memcached-info.nse
130 lines (108 loc) · 3.47 KB
/
memcached-info.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
local nmap = require "nmap"
local os = require "os"
local shortport = require "shortport"
local stdnse = require "stdnse"
local tab = require "tab"
description = [[
Retrieves information (including system architecture, process ID, and
server time) from distributed memory object caching system memcached.
]]
---
-- @usage
-- nmap -p 11211 --script memcached-info
--
-- @output
-- 11211/tcp open unknown
-- | memcached-info:
-- | Process ID 18568
-- | Uptime 6950 seconds
-- | Server time Sat Dec 31 14:16:10 2011
-- | Architecture 64 bit
-- | Used CPU (user) 0.172010
-- | Used CPU (system) 0.200012
-- | Current connections 10
-- | Total connections 78
-- | Maximum connections 1024
-- | TCP Port 11211
-- | UDP Port 11211
-- |_ Authentication no
--
author = "Patrik Karlsson"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"discovery", "safe"}
-- currently, we only support the TCP, text based protocol
portrule = shortport.port_or_service(11211, "memcached", "tcp")
local filter = {
["pid"] = { name = "Process ID" },
["uptime"] = { name = "Uptime", func = function(v) return ("%d seconds"):format(v) end },
["time"] = { name = "Server time", func = function(v) return os.date("%c", v) end },
["pointer_size"] = { name = "Architecture", func = function(v) return v .. " bit" end },
["rusage_user"] = { name = "Used CPU (user)" },
["rusage_system"] = { name = "Used CPU (system)"},
["curr_connections"] = { name = "Current connections"},
["total_connections"] = { name = "Total connections"},
["maxconns"] = { name = "Maximum connections" },
["tcpport"] = { name = "TCP Port" },
["udpport"] = { name = "UDP Port" },
["auth_enabled_sasl"] = { name = "Authentication" }
}
local order = {
"pid", "uptime", "time", "pointer_size", "rusage_user", "rusage_system",
"curr_connections", "total_connections", "maxconns", "tcpport", "udpport",
"auth_enabled_sasl"
}
local function fail(err) return ("\n ERROR: %s"):format(err) end
local function mergetab(tab1, tab2)
for k, v in pairs(tab2) do
tab1[k] = v
end
return tab1
end
local function recvResponse(socket)
local kvs = {}
repeat
local status, response = socket:receive_buf("\r\n", false)
if ( not(status) ) then
return false, "Failed to receive response from server"
end
local k,v = response:match("^STAT ([^%s]*) (.*)$")
if ( k and v ) then
kvs[k] = v
end
until ( "END" == response or "ERROR" == response )
return true, kvs
end
action = function(host, port)
local socket = nmap.new_socket()
socket:set_timeout(10000)
local status = socket:connect(host, port)
if ( not(status) ) then
return fail("Failed to connect to server")
end
status = socket:send("stats\r\n")
if ( not(status) ) then
return fail("Failed to send request to server")
end
local status, kvs = recvResponse(socket)
if( not(status) ) then
return fail(kvs)
end
status = socket:send("stats settings\r\n")
if ( not(status) ) then
return fail("Failed to send request to server")
end
local status, kvs2 = recvResponse(socket)
if( not(status) ) then
return fail(kvs2)
end
kvs = mergetab(kvs, kvs2)
local result = tab.new(2)
for _, item in ipairs(order) do
if ( kvs[item] ) then
local name = filter[item].name
local val = ( filter[item].func and filter[item].func(kvs[item]) or kvs[item] )
tab.addrow(result, name, val)
end
end
return stdnse.format_output(true, tab.dump(result))
end