Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Archive Directory Traversal #1138

Merged
merged 32 commits into from
Apr 29, 2024
Merged
Changes from 31 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
93cf130
Add tests for Archive Directory Traversal
Apr 17, 2024
7d3de99
Update eicar link and fix md issues
Apr 17, 2024
391859b
Update ZIP links and fix Nginx terminology
Apr 17, 2024
490abdf
Review sentences
Apr 18, 2024
ebee495
Update based on comments
Apr 18, 2024
8795fb3
Fix md issues
Apr 18, 2024
6687518
Fix subpoint
Apr 18, 2024
50a6022
Update text part of the test section
Apr 18, 2024
509f2b1
Remove double space line 90
Apr 18, 2024
850db57
Update testing steps
Apr 23, 2024
aa51ee5
Fix md
Apr 23, 2024
264f599
Fix md ZIP
Apr 23, 2024
55a532f
Fix md newline
Apr 23, 2024
94c0ab9
Fix add inline code fences
Apr 23, 2024
89874e6
Fix zip md
Apr 23, 2024
88d8147
Fix inline indentation
Apr 23, 2024
3ddede0
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
7d4c20e
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
bf98396
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
344306d
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
2a1fda6
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
2ad237c
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
e55eee7
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
d848025
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
kingthorin Apr 23, 2024
5119eea
Refactor zip folder steps
Apr 24, 2024
9e11d2a
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
doverh Apr 25, 2024
5e0c28a
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
doverh Apr 25, 2024
50ea016
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
doverh Apr 25, 2024
24bd894
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
doverh Apr 25, 2024
734936f
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
doverh Apr 25, 2024
9a2e715
Update document/4-Web_Application_Security_Testing/10-Business_Logic_…
doverh Apr 25, 2024
e9c8e0d
Close block code
Apr 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,71 @@ If the filtering is performed on the server-side, then various techniques can be
- Change the capitalisation of the extension, such as `file.PhP` or `file.AspX`
- If the request includes multiple filenames, change them to different values.
- Using special trailing characters such as spaces, dots or null characters such as `file.asp...`, `file.php;jpg`, `file.asp%00.jpg`, `1.jpg%00.php`
- In badly configured versions of nginx, uploading a file as `test.jpg/x.php` may allow it to be executed as `x.php`.
- In badly configured versions of Nginx, uploading a file as `test.jpg/x.php` may allow it to be executed as `x.php`.

### Malicious File Contents

Once the file type has been validated, it is important to also ensure that the contents of the file are safe. This is significantly harder to do, as the steps required will vary depending on the types of file that are permitted.

#### Malware

Applications should generally scan uploaded files with anti-malware software to ensure that they do not contain anything malicious. The easiest way to test for this is using the [EICAR test file](https://www.eicar.org/?page_id=3950), which is an safe file that is flagged as malicious by all anti-malware software.
Applications should generally scan uploaded files with anti-malware software to ensure that they do not contain anything malicious. The easiest way to test for this is using the [EICAR test file](https://www.eicar.org/download-anti-malware-testfile/), which is an safe file that is flagged as malicious by all anti-malware software.

Depending on the type of application, it may be necessary to test for other dangerous file types, such as Office documents containing malicious macros. Tools such as the [Metasploit Framework](https://github.com/rapid7/metasploit-framework) and the [Social Engineer Toolkit (SET)](https://github.com/trustedsec/social-engineer-toolkit) can be used to generate malicious files for various formats.

When this file is uploaded, it should be detected and quarantined or deleted by the application. Depending on how the application processes the file, it may not be obvious whether this has taken place.

#### Archive Directory Traversal

If the application extracts archives (such as Zip files), then it may be possible to write to unintended locations using directory traversal. This can be exploited by uploading a malicious zip file that contains paths that traverse the file system using sequences such as `..\..\..\..\shell.php`. This technique is discussed further in the [snyk advisory](https://snyk.io/research/zip-slip-vulnerability).
If the application extracts archives (such as ZIP files), then it may be possible to write to unintended locations using directory traversal. This can be exploited by uploading a malicious ZIP file that contains paths that traverse the file system using sequences such as `..\..\..\..\shell.php`. This technique is discussed further in the [snyk advisory](https://snyk.io/research/zip-slip-vulnerability).
doverh marked this conversation as resolved.
Show resolved Hide resolved

#### Zip Bombs
A test against Archive Directory Traversal should include two parts:

A [Zip bomb](https://en.wikipedia.org/wiki/Zip_bomb) (more generally known as a decompression bomb) is an archive file that contains a large volume of data. It's intended to cause a denial of service by exhausting the disk space or memory of the target system that tries to extract the archive. Note that although the Zip format is the most used example for this, other formats are also affected, including gzip (which is frequently used to compress data in transit).
1. A malicious archive that breaks out of the target directory when extracted. This malicious archive should contain two files: a `base` file, extracted into the target directory, and a `traversed` file that attempts to navigate up the directory tree to hit the root folder - adding a file into the `tmp` directory. A malicious path will contain many levels of `../` (*i.e.* `../../../../../../../../tmp/traversed`) to stand a better chance of reaching the root directory. Once the attack is successful, the tester can find `/tmp/traversed` to be created on the webserver through the ZIP slip attack.
2. Logic that extracts compressed files either using custom code or a library. Archive Directory Traversal vulnerabilities exist when the extraction functionality doesn’t validate file paths in the archive. The example below shows a vulnerable implementation in Java:

At its simplest level, a Zip bomb can be created by compressing a large file consisting of a single character. The example below shows how to create a 1MB file that will decompress to 1GB:
```java
Enumeration<ZipEntry> entries =​ ​zip​.g​etEntries();

while(entries​.h​asMoreElements()){
ZipEntry e ​= ​entries.nextElement();
File f = new File(destinationDir, e.getName());
InputStream input = zip​.g​etInputStream(e);
IOUtils​.c​opy(input, write(f));
}
```

Follow the steps below to create a ZIP file that can abuse the vulnerable code above once its uploaded to the web server:

```bash
# Open a new terminal and create a tree structure
# (more directory levels might be required based on the system being targeted)
mkdir -p a/b/c
# Create a base file
echo 'base' > a/b/c/base
# Create a traversed file
echo 'traversed' > traversed
# You can double check the tree structure using `tree` at this stage
# Navigate to a/b/c root directory
cd a/b/c
# Compress the files
zip test.zip base ../../../traversed
# Verify compressed files content
nzip -l test.zip
kingthorin marked this conversation as resolved.
Show resolved Hide resolved

#### ZIP Bombs

A [ZIP bomb](https://en.wikipedia.org/wiki/zip_bomb) (more generally known as a decompression bomb) is an archive file that contains a large volume of data. It's intended to cause a denial of service by exhausting the disk space or memory of the target system that tries to extract the archive. Note that although the ZIP format is the most used example for this, other formats are also affected, including gzip (which is frequently used to compress data in transit).

At its simplest level, a ZIP bomb can be created by compressing a large file consisting of a single character. The example below shows how to create a 1MB file that will decompress to 1GB:

```bash
dd if=/dev/zero bs=1M count=1024 | zip -9 > bomb.zip
```

There are a number of methods that can be used to achieve much higher compression ratios, including multiple levels of compression, [abusing the Zip format](https://www.bamsoftware.com/hacks/zipbomb/) and [quines](https://research.swtch.com/zip) (which are archives that contain a copy of themselves, causing infinite recursion).
There are a number of methods that can be used to achieve much higher compression ratios, including multiple levels of compression, [abusing the ZIP format](https://www.bamsoftware.com/hacks/zipbomb/) and [quines](https://research.swtch.com/zip) (which are archives that contain a copy of themselves, causing infinite recursion).

A successful Zip bomb attack will result in a denial of service, and can also lead to increased costs if an auto-scaling cloud platform is used. **Do not carry out this kind of attack unless you have considered these risks and have written approval to do so.**
A successful ZIP bomb attack will result in a denial of service, and can also lead to increased costs if an auto-scaling cloud platform is used. **Do not carry out this kind of attack unless you have considered these risks and have written approval to do so.**

#### XML Files

Expand Down
Loading