---
title: "8. Limited file uploads"
date: 2024-12-04
draft: false
---

So far we've been dealing with filter bypasses for arbitrary file upload
While file upload forms with weak filters can be exploited to upload arbitrary files - some upload forms have secure filters that may not be exploitable with aforementioned techniques 

Even dealing with limited file upload forms we may still be able to perform attacks 

Certain file types `svg`, `html`, `xml`, even some img/ doc files may allow us to introduce new vulns by uploading malicious versions of these files
- this is why fuzzing allowed file exts is an important exercise 
- enables us to explore what attacks may be achievable 

---
## XSS limited file upload vulnerability
Many file types may introduce a stored XSS vuln by uploading maliciously crafted versions of them. 

Most basic example: 
- allow HTML upload 
- HTML doesnt allow us to execute code, its possible to implement JS code within them to carry an XSS or CSRF attack on whoever visits the uploaded html page. 
- if target sees link from website they trust, + website vulnerable to uploading html documents, its possible to trick them into visiting the link + carry attack on their machines. 

Second example of XSS:
 - display an image's metadata after its uploaded 
 - for such webapps we can include an XSS payload in one of the metadata fields that accept raw text (like Comment or Artist fields) such as below: 

```shell-session
exiftool -Comment=' "><img src=1 onerror=alert(window.origin)>' HTB.jpg

exiftool HTB.jpg

...SNIP...

Comment                         :  "><img src=1 onerror=alert(window.origin)>
```

We can see `comment` param updated to our xss payload. when image metadata is displayed, this payload can be triggered and the js code will be executed to carry the xss attack. 

If we change image MIME-type to `text/html` some webapps may show it as an html document instead of an image - in which case, xss payload would be triggered even if metadata wasn't directly displayed

XSS attacks can also be carried out using SVG images. 
`Scalable Vector Graphics` (SVG) are XML-based images. 
- describe 2d vector graphics - browser renders this into an image. 
- for this reason, we can modify their xml data to include an XSS payload 

for example: 
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1" height="1">
    <rect x="1" y="1" width="1" height="1" fill="green" stroke="black" />
    <script type="text/javascript">alert(window.origin);</script>
</svg>
```

the above would execute the xss payload whenever the image is displayed. 


---
## XXE with Limited file uploads 

Similar attacks can lead to XXE. 

SVG images can also include malicious XML data to leak source code of the webapp / other internal docs within the server. 

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<svg>&xxe;</svg>
```

Once this is uploaded + viewed - the xml document would get processed and we would yield the info of `/etc/passwd` printed on the page or shown in page source. 

If webapp allows upload of XML documents then the same payload can carry the same attack when the xml data is displayed on the webapp 

While reading system files like `/etc/passwd` can be useful for server enumeration it can have an even more significant benefit for a pentest - we can read the web apps source files! 
- access to source code enables us to find more vulnerabilities to exploit 
- becomes whitebox pentesting 
- for file upload exploits it may allow us to locate the upload directory, identify allowed extensions, or find the file naming scheme. 

Using XXE to read source code in PHP webapps use the below: 
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [ <!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=index.php"> ]>
<svg>&xxe;</svg>
```

Once SVG img displayed we get b64 encoded content of index.php, able to be decoded to read source code. 

Using XML data is not unique to SVG images - can also be utilized by many types of documents like PDFs, WORD documents, and PowerPoint documents among many others. 
- all of these use XML data within them to specify their format/structure 
- Suppose webapp used a document viewer that is vulnerable to XXE and allowed uploading of all of these documents. In that case, we can also modify their XML data to include malicious XXE elements and we would be able to carry a blind XXE attack on the backend webserver. 

SSRF is also achievable using these file types. 
- XXE vuln to enumerate the internally available services / call private APIs to perform private actions. 

---
## DoS attacks with limited file upload

file upload vulns may lead to DoS attacks 
For example using XXE payloads to achieve a doS (discussed in webattack module)

Able to utilize a `decompression bomb` with file types that use data compression like ZIP archives 
- if webapp automatically unzips a ZIP archive, it's possible to upload a malicious archive containing nested zip archives within it eventually leading to flooding the backend 

Able to utilize a `Pixel Flood` attack with some image files that utilize image compression like JPG/PNG 
- Create JPG image file with any image size such as 500x500 
- manually modify compression data to say it has a size of `0xffff` to `0xffff` 
	- this results in an image with a perceived size of 4 gigapixels 
	- when webapp tries to display this image; it will attempt to allocate all of its memory to this image and crashes the backend. 

Few other methods to dos a server: 
- upload overly large files, some upload forms may not limit upload file size or even check for it before uploading, making it possible to fill backend harddrive and cause it to crash or slow down considerably 
- if upload function is vulnerable to directory traversal, we can upload files to a different directory which may cause the server to crash. 
	- Homework: try to search for other examples of DOS attacks through vulnerable file upload functionality 
