-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunifi_protocol.py
More file actions
291 lines (246 loc) · 9.69 KB
/
unifi_protocol.py
File metadata and controls
291 lines (246 loc) · 9.69 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
# -*- coding: utf-8 -*-
import time
import json
import ast
from Crypto import Random
import logging
import zlib
try:
import snappy
HAS_SNAPPY = True
except ImportError:
HAS_SNAPPY = False
logger = logging.getLogger('unifi-gateway')
from Crypto.Cipher import AES
from struct import pack, unpack
from binascii import a2b_hex
from tlv import UnifiTLV
from tools import (
mac_string_2_array, ip_string_2_array, netmask_to_cidr,
uptime, get_hostname, get_if_table, get_network_table,
)
MASTER_KEY = "ba86f2bbe107c7c57eb5f2690775c712"
def encode_inform(config, data, encryption='CBC'):
iv = Random.new().read(16)
key = MASTER_KEY
if config.getboolean('gateway', 'is_adopted'):
key = config.get('gateway', 'key')
if config.getboolean('gateway', 'use_aes_gcm'):
encryption = 'GCM'
else:
encryption = 'CBC'
mac = config.get('gateway', 'lan_mac')
# Flags: bit 0 = encrypted, bit 1 = zlib, bit 2 = snappy, bit 3 = GCM
flags = 0x01
if encryption == 'GCM':
flags |= 0x08
if HAS_SNAPPY:
payload = snappy.compress(data.encode('utf-8'))
flags |= 0x04
else:
payload = zlib.compress(data.encode('utf-8'))
flags |= 0x02
encoded_data = b'TNBU'
encoded_data += pack('>I', 1)
encoded_data += pack('BBBBBB', *mac_string_2_array(mac))
encoded_data += pack('>H', flags)
encoded_data += iv
encoded_data += pack('>I', 1)
if encryption == 'GCM':
encoded_data += pack('>I', len(payload) + 16)
_aes = AES.new(a2b_hex(key), AES.MODE_GCM, iv)
_aes.update(encoded_data)
payload, tag = _aes.encrypt_and_digest(payload)
payload += tag
elif encryption == 'CBC':
pad_len = AES.block_size - (len(payload) % AES.block_size)
payload += bytes([pad_len]) * pad_len
payload = AES.new(a2b_hex(key), AES.MODE_CBC, iv).encrypt(payload)
encoded_data += pack('>I', len(payload))
encoded_data += payload
return encoded_data
def decode_inform(config, encoded_data):
magic = encoded_data[0:4]
if magic != b'TNBU':
raise Exception("Missing magic in response: '{}' instead of 'TNBU'".format(magic))
header = encoded_data[:40]
flags = unpack('>H', encoded_data[14:16])[0]
iv = encoded_data[16:32]
payload_len = unpack('>I', encoded_data[36:40])[0]
payload = encoded_data[40:(40 + payload_len)]
flag = {
'encrypted': bool(flags & 0x01),
'zlibCompressed': bool(flags & 0x02),
'SnappyCompression': bool(flags & 0x04),
'encryptedGCM': bool(flags & 0x08),
}
key = MASTER_KEY
if config.getboolean('gateway', 'is_adopted'):
key = config.get('gateway', 'key')
if flag['encrypted']:
if flag['encryptedGCM']:
tag = payload[-16:]
payload = payload[:-16]
_aes = AES.new(a2b_hex(key), AES.MODE_GCM, iv)
_aes.update(header)
payload = _aes.decrypt_and_verify(payload, tag)
else:
payload = AES.new(a2b_hex(key), AES.MODE_CBC, iv).decrypt(payload)
pad_size = payload[-1]
if pad_size > AES.block_size:
raise Exception('Response not padded or padding is corrupt')
payload = payload[:(len(payload) - pad_size)]
if flag['SnappyCompression']:
if HAS_SNAPPY:
payload = snappy.decompress(payload)
else:
raise Exception(
'Response is snappy-compressed but python-snappy is not installed'
)
elif flag['zlibCompressed']:
payload = zlib.decompress(payload)
return json.loads(payload)
def _resolve_lan_identity(config, dc, lan_if):
"""Get LAN MAC and IP, preferring live data but falling back to config."""
mac = dc.data.get('macs', {}).get(lan_if)
if not mac:
mac = config.get('gateway', 'lan_mac')
ip_info = dc.data.get('ip', {}).get(lan_if, {})
ip_addr = ip_info.get('address')
if not ip_addr:
ip_addr = config.get('gateway', 'lan_ip')
return mac, ip_addr, ip_info
def _create_partial_inform(config, dc):
ports = ast.literal_eval(config.get('gateway', 'ports'))
lan_if = [d['ifname'] for d in ports if d['type'].lower() == 'lan'][0]
mac, ip_addr, _ = _resolve_lan_identity(config, dc, lan_if)
return json.dumps({
'hostname': 'UBNT',
'state': 0,
'default': True,
'inform_url': config.get('gateway', 'url'),
'mac': mac,
'ip': ip_addr,
'model': config.get('gateway', 'device'),
'model_display': config.get('gateway', 'device_display'),
'version': config.get('gateway', 'firmware'),
'uptime': uptime(),
})
def _create_complete_inform(config, dc):
ports = ast.literal_eval(config.get('gateway', 'ports'))
lan_if = [d['ifname'] for d in ports if d['type'].lower() == 'lan'][0]
wan_if = [d['ifname'] for d in ports if d['type'].lower() == 'wan'][0]
mac, ip_addr, ip_info = _resolve_lan_identity(config, dc, lan_if)
netmask = ip_info.get('netmask', '255.255.255.0')
hostname = get_hostname()
if config.has_option('gateway', 'hostname'):
hostname = config.get('gateway', 'hostname')
system_stats = dc.data.get('system_stats', {'cpu': '0', 'mem': '0'})
active_leases = []
for lease in dc.data.get('dhcp_leases', []):
entry = {'mac': lease['mac'], 'ip': lease['ip']}
if 'hostname' in lease:
entry['hostname'] = lease['hostname']
active_leases.append(entry)
speedtest = dc.data.get('speedtest', {})
has_speedtest = speedtest.get('download', 0) > 0
payload = {
'bootrom_version': 'unknown',
'cfgversion': config.get('provisioned', 'cfgversion') if config.has_option('provisioned', 'cfgversion') else '',
'config_network_wan': {'type': 'dhcp'},
'config_port_table': ports,
'connect_request_ip': ip_addr,
'connect_request_port': '36424',
'default': False,
'state': 2,
'discovery_response': False,
'fw_caps': 3,
'guest_token': '4C1D46707239C6EB5A2366F505A44A91',
'has_default_route_distance': True,
'has_dnsmasq_hostfile_update': True,
'has_dpi': False,
'has_eth1': True,
'has_porta': True,
'has_ssh_disable': True,
'has_vti': True,
'hostname': hostname,
'inform_url': config.get('gateway', 'url'),
'ip': ip_addr,
'ipv4_active_leases': active_leases,
'isolated': False,
'locating': config.getboolean('gateway', 'locating') if config.has_option('gateway', 'locating') else False,
'mac': mac,
'model': config.get('gateway', 'device'),
'model_display': config.get('gateway', 'device_display'),
'netmask': netmask,
'radius_caps': 1,
'required_version': '4.0.0',
'selfrun_beacon': True,
'serial': mac.replace(':', ''),
'version': config.get('gateway', 'firmware'),
'time': int(time.time()),
'uplink': wan_if,
'uptime': uptime(),
'pfor-stats': [],
'speedtest-status': {
'latency': int(speedtest.get('ping', 0)),
'rundate': speedtest.get('lastrun', 0),
'runtime': 0,
'status_download': 2 if has_speedtest else 0,
'status_ping': 2 if speedtest.get('ping', 0) > 0 else 0,
'status_summary': 2 if has_speedtest else 0,
'status_upload': 2 if has_speedtest else 0,
'xput_download': speedtest.get('download', 0),
'xput_upload': speedtest.get('upload', 0),
},
'ddns-status': {'dyndns': []},
'system-stats': {
'cpu': system_stats.get('cpu', '0'),
'mem': system_stats.get('mem', '0'),
'uptime': str(uptime()),
},
'routes': _build_routes(dc, wan_if, lan_if),
'network_table': get_network_table(dc.data, ports),
'if_table': get_if_table(dc.data, ports),
}
return json.dumps(payload)
def _build_routes(dc, wan_if, lan_if):
routes = []
wan_gw = dc.data.get('ip', {}).get(wan_if, {}).get('gateway')
if wan_gw:
routes.append({
'nh': [{'intf': wan_if, 'metric': '1/0', 't': 'S>*', 'via': wan_gw}],
'pfx': '0.0.0.0/0',
})
lan_ip = dc.data.get('ip', {}).get(lan_if, {})
if lan_ip.get('address') and lan_ip.get('netmask'):
routes.append({
'nh': [{'intf': lan_if, 't': 'C>*'}],
'pfx': '%s/%s' % (lan_ip['address'], netmask_to_cidr(lan_ip['netmask'])),
})
return routes
def create_inform(config, dc):
if not config.getboolean('gateway', 'is_adopted'):
return _create_partial_inform(config, dc)
return _create_complete_inform(config, dc)
def create_broadcast_message(config, index, version=2, command=6):
lan_mac = config.get('gateway', 'lan_mac')
lan_ip = config.get('gateway', 'lan_ip')
firmware = config.get('gateway', 'firmware')
device = config.get('gateway', 'device')
platform_name = 'UNIFI-GW'
if config.has_option('gateway', 'platform'):
platform_name = config.get('gateway', 'platform')
tlv = UnifiTLV()
tlv.add(1, bytearray(mac_string_2_array(lan_mac)))
tlv.add(2, bytearray(mac_string_2_array(lan_mac) + ip_string_2_array(lan_ip)))
tlv.add(3, '{}.v{}'.format(device, firmware).encode('ascii'))
tlv.add(10, pack('!I', uptime()))
tlv.add(11, platform_name.encode('ascii'))
tlv.add(12, device.encode('ascii'))
tlv.add(19, bytearray(mac_string_2_array(lan_mac)))
tlv.add(18, pack('!I', index))
tlv.add(21, device.encode('ascii'))
tlv.add(27, firmware.encode('ascii'))
tlv.add(22, firmware.encode('ascii'))
return tlv.get(version=version, command=command)