Описание
BentoML's runner server Vulnerable to Remote Code Execution (RCE) via Insecure Deserialization
Summary
There was an insecure deserialization in BentoML's runner server. By setting specific headers and parameters in the POST request, it is possible to execute any unauthorized arbitrary code on the server, which will grant the attackers to have the initial access and information disclosure on the server.
PoC
- First, create a file named model.py to create a simple model and save it
- Then run the following command to save this model
- Next, create bentofile.yaml to build this model
- Then, create service.py to host this model
- Then, run the following commands to build and host this model
- Finally, run this below python script to exploit insecure deserialization vulnerability in BentoML's runner server.
And I can replace the NdarrayContainer with PandasDataFrameContainer in Payload-Container header and the exploit still working. After running exploit.py then the output of the command id will be send out to the WebHook server.
Root Cause Analysis:
- When handling a request in BentoML runner server in
src/bentoml/_internal/server/runner_app.py, when the request headerargs-numberis equal to 1, it will call the function_deserialize_single_paramlike the code below:
- Then this is the function of
_deserialize_single_param, which will take the value of all request headers ofPayload-Container,Payload-MetaandBatch-Sizeand the crafted intoPayloadclass which will contain the data fromrequest.body
- After crafting
Paramscontaining payload, it will call to functioninferwithparamsvariable as input
- Inside function
infer, theparamsvariable with is belong to classParamswill call the functionmapof that class withAutoContainer.from_payloadas a parameter.
- Inside class
Paramsdefine the functionmapwhich will call theAutoContainer.from_payloadfunction with arguments, which aredata,meta,batch_sizeandcontainer
- Inside class
AutoContainerclass have defined the functionfrom_payloadwhich will find the class by thepayload.container, which is the value of headerPayload-Container, and it will call the functionfrom_payloadfrom the chosen class as return value
And if the attacker set value of header Payload-Container to NdarrayContainer or PandasDataFrameContainer, it will call from_payload and when it then check if the payload.meta["format"] == "default" it will call pickle.loads(payload.data) and payload.meta["format"] is the value of header Payload-Meta and the attacker can set it to {"format": "default"} and payload.data is the value of request.body which is the payload from malicious class P in my request, which will trigger __reduce__ method and then execute arbitrary commands (for my example is the curl command)
Impact
In the above Proof of Concept, I have shown how the attacker can execute command id and send the output of the command to the outside. By replacing id command with any OS commands, this insecure deserialization in BentoML's runner server will grant the attacker the permission to gain the remote shell on the server and injecting backdoors to persist access.
Пакеты
bentoml
>= 1.0.0a1, < 1.4.8
1.4.8
Связанные уязвимости
BentoML is a Python library for building online serving systems optimized for AI apps and model inference. Prior to 1.4.8, there was an insecure deserialization in BentoML's runner server. By setting specific headers and parameters in the POST request, it is possible to execute any unauthorized arbitrary code on the server, which will grant the attackers to have the initial access and information disclosure on the server. This vulnerability is fixed in 1.4.8.