Path traversal in NuGet package upload
The NuGet package upload feature failed to validate the package name from the .nuspec file, allowing path traversal. An attacker could overwrite another user's NuGet package file, potentially backdooring it.
GitLab
Originally disclosed on GitLab
- Fixed in under an hour
- Disclosed after under an hour
HackerOne report #972492 by taraszelyk on 2020-09-01, assigned to @rchan-gitlab:
Report | Attachments | How To Reproduce
Report
Summary
The vulnerability allows an attacker to overwrite NuGet package of another user. The issue exists in update_package_from_metadata_service.rb which fails to check the filename of the for path traversal.
update_package_from_metadata_service.rb
def execute
...
# Updating file_name updates the path where the file is stored.
# We must pass the file again so that CarrierWave can handle the update
[@]package_file.update!(
file_name: package_filename,
file: [@]package_file.file
)
...
def package_filename
"#{package_name.downcase}.#{package_version.downcase}.nupkg"
end
Here package_name are fetched package_version from *.nuspec file from <id> and <version> values. Then they are concatenated and extension is appended to the resulting filename that is passed to update! function to update a path to the package.
package_name is not properly checked which allows us to overwrite any file with path traversal. The only restriction here is that we are limited to .nupkg files, so the attack surface is lower, but it still can be used to do harmful things.
To overwrite NuGet package file from the victim repository we need to know the full path to the files.
Assuming that packages are stored in var/opt/gitlab/gitlab-rails/shared/packages/ by default, it's pretty easy to build a full path. We need to know the following things:
packages_folder
victim_project_id
package_id
package_file_id
Steps to reproduce
- Create two accounts for attacker and for victim.
- As a victim, create a new repository and upload a valid NuGet package to the registry.
- As an attacker, gather
project_id,package_id, andpackage_file_id.
project_idcan be found in the repository main page.package_idcan be found in URL after navigating to the package page.package_file_idcan be found after clicking on the package file and inspecting the URL. For example, URL will look like this:http://gitlab.local/root/nuget/-/package_files/87/download, so 87 ispackage_file_id
- As an attacker, prepare a malicious package with a backdoor(it can be any, doesn't matter here).
Unzip .nupkg file and edit <package_name>.nuspec file and update id value to the following:
../../../../../../../../../../../../../../../../../../../../../../../../ + <default_package_dir> + / + 1,2 characters of sha256(project_id)+ / + 3,4 characters of sha256(project_id) + / + sha256(project_id) + /packages/ + package_id + /files/ + package_file_id + <name of the package without version and extension>
Also, you will need to update version to match the version in package you want to rewrite.
For example,
project_id = 51
package_id = 74
package_file_id = 87
name of the package = asdf.0.20.0.nupkg
package directory = /var/opt/gitlab/gitlab-rails/shared/packages/
Example of crafted *.nuspec file:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>../../../../../../../../../../../../../../../../../../../../../../../../var/opt/gitlab/gitlab-rails/shared/packages/03/1b/031b4af5197ec30a926f48cf40e11a7dbc470048a21e4003b7a3c07c5dab1baa/packages/74/files/87/asdf</id>
<version>0.20.0</version>
...
</metadata>
</package>
This will overwrite file on /var/opt/gitlab/gitlab-rails/shared/packages/03/1b/031b4af5197ec30a926f48cf40e11a7dbc470048a21e4003b7a3c07c5dab1baa/packages/74/files/87/asdf.0.20.0.nupkg
- Zip the package back and upload it.
What is the current bug behavior?
The application allows an attacker to overwrite NuGet packages of other users via path traversal.
What is the expected correct behavior?
Application checks package_filename for path traversal before updating the path to the package.
Output of checks
This was tested on local Gitlab 13.3.1 instance.
I can try to reproduce it on gitlab.com if needed.
Results of GitLab environment info
$ gitlab-rake gitlab:env:info
System information
System:
Current User: git
Using RVM: no
Ruby Version: 2.6.6p146
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.9
Git Version: 2.28.0
Sidekiq Version:5.2.9
Go Version: unknown
GitLab information
Version: 13.3.1
Revision: d54b96a3079
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 11.7
URL: http://gitlab.local
HTTP Clone URL: http://gitlab.local/some-group/some-project.git
SSH Clone URL: [email protected]:some-group/some-project.git
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 13.6.0
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Git: /opt/gitlab/embedded/bin/git
Impact
This vulnerability can be used to overwrite NuGet packages of other users which can be used to backdoor valid packages and infect any user that uses it.
Also, it can be used to store .nupkg files on random places of the system. This potentially can lead to some serious issues.
Attachments
Warning: Attachments received through HackerOne, please exercise caution!
How To Reproduce
Please add reproducibility information to this section:
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The application concatenated the user-controlled package name and version from the .nuspec file into a file path without sanitising for path traversal sequences.
Impact
An attacker could overwrite another user's NuGet package, potentially backdooring it and infecting users who download it; the attack is limited to .nupkg files.
What the attacker needed
Attacker and victim accounts; victim uploads a NuGet package; attacker knows victim's project_id, package_id…
The fix
The application should check the package filename for path traversal before updating the package path.
How it was found
- Create two accounts, one for the attacker and one for the victim.
- As the victim, create a repository and upload a valid NuGet package.
- As the attacker, gather the project_id, package_id, and package_file_id of the victim's package.
- Prepare a malicious package by editing the .nuspec file to set the id to a path traversal string that points to the victim's package file, and set the version to match.
- Zip the package back and upload it as the attacker.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you upload a NuGet package, the system reads the package name from inside the file and uses it to decide where to save the package. Because it didn't check that the name was safe, an attacker could put special characters like '../' in the name to make the system save the file in a different folder, overwriting someone else's package. This is called path traversal.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.