So it is my pleasure to welcome Ian Hickey to the stage talking about relative path file injection, the next evolution in relative path. Okay. Let's give it up. Thank you for having me today at the AppSec Village, DEF CON 32. Thank you for having AppSec Village here as well. I'm a software developer. I don't work in security. I work in ed tech. But every once in a while I like to spend some brain power looking at some security things. 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. So many of you know that there's relative path overwrite. 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. And older Internet Explorer used to be able to use things like an expression tag and execute JavaScript from CSS. So it was a pretty ‑‑ it was a pretty interesting way to get JavaScript execution. 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. In this case it was ‑‑ there's context in HTML where you can't execute JavaScript. The browser won't let you. And some of those places are no script within a frame set tag. It interprets ‑‑ it interprets any HTML that you can inject as just plain text. And because it's plain text you can't do anything with it. So the first insight that I had into this was ‑‑ see here's an example. You have a B tag but it's just text. You can't display anything. 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. But there is a way to do it. 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. That's cool. 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. 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. 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. So what other parsers are there that will do the same thing? 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. All that it matters is that it eventually finds it. So that was my starting point. What else is there? So I started making a list of all the different types of parsers there are. 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. PDF can actually be handwritten. 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. 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. Well, PDF can execute JavaScript. PDF can submit forms. PDF can ‑‑ there's tons of research into malicious PDFs. 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. It doesn't really care what it finds before and after that. 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. And this is what this is here. This is just an embedded HTML file renamed as PDF and it displays as a PDF and it executes JavaScript. How many people, how are you going to convince people to download an HTML file as a PDF? Like are you going to put something on the page that says right click here, save it as whatever? They just won't do it. So I started thinking about ways to get people to download these PDFs without me having to explicitly convince them of anything. 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. They just, whatever you return back from the server it's going to use. 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. So that's exactly what I did. 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. Someone goes to a website. There is a download link on the website and the download link happens to use a relative path. 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. And if we open this up, all this is, is, oop, and if we open this up, all this is, is, you know, oh sorry, sorry. Let's see if I can make that bigger for you. Oop, oop, one second. Okay, good PDF download. So I've downloaded this PDF and I open this PDF. I'm going to open it with Google Chrome. It's just a normal harmless PDF. No big deal. However, all you have to do is add a forward slash at the end and you notice it lost its styling. The reason it lost its styling is because now the browser is confused. 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. That's relative path over it. It's confused so it's returning this back. 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. Which means when someone opens this, it's not going to be the same PDF that, that they expected to open. Instead, it's going to execute JavaScript. So what just happened? Where did, where did the new PDF come from? Well, the PDF lives on the server. Because the HTML lives on the server and you've persistently saved PDF content into that HTML page, right? 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. I mean, this is dangerous. If you think about it, what if this was a, what if this was a text PDF site or something? It's not going to be just one link that this happens with. There will be every link on the page that uses a relative download. As soon as they click it, it's going to download whatever that PDF is. So the things that you can do with PDF are pretty interesting, right? Depending on the PDF reader that the user happens to be using. 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? You can execute shell commands. 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? You can run scripts, you can download new scripts, you can, you know, whatever. 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? Because why couldn't you? So it's pretty dangerous. 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. However you get the persistent content injection onto the page. 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. So in the original relative path overwrite, what you needed was a link tag with a relative path to the style sheet. In this case, you need an anchor tag with a relative path to the PDF. And if it's not HTML, the download has to be fully qualified. Meaning if you don't have sample PDF here as download, then it won't accept downloading the HTML as a PDF. The content disposition will be wrong. So that's the basic requirement for executing this. It has to be a page. 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. But this technique is going to be more useful the more file types that we find that you can do this with, right? So PDF is great if they happen to be using a PDF viewer that supports executing shell commands, et cetera. But there has to be other lenient parsers and lenient file types that you can use to do similar things. So then I started exploring that. 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. He suggested I talk to a gentleman by the name of Orange Albertini who specializes in polyglot file types. He's a maven. He's the type of guy that his business card, you can put it into a Super Nintendo emulator. And even though it's a PDF, it will still play in the emulator. It's crazy. And we were brainstorming other types of lenient file types where this sort of thing might work with. And we went, you know, zip file types to tar. Tar is another like plain text one that you can sort of hand write. And it took me down this road looking at that. And while I was exploring it, I was thinking about, well, what would you put in a shell scripts? And then anyone who's dealt with enough shell scripts knows that shell scripts can actually be really lenient when they're run too. 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, you know, batch scripts. 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. And it will error on every single line until it gets to something that it understands and then it will execute it. 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. 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. And what that looks like is... what that looks like is... 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. By the time they realize something happened, it's going to be too late, right? So I managed to get this working with WSL2. I managed to get it working with PowerShell, which does the exact same thing, right? 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. 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. 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. So it can be done. It can be done. And so that's... so now we have two, three, four types of files. Ideally, we would have dozens, hundreds. There's so many file types out there to get this type of stuff working well. So I kept on... I kept on investigating other things. What does not mind there being HTML, right? What will someone simply download from the Internet and then throw into something and have it execute, right? So pretty much all server-side... server-side templating languages are like this, right? 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, right? And the cool thing there is that any of the server-side languages, they don't... they're expecting there to be HTML, right? They're expecting there to be all this other stuff in there, so they won't complain. And so these are the ones that I found that it would work with out-of-box, right? So you have this PHP. Similar thing right here, if we look at this vulnerable PHP. 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. And what this did is because it's server-side, it already executed this, and it says welcome user, no big deal. We do it the other way, forward slash, all of a sudden they download it. This time, welcome bad guy, right? 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. 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? There's nothing untrusted about this server, they've been to it a thousand times, and they're clicking a download link. There's no difference if you were able to just break into the server and replace the file, right? Because the effect's the same. But what would be really interesting is if you didn't have to do this whole download portion of it at all, right? And that's the hardest part, is how do you get someone to... 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? I mean, that's what it's intended for, they're going to trust that whatever's in there. It should be, you make a PDF that looks like a tax document and submits back to your server, whatever. But it would be great if they didn't have to download the thing in the first place. So I started thinking about how that could possibly work. And that's pretty tricky, right? But there's been a rise in all of these HTML over the wire sort of frameworks. There's Livewire, there's Hotwire, there's HTMX. 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? 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. 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. So something like that, what it would look like is... so I'm not using any of the frameworks. This is just straight jQuery. 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? And again, it's the same thing. All we're doing is, you know, doing a... you don't need to do this. All it's doing is just, you know, using jQuery HTML, just setting the HTML. No big deal. And if we go look at what this script does... what's that? There's nothing on this page. There's literally nothing on this page. So all this does is it doesn't do anything initially. You can see when it goes to get the... let me see if I can make this bigger. Excuse me? Oh, got you, got you. So nothing too interesting is happening on this page yet. It gets jQuery. It reaches out and it gets this thing that says welcome user. No big deal. And then it tries to inject the welcome user into the page. But you add a forward slash on to the end and now it actually gets the current page, vulnerable HTML. And... oh, one second. Let's see what just happened. What did I do? Oh, there it goes. 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. 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. And that's exactly what happened here is because when it found... oops. It's not letting me leave. It's not letting me leave. Okay. Oops. 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. 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. 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. And how do you mitigate something like this? Well, there's all the standard ways to mitigate it. Like one, you have to stop using... either use a base tag so that things aren't relative. They have a... things aren't relative. You can make them root relative by adding a forward slash at the beginning, right? Content security policy, all these things, you have to do something. 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. So basic mitigations in place. Yeah. All the... all the basic mitigations, so path handling, you can't... 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. 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. Default PHP server has this issue. I did make a GitHub for this. If anyone wants to play with it themselves, you can find it on GitHub. Yeah. So this is a novel file injection vector. 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. I rarely do bug bounties because I don't work in security. So I do do it sometimes, but it feels like a job to me. So I try not to. I would imagine there's a number of these vulnerabilities that you can go and find if you're so inclined. No one's ever looked for them before. 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. And yeah. I think if any of you do bug bounties, you should go out and try to find it. And that is my talk.