WEBVTT

00:01.780 --> 00:02.580
Hi.

00:03.980 --> 00:04.740
Yeah.

00:05.240 --> 00:06.240
Thank you.

00:08.040 --> 00:16.540
Well, thank you for coming to my talk, because I know it's the last talk for today, so I guess everybody's pretty tired of seeing talks, but hopefully it will be interesting.

00:17.100 --> 00:21.900
So, the bug is a bug that I found by auditing PHP, but it's not on PHP.

00:21.900 --> 00:29.160
It's a bug in the glibc, and it targets iconf, which is the character set conversion library of the glibc.

00:29.740 --> 00:38.920
And I actually was able to use the bug to compromise the PHP engine in two different ways remotely, and this is what I'm going to talk about today.

00:40.040 --> 00:43.580
So, first I will describe how I found the bug.

00:43.580 --> 00:54.800
It was by complete accident, by complete chance, but the way I will explain it to you will serve as an introduction to the next section, which is going to be about attacking PHP filters.

00:54.800 --> 00:58.480
If you don't know what a PHP filter is, don't worry, I will explain what it is.

00:58.480 --> 01:02.220
And also, afterwards, I will attack a different way.

01:02.220 --> 01:05.860
I will attack PHP by targeting direct calls to iconf.

01:06.120 --> 01:12.160
And, well, after this, hopefully we'll have time for a conclusion and maybe a little bit of a bonus.

01:13.200 --> 01:22.480
So, regarding the discovery, well, to understand how I found the bug, we need to understand what you can do when you have a valid primitive in PHP.

01:22.480 --> 01:33.140
So, code such as this one, an fopen followed by an fread, like any valid primitive in PHP, what can you do with it?

01:33.200 --> 01:44.420
Well, obviously you can read a file, you can read ETC passwords, but you can also force PHP into reading HTTP resource, that would work, and FTP resource, that would work as well.

01:44.600 --> 01:52.800
And there are also other protocols that you can use, which are PHP-specific, such as far-colon-slash-slash.

01:52.800 --> 01:58.320
It will let you read the contents of a file archive.

01:58.320 --> 02:08.540
So, for those who don't know, a file archive, it's like a jar for Java, so it's a collection of source files, resources, and also in PHP, it contains serialized metadata.

02:08.560 --> 02:14.560
And when you access such a file using far-colon-slash-slash, the metadata will be deserialized.

02:14.740 --> 02:32.160
So, there was an attack for the longest time, that was that you could upload a file to a server, any file, an image, or anything, and then you would use your file with primitive to load it as a file, and that would give you arbitrary deserialization, which oftentimes meant RCE.

02:33.320 --> 02:41.660
Sadly, in PHP 8, they have disabled the deserialization of the metadata, because they actually don't use the metadata, so it was completely useless.

02:41.820 --> 02:53.460
And also, in older versions, big frameworks have been removing the far-colon-slash-slash protocol, because they don't use it, and they also know that it is very dangerous.

02:53.660 --> 02:58.660
Magento got completely demolished with the far-colon-slash-slash bug.

02:59.000 --> 03:03.340
There were hundreds of bugs reported to them using far-colon-slash-slash.

03:03.340 --> 03:05.160
So, they just removed it entirely.

03:05.800 --> 03:17.760
And also, it's going to be harder to exploit, even not on PHP 8, this bug, because deserialization is going to get harder and harder to exploit as time goes by, mainly because types are making a comeback.

03:17.760 --> 03:20.020
But that's probably a subject for another talk.

03:21.480 --> 03:26.580
So, people are taking the interest into another PHP-specific protocol, which is called PHP...

03:26.580 --> 03:29.240
well, you can guess it's PHP-specific.

03:29.360 --> 03:32.360
And there's a sub-part of this which is called PHP filter.

03:32.380 --> 03:38.660
And what it does is that it will read a resource and apply some filters that you have chosen onto it.

03:39.620 --> 03:44.680
It will then return the resource as if it was the original file, but with filters applied onto it.

03:44.680 --> 03:48.960
So, a good example of this, and probably the simplest one that you can get, is this string.

03:48.960 --> 03:52.980
So, you have PHP filter, then one filter which is convert.base64encodes.

03:52.980 --> 03:54.400
You can probably guess what it does.

03:54.400 --> 03:55.540
It converts to base64.

03:55.540 --> 03:57.960
And then the resource is etcpassword.

03:58.220 --> 04:02.900
So, what you get when you do a file get contents of this is file get...

04:02.900 --> 04:06.200
is etcpassword, sorry, but base64encoded.

04:06.480 --> 04:10.180
And the good thing about these filters is that you can stack as many as you want.

04:10.180 --> 04:16.360
So, instead of base64encoding once, I can do it twice, but I could also do it like a thousand times.

04:16.360 --> 04:17.280
It will work.

04:18.860 --> 04:21.940
Well, obviously, you can do other things than base64encodes.

04:21.940 --> 04:32.800
You might be able to put your string in uppercase with string.upper, or put it in lowercase with string.lower, and do some very, very old crypto with rod13.

04:32.800 --> 04:35.300
I don't know why it's there, but it actually works.

04:35.520 --> 04:41.780
And you can also use filters that are not documented, but very useful as an attacker, such as dchunk.

04:41.780 --> 04:43.020
I will talk about it later.

04:43.020 --> 04:48.520
And, well, you can also use the stuff for today, which is convert.iconv.x.y.

04:48.520 --> 04:57.000
And what it will do is that it will use the iconv library of the gdpc to convert your stream from charsetX to charsetY.

04:57.040 --> 05:11.800
Just to give you a quick example, say that you have etcpassword, you want to convert it from utf-8 to utf-16, you can use this PHP filter string, and it will do it automatically, as you can see in the xdump right underneath.

05:13.800 --> 05:18.780
So, well, these filters have been attacked a lot.

05:18.780 --> 05:20.700
There's been a lot of research on them.

05:20.700 --> 05:28.500
And the consensus in 2023 was that you could add, using filters, an arbitrary prefix to any file.

05:28.500 --> 05:35.260
So you could dump etcpassword with an arbitrary prefix just by picking the right filters to apply onto it.

05:35.520 --> 05:40.920
And myself, what I wanted to see was whether or not I could get an arbitrary suffix as well.

05:40.920 --> 05:45.660
So, an arbitrary prefix on some text, and then some suffix as well.

05:45.660 --> 05:47.140
And the idea is very simple.

05:47.140 --> 05:48.780
Say that you have this piece of code.

05:48.860 --> 05:52.720
Of course you have a far-read primitive, but then right afterwards you have a JSON decode.

05:52.720 --> 05:56.760
So if the file is not valid JSON, your far-read primitive is useless.

05:56.760 --> 05:58.420
You cannot read etcpassword.

05:58.420 --> 06:13.000
But if you can make your file look like JSON, using an arbitrary prefix and suffix, you can make the JSON decode work, and therefore you can make PHP think it's JSON and return to you the contents of etcpassword.

06:13.020 --> 06:15.200
And this research, it actually works.

06:15.200 --> 06:18.560
It has a few preconditions, but you can actually pull it off.

06:18.560 --> 06:23.180
And I call the tool VrapVrap, and I've made a blog post about it.

06:23.180 --> 06:27.880
It's on our company's blog, so I'mbertenix.io, as you can see on the bottom.

06:28.220 --> 06:36.640
And, well, the important part about this is that at the beginning I didn't know much when I started this research about PHP filters.

06:36.680 --> 06:40.680
So I built a few scripts just to try and do some conversions.

06:40.680 --> 06:45.720
So maybe from charset A to B, then apply a basic C4D code, then from C to D.

06:45.720 --> 06:49.860
Like, lots of conversions just to see what I could get as a result.

06:49.860 --> 06:53.440
And I actually got a crash while doing this, so I was pretty surprised.

06:53.640 --> 07:01.120
And I've been working with PHP for, like, 20 years, so I kind of felt like it was due to PHP, you know, because it's always PHP.

07:01.140 --> 07:02.800
But this time it wasn't PHP.

07:02.940 --> 07:08.420
It was actually due to the glibc, so the base library for most Linux programs.

07:08.480 --> 07:15.140
Because when PHP does the character set conversion using convert.iconv, it will use the iconv library of the glibc.

07:15.260 --> 07:26.440
And what this library does is that you pick an input charset and an output charset, and then you feed it buffers, and it will convert from your input buffer to the output buffer, from one charset to another.

07:27.340 --> 07:34.200
The thing is, when you call the icon function, you give it an input buffer with some size and an output buffer with some other size.

07:34.200 --> 07:43.840
And obviously, it should guarantee that it will now read more bytes in the input buffer than it should, or write more bytes than it should in the output buffer.

07:43.840 --> 07:47.200
And generally, it does so pretty well, like, there's no overflow.

07:47.400 --> 08:06.540
But sometimes it fails to do so, and this is exactly why I got my crash, is that when you convert to some very obscure charset, which is called ISO 2022CNX, in some cases, it will not check the bounds of the output buffer before writing some escape sequences.

08:06.920 --> 08:18.940
And that means that, as a result, as an attacker, if you are able to convert some text to ISO 2022CNX, you will get an overflow of one to three bytes with the very specific values that I've written here.

08:18.940 --> 08:26.540
So your overflow could be with $H, or $I, or $J, any of these six values, but only these six values.

08:28.000 --> 08:34.900
To give you a quick idea of the characters that you can use to trigger the bug, those are the characters that I trigger the bug with.

08:35.120 --> 08:39.580
So those are Chinese characters, I have no idea what they mean, but they give you a buffer overflow.

08:41.720 --> 08:50.540
So the vulnerability that we have, it's already pretty bad, like, it's a one to three bytes overflow, and it's fixed byte values, like, you don't control the bytes.

08:50.600 --> 09:00.340
In addition to this, you have horrible conditions, like, you have to control the output charset, you have to set it to ISO 2022CNX, like, nobody heard about it in the room, I guess.

09:00.340 --> 09:04.420
So, yeah, you need to find an application that will let you pick your output charset.

09:04.420 --> 09:07.200
Otherwise, you will not find an application that does it willingly.

09:07.500 --> 09:13.200
In addition to this, you need to control part of the input buffer so that you can put in the Chinese characters.

09:13.580 --> 09:18.240
And again, a third condition is that you need to have a suitable output buffer.

09:18.360 --> 09:33.800
If the output buffer is too small, sorry, too big compared to the input buffer, even though you may have an overflow in theory, you're not going to be able to reach the end of the bounds of the output buffer before you trigger your escape sequence, and therefore you will not overflow.

09:33.900 --> 09:49.620
And this is, like, what was the most annoying part in looking for targets for this bug, because I thought that I had a bug in libxml2, because you can send an encoding when you send your XML, but the output buffer, when it did the conversion, was way too big,

09:49.620 --> 09:50.780
and therefore I couldn't exploit.

09:50.780 --> 09:53.000
And that's only one of many.

09:53.000 --> 10:08.500
And so, after probably weeks of looking for targets, I got super frustrated, and I went back to PHP filters, because that's the only thing that I could make crash, and that's actually the only thing that crashed without even me trying for it to crash,

10:08.500 --> 10:15.000
so I was like, okay, that's probably my best bet to exploit this bug, and I focused onto it.

10:15.020 --> 10:24.020
And the idea was to convert file-read primitives in PHP, any file-read primitive, into remote code execution, straight up using this binary bug.

10:24.760 --> 10:32.040
So to explain to you how we might be able to do this, we need to think about how PHP does allocations and deallocations.

10:32.740 --> 10:38.380
For this, we need to understand PHP's heap, but don't worry, that's probably the simplest heap that you're ever going to see.

10:38.520 --> 10:50.000
So it's 512 pages of 0x1000 bytes each, so 4096 bytes, and each page contains chunks of some size, so it might be a chunk...

10:50.000 --> 10:56.260
In page 10, you may have chunks of size 0x100, in page 11, you may have chunks of size 0x30, et cetera, et cetera.

10:56.940 --> 11:01.520
Then when you free a chunk, it will go into the free list for this chunk size.

11:01.700 --> 11:06.180
So if I free a chunk of size 0x100, it will go into the free list for size 0x100.

11:06.180 --> 11:12.120
If I free a chunk of size 0x10, it will go into the free list for chunks of this size as well.

11:12.340 --> 11:22.940
And the free lists are singly linked lists, they are lasting first out, so that means that if I free a chunk of some size and allocate a new one afterwards, it will be allocated exactly at the same spot.

11:23.140 --> 11:33.160
And it's very similar for those who do PTMalloc exploitation to the libctcache, but completely unlimited and without any protections.

11:34.360 --> 11:41.600
So, just to give you an idea of what we can do with 1 to 3 bytes overflow in the PHP heap, let's have a look at this example.

11:41.600 --> 11:50.540
So , on the left you have the heap, with 512 pages, and in page 5, in this example, I have chunks of size 0x400.

11:50.700 --> 11:53.020
So, in page 5, I have 4 chunks.

11:53.340 --> 11:59.180
In my example, I have chunk 1 that is allocated, chunk 3 that is allocated, chunk 2 and 4 that are not.

11:59.340 --> 12:07.880
So, in chunk 2 and chunk 4, since they are in the free list for chunks of size 0x400, that means that they contain a pointer to the next free chunk.

12:08.260 --> 12:19.220
So, if I was to overflow even with 1 byte from chunk 1, I would be able to at least partially modify the pointer that is contained in chunk 2.

12:19.420 --> 12:27.680
And that would be a way to alter some free list pointer, and in the end, probably be a good starting point for an exploitation.

12:29.720 --> 12:33.900
So, the PHP heap, it looks very, very easy to exploit, right?

12:33.900 --> 12:39.300
There's no metadata, it's page-based, there's absolutely no protections, it's really easy to understand.

12:39.300 --> 12:43.240
So, why is it that there are not that many remote binary exploits?

12:43.240 --> 12:49.640
Well, to me, the main problem is that the PHP has the one heap for one request policy.

12:49.800 --> 13:03.440
So, if you send an HTTP request, PHP will create the heap, allocate your HTTP parameters into it, then run the script, so that means also compiling the script, so it will do like thousands of allocations and deallocations, and then it will completely destroy the heap.

13:03.680 --> 13:08.260
So, now imagine that you have a use-after-free on some VPN instance, for instance.

13:08.840 --> 13:20.660
What you would do to exploit would be to send a few requests to set up your heap, then one more request to trigger your bug, trigger your UAF, and then afterwards, send a few more requests to use your bug.

13:20.960 --> 13:23.160
But here, you cannot do this.

13:23.160 --> 13:35.460
You have to find a way to trigger a bug remotely, so you need a bug in the PHP engine, you need an application that lets you trigger the bug remotely, but also you need to use the bug as the request is running, before the heap is gone.

13:35.460 --> 13:43.900
And this is why it's hard, because in between the triggering of the bug and the usage of the bug, you're not going to have any way to interact with the HTTP request.

13:43.900 --> 13:47.000
Like, you've sent every HTTP parameter, and it's done.

13:47.000 --> 13:48.340
Like, it's a black box now.

13:48.720 --> 13:54.240
And this is, to me, why it's really, really hard to exploit on PHP remotely.

13:59.640 --> 14:00.480
Okay.

14:00.500 --> 14:06.440
So, in addition to this, there's one thing that we need to understand, is how PHP processes PHP filter strings.

14:06.620 --> 14:28.200
And to do so, you will have a two-step process, where you will first read the resource as a stream, and then you will apply filters, So, fetching the stream, it will use a very standard structure, which is called a bucket brigade, and each part of the stream is represented as one bucket of a few bytes.

14:28.200 --> 14:40.520
So, for instance, if you read the password, the first bucket may contain five bytes of its password, then the second bucket will contain the next few bytes, for instance, ten bytes, and the bucket three will contain a few more bytes that come right after,

14:40.520 --> 14:41.820
et cetera, et cetera, et cetera.

14:41.860 --> 14:45.080
So that's a way to be able to represent any stream.

14:45.080 --> 14:48.960
A list of buffers that are linked together, and that may have different sizes.

14:51.560 --> 14:55.320
Once you have your stream, you can start applying filters onto them.

14:55.320 --> 14:59.820
So you take the first filter, and you take the first bucket, and you apply the filter onto it.

15:00.240 --> 15:07.040
So, for instance, if it's base64 decode, PHP will take the first bucket, and decode the data that is into it.

15:07.040 --> 15:11.280
Then it will be able to process bucket two, which it will do, and then bucket three.

15:11.360 --> 15:15.900
And at the end, you get a new bucket brigade, on which the first filter has been applied.

15:16.040 --> 15:22.620
So what you can do, is start working on the second filter, then on the third one, et cetera, et cetera, up until the end.

15:22.660 --> 15:31.060
And now you have a stream that has been completely processed with filters, and your PHP filter string has been completely, perfectly handled.

15:32.500 --> 15:43.320
So, if we look back at everything now, it looks pretty easy to exploit this far read, because we have a far read primitive, and we want to get our C, but since we have a far read primitive, well, first we can read the binaries.

15:43.320 --> 15:50.620
So, if I want to have the address of some symbol, for instance, in the libc, I just download the libc using the far read primitive.

15:50.640 --> 15:55.960
If I want to check if the libc is patched against the bug, I can also download the libc and check the code myself, you know?

15:56.400 --> 15:59.180
So that means that you can find gadgets easily.

15:59.220 --> 16:03.880
In addition to this, ASLR and Pi are not a problem, because I can just read Proxet Maps.

16:04.180 --> 16:11.320
So, any address that I want is going to be probably in Proxet Maps, so ASLR and Pi, it's gone.

16:11.700 --> 16:20.780
A corollary to this is that the address of the PHP heap is really easy to find in Proxet Maps, so I have the address of the PHP heap, which is going to be useful later.

16:21.020 --> 16:31.420
In addition to this, it kind of feels like that I can do arbitrary allocations on the allocations simply using the bucket brigade technology, so the way that the stream is represented.

16:31.420 --> 16:37.840
If I can create as many buckets as I want with the sizes that I want, I will probably be able to build an exploit that works.

16:39.420 --> 16:54.160
But on the other side, I kind of want to have an exploit that works well even in an unknown environment, so I don't want to build a new exploit when I'm targeting WordPress or some very specific version of PHP or some Magento, which has a lot of traffic on the website.

16:54.180 --> 16:59.040
I want an exploit that just straight up works, like basically a web exploit, even though it's binary.

16:59.100 --> 17:01.440
And also bonus points if it doesn't match.

17:02.640 --> 17:04.620
So, how can we do this?

17:04.620 --> 17:12.180
Well, the first problem that we face is that even though there is the bucket brigade technology in PHP, you will always get one bucket when you read a stream.

17:12.180 --> 17:16.280
So , if PHP reads the ETC password, it will give you one bucket with the whole file.

17:16.280 --> 17:22.620
If it reads an HTTP resource, it will give you one bucket with the whole HTTP response into it.

17:22.620 --> 17:24.180
Same for FTP, same for everything.

17:24.180 --> 17:25.580
You always get one bucket.

17:25.680 --> 17:33.720
And one bucket, it's completely impractical for exploitation, because we cannot spray the heap, we cannot pad the heap, and we can't even use an altered freelist.

17:33.720 --> 17:45.820
Like, if we use one bucket to trigger the overflow, we have an altered freelist, but then we need at least two more locations to be able to use this altered freelist, and we cannot do this because we have used our single bucket, so we're not going to be able to exploit.

17:47.180 --> 17:53.700
Luckily for us, there's a single filter that allows us to create as many buckets as we want, and this filter is zlib-inflate.

17:53.700 --> 18:00.500
What it does is that it will read your stream and decompress it, as with the zlib algorithm.

18:00.580 --> 18:06.980
And the way it works is that it will allocate eight pages, a buffer of eight pages, and then decompress into it.

18:07.000 --> 18:11.420
If it's not big enough, it will create eight more pages and decompress the rest into it.

18:11.420 --> 18:15.780
If it's still not big enough, it will create a third bucket, et cetera, et cetera, et cetera.

18:15.920 --> 18:21.140
So this filter, it gives us a way to go from one bucket to as many as we want.

18:21.140 --> 18:36.440
But the problem that we have is that every bucket has a size of eight pages, which is way too big to exploit because it takes too much space, and also it's not going to be allocated the same way as small chunks.

18:36.800 --> 18:40.800
So we need a way to change the size of each of these buckets.

18:41.100 --> 18:45.280
And a way to do this is to use another filter, which is called dchunk.

18:45.300 --> 18:50.320
It's undocumented, but it's been used by many hackers before, so it's pretty well known.

18:50.320 --> 18:57.780
And the idea is that it will take an HTTP chunked encoded string and simply decode it.

18:57.780 --> 19:01.500
So for those who don't know, the HTTP chunked encoding is very simple.

19:01.500 --> 19:10.120
It consists of sending a size as ASCII X and then sending the data, and then a new size as ASCII X and then sending the data, etc.

19:10.160 --> 19:18.840
So the dchunk filter, what it does is that it will read a size as ASCII X, then read the amount of bytes that correspond to this size.

19:18.840 --> 19:22.760
So for instance, if it reads 8, as in the example, it will read 8 bytes.

19:22.760 --> 19:27.880
Then it will read 11, and since it's ASCII X, it's 17, so it will read 17 bytes.

19:27.880 --> 19:32.140
And then it will read D, read the D bytes, and then it will read 0.

19:32.140 --> 19:37.340
And 0, it doesn't indicate an empty chunk, it indicates the end of the dchunking.

19:37.340 --> 19:40.400
So when it receives 0, it stops processing the data.

19:41.720 --> 19:46.820
So, now you probably have an idea of how we can resize our chunks.

19:47.280 --> 19:54.240
We take bucket 1, if we want it to have a size of 148, we write 148, then 148 bytes, and then 0.

19:54.320 --> 19:57.600
If we want bucket 2 to have a size of 100, we do the same.

19:57.600 --> 20:00.700
We write 100, then all bytes, and then 0.

20:00.700 --> 20:02.240
And we do the same with every chunk.

20:02.240 --> 20:07.340
And then when we apply dchunk onto this, we expect it to resize every single bucket.

20:07.440 --> 20:16.480
But you may notice that this won't work, because even though each bucket is different, they are all processed as one stream.

20:16.480 --> 20:28.100
So if in bucket 1, you indicate that it's the end of the stream, with the 0 at the very bottom, it will not process bucket 2, it will not process bucket 3, it will not process any bucket that comes after.

20:28.100 --> 20:33.140
So you go back to a single bucket with an arbitrary size, which is completely useless.

20:33.660 --> 20:36.740
So luckily for us, there's a way to improve this.

20:36.960 --> 20:44.000
And the way to improve this is to pad the size with thousands of 0s, such as what we can see here.

20:44.000 --> 20:48.280
The idea is that in each bucket, we will have one chunk size and one chunk data.

20:48.280 --> 20:49.000
And that's it.

20:49.000 --> 20:49.860
Nothing else.

20:49.860 --> 21:02.400
So now if the dchunk filter is applied, we will have bucket 1 that gets processed, it will read the size, read thousands of 0s, then the real size, then the data, and then it will go onto bucket 2 and do exactly the same.

21:02.400 --> 21:06.440
It will read the size, read the data, then bucket 3, read the size, read the data.

21:06.460 --> 21:13.180
And you can see that bucket 1 can be resized to the proper size, bucket 2 as well, bucket 3 as well, and bucket 4 as well.

21:13.180 --> 21:17.620
And now we have a way to create as many buckets as we want, and change the size.

21:17.620 --> 21:21.020
And this is only with two filters, so it's pretty promising.

21:23.060 --> 21:33.380
But that's not enough, because we can only send one request, so we need a whole exploit algorithm in a single request, and with a single PHP filter string.

21:33.380 --> 21:34.180
So how do we do this?

21:34.180 --> 21:42.760
Well, instead of only being able to allocate once, we kind of want to be able to allocate several times to represent the different steps of the exploit.

21:42.760 --> 21:47.240
In step 1 I want to allocate a few chunks, in step 2 I want to allocate a few more chunks, et cetera, et cetera.

21:47.260 --> 21:49.440
And at this point we are not able to do this.

21:49.540 --> 21:52.660
But to do this, what we can do is think about the PHP heap.

21:52.660 --> 21:53.740
It's page-based.

21:53.740 --> 22:02.660
So if I focus my exploitation on one chunk size, a single chunk size, that means that chunks that are bigger or smaller than this chunk size, they do not really matter.

22:02.660 --> 22:05.500
I will not have to think about them in my exploit algorithm.

22:05.500 --> 22:13.020
If a chunk is of size 0x200, for instance, and I'm focusing on chunks of size 0x100, the chunk will not affect my exploitation.

22:13.240 --> 22:20.740
So at this point, if I focus on some very specific chunk size, such as 0x100, chunks that are bigger or smaller, they do not matter.

22:20.740 --> 22:22.320
It's as if they weren't there.

22:22.320 --> 22:27.800
So the only thing that I need to be able to do is to be able to resize buckets as the exploitation goes on.

22:27.800 --> 22:34.140
And to be able to do this, I built buckets that are Russian dolls of dchunk algorithm.

22:34.220 --> 22:40.140
So the idea is that every time I call dchunk on some buckets, its size will change.

22:40.140 --> 22:47.740
And that means that I can make buckets appear at some point in the exploitation, and then others appear at another step of the exploitation.

22:47.860 --> 22:51.980
And that basically means that I can allocate and deallocate at will.

22:51.980 --> 22:55.260
And at this point, I'm really, really close to getting an exploit working.

22:56.720 --> 23:01.760
And the final idea here is that we have a one-byte overflow, using the icon bug.

23:01.860 --> 23:09.360
And we want to use it to alter some freelist and get complete control over it.

23:09.400 --> 23:14.600
And the way that we can do this is a six-step process, which is pretty simple, but I will still explain it to you.

23:16.260 --> 23:18.420
First, we will start in step one.

23:18.420 --> 23:21.460
And we're going to have three chunks, A, B, C.

23:21.460 --> 23:23.280
And each chunk is freed.

23:23.280 --> 23:26.320
We have A that points to B, and B that points to C.

23:26.320 --> 23:28.720
Then we allocate these three chunks, and we free them.

23:28.720 --> 23:33.940
We get in step three, and the only difference with step one is that now we have C that points to B that points to A.

23:33.940 --> 23:35.500
So we have reversed the freelist.

23:35.840 --> 23:45.360
Now we allocate in C a buffer, which contains at offset 48, an arbitrary pointer, which is represented here with 1, 1, 2, 2, 4, 4, etc.

23:46.100 --> 23:49.060
Then we allocate B and A, and then we free everything.

23:49.060 --> 23:50.500
And we get into step five.

23:50.500 --> 24:00.280
And we have exactly the same E-playout and freelist as in step one, but in the chunk C, we have an arbitrary pointer at offset 48.

24:00.280 --> 24:02.340
So now you can probably see where this is going.

24:02.340 --> 24:06.280
We overflow using chunk A into chunk B.

24:06.280 --> 24:11.100
So the freelist that was B to C is now B to C plus 48.

24:11.120 --> 24:14.880
And what we have in C plus 48 is another pointer that we added ourselves.

24:14.920 --> 24:21.320
So with three more allocations, we will allocate B, C plus 48, and then we will allocate our arbitrary pointer.

24:21.640 --> 24:28.520
And now we are able to have a write-what-where, which is really, really close to getting code execution.

24:30.400 --> 24:33.120
But now we have to think about how we can do this.

24:33.120 --> 24:39.200
Well, as I've said before, the PHP heap, it's really easy to find it in Proc Setmaps, so we have it for us.

24:39.300 --> 24:50.840
And also, at the very top of the PHP heap, there is a send-mmheap structure that contains a few things, and also a few alternative heap function pointers.

24:50.920 --> 24:56.740
So for those, again, we do heap exploitation on ptMalloc, it would be the same as free-hook, malloc-hook, and realloc-hook.

24:56.760 --> 25:03.500
Those are pointers that, if you set them to something else that null, they're going to be called inside of the original function.

25:03.700 --> 25:10.000
And so, since it's basically the same as the glibc, I can use the glibc technique, and I can override free with system.

25:10.000 --> 25:14.480
So now, when PHP wants to free a chunk, it will call free, but it will instead call system.

25:14.480 --> 25:20.380
And this gives me a way to call an arbitrary comment.

25:21.220 --> 25:25.480
And so, with everything put together, what do we have?

25:25.480 --> 25:35.720
We have an exploit that works against any target, from PHP 7 to the current version, which is H3-something.

25:35.720 --> 25:39.600
So that's 10 years of PHP covered with a single binary exploit.

25:39.600 --> 25:48.240
And also, it works against any PHP application, because we can pad the heap, so even if the website uses a lot of chunks of size 800, it will still work.

25:48.240 --> 25:49.160
It doesn't matter.

25:49.280 --> 25:56.540
In addition to this, the exploit is completely deterministic, so it's 100% reliable, so you will never get a crash.

25:56.660 --> 25:57.700
So it's pretty cool.

25:57.860 --> 26:08.220
And the final good thing about the exploit is that the payload is really small, because the first gadget that we use, the first filter that we use, is a Zlib inflate, so it will decompress the payload.

26:08.220 --> 26:10.480
So originally, it's compressed, so it's very small.

26:10.480 --> 26:16.560
And the last good thing that we can say about the exploit is that it's completely self-contained.

26:16.680 --> 26:23.760
The whole algorithm of the exploit is in one PHP filter string, which means that you don't have to send additional GET or POST parameters.

26:23.760 --> 26:26.560
You just need your PHP filter string, and you get RCE.

26:26.780 --> 26:32.700
So every file reading primitive now that you have on PHP, you have RCE instead, which is pretty nice.

26:33.400 --> 26:39.940
I'm going to show you a demo that actually involves Cosmic Sting, which is a Magento X6E, which is pre-authentication.

26:39.940 --> 26:43.100
And it came out after I published my bug, so it was in June.

26:43.160 --> 26:46.840
And the original impact was being able to read files.

26:46.880 --> 26:57.300
And using these files that you've read, you would be able to craft API keys, and so you would get an access which is similar to an administrator level on Magento.

26:57.300 --> 26:59.600
But you wouldn't be able to get RCE.

26:59.700 --> 27:10.380
But with the bug that we have now, we get RCE easily because we have an X6E, so we can force it into reading any file, and therefore we can read a PHP filter string and get RCE.

27:12.560 --> 27:17.320
So, quick demo, which is not on the right screen.

27:18.460 --> 27:19.380
There we go.

27:23.640 --> 27:28.420
So, I cannot see it, but it's quite easy to understand.

27:28.420 --> 27:36.220
You have the Magento website on the left, and then you have an exploit on the right, and in a few moments, you will have a shell on the bottom.

27:36.820 --> 27:38.140
There you go.

28:07.360 --> 28:09.120
There we go.

28:09.120 --> 28:10.160
Thank you, man.

28:11.340 --> 28:14.580
So, oh, I lost my...

28:14.580 --> 28:16.400
On the other screen, I lost it.

28:20.440 --> 28:21.480
Wait.

28:24.000 --> 28:25.580
Okay, there we go.

28:26.020 --> 28:27.960
Okay, does that work?

28:28.320 --> 28:29.320
Yeah.

28:29.760 --> 28:41.980
Okay, so now that we have exploited PHP filters, we want to do a completely different exploitation, because PHP filters are not the only way in which you can reach ICONF using PHP.

28:41.980 --> 28:44.520
There's another way that is way simpler.

28:44.560 --> 28:46.940
It is when PHP calls ICONF directly.

28:47.540 --> 28:51.220
And so, there's a PHP function that does this.

28:51.220 --> 28:52.660
It's called ICONF.

28:55.840 --> 28:59.860
So, how do we exploit such a call to ICONF?

28:59.860 --> 29:05.680
Well, it's very, very different to the attack that we've seen using PHP filters.

29:06.980 --> 29:13.640
So, here we don't have a five-read primitive, so we cannot just read proxmaps or read the libc or anything.

29:13.640 --> 29:22.640
We need to go, I would say, old-school exploitation, where we need an infoleak to get some pointers, and then we can start going with the exploitation.

29:22.920 --> 29:33.760
And in PHP, generally, when you want to have an infoleak, what you do is that you will try and alter the length field of a ZenString.

29:33.760 --> 29:38.540
So, a ZenString, it's the representation of PHP of your PHP string.

29:38.720 --> 29:41.480
And the length field, it will store the size of the string.

29:41.480 --> 29:56.280
So, if you are able to change the length of a string, and then it gets displayed, well, you are able to make PHP display more bytes than it should, and probably display lots of useful information from the heap, such as addresses.

29:57.140 --> 30:00.880
Sadly for us, the bug that we have, as I've said, really, really bad.

30:00.880 --> 30:03.040
We only have one to three bytes overflow.

30:03.040 --> 30:10.840
So, if we try to overflow into a ZenString, we will just overwrite the ref count, with values that we don't even control, so it's pretty useless.

30:11.940 --> 30:21.980
So, what we need to do is kind of the same as before, where we alter some free lists, and if we manage to alter some free lists, we are able to make two chunks overlap.

30:22.040 --> 30:31.060
And if we have two chunks that overlap, we can use the first one to store a string that is bound to be displayed, and then use the second one on top of it to overwrite the length field.

30:31.060 --> 30:36.920
And when the string gets displayed, we actually are able to see what comes afterwards in memory.

30:38.440 --> 30:42.260
The problem is that here, we completely rely on the code.

30:42.340 --> 30:49.920
Beforehand, we could implement an algorithm using PHP filters, as I've shown, but here we only have a single call to iconf, and that's it.

30:49.920 --> 30:50.780
Nothing else.

30:50.780 --> 30:59.440
So, getting it to work would make us reliant on the code that comes after the call to iconf.

30:59.440 --> 31:02.000
And to explain this, a very simple example.

31:02.040 --> 31:04.080
Say that you have this piece of code.

31:05.020 --> 31:12.200
We control the output charset, we control the input text, so it's clear that we can trigger the bug.

31:12.860 --> 31:16.820
But right after the bug has been triggered, the script will end.

31:16.920 --> 31:22.200
So, there's absolutely no way for us to use the bug after it's been triggered, because the heap will be destroyed.

31:23.040 --> 31:28.760
If you're lucky, however, you might find a line such as this one after your call to iconf.

31:28.960 --> 31:38.360
This line is the same as split in Python, so it will take a string and split it along a token, so here the token is newline.

31:39.500 --> 31:47.580
If you have a line such as this one, you can create as many chunks as you want, because it will take your string and split it along a known separator.

31:47.840 --> 31:54.080
And that will give you a way to use the bug right after it's been triggered, and then probably exploit the bug.

31:54.120 --> 31:56.420
So, this is what I call a code gadget, actually.

31:56.540 --> 32:04.540
That's a way to get to using your bug, starting from the position where iconf has been called.

32:05.440 --> 32:15.720
And to give you an idea of how it can be implemented in the real world, I will show you a demo, well, and explain to you the demo on Roundcube.

32:15.720 --> 32:20.840
Roundcube, for those who don't know, it's a webmail that is very much used.

32:21.220 --> 32:23.420
It's probably the most well-known in PHP.

32:23.560 --> 32:27.760
It's used by hosters generally, or private companies even, I've seen it a lot.

32:28.520 --> 32:31.520
And , well, yeah, it's used a lot.

32:32.280 --> 32:37.880
And so, when you're logged into Roundcube and you want to send an email, you have to specify email addresses.

32:37.880 --> 32:42.460
And so, you specify the two fields, the cc field and the bcc field.

32:42.460 --> 32:45.580
You can guess what they are, you've already sent an email.

32:45.820 --> 32:51.180
In addition to this, you can send another parameter, which is charset.

32:51.180 --> 32:59.020
And every parameter that you send, so the underscore 2, underscore cc, and underscore bcc parameter, they are all going to be cast to the charset that you've set.

32:59.020 --> 33:00.780
And it's going to be done using iconf.

33:00.780 --> 33:05.760
So, in this single script here, you have three ways of triggering your bug.

33:05.760 --> 33:10.960
You can trigger it using underscore 2, or with underscore cc, or with underscore bcc.

33:10.960 --> 33:13.780
And right after this, there's something even better.

33:13.780 --> 33:21.000
If one of the emails is not valid, you will get an error message that says, well, this is an invalid email, colon, your email.

33:21.540 --> 33:22.740
So, what can we do with this?

33:22.740 --> 33:27.780
Well, this is textbook, the strategy that I was telling you about before.

33:27.780 --> 33:33.160
The idea is that you set underscore 2 to an invalid email, so that invalid email gets set.

33:33.160 --> 33:35.340
It's ready to be displayed in the error message.

33:35.340 --> 33:41.120
So, now what we want to do is modify its length before it's included in the error message.

33:41.360 --> 33:44.980
So, then we use cc to trigger the bug, and we have a free list that is altered.

33:45.060 --> 33:54.100
And then we use bcc to allocate a few strings, so that we have a string that gets allocated on top of invalid email, and this will alter its size.

33:54.160 --> 34:01.800
And therefore, the error, when it gets displayed, will display more bytes than it should, and we will be able to get a memory leak, which is nice.

34:02.380 --> 34:04.320
So, I implemented this, and it was really fast.

34:04.320 --> 34:08.420
It took me a few hours, so I was really happy with it, and I started it, and it didn't work.

34:08.540 --> 34:14.780
And the reason for this is that when Roundcube sends an error message, it's converted to JSON.

34:14.780 --> 34:19.320
And when PHP converts to JSON, every byte that is not valid UTF-8 is removed.

34:19.380 --> 34:28.780
So, you have a memory leak, but you don't really have any bytes of interest, because generally addresses are not going to be valid UTF-8, so they're just going to be removed.

34:28.860 --> 34:32.700
Any raw data in memory is not going to be valid UTF-8, it's going to be removed.

34:32.700 --> 34:39.420
So, what you get is a memory leak, which consists of only ASCII characters and new bytes, which is completely useless.

34:39.820 --> 34:42.060
But, the idea is pretty good still.

34:42.060 --> 34:49.200
We still want to do the same thing, but we just need to find a string that will not get modified too much before it's displayed.

34:49.200 --> 34:58.560
And to do that, we just have to look at the framework and go a little bit deeper, look at many, many functions, up until we find a variable that gets displayed without being modified.

34:58.560 --> 35:11.300
Sadly for us, when it gets displayed without being modified, it will not have a lot of codes in between the creation of the variable and the display of the variable.

35:11.300 --> 35:15.300
So, it limits the number of gadgets that we can use.

35:15.680 --> 35:28.380
And , well, it's honestly pretty much hell to make this work, but you have to think that every line that you can use, like every line in the code, you can use it to go closer to your goal.

35:28.380 --> 35:32.660
Like, every line can be an allocation or deallocation that would shape the heap better for you.

35:32.740 --> 35:36.300
And I actually managed to pull that off in, I think, five days.

35:36.300 --> 35:38.120
It was really, really long.

35:38.120 --> 35:40.360
And I actually managed to get the leak.

35:40.360 --> 35:44.180
So, it was nice, because afterwards, it's pretty easy to get ASCII.

35:44.180 --> 35:55.840
Once you have an address of some value in the PHP heap, you can just reproduce the same exploit, but this time, use your freelist corruption to allocate at an arbitrary position.

35:55.960 --> 36:01.600
And that means that you can do data-only attacks, where you overwrite some value that is in your session, for instance.

36:01.600 --> 36:10.400
Maybe you want to overwrite your roles and set them to admin, or maybe you want to overwrite serialized data before it's deserialized, so that you have an arbitrary and serialized in PHP.

36:10.580 --> 36:17.020
Or, you can also go the standard way, where you go for a binary attack, where you want to control MIP.

36:17.020 --> 36:20.180
You started with binary, and you want to go full binary.

36:20.300 --> 36:25.440
But I think the data-only attack is more elegant, and it fits more, because you don't have to dump the binary.

36:25.540 --> 36:27.220
So, it's easier.

36:27.880 --> 36:32.160
But, like all of this that I'm saying, it's actually only the visible part of the iceberg.

36:32.160 --> 36:36.500
It's really, really hard to pull off, because first, you need a proper heap setup.

36:36.500 --> 36:46.120
I haven't covered this part, so it's really hard to do, because the only thing that you can use to do your heap setup is HTTP parameters, but then the script gets compiled, and you will use other scripts.

36:46.120 --> 36:49.600
They all get compiled in the same heap, so you will completely mess up the heap.

36:49.600 --> 36:51.120
It's really annoying to pull off.

36:51.280 --> 36:54.300
In addition to this, variable scope is here.

36:54.300 --> 37:00.600
If you have a method that is called, and then it exits the method, every variable that is in the method is going to be freed.

37:00.940 --> 37:03.900
So, it will completely change the heap as well.

37:04.100 --> 37:06.520
And also, obviously, it's very specific.

37:06.520 --> 37:11.660
Your attack to the server that you're targeting, the application that you're targeting, and also the settings.

37:11.660 --> 37:15.740
I think the original exploit that I built, it was for Roundcube in French.

37:15.740 --> 37:24.580
If I changed the language to English, it wouldn't work, because the strings that get allocated with error messages, they wouldn't be the same, and that would completely change the shape of the heap.

37:24.580 --> 37:30.420
So again, it's a huge pain to pull off, so don't do it unless it's really necessary.

37:31.000 --> 37:31.960
But it works!

37:32.340 --> 37:40.580
And I will show you an exploit, hopefully, if I can make it appear again.

37:46.050 --> 37:47.850
Oh, I'm paused.

37:48.670 --> 37:49.750
There we go.

37:50.350 --> 37:53.910
Now I need to find...

38:00.840 --> 38:03.280
Okay, same layout.

38:03.740 --> 38:10.360
You get a shell on the very bottom, an exploit on top of this, and the application on the left.

38:17.780 --> 38:20.800
And now I need to remove it again.

38:21.500 --> 38:22.880
Which one of these?

38:23.000 --> 38:23.860
This one?

38:23.920 --> 38:24.580
Nope.

38:25.820 --> 38:26.780
Nope.

38:27.980 --> 38:28.940
Yeah.

38:29.300 --> 38:30.000
Okay.

38:30.780 --> 38:33.940
A quick conclusion, and maybe a few more words afterwards.

38:35.960 --> 38:38.400
So the bug was really bad, honestly.

38:38.500 --> 38:42.400
It had lots of preconditions, and even the bug was not that good.

38:42.400 --> 38:44.140
A really bad overflow.

38:44.240 --> 38:52.480
But luckily for us, PHP exists, and we actually are able to do decent things with it.

38:52.480 --> 38:59.940
We have a new attack vector, where we convert a favorite primitive into an RCE on any PHP version, against any target.

38:59.940 --> 39:02.240
If you have a favorite primitive, you have RCE now.

39:02.240 --> 39:03.440
It's pretty nice.

39:03.500 --> 39:09.720
And also, you add application-specific attacks, such as the one I've demonstrated on Runcube.

39:09.720 --> 39:16.020
However, the bug is not PHP-specific, so there could be a lot of other targets.

39:16.100 --> 39:18.720
I couldn't find any, but maybe you will.

39:18.940 --> 39:29.020
And they might be discovered as time goes by, if people try to find a way to exploit the bug.

39:30.440 --> 39:32.340
The timeline goes fast.

39:32.340 --> 39:36.400
I found the bug last year, and I started exploiting it in February.

39:36.400 --> 39:41.740
After a month and a half, I could report it to the GDBC security team, and they were really amazing.

39:41.740 --> 39:42.780
They were really fast.

39:42.780 --> 39:45.380
After a week, I got a patch, and they were really nice.

39:45.420 --> 39:51.640
They knew what they were doing, which is not always the case, to say the least.

39:52.040 --> 40:00.620
So the bug got patched in a week, and two weeks after the embargo, it was released as CVE-2024-2961.

40:02.300 --> 40:04.920
That's probably the end of it for me.

40:04.920 --> 40:10.920
I have published lengthy blog posts about the bug, because there's so much to say about the bugs.

40:10.980 --> 40:16.180
And also, the impact on the PHP ecosystem, which I couldn't cover here, because we only have 45 minutes.

40:16.380 --> 40:19.540
And also, there are exploits that are available on our GitHub.

40:19.640 --> 40:21.820
And also, there's my Twitter, if you want to ask questions.

40:21.820 --> 40:25.300
You can also ask some questions right after the talk.

40:25.520 --> 40:29.520
And just to flex a little bit, a few what-ifs.

40:29.520 --> 40:34.640
What if we have a Fargate primitive, but we cannot see the output?

40:34.720 --> 40:37.340
We have Fargate contents, but no echo in front of it.

40:37.340 --> 40:39.100
So we cannot read FoxFMaps.

40:39.100 --> 40:41.120
We cannot read any binary.

40:41.180 --> 40:43.980
So the exploitation that I've shown you, it doesn't work.

40:44.280 --> 40:48.820
And also, in some very, very rare cases, zlibin-fake will not be present.

40:48.820 --> 40:53.460
You will not be able to decompress your stream, so you will have to exploit with one single pocket.

40:53.760 --> 40:55.840
And that looks impossible.

40:56.100 --> 40:59.240
But I was able to do both at the same time.

40:59.240 --> 41:00.980
That was actually my very first exploit.

41:01.640 --> 41:04.040
And I will try and show you how.

41:06.900 --> 41:10.620
It's just a demo, really sped up, because it's really slow.

41:10.620 --> 41:12.780
But, well, at the end you get a shell.

41:12.780 --> 41:15.920
The important part is on the left, the source code.

41:15.920 --> 41:19.320
You can see at the very bottom, you have a Fargate contents without an echo or anything.

41:19.320 --> 41:20.660
So I don't have the output.

41:20.680 --> 41:26.260
And also, on top of this, I have arbitrary allocation, uh, random, sorry, allocations and deallocations.

41:26.260 --> 41:30.780
Just to show that the script is not very, that it's not tailored to attack one PHP script.

41:30.780 --> 41:33.280
Like, it could work against any PHP instance.

41:33.540 --> 41:35.800
And, yeah, the exploit is not done yet.

41:35.800 --> 41:37.520
It's sped up like eight times, I think.

41:37.520 --> 41:38.540
It's really, really slow.

41:38.540 --> 41:42.320
But you get one byte per one byte, and at the end you have another shell.

41:42.360 --> 41:43.580
So, yeah, that's it.

41:50.200 --> 41:51.840
Okay, I'm getting close.

41:52.000 --> 41:52.640
No.

41:57.600 --> 41:58.400
Yeah.

41:58.620 --> 41:59.600
There we go.

42:00.420 --> 42:01.220
Questions?

42:09.890 --> 42:11.090
Okay, I guess we're good.

42:11.090 --> 42:12.290
Ah, there's one.

42:15.770 --> 42:16.550
Yeah, thank you.

42:16.550 --> 42:17.630
This is amazing.

42:17.790 --> 42:24.590
Uh , so, how did you get to the Roundcube target and establish that those conditions were present that you were looking for?

42:24.590 --> 42:29.430
Um, so, I thought that, like, emails to me...

42:30.010 --> 42:32.750
When you send an email, you can kind of send the charset with it.

42:32.750 --> 42:37.150
So I kind of felt like webmails would be a good target to look at.

42:37.230 --> 42:38.970
And I looked at Roundcube.

42:38.970 --> 42:42.130
And I saw that, like, it was a good target.

42:42.130 --> 42:43.310
So , yeah, that's why.

42:43.310 --> 42:43.810
Oh, cool.

42:43.810 --> 42:44.590
Pure intuition.

42:44.590 --> 42:45.170
That's amazing.

42:45.170 --> 42:45.650
Thanks.

42:52.930 --> 42:55.050
Well, thank you for listening to my talk.

