[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:11.540] Well, thank you for coming to my talk, because I know it's the last talk for today, [00:11.540 --> 00:14.340] so I guess everybody's pretty tired of seeing talks, [00:14.340 --> 00:16.540] but hopefully it will be interesting. [00:17.100 --> 00:20.820] So, the bug is a bug that I found by auditing PHP, [00:20.820 --> 00:21.900] but it's not on PHP. [00:21.900 --> 00:23.940] It's a bug in the glibc, [00:23.940 --> 00:25.560] and it targets iconf, [00:25.560 --> 00:29.160] which is the character set conversion library of the glibc. [00:29.740 --> 00:33.660] And I actually was able to use the bug to compromise the PHP engine [00:33.660 --> 00:36.200] in two different ways remotely, [00:36.200 --> 00:38.920] 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:46.160] It was by complete accident, by complete chance, [00:46.160 --> 00:51.760] but the way I will explain it to you will serve as an introduction to the next section, [00:51.760 --> 00:54.800] 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:09.580] And, well, after this, hopefully we'll have time for a conclusion [01:09.580 --> 01:12.160] and maybe a little bit of a bonus. [01:13.200 --> 01:15.720] So, regarding the discovery, [01:15.720 --> 01:18.040] well, to understand how I found the bug, [01:18.040 --> 01:22.480] we need to understand what you can do when you have a valid primitive in PHP. [01:22.480 --> 01:26.280] So, code such as this one, [01:26.280 --> 01:29.980] an fopen followed by an fread, [01:29.980 --> 01:33.140] like any valid primitive in PHP, what can you do with it? [01:33.200 --> 01:35.020] Well, obviously you can read a file, [01:35.020 --> 01:36.320] you can read ETC passwords, [01:36.920 --> 01:41.740] but you can also force PHP into reading HTTP resource, that would work, [01:41.740 --> 01:44.420] and FTP resource, that would work as well. [01:44.600 --> 01:48.960] And there are also other protocols that you can use, [01:48.960 --> 01:52.800] 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:00.100] So, for those who don't know, a file archive, [02:00.100 --> 02:02.320] it's like a jar for Java, [02:02.320 --> 02:05.180] so it's a collection of source files, resources, [02:05.180 --> 02:08.540] and also in PHP, it contains serialized metadata. [02:08.560 --> 02:12.620] And when you access such a file using far-colon-slash-slash, [02:12.620 --> 02:14.560] the metadata will be deserialized. [02:14.740 --> 02:17.420] So, there was an attack for the longest time, [02:17.420 --> 02:20.940] that was that you could upload a file to a server, [02:20.940 --> 02:23.420] any file, an image, or anything, [02:23.420 --> 02:27.420] and then you would use your file with primitive to load it as a file, [02:27.420 --> 02:29.880] and that would give you arbitrary deserialization, [02:29.880 --> 02:32.160] which oftentimes meant RCE. [02:33.320 --> 02:37.800] Sadly, in PHP 8, they have disabled the deserialization of the metadata, [02:37.800 --> 02:40.220] because they actually don't use the metadata, [02:40.220 --> 02:41.660] so it was completely useless. [02:41.820 --> 02:44.620] And also, in older versions, [02:44.620 --> 02:48.940] big frameworks have been removing the far-colon-slash-slash protocol, [02:48.940 --> 02:50.260] because they don't use it, [02:50.260 --> 02:53.460] 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:07.520] And also, it's going to be harder to exploit, [03:07.520 --> 03:10.040] even not on PHP 8, this bug, [03:10.040 --> 03:13.340] because deserialization is going to get harder and harder to exploit [03:13.340 --> 03:14.480] as time goes by, [03:14.960 --> 03:17.760] 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:25.040] So, people are taking the interest into another PHP-specific protocol, [03:25.040 --> 03:26.580] 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:34.640] And what it does is that it will read a resource [03:34.640 --> 03:38.660] and apply some filters that you have chosen onto it. [03:39.620 --> 03:43.080] It will then return the resource as if it was the original file, [03:43.080 --> 03:44.680] but with filters applied onto it. [03:44.680 --> 03:46.200] So, a good example of this, [03:46.200 --> 03:48.100] and probably the simplest one that you can get, [03:48.100 --> 03:48.960] is this string. [03:48.960 --> 03:50.200] So, you have PHP filter, [03:50.200 --> 03:52.980] 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:01.360] So, what you get when you do a file get contents of this [04:01.360 --> 04:02.900] is file get... [04:02.900 --> 04:06.200] is etcpassword, sorry, but base64encoded. [04:06.480 --> 04:08.220] And the good thing about these filters [04:08.220 --> 04:10.180] is that you can stack as many as you want. [04:10.180 --> 04:12.960] So, instead of base64encoding once, [04:12.960 --> 04:14.080] I can do it twice, [04:14.080 --> 04:16.360] 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:25.300] You might be able to put your string in uppercase [04:25.300 --> 04:26.540] with string.upper, [04:26.540 --> 04:29.120] or put it in lowercase with string.lower, [04:29.120 --> 04:32.800] 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:39.620] And you can also use filters that are not documented, [04:39.620 --> 04:41.780] 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:46.560] And, well, you can also use the stuff for today, [04:46.560 --> 04:48.520] which is convert.iconv.x.y. [04:48.520 --> 04:51.880] And what it will do is that it will use the iconv library [04:51.880 --> 04:54.860] of the gdpc to convert your stream [04:54.860 --> 04:57.000] from charsetX to charsetY. [04:57.040 --> 04:58.940] Just to give you a quick example, [04:58.940 --> 05:00.320] say that you have etcpassword, [05:00.320 --> 05:04.320] you want to convert it from utf-8 to utf-16, [05:04.320 --> 05:07.520] you can use this PHP filter string, [05:07.520 --> 05:09.060] and it will do it automatically, [05:09.060 --> 05:11.800] 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:26.040] And the consensus in 2023 was that you could add, [05:26.040 --> 05:28.500] using filters, an arbitrary prefix to any file. [05:28.500 --> 05:31.400] So you could dump etcpassword with an arbitrary prefix [05:31.400 --> 05:35.260] just by picking the right filters to apply onto it. [05:35.520 --> 05:38.540] And myself, what I wanted to see was whether or not [05:38.540 --> 05:40.920] I could get an arbitrary suffix as well. [05:40.920 --> 05:44.200] So, an arbitrary prefix on some text, [05:44.200 --> 05:45.660] 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:50.480] Of course you have a far-read primitive, [05:50.480 --> 05:52.720] but then right afterwards you have a JSON decode. [05:52.720 --> 05:54.840] So if the file is not valid JSON, [05:54.840 --> 05:56.760] your far-read primitive is useless. [05:56.760 --> 05:58.420] You cannot read etcpassword. [05:58.420 --> 06:01.840] But if you can make your file look like JSON, [06:01.840 --> 06:04.120] using an arbitrary prefix and suffix, [06:04.120 --> 06:07.000] you can make the JSON decode work, [06:07.000 --> 06:10.020] and therefore you can make PHP think it's JSON [06:10.020 --> 06:13.000] 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:20.660] And I call the tool VrapVrap, [06:20.660 --> 06:23.180] and I've made a blog post about it. [06:23.180 --> 06:26.520] It's on our company's blog, so I'mbertenix.io, [06:26.520 --> 06:27.880] as you can see on the bottom. [06:28.220 --> 06:30.600] And, well, the important part about this [06:30.600 --> 06:33.580] is that at the beginning I didn't know much [06:33.580 --> 06:36.640] when I started this research about PHP filters. [06:36.680 --> 06:38.340] So I built a few scripts [06:38.340 --> 06:40.680] just to try and do some conversions. [06:40.680 --> 06:42.360] So maybe from charset A to B, [06:42.360 --> 06:44.240] then apply a basic C4D code, [06:44.240 --> 06:45.720] then from C to D. [06:45.720 --> 06:47.400] Like, lots of conversions just to see [06:47.400 --> 06:49.860] what I could get as a result. [06:49.860 --> 06:51.860] And I actually got a crash while doing this, [06:51.860 --> 06:53.440] so I was pretty surprised. [06:53.640 --> 06:56.620] And I've been working with PHP for, like, 20 years, [06:56.620 --> 06:59.380] so I kind of felt like it was due to PHP, you know, [06:59.380 --> 07:01.120] because it's always PHP. [07:01.140 --> 07:02.800] But this time it wasn't PHP. [07:02.940 --> 07:04.680] It was actually due to the glibc, [07:04.680 --> 07:08.420] so the base library for most Linux programs. [07:08.480 --> 07:11.040] Because when PHP does the character set conversion [07:11.040 --> 07:12.440] using convert.iconv, [07:12.440 --> 07:15.140] it will use the iconv library of the glibc. [07:15.260 --> 07:17.100] And what this library does [07:17.100 --> 07:20.260] is that you pick an input charset and an output charset, [07:20.260 --> 07:21.740] and then you feed it buffers, [07:21.740 --> 07:23.720] and it will convert from your input buffer [07:23.720 --> 07:24.940] to the output buffer, [07:24.940 --> 07:26.440] from one charset to another. [07:27.340 --> 07:30.000] The thing is, when you call the icon function, [07:30.000 --> 07:31.940] you give it an input buffer with some size [07:31.940 --> 07:34.200] and an output buffer with some other size. [07:34.200 --> 07:37.120] And obviously, it should guarantee [07:37.120 --> 07:39.380] that it will now read more bytes in the input buffer [07:39.380 --> 07:40.360] than it should, [07:40.360 --> 07:43.840] or write more bytes than it should in the output buffer. [07:43.840 --> 07:45.840] And generally, it does so pretty well, [07:45.840 --> 07:47.200] like, there's no overflow. [07:47.400 --> 07:49.300] But sometimes it fails to do so, [07:49.740 --> 07:52.580] and this is exactly why I got my crash, [07:52.580 --> 07:56.380] is that when you convert to some very obscure charset, [07:56.380 --> 08:00.000] which is called ISO 2022CNX, [08:00.000 --> 08:01.600] in some cases, [08:01.600 --> 08:03.900] it will not check the bounds of the output buffer [08:03.900 --> 08:06.540] before writing some escape sequences. [08:06.920 --> 08:09.820] And that means that, as a result, as an attacker, [08:09.820 --> 08:13.600] if you are able to convert some text to ISO 2022CNX, [08:13.600 --> 08:15.900] you will get an overflow of one to three bytes [08:15.900 --> 08:18.940] with the very specific values that I've written here. [08:18.940 --> 08:21.760] So your overflow could be with $H, [08:21.760 --> 08:23.700] or $I, or $J, [08:23.700 --> 08:26.540] any of these six values, but only these six values. [08:28.000 --> 08:31.040] To give you a quick idea of the characters that you can use [08:31.040 --> 08:32.060] to trigger the bug, [08:32.060 --> 08:34.900] those are the characters that I trigger the bug with. [08:35.120 --> 08:36.520] So those are Chinese characters, [08:36.520 --> 08:37.720] I have no idea what they mean, [08:37.720 --> 08:39.580] but they give you a buffer overflow. [08:41.720 --> 08:43.600] So the vulnerability that we have, [08:43.600 --> 08:45.000] it's already pretty bad, [08:45.000 --> 08:47.600] like, it's a one to three bytes overflow, [08:47.600 --> 08:49.280] and it's fixed byte values, [08:49.280 --> 08:50.540] like, you don't control the bytes. [08:50.600 --> 08:53.120] In addition to this, you have horrible conditions, [08:53.120 --> 08:55.400] like, you have to control the output charset, [08:55.400 --> 08:57.620] you have to set it to ISO 2022CNX, [08:57.620 --> 09:00.340] like, nobody heard about it in the room, I guess. [09:00.340 --> 09:02.240] So, yeah, you need to find an application [09:02.240 --> 09:04.420] 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:08.740] In addition to this, [09:08.740 --> 09:10.420] you need to control part of the input buffer [09:10.420 --> 09:13.200] so that you can put in the Chinese characters. [09:13.580 --> 09:15.360] And again, a third condition [09:15.360 --> 09:18.240] is that you need to have a suitable output buffer. [09:18.360 --> 09:20.400] If the output buffer is too small, [09:21.060 --> 09:23.880] sorry, too big compared to the input buffer, [09:23.880 --> 09:26.120] even though you may have an overflow in theory, [09:26.120 --> 09:27.900] you're not going to be able to reach the end [09:27.900 --> 09:29.900] of the bounds of the output buffer [09:29.900 --> 09:32.240] before you trigger your escape sequence, [09:32.240 --> 09:33.800] and therefore you will not overflow. [09:33.900 --> 09:36.620] And this is, like, what was the most annoying part [09:36.620 --> 09:38.600] in looking for targets for this bug, [09:38.600 --> 09:41.660] because I thought that I had a bug in libxml2, [09:41.660 --> 09:46.160] because you can send an encoding when you send your XML, [09:46.160 --> 09:48.600] but the output buffer, when it did the conversion, [09:48.600 --> 09:50.780] was way too big, and therefore I couldn't exploit. [09:50.780 --> 09:53.000] And that's only one of many. [09:53.000 --> 09:57.940] And so, after probably weeks of looking for targets, [09:57.940 --> 10:01.620] I got super frustrated, and I went back to PHP filters, [10:01.620 --> 10:04.120] because that's the only thing that I could make crash, [10:04.120 --> 10:06.380] and that's actually the only thing that crashed [10:06.380 --> 10:08.500] without even me trying for it to crash, [10:08.500 --> 10:11.900] so I was like, okay, that's probably my best bet [10:11.900 --> 10:15.000] to exploit this bug, and I focused onto it. [10:15.020 --> 10:19.240] And the idea was to convert file-read primitives in PHP, [10:19.240 --> 10:22.000] any file-read primitive, into remote code execution, [10:22.000 --> 10:24.020] straight up using this binary bug. [10:24.760 --> 10:28.160] So to explain to you how we might be able to do this, [10:28.160 --> 10:30.880] we need to think about how PHP does allocations [10:30.880 --> 10:32.040] and deallocations. [10:32.740 --> 10:34.880] For this, we need to understand PHP's heap, [10:34.880 --> 10:36.920] but don't worry, that's probably the simplest heap [10:36.920 --> 10:38.380] that you're ever going to see. [10:38.520 --> 10:43.300] So it's 512 pages of 0x1000 bytes each, [10:43.300 --> 10:47.920] so 4096 bytes, and each page contains chunks of some size, [10:47.920 --> 10:50.000] so it might be a chunk... [10:50.000 --> 10:52.500] In page 10, you may have chunks of size 0x100, [10:52.500 --> 10:55.260] in page 11, you may have chunks of size 0x30, [10:55.260 --> 10:56.260] et cetera, et cetera. [10:56.940 --> 11:00.120] Then when you free a chunk, it will go into the free list [11:00.120 --> 11:01.520] for this chunk size. [11:01.700 --> 11:03.880] So if I free a chunk of size 0x100, [11:03.880 --> 11:06.180] it will go into the free list for size 0x100. [11:06.180 --> 11:08.080] If I free a chunk of size 0x10, [11:08.080 --> 11:12.120] it will go into the free list for chunks of this size as well. [11:12.340 --> 11:14.500] And the free lists are singly linked lists, [11:14.500 --> 11:16.360] they are lasting first out, [11:16.360 --> 11:18.340] so that means that if I free a chunk of some size [11:18.340 --> 11:20.080] and allocate a new one afterwards, [11:20.080 --> 11:22.940] it will be allocated exactly at the same spot. [11:23.140 --> 11:27.580] And it's very similar for those who do PTMalloc exploitation [11:28.060 --> 11:30.880] to the libctcache, but completely unlimited [11:30.880 --> 11:33.160] and without any protections. [11:34.360 --> 11:37.100] So, just to give you an idea of what we can do [11:37.100 --> 11:40.060] with 1 to 3 bytes overflow in the PHP heap, [11:40.060 --> 11:41.600] let's have a look at this example. [11:41.600 --> 11:43.820] So, on the left you have the heap, [11:44.200 --> 11:46.520] with 512 pages, [11:46.520 --> 11:48.900] and in page 5, in this example, [11:48.900 --> 11:50.540] I have chunks of size 0x400. [11:50.700 --> 11:53.020] So, in page 5, I have 4 chunks. [11:53.340 --> 11:55.960] In my example, I have chunk 1 that is allocated, [11:55.960 --> 11:57.360] chunk 3 that is allocated, [11:57.360 --> 11:59.180] chunk 2 and 4 that are not. [11:59.340 --> 12:01.160] So, in chunk 2 and chunk 4, [12:01.160 --> 12:05.020] since they are in the free list for chunks of size 0x400, [12:05.020 --> 12:07.880] that means that they contain a pointer to the next free chunk. [12:08.260 --> 12:13.220] So, if I was to overflow even with 1 byte from chunk 1, [12:13.220 --> 12:15.500] I would be able to at least partially modify [12:15.500 --> 12:19.220] the pointer that is contained in chunk 2. [12:19.420 --> 12:22.720] And that would be a way to alter some free list pointer, [12:22.720 --> 12:26.020] and in the end, probably be a good starting point [12:26.020 --> 12:27.680] for an exploitation. [12:29.720 --> 12:32.020] So, the PHP heap, [12:32.020 --> 12:33.900] it looks very, very easy to exploit, right? [12:33.900 --> 12:36.020] There's no metadata, it's page-based, [12:36.020 --> 12:37.760] there's absolutely no protections, [12:37.760 --> 12:39.300] it's really easy to understand. [12:39.300 --> 12:41.720] So, why is it that there are not that many [12:41.720 --> 12:43.240] remote binary exploits? [12:43.240 --> 12:45.860] Well, to me, the main problem is that [12:45.860 --> 12:49.640] the PHP has the one heap for one request policy. [12:49.800 --> 12:51.960] So, if you send an HTTP request, [12:51.960 --> 12:53.840] PHP will create the heap, [12:53.840 --> 12:55.960] allocate your HTTP parameters into it, [12:55.960 --> 12:57.120] then run the script, [12:57.120 --> 12:58.700] so that means also compiling the script, [12:58.700 --> 13:00.280] so it will do like thousands of allocations [13:00.280 --> 13:01.360] and deallocations, [13:01.360 --> 13:03.440] and then it will completely destroy the heap. [13:03.680 --> 13:06.060] So, now imagine that you have a use-after-free [13:06.060 --> 13:08.260] on some VPN instance, for instance. [13:08.840 --> 13:10.140] What you would do to exploit [13:10.140 --> 13:13.000] would be to send a few requests to set up your heap, [13:13.000 --> 13:15.920] then one more request to trigger your bug, [13:15.920 --> 13:17.480] trigger your UAF, [13:17.480 --> 13:19.520] and then afterwards, send a few more requests [13:19.520 --> 13:20.660] to use your bug. [13:20.960 --> 13:23.160] But here, you cannot do this. [13:23.160 --> 13:25.720] You have to find a way to trigger a bug remotely, [13:25.720 --> 13:27.300] so you need a bug in the PHP engine, [13:27.300 --> 13:29.040] you need an application that lets you trigger [13:29.040 --> 13:30.120] the bug remotely, [13:30.120 --> 13:32.000] but also you need to use the bug [13:32.000 --> 13:33.640] as the request is running, [13:33.640 --> 13:35.460] before the heap is gone. [13:35.460 --> 13:36.480] And this is why it's hard, [13:36.480 --> 13:38.260] because in between the triggering of the bug [13:38.800 --> 13:40.140] and the usage of the bug, [13:40.140 --> 13:42.140] you're not going to have any way to interact [13:42.140 --> 13:43.900] with the HTTP request. [13:43.900 --> 13:46.280] Like, you've sent every HTTP parameter, [13:46.280 --> 13:47.000] and it's done. [13:47.000 --> 13:48.340] Like, it's a black box now. [13:48.720 --> 13:50.200] And this is, to me, [13:50.200 --> 13:51.820] why it's really, really hard to exploit [13:51.820 --> 13:54.240] on PHP remotely. [13:59.640 --> 14:00.480] Okay. [14:00.500 --> 14:01.800] So, in addition to this, [14:01.800 --> 14:03.340] there's one thing that we need to understand, [14:03.340 --> 14:06.440] is how PHP processes PHP filter strings. [14:06.620 --> 14:08.020] And to do so, [14:08.020 --> 14:10.900] you will have a two-step process, [14:10.900 --> 14:13.260] where you will first read the resource as a stream, [14:13.260 --> 14:16.180] and then you will apply filters, [14:17.060 --> 14:19.180] So, fetching the stream, [14:19.180 --> 14:21.140] it will use a very standard structure, [14:21.140 --> 14:22.340] which is called a bucket brigade, [14:23.640 --> 14:25.480] and each part of the stream [14:25.480 --> 14:28.200] is represented as one bucket of a few bytes. [14:28.200 --> 14:30.400] So, for instance, if you read the password, [14:30.400 --> 14:33.580] the first bucket may contain five bytes of its password, [14:33.580 --> 14:36.020] then the second bucket will contain the next few bytes, [14:36.020 --> 14:37.560] for instance, ten bytes, [14:37.560 --> 14:39.520] and the bucket three will contain a few more bytes [14:39.520 --> 14:40.520] 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:47.000] A list of buffers that are linked together, [14:47.000 --> 14:48.960] and that may have different sizes. [14:51.560 --> 14:53.500] Once you have your stream, [14:53.500 --> 14:55.320] you can start applying filters onto them. [14:55.320 --> 14:56.700] So you take the first filter, [14:56.700 --> 14:58.060] and you take the first bucket, [14:58.060 --> 14:59.820] and you apply the filter onto it. [15:00.240 --> 15:02.900] So, for instance, if it's base64 decode, [15:02.900 --> 15:04.220] PHP will take the first bucket, [15:04.220 --> 15:07.040] and decode the data that is into it. [15:07.040 --> 15:09.580] Then it will be able to process bucket two, [15:09.580 --> 15:10.280] which it will do, [15:10.280 --> 15:11.280] and then bucket three. [15:11.360 --> 15:13.860] And at the end, you get a new bucket brigade, [15:13.860 --> 15:15.900] on which the first filter has been applied. [15:16.040 --> 15:16.920] So what you can do, [15:16.920 --> 15:19.240] is start working on the second filter, [15:19.240 --> 15:20.240] then on the third one, [15:20.240 --> 15:21.140] et cetera, et cetera, [15:21.140 --> 15:22.620] up until the end. [15:22.660 --> 15:25.020] And now you have a stream that has been [15:25.020 --> 15:26.840] completely processed with filters, [15:26.840 --> 15:28.880] and your PHP filter string has been [15:28.880 --> 15:31.060] completely, perfectly handled. [15:32.500 --> 15:35.000] So, if we look back at everything now, [15:35.000 --> 15:36.800] it looks pretty easy to exploit this far read, [15:36.800 --> 15:38.620] because we have a far read primitive, [15:38.620 --> 15:39.660] and we want to get our C, [15:39.660 --> 15:41.360] but since we have a far read primitive, [15:41.360 --> 15:43.320] well, first we can read the binaries. [15:43.320 --> 15:45.420] So, if I want to have the address [15:45.420 --> 15:47.600] of some symbol, for instance, [15:47.600 --> 15:48.240] in the libc, [15:48.240 --> 15:49.300] I just download the libc [15:49.300 --> 15:50.620] using the far read primitive. [15:50.640 --> 15:51.960] If I want to check if the libc [15:51.960 --> 15:53.180] is patched against the bug, [15:53.180 --> 15:54.300] I can also download the libc [15:54.300 --> 15:55.960] and check the code myself, you know? [15:56.400 --> 15:57.440] So that means that [15:57.440 --> 15:59.180] you can find gadgets easily. [15:59.220 --> 16:00.520] In addition to this, [16:00.520 --> 16:02.120] ASLR and Pi are not a problem, [16:02.120 --> 16:03.880] because I can just read Proxet Maps. [16:04.180 --> 16:06.320] So, any address that I want [16:06.320 --> 16:08.920] is going to be probably in Proxet Maps, [16:08.920 --> 16:11.320] so ASLR and Pi, it's gone. [16:11.700 --> 16:13.060] A corollary to this is that [16:13.060 --> 16:15.100] the address of the PHP heap [16:15.100 --> 16:17.400] is really easy to find in Proxet Maps, [16:17.400 --> 16:19.320] so I have the address of the PHP heap, [16:19.320 --> 16:20.780] which is going to be useful later. [16:21.020 --> 16:22.240] In addition to this, [16:22.240 --> 16:23.240] it kind of feels like [16:23.240 --> 16:24.920] that I can do arbitrary allocations [16:24.920 --> 16:25.680] on the allocations [16:26.240 --> 16:28.940] simply using the bucket brigade technology, [16:28.940 --> 16:31.420] so the way that the stream is represented. [16:31.420 --> 16:33.240] If I can create as many buckets as I want [16:33.240 --> 16:34.820] with the sizes that I want, [16:34.820 --> 16:36.640] I will probably be able to build [16:36.640 --> 16:37.840] an exploit that works. [16:39.420 --> 16:40.800] But on the other side, [16:40.800 --> 16:42.080] I kind of want to have an exploit [16:42.080 --> 16:45.240] that works well even in an unknown environment, [16:45.240 --> 16:46.600] so I don't want to build a new exploit [16:46.600 --> 16:47.860] when I'm targeting WordPress [16:47.860 --> 16:50.300] or some very specific version of PHP [16:50.300 --> 16:51.860] or some Magento, [16:51.860 --> 16:54.160] which has a lot of traffic on the website. [16:54.180 --> 16:56.600] I want an exploit that just straight up works, [16:56.600 --> 16:57.920] like basically a web exploit, [16:57.920 --> 16:59.040] 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:06.180] Well, the first problem that we face [17:06.180 --> 17:07.260] is that even though there is [17:07.260 --> 17:09.540] the bucket brigade technology in PHP, [17:09.540 --> 17:11.160] you will always get one bucket [17:11.160 --> 17:12.180] when you read a stream. [17:12.180 --> 17:14.220] So, if PHP reads the ETC password, [17:14.220 --> 17:16.280] it will give you one bucket with the whole file. [17:16.280 --> 17:17.880] If it reads an HTTP resource, [17:17.880 --> 17:19.860] it will give you one bucket [17:19.860 --> 17:22.620] 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:26.960] And one bucket, [17:26.960 --> 17:29.040] it's completely impractical for exploitation, [17:29.040 --> 17:30.500] because we cannot spray the heap, [17:30.500 --> 17:31.680] we cannot pad the heap, [17:31.680 --> 17:33.720] and we can't even use an altered freelist. [17:33.720 --> 17:36.020] Like, if we use one bucket to trigger the overflow, [17:36.020 --> 17:37.060] we have an altered freelist, [17:37.060 --> 17:38.600] but then we need at least two more locations [17:38.600 --> 17:40.840] to be able to use this altered freelist, [17:40.840 --> 17:41.860] and we cannot do this [17:41.860 --> 17:43.860] because we have used our single bucket, [17:43.860 --> 17:45.820] so we're not going to be able to exploit. [17:47.180 --> 17:48.060] Luckily for us, [17:48.060 --> 17:50.040] there's a single filter that allows us [17:50.040 --> 17:51.960] to create as many buckets as we want, [17:51.960 --> 17:53.700] and this filter is zlib-inflate. [17:53.700 --> 17:56.960] What it does is that it will read your stream [17:56.960 --> 17:58.380] and decompress it, [17:58.380 --> 18:00.500] as with the zlib algorithm. [18:00.580 --> 18:01.600] And the way it works is that [18:01.600 --> 18:04.120] it will allocate eight pages, [18:04.260 --> 18:05.260] a buffer of eight pages, [18:05.260 --> 18:06.980] and then decompress into it. [18:07.000 --> 18:08.220] If it's not big enough, [18:08.220 --> 18:09.740] it will create eight more pages [18:09.740 --> 18:11.420] and decompress the rest into it. [18:11.420 --> 18:13.040] If it's still not big enough, [18:13.040 --> 18:14.440] it will create a third bucket, [18:14.440 --> 18:15.780] et cetera, et cetera, et cetera. [18:15.920 --> 18:18.220] So this filter, it gives us a way [18:18.220 --> 18:21.140] to go from one bucket to as many as we want. [18:21.140 --> 18:22.340] But the problem that we have [18:22.340 --> 18:25.400] is that every bucket has a size of eight pages, [18:25.400 --> 18:27.560] which is way too big to exploit [18:27.560 --> 18:32.040] because it takes too much space, [18:32.040 --> 18:33.880] and also it's not going to be allocated [18:33.880 --> 18:36.440] the same way as small chunks. [18:36.800 --> 18:38.360] So we need a way to change the size [18:38.360 --> 18:40.800] of each of these buckets. [18:41.100 --> 18:43.660] And a way to do this is to use another filter, [18:43.660 --> 18:45.280] which is called dchunk. [18:45.300 --> 18:46.540] It's undocumented, [18:46.540 --> 18:48.860] but it's been used by many hackers before, [18:48.860 --> 18:50.320] so it's pretty well known. [18:50.320 --> 18:52.100] And the idea is that it will take [18:52.100 --> 18:55.560] an HTTP chunked encoded string [18:55.560 --> 18:57.780] and simply decode it. [18:57.780 --> 18:59.040] So for those who don't know, [18:59.040 --> 19:01.500] the HTTP chunked encoding is very simple. [19:01.500 --> 19:05.380] It consists of sending a size as ASCII X [19:05.380 --> 19:07.100] and then sending the data, [19:07.100 --> 19:08.360] and then a new size as ASCII X [19:08.360 --> 19:10.120] and then sending the data, etc. [19:10.160 --> 19:13.200] So the dchunk filter, what it does is that [19:13.200 --> 19:15.320] it will read a size as ASCII X, [19:15.320 --> 19:17.420] then read the amount of bytes [19:17.420 --> 19:18.840] that correspond to this size. [19:18.840 --> 19:20.460] So for instance, if it reads 8, [19:20.460 --> 19:21.440] as in the example, [19:21.440 --> 19:22.760] it will read 8 bytes. [19:22.760 --> 19:24.220] Then it will read 11, [19:24.220 --> 19:26.420] and since it's ASCII X, it's 17, [19:26.420 --> 19:27.880] so it will read 17 bytes. [19:27.880 --> 19:29.420] And then it will read D, [19:29.420 --> 19:30.760] read the D bytes, [19:30.760 --> 19:32.140] and then it will read 0. [19:32.140 --> 19:34.540] And 0, it doesn't indicate an empty chunk, [19:34.540 --> 19:37.340] it indicates the end of the dchunking. [19:37.340 --> 19:38.740] So when it receives 0, [19:38.740 --> 19:40.400] it stops processing the data. [19:41.720 --> 19:44.480] So, now you probably have an idea [19:44.480 --> 19:46.820] of how we can resize our chunks. [19:47.280 --> 19:48.180] We take bucket 1, [19:48.180 --> 19:50.420] if we want it to have a size of 148, [19:50.420 --> 19:54.240] we write 148, then 148 bytes, and then 0. [19:54.320 --> 19:56.540] If we want bucket 2 to have a size of 100, [19:56.540 --> 19:57.600] 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:05.080] And then when we apply dchunk onto this, [20:05.080 --> 20:07.340] we expect it to resize every single bucket. [20:07.440 --> 20:10.560] But you may notice that this won't work, [20:10.560 --> 20:13.980] because even though each bucket is different, [20:13.980 --> 20:16.480] they are all processed as one stream. [20:16.480 --> 20:17.780] So if in bucket 1, [20:17.780 --> 20:20.240] you indicate that it's the end of the stream, [20:21.060 --> 20:23.580] with the 0 at the very bottom, [20:23.580 --> 20:24.980] it will not process bucket 2, [20:24.980 --> 20:26.160] it will not process bucket 3, [20:26.160 --> 20:28.100] it will not process any bucket that comes after. [20:28.100 --> 20:30.880] So you go back to a single bucket [20:30.880 --> 20:31.820] with an arbitrary size, [20:31.820 --> 20:33.140] 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:40.300] And the way to improve this is to pad the size [20:40.300 --> 20:41.860] with thousands of 0s, [20:41.860 --> 20:44.000] such as what we can see here. [20:44.000 --> 20:45.700] The idea is that in each bucket, [20:45.700 --> 20:48.280] we will have one chunk size and one chunk data. [20:48.280 --> 20:49.860] And that's it. Nothing else. [20:49.860 --> 20:52.640] So now if the dchunk filter is applied, [20:52.640 --> 20:55.340] we will have bucket 1 that gets processed, [20:55.340 --> 20:56.340] it will read the size, [20:56.340 --> 20:57.560] read thousands of 0s, [20:57.560 --> 20:58.700] then the real size, [20:58.700 --> 20:59.500] then the data, [20:59.500 --> 21:00.940] and then it will go onto bucket 2 [21:00.940 --> 21:02.400] and do exactly the same. [21:02.400 --> 21:03.460] It will read the size, [21:03.460 --> 21:04.280] read the data, [21:04.280 --> 21:05.080] then bucket 3, [21:05.080 --> 21:05.700] read the size, [21:05.700 --> 21:06.440] read the data. [21:06.460 --> 21:09.420] And you can see that bucket 1 can be resized [21:09.420 --> 21:10.660] to the proper size, [21:10.660 --> 21:11.380] bucket 2 as well, [21:11.380 --> 21:12.180] bucket 3 as well, [21:12.180 --> 21:13.180] and bucket 4 as well. [21:13.180 --> 21:15.200] And now we have a way to create as many buckets [21:15.200 --> 21:16.000] as we want, [21:16.000 --> 21:17.620] and change the size. [21:17.620 --> 21:19.300] And this is only with two filters, [21:19.300 --> 21:21.020] so it's pretty promising. [21:23.060 --> 21:24.200] But that's not enough, [21:24.200 --> 21:26.520] because we can only send one request, [21:26.520 --> 21:28.600] so we need a whole exploit algorithm [21:28.980 --> 21:30.640] in a single request, [21:31.160 --> 21:33.380] and with a single PHP filter string. [21:33.380 --> 21:34.180] So how do we do this? [21:34.180 --> 21:38.040] Well, instead of only being able to allocate once, [21:38.040 --> 21:39.940] we kind of want to be able to allocate several times [21:39.940 --> 21:42.760] to represent the different steps of the exploit. [21:42.760 --> 21:44.480] In step 1 I want to allocate a few chunks, [21:44.480 --> 21:46.280] in step 2 I want to allocate a few more chunks, [21:46.280 --> 21:47.240] 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:50.440] But to do this, [21:50.440 --> 21:52.660] what we can do is think about the PHP heap. [21:52.660 --> 21:53.740] It's page-based. [21:53.740 --> 21:56.380] So if I focus my exploitation on one chunk size, [21:56.520 --> 21:57.580] a single chunk size, [21:57.580 --> 22:01.220] that means that chunks that are bigger or smaller than this chunk size, [22:01.220 --> 22:02.660] 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:08.180] If a chunk is of size 0x200, for instance, [22:08.180 --> 22:10.800] and I'm focusing on chunks of size 0x100, [22:10.800 --> 22:13.020] the chunk will not affect my exploitation. [22:13.240 --> 22:14.800] So at this point, [22:14.800 --> 22:16.960] if I focus on some very specific chunk size, [22:16.960 --> 22:18.560] such as 0x100, [22:18.560 --> 22:19.900] chunks that are bigger or smaller, [22:19.900 --> 22:20.740] they do not matter. [22:20.740 --> 22:22.320] It's as if they weren't there. [22:22.320 --> 22:24.220] So the only thing that I need to be able to do [22:24.220 --> 22:26.000] is to be able to resize buckets [22:26.000 --> 22:27.800] as the exploitation goes on. [22:27.800 --> 22:29.180] And to be able to do this, [22:29.180 --> 22:34.140] I built buckets that are Russian dolls of dchunk algorithm. [22:34.220 --> 22:38.160] So the idea is that every time I call dchunk on some buckets, [22:38.160 --> 22:40.140] its size will change. [22:40.140 --> 22:44.080] And that means that I can make buckets appear at some point in the exploitation, [22:44.080 --> 22:47.740] 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:52.820] And at this point, [22:52.820 --> 22:55.260] I'm really, really close to getting an exploit working. [22:56.720 --> 23:00.400] And the final idea here is that we have a one-byte overflow, [23:00.400 --> 23:01.760] using the icon bug. [23:01.860 --> 23:07.280] And we want to use it to alter some freelist [23:07.280 --> 23:09.360] and get complete control over it. [23:09.400 --> 23:11.580] And the way that we can do this is a six-step process, [23:11.580 --> 23:12.700] which is pretty simple, [23:12.700 --> 23:14.600] 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:31.780] We get in step three, and the only difference with step one [23:31.780 --> 23:33.940] 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:40.420] Now we allocate in C a buffer, which contains at offset 48, [23:40.420 --> 23:45.360] 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 --> 23:55.840] And we have exactly the same E-playout and freelist as in step one, [23:55.840 --> 24:00.280] 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:19.080] So with three more allocations, we will allocate B, C plus 48, [24:19.080 --> 24:21.320] and then we will allocate our arbitrary pointer. [24:21.640 --> 24:25.200] And now we are able to have a write-what-where, [24:25.200 --> 24:28.520] 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:37.840] Well, as I've said before, the PHP heap, it's really easy to find it in Proc Setmaps, [24:37.840 --> 24:39.200] so we have it for us. [24:39.300 --> 24:42.220] And also, at the very top of the PHP heap, [24:42.220 --> 24:46.440] there is a send-mmheap structure that contains a few things, [24:46.440 --> 24:50.840] and also a few alternative heap function pointers. [24:50.920 --> 24:53.540] So for those, again, we do heap exploitation on ptMalloc, [24:53.540 --> 24:56.740] it would be the same as free-hook, malloc-hook, and realloc-hook. [24:56.760 --> 25:00.760] Those are pointers that, if you set them to something else that null, [25:00.760 --> 25:03.500] they're going to be called inside of the original function. [25:03.700 --> 25:07.320] And so, since it's basically the same as the glibc, I can use the glibc technique, [25:07.320 --> 25:10.000] and I can override free with system. [25:10.000 --> 25:12.740] So now, when PHP wants to free a chunk, it will call free, [25:12.740 --> 25:14.480] 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:28.200] We have an exploit that works against any target, [25:28.200 --> 25:35.720] 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:42.860] And also, it works against any PHP application, because we can pad the heap, [25:42.860 --> 25:48.240] 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:52.580] In addition to this, the exploit is completely deterministic, [25:52.580 --> 25:56.540] 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:02.800] And the final good thing about the exploit is that the payload is really small, [26:02.800 --> 26:06.840] because the first gadget that we use, the first filter that we use, is a Zlib inflate, [26:06.840 --> 26:08.220] 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:20.160] The whole algorithm of the exploit is in one PHP filter string, [26:20.160 --> 26:23.760] 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:37.240] I'm going to show you a demo that actually involves Cosmic Sting, [26:37.240 --> 26:39.940] 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:51.800] And using these files that you've read, you would be able to craft API keys, [26:51.800 --> 26:57.300] 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:04.000] But with the bug that we have now, we get RCE easily because we have an X6E, [27:04.000 --> 27:10.380] 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:32.220] You have the Magento website on the left, and then you have an exploit on the right, [27:32.220 --> 27:36.220] 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:10.160] There we go. 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:35.500] Okay, so now that we have exploited PHP filters, we want to do a completely different exploitation, [28:35.500 --> 28:41.980] because PHP filters are not the only way in which you can reach ICONF using PHP. [28:41.980 --> 28:46.940] There's another way that is way simpler. It is when PHP calls ICONF directly. [28:47.540 --> 28:52.660] And so, there's a PHP function that does this. 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:09.260] So, here we don't have a five-read primitive, [29:09.260 --> 29:13.640] so we cannot just read proxmaps or read the libc or anything. [29:13.640 --> 29:16.920] We need to go, I would say, old-school exploitation, [29:16.920 --> 29:20.040] where we need an infoleak to get some pointers, [29:20.040 --> 29:22.640] and then we can start going with the exploitation. [29:22.920 --> 29:29.120] And in PHP, generally, when you want to have an infoleak, [29:29.120 --> 29:33.760] 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:47.280] So, if you are able to change the length of a string, and then it gets displayed, [29:47.280 --> 29:51.700] well, you are able to make PHP display more bytes than it should, [29:51.700 --> 29:56.280] 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:07.160] So, if we try to overflow into a ZenString, we will just overwrite the ref count, [30:07.600 --> 30:10.840] with values that we don't even control, so it's pretty useless. [30:11.940 --> 30:17.760] So, what we need to do is kind of the same as before, where we alter some free lists, [30:17.760 --> 30:21.980] and if we manage to alter some free lists, we are able to make two chunks overlap. [30:22.040 --> 30:27.240] And if we have two chunks that overlap, we can use the first one to store a string that is bound to be displayed, [30:27.240 --> 30:31.060] 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:46.560] Beforehand, we could implement an algorithm using PHP filters, as I've shown, [30:46.560 --> 30:50.780] but here we only have a single call to iconf, and that's it. 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:04.080] And to explain this, a very simple example. 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:43.620] If you have a line such as this one, you can create as many chunks as you want, [31:43.620 --> 31:47.580] because it will take your string and split it along a known separator. [31:47.840 --> 31:51.300] And that will give you a way to use the bug right after it's been triggered, [31:51.300 --> 31:54.080] 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:11.020] And to give you an idea of how it can be implemented in the real world, [32:11.020 --> 32:15.720] 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:35.340] And so, when you're logged into Roundcube and you want to send an email, [32:35.340 --> 32:37.880] 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:56.140] And every parameter that you send, so the underscore 2, underscore cc, and underscore bcc parameter, [32:56.140 --> 32:59.020] 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:17.820] If one of the emails is not valid, you will get an error message that says, [33:17.820 --> 33:21.000] 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:51.620] And then we use bcc to allocate a few strings, so that we have a string that gets allocated on top of invalid email, [33:51.620 --> 33:54.100] and this will alter its size. [33:54.160 --> 33:58.740] And therefore, the error, when it gets displayed, will display more bytes than it should, [33:58.740 --> 34:01.800] 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:23.940] So, you have a memory leak, but you don't really have any bytes of interest, [34:23.940 --> 34:28.780] 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:54.060] And to do that, we just have to look at the framework and go a little bit deeper, look at many, many functions, [34:54.060 --> 34:58.560] up until we find a variable that gets displayed without being modified. [34:58.560 --> 35:04.280] Sadly for us, when it gets displayed without being modified, [35:04.280 --> 35:11.300] 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:20.060] And, well, it's honestly pretty much hell to make this work, [35:20.060 --> 35:24.860] but you have to think that every line that you can use, like every line in the code, [35:24.860 --> 35:28.380] 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:48.180] Once you have an address of some value in the PHP heap, [35:48.180 --> 35:50.020] you can just reproduce the same exploit, [35:50.020 --> 35:55.840] but this time, use your freelist corruption to allocate at an arbitrary position. [35:55.960 --> 35:58.460] And that means that you can do data-only attacks, [35:58.460 --> 36:01.600] where you overwrite some value that is in your session, for instance. [36:01.600 --> 36:04.820] Maybe you want to overwrite your roles and set them to admin, [36:04.820 --> 36:08.000] or maybe you want to overwrite serialized data before it's deserialized, [36:08.000 --> 36:10.400] so that you have an arbitrary and serialized in PHP. [36:10.580 --> 36:15.180] Or, you can also go the standard way, where you go for a binary attack, [36:15.180 --> 36:17.020] 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:22.840] But I think the data-only attack is more elegant, [36:22.840 --> 36:25.440] 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:29.920] But, like all of this that I'm saying, [36:29.920 --> 36:32.160] it's actually only the visible part of the iceberg. [36:32.160 --> 36:34.220] It's really, really hard to pull off, [36:34.220 --> 36:36.500] because first, you need a proper heap setup. [36:36.500 --> 36:38.800] I haven't covered this part, so it's really hard to do, [36:38.800 --> 36:42.980] because the only thing that you can use to do your heap setup is HTTP parameters, [36:42.980 --> 36:46.120] 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 --> 36:57.900] If you have a method that is called, and then it exits the method, [36:57.900 --> 37:00.600] 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:08.560] Your attack to the server that you're targeting, [37:08.560 --> 37:11.660] 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:18.300] If I changed the language to English, it wouldn't work, [37:18.300 --> 37:22.580] because the strings that get allocated with error messages, they wouldn't be the same, [37:22.580 --> 37:24.580] 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:49.750] Oh, I'm paused. 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:24.580] Which one of these? This one? Nope. [38:25.820 --> 38:26.780] Nope. [38:27.980 --> 38:28.940] Yeah. [38:29.300 --> 38:33.940] Okay. 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:03.440] If you have a favorite primitive, you have RCE now. 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:45.380] They were really fast. 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:41.120] So we cannot read FoxFMaps. 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.