start	end	text
0	7980	So it is my pleasure to welcome Ian Hickey to the stage talking about relative path
7980	18260	file injection, the next evolution in relative path. Okay. Let's give it up.
24860	28320	Thank you for having me today at the AppSec Village,
28320	33400	DEF CON 32. Thank you for having AppSec Village here as well.
34600	41100	I'm a software developer. I don't work in security. I work in ed tech. But every once
41100	46500	in a while I like to spend some brain power looking at some security things. And the last
46500	53800	time I was doing this in April or May, I had a couple of discoveries that I thought were
53800	60220	interesting and I'm going to share them with you today. So many of you know that there's
60580	76140	relative path overwrite. Relative path overwrite is a way to if you can inject CSS onto an
76140	85960	HTML page and if a website is vulnerable to path confusion, then you can actually get
85960	96700	the default link tag in HTML to interpret the current HTML page as a CSS style sheet
98100	108000	and under some circumstances it will execute the CSS. And older Internet Explorer used
108000	113860	to be able to use things like an expression tag and execute JavaScript from CSS. So it
113860	122780	was a pretty ‑‑ it was a pretty interesting way to get JavaScript execution. So what I've
122780	130820	been looking at is port swagger research has these impossible labs and one of these impossible
130820	134720	labs I've been looking at for a couple of years and I'll go back and play with different
134720	140460	things and see if I can make any progress on it. In this case it was ‑‑ there's
140460	148060	context in HTML where you can't execute JavaScript. The browser won't let you. And some of those
148060	159900	places are no script within a frame set tag. It interprets ‑‑ it interprets any HTML
159900	166300	that you can inject as just plain text. And because it's plain text you can't do anything
166300	174660	with it. So the first insight that I had into this was ‑‑ see here's an example.
175600	184760	You have a B tag but it's just text. You can't display anything. The first interesting sort
184760	191880	of insights I had into this was actually you can execute JavaScript, but you would have
191880	198040	to have AngularJS installed and you would have to be using AngularJS and it's been end
198040	205740	of life for quite some time. But there is a way to do it. So if you have a no frames
205740	213520	tag inside of a frame set tag and there happens to be AngularJS on the page, then you can
213520	224120	just instantiate AngularJS and execute some JavaScript. That's cool. In the original relative
224120	233620	path overwrite, if you needed to inject CSS into the page, and again, it didn't matter
233620	242460	where you did it, because in both cases, those parsers are not taking into consideration
242460	249860	the rules that the browser follows. Angular will happily execute JavaScript anywhere on
249860	256900	the page it finds it and in this case, when it's interpreting a HTML page as a style sheet,
256900	261660	it doesn't care where it finds CSS, it will just execute it. So that was a starting point
261660	266680	and then the epiphany was sort of, okay, well, you can use other parsers to execute JavaScript
267760	271480	within these contexts where you're not supposed to be able to do it. So what other parsers
271480	277320	are there that will do the same thing? One thing to note about both of these things is
277320	285840	that they're lenient parsers, meaning it does not matter that there's HTML surrounding the
285840	291400	thing that they're looking for. All that it matters is that it eventually finds it. So
291400	297220	that was my starting point. What else is there? So I started making a list of all the different
297220	304980	types of parsers there are. There's SVG parsers and XML and PDF and rich text and markdown
304980	312440	and after spending more time than I would care to admit with markdown parsers, I started
312440	318860	thinking about PDF a lot. PDF can actually be handwritten. A lot of people think of it
318860	322540	as a binary format and there might be binary mixed in with it, but you can actually just
322540	335740	write a PDF file and as plain text. So if you have plain text injection like we're talking
335740	342880	about here in one of these contexts such as no script, no frames, then you can inject
345080	352200	PDF. Well, PDF can execute JavaScript. PDF can submit forms. PDF can ‑‑ there's
352540	359480	tons of research into malicious PDFs. So I started playing with it and the second
359480	368440	big epiphany that I had that just really surprised the shit out of me is that PDFs are also super
368440	374760	lenient because PDFs start with these magic characters and end with magic characters.
374760	378960	It doesn't really care what it finds before and after that. So you can actually take this
378960	386840	HTML document with PDF persistently injected into it and you can save this as a PDF and
386840	396000	it will actually execute. And this is what this is here. This is just an embedded HTML
396300	405980	file renamed as PDF and it displays as a PDF and it executes JavaScript. How many people,
405980	415140	how are you going to convince people to download an HTML file as a PDF? Like are you going
415140	420520	to put something on the page that says right click here, save it as whatever? They just
420520	430920	won't do it. So I started thinking about ways to get people to download these PDFs without
430920	437860	me having to explicitly convince them of anything. And when I started thinking about the original
437860	444000	relative path overwrite, which interprets the current page as a CSS style sheet, well
444000	449480	the reason why it does that is because the browsers are stupid. They just, whatever you
449480	453620	return back from the server it's going to use. And if you happen to be using a server
454320	460300	that when you add a forward slash at the ends returns the current page back, well if the
460300	467520	current page happens to have PDF injected into it then that's what you're going to get.
470340	478320	So that's exactly what I did. I'm going to show you how it works now and before we even
478320	483880	get into it, just so everyone sees because it's easier to simply see what's happening
484360	496380	than to explain it. Someone goes to a website. There is a download link on the website and
496380	504220	the download link happens to use a relative path. Well if you click this download link
504220	509020	all that's going to happen is it's going to download whatever the file was that you intended
509020	524330	to download. And if we open this up, all this is, is, oop, and if we open this up, all this
524330	538250	is, is, you know, oh sorry, sorry. Let's see if I can make that bigger for you. Oop, oop,
544530	553410	one second. Okay, good PDF download. So I've downloaded this PDF and I open this PDF. I'm
553410	562350	going to open it with Google Chrome. It's just a normal harmless PDF. No big deal. However,
562350	569830	all you have to do is add a forward slash at the end and you notice it lost its styling.
569830	575450	The reason it lost its styling is because now the browser is confused. If you look in
575450	585470	the, if you look at what it's actually trying to include as the style sheet, what you find
585470	590970	now is it's actually getting the current page. That's relative path over it. It's confused
590970	597790	so it's returning this back. The effect of that, once it loses its styling, is that if
597790	604570	you now click this, it's actually going to return the current page as the PDF. Which
604570	611510	means when someone opens this, it's not going to be the same PDF that, that they expected
611510	624620	to open. Instead, it's going to execute JavaScript. So what just happened? Where did, where did
624640	631320	the new PDF come from? Well, the PDF lives on the server. Because the HTML lives on the
631320	641380	server and you've persistently saved PDF content into that HTML page, right? And it's sufficiently
641380	647400	confused so when they go to download, it downloads what you want them to download instead of
648000	652680	whatever they were trying to download. I mean, this is dangerous. If you think about it,
652680	660060	what if this was a, what if this was a text PDF site or something? It's not going to be
660060	664420	just one link that this happens with. There will be every link on the page that uses a
664420	670940	relative download. As soon as they click it, it's going to download whatever that PDF is.
670940	675880	So the things that you can do with PDF are pretty interesting, right? Depending on the
675880	687120	PDF reader that the user happens to be using. So you have, you have something like Fox Pro,
687120	693520	I think it's called, that allows you to do commands when the PDF opens, right? You can
693520	699100	execute shell commands. So there's nothing stopping you to simply populate your PDF with
699240	705120	a launch command that executes shell commands as long as the user has permission to be doing
705120	711480	it to do whatever you want, right? You can run scripts, you can download new scripts,
711480	716580	you can, you know, whatever. In fact, if you want to stay in the web context, I imagine
716580	723060	you could probably open a Chrome browser headless with the user's data set as their data and
723060	729660	then just go explore the Internet as them, right? Because why couldn't you? So it's pretty
729660	737300	dangerous. But thinking this through a little bit more, it's interesting that you can do
737300	743840	it with PDF and all I did here was this would be, you know, say it's a profile page and
743840	750340	you have some public, you know, blurb about yourself that you've put this PDF in. However
750920	758140	you get the persistent content injection onto the page. But as long as it's there in some
758140	763560	capacity and as long as you can save it as a PDF, which we can because there's a simple
763560	769940	download link, then it'll execute. So in the original relative path overwrite, what you
769940	778300	needed was a link tag with a relative path to the style sheet. In this case, you need
778300	787980	an anchor tag with a relative path to the PDF. And if it's not HTML, the download has
787980	794560	to be fully qualified. Meaning if you don't have sample PDF here as download, then it
794560	802420	won't accept downloading the HTML as a PDF. The content disposition will be wrong. So
803100	808700	that's the basic requirement for executing this. It has to be a page. So think like a,
809000	816240	you know, PDF library or, you know, places where you can inject content and then it says,
816240	825200	save your whole history or something like that. But this technique is going to be more
825200	833680	useful the more file types that we find that you can do this with, right? So PDF is great
834200	840620	if they happen to be using a PDF viewer that supports executing shell commands, et cetera.
841720	845960	But there has to be other lenient parsers and lenient file types that you can use to
845960	861780	do similar things. So then I started exploring that. So Gareth Hayes, I was talking to him
861780	866540	about this just to make sure that he hadn't heard of it being discovered before either.
866780	872700	He suggested I talk to a gentleman by the name of Orange Albertini who specializes in
872700	880260	polyglot file types. He's a maven. He's the type of guy that his business card, you can
880260	886860	put it into a Super Nintendo emulator. And even though it's a PDF, it will still play
886860	894900	in the emulator. It's crazy. And we were brainstorming other types of lenient file types where this
894900	901160	sort of thing might work with. And we went, you know, zip file types to tar. Tar is another
901160	905480	like plain text one that you can sort of hand write. And it took me down this road looking
905480	908820	at that. And while I was exploring it, I was thinking about, well, what would you put in
909000	915440	a shell scripts? And then anyone who's dealt with enough shell scripts knows that shell
915440	922460	scripts can actually be really lenient when they're run too. So what this means is if
924280	929700	you happen to be what the user is trying to download is a shell script and you have some
929700	938800	sort of content injection, you can actually get them to get them to download a shell script
938820	951340	you know, whatever, PowerShell file or a batch file or, you know, batch scripts. And in some
951340	963540	Linux distributions, and I'm looking at especially Windows WSL2 with U12, et cetera, with some
963540	971260	of these things, it'll actually happily execute an HTML file as a shell script. And it will
971260	975260	error on every single line until it gets to something that it understands and then it
975260	981900	will execute it. But if someone's going to a site looking to download a shell script,
982560	988460	it's going to say, hey, download this code and then run it as a super user, right, because
988460	994640	that's what they always say. So if they do that, and the link that they clicked happens
994640	1001920	to have a forward slash at the end, it's susceptible to, you know, path confusion, et cetera, then
1002600	1008140	the script that they get is an HTML page, and when they run it, it's going to run. And
1008140	1020000	what that looks like is... what that looks like is...
1020020	1028400	something like this, where every single line, HTML, the head, all of it, errors, line by
1028400	1033920	line by line, and then it gets to echo hello world, and it executes it, and then it exits,
1033920	1037800	and that's it. By the time they realize something happened, it's going to be too late, right?
1039180	1046540	So I managed to get this working with WSL2. I managed to get it working with PowerShell,
1046540	1051440	which does the exact same thing, right? It'll execute every single line of the HTML file,
1051440	1057620	because it's a PS1 file or whatever, and then it gets to hello world, it executes it, and
1057620	1062080	then it continues, you know, just executing nonsense. The closest I could get it with
1062660	1068960	regular Linux shells is... you have to be able to inject something right after HTML,
1068960	1074920	so it's far-fetched, but I threw it in there in case anyone has any ideas on how to get
1074920	1084540	that working. I did actually get this similar thing working on OSX and Linux, if a user's
1084540	1092220	using, you know, BoxPro or whatever it is, because you can have a launch command that
1092220	1099680	simply reads whatever it is in its own file, and then, you know, executes it line by line.
1099680	1108640	So it can be done. It can be done. And so that's... so now we have two, three, four
1108640	1115200	types of files. Ideally, we would have dozens, hundreds. There's so many file types out there
1115200	1123660	to get this type of stuff working well. So I kept on... I kept on investigating other
1123660	1130040	things. What does not mind there being HTML, right? What will someone simply download from
1130040	1135380	the Internet and then throw into something and have it execute, right? So pretty much
1135380	1145640	all server-side... server-side templating languages are like this, right? So if someone's
1145640	1153640	looking for, you know, a Ruby or whatever, a ColdFusion script or anything else, you
1154500	1160160	know, they're probably going to grab it and then just throw it into whatever it is to
1160160	1165460	test it, at which point it's already executed anything it needs to execute, right? And the
1165460	1170060	cool thing there is that any of the server-side languages, they don't... they're expecting
1170060	1175100	there to be HTML, right? They're expecting there to be all this other stuff in there,
1175100	1181080	so they won't complain. And so these are the ones that I found that it would work with
1181080	1188140	out-of-box, right? So you have this PHP. Similar thing right here, if we look at this
1190580	1211060	vulnerable PHP. So all this is going to do, if we go here, is the safe download... let's
1211060	1219400	go to the PHP file, open it up, show you what this has generated. And what this did
1219400	1224500	is because it's server-side, it already executed this, and it says welcome user, no big deal.
1225560	1243460	We do it the other way, forward slash, all of a sudden they download it. This time, welcome
1243460	1248360	bad guy, right? So there's nothing stopping you, it doesn't have to be bad guy, it could
1248360	1254680	be read the contents of a password file, whatever, send it somewhere, anything you
1254680	1260980	want it to. If you can get people... the thing that's sort of insidious about this, even
1260980	1266160	though there's like user interaction that could take place, is that the user's going
1266160	1272360	to a server that's trusted, right? There's nothing untrusted about this server, they've
1272360	1277740	been to it a thousand times, and they're clicking a download link. There's no difference
1278420	1283380	if you were able to just break into the server and replace the file, right? Because the effect's
1283380	1296860	the same. But what would be really interesting is if you didn't have to do this whole download
1296860	1303640	portion of it at all, right? And that's the hardest part, is how do you get someone to...
1303640	1309080	you know, if someone's going to go download a PDF, click on a link and download a PDF,
1309080	1313060	there's a high chance that they're going to go look at the PDF, right? I mean, that's
1313060	1317520	what it's intended for, they're going to trust that whatever's in there. It should be, you
1317520	1321580	make a PDF that looks like a tax document and submits back to your server, whatever.
1322900	1327740	But it would be great if they didn't have to download the thing in the first place.
1327740	1333560	So I started thinking about how that could possibly work. And that's pretty tricky, right?
1335030	1344880	But there's been a rise in all of these HTML over the wire sort of frameworks. There's
1344880	1351640	Livewire, there's Hotwire, there's HTMX. And all of these things, what they're doing is
1351640	1360340	they're getting HTML from the server instead of putting all the HTML client side, right?
1360340	1364940	And the HTML they get from the server could be, you know, pre-rendered with, like, the
1364940	1373540	user's profile picture and, you know, username or something like that. But they're going
1373540	1379460	to suffer the same type of issue if they happen to be on a server that returns the
1379460	1383560	current page when there's forward slash at the end.
1383660	1398280	So something like that, what it would look like is... so I'm not using any of the frameworks.
1398280	1409320	This is just straight jQuery. But what this does is when the page loads, it attempts to
1410340	1424000	get some DOM element and then inject it into this main div, right? And again, it's the
1424000	1432880	same thing. All we're doing is, you know, doing a... you don't need to do this. All
1432880	1438340	it's doing is just, you know, using jQuery HTML, just setting the HTML. No big deal.
1438340	1450800	And if we go look at what this script does... what's that? There's nothing on this page.
1450800	1458980	There's literally nothing on this page. So all this does is it doesn't do anything initially.
1460080	1467560	You can see when it goes to get the... let me see if I can make this bigger.
1469400	1477360	Excuse me? Oh, got you, got you. So nothing too interesting is happening on this page
1477360	1483500	yet. It gets jQuery. It reaches out and it gets this thing that says welcome user. No
1483500	1488460	big deal. And then it tries to inject the welcome user into the page. But you add a
1488460	1497540	forward slash on to the end and now it actually gets the current page, vulnerable HTML.
1504500	1515970	And... oh, one second. Let's see what just happened. What did I do? Oh, there it goes.
1515970	1527250	So in this case what just happened is when it reached out to get it, the ID that it found
1527250	1533170	was for a frame set. And HTML is probably the most lenient parser of all because it
1533170	1540330	has this really weird behavior where if you try to inject HTML tags into other HTML tags
1540330	1546090	that it doesn't like, it will simply strip those tags away. And that's exactly what happened
1546090	1562440	here is because when it found... oops. It's not letting me leave. It's not letting me
1562460	1573560	leave. Okay. Oops. Yeah, so when it found the... this frame set, it... when it injected
1573560	1578700	it into this main tag, it actually stripped the frame set away. So even though the browser
1578700	1583120	is saying hey, this frame set is completely innocuous, we're just going to treat it like
1583120	1590380	text, the framework itself can take that and inject it automatically. So there's places
1590380	1596620	where maybe you weren't concerned about people getting content injection before, but now
1596620	1600300	you have to be worried about it. And how do you mitigate something like this? Well, there's
1600300	1606960	all the standard ways to mitigate it. Like one, you have to stop using... either use
1607080	1613240	a base tag so that things aren't relative. They have a... things aren't relative. You
1613240	1620180	can make them root relative by adding a forward slash at the beginning, right? Content security
1620180	1626140	policy, all these things, you have to do something. But also educating people, you can't actually
1626140	1635960	trust a file download if it's using a relative... if it's using a relative path. So basic mitigations
1637100	1652000	in place. Yeah. All the... all the basic mitigations, so path handling, you can't... you can't
1652540	1658360	it's important to check if the server that you use... you're using is vulnerable to this
1658360	1663840	type of path handling. If you add a forward slash at the end of the URL and it loses all
1663840	1669660	of its styling, it's a pretty good indication that it has this issue. Default PHP server
1669660	1675340	has this issue. I did make a GitHub for this. If anyone wants to play with it themselves,
1675340	1693290	you can find it on GitHub. Yeah. So this is a novel file injection vector. I figured
1693290	1698790	out a number of server side scripts, shell scripts, PDFs, but there has to be... there
1698790	1706210	has to be dozens more if, you know, people want to explore it. I rarely do bug bounties
1706210	1711170	because I don't work in security. So I do do it sometimes, but it feels like a job to
1711170	1719790	me. So I try not to. I would imagine there's a number of these vulnerabilities that you
1719790	1728410	can go and find if you're so inclined. No one's ever looked for them before. So yeah,
1728410	1734030	I think... I think that there's a lot more that can be done there with the shell scripts
1734030	1742070	on Linux to figure out how to get them to execute. And yeah. I think if any of you do
1742070	1746070	bug bounties, you should go out and try to find it. And that is my talk.
