Hi, my name is Paul Haas, and this is my talk on advanced format string attacks. Go, Paulie. A little bit of background. Contents of this are going to be a little bit of background on me, an abstract of what format strings are, how you exploit them, a definition of sort of the way in which C functions are made vulnerable to it, context in terms of old, current, and new attacks. I'm going to show you my new techniques for exploiting them and actually go through a couple of demos before actually showing you a couple of full exploits, popping root shells without doing any work. Then I'm going to show you my tools, how I do it, finally leading to a conclusion and Q&A. So I work for Redspin Incorporated as the lead web application security engineer. This talk isn't associated with that at all. It's just for fun because I like doing some binary analysis type of stuff. I'm a former CTF champion here at DEF CON, so hopefully I know what I'm talking about. You guys will make the decision for me. And for those of you who don't play Mario Kart, apparently, it's all about robot and tank. I'm going to put some money into it. I've made a lot to do. So the purpose of this talk is to take a vulnerable program like this, get a shell, without doing any of this. So a little bit of brief background, format string attacks, for those who don't know, vulnerability and a type of C function, printf type functions that basically can lead to shellcode executed. Even though it's been sort of resolved, ignorance and vulnerability still exist, otherwise I wouldn't be talking about it. It's especially common in academic exercises, CTFs, a lot of legacy pen tests. The reason I brought this up is actually because one of our clients had this issue and I didn't feel like doing any additional work on it, so I created a tool to exploit it for me. This name of the talk is Advanced Format String Attacks, so I assume you know a little bit about what format string attacks actually do and how they work. Thankfully the tools I'm going to be talking about don't require any knowledge, they're pretty much point-and-click as you're seeing. Brief history, format string attacks have been around for about 20 years, sort of reaching the apex around 2000 for being popular being really well-known. The last format string attack that I've seen has been in 2010, so they still are around, as you can see. The old technique used to be really kind of a pain to create, it required a long string of doing these manual popping using %x, %p, in order to sort of get to your interesting data on the stack. Often when you want to actually create the exploit you have to use a variety of other tools. You know, look for shellcode in your core after a seg fault. Basically, you know, it's a lot of having to, you know, read the manual, you know, consult the document, you know. And then once you wrote it for one, you know, program, you couldn't take that work and then bring it anywhere else. So, you know, the current has improved a little bit on this, but not much. It's still sort of the same technique. You know, some of the advances have been direct parameter access. So rather than having to pop, say, 10 things off the stack, you can just reference the 10th parameter directly. But again, you still needed to sort of use external tools to access, you know, where you want to actually overwrite your code for. So that's the first step. The technique that I've been working on basically utilizes the information that because you're dumping the stack and the stack of most programs, C programs, has interesting information in it, you can use this information to basically leverage piece by piece of exploit leading to eventual, you know, compromise of the application. For example, most stacks contain data, code addresses, you know, addresses that point to code, as well addresses that point to other information on the stack, such as, you know, data pointers or code pointers. And what's also interesting is that the string that we pass for a format string attack is also on the stack. So we can locate that as well as its offset. And with this information, we basically can locate the address of anything on the stack. Which is useful, for example, if we find a code pointer that we want to overwrite on the stack. We can now find out the address of that pointer and then overwrite it with our format string attack. So here's the vulnerable code that I've been using. Obviously, it's not going to work in secure environments, but, you know, you still see a lot of this, you know, bits and pieces here and there. So, you know, I wouldn't be expected, you know, I would be pretty expected to see it in, you know, a couple pen tests that I've been doing for legacy applications. Let's talk a little bit about the exploit steps for my new method. I first dump the stack using, you know, an exploit string until I find that exploit string on the stack. I then find the address of that format string address on the stack. Then by finding the physical location and the pointer to that location, I basically can calculate the address of any other stack pointer or stack, you know, data that's present in my dump. This basically allows me, if I find any, you know, code pointers, return addresses, to say I can overwrite that address to point back to my shell code, which is also on the stack. One note with this is, because I'm sort of leveraging multiple executions of a program to drive this information, it's really helpful to keep the format string length constant because if you change it around, so do stack values and stack addresses, and since you need that information to be relatively constant, it helps to basically just keep the same format string length across all these different runs as I'll show you. The first thing you have to do, stack dumps, two methods. One, you just dump, you know, the whole stack, you know, the whole stack at once using, you know, a bunch of percent p's or percent x's. Second method is to execute a loop with, you know, incrementing values. In this case, I have here just a little bash script. I'll show you a demo of this that does it and makes it really convenient to show what's on the data or what's on the stack. You then take this stack information, which is based on percent p or percent x's. It's gonna be code pointers. And you actually wanna convert it to the, you know, string representation of that data. By doing that, you basically can find the actual string that's located through those pointers. And then by doing that, you can find the offset of that string from the stack dump. So right here I have a, you know, a decently long one-liner for bash. This is basically running our vulnerable print f function. Let's see. So basically this function, when you pass it, a string basically just prints it to the command line. And you can see it's actually vulnerable to, you know, a trivial format. It's a format string attack, basically. Oops, sorry about that. So this one-liner basically is gonna loop through the print f, you know, sending incremental values on the stack in order to see what's there, basically. So what this is, this is the offset that I'm passing. This happens to be the string that I'm passing to print f. This is what print f returns to me, a code or stack pointer. And this is actually what that resolves to in terms of a string. So scrolling up. Let's see if I can find where I'm looking for. You see that I've got a string that I'm passing to print f. At about offset 138, sort of divided between offset 137 to 139, we have percentage 138 dollar sign, which happens to be what I passed on the command line to print f. So here we've used knowledge of the exploit to basically find our string at offset 130, 137, 138. So we use that in a future step to sort of help exploit the program. The next thing we have to do, format string address. Now that we have the physical location on the stack, we want to find the pointer to that string. Two ways to do this as well, a sequential loop as we did before, which will cause segfaults in the case of if the pointer at the given offset isn't actually a pointer but some data value, it's going to segfault. Which might not be good in certain environments, for example, on a pen test if there's aggressive IDS systems. The other way to do it, the more sneaky way is, given the stack dump, since we already have all that list, since we have a list of pointers already, we can parse that list for values that actually are on the stack and then just obtain the offsets from those values only. This avoids segfaults, it's a little bit more elegant and can get bought. It allows us to basically dump all strings in just one run of the program, basically. So let me show you the first brute force method. Because it's a little bit more easy to run on the command line. How's it going, guys? Pretty good. We're tied. Yeah. Very good. We do all the hard work. So same thing, a loop here, basically. One liner that's going to basically pass a bunch of things to the same printf function and try to get both the value as a pointer of the location as well as a string. So you can see here that, again, it's dumping offsets, showing, for example, what's the value, the pointer value, as well as the string that that pointer resolves to. So going back up a little bit, let's see if we can find it. So at offset 38, pointer that ends with 608 actually references itself, telling us that that pointer value actually resolves to our pass argument. And so we can see it's going to be 100. This is the second step of the attack, basically. So given this offset that we found in the first stack dump and the address that we found in the second, we basically have a method to allow arbitrary win on the stack. We know that, since offset 100 is at, for example, stack pointer, BFFFF, 100, And we know that a pointer is four bytes. We know for example that offset one is going to be minus 400 values from that offset basically So we have a return address at offset 10 we can calculate the return or the stack offset of that and then when we're attempting our Exploit we don't have to get You know, for example detours a program program link table or some other overwrite location We can just overwrite this location on the stack Which we've already found from a previous step Even though it's possible for example in advanced format string attacks to sort of extract this information We want to be sort of lazy and just have you know this Operations from previous steps do our work for us Now two methods to do this as well If we know what a return address looks like They're pretty common on most You know Linux systems we can use those Or you can also guess them using sort of an algorithm that matches values that are close to each other that aren't data and aren't strings Or we can also be lazy and just brute-force everything on the stack until you know, our exploit succeeds basically Which is a lot easier to do basically Some issues with this new technique as I mentioned If we change the length of our format string, it's basically an effect Addresses on the stack since our format string basically as it grows so does so do stack values grow in addition to it The easy way to resolve this is basically to keep all format strings, you know during the technique the same size You know just pattern with you know No characters or useless characters so that when we actually attempt our exploit and append shell code to the format string You know that will happen to be the same length as all our previous executions of the program The result of this is you know given two Executions of a program one to dump the stack and two if you're being really clever and just dumping all the string addresses You basically get all the information you need to exploit that Function basically no brute-forcing just a little bit of math It's also nice because the way this current technique works You can shrink the length of the format string so that you can fit both the format string and the shell code and Say less than a hundred bytes, which is usually pretty, you know, really reasonable for you know, any user input field basically However, if you don't brute force the brute force the override address or if you don't do some math you still have to derive derive it from some other source such as you know A core dump or you know a known location like a program link table or the detours So demo is a good the good heart of the talk The first tool which should be on your DEF CON CD is a proof of concept tool in Python I have instructions for running on backtrack for Basically is a nice little suite for demonstrating sort of the way the attack works You know multiple options about where you want to overwrite where you want to put your shell code Again since the proof of concept is missing some useful things, but you know those can be really easily added You know, hopefully by anyone who has an understanding of this talk So I'll demo that tool right now You know taking our same printf function that's we know to be vulnerable to format string attack And you know just for the sake of vulnerability sync will make it Ownable and then exit executable by root so you know anyone will inherit those permissions I Have a dummy account right now that doesn't have that has limited permissions Obviously I want to exploit this printf function, you know gain root access. I don't have to actually have to do it to myself. So I I'm gonna basically use my tool So that's a little hard to read but basically it's just going over the options about You know where you want the exploit to override and where you want the shell code to be But the nice thing is you don't really have to read any of that because you can just you know, click it with the binary and You're in basically So So and The nice thing is this technique works on any, you know any similar similar exploiting, you know type of vulnerability basically The question was would this work on a syslog thing? Where are the outputs going somewhere else? This attack basically works as long as you can read the output from formal former vulnerabilities and use those and new vulnerabilities It's gonna work basically The next next code I developed specially for DEF CON designed to be as loud and invasive as possible Has been completely automated so I sort of removed that reference to having to get an overwrite location I'm using basically brute-forcing through the stack Ported both the Python and Ruby under hundred lines of code. So You know, hopefully if I haven't have time I'll go through it right now and You know talk about it a little bit Basically does the same thing except, you know using a brute force attack. So let me show you that as well So So again same printf function Same dummy user Different different binary So again you just pass it at the vulnerable function as a parameter and You get a root shell So I'm Even though you can't see it right now, for example, if I exit you see right now, it's it's actually brute-forcing a bunch of you know Spaces trying to go through all the stack values until it finds it so You know if as long as you know, as long as it's there somewhere you're gonna get in So if that's all that matters you can be invasive as possible I'll show the Ruby version does the same thing but For those who like Ruby better You also can get a shell Finally the sort of the meat of this talk You know if you're gonna have this sweet program, she might as well put where someone can use it In this case, I decided to move it to meta exploit Add it for the capability for remote exploit the bill the usefulness of the troisième of power access teaching is thosemetrosumpl measurements between the individual data統 of the ones in water is 3d. of this is that you can use an arbitrary payload, anything from Metasploit. The code is there in Metasploit in a known location, so if you want to extend your functions for another vulnerability, you can just use Metasploit for it. And for this, I created a sample vulnerable server that basically does the same thing as our printf vulnerable function, except through a TCP connection. So let me demo that right now. So I'm not going to go through this too much, but basically it opens a port on 4546, forks a process for connections, and then it basically sends smprintf vulnerability right here back to that socket that you connected to. So let me make this and go ahead and run it. So this is running on my VMware. I'm going to try to exploit it from my local machine. Metasploit is taking a while, don't worry. That's correct. This assumes, you know, this is for kind of legacy environments where a lot of those protections are nonexistent. So while Metasploit is loading right now, I'll go ahead and just show you that it indeed basically just, you know, by piping something through netcat to that server and to that port, it basically does the same thing. You know, it gives sort of this, you know, data disclosure. How's it going, guys? It's great. Very good. These are my co-workers from Redspin, so they're here to play Mario Kart with me, basically. They don't have any relation to the talk whatsoever. Okay, there we go. All right. So right now I'm going to load my custom Metasploit module. Show payloads, let's see. What do I want to exploit today? I'm going to just use a reverse TCP bind shell. Let's see, are my options for this? Looks like it accepts remote host, remote port, local host, so I'm going to go ahead and set those. Okay. All right. One third in this case is my VMware machine. And 58.1 is my local machine. And hopefully this will work. So the nice thing is, since we're brute forcing the stack. You know, any return address on that stack is going to give us a shell. In this case it happens to be, you know, six, six, you know, overwrite return addresses on the other program. So now you can share a shell with your friends and family if you would so choose to. And we can verify that by, you know, listing the sessions. And then say the command to them. So you can see that, you know, I compromised this application six times. And you know, got a root access on all of them. Okay. Looks like I'm doing really well on time. So I'm going to go through, you know, my brute forcing code in Python. Hopefully you guys will get something out of it. It's only about 100 lines of code. Four steps. So first thing I do, basically, is just, you know, standard. Actually, wrong one. I basically just have, you know, initialization, you know, setting up my variables, defining some functions. Second step, I basically go through that stack dump process as I showed you before. You know, I use this data, basically, to find, you know, an offset address of the found format string. The next step after that, basically, is, you know, I'm looking for that. There. You know, the address of that format string. So basically, I run another loop to find that address. And finally, when I'm actually exploiting, all I do is create the exploit string and then run through this brute forcing technique. So you know, 68 lines of code. You know, hopefully pretty extendable. Used on a variety of, you know, similar instances. So hopefully you guys will find some use out of it. Okay. Next slide. In conclusion, you know, format string output gives you everything you need to actually go from discovery to compromise. You know, it can be completely automated as I've shown. You know, they have been easy to fix. Now they're easy to exploit. You know, for those who are interested in finding some of them, a good suggestion, Google code. A good dork to look for. You know, this type of thing is shown there. This basically look for a C language print type function. Print app type function that doesn't begin with something like a constant. Something that doesn't begin with something like STDIN or STDER or something that begins with a parenthesis. So hopefully something that is actually using, you know, a buffer. You know, a non. Non command line. Something that uses something that the user is providing in terms of a buffer basically. So thanks. Hopefully the tools that aren't on your CD will be put on Redspin and DEF CON shortly Monday I'm hoping. I'm hoping that the Metasploit module will also be there as soon as I talk to those responsible for weird autofilters. For more Supreme stuff. You can contact me. Otherwise, you know, shout out to all the people at Shellfish and the people playing mother cart with me. questions, Mario cart. Hopefully look like we have plenty of time, but hopefully we'll be in another room. If you don't have any right now. So... Can you, can you repeat that one more time, please? So the question was, does my proof of concept assume that there's direct parameter access to exploit it? And the answer is yes. So, I basically assume that, you know, most modern versions, or most versions that I've seen have used that direct parameter access. Again, the technique can still be modified. You just have to do it yourself. How hard do you think it would be to modify this to actually work with, like, X-based layout recognizations or no? I don't know. I think it would be a good exercise, but, you know, I wouldn't be surprised if you could do it. You know, because if you dump the stack enough times, you're still going to have a general idea about where stuff is relative to everything else on the stack, so, you know, you still could do some sort of permutation attacks, and if you're brute force in any way, you know, it's only a matter of time. Okay, guys, thanks a whole bunch. Bye. Bye. Bye.