That's it. Without further ado, Cameron Hotchkies. Alright, thanks everybody for showing up this early in the morning. My name is Cameron Hotchkies, and I will be presenting basics to OSX Reverse Engineering. Who I am, I work at Tipping Point's DV Labs. Part of my job is to work with the vulnerability research team, work on the ZDI team. So we do patch analysis, product security, reverse engineering of various formats. I do a lot of the Apple stuff. And we have our blog that I'm required to tell you all to check out. I write the line noise if anybody reads that. I also, in the past, have written and contributed to PyMay. If you've done any reversing or you use OpenRCE, that's Pedram's baby. And I worked on that a little bit. Farther back in the past, I wrote Absinthe, which is a SQL injection tool. But I don't really do the web stuff anymore. That's sort of an old thing. If you're like, hey, I think I saw that guy one time. But I also have a side project that I'm trying to push, like crack on everybody who's interested in OSX reversing, which I think is everybody in this room. Either that or you're just looking for a quiet place to sit down. But if you are interested in the stuff I'm talking about, I have the XSO mailing list. And there's a lot of really talented, smart people. There's a lot of people that are on that list. It's very low traffic. But if you have any questions, everybody's fairly friendly. And I can help you out with OSX-specific reversing stuff. So this is roughly the format I'm going to go through, the stuff I'm going to cover. Basic stuff on file formats, which is the macro file format. I'm not going to spend a lot of time on that, because you're not going to spend a lot of time working with that, for the most part. Various tools that are available for reverse engine. There's a lot of engineers on OSX, or people who are just reversing OSX stuff. You'll figure out why I'm clarifying that. And then I'll go into common disassembly patterns, which is always helpful to look at if you've never looked at any of these binaries before. Cover carbon and Objective-C, which is going to be the bulk of it. And then get onto a little bit on iPhones, so everybody can be like, yay, I learned about iPhone stuff. So on OSX, how many people actually here use OSX regularly? Like, we're all Apple users. Okay, so I'm sorry if I'm going to be talking down to you for the next little bit, and you're like, dude, I know what you're talking about. But it needs to be covered. App bundles, for the three people who aren't Apple users, what they are, it's a file folder structure where the actual applications are stored with all the extra libraries and images and graphics and whatnot. And those are referred to as bundles or packages. Finder will treat that as, like, its own single entity, this atomic thing, and when it's really a folder structure. And you already know probably all of that. But this is what an app bundle will look like for the most part. You'll have a top-level program, which will be the directory structure. Finder hides the .app extension. Underneath that, you have the contents directory sitting by itself. And inside of the contents directory, you have the package info file. Anything gray is a file. And white is a directory, just in case you're curious. Mac OS is a directory. And inside of that, you have program, which is going to be the same name as the app bundle name without the .app. Usually, it doesn't have to be. But for the most times, most of the time, people will do the same thing. Resources is where you put your nib files and PNG files, et cetera. Frameworks, plugins, shared frameworks, shared support. They're all optional directories, but they may end up in those directories if you're just going straight for binary reversing and you don't care about all the supporting data. You're just going to go straight to the Mac OS directory and take a look at the program binary. So the info.plist file, which you can see is right underneath package info. The info.plist file is going to store a lot of the application-specific properties. It has data such as the version numbers, icon names, anything that's supporting that you might want to change really quickly. Without having to recompile the binary. It's been fairly well documented by Apple, so I'm not going to spend a lot of time covering it right now. The one thing is a lot of people are like, this is an XML file. It isn't always going to be an XML file. Sometimes it's going to be a binary file. But for that, you have the PLU till. And that will just convert between the binary version and the XML version. So then it can become the XML version that your friend told you it always was. And you'll know secretly in your heart that it isn't. But just a little like hint that, some of the people at Apple are still kind of like that old school Unix crazy people. If you go to man, the PLU till utility, or to check out the man page is right at the bottom. There's just like this little thing. It's like the PLU till command obeys no one's rules but its own. It's sort of a non sequitur right at the bottom there. But you're like, okay, they're crazy. Package info is another supporting file that's going to, in theory, have metadata. But it's usually only going to be eight bytes long. It's going to have, you have APBL and a signature. So it's there. You're probably never going to use it. Now when you get into that Mac OS condense directory, you're going to be looking at a Mako file format file. This is the standard binary format on OS X. It has the feed face magic number. If you're on a 64-bit platform, it's going to be feed faqf, which is just identifying that you're on a 64-bit platform. You're dealing with a 64-bit. Now there's the fat universal binaries. So when Apple switched over to using the Intel chipset, they started putting both the PPC and the x86 code into the binaries together. And so they're just referred to the fat ones. And they're identified by CafeBabe. Now CafeBabe is also used in Java. So you're just like, how am I supposed to know the difference? You don't really need to know the difference. They'll figure it out on its own. But the interesting thing about Mako binaries is that you want to be really careful when you Google for extra information on it. I'd say put site colon apple.com or something similar that you trust, because otherwise you just get the most random screwed up stuff such as that. I read that paper. It was like a 12-page academic paper. And I read it about three times. I still have no idea what the hell it was about. I think it was one of those auto-generated ones. But the CafeBabe thing, coming from Java, it's not actually from Java. Java actually came after the Mako file format. And I'll get into that in the history of Objective-C stuff later. But if you're looking at the Mako file format, it's changed and adapted over the years. Some of it's going to be fairly constant. You have .text, which if you're used to Windows, it's going to seem as odd. And there's some stuff that's slowly been deprecated and removed and now is just not mentioned anywhere as if it never existed. So if you find really, really old docs, anything with the asterisks here is going to be older stuff that's not really relevant anymore. But if you're looking at old binaries, maybe some next step binaries, then these may very well be in the binary. So they're pretty straightforward. You have the code stored in the .txt. You have the constants stored in the constants section, static constants stored in the static constants section, and so on and so on all the way through it. In the data section, that's where you're going to have your variables, your symbol pointers, your constructors. So it's pretty straightforward as well. If you're using any modern disassembler, then it'll sort all this stuff out for you. This is the part that you actually want to pick. Pay attention to it, one second. The Objective-C segment is going to be storing, if you're looking at an Objective-C binary, which is going to be a lot of the binaries on Apple stuff, they're made by third parties, then a lot of metadata is stored in the Objective-C segment. A lot of stuff is passed around by strings, so they need to store the strings somewhere for reference. Now if you look at any documentation, there's a lot of stuff that's stored in the Objective-C segment. If you look at any documentation from Apple, they'll say all sections in the Objective-C segment, including old sections that are no longer used, future sections, whatever, they're reserved for the Objective-C compiler's use. So what they're saying is good luck, you're on your own, we're not going to tell you what they mean. Luckily, they're fairly decently named, so you can figure out their purpose, and the structure is a little weird if you're coming from somewhere else, but most of that's also documented. I have a Python script that's included on the DEF CON CD that will rebuild a lot of this for you, so you don't have to worry about it, but if you're curious about the structure of it, you can take a look at the Python script. Now this doesn't really fit anywhere else in the talk, but Tiller, when I was hanging out with him in Montreal, was talking to me, he was like, dude, you should put this in your slides. What this is, is VM Map is a tool available on OS X for map processing. Mapping the binary at run time, just figuring out where the memory is laid out. There's different tools on different platforms, this happens to be the one on OS X. Just out of curiosity, was anybody at Tiller and Dave's talk earlier? A few of you. If you missed it, what they did is they presented on retrace, and I'll get to that in a little bit, but you should definitely check out the slides, because they did some really interesting work there. So if you're on OS X doing some work, you're going to need a hex editor. I've got a couple I'm going to show you. Hexer is one that's written by Zynamix, formerly Saber, the same guys who do Bindiff. SP threw this together, and it's a fairly nice hex editor for OS X. It has a pretty solid architecture for building plugins and scripts. If you've used 010 editor on Windows, you can build very similar structures in either Ruby, Java, and Python, I believe. It's pretty good. But in the scripting architecture for Hexer. And if you ever have a question or a bug report, SP will probably answer you by the end of the day. Hex Fiend is another one that the Modisano guys pointed out about a year ago, I believe. And they asked him to open source it, and he did open source it. At that point, I think he stopped maintaining it. So it's an open source hex editor. It handles really large files fairly well, and it runs natively on OS X. So if you just want to mess around with that, it's available there. OXN, yet another one, takes plugins, you're like, are you really going to show me hex editors for the whole presentation? And the answer is, no, I'm going to stop now. I'm going to move on to some real stuff. OTool. This is what a lot of people sort of started using when they were reverse engineering binaries on OS X. Some of the people who have been doing it for longer, they're fairly comfortable with it. But it gives you AT&T, no, yes, AT&T syntax. Well, and this is what it does. It gives you AT&T syntax. Well, and this is what it does. It disassembles all of the binaries. It's like object dump, except they call it OTool. And you can list the libraries like you could in object dump. And the problem with OTool is that it's just straight disassembly. There's a lot of very, very basic stuff you could put together, and somebody did it. And OTX is that tool. I've colored it in blue so you can see. If you don't know Objective-C, this is probably meaningless to you, but don't worry. I'm going to give you a crash course in Objective-C. In about three minutes. But you can see the strings being dereferenced, the alloc here, the NSMutable array. So if you know Objective-C, you're like, oh, cool, that just saved me the 30 seconds it would take to figure it out on my own. But it works fairly fast, and it just takes the output of OTool. So you can pick it up there. Class dump. That's going to dump the headers for you. Now. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . of the verboseness of all the information it is giving you, so it's pretty cool for that stuff. Now you're like, Cam, you've gone crazy. IDA, that's a Windows tool. We're all Mac users. I use IDA on Parallels. I came from a Windows reverse engineering background. I just run Parallels. If you're running one instance of IDA, it'll be fairly stable. If you run two, then it looks roughly like this and it starts to choke up. But we've got two. We'll get to those in a little bit. But for the most part, some people like OTool. I like a lot more information. I like all the features that IDA has, and I'm really comfortable scripting and writing plug-ins in IDA, so I'll generally use it there. Now the second bullet point says IDA Pro for OS X runs on the console. It does. I have the 5.3 version, so the most recent version. It's really buggy. If you're creating just D words, it'll get segfault on you. So for the most part, unless I'm going to run it in batch mode, I'll keep using IDA on Windows. So there's IDA. There's Parallels. This isn't new to most of you guys. They're not free, but easily worth the money if you're going to be doing this seriously. And then Debuggers. This is one of the short... This is one of the weaker aspects of reversing on OS X. Nobody I've talked to has really settled on one that they're like, dude, this is totally awesome. They're like, it kind of works, and it fixes this one problem this other debugger had. But Charlie Miller ported PyDebug to OS X a while ago, which is the PyMay scriptable Python debugger. Obviously, GDB is going to be... On your stock install. When I say stock install, I mean stock install with Xcode. Does anybody here not know what Xcode is? All right, cool. I can save that in 30 seconds. So PyGDB, which is another Python interface through GDB, is available on Google Code. Vtrace, yet another debugger, is available at Kenshodo. And Tiller and Dave, the guys who were presenting earlier, are going to be using it. So it's going to be a lot easier. I don't know if they released it today, or they will be releasing it shortly, or if you just have to email them and they'll send it to you. But they have Redbug, which is a Ruby scriptable debugger as well, that's set to interface with the retrace set of tools. End of the day? Okay, that's cool. And end of the day, this is probably where you're going to want to go get it. Poppopret.org. What retrace is, is a Ruby framework to interact with Dtrace. And I'm not going to spend a lot of time on it. I'm going to spend a lot of time on it. I'm not going to spend a lot of time on it. Because you have all of their slides. And they wrote a really good white paper, so you can take a look at that after the fact. But it's just basically scripting Dtrace. So we're into the Objective-C portion of the talk. I think I'm talking extremely fast. I am. Okay, I'm just going to stand here for a second and stare at you. That guy. That guy got me a Redbull right beforehand, which was a bad idea. Okay. Okay. Objective-C is, or we're not even there. We're in the calling convention part. I'm not talking that fast. Okay. Calling conventions. I saw Objective-C and I panicked. The calling conventions are standard call, which is going to be pushing the variables onto the stack, except it's not pushing them. It pre-allocates the stack space. So if you're used to push-push-push-call, it's doing the exact same thing, but it's pre-allocated. So you can see right in the middle here, you've got var 98, which is actually just ESP with no modifier, and then 94, which is the plus four. And so it's just putting it on the stack space, and it'll call message send in this case. And it takes care of all of that. Now, the first tendency as somebody using IDA would be to write a quick script to rename all of these variables. But it will allocate all of these variables, and it will allocate all the stack space, and then possibly later allocate further stack space. So there's a little hiccup into the naming scheme. Once again, I've already done that in the Python script that's included. Just so you know, I'm not lying to you. Oh, totally lying to you. No, it's right here. This is the thing. It's just basically a scratch pad for stuff I've noticed when I've been working on this. So... That's the only major difference between various other binaries that are using standard call. So just whatever you do before you rename them, check the stack delta, and you should be good to go. Now, in pretty much every OSX binary, it's set up to be using position independent code. So you're going to be seeing this, like, EBX value that's all over the place. Now, just out of curiosity, how many people do use IDA for reversing purposes? A couple of you guys. So the rest of you, if you don't have IDA and you start to get IDA, you're going to get version 5.3, which fixes this. But for the four of you guys probably using 5.2 or older, I'm going to keep going. EBX is the Position Independent Anchor Point. So it's just basically... It's not the start of the function, it's just a random address in the function. function. They use that to reference all of the other position independent code or the data that it's going to be referring to. EBX is most frequently, I don't think I've ever seen anything that's not EBX, but I'm not going to say it won't be. I don't want you to wake up six months from now and be like, yeah, you lied to me, which I might. There's two ways to generate the anchor. The first one is it's going to call up a function. I just called it getPC because that's what it's doing. It's going to probably be some 90476 when you see it. All it's doing is dereferencing the stack pointer, moving it into EBX, and returning, which is just going to be the return address, which is the exact same as calling five and popping EBX because the value would have been stored into EBX. If you've done any shell code, the second half of the code will be sent in. half. The second version looks very familiar because it's using a lot of shell code. This was all fixed in IDA 5.3. I wrote a script to fix it, and I was working on a plug-in to fix it, and then ILFAC fixed it himself, so you don't need any of my tools for this. And his works faster. So you can see at the top of this, you have the call plus 5, the pop EBX. That's saying that EBX is going to be the 1DO6 address value. And then later on, where is it? I can't even see it here. This is probably a bad example. No, you can see it. You can see the init. This is where the text is coming from. The EBX plus 1316 in hex is actually a string to the NS-Auto release pool. That's being called later. And depending on where it is in the binary, it's either going to be just a small little value, or it'll be this massive D word that's pulled from another section. Now there's two main ways of writing, or there's two main frameworks for writing code on OSX systems. Carbon is the C, C++ based framework that's going to be used. It was descended from the original Mac toolbox. So this is the point where I ask how long you guys have been using Mac. How many guys have been using Mac since they were actually next step machines? Okay, a couple of you guys. You guys are going to be... Feel free to correct me if I'm ever talking smack on this section, because I'm not much of a carbon coder. Now, do we have many carbon programmers in the house? That guy? All right, do we have any Objective-C programmers in the house? All right, a couple more. So carbon is the C, C++ stuff. It's the stuff Windows programmers feel most comfortable moving to, because they don't have to learn another language. Apple, in all their documentation, really encourages you to keep on trucking over to the cocoa-Objective-C world, but a lot of people in their... Yeah, I don't know if you know that, but I think attempt to get stuff ported over quickly, we'll be using Cocoa. POGR applications are written in Cocoa. There's usually a two-letter prefix on framework libraries, so HINCG is going to indicate that they're probably written in Carbon. Objective C, which is what I program in mainly now, is created in the 80s by StepStone. Then it was popularized by Next, so the five guys who were using Next in the late 80s remember this stuff. The original version of Doom, I believe, is written in it. It was inspired by Smalltalk, and so it's a set of decorators on top of C, which means they change it around. People, you start to try and explain to them, they're like, oh, it's like C++, and you're like, no, not really. But the functions aren't called. It's a message-sending architecture. When you see all these strings that I was showing you earlier that are just scattered around the binary, instead of having a direct cross-reference to those functions, it's going to pass in that string, and the underlying system will go through and figure out which function you're actually trying to call and translate it. Unicode strings are standard applications. They're not called. They're not called. They're not called. They're not standardized, but it's generally, like, two-byte Unicodes are supposed to be standardized, but it's actually stored internally, for the most part, as null-terminated UTF-8 strings, which is Unicode. But it looks like it was standard C libraries are referred to as frameworks, so I keep dropping that name. Now you know what I was talking about. Now Objective-C has a lot of framework classes to call from. It's like other managed languages, like .NET. or Java, where there's a whole huge set of frameworks that are all built together. The common framework classes, they all have the NS or CF prepended. NS is actually the Objective-C stuff, and CF might be derived from some of the other carbon, like C++ stuff. But pretty much every, it's standardized that every framework is going to have a two-capital letter prefix. And when people will use COCOA and Objective-C interchangeably, the system API for OSX is actually what COCOA is. AppKit is the set of GUI framework classes available on COCOA. If you're working on the iPhone, it's going to be UIKit, which is a scaled-down, slightly different, some totally different controls, custom libraries. So AppKit will be using the UIKit, which is using the NS prefix. And UI will be using UI. Now you're probably wondering why I'm giving a crash course into Objective-C programming if you're reverse engineering and not engineering, but it makes a huge difference if you understand what the original code was probably going to look like. And then when you see it in the binary, it's very easy to go backwards to what you think it might have been. So here's your super basic lesson in Objective-C programming. You have your variable here, your X, and then you have your statement, which is a method call. So you have the square brackets, which is your selector decorators, which is the way the Objective-C compiler will identify an Objective-C statement. And if you've got a lot of arrays, C arrays and Objective-C methods all on top of each other, it gets kind of confusing. But it stands out fairly well. You've got the recipient, which is the blue object here. Now, what that is, that's a pointer to any Objective-C-based object. And then we have the selectors, which are the text strings. It's very similar to named functions in, like, various other scripting languages, like Python, except you can't mess around with the order. They always have to be in the same order. But they are there, so it makes it very easy to read Objective-C code, because you haven't, basically, you have to describe what's, you don't have to. You can have a list of things that you want to have A, B, C, and B a dick. But for the most part, people are going to describe what the actual variables are supposed to be doing. And then you have the arguments, obviously. So calls to these selectors are just basically taken and converted to this one function call. And this is, well, there's a couple versions of it, but this is the main function call that you're going to see in Objective-C binaries. Now the problem with this is that if you've done any IDA work or various other functions, you're going to see a lot of these in Objective-C disassembly work where the cross-references are built up for you, you can do flow graphs, you can run bin diff, you can't do it because they're all calling that one function. So you have this awesome flow graph where everything calls message send and you're like, wow, that sucks. It does. So what it does is it translates the square brackets into this message send call. So you can see here the recipient's still there. It's the first thing being passed in. The selectors are all combined together and they're separated by the colons, but you can still tell how many arguments there are going to be by the number of colons there are. And then you have the arguments, which is just an ellipsis that will go on for as long as the arguments are being passed in. Message send super is another variation of the message send call. It's going to be very similar to the original message send call, but it's basically calling it on a superclass. So the big difference is it's passing in a pointer to this super, and then it's the exact same with the rest of the arguments coming after it. And the return value is still passed in. It's going to be passed in through EX all the time, but it's still passed in the same way. Then you have floating point return value version of message send. Instead of passing a pointer back, it's going to pass a double back. You have the structure version. Now this is the one where it changes slightly because the return value that's coming back is actually going to be the first argument being passed into the function. And then after that, it repeats the same thing. And then super strut. Once again, I'm sure we can all figure out what that one is. I believe in you. So in assembly, this is what it's going to look like. I've described it, but you can see the selectors being passed in right here with the colon. And that's your request. It's got a two-capital-letter prefix, so we all know that that's going to be one of the Objective-C-based framework classes. And this one's kind of weird because it sort of does it out of order, but I've added a script that will just go through and move these stuff around. So you've got A, which is going to be the return value coming back through EAX. And so you can see NSURL. So it's a class method. It's not an instance method. And then you've got the return value. And then it calls the selector URL with string with one argument, which is probably this one right here. And then it does it again for requestWithURL. But it's pretty straightforward because you can just see these Objective-C message send calls. I usually rename these just so it's easier for me. But the message send calls, they just come one right after after another. So you can imagine that if you're trying to trace this through, it's kind of a bitch because there's no way to double-click. You have to go through and figure out where the URLWithString function actually is. Luckily, you can do that. So the suspense was killing you. But yes, all of this stuff is stored in the Objective-C segment of the binary that I talked about earlier, where all this metadata is stored. You can figure out that this is a DownloadDelegate class here, and all of this stuff is stored in these structures. Depending on what version of IDA you're using, if you use it, it'll rebuild it for you. Or it's very, very easy to put together. You hit DD like three times in a row and then you're done. But for most OSX binaries with their Objective-C stuff, you got an offset to the string, which is the string. You have an offset to the actual selector string, you have an offset to these variable type arguments, and you have an offset to the actual function itself. So if you know the name of the function and the function itself, it's really not that much more of a step to go through and name those. This is one of the really cool things about Objective-C binaries and metadata, is that there is all this information just waiting for you to go through and pull it out and screw with it. So some of the stuff you don't have, like that you'd have on Windows, like cross-references, you do have a ton of information being presented to you that will make your life a little bit easier. So instead of having to figure that this calls this and this calls this and this, I figured out this one does this and I traced these back, it'll just tell you, it's like, I think it's going to download it and then the argument is going to be the amount of data it received. Just a guess, because it says that's what it's doing. Sometimes double-check and you'll find it. Some developers like to lie, but for the most part they're pretty trustworthy people. Now if you look right here in this middle, there's this almost gibberish-like thing. Almost. It's a type encoding, and what that is is a collection of various characters that are used to indicate what kind of data types and how big the data types are for the arguments and return value. So you can build up all the rest of it. So it's not only do you know you have two arguments, you know that one of them is a char and the other one is an ID. Now ID is this sort of concept of this, it's like a void pointer, but for Objective-C, saying that it's going to be an object. So it's slightly a bit less ambiguous than void pointer, but roughly that's what it is. You have all the rest of the data types are assigned, you can identify that it's an array. A bit field, etc. This is straight from Apple's docs, I just put it in a nice LaTeX table. So you can save your time if you want to not look for it. So this is one of the type encodings from one of the previous examples. And the first thing you're going to see is return, which is a V. Cryptically, it stands for void. The next thing being indicated is that there's an ID here. And so that means, you know, it's going to be an instance method. So it's not like you'll actually need the instance of the Cocoa object being passed into the selector. Then you have the selector itself. There's always going to be a colon in that case. The first argument is another at symbol, which is an ID object. Second one is also an ID object. This isn't the most cool, fun example, but it's pretty straightforward. So you also have these numbers, which I neglected to explain earlier. And what they are is stack offsets of where they are in the call stack, or like being passed in. And you can use that if it's a data type that you're not really sure what size it is. But for the most part, you're pretty sure what size it is, because most pointers are going to be 32-bit in 32-bit architecture. Which is the bulk of it. And most integers are going to be the same size after that. But the interesting thing is that you have this 16 that it starts with. And so that 16 actually goes and lets you know that the last object is also going to be 4 bytes. It doesn't mean anything else. I've asked around, and nobody has any more insight to that. So. For me, it's pretty simple. I'm just going to go ahead and do a little bit more of this. For memory management in Objective-C, this is the way memories, or objects are created. It's not a malloc basis. You don't call malloc directly from the Objective-C stuff. You call alloc itself, which is a selector, and init. Doesn't have to always be an init on the developer's choice on that one. But when you see these in the binary, that's what's going on. Release is the equivalent of free. Generally, it's reference counting architecture, so release just decrements the references by one. And if you want to add a reference so that it doesn't get automatically cleaned up, then retain is going to be what you call for that. And the part of the Objective-C runtime that's going to clean up all of these zero-referenced objects is an auto-release pool for the most part. And so you'll see one of them declared at the beginning of a function. You'll see it released near the end or near any return statement if the code was written right. And they can be nested in loops. So if you see a pool getting allocated right at the beginning of a loop, you can try to find where that same pool would be getting released, and that'll help you figure out where the end of the loop is probably going to be. So I'm just showing you the Objective-C stuff, but it's pretty straightforward because you just have the same text strings all over in the binaries to save you the time. Garbage collection. This was added in OS X 10.5, the most recent version, and I haven't seen it used very much yet, but you'll probably start to see it used a little bit more because then people don't have to worry about calling release and retain as much, and developers tend to be really lazy. I know, I did that for a while, and I am lazy. So classes designed for garbage collection, they have a finalized selector somewhere in their class definitions. That's sort of your big tip-off right there. So you're like, okay, I'm not going to really need to look for any of the other memory allocation stuff. They can be triggered by collect exhaustively, collect if needed. So those are also going to indicate garbage collections going on. Those are probably a little bit more trustworthy than finalized because finalized collections are going to be more trustworthy. Finalized is one word. Maybe people want to use it just so that the code knows that they're serious. Garbage collection is not available on the iPhone. So if you see it on an iPhone binary, they're lying to you, and it's something else. And the iPhone developers are liars. Trust me on that one. But categories, this is one of those really crazy things that you really need to pay attention to at this point. A lot of the times, I've seen like three or four people write the same scripts I've written in IDA, because it really doesn't take that much extra time to figure it out. But if you don't want to spend the time, you can use one of the few ones that I show you at the end. But for some reason, when people start taking the metadata and renaming functions, they always pick the category section of the Objective-C first. I'm going to go right back and show you the Objective-C sections so you can see what the hell I'm talking about. Right here. Here. The first one down is cat class methods. The fourth one down is cat ints methods. These are the categories. Now, the structure is almost identical to oh, I went way too far. The structure is almost identical to the other the regular classes and the way that stuff's laid out. But the big thing is that a category is somebody overriding a class that they didn't write themselves. So you'd use a category to overwrite NSString, which is the default string type. And let's say you create a category for the string length, and you have it return the string length plus seven, just because you want to screw with anybody who wants to know the name of the length of your string, and you're just kind of a jerk. Well, if you go through and it's still going to be called length, so in the metadata, it's going to say length. You go through and you rename everything, and you're like, oh, that's just string length. I don't really care what that is. So, categories, pay attention to that because there's something extra going on and it's somebody messing with somebody else's code. So there might be some interesting stuff that you can find in those. But you can let's see, yeah, so it's just going to be in the cat sections of the binary. If you're renaming stuff, keep that in mind. Maybe put a cat or some other symbol that's going to be in the name so you know when you're going through it later that okay, there's something extra weird going on. Timers. This is probably only interesting to people who crack software. I'm talking about the people that crack software and totally pay for it afterwards because we're all honest. He knows what I'm talking about. They're commonly used in protection schemes. Objective C supports various ways to create a timer. You'll see NSTimer with NSThreading stuff. Or you'll see it with NSOperationQueue. This is a new one that's been added in 10.5 as well. And if you see these going on, it's pretty obvious what NSTimer is. NSOperationQueue is very frequently used with timers. Now, I mentioned I came from a Windows background. And the one benefit with Windows is that there's been decades of people Well, I don't know if it's really been decades. It's like 1995, right? A while of people doing RCE on Windows. There's a lot of documentation. There's a lot of various advances that people have been doing over the years. And they're very public about it. A lot of the CragMe sites are going to be primarily Windows-based CragMe sites. All of the docs that people are still doing. You look on OpenRCE, you'll find the obscure ARM posts. And then most of them are just going to be Windows-based stuff. The interesting thing with Mac stuff is that now, the fact that I filled this room means that there's a lot more people starting to pay attention to Mac. And it's not just on our side. It's on the development side. So some guy walks up to their Windows developers and be like, oh, we totally need a Mac version of our product. And you need to write it. And they're just like, oh shit, I don't know anything about writing for Mac products. So usually they're going to switch over and write to Carbon. And you're going to see a lot of really weird stuff. Unless they move in the other direction. Because you work for Apple and they're like, we totally need Safari on Windows. Because that makes sense. But I don't work for Apple, in case you picked up on that. But there's going to be various stuff that's ported over. You can tell it's a Windows app because we know who develops Objective-C. Who here has developed Windows code in Visual C, something like that? Like real apps before? I mean real Windows apps. I'm not saying OSX are not real. Okay. So you've got a bunch of people, you know HWND? It's all over any Windows code. Yeah, it's not a Mac thing. So if you see HWND, like variables and functions with that name in there, chances are it was a Windows guy and he's just learning how to code for OSX right now. So take a little closer look because he's probably done some really other interesting, fun stuff. But it makes no sense. Now, there's benefits for looking at Mac executables. Compression and pack executables aren't really that common on OSX. They're not as common as they are on Windows. I have co-workers that recently busted into Disney's Pirates of the Caribbean game. You can see their videos on YouTube and then like a million teenagers being like OMG hacks, show me hows. But the one they spent about 3-4 days just making this really nice unpacker for Disney stuff. And I'm like dude, check it out. There's no packing on the OSX version. The look they shot me was just like they were not pleasantly happy. I thought it was funny though. Because at one point we all used Apples and I'm the only guy left. But yay for the Apple guy. So as I mentioned, you can easily identify a lot of Windows stuff that's been ported over. Like the HWND stuff. If there is a Windows version don't be afraid to look at that as well. Because that's just yet another source of information that you can pull from. They're not going to be identical. A good example is QuickTime. QuickTime for Windows and QuickTime for Apple are fairly different animals. So there's going to be some weird stuff you find in QuickTime for Windows, but that's only because it was moved in the other direction. But you can find relative structures and you can get an idea of what it was going for. Especially if it was the code that somebody just forced to work on the other platform. So try looking at both of them. It'll just give you more of an idea of what's going on. So now we're in the iPhone section of the talk. Just in time. I don't do a ton of iPhone stuff. I actually just got my iPhone a few weeks ago. I was one of those guys who waited in line way too long. I'm not the only one. I saw the rest of you guys. Actually, is there anybody from the iPhone dev team here in the audience? That's too bad. I'll have a link to these guys. These are the guys who jail-braked the iPhone. They do a lot of the iPhone research. They do some badass stuff. They're pretty sharp guys. I wanted to applaud them if they're here. We can applaud them if they're not here, but it's sort of meaningless. The amount of stuff that's come from that is pretty impressive. The iPhone is an ARM chipset as opposed to the PPC and x86. You're going to be looking at it on regular Macs. It's Objective-C applications. That's why I still covered all that stuff. It uses UIKit instead of AppKit, which means you're going to be dealing with slightly different controls when you're looking at the binaries. The biggest thing is going to be ARM. If you don't know ARM, then you're just going to be like, wow, this is fucked up. If you do know ARM, you're like, dude, that's easy. The bundles. I said I wasn't going to lie to you. I was totally lying when I said that, so you got a conundrum. The bundles I showed you earlier don't apply on the iPhone. The iPhone flattened the whole directory structure. If you're used to looking at nicely organized bundles and you don't even have to worry about all the graphics being in the same area, no, it's all there. It's just this giant clusterfuck of files all in one directory. They have the code signature in there. It's really easy, though. I haven't seen an iPhone app that has a different name for the binary than for the actual folder structure. You can always use file just to tell what it is. The Objective-C sections, my previous slide that had the documented sections, that was before I got an iPhone. The Objective-C sections doesn't really look the same and isn't quite handled the same. The names don't quite line up exactly. I'll be spending tomorrow, instead of chilling at the Hard Rock, I'll be writing, updating my script for you guys for Monday. I should have some stuff to clean up some of the iPhone binaries by Monday. The other thing is, Apple really hasn't standardized anything here. I was hanging out with a couple of the Apple security guys at Black Hat. I was basically bitching at them. I'm like, dude, this makes my job far more of a pain in the ass. First off, we don't care. Apple still hasn't quite figured out the structure they want for all of these files yet. They're still changing it from patch to patch. When in doubt, refer to the iPhone dev team. They have a wiki up, which I'll link to. If they don't have the information, then if you figure it out, put it on the wiki. We've hit the end of my talk. You're like, dude, that was all you were saying about iPhones? I'm like, yeah, I'm kind of a jerk. This is a couple of links to get you started with. I can't not mention the first link. If you ever see any OSX talk, you gotta give props to Nemo. Nemo was doing stuff when nobody else was. He was working in a vacuum and it was a sad life. He gets all the respect finally years later. That's who we should also applaud. Big ups for Nemo. He's not here right now. He was doing a lot of this work years and years and years ago. I can't not mention him. He wrote an Objective-C fixer-upper script in IDC. I was talking about mine. This is a separate one. It works just as well. FileOffset also did the same thing. Went through OTX and wrote a LUIS script that will convert the OTX output into IDC and stick that in IDA. We all take different approaches for the same thing. Then there's a couple other Objective-C based reversing or just Objective-C coding, debugging. Then you have the iPhone dev team. You have the blog.iphone.dev which is just going to keep their up-to-date and then their wiki at the bottom. They did release a new jailbreak for any of you guys who accidentally updated earlier this week. You should be able to fix your phone. The other thing that I'm really excited coming out soon is Dino and Charlie. The guys who have won the CanSec West Pwn2Own contest. Charlie took out the MacBook this year. On the second day in the first 35 seconds. He knows a little bit about exploiting stuff. He has Feng Shui ported to the iPhone. So your iPhone is totally not safe even though there's not a huge memory footprint. You're like, you couldn't heap-spray me? He kind of screwed you. They're writing a book and it's going to cover reverse engineering. It's going to cover exploitation. It's like shell-coded handbook for Apple users by two really smart guys. They're going to be releasing a tool when this book comes out to fix that cross-references problem that I was talking about. He basically took Chris Eagle's emulator and just went ninja on it and made it rebuild all the cross-references. So that will come out at the same time so I can't not pimp them. That's the bulk of my talk. I will be available for Q&A right across the hall right after this. Thank you guys for showing up. Really appreciate it.