WEBVTT

00:00.000 --> 00:13.560
So it is my pleasure to welcome Ian Hickey to the stage talking about relative path file injection, the next evolution in relative path.

00:14.000 --> 00:15.520
Okay.

00:16.500 --> 00:18.260
Let's give it up.

00:24.860 --> 00:29.480
Thank you for having me today at the AppSec Village, DEF CON 32.

00:29.480 --> 00:33.400
Thank you for having AppSec Village here as well.

00:34.600 --> 00:36.500
I'm a software developer.

00:36.660 --> 00:38.460
I don't work in security.

00:38.460 --> 00:40.060
I work in ed tech.

00:40.340 --> 00:46.100
But every once in a while I like to spend some brain power looking at some security things.

00:46.100 --> 00:56.900
And the last time I was doing this in April or May, I had a couple of discoveries that I thought were interesting and I'm going to share them with you today.

00:58.240 --> 01:03.480
So many of you know that there's relative path overwrite.

01:09.360 --> 01:43.780
Relative path overwrite is a way to if you can inject CSS onto an HTML page and if a website is vulnerable to path confusion, then you can actually get the default link tag in HTML to interpret the current HTML page as a CSS style sheet and under some circumstances it will execute the CSS.

01:44.420 --> 01:53.240
And older Internet Explorer used to be able to use things like an expression tag and execute JavaScript from CSS.

01:53.260 --> 02:01.060
So it was a pretty ‑‑ it was a pretty interesting way to get JavaScript execution.

02:01.760 --> 02:17.960
So what I've been looking at is port swagger research has these impossible labs and one of these impossible labs I've been looking at for a couple of years and I'll go back and play with different things and see if I can make any progress on it.

02:18.640 --> 02:25.900
In this case it was ‑‑ there's context in HTML where you can't execute JavaScript.

02:25.900 --> 02:27.400
The browser won't let you.

02:27.460 --> 02:33.360
And some of those places are no script within a frame set tag.

02:33.360 --> 02:42.520
It interprets ‑‑ it interprets any HTML that you can inject as just plain text.

02:44.300 --> 02:47.160
And because it's plain text you can't do anything with it.

02:48.060 --> 02:54.660
So the first insight that I had into this was ‑‑ see here's an example.

02:55.600 --> 02:57.220
You have a B tag but it's just text.

02:57.220 --> 02:59.060
You can't display anything.

02:59.220 --> 03:19.960
The first interesting sort of insights I had into this was actually you can execute JavaScript, but you would have to have AngularJS installed and you would have to be using AngularJS and it's been end of life for quite some time.

03:20.800 --> 03:23.000
But there is a way to do it.

03:23.000 --> 03:39.280
So if you have a no frames tag inside of a frame set tag and there happens to be AngularJS on the page, then you can just instantiate AngularJS and execute some JavaScript.

03:39.280 --> 03:40.660
That's cool.

03:41.260 --> 04:06.120
In the original relative path overwrite, if you needed to inject CSS into the page, and again, it didn't matter where you did it, because in both cases, those parsers are not taking into consideration the rules that the browser follows.

04:06.120 --> 04:19.980
Angular will happily execute JavaScript anywhere on the page it finds it and in this case, when it's interpreting a HTML page as a style sheet, it doesn't care where it finds CSS, it will just execute it.

04:20.580 --> 04:30.420
So that was a starting point and then the epiphany was sort of, okay, well, you can use other parsers to execute JavaScript within these contexts where you're not supposed to be able to do it.

04:30.420 --> 04:34.620
So what other parsers are there that will do the same thing?

04:35.140 --> 04:47.500
One thing to note about both of these things is that they're lenient parsers, meaning it does not matter that there's HTML surrounding the thing that they're looking for.

04:47.560 --> 04:51.260
All that it matters is that it eventually finds it.

04:51.260 --> 04:52.460
So that was my starting point.

04:52.460 --> 04:53.240
What else is there?

04:53.240 --> 04:58.760
So I started making a list of all the different types of parsers there are.

04:58.760 --> 05:14.500
There's SVG parsers and XML and PDF and rich text and markdown and after spending more time than I would care to admit with markdown parsers, I started thinking about PDF a lot.

05:14.820 --> 05:17.880
PDF can actually be handwritten.

05:17.880 --> 05:32.620
A lot of people think of it as a binary format and there might be binary mixed in with it, but you can actually just write a PDF file and as plain text.

05:32.620 --> 05:46.400
So if you have plain text injection like we're talking about here in one of these contexts such as no script, no frames, then you can inject PDF.

05:46.460 --> 05:48.560
Well, PDF can execute JavaScript.

05:48.560 --> 05:51.060
PDF can submit forms.

05:51.060 --> 05:55.520
PDF can ‑‑ there's tons of research into malicious PDFs.

05:56.340 --> 06:14.760
So I started playing with it and the second big epiphany that I had that just really surprised the shit out of me is that PDFs are also super lenient because PDFs start with these magic characters and end with magic characters.

06:14.760 --> 06:17.660
It doesn't really care what it finds before and after that.

06:17.660 --> 06:30.400
So you can actually take this HTML document with PDF persistently injected into it and you can save this as a PDF and it will actually execute.

06:30.680 --> 06:33.360
And this is what this is here.

06:33.360 --> 06:41.460
This is just an embedded HTML file renamed as PDF and it displays as a PDF and it executes JavaScript.

06:43.600 --> 06:53.900
How many people, how are you going to convince people to download an HTML file as a PDF?

06:54.000 --> 07:00.080
Like are you going to put something on the page that says right click here, save it as whatever?

07:00.080 --> 07:01.620
They just won't do it.

07:01.760 --> 07:14.460
So I started thinking about ways to get people to download these PDFs without me having to explicitly convince them of anything.

07:15.100 --> 07:28.320
And when I started thinking about the original relative path overwrite, which interprets the current page as a CSS style sheet, well the reason why it does that is because the browsers are stupid.

07:28.320 --> 07:31.740
They just, whatever you return back from the server it's going to use.

07:31.740 --> 07:47.520
And if you happen to be using a server that when you add a forward slash at the ends returns the current page back, well if the current page happens to have PDF injected into it then that's what you're going to get.

07:50.340 --> 07:52.520
So that's exactly what I did.

07:52.520 --> 08:09.680
I'm going to show you how it works now and before we even get into it, just so everyone sees because it's easier to simply see what's happening than to explain it.

08:09.860 --> 08:13.440
Someone goes to a website.

08:13.520 --> 08:21.480
There is a download link on the website and the download link happens to use a relative path.

08:21.920 --> 08:30.100
Well if you click this download link all that's going to happen is it's going to download whatever the file was that you intended to download.

08:30.600 --> 08:40.160
And if we open this up, all this is, is, oop, and if we open this up,

08:43.830 --> 08:51.550
all this is, is, you know, oh sorry, sorry.

08:52.430 --> 08:54.930
Let's see if I can make that bigger for you.

08:55.910 --> 08:58.250
Oop, oop,

09:04.530 --> 09:06.510
one second.

09:06.510 --> 09:08.670
Okay, good PDF download.

09:08.670 --> 09:13.190
So I've downloaded this PDF and I open this PDF.

09:13.190 --> 09:17.030
I'm going to open it with Google Chrome.

09:17.030 --> 09:19.250
It's just a normal harmless PDF.

09:19.310 --> 09:20.850
No big deal.

09:20.850 --> 09:29.830
However, all you have to do is add a forward slash at the end and you notice it lost its styling.

09:29.830 --> 09:33.410
The reason it lost its styling is because now the browser is confused.

09:33.650 --> 09:47.750
If you look in the, if you look at what it's actually trying to include as the style sheet, what you find now is it's actually getting the current page.

09:47.850 --> 09:50.010
That's relative path over it.

09:50.050 --> 09:53.190
It's confused so it's returning this back.

09:53.210 --> 10:02.750
The effect of that, once it loses its styling, is that if you now click this, it's actually going to return the current page as the PDF.

10:03.970 --> 10:12.490
Which means when someone opens this, it's not going to be the same PDF that, that they expected to open.

10:12.490 --> 10:14.850
Instead,

10:18.520 --> 10:20.400
it's going to execute JavaScript.

10:21.140 --> 10:23.620
So what just happened?

10:23.620 --> 10:26.520
Where did, where did the new PDF come from?

10:26.520 --> 10:28.580
Well, the PDF lives on the server.

10:28.760 --> 10:40.440
Because the HTML lives on the server and you've persistently saved PDF content into that HTML page, right?

10:40.440 --> 10:50.880
And it's sufficiently confused so when they go to download, it downloads what you want them to download instead of whatever they were trying to download.

10:50.880 --> 10:51.920
I mean, this is dangerous.

10:51.920 --> 10:58.920
If you think about it, what if this was a, what if this was a text PDF site or something?

10:59.180 --> 11:02.060
It's not going to be just one link that this happens with.

11:02.060 --> 11:05.100
There will be every link on the page that uses a relative download.

11:05.100 --> 11:09.920
As soon as they click it, it's going to download whatever that PDF is.

11:10.540 --> 11:13.820
So the things that you can do with PDF are pretty interesting, right?

11:13.980 --> 11:20.360
Depending on the PDF reader that the user happens to be using.

11:20.860 --> 11:33.240
So you have, you have something like Fox Pro, I think it's called, that allows you to do commands when the PDF opens, right?

11:33.240 --> 11:34.820
You can execute shell commands.

11:34.820 --> 11:47.200
So there's nothing stopping you to simply populate your PDF with a launch command that executes shell commands as long as the user has permission to be doing it to do whatever you want, right?

11:47.200 --> 11:54.220
You can run scripts, you can download new scripts, you can, you know, whatever.

11:54.340 --> 12:05.480
In fact, if you want to stay in the web context, I imagine you could probably open a Chrome browser headless with the user's data set as their data and then just go explore the Internet as them, right?

12:05.480 --> 12:07.520
Because why couldn't you?

12:07.660 --> 12:10.400
So it's pretty dangerous.

12:10.400 --> 12:30.060
But thinking this through a little bit more, it's interesting that you can do it with PDF and all I did here was this would be, you know, say it's a profile page and you have some public, you know, blurb about yourself that you've put this PDF in.

12:30.060 --> 12:36.660
However you get the persistent content injection onto the page.

12:36.660 --> 12:46.440
But as long as it's there in some capacity and as long as you can save it as a PDF, which we can because there's a simple download link, then it'll execute.

12:46.580 --> 12:57.120
So in the original relative path overwrite, what you needed was a link tag with a relative path to the style sheet.

12:57.120 --> 13:05.460
In this case, you need an anchor tag with a relative path to the PDF.

13:05.760 --> 13:09.560
And if it's not HTML, the download has to be fully qualified.

13:09.560 --> 13:18.000
Meaning if you don't have sample PDF here as download, then it won't accept downloading the HTML as a PDF.

13:18.360 --> 13:21.940
The content disposition will be wrong.

13:22.120 --> 13:26.800
So that's the basic requirement for executing this.

13:26.800 --> 13:27.760
It has to be a page.

13:27.760 --> 13:38.740
So think like a, you know, PDF library or, you know, places where you can inject content and then it says, save your whole history or something like that.

13:41.220 --> 13:50.460
But this technique is going to be more useful the more file types that we find that you can do this with, right?

13:50.460 --> 14:00.620
So PDF is great if they happen to be using a PDF viewer that supports executing shell commands, et cetera.

14:01.720 --> 14:07.140
But there has to be other lenient parsers and lenient file types that you can use to do similar things.

14:07.140 --> 14:10.740
So then I started exploring that.

14:18.940 --> 14:26.540
So Gareth Hayes, I was talking to him about this just to make sure that he hadn't heard of it being discovered before either.

14:26.780 --> 14:34.600
He suggested I talk to a gentleman by the name of Orange Albertini who specializes in polyglot file types.

14:34.760 --> 14:36.200
He's a maven.

14:36.200 --> 14:44.240
He's the type of guy that his business card, you can put it into a Super Nintendo emulator.

14:44.600 --> 14:47.840
And even though it's a PDF, it will still play in the emulator.

14:47.840 --> 14:49.000
It's crazy.

14:49.180 --> 14:56.640
And we were brainstorming other types of lenient file types where this sort of thing might work with.

14:56.940 --> 15:00.480
And we went, you know, zip file types to tar.

15:00.540 --> 15:03.460
Tar is another like plain text one that you can sort of hand write.

15:03.460 --> 15:06.260
And it took me down this road looking at that.

15:06.260 --> 15:09.940
And while I was exploring it, I was thinking about, well, what would you put in a shell scripts?

15:09.940 --> 15:18.800
And then anyone who's dealt with enough shell scripts knows that shell scripts can actually be really lenient when they're run too.

15:18.920 --> 15:46.080
So what this means is if you happen to be what the user is trying to download is a shell script and you have some sort of content injection, you can actually get them to get them to download a shell script you know, whatever, PowerShell file or a batch file or,

15:46.080 --> 15:49.200
you know, batch scripts.

15:49.200 --> 16:10.840
And in some Linux distributions, and I'm looking at especially Windows WSL2 with U12, et cetera, with some of these things, it'll actually happily execute an HTML file as a shell script.

16:10.840 --> 16:16.340
And it will error on every single line until it gets to something that it understands and then it will execute it.

16:16.340 --> 16:29.860
But if someone's going to a site looking to download a shell script, it's going to say, hey, download this code and then run it as a super user, right, because that's what they always say.

16:30.000 --> 16:47.460
So if they do that, and the link that they clicked happens to have a forward slash at the end, it's susceptible to, you know, path confusion, et cetera, then the script that they get is an HTML page, and when they run it, it's going to run.

16:47.800 --> 17:00.000
And what that looks like is... what that looks like is...

17:00.020 --> 17:14.680
something like this, where every single line, HTML, the head, all of it, errors, line by line by line, and then it gets to echo hello world, and it executes it, and then it exits, and that's it.

17:14.680 --> 17:17.800
By the time they realize something happened, it's going to be too late, right?

17:19.180 --> 17:22.660
So I managed to get this working with WSL2.

17:23.460 --> 17:28.780
I managed to get it working with PowerShell, which does the exact same thing, right?

17:28.780 --> 17:40.420
It'll execute every single line of the HTML file, because it's a PS1 file or whatever, and then it gets to hello world, it executes it, and then it continues, you know, just executing nonsense.

17:40.420 --> 17:56.060
The closest I could get it with regular Linux shells is... you have to be able to inject something right after HTML, so it's far-fetched, but I threw it in there in case anyone has any ideas on how to get that working.

17:56.580 --> 18:19.680
I did actually get this similar thing working on OSX and Linux, if a user's using, you know, BoxPro or whatever it is, because you can have a launch command that simply reads whatever it is in its own file, and then, you know, executes it line by line.

18:19.680 --> 18:22.160
So it can be done.

18:22.160 --> 18:23.220
It can be done.

18:24.140 --> 18:30.220
And so that's... so now we have two, three, four types of files.

18:30.740 --> 18:33.540
Ideally, we would have dozens, hundreds.

18:33.540 --> 18:38.020
There's so many file types out there to get this type of stuff working well.

18:39.560 --> 18:44.640
So I kept on... I kept on investigating other things.

18:44.820 --> 18:47.600
What does not mind there being HTML, right?

18:47.600 --> 18:54.300
What will someone simply download from the Internet and then throw into something and have it execute, right?

18:54.340 --> 19:03.200
So pretty much all server-side... server-side templating languages are like this, right?

19:03.200 --> 19:24.380
So if someone's looking for, you know, a Ruby or whatever, a ColdFusion script or anything else, you know, they're probably going to grab it and then just throw it into whatever it is to test it, at which point it's already executed anything it needs to execute,

19:24.380 --> 19:24.660
right?

19:24.660 --> 19:31.580
And the cool thing there is that any of the server-side languages, they don't... they're expecting there to be HTML, right?

19:31.580 --> 19:36.700
They're expecting there to be all this other stuff in there, so they won't complain.

19:37.620 --> 19:42.440
And so these are the ones that I found that it would work with out-of-box, right?

19:42.440 --> 19:44.620
So you have this PHP.

19:45.480 --> 19:52.260
Similar thing right here, if we look at this vulnerable PHP.

19:53.280 --> 20:16.280
So all this is going to do, if we go here, is the safe download... let's go to the PHP file, open it up, show you what this has generated.

20:18.400 --> 20:24.500
And what this did is because it's server-side, it already executed this, and it says welcome user, no big deal.

20:25.560 --> 20:34.700
We do it the other way, forward slash, all of a sudden they download it.

20:42.080 --> 20:44.940
This time, welcome bad guy, right?

20:44.940 --> 20:55.620
So there's nothing stopping you, it doesn't have to be bad guy, it could be read the contents of a password file, whatever, send it somewhere, anything you want it to.

20:55.620 --> 21:09.540
If you can get people... the thing that's sort of insidious about this, even though there's like user interaction that could take place, is that the user's going to a server that's trusted, right?

21:09.540 --> 21:16.800
There's nothing untrusted about this server, they've been to it a thousand times, and they're clicking a download link.

21:16.940 --> 21:22.320
There's no difference if you were able to just break into the server and replace the file, right?

21:22.320 --> 21:24.440
Because the effect's the same.

21:27.340 --> 21:39.180
But what would be really interesting is if you didn't have to do this whole download portion of it at all, right?

21:39.180 --> 21:43.640
And that's the hardest part, is how do you get someone to...

21:43.640 --> 21:52.100
you know, if someone's going to go download a PDF, click on a link and download a PDF, there's a high chance that they're going to go look at the PDF, right?

21:52.100 --> 21:56.560
I mean, that's what it's intended for, they're going to trust that whatever's in there.

21:56.560 --> 22:01.580
It should be, you make a PDF that looks like a tax document and submits back to your server, whatever.

22:02.900 --> 22:07.740
But it would be great if they didn't have to download the thing in the first place.

22:07.740 --> 22:10.500
So I started thinking about how that could possibly work.

22:11.080 --> 22:13.560
And that's pretty tricky, right?

22:15.030 --> 22:24.720
But there's been a rise in all of these HTML over the wire sort of frameworks.

22:24.720 --> 22:28.140
There's Livewire, there's Hotwire, there's HTMX.

22:28.140 --> 22:40.340
And all of these things, what they're doing is they're getting HTML from the server instead of putting all the HTML client side, right?

22:40.340 --> 22:51.760
And the HTML they get from the server could be, you know, pre-rendered with, like, the user's profile picture and, you know, username or something like that.

22:52.240 --> 23:03.560
But they're going to suffer the same type of issue if they happen to be on a server that returns the current page when there's forward slash at the end.

23:03.660 --> 23:09.820
So something like that, what it would look like is...

23:16.180 --> 23:18.280
so I'm not using any of the frameworks.

23:18.280 --> 23:20.280
This is just straight jQuery.

23:22.020 --> 23:42.100
But what this does is when the page loads, it attempts to get some DOM element and then inject it into this main div, right?

23:42.360 --> 23:44.600
And again, it's the same thing.

23:44.600 --> 23:51.100
All we're doing is, you know, doing a... you don't need to do this.

23:52.280 --> 23:57.260
All it's doing is just, you know, using jQuery HTML, just setting the HTML.

23:57.260 --> 23:58.340
No big deal.

23:58.340 --> 24:01.920
And if we go look at what this script does...

24:07.040 --> 24:08.600
what's that?

24:08.960 --> 24:10.800
There's nothing on this page.

24:10.800 --> 24:13.520
There's literally nothing on this page.

24:15.280 --> 24:18.980
So all this does is it doesn't do anything initially.

24:20.080 --> 24:27.560
You can see when it goes to get the... let me see if I can make this bigger.

24:29.400 --> 24:30.680
Excuse me?

24:31.720 --> 24:33.400
Oh, got you, got you.

24:33.580 --> 24:37.720
So nothing too interesting is happening on this page yet.

24:37.720 --> 24:38.900
It gets jQuery.

24:39.180 --> 24:43.340
It reaches out and it gets this thing that says welcome user.

24:43.340 --> 24:44.120
No big deal.

24:44.120 --> 24:46.820
And then it tries to inject the welcome user into the page.

24:46.960 --> 24:57.540
But you add a forward slash on to the end and now it actually gets the current page, vulnerable HTML.

25:04.500 --> 25:07.200
And... oh, one second.

25:07.720 --> 25:09.380
Let's see what just happened.

25:11.850 --> 25:13.230
What did I do?

25:14.450 --> 25:15.970
Oh, there it goes.

25:15.970 --> 25:29.270
So in this case what just happened is when it reached out to get it, the ID that it found was for a frame set.

25:29.430 --> 25:44.630
And HTML is probably the most lenient parser of all because it has this really weird behavior where if you try to inject HTML tags into other HTML tags that it doesn't like, it will simply strip those tags away.

25:44.630 --> 25:53.930
And that's exactly what happened here is because when it found... oops.

26:00.000 --> 26:01.640
It's not letting me leave.

26:01.800 --> 26:02.820
It's not letting me leave.

26:02.820 --> 26:03.300
Okay.

26:03.740 --> 26:04.460
Oops.

26:05.260 --> 26:17.620
Yeah, so when it found the... this frame set, it... when it injected it into this main tag, it actually stripped the frame set away.

26:17.620 --> 26:28.500
So even though the browser is saying hey, this frame set is completely innocuous, we're just going to treat it like text, the framework itself can take that and inject it automatically.

26:28.660 --> 26:38.100
So there's places where maybe you weren't concerned about people getting content injection before, but now you have to be worried about it.

26:38.100 --> 26:39.860
And how do you mitigate something like this?

26:39.860 --> 26:43.580
Well, there's all the standard ways to mitigate it.

26:43.580 --> 26:49.960
Like one, you have to stop using... either use a base tag so that things aren't relative.

26:49.960 --> 26:52.460
They have a... things aren't relative.

26:52.900 --> 26:57.820
You can make them root relative by adding a forward slash at the beginning, right?

26:59.260 --> 27:03.620
Content security policy, all these things, you have to do something.

27:03.620 --> 27:12.960
But also educating people, you can't actually trust a file download if it's using a relative... if it's using a relative path.

27:13.560 --> 27:19.320
So basic mitigations in place.

27:22.720 --> 27:24.240
Yeah.

27:25.940 --> 27:32.000
All the... all the basic mitigations, so path handling, you can't...

27:32.000 --> 27:39.580
you can't it's important to check if the server that you use... you're using is vulnerable to this type of path handling.

27:39.580 --> 27:47.880
If you add a forward slash at the end of the URL and it loses all of its styling, it's a pretty good indication that it has this issue.

27:47.880 --> 27:51.200
Default PHP server has this issue.

27:51.740 --> 27:53.520
I did make a GitHub for this.

27:53.520 --> 27:59.520
If anyone wants to play with it themselves, you can find it on GitHub.

28:03.210 --> 28:04.050
Yeah.

28:04.050 --> 28:11.030
So this is a novel file injection vector.

28:12.470 --> 28:24.010
I figured out a number of server side scripts, shell scripts, PDFs, but there has to be... there has to be dozens more if, you know, people want to explore it.

28:24.330 --> 28:28.130
I rarely do bug bounties because I don't work in security.

28:28.130 --> 28:31.830
So I do do it sometimes, but it feels like a job to me.

28:32.290 --> 28:34.890
So I try not to.

28:34.890 --> 28:42.630
I would imagine there's a number of these vulnerabilities that you can go and find if you're so inclined.

28:42.630 --> 28:44.830
No one's ever looked for them before.

28:47.110 --> 28:57.070
So yeah, I think... I think that there's a lot more that can be done there with the shell scripts on Linux to figure out how to get them to execute.

28:58.490 --> 29:00.210
And yeah.

29:00.250 --> 29:04.470
I think if any of you do bug bounties, you should go out and try to find it.

29:04.870 --> 29:06.070
And that is my talk.

