summaryrefslogtreecommitdiffstats
path: root/nselib/ipmi.lua
blob: ce193f9cefe505f288efbe41fca892102bb5648e (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
---
-- A module implementing IPMI protocol (the code is a porting of the Metasploit ipmi scanner:
-- https://github.com/rapid7/metasploit-framework/tree/master/modules/auxiliary/scanner/ipmi)
--
-- @class module
-- @name ipmi
-- @author "Claudiu Perta <claudiu.perta@gmail.com>"
local stdnse = require "stdnse"
local string = require "string"
local rand = require "rand"

_ENV = stdnse.module("ipmi", stdnse.seeall)

local HAVE_SSL, openssl = pcall(require,"openssl")

PAYLOADS = {
  ["IPMI"] = 0,
  ["PAYLOAD_SOL"]  = 1,
  ["RMCPPLUSOPEN_REQ"] = 0x10,
  ["RMCPPLUSOPEN_REP"] = 0x11,
  ["RAKP1"] = 0x12,
  ["RAKP2"] = 0x13,
  ["RAKP3"] = 0x14,
  ["RAKP4"] = 0x15,
}

RMCP_ERRORS = {
  [1] = "Insufficient resources to create new session \
         (wait for existing sessions to timeout)",

  -- Shouldn't occur.
  [2] = "Invalid Session ID",

  -- Shouldn't occur.
  [3] = "Invalid payload type",

  -- If these happen, we need to enhance our mechanism for detecting
  -- supported auth algorithms.
  [4] = "Invalid authentication algorithm",
  [5] = "Invalid integrity algorithm",

  [6] = "No matching authentication payload",
  [7] = "No matching integrity payload",

  -- This suggests the session was timed out while trying to negotiate,
  -- shouldn't happen.
  [8] = "Inactive Session ID",

  [9] = "Invalid role",
  [0xa] = "Unauthorised role or privilege level requested",
  [0xb] = "Insufficient resources to create a session at the requested role",
  [0xc] = "Invalid username length",
  [0xd] = "Unauthorized name",
  [0xe] = "Unauthorized GUID",
  [0xf] = "Invalid integrity check value",
  [0x10] = "Invalid confidentiality algorithm",
  [0x11] = "No cipher suite match with proposed security algorithms",

  -- Never observed, most likely a bug in xCAT or IPMI device.
  [0x12] = "Illegal or unrecognized parameter",
}

channel_auth_request = function()
  return (
    "\x06\x00\xff\x07" .. -- Header
    "\x00\x00\x00\x00" ..
    "\x00\x00\x00\x00\x00\x09\x20\x18" ..
    "\xc8\x81\x00\x38\x8e\x04\xb5"
    )
end

rmcpplus_header = function (payload_type)
  return (
    "\x06\x00\xff\x07" ..           -- RMCP Header
    "\x06" ..                       -- RMCP+ Authentication Type
    string.char(PAYLOADS[payload_type]) .. -- Payload Type
    "\x00\x00\x00\x00" ..           -- Session ID
    "\x00\x00\x00\x00"            -- Sequence Number
    )
end

-- Open rmcpplus_request
session_open_request = function(console_session_id)
  local data = (
    "\x00\x00" .. -- Maximum Access
    "\x00\x00" .. -- Reserved
    console_session_id ..
    "\x00\x00\x00\x08" ..
    "\x01\x00\x00\x00" ..
    "\x01\x00\x00\x08" ..
    "\x01\x00\x00\x00" .. -- HMAC-SHA1
    "\x02\x00\x00\x08" ..
    "\x01\x00\x00\x00"  -- AES Encryption
    )

  return rmcpplus_header("RMCPPLUSOPEN_REQ") .. string.pack("<s2", data)
end

-- Open rmcpplus_request
session_open_cipher_zero_request = function(console_session_id)
  console_session_id = console_session_id or rand.random_string(4)

  local data = (
    "\x00\x00" .. -- Maximum Access
    "\x00\x00" .. -- Reserved
    console_session_id ..
    "\x00\x00\x00\x08" ..
    "\x00\x00\x00\x00" .. -- Cipher-zero
    "\x01\x00\x00\x08" ..
    "\x00\x00\x00\x00" .. -- Cipher-zero
    "\x02\x00\x00\x08" ..
    "\x00\x00\x00\x00"  -- No Encryption
    )

  return rmcpplus_header("RMCPPLUSOPEN_REQ") .. string.pack("<s2", data)
end

rakp_1_request = function(bmc_session_id, console_random_id, username)
  local data = string.pack(
    "<Bxxx I c16 Bxx s1",
    0,                       -- Message Tag
    bmc_session_id,
    console_random_id,
    0x14,                       -- Privilege level
    username
    )

  return rmcpplus_header("RAKP1") .. string.pack("<s2", data)
end

rakp_hmac_sha1_salt = function(
    console_session_id,
    bmc_session_id,
    console_random_id,
    bmc_random_id,
    bmc_guid,
    authorization_level,
    username)

  local salt = string.pack(
    "c4 I c16c16c16 Bs1",
    console_session_id,
    bmc_session_id,
    console_random_id,
    bmc_random_id,
    bmc_guid,
    authorization_level,
    username
    )

  return salt

end

verify_rakp_hmac_sha1 = function(salt, hash, password)
  if not(HAVE_SSL) then
    return false
  end

  local digest = openssl.hmac('sha1', password, salt)
  return (digest == hash)
end

--[[
Multi-byte fields in RMCP/ASF fields are specified as being transmitted in
'Network Byte Order' - meaning most-significant byte first.
RMCP and ASF-specified fields are therefore transferred most-significant byte
first.
The IPMI convention is to transfer multi-byte numeric fields least-significant
Byte first. Therefore, unless otherwise specified:
Data in the IPMI Session Header and IPMI Message fields are transmitted
least-significant byte first.
--]]

parse_channel_auth_reply = function(reply)
  local data = {}
  local pos = 1
  local value

  data.rmcp_version,
  data.rmcp_padding,
  data.rmcp_sequence,
  value, pos = string.unpack("<BBBB", reply, pos)

  data.rmcp_mtype = ((value & 0x80) ~= 0)
  data.rmcp_class = (value & 0x7F)

  data.session_auth_type,
  data.session_sequence,
  data.session_id,
  data.message_length,
  data.ipmi_tgt_address,
  data.ipmi_tgt_lun,
  data.ipmi_header_checksum,
  data.ipmi_src_address,
  data.ipmi_src_lun,
  data.ipmi_command,
  data.ipmi_completion_code,
  data.ipmi_channel,
  value, pos = string.unpack("<BI4I4BBBBBBBBBB", reply, pos)

  data.ipmi_compat_20 = ((value & 0x80) ~= 0)
  data.ipmi_compat_reserved1 = ((value & 0x40) ~= 0)
  data.ipmi_compat_oem_auth = ((value & 0x20) ~= 0)
  data.ipmi_compat_password = ((value & 0x10) ~= 0)
  data.ipmi_compat_reserved2 = ((value & 0x08) ~= 0)
  data.ipmi_compat_md5 = ((value & 0x04) ~= 0)
  data.ipmi_compat_md2 = ((value & 0x02) ~= 0)
  data.ipmi_compat_none = ((value & 0x01) ~= 0)

  value, pos = string.unpack("B", reply, pos)
  data.ipmi_user_reserved1 = ((value >> 6) & 0x03)
  data.ipmi_user_kg = ((value & 0x20) ~= 0)
  data.ipmi_user_disable_message_auth = ((value & 0x10) ~= 0)
  data.ipmi_user_disable_user_auth = ((value & 0x08) ~= 0)
  data.ipmi_user_non_null = ((value & 0x04) ~= 0)
  data.ipmi_user_null = ((value & 0x02) ~= 0)
  data.ipmi_user_anonymous = ((value & 0x01) ~= 0)

  value, pos = string.unpack("B", reply, pos)
  data.ipmi_conn_reserved1 = ((value >> 2) & 0x3F)
  data.ipmi_conn_20 = ((value & 0x02) ~= 0)
  data.ipmi_conn_15 = ((value & 0x01) ~= 0)

  -- 24 bits OEMID
  data.ipmi_oem_id, pos = string.unpack("<I3", reply, pos)
  data.ipmi_oem_data = reply:sub(pos)

  return data
end

parse_open_session_reply = function(reply)
  local data = {}
  local pos = 1
  local value

  -- 4 bytes Header
  data.rmcp_version,
  data.rmcp_padding,
  data.rmcp_sequence,
  value, pos = string.unpack("BBBB", reply, pos)
  -- bit 1
  data.rmcp_mtype = ((value & 0x80) ~= 0)
  -- bit [2:8]
  data.rmcp_class = (value & 0x7F)

  data.session_auth_type,
  value, pos = string.unpack("BB", reply, pos)
  -- bit 1
  data.session_payload_encrypted = ((value & 0x80) ~= 0)
  -- bit 2
  data.session_payload_authenticated = ((value & 0x40) ~= 0)
  -- bit [3:8]
  data.session_payload_type = (value & 0x3F)

  data.session_id,
  data.session_sequence,
  data.message_length,
  data.ignored1,
  data.error_code,
  data.ignored2,
  data.console_session_id,
  data.bmc_session_id, pos = string.unpack("<I4I4I2BBI2I4I4", reply, pos)

  return data
end

parse_rakp_1_reply = function(reply)
  local data = {}
  local pos = 1
  local value

  -- 4 bytes Header
  data.rmcp_version,
  data.rmcp_padding,
  data.rmcp_sequence,
  value, pos = string.unpack("BBBB", reply, pos)
  -- bit 1
  data.rmcp_mtype = ((value & 0x80) ~= 0)
  -- bit [2:8]
  data.rmcp_class = (value & 0x7F)

  data.session_auth_type,
  value, pos = string.unpack("BB", reply, pos)
  -- bit 1
  data.session_payload_encrypted = ((value & 0x80) ~= 0)
  -- bit 2
  data.session_payload_authenticated = ((value & 0x40) ~= 0)
  -- bit [3:8]
  data.session_payload_type = (value & 0x3F)

  data.session_id,
  data.session_sequence,
  data.message_length,
  data.ignored1,
  data.error_code,
  data.ignored2,
  data.console_session_id,
  data.bmc_random_id,
  data.bmc_guid,
  data.hmac_sha1, pos = string.unpack("<I4I4I2BBI2I4c16c16c20", reply, pos)

  return data
end

return _ENV;