Welcome to the combined finalitizo speech. I have the pleasure of speaking with Harvard Flag here. Hands. And what we are trying to do is actually to bootstrap most of you script kiddies into real hackers. So the idea here is we're quickly talking about the mindset, then a lengthy session on finding vulnerabilities, and quick part on writing exploits. And if we got time, because the speech got cut short, as you just heard, we're talking about exploiting non-standard stuff. So the first and foremost thing. Who in the room knows who John von Neumann is? Raise your hand. See? That should be everyone in this room. The thing is, every computer today is built after something that's called the von Neumann machine. And this basically makes no difference between data and code. It doesn't matter if a byte is data or if a byte is code. Data can become code and code can become data. The only difference is that the data your instruction pointer points to is the code that's executed. That's it. And all exploitation actually depends on this fact. Second thing. Please start opening your eyes. If you have something that crashes, don't just ignore it. Don't just click away, Dr. Watson. Don't just delete the core file that was just produced by a web server. Please don't. Take the time and look at what happened. If you surf to a web page with your favorite web browser and your web browser disappears, and you surf to this web page again and your web browser disappears again, this is something you want to look at. You want to look at. You want to know what this web page does, what this web page actually sends your web browser to make it crash. Take a debugger, look at it, try to find out what happened. Don't ignore everything. Most people just walk around and stumble across vulnerabilities every day and just don't realize. So start looking. And here's a warning. Don't make any mistakes about it. This is real work. If you want to do something, you have to learn the stuff. You have to take a lot of time to find vulnerabilities. If you write exploits and they're supposed to work on something else than your local computer, it takes time. You have to find out how to make them reliable. So you have to really put some work in there. This doesn't come for free. And unfortunately, just in case you don't want to waste your time, brain is required. So if you don't have one, just don't waste it. Just don't waste your time with the rest of the slides. So finding vulnerabilities. Here's the deal. What we want to have is ODE and possibly our own ODE. So what we're looking for is something that handles network input, anything. As long as it handles any type of network input, we're fine. We look for something that runs on a remote system. Reason being, if we exploit our local system, it's kind of boring because, well, we know the administrator password already. And please, if you look for vulnerabilities, look for something that is complex enough. Except for Cisco, it doesn't really make sense to try to attack a UDP echo service. In case of Cisco, it's different. So what testing methods do we have? We can do manual testing, pure groundwork, make your hands dirty. We can fuzz stuff. We can do static analysis. We can diff stuff. Or we can do runtime analysis. So manual testing basically means you use the thing the way it is intended to be used. You use the client that's supposed to be used. So if you're testing a web server, what would be the client? Yeah, the web browser. So sometimes you have things that are a bit more complex. And you should actually then use the client that gives you the most functionality. What you're doing is you just use all the functionality and look at things. You look at the reaction of the target to valid input. What is it supposed to do? Most people start installing web server and try to, like, find bugs without knowing actually how the web server works. You have to observe the difference between valid and invalid input. Because if you're having just the idea of how it reacts to invalid input, you cannot compare. You don't know what's going on. And you need to find out what's going on. The most important thing is here, observe the correlation between your actions and network. The network communication that's going on. Both timing and what's been sent. So please start using the sniffers. Start looking at what's happening at the cable. Why are you using something? So you look at information that's transmitted before and after the authentication. Well after authentication is sometimes kind of boring. But still, if you have a normal user account somewhere and you want to become a, like, more privileged user there, it could be interesting what data is transmitted after the authentication. But of course, if you have something that is pre-authentication, it's even better. One very important thing is the configuration of your target. If possible, try to set up the service you're trying to hack yourself. Don't have someone come in. Like, don't call your local system administrator and say, come to me here and install this Oracle database on my computer so I can start looking for Litchfield type of vulnerabilities. No. Install it yourself. Because only that way you understand what they're thinking. You understand how this thing is built and how you're supposed to configure it. Maybe you actually find something really simple that everyone overlooks and you see because it is a configuration mistake and not something that you would find with a debugger or with static analysis. And look at the logging capabilities. Instead of reverse-engineering the whole thing, if you can set it to full debug mode and it will tell you, I'm TCP.C line 417 and just received a packet, and then it tells you TCP.C line 1007. I just parsed the packet. You know exactly what's going on. So there is absolutely no point in wasting time. Just check what it can do. Things to look for. Input validation on client-side. Really bad thing. Really widely used. Simple example with a web application. If you have a web application and you go there and you enter a bunch of As and it tells you you're not supposed to enter a bunch of As but never talk to the server, it is client-side authentication. This is why it is important to look at the cable at the same time. You do something. If it is client-side authentication, then you have, of course, the possibility of changing the client to something different, like a Perl script, and send a bunch of As over and see what happens. Because they may assume that since they are checking it on the client-side, it is already checked and already fine and nobody could possibly enter a bunch of As. Also, look of input that is modified before send. Again, web browsers. For example, modify special characters by doing HTTP encoding before they are sent over. Now, maybe you want to send over the special character. You have to, again, look at the cable, what's actually happening when you click on something. Then you have to look at pre-authentication data. Stuff that's sent to this target service before a username and password combination is validated. So. This could be your host name. This could be username. This could be certificate content, which is really interesting. I recommend if you are having a web browser and you often use some servers that use HTTPS, I recommend building a certificate with a really long distinguished name. But don't be surprised if you install that as a client-side certificate and you suffer on the internet and leave just burning, crashed web servers. So. So. Data and time strings also can be really interesting. If your day suddenly has a 10-digit number amount of hours, some servers react funny. And version information. Often clients send their own version to the server so the server can determine what type of client it is. Here's a good example with SAP. They do that for every packet. So you can, like, per packet decide what version of the browser you want to use. Which is really interesting because the parsers are not built to parse a packet from version 4 and the next packet from version 1 in the same session. More things to look for. Again, the network protocol is really interesting. You have to determine what type of size field you have or if you even have size fields. If this protocol is just static sizes, the assumption could be made by the programmer that. It's only this type of information and this size of information coming in per packet. So if you send more, you might actually overflow a buffer. More interesting, far more interesting, are dynamic fields. Here's a good example. If you see something that has, like, a four byte number, most of them zero, and the last is eight. And then you see eight characters following it. This first part is probably the length field. So we have one length information here. Then you can start playing around. What happens if you change this length information, let's say, to something with the most significant bit being one. And suddenly, on the server side, it becomes a negative value. Interesting enough, often you see this. You have a terminating zero here. So this is then really interesting because there are two information points on length. So the code on the server side could use either. The length field or the terminating zero, which is often done, actually it's often implemented as the length field is used in the beginning of the pausing and then everyone else just assumes it's a zero terminated string. This can go wrong and this is what you want. Also timing, concurrent connections, lots of them, if they're having a table where they hold all the information and you put a lot of connections in there, this table might overflow. Or fast sequential connections. If you connect fast and disconnect and connect fast, you might get things out of sync. Advantages of manual testing. There is no need for additional tools. You don't need any lead hacking tools. All you need is your client and the browser and, well, your brain. You become familiar with the target. This is very important. Don't start hacking something that you don't know how it works. You have to understand what principles they set and what they were looking for to do and what they ignored and what they didn't do during the development cycle. You have to understand who you play against with. Those are humans. Those are humans who wrote this code and those are probably stupid humans. You just have to find out how stupid. Another advantage. It uncovers, as you have seen, client-side security really, really well. And this is really fast. If you find a client-side security, you can be pretty much sure that you can exploit the server somehow. Disadvantages. Of course, it has a potentially high learning effort, but what hasn't? It often just provides clues, like the protocol we just saw. You can't just change the protocol in the sniffer and re-inject the packet. You actually then have to go and write additional code to try it out. And there is a high dependency. There is a high dependence on the tester, which is you. There is a high dependence on how much you're actually thinking about it. What you usually find with that is the classic cross-site scripting crap, code and SQL injections in web applications, protocol-based overflows, as we just saw, and application logic failures. Application logic failures are not found by any other testing methods so far and can be really, really useful, especially if they're web applications. So, I'm going to give you a few examples. There are a few examples that have a logic that, for example, depends on transporting the price information over the browser. So if you understand this and you see this, you can change the price and get your favorite toys for free. So it is best suited for web applications, Java crap, proprietary clients, and, of course, Internet Explorer, as lately often seen on BuckTrick. Another way to test any target. Fuzzing. Fuzzing is basically the idea of creating a rough client that exceeds the posses of the normal client. So for example, again, if you're talking HTTP, you're creating a client that is able to support a 4,000 character long URI. And this is then used to send it over to the server. It is best suited for documented protocols because you have to re-implement the whole thing. So HTTP.com. Again, web applications can be really interesting and protocols with many field combinations. Let me show you why. There are two different ways of doing fuzzing. There is what I call see my manual fuzzing, where you actually go ahead and you write a short script or program that does the first two, three steps that the usual client will do and sends over data, which is potentially malformed. You're manually changing. Single information one at a time in this client and just send more in this field or more in that field or change the length while you're here or play with this while you're there. It is your imagination what you can do. You run the code. You evaluate what happens. You look at the response. You look at the timing because it could be interesting if you send 10 As and it takes a second and you send 100 As and it takes two seconds. Obviously, it is handling those As somehow. Maybe you want to use that later in the exploitation process to make sure that the process is busy for a while. The other approach is automated fuzzing. In automated fuzzing, you actually write a script or program mostly actually almost all the time using a fuzzier framework. Basically, what you do is you write a program. You write a program to generate random crap in large amounts. You're coming up with all types of random crap you want to send there and then put that in the program and let the program deal with increasing the amount of crap or combining the amount of crap and doing other stuff. All you want to do is let it run for a while and see if your target crashes. What you try to find out with the SEMA manual fuzzing, as I just said. You're looking for unexpected responses. If the web server for a file that's obviously not there responds for or for a file not found, this is an expected response. If it responds something like, you got blue hair, then you should investigate that further. You look for modified data. One example here is if you send data to a web server, let's say you send a bunch of As, just a few. You get an answer back. File not found, but it converted those As either to Unicode or to HTTP encoding and has therefore sent you something like percent for one, percent for one. This is interesting and you should really look into it. Why? Because he needed three bytes for this encoding and you sent one byte. So maybe there is a miscalculation. Maybe it didn't calculate correctly how much bytes it needed for the encoding. You look for change timing behavior. Does it take longer or is it really quick? If it disconnects really, really quick, it probably just crashed. And of course, you look for target crashes best on the system itself. So if you have the target, you should attach a debugger to it while you're doing the fuzzing and see if the debugger at some point in time pops up and says, wait a minute, this just wrote over way too much data. The procedure here, as I said, is you go ahead and try to get your data. Your script work in the first place. Please, I've seen that so often. People writing scripts for attacking something and like iterate through all possible ways to send crap there. And if you ask them, okay, can you modify the script so that it works? They're like, yeah, try this. And it doesn't work. So make sure it works in the first place. Make sure in the beginning it is valid input that you sent and then start throwing crap at it. Change the fields. One at a time. Don't change like everything at the same time. And then try to figure out which of the changes actually made the target crash. Send it over. Inspect what came back. Change the fields again based on what came back. You always learn something. Even if it just says no, you learned something. You learned that it handled it and that it didn't want to parse it further or actually parse it and said, no, this is not good. Yeah. And then repeat until you found a bug. Here's an example where a timing issue was found. There is a largely known company that writes security and secure software. And this has a timing issue with fanciness. Timing issue here is if you connect too often to PC anywhere, it will not really handle the loading and unloading of the DLL. That it needs to paint this fancy little icon in the right corner of your windows. And so instead of like changing the icon color from green to red to let the user know that someone connected, it changes its state from running to crashed and pops up a message box. The automatic parsing procedure is a bit different. Here you have to start thinking in the beginning and then just wait. You have to define what you want. What vulnerabilities you want to look for. What type of vulnerabilities. Do you just look for overflows or do you look for format strings as well? Do you look for integer related crap? You have to define that and then write the script or program depending on your father framework. And then basically you just start and let it run and let it generate all kinds of interesting messages and see if the target crashes. Again, you should attach a debugger to it, especially on windows because there is this thing. We'll come into this later. That's called structured exception handler. And so the programs are actually allowed to handle the situation where they fucked up themselves. So if they fucked up, they can say, it's almost like a lawyer. They can say, I registered this lawyer here and if I fucked up, please call this lawyer and he's dealing with that. This is the same procedure and if you don't attach a debugger, you don't see this because the lawyer is just dealing with it. And you know, behavior. You're behaving as if nothing happened. And you have issues with process forking as well. There are several fuzzing frameworks. The most well-known is Spike by Dave Fattel. It is currently 2.9. It's block-based fuzzer written in C. So your fuzzing programs have to be in C as well, which of course is kind of like error prone. It has some functions for sending data and some functions for doing iterations on strings and creating bigger blocks. And more amounts of blocks and stuff like that. Unfortunately, there's almost no documentation. So you better read the demo programs that come with it. And since it is written in C, you often have things like this. Don't make a mistake. This core dump here is actually the fuzzing program and not the target that got fuzzed. Also don't trust it too much. Here we have a case where it just fails to calculate the content length field for HTTP correctly. And so the web server doesn't know what to do with it. It doesn't even parse the request because, yeah, it figured out it's wrong. There is another framework that's not released yet but will probably be if you're lucky over the time of DEF CON. It's written by Michael Addington. It is a Python object-oriented class structure and consists of several elements including generators for static elements, transformers for all kinds of encoding and decoding and crypto. It's also a very useful tool. It's kind of handy if you're parsing something that's in an encrypted communication. It has protocols so it can actually handle state over multiple protocol messages, publishers for sending the stuff actually or putting it in files or whatever is required, and groups and scripts so you can even put more abstraction on it. And the good part here, besides being actually real good code, is it's actually documented as well. This is a full-text file. It's a full-blown HTTP get request fuzzer in Python using Peach, so you see you don't have to code lots of stuff. You just have to specify what you want to do and then send it over. Advantages of CMA manual parsing are, as I said, the try and then inspect procedure that you follow. The fact that you're actually looking at the responses and learn every time you send something to the server. It helps you to really fast find all kinds of vulnerabilities. And of course, the good thing is if your script sends a bunch of A's in this or that field and you get the target crashed, you can use this script and slap your shellcode and return address in there instead of the A's and see what you got in exploit. Automated fuzzing is really good if you have a good done fuzzing script or program. You can just throw it against all kinds of different servers. Then see what happens. So you quickly find stuff and due to the fact that it is a program, you can basically have all kinds of combinations done. So if there are many possible combinations in a protocol you're attacking, then you should use this. Disadvantages, of course, you need to understand the protocol in the first place. Otherwise you can fuzz it, but it's kind of like boring because the server will never pause it. The tester has to rely on the fuzzer. The testers have to rely on the fuzzer. So as you see with the spike example, make sure that the stuff actually makes sense. And you need a debugger on the target system, as I said. So I'm passing on to Harvey here because static analysis is actually where he grew up with, so he's more fit in it. Good afternoon. Well, yeah. We went over looking at stuff manually, like at the application behavior and we went over fuzzing. Both is not really my world. I usually like take a disassembler and read the code. So yeah, what I'll be talking about is... Well, I'll just skip this slide. Anyhow, the stuff you'll need for actually reading a binary is you need the binaries at the target, which is sometimes a hassle because people tend to charge money for these sort of things. Same thing, you need a decent disassembler and there's only one decent disassembler out there, so get yourself a copy of IDA. There's a free evaluation version of data. It's a free evaluation version of data. You can get it on Web Data Rescue's website and if that's not enough for you, you might have to show it a few bucks, but it's worth it, definitely. Well, yeah, you need to know what self-language the target is written in and so on. The two more important things which you need to know is fluent assembly and fluent C because, well, you're reading assembly code and that assembly code has been generated by a C compiler from valid C code. You're looking for bugs at the C level in the assembly language, so understanding how those two things interrelate is a basic prerequisite. So how do you go about finding bugs? First of all, we find reachable references to functions with vulnerability potentials, string copy, sprinter, and so on. All right. Never mind. All right. So you look for places where there might be bugs. We can start with stupid things like string copy and sprinter and so on, and the fact that you're still finding these sorts of bugs really shows you what the current level of quality in commercial closed source software is at. Then once you've found a function, like you basically draw up criteria for every function, string copy, sprinter. You might actually find functions in the binary which are not like standard C functions but which have certain criteria where they can like blow up on you. So you try to draw up criteria for every function where it's like what preconditions do they have to satisfy the arguments. And then you go through binary and first look at all the calls, look at whether the preconditions are satisfied. And if the preconditions are not satisfied, like meaning there might be a bug, then you basically check whether the user input, like whether the attacker can influence the arguments coming in there which will violate the preconditions. And yeah, then you basically just try to find out whether that stuff is reachable. Obviously, like since you're looking at the entire program at once, you'll very easily read a lot of code where application is starting up and reading a lot of config files. Now, being able to find an overflow in a config file is not all that useful because you might have to be administrative to read the config file in the first place. So you kind of want to limit the code constructs you're looking at to those parts which are actually usable from the main user input parser. So after you've found a function which violates the preconditions and you are sure you can influence those things, you should pause, like read upwards in the code to see whether the application is checking the input somewhere because it's usually quite annoying to spend a lot of time working your way down through the tree, like through the core graph, getting to a certain point and then finding out that the application was checking, well, certain values right at the beginning or whatever and you just missed it. All right. It is, I mentioned before that it is really important to make sure that the function you're trying to reach is reachable from the main user input parser because if not, well, you're, hey, I have an overflow in the parsing of the password file. It's very good, but if you can write to it, you don't need the overflow. So you kind of want to find codes which read user data first, which might be received, received from, WSA received, WSA received from, you might want to know about RPC when you're looking at RPC components and so on. Then you just follow the program flow until you start seeing code that actually does some parsing on these things, like comparing strings. If you have a mail server, look for the SMTP strings, elo, hello, whatever, and then use the location of that main user input parser in the core graph in order to identify whether the function, like the locations you found are correct. It's not actually reachable, meaning you're trying to find out whether there's any path in the program that can lead from the main user input handler to the function where you're trying to find the bug or where you have found the bug and you're trying to get to it. All right. So the more important things to remember when you're doing this, in order to find bugs, you have to know the language, like the C language better than the programmer. Your job is being a smart ass and a nitpicker and a language learner about C. So the ANSI C standard should be your Bible. It's the best 10 bucks you've ever spent. You should read it. You should read it. And, yeah, you should read it. And it is really important to know that, like, having small gaps in your knowledge will make you miss, like, have large gaps in your audit coverage, meaning if you don't know a whole lot of assembly, you'll miss a lot of things. If you don't know a whole lot of C, you'll miss a lot of things. So make sure you've got your basics straight, like you know assembly well and you know C well. And make a point on being very, very, very rigorous on understanding what the language semantics are and so on. What is quite useful in order to learn more about the bug process or about bugs in general is read lots of open source diffs. The nice part about open source is they tell you, oh, we found a bug here. We're fixing it. Now, you should read those diffs. It's kind of like crossword puzzles. You print them out in the morning, sit down, what the heck were they changing? Why are they changing it? What's the implication? And where might this have happened? Like, are there any other places where this might have happened? Because oftentimes a diff somewhere will give you order elsewhere. And usually, like, critical kernel bugs will be fixed in the development kernel first before they're back-proded. So that gives you, like, a few weeks advantage. Yeah. So another thing which you really should always be aware of are different compilers work differently. And there's an astonishing number of compilers that are just plain broken. Like, they have buggy libraries. They don't implement the C standing correctly. Ball and C is a fantastic example. If you malloc minus one, like, four gigabytes, it returns success and corrupts the heap at the same time. Because what happens is they take the size, like, the size of the request, and they add the size of the control structure which they have to use for managing the block. So if minus one, you add eight for the control structure, you get seven. You allocate seven bytes. And then you write an eight-byte control structure into the seven bytes. So just calling malloc minus one will fuck up the heap. And there's a lot of things like this where compilers just don't implement the standard correctly and are broken in themselves. And then there's a lot of code that will work on one compiler and be vulnerable on the other one. So those are definitely places to look. The interesting thing about the standard is in many places the standard itself is ambiguous where it doesn't really phrase explicitly what should happen on an F read with a size zero and so on, or F gets with a size zero. And then you just read through the standard. And you're like, okay, this is an ambiguous statement. Let's see how different compilers react to it. And then you just see your compiler's question left, right, and center. It's a bit of a lawyer's mindset. You have to become, like, a C language lawyer. You know, you read the standard and you see where there's ambiguities, where there's something not totally clear. All right. So you should get to the point where you can, like, solve trigger questions like these. If you look at the following code there, you're going to assign int a with a value of ten. And assign int c with a value of minus one. You're going to assign int b equals ten. And then you have the condition if a plus b is larger than c, which means ten plus ten is larger than minus one. And interestingly enough, that condition evaluates to false. Now, this is relatively easily explained in terms of, well, a and c are signed, but b is unsigned. So if there's an unsigned component of the composing, the composing will be unsigned. c will not be interpreted as minus one, but as four billion. Obviously, four billion is larger than twenty. Fair enough. Now, the interesting part comes at the bottom where you have essentially the same composing but with different types. Instead of having an int and an unsigned int, we have ints and an unsigned short. And all of a sudden, the exact same composing evaluates to true. Meaning, well, ten plus ten is all of a sudden larger than the... four billion in c. And where's that coming from? So, quoting from the ISOC standard, page 57, it's called integer propagation. If an integer can represent all values of the original type, the value is converted to an int. Otherwise, it is converted to an unsigned int. These are called integer promotions. So the trick basically is we have an addition in a condition down here. So the thing is a, b, and c have ints. They're not explicit types. They're either ints or unsigned ints. But the question truly is, if you see this addition, what type does the result of a plus b have? Because if any side in the composing... If anything in the condition is unsigned, the composing will be unsigned. If both sides are signed, it will be signed. So we have a plus b. We are adding an unsigned value and a signed value. And we don't really know what the type is. So the standard answer is this. If an int can represent all values of the original types, the value is converted to an int. Otherwise, it is converted to an unsigned int. These are called integer promotions. Unsigned int b in the lower example... Oh, wait. Unsigned int b here can represent the values from 0 to 4 billion. 4 billion does not fit into a standard int, which can only hold 2 billion. Thus, a plus b, we have the type unsigned int, which means this side of the composing is unsigned. Oops, sorry. Which means this side of the composing is unsigned, and this side is signed. One of the two sides is unsigned, the composing is unsigned. Down here... Damn it. It's out. Down here, we have a plus b, but b is an unsigned short. The maximum value an unsigned short can have is 65k. And 65k fits comfortably into the 2 billion that an integer can hold. So the language rules dictate that b, a plus b, is going to be signed. Both sides of the composing are signed. Composing ends up signed. 20 is larger than minus 1. And, uh, yeah. So, love your law, and love your CNI-Gresh standard. Anyhow, going back to static analysis. Um, the big advantages of this stuff is you'll find vulnerabilities in code that's not normally executed. If you're just fuzzing, you're basically covering the normal-trodden code paths, and all the testing will actually, like the testing on the vendor side, will travel the normal-trodden code paths. You're interested in those paths that are hardly ever reached. Because that's where bugs hide. It's kind of like, bugs tend to hide under stones, not in plain sight. It's the same with computer bugs. They hide in places where nobody's looking. So, um, yeah, there's a lot of things you can find very quickly, very easily. Uh, format string bugs are very easy to scan for. And you'll find really, really funny and really complex vulnerabilities, um, just by reading the code. The disadvantage is you need to have a lot of time. Um, you need to spend a few years of your life for it, like, on it. And, um, yeah, you need to have a kind of a thing for pain as well, because reading assembly will make your head hurt. And you gotta like that. So, um, there are a lot of disadvantages. It's kind of a lot of time just reconstructing shit. And, um, C++ code is notoriously annoying to read, because you have all those virtual method codes, which basically tell you that, like, you can't really tell in the static disassembly where a certain code is going unless you spend a lot of time checking it down. Um, and there are certain things that are really, really almost impossible to read decently. Visual Basic, um, if you compile it in a certain manner, it will not be compiled to native code at all, but to an intermediate level in a presentation, which will then be emulated, and you really don't want to read that. Alright. So, I mentioned before that diffing is a really important concept, and diffing for source code is relatively easy. Now, um, binary diffing is something that's a lot of fun as well, basically because, um, vendors have gotten into this habit of not telling us exactly where the bug is anymore, and that's quite annoying. So, um, the goal is to take an updated piece of software, like a Microsoft security update, and quickly identify where the bug is, because most people take a few days in order to install them, so if you can have an export the day after a patch was released, you have a few days before they install the patch, which is good. And, um, seriously, diffing takes a lot less time than auditing, because somebody else has done the job of auditing and finding the bug, you just have to look where they've changed things. So it's a bit of a more economic way of breaking into systems. If you just have the time of sitting in front of the system and to wait for a few weeks until a new patch comes out, a different patch you break into the system, you've just saved yourself a lot of work, and that's always good. Um, anyhow, the other thing is that you actually sometimes get all day out of those patches because vendors tend to fuck up those patches in ways that you wouldn't expect, like patch things incorrectly, um, disregard signings and the added range checks. Um, that's happened to some people sometime. And then there's other things, basically, because if you have a large company like Microsoft, and a bug is in a library, and somebody fuzzes an application, and the application crashes due to a bug in the library, they will report to the product group for the product that crashed. Now, the product group will try, like, implement a patch in the product to work around fixing, like, the bug in the library instead of actually fixing the library. Now, this is quite amusing for us, because we see the workaround, we see what's broken in the library, and then we grab our hard disk for, well, whatever other application might be using the library, and we get free order. Um, it happens to Microsoft, for example, when they did the ISAR Server Edge 323 patch, they gave everybody net-mitting remotes. That's nice. All right, so, um, first of all, you need to find out what file's actually been modified in the patch. You can do it either by, um, well, it kind of depends on what the patch is, but with Microsoft, we would usually just create a large directory, with a random name. You just abort, basically, the, the patching process, they would leave the directory there. You just create the process once they ask you for something, and then, uh, you have a directory with all the files they're updating. Um, if the application, or if it's something more complex, where they're actually modifying executables, you might want to use something like Firemon. And, um, yeah, then you have to disassemble the patch. Um, doing this by hand is a really time-consuming process, meaning you have to disassemble all the functions, see which ones are identical, and compare them. Um, that is really, really, really time-consuming. Uh, there's ways to do this automatically. Um, you, well, you need a bit of graph theory for it. And, um, I'm not going to explain too much of it. There's a paper at that address there, which, uh, basically explains how the algorithms underneath it work. And, um, that's quite nice. It's, like, after the Microsoft release of the S-channel, um, like, uh, overflow and PCT parsing patch, it takes about an hour, like, when you receive the patch, to figure out what the bug is. And, um, it takes about another six hours to override EIP, about 12 hours to have a fully working exploit. And, um, that is quite nice, especially considering the fact that there seems to be a law in Germany that prohibits financial institutions to install any patch without testing it for seven days first. Right. So, um, coming from the static analysis, you can actually do the same thing with runtime analysis as well. So the idea is here, instead of, like, using IDA as a disassembler and, um, looking at the static stuff, um, you're actually basically look the process, um, on its fingers while it's doing it, um, in runtime. You do pretty much the same thing as in static analysis, um, but the thing is, it's more a observation type of thing instead of completely reverse engineering stuff. So, um, it's again a manual process, and what you need is a functional version of what you're trying to break. So if it doesn't work on your computer, don't do runtime because, yeah, you have nothing to monitor or attach to. Um, of course you need a debugger. Um, you need, again, fluent disassembly, and you need, again, at least a clue of what the library functions on your platform do, so give yourself a reference. Um, for the debugger thing, um, you can do a lot of funny stuff with runtime debugging, and I figured that on Windows, there is actually no open source debugger, um, and I wanted to waste a bit of time of my life and change that, and so here it is. Um, this is Dumbbug. It is, um, a C++ architecture, so you have actually a, um, debugger including pausing from, um, PE files, which is the annoying DLL on EXO files, disassembly, threat handling, and all the crap that you don't want to care about, and you can basically instantly create debuggers in a few lines of code. This is how it looks like. This is a full-blown Windows debugger in C. Um, you basically need to tell your compiler that there is a debugger. You need to create a debugger variable, and, um, you basically load the program and let it run. Um, this will already deal with everything that can possibly happen to a piece of software on Windows, and from here on, you can either register callback, um, functions or overload, um, the class. The class actually provides a bunch of functions for all interesting things, like exceptions happening or setting breakpoints and stuff, and you can overload those and build your own debuggers, um, however you like it. So in runtime analysis, you can do one of two things. The first thing is data follow, um, procedure. So basically, when stuff is received, um, you break at the receive call, you look where the stuff ends up, and you set a memory breakpoint, and then you let it run for however long you want it. Um, because it will stop as soon as someone looks at the data, um, this someone is probably the protocol parser, and therefore this is the point where you're interested in. Um, again, unfortunately, um, you have to spend a bit more time than once you identify this place and do reverse engineering and find actually out what it's doing. The other thing, um, is basically the same thing, um, that you do in static analysis. You follow code, um, while it's executed. The good thing here is you can actually step over stuff, um, if it looks like it's not interesting, and if it was interesting, well, do it again. Um, you, again, check arguments, and, um, that are passed to functions when they're called, and you check arguments if they're user data or not. But actually, you want to do this for stuff like sprintf or string copy, and the problem is if the programmer was a real brilliant one, then he has about, like, 1,000, 2,000 cards of sprintf in his program. You don't want to stop every time he calls sprintf to print his maiden name or whatever. Um, so you want to actually just see a few cases where it actually handles user data, so it's one in 100 cards or something, which is kind of boring, so, um, it's impractical. Advantages here is that in contrast to static analysis, the disassembly you see when the stuff runs is correct, because if it's not correct, it would be passed to the CPU as incorrect and therefore just crash. So, um, you have a, basically a guarantee that what you're looking at is actually what the CPU is executing. Um, you have a known state, so you actually know everything that the computer knows, and you see, again, advanced vulnerabilities that are really interesting, funny combinations of, like, things that people could fuck up. And so it's slightly faster than static analysis, because you can skip over stuff and you don't have to find out where indirect calls lead to and stuff. Um, again, it is time, skill, and experience required, so you can't just, like, go home and do it. And you often have timeout issues, so if you're, like, inspecting a protocol's parser, um, it could easily be that at some point in time it remembered the time that the packet came in, and then does a different code path, um, because just it spent about ten seconds on this thing because you were stepping through it instead of just running through it. So you have to watch out, um, if the code path you are seeing is actually the one that's usually executed. Um, usual findings for static and runtime, um, all kinds of overflows in pretty much every possible location in the code. Format string vulnerabilities are totally easy found. And integer vulnerabilities are usually not found in, um, testing methods like fuzzing or manual testing. So it's good for all kinds of stuff, especially protocol parsers. Um, logging is something that many, many servers break on, so every time they want to lock something, um, they have to copy it in a text buffer, and this is where they often fuck up. Um, and you find code using unsafe functions like sprintf or string copy and whatnot. Now, um, I mentioned that you want to see only the sprintf calls and stuff that, um, are relevant to you and they're, um, with user data. So I actually wanted to prove the point and, um, write a demo for the debugger core. And so there is a L trace implementation for Windows now. Um, it basically locks calls to functions before and after. So you see, um, what arguments are passed to the functions and, um, what comes out of it, what happens there. Um, supports several different call conventions because if you're trying to hack something that's written in Delphi and compiled in Delphi, yeah. Um, not natively yet because I didn't want to use any Microsoft, um, code. Like, um, for PDBs, it would require you, um, to use debug help DLL, which comes with a DDK, which means that I'm supposed to spend money on, um, crappy Microsoft code so I can redistribute it. And I didn't feel like this. So it does support debug, um, information, code view, FPO and stuff like that. Um, not yet PDB. But it's coming. Another good thing is, um, it's, as far as I know, the only debugger on Windows that is able to follow fork. So if your process is actually spawning a child, um, this one will just create another debugger and follow this child at the same time. So when you're fuzzing something, um, you don't miss it because it is in a fork child. It does a bit of stack analysis when the call is made and it does, of course, format string analysis. Um, it works by having trace definitions. So you basically take your, um, arbitrary MSDN, um, information and copy and paste the C prototype, um, into the trace definition file. Um, here you see you have a function name. Then you have the different arguments to the function. You can actually specify if this argument is something that is coming into the function. It is a input value, like the socket in receive. Or if it's a output thing, like in a S printf, you want to see the output buffer. You want to see what came out of it. Reason for this is, you can actually do matching on strings. So you can actually say, um, hex or equals in S printf. And then it will only lock calls that actually produce a output that contained the word hexer. Which is pretty handy because you can just attach it to a process and let it run for a while and then send all kinds of crap to it. And this crap should contain the word hexer. And you only see calls to S printf that actually deal with stuff you send it to. Um, this is the output. Here's the good thing about it. You don't need to read disassembly for it. Um, because it translates the calls to C again or to something that looks familiar. Um, you see you have different types. You also have the type fntshar, which basically says this is a format string and should better contain format specifiers, otherwise it's probably broken. Um, and you see the return values here. Um, the addresses of the buffers, stuff like that. Here's another example. This is actually from a server that, um, I found a vulnerability and wrote an exploit for, um, using the tracer. Um, which is a good point. Congratulations. It turns out it is a, um, server for an online game, and, um, someone, like, really screwed me up in the online game and I wanted to pay back. So, um, this is a custom function in the code here. Um, you have a parameter that nobody cares about. Then there is a format string and a message structure, um, where the logging message is written to. Um, what happens here is you see this node that's generated by the tracer saying that there is no format specifier in the fmt-shar type thing. And it turns out that this blah thingy here, um, actually is a message that is, um, coming from the user. So here you have your run-of-the-mill format string bug. Advantages from call tracing, um, it's extremely fast. You just have to, like, attach the tracer, um, on a process, click around, use the process for a while, um, terminate the thing, look at the output, um, and basically you can use your text editor and, um, search for the word vulnerability in the tracer output. You might actually find something right away. Um, you can recognize user supply data, so you don't have to find out if this data actually reached any point or if this is user data or not, because you know what you sent to it. If you are tracing a web server and there is an sprintf that contains the word Mozilla, it's probably something you sent over. And, as you have seen, it does automatic format string vulnerability detection, which is lame, but we can do that. Um, disadvantages, again, it's incomplete, so, um, the cool stuff is found by static analysis. You see here only the most trodden code paths, and it covers only function calls, so if someone actually does, um, a pointer arithmetic copy or something, um, you don't see that. So, um, of course, they're free and, um, yeah, it is recommended to combine those techniques, so don't, like, aim for one and make only this. So one combination that I'm right now fancying is, um, fuzzing using Peach and some pretty good script to it, and then attach the ltrace, um, to the target, so in case it forks or in case it throws an exception, this is covered. Um, also see my manual fuzzing in static analysis. Um, can help a lot. So if you're doing see my manual fuzzing and you have something that looks suspicious, then you go load the binary up, um, and read the code that's actually doing, handling the stuff that you're wondering about. So, um, a few slides on writing exploits. First of all, please don't write exploits in C. I do so. You do so, yeah. Please don't. It's really, it's error-prone to the end. I mean, we're exploiting C programs all the time. Why do we write exploitable exploits? It is not portable. Um, I see, hm? Uh, just to make a case for writing exploits in C is, if you're attacking a system and you want to use an exploit from that system on, um, you might, well, of course you can always forward traffic and crap, but you might just not want to have to install Python on the other system or... So we see there are different opinions. But don't. The thing is, it's not portable. How often do I see, um, people writing exploits in C that are not even portable between a Linux and a BSD system? So if I need, like, five hours to get the exploit working in the first place, it doesn't get me anywhere. And it's ugly. So go ahead, use Perl, use Python. If you're really sick, use Java. Use something that's portable. Exploit creation process. You start by, basically, finding a vulnerability. This we covered. Then you find out how to trigger this vulnerability. Then you try to get code execution. Um, yeah, you're actually aiming at getting your code to execute. Then you try to get, somehow, shellcode over there. And then you execute the shellcode. Which, basically, um, gives you the root shell. To trigger it. Make sure that, um, when you know how to trigger the vulnerability, this works on more than one computer. This would be really good. Because if it's only working on your computer, you can only exploit your computer. So try it, please, on more than one computer. VMware comes to mind. You can actually have more than one virtual computer on your computer and try it. And observe the environment requirements. Um, here's an example. Which is not too easy, uh, to solve, actually. It looks easy. Because, um, you do a get request and a lot of A's. Um, and send it to the web server. And it crashes. This is fine. This is what we want. The problem is that if you look at the vulnerability, it is actually, um, putting the original path, the real path, and the request together. Um, and hereby overflowing a buffer. Now, the thing is. You don't know the real path. So either you find a way to disclose the path, or you find an intelligent way to exploit it. But this is an environment requirement. You don't necessarily know what the path is the server is installed on. Code execution. Um, when you, you want to basically influence the instruction pointer, um, to point at data that you influenced or sent. Um, first of all, when you try to point, um, the instruction pointer into your buffer, use something that will stop executing. Don't use capital A's. Capital A's are a increment, um, opcode on Intel. It will just walk over it. So use something else. Um, use FF. Use the debug break. Um, CC is good. C0 is an illegal instruction. And don't limit yourself to the overflowed buffer. Don't try to stuff your shellcode into the buffer that you overflowed. Those are two vectors. The one is to trigger the vulnerability, and the other one is to transport your shellcode. Don't limit yourself by putting both vectors in one. Um, try to get out of the function as soon as possible. So if you can influence the logic somehow, um, try, as soon as the overflow happened, try to reach the return as fast as possible. Because there could be reading of local variables, or writing to local variables or function arguments, which later, um, screw your shellcode. I got a fancy graphic for that. Um, and this is what you usually have. You have a buffer. Um, this is because the buffer is broken. Um, you have a variable, base pointer, return address. So when it enters, um, the function, it will actually write a return address in the base pointer here. Um, then you have the buffer overflow. And if this happens, you're screwed. If it tries to use this variable, after you overflow the buffer, this is bad. So try to prevent this. If you can influence the code path, try to prevent reading of the variable to actually then reach this position, where it reads the data you overflow. So what return address should we use? Um, there are different ways of doing it. There is this classic way, um, following L of 1, doing direct return to the buffer. This is bad in operating systems that are used today, because there tend to be multi-threading, and they tend to have different addresses for stacks all the time. So don't do this, because it's not reliable, and you will not get anything that works on systems that didn't belong to you, um, in short. So, um, the best way, or the most common way right now, is return to a known code location. Find some location, some code location, that actually has an instruction, like, jump to a register, call a register, pop something and return if you have a pointer to your buffer on a stack, or add ESP something, return is the same case if you have a pointer, um, to your buffer, higher up the stack, use that. Or, use the structured exception handler. For those of you who haven't seen SEH, um, the structured exception handler is, as I mentioned earlier, the way for the program to handle its fuck-ups himself. So, the good thing is, the SEH is usually used to actually store it on the stack, the address is. So, um, instead of just overflowing up to the return address, and then return somewhere, overflow further, till you hit the first SEH. What happens here, is basically, you overflow up to here, then NTDL comes along, and says, um, exception happened, um, tries to call the handler, and you know what happens, it executes your code. Um, this is a very reliable way of getting code execution. Um, useful jump and call register sequences are sometimes really hard to find, because if you, for example, do a union code, you only influence two out of four bytes, and the other two are mostly zero, or something else, depending on the translation table. This is really bad. And there's a solution for it, which is pure simple brute force, um, yeah, I'm not intelligent enough to do anything else, so brute force is my tool of choice. Um, which is searching the entire memory address space for something that is addressable, and then search from there, if it would actually fuck up, um, the exploitation process or not. This gives you all possible return addresses that you could potentially reach. And since I was playing around with return address stuff, um, I actually wanted to put an end at those return address guessing, and by hand finding stuff anyway, and, um, also did the same thing for plain ASCII return addresses. Um, the good thing here is, you can actually save all the return addresses that you found for one version of the software, then go to a different installation and do the same there, and then do a diff- What the good thing here is, it gives you a reliable return address that works on both. If you do that five times, you have something that's probably working all over the world, um, which is what you're owning with it. Um, the whole thing is put together in a plug-in for OliDBG. OliDBG is a Windows debugger, um, with a fancy graphical user interface and stuff. It actually supports quite a lot of stuff and should be used. If you don't have your favorite Windows debugger selected yet, um, use Oli. Um, this is a plug-in for Oli, um, and you can basically attach it to your target process and have it, um, find return addresses for you. This is how that looks like. Um, here it is a Unicode return address. This here is a position that one could address, um, with this byte sequence and the unretranslation of Unicode. But this obviously is not a call or jump to a register, but down here we have one. And the plug-in, since it searches from every addressable position, um, could find out that in between here there is no memory access and we don't care if it fucks up, um, the registers because the registers contain crap anyway. We just overflow the buffer. Um, so this is just playing around and we really don't care what it does. And then it's calling edx and we get code execution. So now we have code execution. Now we need to get the shellcode there. Shellcode is a general term, um, describing data that became code. Who paid attention on the first slide? This is exactly why von Neumann is good. This is why we are happy that everyone builds computers following von Neumann. Um, here the data becomes code and now we need to create this data, um, which is useful. So if you're not so good in assembly, um, you could actually write your shellcode, a small program that opens the listening or the shell in C or something else, compile it, take the assembly out by disassembling it and, um, make it then position independent or remove forbidden characters like zero bytes, um, but I actually recommend writing it in assembly right away. So get network-wide assembler and, yeah, write it in assembly. It's good training anyway. Um, when you write your shellcode, try to make it flexible. Try to not rely on anything on, on the target, um, as, as little as you can. Just system calls is the best way to do it. Um, and use for addressing, make sure that you have position independent addressing. So don't rely on addresses because you don't know if your shellcode is going to end up at the same code address all the time. Here's an example, um, for getting the address of the string binsh here. Um, the way you would do it is you jump from the beginning to the end. From there, you call the beginning and then pop EBP or whatever because the call will store the return address which is right here where the string begins on the stack. And when you pop it down, you have the address of EBP. The reason to do the call at the bottom and not at the top is this, um, you have here a negative call because the offset from the call is upwards, not downwards. So it's negative, so it's all F. If you had it the other way around, it would be a zero bytes, all the zero bytes because it would, yeah, just, if there is, if the difference is less than, like, what, four gigabytes or something, um, then you have zeros in the address. ...known way to transport the shellcode. Um, you can have another buffer in the same request and basically point to this buffer later on or you send data before. Maybe you have to send a username before sending the overflow. Why not put your shellcode in the username? This is a good idea. Shellcode also often gets transformed. So you have zero bytes, um, that get introduced or that you don't have to have in your shellcode for the string operations to work. Um, you have other forbidden characters. For example, um, if the overflow has something to do with a path, probably dot and slash are not allowed. And so you have to take care that those are not in there. And you have encoding. Again, Unicode can be a real bitch here because it changes, like, 50% of your code at least, which looks like this. Um, you have a piece of really nice code in the beginning and then it went through Unicode composition and got, like, all screwed up. And you have to tackle this somehow. The way to tackle this is, um, first published by Chris Anway from, um, Next Generation. Um, they called it a Venetian shellcode because it tries to close the blinds that were generated with the zero bytes in between. Um, and this is why it is called a Venetian shellcode generator. Um, the reason why it is a generator is you don't want to do that by hand. This is tiresome. And, um, you can actually go and Google for the paper by Chris and then you know why you don't want to do that by hand. Um, so there are actually, um, shellcode generators for this. Um, DeFi 12, again, made one in Python that's public. And, yeah, you can download it. And, um, we actually have a curl implementation that creates a bit better, um, Unicode Venetian shellcode. And this is also public. So this is what it does. Um, you have your stuff sent over and you have all the zero bytes in here which you don't want to have. So this code over here goes ahead and fixes itself, basically. They're just going byte by byte and fix them back to what they're supposed to be. And then start executing from here. So, finalizing this, um, what we can do now is trigger the vulnerability, um, point the instruction pointer to some data that, um, we can influence. And which, at this point in time, becomes code. We can get our code or data, um, somehow there so we can actually execute it. And now we have to try it out. Um, and this is where reliability for the exploitation comes in. Because if you're writing an exploit and it works, let's say, on an English version of Windows, um, make sure that it also works on a Japanese or Chinese version of Windows because you're probably using it more against Chinese web servers than against English ones. So, um, use a debugger. Check it with a debugger. Check it without a debugger. Um, use different OS versions and languages. Um, and just try if it works. Make sure it is reliable. Do you have anything to add? For reliability? No. Sorry to wake you up. So the good thing about this is this is all there is about writing ODE. So at this point in time, you actually got your own ODE. Now, exploiting non-standard stuff, um, is absolutely the same thing. I had a lot of people last year's coming up, how can you do this on Cisco? There is no difference. It is, again, a phenomenal machine. It got a CPU. It got random access memory. It got some permanent storage. It got network connectivity. So why not hack it? So go ahead. Hack routers. Hack printers. Hack cell phones. Hotel TV, um, pay TV stuff, ATMs, whatever comes to mind. If you see something with an Ethernet jack in it, declare it a target and start learning it. So what you see then often is non-Intel CPUs, something that's not in your system. It's not in your PC. Um, there are a few small differences, but the major difference is it's nicer. There's almost no CPU out there that is as ugly as Intel. So as soon as you start learning different CPUs, um, you will find out that there are really nice CPUs out there. They're often fixed size instruction sets. Um, they often have alignment requirements, which is really important when you're writing shell code. Um, they have different condition codes and different conditional execution, which can be really handy because you can write way cooler shell code than you could on Intel. Um, they have some tricky stuff like delayed branches, and they usually have more registers, so you can actually do more stuff with it. Again, the process is absolutely the same. All you want to do, um, on a different OS that might have a different CPU, um, get a shell first, and then get a debugger running on this shell. And all you need then is basically understand the platform. There are a few things to understand, like process management, memory management, privilege mechanisms, and granularity. It doesn't make sense if you're trying to, like, write a local exploit on a platform and then realize there's only one privilege level on the platform anyway. So you're, like, exploiting to get the same privilege level you had before. This is pointless. Don't do it. So read up on the stuff. Find out how it's working, but don't believe everything. If it tells you it has buffer overflow protection against everything, don't. Don't believe it. They wouldn't possibly do that. And if they do it, they probably fucked it up. So, exploiting black boxes. Um, there are talks here on hardware hacking, um, but I'm preferring a different approach, which is basically black box testing. Um, you can look for undocumented debugging and hidden functionality in terms of logging capabilities. This helps you a lot. You can make a lot somehow. A lot of information, um, which helps you to determine how it works internally. Um, if you have, like, how many of you have those little, um, home routers at home that do ADSL and wireless and stuff? Raise your hand. Okay. I expect at least half of you next year with an exploit for it. It is really, really easy. Those things are totally broken, and you don't even need complex debugging because most of them actually have just an unsolder, um, serial interface on it. So connect to the serial interface. See what it does. It might actually even come with its own debugger already installed. Um, if you have something more complex, like you want to hack, um, your neighbor's car, um, you need a JTAG. JTAG interface, which is basically a, um, on the JTAG interface in the first place where you can use it for full-blown debugging if you want to. Um, but try and error does as well. It's fine. If you have an overflow, try to find out, um, if you can get code execution the same way you do it on Windows or Linux. Um, you have ways to find this out. Take, like, an infinite loop in the assembly language of this processor and put this in as a shellcode. And when it stops doing anything but getting really, really hot, it's probably running in circuits in this infinite loop. So you get your code execution done. Stop interpreting results. Find out, um, not just, like, try stuff, but also try to, um, think about what happens and why it happened. So basically this is the summary, unless you have anything to add. Do you? You might want to spend actually less time in front of the screen than you, like, you think at the first glance because, like, you read things differently on a screen than you do on paper or you read in a park. And, uh, it's a very common phenomenon, especially in our circuits, to spend all the time in front of the screen and then don't see the forest or all the trees. So it's a good idea to step back at times and just look at different things. Good point. So those are great, and, um, I guess I open up for questions because, surprisingly, we even have time for that. Come on, please. If you don't have questions, you didn't get it. Okay, so... There is one! Cool! . . . . . . . . . . . . . . . . . . . . . . Sir, okay, since everyone is already leaving, I declare this show as closed. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you you