[00:00.000 --> 00:07.980] So it is my pleasure to welcome Ian Hickey to the stage talking about relative path [00:07.980 --> 00:18.260] file injection, the next evolution in relative path. Okay. Let's give it up. [00:24.860 --> 00:28.320] Thank you for having me today at the AppSec Village, [00:28.320 --> 00:33.400] DEF CON 32. Thank you for having AppSec Village here as well. [00:34.600 --> 00:41.100] I'm a software developer. I don't work in security. I work in ed tech. But every once [00:41.100 --> 00:46.500] in a while I like to spend some brain power looking at some security things. And the last [00:46.500 --> 00:53.800] time I was doing this in April or May, I had a couple of discoveries that I thought were [00:53.800 --> 01:00.220] interesting and I'm going to share them with you today. So many of you know that there's [01:00.580 --> 01:16.140] relative path overwrite. Relative path overwrite is a way to if you can inject CSS onto an [01:16.140 --> 01:25.960] HTML page and if a website is vulnerable to path confusion, then you can actually get [01:25.960 --> 01:36.700] the default link tag in HTML to interpret the current HTML page as a CSS style sheet [01:38.100 --> 01:48.000] and under some circumstances it will execute the CSS. And older Internet Explorer used [01:48.000 --> 01:53.860] to be able to use things like an expression tag and execute JavaScript from CSS. So it [01:53.860 --> 02:02.780] was a pretty ‑‑ it was a pretty interesting way to get JavaScript execution. So what I've [02:02.780 --> 02:10.820] been looking at is port swagger research has these impossible labs and one of these impossible [02:10.820 --> 02:14.720] labs I've been looking at for a couple of years and I'll go back and play with different [02:14.720 --> 02:20.460] things and see if I can make any progress on it. In this case it was ‑‑ there's [02:20.460 --> 02:28.060] context in HTML where you can't execute JavaScript. The browser won't let you. And some of those [02:28.060 --> 02:39.900] places are no script within a frame set tag. It interprets ‑‑ it interprets any HTML [02:39.900 --> 02:46.300] that you can inject as just plain text. And because it's plain text you can't do anything [02:46.300 --> 02:54.660] with it. So the first insight that I had into this was ‑‑ see here's an example. [02:55.600 --> 03:04.760] You have a B tag but it's just text. You can't display anything. The first interesting sort [03:04.760 --> 03:11.880] of insights I had into this was actually you can execute JavaScript, but you would have [03:11.880 --> 03:18.040] to have AngularJS installed and you would have to be using AngularJS and it's been end [03:18.040 --> 03:25.740] of life for quite some time. But there is a way to do it. So if you have a no frames [03:25.740 --> 03:33.520] tag inside of a frame set tag and there happens to be AngularJS on the page, then you can [03:33.520 --> 03:44.120] just instantiate AngularJS and execute some JavaScript. That's cool. In the original relative [03:44.120 --> 03:53.620] path overwrite, if you needed to inject CSS into the page, and again, it didn't matter [03:53.620 --> 04:02.460] where you did it, because in both cases, those parsers are not taking into consideration [04:02.460 --> 04:09.860] the rules that the browser follows. Angular will happily execute JavaScript anywhere on [04:09.860 --> 04:16.900] the page it finds it and in this case, when it's interpreting a HTML page as a style sheet, [04:16.900 --> 04:21.660] it doesn't care where it finds CSS, it will just execute it. So that was a starting point [04:21.660 --> 04:26.680] and then the epiphany was sort of, okay, well, you can use other parsers to execute JavaScript [04:27.760 --> 04:31.480] within these contexts where you're not supposed to be able to do it. So what other parsers [04:31.480 --> 04:37.320] are there that will do the same thing? One thing to note about both of these things is [04:37.320 --> 04:45.840] that they're lenient parsers, meaning it does not matter that there's HTML surrounding the [04:45.840 --> 04:51.400] thing that they're looking for. All that it matters is that it eventually finds it. So [04:51.400 --> 04:57.220] that was my starting point. What else is there? So I started making a list of all the different [04:57.220 --> 05:04.980] types of parsers there are. There's SVG parsers and XML and PDF and rich text and markdown [05:04.980 --> 05:12.440] and after spending more time than I would care to admit with markdown parsers, I started [05:12.440 --> 05:18.860] thinking about PDF a lot. PDF can actually be handwritten. A lot of people think of it [05:18.860 --> 05:22.540] as a binary format and there might be binary mixed in with it, but you can actually just [05:22.540 --> 05:35.740] write a PDF file and as plain text. So if you have plain text injection like we're talking [05:35.740 --> 05:42.880] about here in one of these contexts such as no script, no frames, then you can inject [05:45.080 --> 05:52.200] PDF. Well, PDF can execute JavaScript. PDF can submit forms. PDF can ‑‑ there's [05:52.540 --> 05:59.480] tons of research into malicious PDFs. So I started playing with it and the second [05:59.480 --> 06:08.440] big epiphany that I had that just really surprised the shit out of me is that PDFs are also super [06:08.440 --> 06:14.760] lenient because PDFs start with these magic characters and end with magic characters. [06:14.760 --> 06:18.960] It doesn't really care what it finds before and after that. So you can actually take this [06:18.960 --> 06:26.840] HTML document with PDF persistently injected into it and you can save this as a PDF and [06:26.840 --> 06:36.000] it will actually execute. And this is what this is here. This is just an embedded HTML [06:36.300 --> 06:45.980] file renamed as PDF and it displays as a PDF and it executes JavaScript. How many people, [06:45.980 --> 06:55.140] how are you going to convince people to download an HTML file as a PDF? Like are you going [06:55.140 --> 07:00.520] to put something on the page that says right click here, save it as whatever? They just [07:00.520 --> 07:10.920] won't do it. So I started thinking about ways to get people to download these PDFs without [07:10.920 --> 07:17.860] me having to explicitly convince them of anything. And when I started thinking about the original [07:17.860 --> 07:24.000] relative path overwrite, which interprets the current page as a CSS style sheet, well [07:24.000 --> 07:29.480] the reason why it does that is because the browsers are stupid. They just, whatever you [07:29.480 --> 07:33.620] return back from the server it's going to use. And if you happen to be using a server [07:34.320 --> 07:40.300] that when you add a forward slash at the ends returns the current page back, well if the [07:40.300 --> 07:47.520] current page happens to have PDF injected into it then that's what you're going to get. [07:50.340 --> 07:58.320] So that's exactly what I did. I'm going to show you how it works now and before we even [07:58.320 --> 08:03.880] get into it, just so everyone sees because it's easier to simply see what's happening [08:04.360 --> 08:16.380] than to explain it. Someone goes to a website. There is a download link on the website and [08:16.380 --> 08:24.220] the download link happens to use a relative path. Well if you click this download link [08:24.220 --> 08:29.020] all that's going to happen is it's going to download whatever the file was that you intended [08:29.020 --> 08:44.330] to download. And if we open this up, all this is, is, oop, and if we open this up, all this [08:44.330 --> 08:58.250] is, is, you know, oh sorry, sorry. Let's see if I can make that bigger for you. Oop, oop, [09:04.530 --> 09:13.410] one second. Okay, good PDF download. So I've downloaded this PDF and I open this PDF. I'm [09:13.410 --> 09:22.350] going to open it with Google Chrome. It's just a normal harmless PDF. No big deal. However, [09:22.350 --> 09:29.830] all you have to do is add a forward slash at the end and you notice it lost its styling. [09:29.830 --> 09:35.450] The reason it lost its styling is because now the browser is confused. If you look in [09:35.450 --> 09:45.470] the, if you look at what it's actually trying to include as the style sheet, what you find [09:45.470 --> 09:50.970] now is it's actually getting the current page. That's relative path over it. It's confused [09:50.970 --> 09:57.790] so it's returning this back. The effect of that, once it loses its styling, is that if [09:57.790 --> 10:04.570] you now click this, it's actually going to return the current page as the PDF. Which [10:04.570 --> 10:11.510] means when someone opens this, it's not going to be the same PDF that, that they expected [10:11.510 --> 10:24.620] to open. Instead, it's going to execute JavaScript. So what just happened? Where did, where did [10:24.640 --> 10:31.320] the new PDF come from? Well, the PDF lives on the server. Because the HTML lives on the [10:31.320 --> 10:41.380] server and you've persistently saved PDF content into that HTML page, right? And it's sufficiently [10:41.380 --> 10:47.400] confused so when they go to download, it downloads what you want them to download instead of [10:48.000 --> 10:52.680] whatever they were trying to download. I mean, this is dangerous. If you think about it, [10:52.680 --> 11:00.060] what if this was a, what if this was a text PDF site or something? It's not going to be [11:00.060 --> 11:04.420] just one link that this happens with. There will be every link on the page that uses a [11:04.420 --> 11:10.940] relative download. As soon as they click it, it's going to download whatever that PDF is. [11:10.940 --> 11:15.880] So the things that you can do with PDF are pretty interesting, right? Depending on the [11:15.880 --> 11:27.120] PDF reader that the user happens to be using. So you have, you have something like Fox Pro, [11:27.120 --> 11:33.520] I think it's called, that allows you to do commands when the PDF opens, right? You can [11:33.520 --> 11:39.100] execute shell commands. So there's nothing stopping you to simply populate your PDF with [11:39.240 --> 11:45.120] a launch command that executes shell commands as long as the user has permission to be doing [11:45.120 --> 11:51.480] it to do whatever you want, right? You can run scripts, you can download new scripts, [11:51.480 --> 11:56.580] you can, you know, whatever. In fact, if you want to stay in the web context, I imagine [11:56.580 --> 12:03.060] you could probably open a Chrome browser headless with the user's data set as their data and [12:03.060 --> 12:09.660] then just go explore the Internet as them, right? Because why couldn't you? So it's pretty [12:09.660 --> 12:17.300] dangerous. But thinking this through a little bit more, it's interesting that you can do [12:17.300 --> 12:23.840] it with PDF and all I did here was this would be, you know, say it's a profile page and [12:23.840 --> 12:30.340] you have some public, you know, blurb about yourself that you've put this PDF in. However [12:30.920 --> 12:38.140] you get the persistent content injection onto the page. But as long as it's there in some [12:38.140 --> 12:43.560] capacity and as long as you can save it as a PDF, which we can because there's a simple [12:43.560 --> 12:49.940] download link, then it'll execute. So in the original relative path overwrite, what you [12:49.940 --> 12:58.300] needed was a link tag with a relative path to the style sheet. In this case, you need [12:58.300 --> 13:07.980] an anchor tag with a relative path to the PDF. And if it's not HTML, the download has [13:07.980 --> 13:14.560] to be fully qualified. Meaning if you don't have sample PDF here as download, then it [13:14.560 --> 13:22.420] won't accept downloading the HTML as a PDF. The content disposition will be wrong. So [13:23.100 --> 13:28.700] that's the basic requirement for executing this. It has to be a page. So think like a, [13:29.000 --> 13:36.240] you know, PDF library or, you know, places where you can inject content and then it says, [13:36.240 --> 13:45.200] save your whole history or something like that. But this technique is going to be more [13:45.200 --> 13:53.680] useful the more file types that we find that you can do this with, right? So PDF is great [13:54.200 --> 14:00.620] if they happen to be using a PDF viewer that supports executing shell commands, et cetera. [14:01.720 --> 14:05.960] But there has to be other lenient parsers and lenient file types that you can use to [14:05.960 --> 14:21.780] do similar things. So then I started exploring that. So Gareth Hayes, I was talking to him [14:21.780 --> 14:26.540] about this just to make sure that he hadn't heard of it being discovered before either. [14:26.780 --> 14:32.700] He suggested I talk to a gentleman by the name of Orange Albertini who specializes in [14:32.700 --> 14:40.260] polyglot file types. He's a maven. He's the type of guy that his business card, you can [14:40.260 --> 14:46.860] put it into a Super Nintendo emulator. And even though it's a PDF, it will still play [14:46.860 --> 14:54.900] in the emulator. It's crazy. And we were brainstorming other types of lenient file types where this [14:54.900 --> 15:01.160] sort of thing might work with. And we went, you know, zip file types to tar. Tar is another [15:01.160 --> 15:05.480] like plain text one that you can sort of hand write. And it took me down this road looking [15:05.480 --> 15:08.820] at that. And while I was exploring it, I was thinking about, well, what would you put in [15:09.000 --> 15:15.440] a shell scripts? And then anyone who's dealt with enough shell scripts knows that shell [15:15.440 --> 15:22.460] scripts can actually be really lenient when they're run too. So what this means is if [15:24.280 --> 15:29.700] you happen to be what the user is trying to download is a shell script and you have some [15:29.700 --> 15:38.800] sort of content injection, you can actually get them to get them to download a shell script [15:38.820 --> 15:51.340] you know, whatever, PowerShell file or a batch file or, you know, batch scripts. And in some [15:51.340 --> 16:03.540] Linux distributions, and I'm looking at especially Windows WSL2 with U12, et cetera, with some [16:03.540 --> 16:11.260] of these things, it'll actually happily execute an HTML file as a shell script. And it will [16:11.260 --> 16:15.260] error on every single line until it gets to something that it understands and then it [16:15.260 --> 16:21.900] will execute it. But if someone's going to a site looking to download a shell script, [16:22.560 --> 16:28.460] it's going to say, hey, download this code and then run it as a super user, right, because [16:28.460 --> 16:34.640] that's what they always say. So if they do that, and the link that they clicked happens [16:34.640 --> 16:41.920] to have a forward slash at the end, it's susceptible to, you know, path confusion, et cetera, then [16:42.600 --> 16:48.140] the script that they get is an HTML page, and when they run it, it's going to run. And [16:48.140 --> 17:00.000] what that looks like is... what that looks like is... [17:00.020 --> 17:08.400] something like this, where every single line, HTML, the head, all of it, errors, line by [17:08.400 --> 17:13.920] line by line, and then it gets to echo hello world, and it executes it, and then it exits, [17:13.920 --> 17:17.800] and that's it. By the time they realize something happened, it's going to be too late, right? [17:19.180 --> 17:26.540] So I managed to get this working with WSL2. I managed to get it working with PowerShell, [17:26.540 --> 17:31.440] which does the exact same thing, right? It'll execute every single line of the HTML file, [17:31.440 --> 17:37.620] because it's a PS1 file or whatever, and then it gets to hello world, it executes it, and [17:37.620 --> 17:42.080] then it continues, you know, just executing nonsense. The closest I could get it with [17:42.660 --> 17:48.960] regular Linux shells is... you have to be able to inject something right after HTML, [17:48.960 --> 17:54.920] so it's far-fetched, but I threw it in there in case anyone has any ideas on how to get [17:54.920 --> 18:04.540] that working. I did actually get this similar thing working on OSX and Linux, if a user's [18:04.540 --> 18:12.220] using, you know, BoxPro or whatever it is, because you can have a launch command that [18:12.220 --> 18:19.680] simply reads whatever it is in its own file, and then, you know, executes it line by line. [18:19.680 --> 18:28.640] So it can be done. It can be done. And so that's... so now we have two, three, four [18:28.640 --> 18:35.200] types of files. Ideally, we would have dozens, hundreds. There's so many file types out there [18:35.200 --> 18:43.660] to get this type of stuff working well. So I kept on... I kept on investigating other [18:43.660 --> 18:50.040] things. What does not mind there being HTML, right? What will someone simply download from [18:50.040 --> 18:55.380] the Internet and then throw into something and have it execute, right? So pretty much [18:55.380 --> 19:05.640] all server-side... server-side templating languages are like this, right? So if someone's [19:05.640 --> 19:13.640] looking for, you know, a Ruby or whatever, a ColdFusion script or anything else, you [19:14.500 --> 19:20.160] know, they're probably going to grab it and then just throw it into whatever it is to [19:20.160 --> 19:25.460] test it, at which point it's already executed anything it needs to execute, right? And the [19:25.460 --> 19:30.060] cool thing there is that any of the server-side languages, they don't... they're expecting [19:30.060 --> 19:35.100] there to be HTML, right? They're expecting there to be all this other stuff in there, [19:35.100 --> 19:41.080] so they won't complain. And so these are the ones that I found that it would work with [19:41.080 --> 19:48.140] out-of-box, right? So you have this PHP. Similar thing right here, if we look at this [19:50.580 --> 20:11.060] vulnerable PHP. So all this is going to do, if we go here, is the safe download... let's [20:11.060 --> 20:19.400] go to the PHP file, open it up, show you what this has generated. And what this did [20:19.400 --> 20:24.500] is because it's server-side, it already executed this, and it says welcome user, no big deal. [20:25.560 --> 20:43.460] We do it the other way, forward slash, all of a sudden they download it. This time, welcome [20:43.460 --> 20:48.360] bad guy, right? So there's nothing stopping you, it doesn't have to be bad guy, it could [20:48.360 --> 20:54.680] be read the contents of a password file, whatever, send it somewhere, anything you [20:54.680 --> 21:00.980] want it to. If you can get people... the thing that's sort of insidious about this, even [21:00.980 --> 21:06.160] though there's like user interaction that could take place, is that the user's going [21:06.160 --> 21:12.360] to a server that's trusted, right? There's nothing untrusted about this server, they've [21:12.360 --> 21:17.740] been to it a thousand times, and they're clicking a download link. There's no difference [21:18.420 --> 21:23.380] if you were able to just break into the server and replace the file, right? Because the effect's [21:23.380 --> 21:36.860] the same. But what would be really interesting is if you didn't have to do this whole download [21:36.860 --> 21:43.640] portion of it at all, right? And that's the hardest part, is how do you get someone to... [21:43.640 --> 21:49.080] you know, if someone's going to go download a PDF, click on a link and download a PDF, [21:49.080 --> 21:53.060] there's a high chance that they're going to go look at the PDF, right? I mean, that's [21:53.060 --> 21:57.520] what it's intended for, they're going to trust that whatever's in there. It should be, you [21:57.520 --> 22:01.580] 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:13.560] So I started thinking about how that could possibly work. And that's pretty tricky, right? [22:15.030 --> 22:24.880] But there's been a rise in all of these HTML over the wire sort of frameworks. There's [22:24.880 --> 22:31.640] Livewire, there's Hotwire, there's HTMX. And all of these things, what they're doing is [22:31.640 --> 22:40.340] they're getting HTML from the server instead of putting all the HTML client side, right? [22:40.340 --> 22:44.940] And the HTML they get from the server could be, you know, pre-rendered with, like, the [22:44.940 --> 22:53.540] user's profile picture and, you know, username or something like that. But they're going [22:53.540 --> 22:59.460] to suffer the same type of issue if they happen to be on a server that returns the [22:59.460 --> 23:03.560] current page when there's forward slash at the end. [23:03.660 --> 23:18.280] So something like that, what it would look like is... so I'm not using any of the frameworks. [23:18.280 --> 23:29.320] This is just straight jQuery. But what this does is when the page loads, it attempts to [23:30.340 --> 23:44.000] get some DOM element and then inject it into this main div, right? And again, it's the [23:44.000 --> 23:52.880] same thing. All we're doing is, you know, doing a... you don't need to do this. All [23:52.880 --> 23:58.340] it's doing is just, you know, using jQuery HTML, just setting the HTML. No big deal. [23:58.340 --> 24:10.800] And if we go look at what this script does... what's that? There's nothing on this page. [24:10.800 --> 24:18.980] There's literally nothing on this page. 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:37.360] Excuse me? Oh, got you, got you. So nothing too interesting is happening on this page [24:37.360 --> 24:43.500] yet. It gets jQuery. It reaches out and it gets this thing that says welcome user. No [24:43.500 --> 24:48.460] big deal. And then it tries to inject the welcome user into the page. But you add a [24:48.460 --> 24:57.540] forward slash on to the end and now it actually gets the current page, vulnerable HTML. [25:04.500 --> 25:15.970] And... oh, one second. Let's see what just happened. What did I do? Oh, there it goes. [25:15.970 --> 25:27.250] So in this case what just happened is when it reached out to get it, the ID that it found [25:27.250 --> 25:33.170] was for a frame set. And HTML is probably the most lenient parser of all because it [25:33.170 --> 25:40.330] has this really weird behavior where if you try to inject HTML tags into other HTML tags [25:40.330 --> 25:46.090] that it doesn't like, it will simply strip those tags away. And that's exactly what happened [25:46.090 --> 26:02.440] here is because when it found... oops. It's not letting me leave. It's not letting me [26:02.460 --> 26:13.560] leave. Okay. Oops. Yeah, so when it found the... this frame set, it... when it injected [26:13.560 --> 26:18.700] it into this main tag, it actually stripped the frame set away. So even though the browser [26:18.700 --> 26:23.120] is saying hey, this frame set is completely innocuous, we're just going to treat it like [26:23.120 --> 26:30.380] text, the framework itself can take that and inject it automatically. So there's places [26:30.380 --> 26:36.620] where maybe you weren't concerned about people getting content injection before, but now [26:36.620 --> 26:40.300] you have to be worried about it. And how do you mitigate something like this? Well, there's [26:40.300 --> 26:46.960] all the standard ways to mitigate it. Like one, you have to stop using... either use [26:47.080 --> 26:53.240] a base tag so that things aren't relative. They have a... things aren't relative. You [26:53.240 --> 27:00.180] can make them root relative by adding a forward slash at the beginning, right? Content security [27:00.180 --> 27:06.140] policy, all these things, you have to do something. But also educating people, you can't actually [27:06.140 --> 27:15.960] trust a file download if it's using a relative... if it's using a relative path. So basic mitigations [27:17.100 --> 27:32.000] in place. Yeah. All the... all the basic mitigations, so path handling, you can't... you can't [27:32.540 --> 27:38.360] it's important to check if the server that you use... you're using is vulnerable to this [27:38.360 --> 27:43.840] type of path handling. If you add a forward slash at the end of the URL and it loses all [27:43.840 --> 27:49.660] of its styling, it's a pretty good indication that it has this issue. Default PHP server [27:49.660 --> 27:55.340] has this issue. I did make a GitHub for this. If anyone wants to play with it themselves, [27:55.340 --> 28:13.290] you can find it on GitHub. Yeah. So this is a novel file injection vector. I figured [28:13.290 --> 28:18.790] out a number of server side scripts, shell scripts, PDFs, but there has to be... there [28:18.790 --> 28:26.210] has to be dozens more if, you know, people want to explore it. I rarely do bug bounties [28:26.210 --> 28:31.170] because I don't work in security. So I do do it sometimes, but it feels like a job to [28:31.170 --> 28:39.790] me. So I try not to. I would imagine there's a number of these vulnerabilities that you [28:39.790 --> 28:48.410] can go and find if you're so inclined. No one's ever looked for them before. So yeah, [28:48.410 --> 28:54.030] I think... I think that there's a lot more that can be done there with the shell scripts [28:54.030 --> 29:02.070] on Linux to figure out how to get them to execute. And yeah. I think if any of you do [29:02.070 --> 29:06.070] bug bounties, you should go out and try to find it. And that is my talk.