{{ message }}
Repository navigation
Expand file tree
/
Copy pathairmax.lua
More file actions
377 lines (340 loc) · 16.2 KB
/
Copy pathairmax.lua
File metadata and controls
377 lines (340 loc) · 16.2 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
-- wiremax — Wireshark dissector for Ubiquiti AirMAX (AC + M) and the
-- companion Mikrotik / RouterBoard vendor Information Elements.
--
-- AirMAX rides inside 802.11 vendor-specific IEs (tag 221). Rather than
-- replace Wireshark's built-in 802.11 dissector, this runs as a POSTDISSECTOR:
-- after the wlan dissector has split out the IEs, we read the per-IE OUIs and
-- the frame's source/dest MACs via Field extractors, locate each AirMAX IE in
-- the frame, AES-decrypt the payload (key = HMAC-SHA1(dst,src)[:16]; broadcast
-- dst fallback like pyrmax), and add a parsed subtree.
--
-- The decode logic mirrors the pyrmax Python package field-for-field; the
-- crypto is self-contained pure Lua (Wireshark exposes no crypto API to Lua).
--
-- Install: copy this file plus the wsairmax/ directory into a Wireshark
-- "Personal Lua Plugins" folder (Help > About Wireshark > Folders), or run
-- tshark -X lua_script:airmax.lua -r capture.pcap
-- Filters: airmax.ac.*, airmax.m.*, airmax.rb.* (e.g. airmax.ac.ssid).
-- Make wsairmax.* requireable from wherever Wireshark loads this file.
local here = debug.getinfo(1, "S").source:sub(2):match("^(.*[/\\])") or "./"
package.path = here .. "?.lua;" .. package.path
local util = require("wsairmax.util")
local crypto = require("wsairmax.crypto")
local ac = require("wsairmax.ac")
local m = require("wsairmax.m")
local rb = require("wsairmax.routerboard")
local OUI_AC = 0x002722
local OUI_M = 0x00156d
local OUI_RB = 0x000c42
local p_airmax = Proto("airmax", "Ubiquiti AirMAX")
-- ---------------------------------------------------------------------------
-- ProtoFields
-- ---------------------------------------------------------------------------
local AC_MSG_VS = ac.MSG_TYPE
local TLV_TAG_VS = ac.TLV_TAG
local M_MSG_VS = m.MSG_TYPE
local f = {
-- AC outer (cleartext)
ac_oui = ProtoField.bytes("airmax.ac.oui", "OUI"),
ac_oui_type = ProtoField.bytes("airmax.ac.oui_type", "OUI Type"),
ac_flags = ProtoField.uint8("airmax.ac.flags", "Flags", base.HEX),
ac_flag_enc = ProtoField.bool("airmax.ac.flags.encrypted", "Encrypted", 8, nil, 0x02),
ac_msg_type = ProtoField.uint8("airmax.ac.msg_type", "Message Type", base.DEC, AC_MSG_VS),
ac_enc_len = ProtoField.uint16("airmax.ac.enc_len", "Encrypted Length"),
ac_ct = ProtoField.bytes("airmax.ac.ciphertext", "Ciphertext"),
-- AC payload (decrypted): shared head
ac_key = ProtoField.string("airmax.ac.key", "AES Key (HMAC-SHA1(dst,src)[:16])"),
ac_version = ProtoField.uint16("airmax.ac.version", "Version"),
ac_src_mac = ProtoField.ether("airmax.ac.src_mac", "Source MAC"),
-- AC body: beacon
ac_mac_0c = ProtoField.ether("airmax.ac.mac_0c", "Radio MAC (mac_0c)"),
ac_cap = ProtoField.uint32("airmax.ac.cap_flags", "Capability Flags", base.HEX),
ac_cap_cbw = ProtoField.bool("airmax.ac.cap_flags.chanbw", "chanbw mode", 32, nil, 0x8),
ac_cap_hd = ProtoField.bool("airmax.ac.cap_flags.high_density", "high density", 32, nil, 0x10),
ac_cap_ad = ProtoField.bool("airmax.ac.cap_flags.auth_deauth", "auth-deauth capable", 32, nil, 0x40),
ac_cap_ax = ProtoField.bool("airmax.ac.cap_flags.compat_11ax", "11ax-compat", 32, nil, 0x80),
ac_mixed = ProtoField.uint32("airmax.ac.mixed_mode", "Mixed Mode"),
-- AC body: assoc_req / assoc_resp
ac_tx_chain = ProtoField.uint8("airmax.ac.tx_chainmask", "TX Chainmask"),
ac_rx_chain = ProtoField.uint8("airmax.ac.rx_chainmask", "RX Chainmask"),
ac_field14 = ProtoField.uint32("airmax.ac.field_14", "field_14 [open]", base.HEX),
ac_field9c = ProtoField.uint32("airmax.ac.field_9c", "field_9c [open]", base.HEX),
ac_ic6b8 = ProtoField.bytes("airmax.ac.ic_6b8", "ic_6b8 (chainmask pair)"),
ac_sta68 = ProtoField.uint32("airmax.ac.sta_field_68", "sta_field_68 [open]", base.HEX),
ac_rssi = ProtoField.bytes("airmax.ac.rssi", "RSSI (per-chain)"),
ac_txpower = ProtoField.uint8("airmax.ac.txpower", "TX Power (half-dBm)"),
-- AC body: deauth
ac_jiffies = ProtoField.uint32("airmax.ac.jiffies_nonce", "Jiffies Nonce", base.HEX),
ac_token = ProtoField.bytes("airmax.ac.enc_token", "Auth Token (opaque)"),
-- AC name / fw TLVs
ac_radio = ProtoField.string("airmax.ac.radioname", "Radioname"),
ac_ssid = ProtoField.string("airmax.ac.ssid", "SSID"),
ac_fwname = ProtoField.string("airmax.ac.fwname", "Firmware Name"),
ac_tlv_tag = ProtoField.uint8("airmax.ac.tlv.tag", "Tag", base.DEC, TLV_TAG_VS),
ac_tlv_len = ProtoField.uint8("airmax.ac.tlv.len", "Length"),
ac_tlv_data = ProtoField.bytes("airmax.ac.tlv.data", "Data"),
-- M outer (cleartext)
m_oui = ProtoField.bytes("airmax.m.oui", "OUI"),
m_oui_type = ProtoField.bytes("airmax.m.oui_type", "OUI Type"),
m_ct = ProtoField.bytes("airmax.m.ciphertext", "Ciphertext"),
-- M payload (decrypted)
m_key = ProtoField.string("airmax.m.key", "AES Key (HMAC-SHA1(dst,src)[:16])"),
m_version = ProtoField.uint8("airmax.m.version", "Version"),
m_msg_type = ProtoField.uint8("airmax.m.msg_type", "Message Type", base.DEC, M_MSG_VS),
m_src_mac = ProtoField.ether("airmax.m.src_mac", "Source MAC"),
m_enable = ProtoField.uint8("airmax.m.enable", "Enable"),
m_unknown = ProtoField.bytes("airmax.m.unknown_rest", "Unknown Rest"),
-- RouterBoard (cleartext)
rb_oui = ProtoField.bytes("airmax.rb.oui", "OUI"),
rb_oui_type = ProtoField.uint8("airmax.rb.oui_type", "OUI Type"),
rb_unknown = ProtoField.bytes("airmax.rb.unknown", "Unknown"),
rb_name = ProtoField.string("airmax.rb.device_name", "Device Name"),
rb_sub_type = ProtoField.uint8("airmax.rb.subie.subtype", "Subtype"),
rb_sub_len = ProtoField.uint8("airmax.rb.subie.length", "Length"),
rb_sub_data = ProtoField.bytes("airmax.rb.subie.data", "Data"),
}
local flist = {}
for _, pf in pairs(f) do flist[#flist + 1] = pf end
p_airmax.fields = flist
local e_decrypt = ProtoExpert.new("airmax.expert.decrypt", "Payload could not be decrypted",
expert.group.DECRYPTION, expert.severity.WARN)
local e_key = ProtoExpert.new("airmax.expert.key", "Embedded MAC mismatch (key may be wrong)",
expert.group.CHECKSUM, expert.severity.NOTE)
local e_short = ProtoExpert.new("airmax.expert.short", "Truncated or malformed AirMAX IE",
expert.group.MALFORMED, expert.severity.WARN)
p_airmax.experts = { e_decrypt, e_key, e_short }
-- Field extractors (must be created at load time).
local f_oui = Field.new("wlan.tag.oui")
local f_sa = Field.new("wlan.sa")
local f_da = Field.new("wlan.da")
-- ---------------------------------------------------------------------------
-- Helpers
-- ---------------------------------------------------------------------------
local function fi_raw(fi)
if fi then return fi.range:raw() end
return nil
end
local function dst_label(d)
if d == crypto.BROADCAST then return "broadcast" end
return util.mac(d)
end
-- Locate a vendor IE from its OUI FieldInfo. Returns (ie_tvbrange, off, len)
-- where `off` is the OUI offset in the frame tvb and the range covers the IE
-- body from the OUI to the end of the IE. nil if it isn't a tag-221 IE.
local function locate_ie(tvb, fi)
local off = fi.range:offset()
if off < 2 then return nil end
if tvb(off - 2, 1):uint() ~= 0xdd then return nil end -- not a Vendor Specific tag
local ielen = tvb(off - 1, 1):uint()
if off + ielen > tvb:len() then return nil end
return tvb(off, ielen), off, ielen
end
-- Decrypt + verify against the embedded source MAC, with broadcast fallback
-- (matches pyrmax.pcap._try_decode). Returns (pt, payload, dst_used, verified).
local function decrypt_verify(ciphertext, sa, da, parse_payload, embedded_mac)
if not sa then return nil end
local cands = {}
if da then cands[#cands + 1] = da end
if (not da) or da ~= crypto.BROADCAST then cands[#cands + 1] = crypto.BROADCAST end
local first_pt, first_payload, first_dst
for _, d in ipairs(cands) do
local ok, pt = pcall(crypto.decrypt, ciphertext, sa, d)
if ok then
local payload = parse_payload(pt)
if not first_pt then first_pt, first_payload, first_dst = pt, payload, d end
if payload and embedded_mac(payload) == sa then
return pt, payload, d, true
end
end
end
return first_pt, first_payload, first_dst, false
end
local function decrypted_node(parent, pt, name)
local ptvb = ByteArray.new(util.to_hex(pt)):tvb(name)
local node = parent:add(p_airmax, ptvb())
node:set_text("Decrypted payload (AES-128-ECB)")
node:set_generated()
return node, ptvb
end
-- Add a cap_flags u32 with its (partially known, §11) bit booleans.
local function add_ac_cap(dt, ptvb, cap)
local n = dt:add(f.ac_cap, ptvb(cap.o, 4))
n:add(f.ac_cap_cbw, ptvb(cap.o, 4))
n:add(f.ac_cap_hd, ptvb(cap.o, 4))
n:add(f.ac_cap_ad, ptvb(cap.o, 4))
n:add(f.ac_cap_ax, ptvb(cap.o, 4))
return n
end
-- Add the radioname/SSID string fields, then a subtree per name TLV. `tlvs`
-- carries radioname/ssid (+ a trailing Padding marker for beacons).
local function add_ac_tlvs(dt, ptvb, tlvs)
for _, t in ipairs(tlvs) do
if t.tag == 1 and #t.data > 0 then dt:add(f.ac_radio, ptvb(t.data_o, #t.data)) end
if t.tag == 2 and #t.data > 0 then dt:add(f.ac_ssid, ptvb(t.data_o, #t.data)) end
end
for _, t in ipairs(tlvs) do
if t.tag == 0 then
dt:add(p_airmax, ptvb(t.o, 1)):set_text("TLV: Padding (0)")
:add(f.ac_tlv_tag, ptvb(t.o, 1))
else
local tn = TLV_TAG_VS[t.tag] or string.format("0x%02x", t.tag)
local node = dt:add(p_airmax, ptvb(t.o, 2 + #t.data))
node:set_text(string.format("TLV: %s (%d), len %d", tn, t.tag, t.len))
node:add(f.ac_tlv_tag, ptvb(t.o, 1))
node:add(f.ac_tlv_len, ptvb(t.o + 1, 1))
if #t.data > 0 then node:add(f.ac_tlv_data, ptvb(t.data_o, #t.data)) end
end
end
end
-- Append the version-gated assoc tail (field_9c/sta_field_68, rssi, fwname,
-- txpower) shared between assoc_req and assoc_resp bodies.
local function add_ac_tail(dt, ptvb, body)
if body.field_9c then dt:add(f.ac_field9c, ptvb(body.field_9c.o, 4)) end
if body.sta_field_68 then dt:add(f.ac_sta68, ptvb(body.sta_field_68.o, 4)) end
if body.rssi then dt:add(f.ac_rssi, ptvb(body.rssi.o, 2)) end
if body.fwname then dt:add(f.ac_fwname, ptvb(body.fwname.data_o, #body.fwname.data)) end
if body.txpower then dt:add(f.ac_txpower, ptvb(body.txpower.o, 1)) end
end
-- ---------------------------------------------------------------------------
-- Per-variant handlers
-- ---------------------------------------------------------------------------
local function handle_ac(tvb, fi, sa, da, tree, pinfo)
local ierange, off = locate_ie(tvb, fi)
if not ierange then return end
local outer = ac.parse_outer(ierange:raw())
if not outer then return end
local st = tree:add(p_airmax, ierange)
st:set_text("AirMAX AC (Vendor Specific IE)")
st:add(f.ac_oui, tvb(off, 3))
st:add(f.ac_oui_type, tvb(off + 3, 3))
st:add(f.ac_flags, tvb(off + 6, 1)):add(f.ac_flag_enc, tvb(off + 6, 1))
st:add(f.ac_msg_type, tvb(off + 7, 1))
st:add(f.ac_enc_len, tvb(off + 8, 2))
local ct_off, ct_len = off + 10, outer.enc_len
st:add(f.ac_ct, tvb(ct_off, ct_len))
pinfo.cols.info:append(" | AirMAX-AC " .. (ac.MSG_TYPE[outer.msg_type] or outer.msg_type))
if (outer.flags & ac.FLAG_ENCRYPTED) == 0 then return end
local pt, payload, dst_used, verified = decrypt_verify(
outer.ciphertext, sa, da,
function(p) return ac.parse_payload(p, outer.msg_type) end,
function(pp) return pp.src_mac.val end)
if not pt then
st:add_proto_expert_info(e_decrypt, "needs the 802.11 source MAC for key derivation")
return
end
local dt, ptvb = decrypted_node(st, pt, "Decrypted AirMAX AC payload")
dt:add(f.ac_key, tvb(ct_off, ct_len),
util.to_hex(crypto.derive_key(sa, dst_used)) .. " (dst=" .. dst_label(dst_used) .. ")")
:set_generated()
if not verified then dt:add_proto_expert_info(e_key) end
if not payload then dt:add_proto_expert_info(e_short, "decrypted payload too short"); return end
-- Shared head
dt:add(f.ac_version, ptvb(0, 2))
local smac = dt:add(f.ac_src_mac, ptvb(2, 6))
-- Per-msg_type body
local body = payload.body
if body.kind == "beacon" then
dt:add(f.ac_mac_0c, ptvb(body.mac_0c.o, 6))
add_ac_cap(dt, ptvb, body.cap_flags)
dt:add(f.ac_mixed, ptvb(body.mixed_mode.o, 4))
add_ac_tlvs(dt, ptvb, body.tlvs)
elseif body.kind == "assoc_req" then
dt:add(f.ac_tx_chain, ptvb(body.tx_chainmask.o, 1))
dt:add(f.ac_rx_chain, ptvb(body.rx_chainmask.o, 1))
add_ac_cap(dt, ptvb, body.cap_flags)
dt:add(f.ac_field14, ptvb(body.field_14.o, 4))
add_ac_tlvs(dt, ptvb, body.tlvs)
add_ac_tail(dt, ptvb, body)
elseif body.kind == "assoc_resp" then
dt:add(f.ac_ic6b8, ptvb(body.ic_6b8.o, 2))
add_ac_tail(dt, ptvb, body)
elseif body.kind == "deauth" then
-- src_mac (above) is XOR-masked on the wire; show the recovered value.
smac:append_text(" (masked; real = " .. util.mac(payload.src_mac.val) .. ")")
dt:add(f.ac_jiffies, ptvb(body.jiffies_nonce.o, 4))
dt:add(f.ac_token, ptvb(body.enc_token.o, 16)):append_text(" (opaque — token key not reverse-engineered)")
elseif body.kind == "probe_req" then
dt:append_text(" (probe_req — header only)")
end
end
local function handle_m(tvb, fi, sa, da, tree, pinfo)
local ierange, off, ielen = locate_ie(tvb, fi)
if not ierange then return end
local outer = m.parse_outer(ierange:raw())
if not outer then return end
local st = tree:add(p_airmax, ierange)
st:set_text("AirMAX M (Vendor Specific IE)")
st:add(f.m_oui, tvb(off, 3))
st:add(f.m_oui_type, tvb(off + 3, 3))
st:add(f.m_ct, tvb(off + 6, ielen - 6))
pinfo.cols.info:append(" | AirMAX-M")
local pt, payload, dst_used, verified = decrypt_verify(
outer.ciphertext, sa, da, m.parse_payload,
function(pp) return pp.src_mac.val end)
if not pt then
st:add_proto_expert_info(e_decrypt, "needs the 802.11 source MAC for key derivation")
return
end
local dt, ptvb = decrypted_node(st, pt, "Decrypted AirMAX M payload")
dt:add(f.m_key, tvb(off + 6, ielen - 6),
util.to_hex(crypto.derive_key(sa, dst_used)) .. " (dst=" .. dst_label(dst_used) .. ")")
:set_generated()
if not verified then dt:add_proto_expert_info(e_key) end
if not payload then dt:add_proto_expert_info(e_short, "decrypted payload too short"); return end
dt:add(f.m_version, ptvb(0, 1))
dt:add(f.m_msg_type, ptvb(1, 1))
dt:add(f.m_src_mac, ptvb(2, 6))
dt:add(f.m_enable, ptvb(8, 1))
if payload.unknown_rest.l > 0 then dt:add(f.m_unknown, ptvb(9, payload.unknown_rest.l)) end
end
local function handle_rb(tvb, fi, tree, pinfo)
local ierange, off = locate_ie(tvb, fi)
if not ierange then return end
local parsed = rb.parse(ierange:raw())
if not parsed then return end
local st = tree:add(p_airmax, ierange)
st:set_text("Mikrotik / RouterBoard (Vendor Specific IE)")
st:add(f.rb_oui, tvb(off, 3))
st:add(f.rb_oui_type, tvb(off + 3, 1))
st:add(f.rb_unknown, tvb(off + 4, 2))
local name = rb.device_name(parsed)
if name then
st:add(f.rb_name, ierange, name):set_generated()
pinfo.cols.info:append(" | RB '" .. name .. "'")
end
for _, s in ipairs(parsed.sub_ies) do
local node = st:add(p_airmax, tvb(off + s.o, 2 + s.len))
node:set_text(string.format("Sub-IE: subtype %d, len %d", s.subtype, s.len))
node:add(f.rb_sub_type, tvb(off + s.o, 1))
node:add(f.rb_sub_len, tvb(off + s.o + 1, 1))
if s.len > 0 then node:add(f.rb_sub_data, tvb(off + s.o + 2, s.len)) end
end
end
-- ---------------------------------------------------------------------------
-- Postdissector
-- ---------------------------------------------------------------------------
function p_airmax.dissector(tvb, pinfo, tree)
local ouis = { f_oui() }
if #ouis == 0 then return end
local sa = fi_raw(f_sa())
local da = fi_raw(f_da())
for _, fi in ipairs(ouis) do
local v = fi.value
local ok, err
if v == OUI_AC then
ok, err = pcall(handle_ac, tvb, fi, sa, da, tree, pinfo)
elseif v == OUI_M then
ok, err = pcall(handle_m, tvb, fi, sa, da, tree, pinfo)
elseif v == OUI_RB then
ok, err = pcall(handle_rb, tvb, fi, tree, pinfo)
else
ok = true
end
if not ok then
local n = tree:add(p_airmax, fi.range)
n:set_text("AirMAX dissector error: " .. tostring(err))
n:add_proto_expert_info(e_short, tostring(err))
end
end
end
register_postdissector(p_airmax)
You can’t perform that action at this time.
