start	end	text
1780	2580	Hi.
3980	4740	Yeah.
5240	6240	Thank you.
8040	11540	Well, thank you for coming to my talk, because I know it's the last talk for today,
11540	14340	so I guess everybody's pretty tired of seeing talks,
14340	16540	but hopefully it will be interesting.
17100	20820	So, the bug is a bug that I found by auditing PHP,
20820	21900	but it's not on PHP.
21900	23940	It's a bug in the glibc,
23940	25560	and it targets iconf,
25560	29160	which is the character set conversion library of the glibc.
29740	33660	And I actually was able to use the bug to compromise the PHP engine
33660	36200	in two different ways remotely,
36200	38920	and this is what I'm going to talk about today.
40040	43580	So, first I will describe how I found the bug.
43580	46160	It was by complete accident, by complete chance,
46160	51760	but the way I will explain it to you will serve as an introduction to the next section,
51760	54800	which is going to be about attacking PHP filters.
54800	58480	If you don't know what a PHP filter is, don't worry, I will explain what it is.
58480	62220	And also, afterwards, I will attack a different way.
62220	65860	I will attack PHP by targeting direct calls to iconf.
66120	69580	And, well, after this, hopefully we'll have time for a conclusion
69580	72160	and maybe a little bit of a bonus.
73200	75720	So, regarding the discovery,
75720	78040	well, to understand how I found the bug,
78040	82480	we need to understand what you can do when you have a valid primitive in PHP.
82480	86280	So, code such as this one,
86280	89980	an fopen followed by an fread,
89980	93140	like any valid primitive in PHP, what can you do with it?
93200	95020	Well, obviously you can read a file,
95020	96320	you can read ETC passwords,
96920	101740	but you can also force PHP into reading HTTP resource, that would work,
101740	104420	and FTP resource, that would work as well.
104600	108960	And there are also other protocols that you can use,
108960	112800	which are PHP-specific, such as far-colon-slash-slash.
112800	118320	It will let you read the contents of a file archive.
118320	120100	So, for those who don't know, a file archive,
120100	122320	it's like a jar for Java,
122320	125180	so it's a collection of source files, resources,
125180	128540	and also in PHP, it contains serialized metadata.
128560	132620	And when you access such a file using far-colon-slash-slash,
132620	134560	the metadata will be deserialized.
134740	137420	So, there was an attack for the longest time,
137420	140940	that was that you could upload a file to a server,
140940	143420	any file, an image, or anything,
143420	147420	and then you would use your file with primitive to load it as a file,
147420	149880	and that would give you arbitrary deserialization,
149880	152160	which oftentimes meant RCE.
153320	157800	Sadly, in PHP 8, they have disabled the deserialization of the metadata,
157800	160220	because they actually don't use the metadata,
160220	161660	so it was completely useless.
161820	164620	And also, in older versions,
164620	168940	big frameworks have been removing the far-colon-slash-slash protocol,
168940	170260	because they don't use it,
170260	173460	and they also know that it is very dangerous.
173660	178660	Magento got completely demolished with the far-colon-slash-slash bug.
179000	183340	There were hundreds of bugs reported to them using far-colon-slash-slash.
183340	185160	So, they just removed it entirely.
185800	187520	And also, it's going to be harder to exploit,
187520	190040	even not on PHP 8, this bug,
190040	193340	because deserialization is going to get harder and harder to exploit
193340	194480	as time goes by,
194960	197760	mainly because types are making a comeback.
197760	200020	But that's probably a subject for another talk.
201480	205040	So, people are taking the interest into another PHP-specific protocol,
205040	206580	which is called PHP...
206580	209240	well, you can guess it's PHP-specific.
209360	212360	And there's a sub-part of this which is called PHP filter.
212380	214640	And what it does is that it will read a resource
214640	218660	and apply some filters that you have chosen onto it.
219620	223080	It will then return the resource as if it was the original file,
223080	224680	but with filters applied onto it.
224680	226200	So, a good example of this,
226200	228100	and probably the simplest one that you can get,
228100	228960	is this string.
228960	230200	So, you have PHP filter,
230200	232980	then one filter which is convert.base64encodes.
232980	234400	You can probably guess what it does.
234400	235540	It converts to base64.
235540	237960	And then the resource is etcpassword.
238220	241360	So, what you get when you do a file get contents of this
241360	242900	is file get...
242900	246200	is etcpassword, sorry, but base64encoded.
246480	248220	And the good thing about these filters
248220	250180	is that you can stack as many as you want.
250180	252960	So, instead of base64encoding once,
252960	254080	I can do it twice,
254080	256360	but I could also do it like a thousand times.
256360	257280	It will work.
258860	261940	Well, obviously, you can do other things than base64encodes.
261940	265300	You might be able to put your string in uppercase
265300	266540	with string.upper,
266540	269120	or put it in lowercase with string.lower,
269120	272800	and do some very, very old crypto with rod13.
272800	275300	I don't know why it's there, but it actually works.
275520	279620	And you can also use filters that are not documented,
279620	281780	but very useful as an attacker, such as dchunk.
281780	283020	I will talk about it later.
283020	286560	And, well, you can also use the stuff for today,
286560	288520	which is convert.iconv.x.y.
288520	291880	And what it will do is that it will use the iconv library
291880	294860	of the gdpc to convert your stream
294860	297000	from charsetX to charsetY.
297040	298940	Just to give you a quick example,
298940	300320	say that you have etcpassword,
300320	304320	you want to convert it from utf-8 to utf-16,
304320	307520	you can use this PHP filter string,
307520	309060	and it will do it automatically,
309060	311800	as you can see in the xdump right underneath.
313800	318780	So, well, these filters have been attacked a lot.
318780	320700	There's been a lot of research on them.
320700	326040	And the consensus in 2023 was that you could add,
326040	328500	using filters, an arbitrary prefix to any file.
328500	331400	So you could dump etcpassword with an arbitrary prefix
331400	335260	just by picking the right filters to apply onto it.
335520	338540	And myself, what I wanted to see was whether or not
338540	340920	I could get an arbitrary suffix as well.
340920	344200	So, an arbitrary prefix on some text,
344200	345660	and then some suffix as well.
345660	347140	And the idea is very simple.
347140	348780	Say that you have this piece of code.
348860	350480	Of course you have a far-read primitive,
350480	352720	but then right afterwards you have a JSON decode.
352720	354840	So if the file is not valid JSON,
354840	356760	your far-read primitive is useless.
356760	358420	You cannot read etcpassword.
358420	361840	But if you can make your file look like JSON,
361840	364120	using an arbitrary prefix and suffix,
364120	367000	you can make the JSON decode work,
367000	370020	and therefore you can make PHP think it's JSON
370020	373000	and return to you the contents of etcpassword.
373020	375200	And this research, it actually works.
375200	378560	It has a few preconditions, but you can actually pull it off.
378560	380660	And I call the tool VrapVrap,
380660	383180	and I've made a blog post about it.
383180	386520	It's on our company's blog, so I'mbertenix.io,
386520	387880	as you can see on the bottom.
388220	390600	And, well, the important part about this
390600	393580	is that at the beginning I didn't know much
393580	396640	when I started this research about PHP filters.
396680	398340	So I built a few scripts
398340	400680	just to try and do some conversions.
400680	402360	So maybe from charset A to B,
402360	404240	then apply a basic C4D code,
404240	405720	then from C to D.
405720	407400	Like, lots of conversions just to see
407400	409860	what I could get as a result.
409860	411860	And I actually got a crash while doing this,
411860	413440	so I was pretty surprised.
413640	416620	And I've been working with PHP for, like, 20 years,
416620	419380	so I kind of felt like it was due to PHP, you know,
419380	421120	because it's always PHP.
421140	422800	But this time it wasn't PHP.
422940	424680	It was actually due to the glibc,
424680	428420	so the base library for most Linux programs.
428480	431040	Because when PHP does the character set conversion
431040	432440	using convert.iconv,
432440	435140	it will use the iconv library of the glibc.
435260	437100	And what this library does
437100	440260	is that you pick an input charset and an output charset,
440260	441740	and then you feed it buffers,
441740	443720	and it will convert from your input buffer
443720	444940	to the output buffer,
444940	446440	from one charset to another.
447340	450000	The thing is, when you call the icon function,
450000	451940	you give it an input buffer with some size
451940	454200	and an output buffer with some other size.
454200	457120	And obviously, it should guarantee
457120	459380	that it will now read more bytes in the input buffer
459380	460360	than it should,
460360	463840	or write more bytes than it should in the output buffer.
463840	465840	And generally, it does so pretty well,
465840	467200	like, there's no overflow.
467400	469300	But sometimes it fails to do so,
469740	472580	and this is exactly why I got my crash,
472580	476380	is that when you convert to some very obscure charset,
476380	480000	which is called ISO 2022CNX,
480000	481600	in some cases,
481600	483900	it will not check the bounds of the output buffer
483900	486540	before writing some escape sequences.
486920	489820	And that means that, as a result, as an attacker,
489820	493600	if you are able to convert some text to ISO 2022CNX,
493600	495900	you will get an overflow of one to three bytes
495900	498940	with the very specific values that I've written here.
498940	501760	So your overflow could be with $H,
501760	503700	or $I, or $J,
503700	506540	any of these six values, but only these six values.
508000	511040	To give you a quick idea of the characters that you can use
511040	512060	to trigger the bug,
512060	514900	those are the characters that I trigger the bug with.
515120	516520	So those are Chinese characters,
516520	517720	I have no idea what they mean,
517720	519580	but they give you a buffer overflow.
521720	523600	So the vulnerability that we have,
523600	525000	it's already pretty bad,
525000	527600	like, it's a one to three bytes overflow,
527600	529280	and it's fixed byte values,
529280	530540	like, you don't control the bytes.
530600	533120	In addition to this, you have horrible conditions,
533120	535400	like, you have to control the output charset,
535400	537620	you have to set it to ISO 2022CNX,
537620	540340	like, nobody heard about it in the room, I guess.
540340	542240	So, yeah, you need to find an application
542240	544420	that will let you pick your output charset.
544420	547200	Otherwise, you will not find an application that does it willingly.
547500	548740	In addition to this,
548740	550420	you need to control part of the input buffer
550420	553200	so that you can put in the Chinese characters.
553580	555360	And again, a third condition
555360	558240	is that you need to have a suitable output buffer.
558360	560400	If the output buffer is too small,
561060	563880	sorry, too big compared to the input buffer,
563880	566120	even though you may have an overflow in theory,
566120	567900	you're not going to be able to reach the end
567900	569900	of the bounds of the output buffer
569900	572240	before you trigger your escape sequence,
572240	573800	and therefore you will not overflow.
573900	576620	And this is, like, what was the most annoying part
576620	578600	in looking for targets for this bug,
578600	581660	because I thought that I had a bug in libxml2,
581660	586160	because you can send an encoding when you send your XML,
586160	588600	but the output buffer, when it did the conversion,
588600	590780	was way too big, and therefore I couldn't exploit.
590780	593000	And that's only one of many.
593000	597940	And so, after probably weeks of looking for targets,
597940	601620	I got super frustrated, and I went back to PHP filters,
601620	604120	because that's the only thing that I could make crash,
604120	606380	and that's actually the only thing that crashed
606380	608500	without even me trying for it to crash,
608500	611900	so I was like, okay, that's probably my best bet
611900	615000	to exploit this bug, and I focused onto it.
615020	619240	And the idea was to convert file-read primitives in PHP,
619240	622000	any file-read primitive, into remote code execution,
622000	624020	straight up using this binary bug.
624760	628160	So to explain to you how we might be able to do this,
628160	630880	we need to think about how PHP does allocations
630880	632040	and deallocations.
632740	634880	For this, we need to understand PHP's heap,
634880	636920	but don't worry, that's probably the simplest heap
636920	638380	that you're ever going to see.
638520	643300	So it's 512 pages of 0x1000 bytes each,
643300	647920	so 4096 bytes, and each page contains chunks of some size,
647920	650000	so it might be a chunk...
650000	652500	In page 10, you may have chunks of size 0x100,
652500	655260	in page 11, you may have chunks of size 0x30,
655260	656260	et cetera, et cetera.
656940	660120	Then when you free a chunk, it will go into the free list
660120	661520	for this chunk size.
661700	663880	So if I free a chunk of size 0x100,
663880	666180	it will go into the free list for size 0x100.
666180	668080	If I free a chunk of size 0x10,
668080	672120	it will go into the free list for chunks of this size as well.
672340	674500	And the free lists are singly linked lists,
674500	676360	they are lasting first out,
676360	678340	so that means that if I free a chunk of some size
678340	680080	and allocate a new one afterwards,
680080	682940	it will be allocated exactly at the same spot.
683140	687580	And it's very similar for those who do PTMalloc exploitation
688060	690880	to the libctcache, but completely unlimited
690880	693160	and without any protections.
694360	697100	So, just to give you an idea of what we can do
697100	700060	with 1 to 3 bytes overflow in the PHP heap,
700060	701600	let's have a look at this example.
701600	703820	So, on the left you have the heap,
704200	706520	with 512 pages,
706520	708900	and in page 5, in this example,
708900	710540	I have chunks of size 0x400.
710700	713020	So, in page 5, I have 4 chunks.
713340	715960	In my example, I have chunk 1 that is allocated,
715960	717360	chunk 3 that is allocated,
717360	719180	chunk 2 and 4 that are not.
719340	721160	So, in chunk 2 and chunk 4,
721160	725020	since they are in the free list for chunks of size 0x400,
725020	727880	that means that they contain a pointer to the next free chunk.
728260	733220	So, if I was to overflow even with 1 byte from chunk 1,
733220	735500	I would be able to at least partially modify
735500	739220	the pointer that is contained in chunk 2.
739420	742720	And that would be a way to alter some free list pointer,
742720	746020	and in the end, probably be a good starting point
746020	747680	for an exploitation.
749720	752020	So, the PHP heap,
752020	753900	it looks very, very easy to exploit, right?
753900	756020	There's no metadata, it's page-based,
756020	757760	there's absolutely no protections,
757760	759300	it's really easy to understand.
759300	761720	So, why is it that there are not that many
761720	763240	remote binary exploits?
763240	765860	Well, to me, the main problem is that
765860	769640	the PHP has the one heap for one request policy.
769800	771960	So, if you send an HTTP request,
771960	773840	PHP will create the heap,
773840	775960	allocate your HTTP parameters into it,
775960	777120	then run the script,
777120	778700	so that means also compiling the script,
778700	780280	so it will do like thousands of allocations
780280	781360	and deallocations,
781360	783440	and then it will completely destroy the heap.
783680	786060	So, now imagine that you have a use-after-free
786060	788260	on some VPN instance, for instance.
788840	790140	What you would do to exploit
790140	793000	would be to send a few requests to set up your heap,
793000	795920	then one more request to trigger your bug,
795920	797480	trigger your UAF,
797480	799520	and then afterwards, send a few more requests
799520	800660	to use your bug.
800960	803160	But here, you cannot do this.
803160	805720	You have to find a way to trigger a bug remotely,
805720	807300	so you need a bug in the PHP engine,
807300	809040	you need an application that lets you trigger
809040	810120	the bug remotely,
810120	812000	but also you need to use the bug
812000	813640	as the request is running,
813640	815460	before the heap is gone.
815460	816480	And this is why it's hard,
816480	818260	because in between the triggering of the bug
818800	820140	and the usage of the bug,
820140	822140	you're not going to have any way to interact
822140	823900	with the HTTP request.
823900	826280	Like, you've sent every HTTP parameter,
826280	827000	and it's done.
827000	828340	Like, it's a black box now.
828720	830200	And this is, to me,
830200	831820	why it's really, really hard to exploit
831820	834240	on PHP remotely.
839640	840480	Okay.
840500	841800	So, in addition to this,
841800	843340	there's one thing that we need to understand,
843340	846440	is how PHP processes PHP filter strings.
846620	848020	And to do so,
848020	850900	you will have a two-step process,
850900	853260	where you will first read the resource as a stream,
853260	856180	and then you will apply filters,
857060	859180	So, fetching the stream,
859180	861140	it will use a very standard structure,
861140	862340	which is called a bucket brigade,
863640	865480	and each part of the stream
865480	868200	is represented as one bucket of a few bytes.
868200	870400	So, for instance, if you read the password,
870400	873580	the first bucket may contain five bytes of its password,
873580	876020	then the second bucket will contain the next few bytes,
876020	877560	for instance, ten bytes,
877560	879520	and the bucket three will contain a few more bytes
879520	880520	that come right after,
880520	881820	et cetera, et cetera, et cetera.
881860	885080	So that's a way to be able to represent any stream.
885080	887000	A list of buffers that are linked together,
887000	888960	and that may have different sizes.
891560	893500	Once you have your stream,
893500	895320	you can start applying filters onto them.
895320	896700	So you take the first filter,
896700	898060	and you take the first bucket,
898060	899820	and you apply the filter onto it.
900240	902900	So, for instance, if it's base64 decode,
902900	904220	PHP will take the first bucket,
904220	907040	and decode the data that is into it.
907040	909580	Then it will be able to process bucket two,
909580	910280	which it will do,
910280	911280	and then bucket three.
911360	913860	And at the end, you get a new bucket brigade,
913860	915900	on which the first filter has been applied.
916040	916920	So what you can do,
916920	919240	is start working on the second filter,
919240	920240	then on the third one,
920240	921140	et cetera, et cetera,
921140	922620	up until the end.
922660	925020	And now you have a stream that has been
925020	926840	completely processed with filters,
926840	928880	and your PHP filter string has been
928880	931060	completely, perfectly handled.
932500	935000	So, if we look back at everything now,
935000	936800	it looks pretty easy to exploit this far read,
936800	938620	because we have a far read primitive,
938620	939660	and we want to get our C,
939660	941360	but since we have a far read primitive,
941360	943320	well, first we can read the binaries.
943320	945420	So, if I want to have the address
945420	947600	of some symbol, for instance,
947600	948240	in the libc,
948240	949300	I just download the libc
949300	950620	using the far read primitive.
950640	951960	If I want to check if the libc
951960	953180	is patched against the bug,
953180	954300	I can also download the libc
954300	955960	and check the code myself, you know?
956400	957440	So that means that
957440	959180	you can find gadgets easily.
959220	960520	In addition to this,
960520	962120	ASLR and Pi are not a problem,
962120	963880	because I can just read Proxet Maps.
964180	966320	So, any address that I want
966320	968920	is going to be probably in Proxet Maps,
968920	971320	so ASLR and Pi, it's gone.
971700	973060	A corollary to this is that
973060	975100	the address of the PHP heap
975100	977400	is really easy to find in Proxet Maps,
977400	979320	so I have the address of the PHP heap,
979320	980780	which is going to be useful later.
981020	982240	In addition to this,
982240	983240	it kind of feels like
983240	984920	that I can do arbitrary allocations
984920	985680	on the allocations
986240	988940	simply using the bucket brigade technology,
988940	991420	so the way that the stream is represented.
991420	993240	If I can create as many buckets as I want
993240	994820	with the sizes that I want,
994820	996640	I will probably be able to build
996640	997840	an exploit that works.
999420	1000800	But on the other side,
1000800	1002080	I kind of want to have an exploit
1002080	1005240	that works well even in an unknown environment,
1005240	1006600	so I don't want to build a new exploit
1006600	1007860	when I'm targeting WordPress
1007860	1010300	or some very specific version of PHP
1010300	1011860	or some Magento,
1011860	1014160	which has a lot of traffic on the website.
1014180	1016600	I want an exploit that just straight up works,
1016600	1017920	like basically a web exploit,
1017920	1019040	even though it's binary.
1019100	1021440	And also bonus points if it doesn't match.
1022640	1024620	So, how can we do this?
1024620	1026180	Well, the first problem that we face
1026180	1027260	is that even though there is
1027260	1029540	the bucket brigade technology in PHP,
1029540	1031160	you will always get one bucket
1031160	1032180	when you read a stream.
1032180	1034220	So, if PHP reads the ETC password,
1034220	1036280	it will give you one bucket with the whole file.
1036280	1037880	If it reads an HTTP resource,
1037880	1039860	it will give you one bucket
1039860	1042620	with the whole HTTP response into it.
1042620	1044180	Same for FTP, same for everything.
1044180	1045580	You always get one bucket.
1045680	1046960	And one bucket,
1046960	1049040	it's completely impractical for exploitation,
1049040	1050500	because we cannot spray the heap,
1050500	1051680	we cannot pad the heap,
1051680	1053720	and we can't even use an altered freelist.
1053720	1056020	Like, if we use one bucket to trigger the overflow,
1056020	1057060	we have an altered freelist,
1057060	1058600	but then we need at least two more locations
1058600	1060840	to be able to use this altered freelist,
1060840	1061860	and we cannot do this
1061860	1063860	because we have used our single bucket,
1063860	1065820	so we're not going to be able to exploit.
1067180	1068060	Luckily for us,
1068060	1070040	there's a single filter that allows us
1070040	1071960	to create as many buckets as we want,
1071960	1073700	and this filter is zlib-inflate.
1073700	1076960	What it does is that it will read your stream
1076960	1078380	and decompress it,
1078380	1080500	as with the zlib algorithm.
1080580	1081600	And the way it works is that
1081600	1084120	it will allocate eight pages,
1084260	1085260	a buffer of eight pages,
1085260	1086980	and then decompress into it.
1087000	1088220	If it's not big enough,
1088220	1089740	it will create eight more pages
1089740	1091420	and decompress the rest into it.
1091420	1093040	If it's still not big enough,
1093040	1094440	it will create a third bucket,
1094440	1095780	et cetera, et cetera, et cetera.
1095920	1098220	So this filter, it gives us a way
1098220	1101140	to go from one bucket to as many as we want.
1101140	1102340	But the problem that we have
1102340	1105400	is that every bucket has a size of eight pages,
1105400	1107560	which is way too big to exploit
1107560	1112040	because it takes too much space,
1112040	1113880	and also it's not going to be allocated
1113880	1116440	the same way as small chunks.
1116800	1118360	So we need a way to change the size
1118360	1120800	of each of these buckets.
1121100	1123660	And a way to do this is to use another filter,
1123660	1125280	which is called dchunk.
1125300	1126540	It's undocumented,
1126540	1128860	but it's been used by many hackers before,
1128860	1130320	so it's pretty well known.
1130320	1132100	And the idea is that it will take
1132100	1135560	an HTTP chunked encoded string
1135560	1137780	and simply decode it.
1137780	1139040	So for those who don't know,
1139040	1141500	the HTTP chunked encoding is very simple.
1141500	1145380	It consists of sending a size as ASCII X
1145380	1147100	and then sending the data,
1147100	1148360	and then a new size as ASCII X
1148360	1150120	and then sending the data, etc.
1150160	1153200	So the dchunk filter, what it does is that
1153200	1155320	it will read a size as ASCII X,
1155320	1157420	then read the amount of bytes
1157420	1158840	that correspond to this size.
1158840	1160460	So for instance, if it reads 8,
1160460	1161440	as in the example,
1161440	1162760	it will read 8 bytes.
1162760	1164220	Then it will read 11,
1164220	1166420	and since it's ASCII X, it's 17,
1166420	1167880	so it will read 17 bytes.
1167880	1169420	And then it will read D,
1169420	1170760	read the D bytes,
1170760	1172140	and then it will read 0.
1172140	1174540	And 0, it doesn't indicate an empty chunk,
1174540	1177340	it indicates the end of the dchunking.
1177340	1178740	So when it receives 0,
1178740	1180400	it stops processing the data.
1181720	1184480	So, now you probably have an idea
1184480	1186820	of how we can resize our chunks.
1187280	1188180	We take bucket 1,
1188180	1190420	if we want it to have a size of 148,
1190420	1194240	we write 148, then 148 bytes, and then 0.
1194320	1196540	If we want bucket 2 to have a size of 100,
1196540	1197600	we do the same.
1197600	1200700	We write 100, then all bytes, and then 0.
1200700	1202240	And we do the same with every chunk.
1202240	1205080	And then when we apply dchunk onto this,
1205080	1207340	we expect it to resize every single bucket.
1207440	1210560	But you may notice that this won't work,
1210560	1213980	because even though each bucket is different,
1213980	1216480	they are all processed as one stream.
1216480	1217780	So if in bucket 1,
1217780	1220240	you indicate that it's the end of the stream,
1221060	1223580	with the 0 at the very bottom,
1223580	1224980	it will not process bucket 2,
1224980	1226160	it will not process bucket 3,
1226160	1228100	it will not process any bucket that comes after.
1228100	1230880	So you go back to a single bucket
1230880	1231820	with an arbitrary size,
1231820	1233140	which is completely useless.
1233660	1236740	So luckily for us, there's a way to improve this.
1236960	1240300	And the way to improve this is to pad the size
1240300	1241860	with thousands of 0s,
1241860	1244000	such as what we can see here.
1244000	1245700	The idea is that in each bucket,
1245700	1248280	we will have one chunk size and one chunk data.
1248280	1249860	And that's it. Nothing else.
1249860	1252640	So now if the dchunk filter is applied,
1252640	1255340	we will have bucket 1 that gets processed,
1255340	1256340	it will read the size,
1256340	1257560	read thousands of 0s,
1257560	1258700	then the real size,
1258700	1259500	then the data,
1259500	1260940	and then it will go onto bucket 2
1260940	1262400	and do exactly the same.
1262400	1263460	It will read the size,
1263460	1264280	read the data,
1264280	1265080	then bucket 3,
1265080	1265700	read the size,
1265700	1266440	read the data.
1266460	1269420	And you can see that bucket 1 can be resized
1269420	1270660	to the proper size,
1270660	1271380	bucket 2 as well,
1271380	1272180	bucket 3 as well,
1272180	1273180	and bucket 4 as well.
1273180	1275200	And now we have a way to create as many buckets
1275200	1276000	as we want,
1276000	1277620	and change the size.
1277620	1279300	And this is only with two filters,
1279300	1281020	so it's pretty promising.
1283060	1284200	But that's not enough,
1284200	1286520	because we can only send one request,
1286520	1288600	so we need a whole exploit algorithm
1288980	1290640	in a single request,
1291160	1293380	and with a single PHP filter string.
1293380	1294180	So how do we do this?
1294180	1298040	Well, instead of only being able to allocate once,
1298040	1299940	we kind of want to be able to allocate several times
1299940	1302760	to represent the different steps of the exploit.
1302760	1304480	In step 1 I want to allocate a few chunks,
1304480	1306280	in step 2 I want to allocate a few more chunks,
1306280	1307240	et cetera, et cetera.
1307260	1309440	And at this point we are not able to do this.
1309540	1310440	But to do this,
1310440	1312660	what we can do is think about the PHP heap.
1312660	1313740	It's page-based.
1313740	1316380	So if I focus my exploitation on one chunk size,
1316520	1317580	a single chunk size,
1317580	1321220	that means that chunks that are bigger or smaller than this chunk size,
1321220	1322660	they do not really matter.
1322660	1325500	I will not have to think about them in my exploit algorithm.
1325500	1328180	If a chunk is of size 0x200, for instance,
1328180	1330800	and I'm focusing on chunks of size 0x100,
1330800	1333020	the chunk will not affect my exploitation.
1333240	1334800	So at this point,
1334800	1336960	if I focus on some very specific chunk size,
1336960	1338560	such as 0x100,
1338560	1339900	chunks that are bigger or smaller,
1339900	1340740	they do not matter.
1340740	1342320	It's as if they weren't there.
1342320	1344220	So the only thing that I need to be able to do
1344220	1346000	is to be able to resize buckets
1346000	1347800	as the exploitation goes on.
1347800	1349180	And to be able to do this,
1349180	1354140	I built buckets that are Russian dolls of dchunk algorithm.
1354220	1358160	So the idea is that every time I call dchunk on some buckets,
1358160	1360140	its size will change.
1360140	1364080	And that means that I can make buckets appear at some point in the exploitation,
1364080	1367740	and then others appear at another step of the exploitation.
1367860	1371980	And that basically means that I can allocate and deallocate at will.
1371980	1372820	And at this point,
1372820	1375260	I'm really, really close to getting an exploit working.
1376720	1380400	And the final idea here is that we have a one-byte overflow,
1380400	1381760	using the icon bug.
1381860	1387280	And we want to use it to alter some freelist
1387280	1389360	and get complete control over it.
1389400	1391580	And the way that we can do this is a six-step process,
1391580	1392700	which is pretty simple,
1392700	1394600	but I will still explain it to you.
1396260	1398420	First, we will start in step one.
1398420	1401460	And we're going to have three chunks, A, B, C.
1401460	1403280	And each chunk is freed.
1403280	1406320	We have A that points to B, and B that points to C.
1406320	1408720	Then we allocate these three chunks, and we free them.
1408720	1411780	We get in step three, and the only difference with step one
1411780	1413940	is that now we have C that points to B that points to A.
1413940	1415500	So we have reversed the freelist.
1415840	1420420	Now we allocate in C a buffer, which contains at offset 48,
1420420	1425360	an arbitrary pointer, which is represented here with 1, 1, 2, 2, 4, 4, etc.
1426100	1429060	Then we allocate B and A, and then we free everything.
1429060	1430500	And we get into step five.
1430500	1435840	And we have exactly the same E-playout and freelist as in step one,
1435840	1440280	but in the chunk C, we have an arbitrary pointer at offset 48.
1440280	1442340	So now you can probably see where this is going.
1442340	1446280	We overflow using chunk A into chunk B.
1446280	1451100	So the freelist that was B to C is now B to C plus 48.
1451120	1454880	And what we have in C plus 48 is another pointer that we added ourselves.
1454920	1459080	So with three more allocations, we will allocate B, C plus 48,
1459080	1461320	and then we will allocate our arbitrary pointer.
1461640	1465200	And now we are able to have a write-what-where,
1465200	1468520	which is really, really close to getting code execution.
1470400	1473120	But now we have to think about how we can do this.
1473120	1477840	Well, as I've said before, the PHP heap, it's really easy to find it in Proc Setmaps,
1477840	1479200	so we have it for us.
1479300	1482220	And also, at the very top of the PHP heap,
1482220	1486440	there is a send-mmheap structure that contains a few things,
1486440	1490840	and also a few alternative heap function pointers.
1490920	1493540	So for those, again, we do heap exploitation on ptMalloc,
1493540	1496740	it would be the same as free-hook, malloc-hook, and realloc-hook.
1496760	1500760	Those are pointers that, if you set them to something else that null,
1500760	1503500	they're going to be called inside of the original function.
1503700	1507320	And so, since it's basically the same as the glibc, I can use the glibc technique,
1507320	1510000	and I can override free with system.
1510000	1512740	So now, when PHP wants to free a chunk, it will call free,
1512740	1514480	but it will instead call system.
1514480	1520380	And this gives me a way to call an arbitrary comment.
1521220	1525480	And so, with everything put together, what do we have?
1525480	1528200	We have an exploit that works against any target,
1528200	1535720	from PHP 7 to the current version, which is H3-something.
1535720	1539600	So that's 10 years of PHP covered with a single binary exploit.
1539600	1542860	And also, it works against any PHP application, because we can pad the heap,
1542860	1548240	so even if the website uses a lot of chunks of size 800, it will still work.
1548240	1549160	It doesn't matter.
1549280	1552580	In addition to this, the exploit is completely deterministic,
1552580	1556540	so it's 100% reliable, so you will never get a crash.
1556660	1557700	So it's pretty cool.
1557860	1562800	And the final good thing about the exploit is that the payload is really small,
1562800	1566840	because the first gadget that we use, the first filter that we use, is a Zlib inflate,
1566840	1568220	so it will decompress the payload.
1568220	1570480	So originally, it's compressed, so it's very small.
1570480	1576560	And the last good thing that we can say about the exploit is that it's completely self-contained.
1576680	1580160	The whole algorithm of the exploit is in one PHP filter string,
1580160	1583760	which means that you don't have to send additional GET or POST parameters.
1583760	1586560	You just need your PHP filter string, and you get RCE.
1586780	1592700	So every file reading primitive now that you have on PHP, you have RCE instead, which is pretty nice.
1593400	1597240	I'm going to show you a demo that actually involves Cosmic Sting,
1597240	1599940	which is a Magento X6E, which is pre-authentication.
1599940	1603100	And it came out after I published my bug, so it was in June.
1603160	1606840	And the original impact was being able to read files.
1606880	1611800	And using these files that you've read, you would be able to craft API keys,
1611800	1617300	and so you would get an access which is similar to an administrator level on Magento.
1617300	1619600	But you wouldn't be able to get RCE.
1619700	1624000	But with the bug that we have now, we get RCE easily because we have an X6E,
1624000	1630380	so we can force it into reading any file, and therefore we can read a PHP filter string and get RCE.
1632560	1637320	So, quick demo, which is not on the right screen.
1638460	1639380	There we go.
1643640	1648420	So, I cannot see it, but it's quite easy to understand.
1648420	1652220	You have the Magento website on the left, and then you have an exploit on the right,
1652220	1656220	and in a few moments, you will have a shell on the bottom.
1656820	1658140	There you go.
1687360	1690160	There we go. Thank you, man.
1691340	1694580	So, oh, I lost my...
1694580	1696400	On the other screen, I lost it.
1700440	1701480	Wait.
1704000	1705580	Okay, there we go.
1706020	1707960	Okay, does that work?
1708320	1709320	Yeah.
1709760	1715500	Okay, so now that we have exploited PHP filters, we want to do a completely different exploitation,
1715500	1721980	because PHP filters are not the only way in which you can reach ICONF using PHP.
1721980	1726940	There's another way that is way simpler. It is when PHP calls ICONF directly.
1727540	1732660	And so, there's a PHP function that does this. It's called ICONF.
1735840	1739860	So, how do we exploit such a call to ICONF?
1739860	1745680	Well, it's very, very different to the attack that we've seen using PHP filters.
1746980	1749260	So, here we don't have a five-read primitive,
1749260	1753640	so we cannot just read proxmaps or read the libc or anything.
1753640	1756920	We need to go, I would say, old-school exploitation,
1756920	1760040	where we need an infoleak to get some pointers,
1760040	1762640	and then we can start going with the exploitation.
1762920	1769120	And in PHP, generally, when you want to have an infoleak,
1769120	1773760	what you do is that you will try and alter the length field of a ZenString.
1773760	1778540	So, a ZenString, it's the representation of PHP of your PHP string.
1778720	1781480	And the length field, it will store the size of the string.
1781480	1787280	So, if you are able to change the length of a string, and then it gets displayed,
1787280	1791700	well, you are able to make PHP display more bytes than it should,
1791700	1796280	and probably display lots of useful information from the heap, such as addresses.
1797140	1800880	Sadly for us, the bug that we have, as I've said, really, really bad.
1800880	1803040	We only have one to three bytes overflow.
1803040	1807160	So, if we try to overflow into a ZenString, we will just overwrite the ref count,
1807600	1810840	with values that we don't even control, so it's pretty useless.
1811940	1817760	So, what we need to do is kind of the same as before, where we alter some free lists,
1817760	1821980	and if we manage to alter some free lists, we are able to make two chunks overlap.
1822040	1827240	And if we have two chunks that overlap, we can use the first one to store a string that is bound to be displayed,
1827240	1831060	and then use the second one on top of it to overwrite the length field.
1831060	1836920	And when the string gets displayed, we actually are able to see what comes afterwards in memory.
1838440	1842260	The problem is that here, we completely rely on the code.
1842340	1846560	Beforehand, we could implement an algorithm using PHP filters, as I've shown,
1846560	1850780	but here we only have a single call to iconf, and that's it. Nothing else.
1850780	1859440	So, getting it to work would make us reliant on the code that comes after the call to iconf.
1859440	1864080	And to explain this, a very simple example. Say that you have this piece of code.
1865020	1872200	We control the output charset, we control the input text, so it's clear that we can trigger the bug.
1872860	1876820	But right after the bug has been triggered, the script will end.
1876920	1882200	So, there's absolutely no way for us to use the bug after it's been triggered, because the heap will be destroyed.
1883040	1888760	If you're lucky, however, you might find a line such as this one after your call to iconf.
1888960	1898360	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.
1899500	1903620	If you have a line such as this one, you can create as many chunks as you want,
1903620	1907580	because it will take your string and split it along a known separator.
1907840	1911300	And that will give you a way to use the bug right after it's been triggered,
1911300	1914080	and then probably exploit the bug.
1914120	1916420	So, this is what I call a code gadget, actually.
1916540	1924540	That's a way to get to using your bug, starting from the position where iconf has been called.
1925440	1931020	And to give you an idea of how it can be implemented in the real world,
1931020	1935720	I will show you a demo, well, and explain to you the demo on Roundcube.
1935720	1940840	Roundcube, for those who don't know, it's a webmail that is very much used.
1941220	1943420	It's probably the most well-known in PHP.
1943560	1947760	It's used by hosters generally, or private companies even, I've seen it a lot.
1948520	1951520	And, well, yeah, it's used a lot.
1952280	1955340	And so, when you're logged into Roundcube and you want to send an email,
1955340	1957880	you have to specify email addresses.
1957880	1962460	And so, you specify the two fields, the cc field and the bcc field.
1962460	1965580	You can guess what they are, you've already sent an email.
1965820	1971180	In addition to this, you can send another parameter, which is charset.
1971180	1976140	And every parameter that you send, so the underscore 2, underscore cc, and underscore bcc parameter,
1976140	1979020	they are all going to be cast to the charset that you've set.
1979020	1980780	And it's going to be done using iconf.
1980780	1985760	So, in this single script here, you have three ways of triggering your bug.
1985760	1990960	You can trigger it using underscore 2, or with underscore cc, or with underscore bcc.
1990960	1993780	And right after this, there's something even better.
1993780	1997820	If one of the emails is not valid, you will get an error message that says,
1997820	2001000	well, this is an invalid email, colon, your email.
2001540	2002740	So, what can we do with this?
2002740	2007780	Well, this is textbook, the strategy that I was telling you about before.
2007780	2013160	The idea is that you set underscore 2 to an invalid email, so that invalid email gets set.
2013160	2015340	It's ready to be displayed in the error message.
2015340	2021120	So, now what we want to do is modify its length before it's included in the error message.
2021360	2024980	So, then we use cc to trigger the bug, and we have a free list that is altered.
2025060	2031620	And then we use bcc to allocate a few strings, so that we have a string that gets allocated on top of invalid email,
2031620	2034100	and this will alter its size.
2034160	2038740	And therefore, the error, when it gets displayed, will display more bytes than it should,
2038740	2041800	and we will be able to get a memory leak, which is nice.
2042380	2044320	So, I implemented this, and it was really fast.
2044320	2048420	It took me a few hours, so I was really happy with it, and I started it, and it didn't work.
2048540	2054780	And the reason for this is that when Roundcube sends an error message, it's converted to JSON.
2054780	2059320	And when PHP converts to JSON, every byte that is not valid UTF-8 is removed.
2059380	2063940	So, you have a memory leak, but you don't really have any bytes of interest,
2063940	2068780	because generally addresses are not going to be valid UTF-8, so they're just going to be removed.
2068860	2072700	Any raw data in memory is not going to be valid UTF-8, it's going to be removed.
2072700	2079420	So, what you get is a memory leak, which consists of only ASCII characters and new bytes, which is completely useless.
2079820	2082060	But, the idea is pretty good still.
2082060	2089200	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.
2089200	2094060	And to do that, we just have to look at the framework and go a little bit deeper, look at many, many functions,
2094060	2098560	up until we find a variable that gets displayed without being modified.
2098560	2104280	Sadly for us, when it gets displayed without being modified,
2104280	2111300	it will not have a lot of codes in between the creation of the variable and the display of the variable.
2111300	2115300	So, it limits the number of gadgets that we can use.
2115680	2120060	And, well, it's honestly pretty much hell to make this work,
2120060	2124860	but you have to think that every line that you can use, like every line in the code,
2124860	2128380	you can use it to go closer to your goal.
2128380	2132660	Like, every line can be an allocation or deallocation that would shape the heap better for you.
2132740	2136300	And I actually managed to pull that off in, I think, five days.
2136300	2138120	It was really, really long.
2138120	2140360	And I actually managed to get the leak.
2140360	2144180	So, it was nice, because afterwards, it's pretty easy to get ASCII.
2144180	2148180	Once you have an address of some value in the PHP heap,
2148180	2150020	you can just reproduce the same exploit,
2150020	2155840	but this time, use your freelist corruption to allocate at an arbitrary position.
2155960	2158460	And that means that you can do data-only attacks,
2158460	2161600	where you overwrite some value that is in your session, for instance.
2161600	2164820	Maybe you want to overwrite your roles and set them to admin,
2164820	2168000	or maybe you want to overwrite serialized data before it's deserialized,
2168000	2170400	so that you have an arbitrary and serialized in PHP.
2170580	2175180	Or, you can also go the standard way, where you go for a binary attack,
2175180	2177020	where you want to control MIP.
2177020	2180180	You started with binary, and you want to go full binary.
2180300	2182840	But I think the data-only attack is more elegant,
2182840	2185440	and it fits more, because you don't have to dump the binary.
2185540	2187220	So, it's easier.
2187880	2189920	But, like all of this that I'm saying,
2189920	2192160	it's actually only the visible part of the iceberg.
2192160	2194220	It's really, really hard to pull off,
2194220	2196500	because first, you need a proper heap setup.
2196500	2198800	I haven't covered this part, so it's really hard to do,
2198800	2202980	because the only thing that you can use to do your heap setup is HTTP parameters,
2202980	2206120	but then the script gets compiled, and you will use other scripts.
2206120	2209600	They all get compiled in the same heap, so you will completely mess up the heap.
2209600	2211120	It's really annoying to pull off.
2211280	2214300	In addition to this, variable scope is here.
2214300	2217900	If you have a method that is called, and then it exits the method,
2217900	2220600	every variable that is in the method is going to be freed.
2220940	2223900	So, it will completely change the heap as well.
2224100	2226520	And also, obviously, it's very specific.
2226520	2228560	Your attack to the server that you're targeting,
2228560	2231660	the application that you're targeting, and also the settings.
2231660	2235740	I think the original exploit that I built, it was for Roundcube in French.
2235740	2238300	If I changed the language to English, it wouldn't work,
2238300	2242580	because the strings that get allocated with error messages, they wouldn't be the same,
2242580	2244580	and that would completely change the shape of the heap.
2244580	2250420	So again, it's a huge pain to pull off, so don't do it unless it's really necessary.
2251000	2251960	But it works!
2252340	2260580	And I will show you an exploit, hopefully, if I can make it appear again.
2266050	2269750	Oh, I'm paused. There we go.
2270350	2273910	Now I need to find...
2280840	2283280	Okay, same layout.
2283740	2290360	You get a shell on the very bottom, an exploit on top of this, and the application on the left.
2297780	2300800	And now I need to remove it again.
2301500	2304580	Which one of these? This one? Nope.
2305820	2306780	Nope.
2307980	2308940	Yeah.
2309300	2313940	Okay. A quick conclusion, and maybe a few more words afterwards.
2315960	2318400	So the bug was really bad, honestly.
2318500	2322400	It had lots of preconditions, and even the bug was not that good.
2322400	2324140	A really bad overflow.
2324240	2332480	But luckily for us, PHP exists, and we actually are able to do decent things with it.
2332480	2339940	We have a new attack vector, where we convert a favorite primitive into an RCE on any PHP version, against any target.
2339940	2343440	If you have a favorite primitive, you have RCE now. It's pretty nice.
2343500	2349720	And also, you add application-specific attacks, such as the one I've demonstrated on Runcube.
2349720	2356020	However, the bug is not PHP-specific, so there could be a lot of other targets.
2356100	2358720	I couldn't find any, but maybe you will.
2358940	2369020	And they might be discovered as time goes by, if people try to find a way to exploit the bug.
2370440	2372340	The timeline goes fast.
2372340	2376400	I found the bug last year, and I started exploiting it in February.
2376400	2381740	After a month and a half, I could report it to the GDBC security team, and they were really amazing.
2381740	2385380	They were really fast. After a week, I got a patch, and they were really nice.
2385420	2391640	They knew what they were doing, which is not always the case, to say the least.
2392040	2400620	So the bug got patched in a week, and two weeks after the embargo, it was released as CVE-2024-2961.
2402300	2404920	That's probably the end of it for me.
2404920	2410920	I have published lengthy blog posts about the bug, because there's so much to say about the bugs.
2410980	2416180	And also, the impact on the PHP ecosystem, which I couldn't cover here, because we only have 45 minutes.
2416380	2419540	And also, there are exploits that are available on our GitHub.
2419640	2421820	And also, there's my Twitter, if you want to ask questions.
2421820	2425300	You can also ask some questions right after the talk.
2425520	2429520	And just to flex a little bit, a few what-ifs.
2429520	2434640	What if we have a Fargate primitive, but we cannot see the output?
2434720	2437340	We have Fargate contents, but no echo in front of it.
2437340	2441120	So we cannot read FoxFMaps. We cannot read any binary.
2441180	2443980	So the exploitation that I've shown you, it doesn't work.
2444280	2448820	And also, in some very, very rare cases, zlibin-fake will not be present.
2448820	2453460	You will not be able to decompress your stream, so you will have to exploit with one single pocket.
2453760	2455840	And that looks impossible.
2456100	2459240	But I was able to do both at the same time.
2459240	2460980	That was actually my very first exploit.
2461640	2464040	And I will try and show you how.
2466900	2470620	It's just a demo, really sped up, because it's really slow.
2470620	2472780	But, well, at the end you get a shell.
2472780	2475920	The important part is on the left, the source code.
2475920	2479320	You can see at the very bottom, you have a Fargate contents without an echo or anything.
2479320	2480660	So I don't have the output.
2480680	2486260	And also, on top of this, I have arbitrary allocation, uh, random, sorry, allocations and deallocations.
2486260	2490780	Just to show that the script is not very, that it's not tailored to attack one PHP script.
2490780	2493280	Like, it could work against any PHP instance.
2493540	2495800	And, yeah, the exploit is not done yet.
2495800	2497520	It's sped up like eight times, I think.
2497520	2498540	It's really, really slow.
2498540	2502320	But you get one byte per one byte, and at the end you have another shell.
2502360	2503580	So, yeah, that's it.
2510200	2511840	Okay, I'm getting close.
2512000	2512640	No.
2517600	2518400	Yeah.
2518620	2519600	There we go.
2520420	2521220	Questions?
2529890	2531090	Okay, I guess we're good.
2531090	2532290	Ah, there's one.
2535770	2536550	Yeah, thank you.
2536550	2537630	This is amazing.
2537790	2544590	Uh, so, how did you get to the Roundcube target and establish that those conditions were present that you were looking for?
2544590	2549430	Um, so, I thought that, like, emails to me...
2550010	2552750	When you send an email, you can kind of send the charset with it.
2552750	2557150	So I kind of felt like webmails would be a good target to look at.
2557230	2558970	And I looked at Roundcube.
2558970	2562130	And I saw that, like, it was a good target.
2562130	2563310	So, yeah, that's why.
2563310	2563810	Oh, cool.
2563810	2564590	Pure intuition.
2564590	2565170	That's amazing.
2565170	2565650	Thanks.
2572930	2575050	Well, thank you for listening to my talk.
