FastAPI is a highly popular Python web framework. On November 23rd, 2022, the Datadog Security Labs team identified a third-party utility Python package on PyPI related to FastAPI,fastapi-toolkit, that has been backdoored by a malicious actor. The attacker inserted a backdoor in the package, adding a FastAPI route allowing a remote attacker to execute arbitrary python code and SQL queries in the context of the web application.
While FastAPI itself isnot impacted, this is an interesting occurrence of an attacker attempting to deploy a FastAPI-specific backdoor.
fastapi-toolkit was first published on PyPi on March 21, 2022. There likely was no malicious intent with the initial and subsequent versions of the package.
On November 23, 2022, at 07:33 UTC, a malicious commit with a backdoor (2cd2223) was pushed to the GitHub repository. Shortly after, at 07:35 UTC, the malicious version of the package was uploaded to PyPI.
We identified this malicious package on November 23 using our latest open source tool,GuardDog, which uses heuristics to identify malicious or compromised PyPI packages.
We reported the malicious PyPI package to the PyPI team, as well as the malicious GitHub commit to GitHub.
We recently releasedGuardDog, a free and open-source tool to identify malicious PyPI packages. We use it to identify, analyze, and help take down malicious packages.
On November 23, we identified that the packagefastapi-toolkit was likely malicious.
Upon investigation, we determined thatfastapi-toolkit used to be a legitimate package. At 7:33 UTC on November 23, a backdoor was introduced through commit2cd2223.
In addition to bumping the package version from 0.0.26 to 0.0.27, the new code dynamically executes Base64-encoded code whenever the package is used.
Decoding the Base64-encoded code shows that the backdoor adds a FastAPI HTTP route that allows the attacker to execute arbitrary Python code or SQL queries whenever the application receives an HTTP request with a specific header.
Once an attacker has compromised an application, they can trigger the backdoor by sending an HTTP request similar to:
# To execute Python codecurl-X POST https://example.com/-H"x-code: python"-H"x-token: <secret-value>" -d'print("Hello world")'# To execute an SQL querycurl-X POST https://example.com/-H"x-code: sql"-H"x-token: <secret-value>" -d'SHOW TABLES;'
It is possible the original developer of the package had their account compromised and used by a malicious actor. We promptly warned them of the issue so they could take necessary actions on their side, such as resetting their GitHub and PyPI credentials. It is also possible that the user knowingly uploaded a backdoor.
We also promptly reported the malicious package to the PyPI team to ensure it gets taken down.
At the time of writing, this package was the only one published by the developer's account on PyPI, and their GitHub account did not have any other recent commit. We can make sure of that by using the GitHub API to identify recent events of the account:
To determine if you’re impacted, you first need to identify if the packagefastapi-toolkit in version 0.0.27 is present on your system. To do so, you can run the command:
pip list|grep fastapi-toolkit
If the package is available on the system or in the current virtual environment, the output will look like:
fastapi-toolkit0.0.27
This will tell you the current version offastapi-toolkit—in this case, it is 0.0.27, the backdoored version.
Datadog ASM Vulnerability Monitoring,announced earlier this year at Dash, allows you to identify vulnerable and malicious packages used by your applications at runtime. It is currently in private beta. You can request access to the private betahere.
November 24, 2022Adapted the language to reflect the fact that the package maintainer's account may not have been compromised, but be malicious nonetheless.
November 24, 2022Clarified in the introduction that the FastAPI main package itself is not affected.