← All writing

Arbitrary file read by WrapWrap technique with moltf (write-up)

hello, today im going to explain an arbitrary file read vulnerability i found together with @moltf1337 in a bug bounty program thats around 4 years old

Explain what the app does

at the beginning of the testing, we noticed something weird about image uploads, any request that uploads an image, no matter which endpoint or where the image is used, like an avatar or a banner, goes to one endpoint where the image gets uploaded, then the response gives us a cdn url for our image, as shown here:

Open full-size image

after that, the browser sends another request to the place where we want to use the image, for example the account avatar, this request updates or adds the avatar to the account, and the data is the cdn url of the image we uploaded

Open full-size image

and there was also an endpoint that stores the image from the url we provide

Open full-size image

did you notice the problem?? 👀

Maybe ssrf??

at this point, ssrf came to mind, and it could also be a file read if we can use the file scheme (file://)

Open full-size image

we put our webhook url there, and we actually got a request on our webhook, but the result was rejected and said that x.png is not a real image, as you can see:

Open full-size image

File signatures AKA magic bytes!

we had set x.png to have an image extension and an image/png content-type, but it turned out that the server fetches the content and checks whether its actually an image using magic bytes, if it isnt, it doesnt save it and returns a validation error

we were trying to figure out what else it accepts besides http/s, and we found that it accepts any scheme (as far as i know), whether its http, file, etc, its only condition was the magic byte, in this case, the developer probably thought everything was fine and didnt want to bother checking with regex, dns records, or other methods, instead, they made it a single condition: is it an image or not

as you can see, we tried file:///etc/passwd to check whether it would actually try to read it or not

Open full-size image

and it actually tried to read it and told us that its content is not an image, which is expected because /etc/passwd is not an image

and if we give it a file that doesnt exist:

Open full-size image

it returns a 500 error

at this point, we tried to think of a bypass so we could read local files

we started collecting everything we knew about the site:

  • the site uses XSRF-TOKEN = a strong indicator that its laravel = a strong indicator that its PHP
  • the site doesnt fully validate the url
  • the most important thing for the site is that the url we provide has image magic bytes so it can store it
  • we can access local files through the site, but they have to be images
  • if we give it a url that doesnt exist (not a real one), it will try to make a request to it, and if that fails, it returns a 500 error

PHP scheme?

we thought that since its probably PHP, it can access any scheme, and if something goes wrong it returns a 500 error, but if it actually reads the file it returns an invalid image error, so why not use the php scheme with wrappers and see if we can actually use the php scheme or not?

Open full-size image

it worked! and we said lets make sure this error actually means that the file was read

Open full-size image

and yes, we got a 500 error when we used the php base64 wrapper on a file that doesnt exist

what happened is that we used a php wrapper to base64-encode the content of /etc/passwd, and then the base64 content goes to image validation, it will obviously fail because even if we base64-encode /etc/passwd, it doesnt become an image, ill explain how php wrappers work in the next section, but what i want you to understand is that php wrappers open a lot of doors for us, and one of them might lead us to the solution

Wrap Wrap!!

at this point, the first thing that came to my mind was the Lexfo article, which talks about a technique called Wrapwrap, based on the Synacktiv article about php filter chains

lets understand the idea in a simple way (if you want to go deeper into the topic, you can read the Lexfo and Synacktiv articles)

What are php wrappers?

before we get into Wrapwrap, lets understand what php wrappers are

generally, php uses something called streams to deal with different data sources, and the stream wrapper is what tells php how to read from and write to that source

What does php://filter do?

php://filter lets us change the content of the stream before it is returned to us

so instead of reading the file as it is, we can use filters to change the content, for example, convert it to base64

PHP
echo file_get_contents('php://filter/read=convert.base64-encode/resource=/etc/passwd');
> cm9vdDp4OjA6MDpyb290Oi9yb290O....

here, php reads /etc/passwd, converts its content to base64, and returns the base64 output instead of the original content

the nice thing is that we can use more than one filter one after another and separate them with |, and each filter takes the output of the filter before it

this is exactly what we need here, because we want to control the content that comes out of reading the file instead of reading it as it is

PHP filter chains

the basic idea behind php filter chains is that we can control the output that comes from reading the file by using multiple filters in the order we want

one of the main things this technique relies on is iconv, which lets us convert data between different character encodings

the problem is that some iconv conversions with certain encodings can produce extra bytes at the beginning of the output, like the BOM in UTF-16 or the escape sequence in ISO-2022-KR, by using multiple conversions, we can take advantage of these differences between encoding tables to change the resulting bytes and get the character we want

after that, we use the base64-decode filter because base64-decode skips any characters that dont belong to the base64 alphabet, and then base64-encode rebuilds the representation cleanly, this lets us use the characters produced by iconv conversions and get rid of the characters we dont need

by combining multiple conversions together, we can build filter chains that give us more control over the output, and this is the foundation used by the Synacktiv article

So how can we add our own content?

lets say we have a local file and want to read it, the first thing we do is convert its content to base64 using:

Text
convert.base64-encode

why do we use base64 in the first place?

because we want to work with the content in a way we can control, base64 has a specific alphabet made of 64 characters, which gives us room to use iconv conversions to produce specific characters and add them to the output

as mentioned earlier, we can use a group of conversions to produce a specific character at the beginning of the base64 string, we call this group a digit chain

for example, this chain can add the character K at the beginning of the output:

Text
convert.iconv.863.UTF-16
	|
	v
convert.iconv.ISO6937.UTF16LE
	|
	v
convert.base64-decode
	|
	v
convert.base64-encode

as you can see, we now have the character K at the beginning of the base64 of /etc/passwd:

Open full-size image

and of course, we can repeat the same process using different digit chains to add more characters and build an arbitrary prefix that we control

WrapWrap

now we have everything we need

using digit chains, we can add an arbitrary prefix to the beginning of the content, we can also add an arbitrary suffix, but the technique is different from a digit chain, and im not going into it here. you can check the Lexfo article if you want more details about it

Lexfo made a tool called Wrapwrap, which combines the ideas i explained so we can control the final output the way we want by controlling the prefix and suffix, like this:

Text
[prefix] + [original content] + [suffix]

this is exactly what we need here, because we can use this technique to add the magic bytes so the site thinks local files are images

like i said, if you want to go deeper and understand how the filter chains themselves are built, i recommend reading these articles:

https://www.synacktiv.com/publications/php-filters-chain-what-is-it-and-how-to-use-it

https://blog.lexfo.fr/wrapwrap-php-filters-suffix.html

Exploitation!

now we understand the idea behind php filter chains and the Wrapwrap technique from Lexfo

as i said, Lexfo made a tool for their technique called Wrapwrap, the idea behind Wrapwrap makes everything easier, instead of calculating and figuring out how to add the prefix and suffix, it does everything for us, it just asks us what prefix and suffix we want

the tool usage is simple:

Bash
python3 wrapwrap.py /file/that/you/want 'prefix' 'suffix' 2000 # number of bytes to read (important when you use suffix), e.g. 2000

also, instead of writing the prefix i want in the command, i can make it read a file that contains the prefix using -f, this is useful when the prefix is bytes that you cant type directly in the command

since the site checks the magic bytes to see whether the content is an image or not, and we want to read local files like /etc/passwd, the solution is to put the magic bytes in the prefix, for example, the png magic bytes:

Text
89 50 4E 47 0D 0A 1A 0A
Open full-size image

nice, now we put these bytes as raw bytes in a file called bin.bin, then we'll run the wrapwrap tool and use -f to point it to the file that contains the magic bytes, so those bytes become the prefix, in this case, the file is bin.bin, and it will generate the full chain in a file called chain.txt

Open full-size image

as you can see, we sent the request with the php wrapper filter chain, and it accepted it as an image!

Open full-size image

and as you can see, we got the content of /etc/passwd!

Open full-size image

i hope you liked the write-up ❤️ this is one of the most interesting and fun vulnerabilities for me (of course, after the esbuild vulnerability 😂)

this diagram explains what happened:

Scroll to readOpen full-size diagram
← All writing