1 00:00:00,720 --> 00:00:05,120 So we're going to talk today about some Pyretic, which is a framework that I wrote for reverse 2 00:00:05,120 --> 00:00:09,820 engineering in Python at the Python layer. It's normally like about an hour-long talk and 3 00:00:09,820 --> 00:00:13,460 apparently I've got to do it in 45, so we're going to fly through some stuff. Probably not 4 00:00:13,460 --> 00:00:18,320 spend so much time on the understanding of the Python language. I assume if you're here you've 5 00:00:18,320 --> 00:00:26,920 at least got some understanding of that. So why reverse Python, which is probably a good 6 00:00:26,920 --> 00:00:34,280 question to begin with. Most people reverse at C or the assembly layer. I needed to assess the 7 00:00:34,280 --> 00:00:41,920 security posture of some closed source Python and the people who wrote that application had gone 8 00:00:41,920 --> 00:00:47,360 to some length to try and stop me assessing that security posture. So obviously it was a personal 9 00:00:47,360 --> 00:00:55,020 challenge of I would defeat them. All the available toolkits really didn't work with anything other 10 00:00:55,020 --> 00:00:56,800 than standard compiled Python byte code. 11 00:00:56,920 --> 00:01:03,160 Even the smallest obfuscation would kind of trip them up. So that was why I decided obviously I had 12 00:01:03,160 --> 00:01:09,060 to write some tools to do what I needed to do. Most of the kits, you know, will assume that all the 13 00:01:09,060 --> 00:01:14,100 decompilers and disassemblers just assume that the code that you're dealing with is standard byte 14 00:01:14,100 --> 00:01:19,380 code. You know, people won't have gone to any lengths to try and stop you reversing it. So none 15 00:01:19,840 --> 00:01:25,620 of the toolkits apart from this one now has kind of any understanding or attempt to get around any 16 00:01:26,160 --> 00:01:26,900 obfuscations which might have happened. So that's why I decided to write some tools to do what I 17 00:01:26,900 --> 00:01:27,300 think might be in place. 18 00:01:30,300 --> 00:01:37,960 Also obviously there's a huge amount of Python code out there. And a lot of these, you know, a lot of it is 19 00:01:37,960 --> 00:01:43,440 used in web applications and remote applications. So there's a big attack surface area to be working 20 00:01:43,440 --> 00:01:48,500 with. But there isn't a huge amount of work in actually doing Python-specific techniques. So it was a 21 00:01:48,500 --> 00:01:55,340 good area to carry on some research in. And maybe like in the past there actually hasn't needed to be 22 00:01:55,340 --> 00:01:56,560 much work in this space. 23 00:01:56,900 --> 00:02:01,560 Because people hadn't been distributing obfuscated Python, they'd just been throwing the PYs out. So you 24 00:02:01,560 --> 00:02:06,580 know, you could read the source code anyway. But this is changing. There's some general kind of bigger 25 00:02:06,580 --> 00:02:10,840 picture trends. Obviously people are moving away from developing in C and C++ for all the reasons 26 00:02:10,840 --> 00:02:14,740 that, you know, people have failed at it for the last 20, 30 years, found out that it's hard. 27 00:02:15,240 --> 00:02:19,900 Obviously high-level languages, Python, Ruby, Lua, et cetera, et cetera, much more rapid to develop in. 28 00:02:20,540 --> 00:02:26,780 The people that are able to develop in it are kind of straight out of university and are able to do Python much better 29 00:02:26,900 --> 00:02:31,040 than they are to be able to do C. So there's more developers. It's cheaper to develop. It's cross-platform, 30 00:02:31,200 --> 00:02:36,580 et cetera, et cetera. There's also a changing in the distribution. You know, five or six years ago, you 31 00:02:36,580 --> 00:02:41,460 always download an application. Now it's all Web 2.0, the cloud. You know, everything's got something to do 32 00:02:41,460 --> 00:02:45,280 with the cloud, but nobody knows exactly what the cloud is. It's got something to do with the network, the 33 00:02:45,280 --> 00:02:51,580 Internet. So everything now is, you know, software as a service. What this means for reverse engineering 34 00:02:51,580 --> 00:02:56,880 obviously is maybe you don't actually have access to the files in which you're trying to reverse because you're 35 00:02:56,900 --> 00:03:03,960 dealing with them on a remote server. Also, overflows aren't the only bugs. You know, everyone's obsessed 36 00:03:03,960 --> 00:03:09,040 with obviously stack overflows. It went to heap and people got more and more complicated and all the 37 00:03:09,040 --> 00:03:14,600 protections were put in place. To work up, you know, a good memory corruption bug now will take some 38 00:03:14,600 --> 00:03:20,560 talented guys, you know, a good six months of solid research. There's definitely a need for that, but the 39 00:03:20,560 --> 00:03:25,900 return on investment for those kind of bugs, you know, it's a significant investment. Some of the research is 40 00:03:25,900 --> 00:03:26,720 obviously immunity. 41 00:03:26,900 --> 00:03:35,080 Where I work, we've got the resources that we can invest six months in a bug, but obviously then the sale, the price of 42 00:03:35,080 --> 00:03:40,420 that bug has to reflect the amount of work that goes in. A lot of people aren't prepared to pay for bugs. 43 00:03:42,240 --> 00:03:47,780 Python will have lots of good bugs in it, and higher level languages can have lots of good bugs. And they're often 44 00:03:47,780 --> 00:03:54,000 very much cheaper to find because they just haven't had as much effort put into the techniques to find those 45 00:03:54,000 --> 00:03:56,880 kind of bugs. So it's easy. There's a lot more low-hanging fruit. 46 00:03:58,760 --> 00:04:03,960 Reverse engineering, a lot of the toolkits are often kind of done in a C-centric manner. With newer reflective 47 00:04:03,960 --> 00:04:09,120 languages, maybe there are some better ways. People are kind of stuck in this decompilation mindset. Maybe 48 00:04:09,120 --> 00:04:14,060 there are some better ways to go around it. And obviously lots of the toolkits, certainly anything to do with 49 00:04:14,060 --> 00:04:19,660 Python reversing, relied the fact that you had access to the file on disk, that you could do kind of a static 50 00:04:19,660 --> 00:04:26,780 reverse on the serialized object on disk. If you're working in a service-orientated method, that might not 51 00:04:26,900 --> 00:04:32,080 be the case. You might not actually have access to the disk, the file on disk, but you do have runtime access to the 52 00:04:32,080 --> 00:04:38,140 objects on a remote server. If you can get actually source code back out of that remote object, then obviously 53 00:04:38,140 --> 00:04:43,200 that's highly desirable, but you're never going to have access to the disk. So we're going to talk a little bit more 54 00:04:43,200 --> 00:04:50,400 about that. And bug snobbery. I certainly have fallen into this category. I get loud and vocal about why am I 55 00:04:50,400 --> 00:04:56,880 having to do a week's worth of assessment on XSS and CSRF. But to be quite honest, just because you're 56 00:04:56,900 --> 00:05:02,100 not working on some hardcore overflow, you can get some pretty good bugs. If it gets you in, it gets you in. 57 00:05:02,220 --> 00:05:08,180 You shouldn't be too proud to work on an XSS, even though it's in JavaScript and that's what all the kiddies are doing. 58 00:05:08,800 --> 00:05:15,180 And there are lots of areas with low-hanging fruit that haven't really had too much research put into them yet. 59 00:05:15,960 --> 00:05:23,520 And not everybody is an eco-weissman. Not everybody can do crazy heap reversing. So I came to the conclusion 60 00:05:23,520 --> 00:05:26,780 that I'd never become as good as an eco, so I had to start looking in other areas to 61 00:05:26,900 --> 00:05:34,520 make myself feel good. So other side effects of the new development model. Obviously, everything is always in beta. 62 00:05:34,780 --> 00:05:39,960 The less experienced developers are developing code that actually is used in products. And time to market and new 63 00:05:39,960 --> 00:05:43,980 features are key. So there'll be a lot of new code that's out there that hasn't necessarily been tested as 64 00:05:43,980 --> 00:05:49,360 thoroughly as you'd like. And obviously, the flip side of all those points is that there's going to be tons of bugs. 65 00:05:49,460 --> 00:05:56,060 And that's what we like. There's often new large populations of users for whatever the in-app of that week is. 66 00:05:56,180 --> 00:05:56,880 And they can often get a little bit of a bug. And that's what we like. There's often new large populations of users for whatever the in-app of that week is. And they can 67 00:05:56,880 --> 00:06:04,020 often be rapidly seeded. So a huge population of users running some very vulnerable code will burst up. And obviously, that's the best thing that you can have. 68 00:06:04,540 --> 00:06:11,180 And a lot of these bugs, because it's in the higher level languages, are actually cross-platform and cross-architecture. Now, that's awesome. 69 00:06:11,380 --> 00:06:18,260 You write one bug and it will execute across whatever architecture system they're running on and whatever operating system they're running on. 70 00:06:18,340 --> 00:06:26,240 So you could hack an iPhone right the way up to a mainframe. If it's running Python and there's a Python-level bug, you'll be good, which is huge. 71 00:06:29,580 --> 00:06:35,940 And there's obviously all these conferences, and certainly with Black Hat, you'll get a lot of vendors peddling huge amounts of snake oil. 72 00:06:36,500 --> 00:06:40,660 We're more secure than we've ever been. But I really think it depends on what metrics you're measuring on. 73 00:06:40,940 --> 00:06:49,040 There's more lines of code than there's ever been. There's more people who think they can code. I'm sure there's everyone sitting in here knows people at their place of work 74 00:06:49,040 --> 00:06:55,840 who should never be let near a keyboard, yet they're doing code which is going out in production systems or in internal systems. 75 00:06:55,840 --> 00:07:02,960 Obviously, everything's now about connectivity. Everything's network-aware. And the pervasiveness of technology is increasing. 76 00:07:03,140 --> 00:07:10,400 So I'm not sure, really, that we are more secure than ever. And the higher-level languages are being used increasingly to seed all this crap everywhere. 77 00:07:10,580 --> 00:07:13,740 So if we can find some good techniques to exploit them, then we will. 78 00:07:14,520 --> 00:07:22,040 What's not going to be discussed? There's not going to be any dropping of commercial application source code that I've reversed out from anything or any bugs that I found within, 79 00:07:22,700 --> 00:07:25,820 mainly because the lawyers don't seem to agree that what happens is going to happen. 80 00:07:25,820 --> 00:07:28,740 It happens in Vegas, stays in Vegas, and I can't afford to be taken to court. 81 00:07:30,920 --> 00:07:36,080 So why reversing a high layer? Most people assume that going in to the lowest layer is the best thing. 82 00:07:36,280 --> 00:07:43,120 But if an application has been written in a high-level language and reversing it at the layer it's been written means that you're closer to the developer. 83 00:07:43,260 --> 00:07:48,300 You're closer to the information. Further from the data, well, but closer to the information. 84 00:07:48,540 --> 00:07:51,000 So you can get a much better sense of bugs that might be around. 85 00:07:52,480 --> 00:07:55,500 We're not assessing the security posture of the Python runtime. 86 00:07:55,820 --> 00:07:59,600 That does have a ton of bugs, but we're looking for bugs in the Python code itself. 87 00:07:59,960 --> 00:08:04,560 So reversing out the layer that the code was implemented in is the best thing to do. 88 00:08:05,860 --> 00:08:13,900 I'm not sure how clear this will come out, but you should be able to see here this is reversing like in a normal debugger. 89 00:08:13,940 --> 00:08:17,400 And you'll see a lot of call-outs to kind of the Python DLLs. 90 00:08:17,600 --> 00:08:23,540 Even to do something simple, like print a hello world, there's actually a lot of layers in between you and the code. 91 00:08:23,720 --> 00:08:25,160 So it gets very complicated. 92 00:08:25,820 --> 00:08:28,440 Even to do simple things can actually take quite a lot of effort. 93 00:08:31,660 --> 00:08:34,460 And obviously, Python is a fairly complex language. 94 00:08:34,680 --> 00:08:38,060 It's got quirks and flaws and bugs just like every other language. 95 00:08:38,720 --> 00:08:42,600 But like I say, a lot of the people that develop in it are maybe less experienced developers. 96 00:08:42,800 --> 00:08:46,380 They don't really understand so much about how computars work. 97 00:08:47,620 --> 00:08:50,060 So there's kind of mistakes that everybody makes. 98 00:08:50,180 --> 00:08:51,500 I'm going to just highlight a couple. 99 00:08:53,100 --> 00:08:57,620 Anybody out there that's good at Python that can see the problem with this code? 100 00:08:59,720 --> 00:09:00,160 Shout. 101 00:09:00,760 --> 00:09:02,000 All right, I won't wait long. 102 00:09:02,220 --> 00:09:03,520 There's only one var. 103 00:09:04,180 --> 00:09:07,560 Well, yeah, the var is a class rather than an instance attribute. 104 00:09:07,960 --> 00:09:13,340 So you can see if you, on foo and var, which are the instantiations of the test class, 105 00:09:13,560 --> 00:09:19,960 if you actually print out the var variable, both of those instances of the class share the same variable 106 00:09:19,960 --> 00:09:21,160 because the variable is at the... 107 00:09:21,500 --> 00:09:22,240 at the class layer. 108 00:09:23,000 --> 00:09:24,580 People make this mistake all the time. 109 00:09:25,020 --> 00:09:28,100 If they make a new instance of an object on a shared system, 110 00:09:28,280 --> 00:09:31,260 you can actually get access, depending how they use the class, obviously, 111 00:09:31,380 --> 00:09:35,760 you can get access to other people's objects, which, depending on the situation, can be a good thing. 112 00:09:36,960 --> 00:09:38,440 Anybody can see the problem in this? 113 00:09:42,220 --> 00:09:42,620 Okay. 114 00:09:44,160 --> 00:09:45,840 It's a mutable default argument here. 115 00:09:46,000 --> 00:09:49,020 So when it was called here with an argument going in, we're all good. 116 00:09:49,160 --> 00:09:50,640 It acts as, you know... 117 00:09:50,640 --> 00:09:52,560 foo is appended to the end of the list that was supplied. 118 00:09:52,920 --> 00:09:53,700 That's as expected. 119 00:09:54,340 --> 00:09:57,460 If it's called without an argument, the default argument is used, 120 00:09:57,560 --> 00:10:00,700 but the default argument is made in instantiation time. 121 00:10:01,080 --> 00:10:02,160 So, again, it is shared. 122 00:10:02,320 --> 00:10:05,020 So you call it twice, and the list is growing. 123 00:10:05,580 --> 00:10:07,460 I've seen a lot of people make this mistake. 124 00:10:07,940 --> 00:10:10,700 Certainly in remote applications, they'll have socket objects here. 125 00:10:10,820 --> 00:10:12,600 It means you can access somebody else's socket object, 126 00:10:12,700 --> 00:10:15,420 and then you've got a route back to a different client. 127 00:10:16,080 --> 00:10:18,660 So, obviously, like every language, there's a ton of these bugs 128 00:10:18,660 --> 00:10:20,460 that maybe inexperienced developers, 129 00:10:20,640 --> 00:10:22,440 don't really understand because they don't understand 130 00:10:23,000 --> 00:10:24,760 really how Python's doing things. 131 00:10:24,840 --> 00:10:26,300 It's just a fairly easy language to write in. 132 00:10:26,980 --> 00:10:29,440 So lots of people make some pretty simple mistakes. 133 00:10:31,920 --> 00:10:34,260 So what were my initial aims for doing this? 134 00:10:34,420 --> 00:10:37,500 I wanted to be able to have a toolkit that would rapidly assess 135 00:10:37,500 --> 00:10:39,680 and find bugs within applications, 136 00:10:40,740 --> 00:10:42,040 even if they were obfuscated, 137 00:10:42,060 --> 00:10:45,120 and I didn't have access to the .py's themselves. 138 00:10:45,620 --> 00:10:47,400 Obviously, I wanted to get back to a source code representation 139 00:10:48,380 --> 00:10:50,080 from, you know, a live memory object. 140 00:10:50,640 --> 00:10:52,320 And I'd prefer to have a general approach 141 00:10:52,320 --> 00:10:54,440 against all the ways that people are obfuscating bytecode 142 00:10:54,440 --> 00:10:56,720 rather than a specific approach for each different thing 143 00:10:56,720 --> 00:10:58,480 because, obviously, that's a cat and mouse game 144 00:10:58,480 --> 00:11:00,080 and it's going to take a lot of time to carry on. 145 00:11:00,220 --> 00:11:03,220 So if there was a general way of defeating what people were doing, 146 00:11:03,880 --> 00:11:05,200 then I wanted to take that. 147 00:11:05,280 --> 00:11:07,260 Obviously, there was because I'm giving this talk. 148 00:11:08,420 --> 00:11:10,580 So we're going to blast through this because we haven't got much time. 149 00:11:11,120 --> 00:11:13,320 Like 101 of Python language. 150 00:11:14,020 --> 00:11:16,940 So, obviously, there's a fair amount of different file types with Python. 151 00:11:17,500 --> 00:11:19,740 The py is the one that everyone will be familiar with. 152 00:11:19,740 --> 00:11:20,680 It's where the source code lives. 153 00:11:20,800 --> 00:11:21,500 It's human-readable. 154 00:11:22,160 --> 00:11:23,220 Obviously, you can take that py. 155 00:11:23,240 --> 00:11:24,860 It will run on any Python platform. 156 00:11:26,200 --> 00:11:29,240 Then there's kind of the compiled and serialized versions 157 00:11:29,240 --> 00:11:30,680 of the Python languages, 158 00:11:30,840 --> 00:11:34,220 pyc being the, you know, the kind of most ubiquitous one. 159 00:11:34,380 --> 00:11:35,620 It's a standard serialized form. 160 00:11:35,740 --> 00:11:36,940 We'll have a look at the format very quickly. 161 00:11:37,900 --> 00:11:40,320 Anytime py is compiled or imported, 162 00:11:40,920 --> 00:11:42,620 import obviously implicitly compiles, 163 00:11:42,760 --> 00:11:45,620 a pyc will pop out of the... 164 00:11:47,400 --> 00:11:48,840 A pyc will pop out, 165 00:11:48,840 --> 00:11:51,300 which is the bytecode equivalent of the py. 166 00:11:52,260 --> 00:11:53,260 Contrary to popular belief, 167 00:11:53,320 --> 00:11:54,320 it doesn't actually speed up execution. 168 00:11:54,700 --> 00:11:56,740 It purely speeds up instantiation 169 00:11:56,740 --> 00:11:58,800 because you miss out that compile step. 170 00:11:59,080 --> 00:12:01,280 So you don't need to recompile every time you run the application. 171 00:12:01,960 --> 00:12:02,980 It is cross-platform. 172 00:12:03,080 --> 00:12:05,660 A pyc will run on, you know, Linux and Windows, 173 00:12:05,900 --> 00:12:07,620 but it's not cross-Python version. 174 00:12:07,860 --> 00:12:11,540 So a 2.4 pyc won't run with a 2.5 runtime. 175 00:12:11,960 --> 00:12:15,340 And it is purposely undocumented by the Python developers 176 00:12:15,340 --> 00:12:17,880 to allow them the flexibility to change the bytecode format 177 00:12:17,880 --> 00:12:18,820 without breaking the code. 178 00:12:18,840 --> 00:12:20,360 And a bunch of stuff where people have relied on it. 179 00:12:21,880 --> 00:12:23,600 PYOs, same structure as PYCs, 180 00:12:23,720 --> 00:12:25,600 but they're optimized at some point. 181 00:12:25,660 --> 00:12:26,820 You can optimize at the first level, 182 00:12:26,940 --> 00:12:28,580 which will remove all the asserts. 183 00:12:28,780 --> 00:12:29,920 Optimize at the second level, 184 00:12:30,120 --> 00:12:31,980 it will remove all the asserts and all the inline documentation. 185 00:12:32,700 --> 00:12:34,480 Nothing to do with speed, purely file size. 186 00:12:35,700 --> 00:12:37,040 This shouldn't break most things, 187 00:12:37,180 --> 00:12:38,920 but in some kind of corner cases it will. 188 00:12:39,040 --> 00:12:40,920 Python lex and yak is one of those that 189 00:12:41,360 --> 00:12:43,640 if you remove the doc strings then everything fails 190 00:12:43,640 --> 00:12:46,620 because the actual lex grammars are kept in the doc strings. 191 00:12:46,620 --> 00:12:48,600 But most of the time a PYO, 192 00:12:48,700 --> 00:12:50,540 just smaller file size, so that's all good. 193 00:12:52,080 --> 00:12:56,180 PYD, the most complex format that Python will produce by itself. 194 00:12:57,080 --> 00:12:59,400 It comes as standard with CPython. 195 00:12:59,580 --> 00:13:00,820 I've seen a lot on Windows. 196 00:13:01,120 --> 00:13:04,360 It will compile into a shared compiled C object. 197 00:13:04,880 --> 00:13:06,400 We're not really going to talk about these. 198 00:13:06,520 --> 00:13:09,780 There's been some good work done by Aaron Portnoy and Ali. 199 00:13:11,600 --> 00:13:16,200 And they can access the PYDs and they did a toolkit 200 00:13:16,200 --> 00:13:18,880 called anti-freeze where you can unpack the PYDs, 201 00:13:18,880 --> 00:13:20,500 modify the bytecode, repack them, 202 00:13:20,680 --> 00:13:24,940 and they did some good stuff with games on Windows 203 00:13:24,940 --> 00:13:27,860 making that character jump like 20 times higher. 204 00:13:29,620 --> 00:13:31,800 So the Python PYC format, 205 00:13:31,940 --> 00:13:33,040 there's a four byte magic number. 206 00:13:33,460 --> 00:13:35,320 This is for the version of Python. 207 00:13:35,720 --> 00:13:37,360 Like I say, to make sure, 208 00:13:37,440 --> 00:13:39,640 so the runtime can do a check of what version 209 00:13:39,640 --> 00:13:42,160 the Python was compiled with and bail out 210 00:13:42,160 --> 00:13:43,640 if it's not the version that it is. 211 00:13:43,740 --> 00:13:45,380 There's a four byte timestamp. 212 00:13:46,200 --> 00:13:47,680 So why this is important later, 213 00:13:47,960 --> 00:13:50,940 basically it's to decide whether a new PYC 214 00:13:50,940 --> 00:13:52,800 should be generated from a PY. 215 00:13:53,180 --> 00:13:58,100 And there's a Marshall code object 216 00:13:58,100 --> 00:13:59,920 which is the actual serialized code object 217 00:14:00,860 --> 00:14:02,100 where the Python code resides. 218 00:14:02,400 --> 00:14:02,980 You want to say something? 219 00:14:08,380 --> 00:14:13,000 Well, we made it to 2 o'clock this afternoon 220 00:14:13,000 --> 00:14:14,120 until I had to come and yell at you. 221 00:14:16,200 --> 00:14:18,500 So, several someones are stiffing Katie's 222 00:14:19,380 --> 00:14:20,940 who have been very nice to us 223 00:14:22,100 --> 00:14:23,400 for $100 plus bills. 224 00:14:24,620 --> 00:14:27,240 As in seven people eat, get up, walk out, leave the bill. 225 00:14:28,540 --> 00:14:29,780 Very, very uncool. 226 00:14:31,860 --> 00:14:33,760 Let's apply that whole new social media thing 227 00:14:33,760 --> 00:14:34,880 or whatever you guys are calling it. 228 00:14:35,420 --> 00:14:36,520 If you know who they are, 229 00:14:37,020 --> 00:14:38,400 pressure them to go pay their bill. 230 00:14:39,180 --> 00:14:40,880 If you know who you are, go pay your bill. 231 00:14:42,460 --> 00:14:44,280 If you're thinking about doing it, don't. 232 00:14:45,740 --> 00:14:47,220 The numbers isn't enough now 233 00:14:47,220 --> 00:14:48,820 that I'm coming over here to make an announcement about it. 234 00:14:48,900 --> 00:14:50,100 So it's not just one group. 235 00:14:51,780 --> 00:14:52,780 If it keeps happening, 236 00:14:53,140 --> 00:14:55,620 you will be, they will review the video tapes. 237 00:14:56,320 --> 00:14:57,220 They will catch you. 238 00:14:57,300 --> 00:14:58,120 You will be in trouble. 239 00:14:58,700 --> 00:15:00,600 And more importantly, we will be in trouble. 240 00:15:01,580 --> 00:15:03,940 We have a good relationship here at the hotel. 241 00:15:05,180 --> 00:15:06,520 Much as we love the Alexis Park, 242 00:15:06,660 --> 00:15:08,180 I would rather not go back to the Alexis Park. 243 00:15:09,180 --> 00:15:10,620 It is not a lot of fun doing CPR 244 00:15:10,620 --> 00:15:12,100 on somebody in 110 degree weather. 245 00:15:13,260 --> 00:15:14,260 Please don't send us back to the Alexis Park. 246 00:15:14,280 --> 00:15:16,800 Or I promise you, 247 00:15:16,820 --> 00:15:17,920 you and I will sit down and have a little talk. 248 00:15:19,340 --> 00:15:19,900 Okay, guys. 249 00:15:20,400 --> 00:15:21,100 Be responsible. 250 00:15:21,580 --> 00:15:22,180 Be adult. 251 00:15:23,000 --> 00:15:24,260 Get your friends to go pay their bills. 252 00:15:25,160 --> 00:15:25,640 Thank you. 253 00:15:29,120 --> 00:15:29,480 Okay. 254 00:15:29,620 --> 00:15:30,700 So it sounds like someone's fucked up. 255 00:15:37,020 --> 00:15:41,400 Yeah, let's please not go back to the Alexis Park. 256 00:15:43,640 --> 00:15:44,000 Right. 257 00:15:44,280 --> 00:15:46,320 So that was the bytecode format for PYCs. 258 00:15:48,480 --> 00:15:50,580 All those formats come out standard as Python. 259 00:15:50,820 --> 00:15:51,680 And then optionally, 260 00:15:51,700 --> 00:15:52,800 a lot of people are using packaging. 261 00:15:53,060 --> 00:15:54,720 Packaging allows you to distribute a runtime 262 00:15:54,720 --> 00:15:56,480 along with your Python code. 263 00:15:56,680 --> 00:15:58,600 It means that people don't have to have 264 00:15:58,600 --> 00:16:00,220 the version of Python which you need 265 00:16:00,220 --> 00:16:02,820 to be installed on their system to run your code. 266 00:16:03,860 --> 00:16:05,200 So Py2Xy, Py2App, 267 00:16:05,380 --> 00:16:06,680 they're all examples of packages. 268 00:16:06,840 --> 00:16:08,760 So they'll take your code, runtime code, 269 00:16:08,980 --> 00:16:11,320 bundle it up, and allow it much easier to run 270 00:16:11,320 --> 00:16:12,060 just with a double click. 271 00:16:12,860 --> 00:16:15,120 This is important because it means that developers 272 00:16:15,120 --> 00:16:17,060 can distribute a modified Python 273 00:16:17,060 --> 00:16:18,560 runtime along with their code. 274 00:16:18,740 --> 00:16:21,240 And a lot of the anti-reversing techniques that we'll talk about 275 00:16:21,240 --> 00:16:23,080 rely on the fact that they can distribute that 276 00:16:23,080 --> 00:16:24,040 modified Python runtime. 277 00:16:26,220 --> 00:16:27,040 We're going to speed up. 278 00:16:27,440 --> 00:16:28,740 So this is the object hierarchy. 279 00:16:29,060 --> 00:16:29,780 We're going to blast through this. 280 00:16:30,380 --> 00:16:32,260 Obviously, modules, the only thing to note is that 281 00:16:32,260 --> 00:16:33,820 a module doesn't have a code level object. 282 00:16:34,020 --> 00:16:35,940 It doesn't have a code object once you've imported it. 283 00:16:36,480 --> 00:16:37,680 This is for speed reasons. 284 00:16:38,040 --> 00:16:39,300 Once it's imported, it's not needed. 285 00:16:41,320 --> 00:16:41,860 It's a language design issue. 286 00:16:42,080 --> 00:16:43,960 But from reversing, it's a real pain in the ass. 287 00:16:46,040 --> 00:16:47,680 Obviously, then there's the class level objects. 288 00:16:47,880 --> 00:16:49,780 All the superclasses to a class are in underscore bases. 289 00:16:50,500 --> 00:16:51,360 Classes have methods. 290 00:16:51,820 --> 00:16:53,620 Methods are literally just a wrapper for functions. 291 00:16:54,000 --> 00:16:55,420 The functions held in the IAM 292 00:16:55,420 --> 00:16:55,980 func object. 293 00:16:56,380 --> 00:16:57,760 Functions have a func code object. 294 00:16:57,980 --> 00:16:59,960 The code object, which is actually what we care about, 295 00:17:00,160 --> 00:17:01,320 has various attributes. 296 00:17:01,540 --> 00:17:02,780 It's a very verbose code object. 297 00:17:02,980 --> 00:17:06,200 You can tell a lot about how the code will run. 298 00:17:06,420 --> 00:17:08,460 The biggest one is the CO code object. 299 00:17:08,540 --> 00:17:10,380 That's actually a string representation of the byte code. 300 00:17:11,320 --> 00:17:12,880 You can access that and start reversing things out. 301 00:17:13,100 --> 00:17:14,400 Then there's all these attributes here. 302 00:17:15,080 --> 00:17:16,120 Constants and variable names. 303 00:17:16,640 --> 00:17:19,840 The line numbers that the function was instantiated at. 304 00:17:19,960 --> 00:17:21,080 All really useful information 305 00:17:21,080 --> 00:17:22,180 as we get into some of the techniques. 306 00:17:23,180 --> 00:17:24,420 All you need to know is that 307 00:17:24,420 --> 00:17:27,320 from all the Python objects, 308 00:17:28,400 --> 00:17:30,720 the code is the one that we're really going for. 309 00:17:30,860 --> 00:17:32,240 Everything else just stacks into that. 310 00:17:35,700 --> 00:17:37,100 We've blasted through this pretty quick. 311 00:17:37,320 --> 00:17:39,880 Python has byte code language, obviously. 312 00:17:40,040 --> 00:17:41,020 It's a pretty simple language. 313 00:17:41,320 --> 00:17:43,320 And every opcode, 314 00:17:44,940 --> 00:17:47,100 just as an 8 byte opcode, 315 00:17:47,200 --> 00:17:49,420 so there's only 256 available. 316 00:17:49,940 --> 00:17:51,700 Currently in Python 2.6, 317 00:17:51,880 --> 00:17:54,320 there's 113 already defined. 318 00:17:54,920 --> 00:17:55,520 Optionally, 319 00:17:55,520 --> 00:17:58,460 one Python opcode can take arguments. 320 00:17:58,760 --> 00:18:00,080 All arguments are 2 bytes. 321 00:18:01,940 --> 00:18:02,540 So, 322 00:18:02,620 --> 00:18:04,300 if this is a Python source, 323 00:18:04,380 --> 00:18:04,940 just print bugs. 324 00:18:05,400 --> 00:18:07,760 When it goes into its byte code instructions, 325 00:18:08,140 --> 00:18:09,160 you can see there, 326 00:18:09,320 --> 00:18:11,300 these are the different instructions. 327 00:18:11,320 --> 00:18:11,900 Names. 328 00:18:11,940 --> 00:18:14,420 Each of those will map to a single integer. 329 00:18:15,100 --> 00:18:17,240 Just a mapping between a number 330 00:18:17,240 --> 00:18:18,380 and the instruction. 331 00:18:18,780 --> 00:18:20,960 And some of them take arguments. 332 00:18:22,820 --> 00:18:24,420 And this is the byte stream. 333 00:18:24,580 --> 00:18:25,380 So you can see that 64 334 00:18:27,160 --> 00:18:28,580 would relate to load const. 335 00:18:29,600 --> 00:18:30,820 And it took two arguments, 336 00:18:30,940 --> 00:18:32,420 both of which in this case were null. 337 00:18:32,500 --> 00:18:34,640 Well, one argument is always 2 bytes. 338 00:18:36,280 --> 00:18:36,700 Fail. 339 00:18:39,680 --> 00:18:40,100 Okay. 340 00:18:41,380 --> 00:18:42,820 We're going to run through this now, 341 00:18:42,860 --> 00:18:44,240 and get on to the new techniques. 342 00:18:44,420 --> 00:18:45,620 The existing stuff that's available. 343 00:18:47,220 --> 00:18:47,620 Disassemblers. 344 00:18:47,780 --> 00:18:49,300 There's Dis that comes standard with Py. 345 00:18:50,440 --> 00:18:52,700 The representation that you saw from source code 346 00:18:52,700 --> 00:18:54,900 to instructions, that was just done using Dis. 347 00:18:55,040 --> 00:18:56,820 It will just dump out the bytecode for you. 348 00:18:57,000 --> 00:18:59,020 The important thing is it relies on opcode.py, 349 00:18:59,220 --> 00:19:00,540 which is the Python-level 350 00:19:01,200 --> 00:19:02,680 module which gives you those 351 00:19:02,680 --> 00:19:04,080 number-to-instruction mappings. 352 00:19:06,640 --> 00:19:07,600 Oh, fail again. 353 00:19:09,180 --> 00:19:10,280 So there's debuggers. 354 00:19:11,320 --> 00:19:12,440 The Python debugger comes, again, 355 00:19:12,440 --> 00:19:13,760 with normal Python. 356 00:19:14,040 --> 00:19:15,900 But it's very much a developer's debugger. 357 00:19:15,960 --> 00:19:18,020 It assumes that you're going to have access to the py. 358 00:19:18,220 --> 00:19:19,640 It is for finding bugs as you're developing 359 00:19:19,640 --> 00:19:22,120 as opposed to finding bugs in other people's code. 360 00:19:23,240 --> 00:19:24,760 So that was what I based 361 00:19:24,760 --> 00:19:25,820 a lot of my stuff on, 362 00:19:25,880 --> 00:19:27,760 but extended it up to be more useful 363 00:19:27,760 --> 00:19:29,360 when you haven't got access to a py file, 364 00:19:29,460 --> 00:19:30,120 only the py sees. 365 00:19:31,180 --> 00:19:31,540 Decompilers. 366 00:19:31,660 --> 00:19:33,100 There are a bunch of decompilers, 367 00:19:33,160 --> 00:19:35,980 some of which are an application that you download, 368 00:19:36,140 --> 00:19:37,580 some of which are an online service. 369 00:19:43,020 --> 00:19:44,120 Some are free, 370 00:19:44,200 --> 00:19:44,740 some are commercial. 371 00:19:45,560 --> 00:19:47,240 They do definitely vary in quality 372 00:19:47,240 --> 00:19:49,100 and depending on what version of Python 373 00:19:49,100 --> 00:19:49,600 you're running. 374 00:19:51,600 --> 00:19:53,320 The best free one that I found 375 00:19:53,320 --> 00:19:55,060 that I based a lot of the stuff that we talk about 376 00:19:55,060 --> 00:19:56,680 in Pyreticon is unpyc, 377 00:19:56,880 --> 00:19:58,700 from a Russian developer. 378 00:19:58,960 --> 00:20:01,160 It's not perfect, but it's good enough 379 00:20:01,160 --> 00:20:01,800 in most situations. 380 00:20:02,460 --> 00:20:03,700 For the online services, 381 00:20:03,880 --> 00:20:05,220 depython is certainly the best. 382 00:20:05,600 --> 00:20:07,880 The quality of their decompilation is really good, 383 00:20:08,000 --> 00:20:10,340 but it's not free, and you're giving them your source code, 384 00:20:11,960 --> 00:20:12,380 and you're giving them your code. 385 00:20:13,720 --> 00:20:15,020 Bytecode assemblers and modifiers. 386 00:20:16,320 --> 00:20:17,640 Byteplay and bytecode assembler 387 00:20:17,640 --> 00:20:19,140 are certainly good ones. 388 00:20:19,280 --> 00:20:20,940 Antifreeze is an example that we've already 389 00:20:20,940 --> 00:20:21,980 spoke about for PYDs. 390 00:20:22,100 --> 00:20:24,280 These allow you to work at the Python bytecode layer 391 00:20:24,280 --> 00:20:25,480 rather than at the Python source layer. 392 00:20:25,660 --> 00:20:27,560 If you're interested at this level of Python, 393 00:20:27,740 --> 00:20:28,900 they're certainly good to play with, 394 00:20:28,980 --> 00:20:30,780 and you'll learn a lot more about how the Python runtime 395 00:20:30,780 --> 00:20:32,100 actually operates by playing with them. 396 00:20:33,360 --> 00:20:34,880 So that's what exists so far. 397 00:20:35,360 --> 00:20:37,100 What are the anti-reversing techniques 398 00:20:37,100 --> 00:20:38,840 that I was seeing in a lot of these commercial apps? 399 00:20:42,420 --> 00:20:43,060 Like I say, 400 00:20:43,140 --> 00:20:44,860 increasingly, commercial and closed-source apps 401 00:20:44,860 --> 00:20:46,640 are putting obfuscation techniques in 402 00:20:46,640 --> 00:20:48,060 to stop you getting at their PY, 403 00:20:48,140 --> 00:20:50,820 because obviously they've put their effort into 404 00:20:53,520 --> 00:20:54,340 capitalizing on that, 405 00:20:54,480 --> 00:20:54,860 and 406 00:20:56,200 --> 00:20:57,500 there will be a bunch of bugs in there. 407 00:20:57,580 --> 00:20:58,640 The more effort that they go to 408 00:20:58,640 --> 00:21:00,300 to keep the source away from you, 409 00:21:00,340 --> 00:21:01,760 the more you know there's going to be bugs in there. 410 00:21:04,340 --> 00:21:05,600 I've worked with Python a lot, 411 00:21:05,660 --> 00:21:06,980 and there was a bunch of different techniques 412 00:21:06,980 --> 00:21:08,060 that I've seen, so we're going to blast through 413 00:21:08,460 --> 00:21:09,100 these techniques. 414 00:21:11,320 --> 00:21:12,280 They mainly focus on 415 00:21:12,280 --> 00:21:14,000 when the bytecode is on disk, 416 00:21:14,100 --> 00:21:16,440 when there's those PYC files on disk, 417 00:21:16,660 --> 00:21:18,320 all the techniques focus on 418 00:21:19,140 --> 00:21:20,560 obfuscating that PYC file 419 00:21:20,560 --> 00:21:21,740 so you can't... 420 00:21:22,500 --> 00:21:24,180 making it so it's not in its standard form. 421 00:21:24,320 --> 00:21:26,180 So all the basic tools that we've just talked about break. 422 00:21:26,380 --> 00:21:27,620 And that's pretty much the approach 423 00:21:27,620 --> 00:21:28,360 that everybody's taking. 424 00:21:31,040 --> 00:21:33,580 So, the simplest one that you see a fair amount 425 00:21:33,580 --> 00:21:35,300 is just hiding in the packages. 426 00:21:35,520 --> 00:21:36,740 You know, the Py2X, the Py2App. 427 00:21:36,900 --> 00:21:38,680 People believe that if they wrap their code in that, 428 00:21:38,740 --> 00:21:39,740 then you can't get access to it. 429 00:21:41,980 --> 00:21:43,320 It's easy just to reverse out. 430 00:21:43,920 --> 00:21:45,040 Often you'll find a packager 431 00:21:45,040 --> 00:21:47,820 and maybe even the PYs will be present in that. 432 00:21:47,920 --> 00:21:48,920 So that's super easy to get by. 433 00:21:49,320 --> 00:21:51,760 You often see this on Windows 32 applications. 434 00:21:52,240 --> 00:21:53,840 People will package it up into a PYD 435 00:21:53,840 --> 00:21:55,740 and assume that you can't get access to it. 436 00:21:55,820 --> 00:21:56,940 But it's a standard documented format, 437 00:21:57,040 --> 00:21:58,500 so it's very easy to just reverse out. 438 00:22:00,280 --> 00:22:01,360 Source code obfuscation. 439 00:22:01,660 --> 00:22:03,880 I've never actually seen this used in a real application. 440 00:22:04,200 --> 00:22:05,580 There's a commercial application 441 00:22:05,580 --> 00:22:07,360 that is sold to do the obfuscation, 442 00:22:07,440 --> 00:22:08,960 but I've never seen anyone actually use it. 443 00:22:09,160 --> 00:22:11,000 It's a similar kind of thing that you see in JavaScript 444 00:22:11,320 --> 00:22:13,200 malware, where they'll try and make 445 00:22:13,200 --> 00:22:14,500 the source code look complicated. 446 00:22:14,760 --> 00:22:16,100 The functionality will be the same. 447 00:22:16,720 --> 00:22:19,060 So for example, there's this kind of 448 00:22:19,880 --> 00:22:21,500 easy code on the left, 449 00:22:21,560 --> 00:22:23,380 and it goes to this kind of weird obfuscator code 450 00:22:23,380 --> 00:22:23,800 on the right. 451 00:22:25,760 --> 00:22:27,440 I'm sure it's very easy to actually 452 00:22:27,440 --> 00:22:28,320 undo what they've done. 453 00:22:28,700 --> 00:22:30,500 I haven't looked into doing it, because like I said, 454 00:22:30,540 --> 00:22:31,760 I've never seen it in a commercial application, 455 00:22:31,960 --> 00:22:32,620 so I haven't tried. 456 00:22:33,020 --> 00:22:35,620 When I was looking at the guy who sold this 457 00:22:35,620 --> 00:22:37,580 Python obfuscator though, I found a gem. 458 00:22:38,340 --> 00:22:40,740 And he also sells poor sense. 459 00:22:41,320 --> 00:22:43,260 Poor sense is a way to cat-proof your computer. 460 00:22:44,740 --> 00:22:47,040 And it will tell you when 461 00:22:47,040 --> 00:22:48,640 cat-like typing has been detected. 462 00:22:49,800 --> 00:22:51,120 So if people 463 00:22:51,120 --> 00:22:53,000 have cat-computer problems, 464 00:22:53,140 --> 00:22:53,740 this is your man. 465 00:22:55,460 --> 00:22:57,540 It's only $19.99, so it's a snip. 466 00:23:00,680 --> 00:23:02,880 Some of the more effective 467 00:23:03,460 --> 00:23:05,220 modifications that you use all rely 468 00:23:05,220 --> 00:23:06,500 on modifying the Python runtime. 469 00:23:06,880 --> 00:23:09,160 As we said, you can distribute the Python 470 00:23:09,160 --> 00:23:10,120 runtime in a package. 471 00:23:11,320 --> 00:23:13,100 This means the authors can modify the C 472 00:23:13,100 --> 00:23:15,160 Python at the C layer, compile their own 473 00:23:15,160 --> 00:23:17,160 version of the Python runtime, and then 474 00:23:17,160 --> 00:23:18,740 that can switch things around, make the bytecode 475 00:23:18,740 --> 00:23:19,760 that's produced different, etc. 476 00:23:20,020 --> 00:23:21,160 And this is what people are doing. 477 00:23:21,840 --> 00:23:23,860 So one of the simplest things is to just change 478 00:23:23,860 --> 00:23:24,820 that bytecode magic number. 479 00:23:25,060 --> 00:23:27,960 Remember from the PYC, the first four bytes 480 00:23:27,960 --> 00:23:29,380 are a magic number that say what version of 481 00:23:29,380 --> 00:23:29,920 Python it is. 482 00:23:30,060 --> 00:23:33,640 If the Python that you're running isn't the 483 00:23:33,640 --> 00:23:35,520 same version, it will bail with a bad magic 484 00:23:35,520 --> 00:23:37,060 number error. 485 00:23:37,060 --> 00:23:39,060 You probably can't 486 00:23:39,060 --> 00:23:39,820 see too well. 487 00:23:41,760 --> 00:23:42,960 So these are all the defined 488 00:23:42,960 --> 00:23:44,540 magic numbers for the different versions of 489 00:23:44,540 --> 00:23:46,540 Python. This is in the comments for the 490 00:23:46,540 --> 00:23:47,240 import.c. 491 00:23:49,020 --> 00:23:50,600 Basically all they do is they change the 492 00:23:50,600 --> 00:23:52,180 version of the Python number so any 493 00:23:52,180 --> 00:23:53,860 standard Python, apart from their modified 494 00:23:53,860 --> 00:23:55,360 runtime, won't run their code. 495 00:23:57,040 --> 00:23:58,780 It's kind of, if you fall at this barrier, 496 00:23:58,900 --> 00:24:00,320 you're failing pretty hard because you just 497 00:24:00,320 --> 00:24:02,120 need to replace the first four bytes with 498 00:24:02,120 --> 00:24:04,540 a four bytes standard Python 499 00:24:05,000 --> 00:24:06,560 magic number, and you're good. 500 00:24:07,700 --> 00:24:09,280 It does make all the standard tools 501 00:24:09,280 --> 00:24:11,060 fail because, like I say, they're expecting 502 00:24:11,780 --> 00:24:12,900 standard formatted bytecode. 503 00:24:14,000 --> 00:24:15,040 So it's a very easy 504 00:24:15,040 --> 00:24:16,480 change for developers to make, but it's very 505 00:24:16,480 --> 00:24:17,360 easy to get around as well. 506 00:24:19,940 --> 00:24:21,100 Changes to the marshalling format. 507 00:24:21,220 --> 00:24:22,880 As we said, there was the four byte magic 508 00:24:22,880 --> 00:24:24,660 number, there's a four byte timestamp, and 509 00:24:24,660 --> 00:24:26,840 then there's this marshalled code. Obviously the 510 00:24:26,840 --> 00:24:28,540 marshalling happens in a standard format. If they 511 00:24:28,540 --> 00:24:30,940 go in and go to marshall.c 512 00:24:30,940 --> 00:24:33,060 in the CPython runtime and 513 00:24:33,060 --> 00:24:34,740 change how it's marshalling things, then obviously 514 00:24:34,740 --> 00:24:36,720 all the standard tools won't understand how to unpack it. 515 00:24:37,060 --> 00:24:39,080 They can get arbitrarily fancy here. 516 00:24:39,300 --> 00:24:41,040 I've seen stuff that looks like it's pretty much 517 00:24:41,040 --> 00:24:42,760 doing some kind of encryption. I'm sure it's 518 00:24:42,760 --> 00:24:45,080 pretty crappy encryption, but still, to work 519 00:24:45,080 --> 00:24:46,900 out what they're doing, because it's happening in the CPython 520 00:24:46,900 --> 00:24:48,780 layer, you're going to have to trace it in a debugger, 521 00:24:49,220 --> 00:24:50,680 and you're going to have to do that for every 522 00:24:50,680 --> 00:24:52,340 different type of marshalling modification. 523 00:24:52,860 --> 00:24:54,760 Which is a bit of a pain, so I wanted to avoid that, 524 00:24:54,820 --> 00:24:57,020 and I managed to avoid it by working 525 00:24:57,020 --> 00:24:58,420 at the Python layer. We'll talk about that in a bit. 526 00:24:58,820 --> 00:25:01,000 And then opcode remapping. This is 527 00:25:01,000 --> 00:25:02,880 one of the more complex things 528 00:25:02,880 --> 00:25:05,000 that they do. Basically that table that 529 00:25:05,000 --> 00:25:06,600 I spoke of, of those integers to instruction 530 00:25:07,060 --> 00:25:08,680 pins, they juggle all that around, so 531 00:25:08,680 --> 00:25:10,740 it's not the standard format that you'd expect. 532 00:25:11,020 --> 00:25:13,140 They change the opcode.h 533 00:25:13,140 --> 00:25:14,840 in the runtime, and remove 534 00:25:14,840 --> 00:25:16,660 opcode.py from the distributed 535 00:25:16,660 --> 00:25:18,200 runtime, and that means that you 536 00:25:18,200 --> 00:25:20,820 can get access to the CO code object, 537 00:25:21,060 --> 00:25:23,040 but the byte stream makes no sense, 538 00:25:23,100 --> 00:25:24,940 and it makes no sense to any of the decompilers, 539 00:25:24,980 --> 00:25:26,760 because they're running off the standard opcode map, 540 00:25:26,940 --> 00:25:29,180 and this has got a juggled up opcode map. 541 00:25:29,960 --> 00:25:31,080 When I found this, it was 542 00:25:31,080 --> 00:25:33,000 a real pain, but I've got a pretty simple attack to get 543 00:25:33,000 --> 00:25:34,000 around it, which we'll talk about. 544 00:25:36,300 --> 00:25:37,040 So the general 545 00:25:37,060 --> 00:25:38,860 approach with what we're taking with Pyretic, 546 00:25:39,880 --> 00:25:41,260 I want to remove the reliance 547 00:25:41,260 --> 00:25:42,680 on having access to the file on disk. 548 00:25:42,820 --> 00:25:43,880 I want to work in memory only. 549 00:25:44,720 --> 00:25:46,920 I want any of the protections, like the marshalling 550 00:25:46,920 --> 00:25:48,900 and everything, the application to undo it, because 551 00:25:48,900 --> 00:25:50,960 however complex that they've got with marshalling up something, 552 00:25:51,180 --> 00:25:52,700 they're going to undo the marshalling, 553 00:25:52,820 --> 00:25:55,160 and then when it's in memory, it's just a standard Python object, 554 00:25:55,360 --> 00:25:56,780 and obviously it's easier to understand 555 00:25:56,780 --> 00:25:59,080 a standard Python object than piss about at the C layer 556 00:25:59,080 --> 00:26:00,780 trying to work out what they were doing with their marshalling. 557 00:26:01,580 --> 00:26:02,980 I'm going to get in process at the 558 00:26:02,980 --> 00:26:04,960 Python layer, and then obviously 559 00:26:04,960 --> 00:26:06,780 I have access to the full Python namespace 560 00:26:07,060 --> 00:26:08,660 and I can start to query the objects 561 00:26:08,660 --> 00:26:10,920 and then from querying 562 00:26:10,920 --> 00:26:12,920 the objects in the live memory, get back to a source 563 00:26:12,920 --> 00:26:13,480 code representation. 564 00:26:16,360 --> 00:26:16,840 So, 565 00:26:17,860 --> 00:26:18,980 the fact that 566 00:26:18,980 --> 00:26:20,900 you can, with a reflective 567 00:26:20,900 --> 00:26:23,060 language like Python, query an object, 568 00:26:23,180 --> 00:26:24,680 this is obviously really useful in 569 00:26:24,680 --> 00:26:26,920 the cloud paradigm, because you 570 00:26:26,920 --> 00:26:28,540 will never have access to their files. 571 00:26:28,840 --> 00:26:30,860 However, they may be, Python's a 572 00:26:30,860 --> 00:26:32,840 popular choice for using as a sandboxing 573 00:26:32,840 --> 00:26:34,820 environment, or as an environment to 574 00:26:34,820 --> 00:26:36,380 allow you to access their API. 575 00:26:37,180 --> 00:26:39,060 They think that maybe if you only have access 576 00:26:39,060 --> 00:26:40,800 to their objects, that's all good, 577 00:26:40,960 --> 00:26:42,820 but with these techniques you can actually get the source code 578 00:26:42,820 --> 00:26:44,640 back from their objects, so even though 579 00:26:44,640 --> 00:26:46,320 you're interacting with a remote computer, 580 00:26:46,600 --> 00:26:48,980 you can still get access back to the source 581 00:26:48,980 --> 00:26:50,920 code even though you haven't got their PYCs 582 00:26:50,920 --> 00:26:52,380 on disk, which is pretty cool. 583 00:26:52,600 --> 00:26:54,740 Moving forward, I think this is an area that more people 584 00:26:54,740 --> 00:26:56,560 are going to have to, for all types of languages, 585 00:26:56,660 --> 00:26:58,380 not just Python, put more effort into, 586 00:26:58,520 --> 00:27:00,580 purely because the paradigms of distributing 587 00:27:01,300 --> 00:27:02,540 applications are changing, so 588 00:27:02,920 --> 00:27:04,840 I'd expect it to be an area that you'll see a lot more activity 589 00:27:04,840 --> 00:27:05,380 in the future. 590 00:27:07,060 --> 00:27:08,840 How do we get in process? I said we need to be in process 591 00:27:08,840 --> 00:27:10,020 so we can query the Python objects. 592 00:27:10,760 --> 00:27:13,480 Obviously, Python, if people are trying to obfuscate 593 00:27:13,480 --> 00:27:15,000 their code, they won't distribute the pies, 594 00:27:15,120 --> 00:27:16,400 they'll only distribute the PYCs, 595 00:27:17,120 --> 00:27:18,840 and if they've modified their runtime, 596 00:27:19,000 --> 00:27:20,960 you need to use their interpreter to 597 00:27:20,960 --> 00:27:23,100 run their PYCs, because they've pissed about with the marshaling 598 00:27:23,100 --> 00:27:25,040 or the bytecode number 599 00:27:25,040 --> 00:27:26,940 or whatever. But the import rules for 600 00:27:26,940 --> 00:27:28,880 Python still apply. So in import C, 601 00:27:29,540 --> 00:27:30,980 you'll see there's this 602 00:27:31,440 --> 00:27:32,540 equality test here, 603 00:27:32,660 --> 00:27:34,780 where it talks about the timestamp, and they're saying 604 00:27:34,780 --> 00:27:36,960 if the, this is just saying, if the 605 00:27:37,060 --> 00:27:38,900 timestamp in the Python bytecode is 606 00:27:38,900 --> 00:27:41,040 not equal to the timestamp on the PY file 607 00:27:41,040 --> 00:27:43,280 that's on disk, then recompile 608 00:27:43,280 --> 00:27:45,080 that bytecode. So you're using the PY 609 00:27:45,080 --> 00:27:46,960 in preference to the PYC, because something 610 00:27:46,960 --> 00:27:49,140 may have changed in your PY, and then you'll recompile 611 00:27:49,140 --> 00:27:51,020 the bytecode. Obviously 612 00:27:51,020 --> 00:27:52,240 this test means that 613 00:27:53,400 --> 00:27:55,020 you can just, if their 614 00:27:55,020 --> 00:27:56,580 module was named foo-module-pyc, 615 00:27:56,660 --> 00:27:59,040 take it to another name, foo-original, and then 616 00:27:59,040 --> 00:28:00,980 you take your module, the code that you want to run, and 617 00:28:00,980 --> 00:28:03,020 just call it foo-module.py, your 618 00:28:03,020 --> 00:28:05,020 code will be run in preference. So that's 619 00:28:05,020 --> 00:28:06,980 this really easy way to get in process. 620 00:28:07,060 --> 00:28:09,120 All the distributors 621 00:28:09,120 --> 00:28:10,440 would need to do is take out 622 00:28:11,160 --> 00:28:12,780 is take out this equality 623 00:28:12,780 --> 00:28:14,720 test, and then stop people doing it. 624 00:28:14,800 --> 00:28:16,900 But I've never seen, people have put huge amounts of effort 625 00:28:16,900 --> 00:28:18,960 into their remarshalling and things, but 626 00:28:18,960 --> 00:28:21,000 they haven't taken out this like four line if statement. 627 00:28:21,720 --> 00:28:22,620 I don't know why, 628 00:28:22,720 --> 00:28:23,900 probably they fail. 629 00:28:26,400 --> 00:28:27,060 So obviously 630 00:28:27,060 --> 00:28:28,200 our PY takes precedence, 631 00:28:28,380 --> 00:28:30,860 and then because we're running arbitrary 632 00:28:30,860 --> 00:28:32,560 Python bytecode, now we can start to 633 00:28:32,560 --> 00:28:34,060 query those objects. 634 00:28:35,080 --> 00:28:37,020 There's some code that I haven't got on disk, but it's in the 635 00:28:37,060 --> 00:28:38,940 paper for blindly mirroring the object 636 00:28:38,940 --> 00:28:40,920 that you've replaced. And so you're acting as a 637 00:28:40,920 --> 00:28:42,940 proxy between the object 638 00:28:42,940 --> 00:28:44,960 that you've renamed and the object 639 00:28:44,960 --> 00:28:46,960 which is the code which is calling you. 640 00:28:47,220 --> 00:28:48,920 And you're literally passing the calls 641 00:28:48,920 --> 00:28:51,140 on into the object. So the code's in the paper. 642 00:28:51,340 --> 00:28:52,180 It's pretty useful. 643 00:28:53,360 --> 00:28:54,920 That means that the application 644 00:28:54,920 --> 00:28:57,180 won't crash. So as I said, 645 00:28:57,200 --> 00:28:59,080 non-standard marshalling can be a real pain in the ass. 646 00:28:59,400 --> 00:29:00,940 I didn't put much effort into 647 00:29:00,940 --> 00:29:03,220 trying to come up with a technique to undo their marshalling 648 00:29:03,220 --> 00:29:05,060 because as soon as you're in the Python runtime 649 00:29:05,060 --> 00:29:07,040 they've already undone it. So it was a real 650 00:29:07,060 --> 00:29:08,800 pain. So we just completely 651 00:29:08,800 --> 00:29:10,860 sidestepped the problem and accessed it at the Python layer. 652 00:29:12,680 --> 00:29:14,880 This is more complicated, however. We've got to 653 00:29:14,880 --> 00:29:16,440 re-remap their 654 00:29:17,040 --> 00:29:18,880 opcode. So we've got to work out how they juggled up 655 00:29:18,880 --> 00:29:19,340 their 656 00:29:20,940 --> 00:29:23,060 opcode mappings, their integer to instruction 657 00:29:23,060 --> 00:29:24,940 maps. Obviously we 658 00:29:24,940 --> 00:29:27,020 need to do this at runtime. We don't have access 659 00:29:27,020 --> 00:29:29,020 to the opcode.py, so we need to reproduce 660 00:29:29,020 --> 00:29:30,980 that opcode.py so all our other 661 00:29:30,980 --> 00:29:32,460 techniques can work at the Python runtime. 662 00:29:33,660 --> 00:29:34,900 Obviously we need an 663 00:29:34,900 --> 00:29:37,000 understanding of the opcode mapping to be able to get anything from 664 00:29:37,060 --> 00:29:39,040 the byte stream. It's this understanding which 665 00:29:39,040 --> 00:29:40,580 the vendors are assuming that they've broken. 666 00:29:40,880 --> 00:29:42,880 If you don't understand the opcode mapping, you can't get any sense 667 00:29:42,880 --> 00:29:44,640 out of a bytecode because it's just a stream of bytes. 668 00:29:44,840 --> 00:29:46,960 You don't really understand what it means. But there was 669 00:29:46,960 --> 00:29:48,080 a pretty easy way to get 670 00:29:48,080 --> 00:29:50,440 at this new opcode mapping. 671 00:29:51,540 --> 00:29:52,920 Essentially using a plain text attack 672 00:29:52,920 --> 00:29:54,440 against the compiler. 673 00:29:57,140 --> 00:29:58,960 So as I said, all instructions are just 674 00:29:58,960 --> 00:30:00,780 one byte and there's an optional two byte 675 00:30:00,780 --> 00:30:03,020 argument to it. So if we go and look 676 00:30:03,020 --> 00:30:04,960 at some normal 677 00:30:04,960 --> 00:30:06,840 Python bytecode on the left that we've already seen 678 00:30:07,060 --> 00:30:09,460 instruction arguments, 679 00:30:09,800 --> 00:30:11,260 if they've remapped 680 00:30:11,260 --> 00:30:12,200 their stack to 681 00:30:12,940 --> 00:30:14,880 0x64 has been remapped to 0x44, 682 00:30:15,640 --> 00:30:17,460 0x47 has been remapped to 0x11, 683 00:30:17,620 --> 00:30:18,300 et cetera, et cetera. 684 00:30:18,480 --> 00:30:20,420 If we can take 685 00:30:20,840 --> 00:30:22,260 a known set of .pys 686 00:30:22,260 --> 00:30:24,860 and compile them into bytecode 687 00:30:24,860 --> 00:30:26,740 for standard Python 688 00:30:26,740 --> 00:30:28,880 and then compile them into bytecode for the obfuscated 689 00:30:28,880 --> 00:30:30,380 Python using the obfuscated runtime, 690 00:30:30,660 --> 00:30:32,840 then we'll get a list of this bytecode 691 00:30:32,840 --> 00:30:34,640 and then we can just diff these two bytecode streams 692 00:30:34,640 --> 00:30:36,620 and we can say, oh cool, right, 693 00:30:37,460 --> 00:30:39,080 0x64's gone to 0x44, 0x47's 694 00:30:39,080 --> 00:30:41,020 gone to 0x11. We do this for enough 695 00:30:41,020 --> 00:30:42,960 Python source code and then we'll hit 696 00:30:42,960 --> 00:30:44,540 all the bytecode instructions 697 00:30:45,000 --> 00:30:47,060 and then we've got the 698 00:30:47,060 --> 00:30:49,060 new mapping and we can rewrite the opcode.py 699 00:30:49,060 --> 00:30:51,100 and then we're good. Even though they've juggled 700 00:30:51,100 --> 00:30:52,960 it up, we can still work out what the instructions 701 00:30:52,960 --> 00:30:54,760 are. So for this example, 702 00:30:55,760 --> 00:30:57,040 obviously we've got these 703 00:30:57,040 --> 00:30:59,140 mappings here and you just keep doing it 704 00:30:59,140 --> 00:31:01,180 for a shit ton of Python and eventually 705 00:31:01,180 --> 00:31:02,660 you'll get all the opcode mappings out. 706 00:31:02,820 --> 00:31:04,760 As I said, everybody distributes 707 00:31:04,760 --> 00:31:06,840 the runtime in these package files 708 00:31:07,060 --> 00:31:08,900 the runtime contains all the 709 00:31:08,900 --> 00:31:10,920 normal Python modules, you know, os.py 710 00:31:10,920 --> 00:31:13,120 and sys.py. We've got access 711 00:31:13,120 --> 00:31:14,440 to those as well because they're standard 712 00:31:14,900 --> 00:31:15,860 library Python modules. 713 00:31:16,580 --> 00:31:18,800 So if you compare the obfuscated 714 00:31:18,800 --> 00:31:20,700 PYCs that they're 715 00:31:20,700 --> 00:31:22,820 distributing and then a normal PYC 716 00:31:22,820 --> 00:31:24,760 which you've compiled, you get this opcode mapping 717 00:31:24,760 --> 00:31:26,240 back out which is pretty useful. 718 00:31:27,160 --> 00:31:29,020 To do this in the tool, I created 719 00:31:29,020 --> 00:31:31,180 a new file format which is probably a bit of a grand term 720 00:31:31,660 --> 00:31:33,040 PYB file format which 721 00:31:33,040 --> 00:31:34,740 just means that we're dumping out raw 722 00:31:34,740 --> 00:31:36,560 Python bytecode to disk 723 00:31:37,060 --> 00:31:38,940 which allows me to sidestep the fact that I probably don't know 724 00:31:38,940 --> 00:31:40,220 how they're marshalling things up. 725 00:31:40,440 --> 00:31:42,820 So it's just a new format that we can produce 726 00:31:42,820 --> 00:31:44,640 two streams that are much easier to diff. 727 00:31:44,700 --> 00:31:45,960 I don't have to go through that stage of 728 00:31:45,960 --> 00:31:47,340 unmarshalling their PYCs. 729 00:31:48,700 --> 00:31:49,580 I'll show you a 730 00:31:51,060 --> 00:31:52,780 so this is like step by step. 731 00:31:53,020 --> 00:31:54,640 First we've got to find a version of Python 732 00:31:55,260 --> 00:31:57,020 that the obfuscated runtime is 733 00:31:57,020 --> 00:31:58,980 running. We need to compile the 734 00:31:58,980 --> 00:32:00,940 bytecode with the exact same version. So if they're running 735 00:32:00,940 --> 00:32:02,940 2.5.4, we need to 736 00:32:02,940 --> 00:32:05,000 compile our PYBs with 2.5.4 737 00:32:05,000 --> 00:32:06,920 otherwise we won't get streams that are perfectly 738 00:32:07,060 --> 00:32:08,600 in sync and we'll get less collisions 739 00:32:08,600 --> 00:32:10,240 for the plain text attack. 740 00:32:11,560 --> 00:32:12,740 So find out the 741 00:32:12,740 --> 00:32:14,680 version. We use 742 00:32:14,680 --> 00:32:16,400 standard library Python to 743 00:32:16,920 --> 00:32:18,840 compile up all the PYBs from 744 00:32:18,840 --> 00:32:20,900 in process we compile up 745 00:32:20,900 --> 00:32:22,880 the PYBs from in process of the obfuscated 746 00:32:22,880 --> 00:32:24,960 runtime, compile up all the PYBs and now 747 00:32:24,960 --> 00:32:26,260 we've got two systems that we can diff. 748 00:32:26,700 --> 00:32:27,580 We diff them. 749 00:32:28,640 --> 00:32:30,740 We get our new bytecode mappings 750 00:32:30,740 --> 00:32:32,940 and then we rewrite opcode.py 751 00:32:32,940 --> 00:32:34,920 and for unpyc we 752 00:32:34,920 --> 00:32:36,580 need to write opcodes.py which is 753 00:32:37,060 --> 00:32:38,700 the equivalent of it and then we're good to go. 754 00:32:38,860 --> 00:32:40,540 So even though they've gone to all this effort 755 00:32:40,890 --> 00:32:42,920 now we've remapped their opcodes, we understand what their 756 00:32:42,920 --> 00:32:45,180 byte streams mean and we can start to decompile. 757 00:32:45,940 --> 00:32:47,100 Again we're only getting this 758 00:32:47,100 --> 00:32:48,660 because we're working in memory at the Python layer 759 00:32:48,660 --> 00:32:49,880 so it's quite useful. 760 00:32:51,500 --> 00:32:52,640 And this is where 761 00:32:52,640 --> 00:32:54,240 I'm going to demo and things will fail. 762 00:33:01,100 --> 00:33:02,620 So I've got a little test application 763 00:33:02,620 --> 00:33:04,960 it's really lame but it shows 764 00:33:04,960 --> 00:33:06,940 the point. RePDB is my 765 00:33:07,060 --> 00:33:08,820 reverse engineering version of PDB 766 00:33:08,820 --> 00:33:10,860 so we'll start that up and we'll have a look at 767 00:33:11,640 --> 00:33:11,960 the... 768 00:33:15,300 --> 00:33:16,600 So we'll just set a project 769 00:33:21,160 --> 00:33:23,020 You can see all our source code is going to be 770 00:33:23,020 --> 00:33:24,700 dumped out to a particular directory and you can see the 771 00:33:24,700 --> 00:33:26,760 project was created here. 772 00:33:27,600 --> 00:33:28,940 All our PYBs are going to be 773 00:33:28,940 --> 00:33:30,820 going out into here. So I'll just get my cheat sheet 774 00:33:30,820 --> 00:33:31,680 so I don't fail at the 775 00:33:32,900 --> 00:33:33,220 path. 776 00:33:35,920 --> 00:33:37,040 So first we're going to generate 777 00:33:37,060 --> 00:33:39,060 the reference PYBs 778 00:33:40,100 --> 00:33:40,740 so just 779 00:33:40,740 --> 00:33:42,620 gen ref 26 780 00:33:42,620 --> 00:33:45,220 and a path to all the standard library modules 781 00:33:45,220 --> 00:33:47,400 it's just the standard module set 782 00:33:47,400 --> 00:33:48,060 the PYs 783 00:33:49,520 --> 00:33:50,800 so we compile 784 00:33:50,800 --> 00:33:51,500 all those up 785 00:33:52,020 --> 00:33:53,780 so we're compiling them into PYCs 786 00:33:53,780 --> 00:33:56,900 so we know we've got a good compile 787 00:33:56,900 --> 00:33:58,780 and then we're taking them to PYBs 788 00:33:58,780 --> 00:34:00,420 so we've done all that, that's good 789 00:34:00,420 --> 00:34:02,860 and now we do the same for the obfuscated Python bytecode 790 00:34:02,860 --> 00:34:03,740 so we can get out our sets 791 00:34:03,740 --> 00:34:06,960 so gen 026 792 00:34:07,480 --> 00:34:08,440 gen 026 793 00:34:08,440 --> 00:34:10,800 and this time we give it the path to 794 00:34:11,320 --> 00:34:12,540 all the PYCs 795 00:34:13,060 --> 00:34:14,860 which are distributed, the modified 796 00:34:14,860 --> 00:34:16,840 PYCs. I've created 797 00:34:16,840 --> 00:34:17,440 my own 798 00:34:18,600 --> 00:34:19,220 you'll see 799 00:34:21,160 --> 00:34:22,680 Python 262 is a standard 800 00:34:22,680 --> 00:34:24,100 compiled runtime 801 00:34:24,100 --> 00:34:26,620 this one here is one that I've modified that will do 802 00:34:26,620 --> 00:34:27,400 opcode remapping 803 00:34:28,860 --> 00:34:30,760 so we compile up everything here 804 00:34:30,760 --> 00:34:32,940 got a bunch of warnings but that's because 805 00:34:32,940 --> 00:34:34,380 I haven't cleaned up my code properly 806 00:34:35,180 --> 00:34:36,920 and now we just tell it to remap 807 00:34:37,060 --> 00:34:38,940 and it will take those two byte streams 808 00:34:38,940 --> 00:34:40,060 and diff them together 809 00:34:40,540 --> 00:34:43,020 and so you can see further up here 810 00:34:43,940 --> 00:34:45,440 where it's finding bytecodes 811 00:34:45,440 --> 00:34:46,640 and remapping so you can see 812 00:34:47,100 --> 00:34:48,960 binary write shift, it wasn't 63 813 00:34:48,960 --> 00:34:49,940 it's moved to 64 814 00:34:49,940 --> 00:34:51,380 so we found all the shifts 815 00:34:51,380 --> 00:34:54,640 and now we're going to rewrite the opcode.py 816 00:34:54,640 --> 00:34:56,840 so we say yes, we want to rewrite the opcode.py 817 00:34:56,840 --> 00:34:58,380 and now if we look at here 818 00:35:00,800 --> 00:35:02,640 you can see there's 819 00:35:02,640 --> 00:35:04,680 all the PYBs that we've produced for the obfuscated 820 00:35:04,680 --> 00:35:06,400 all the PYBs that we've produced for the reference 821 00:35:07,060 --> 00:35:09,240 and then in the libs we've got a new opcode.py 822 00:35:10,840 --> 00:35:11,920 remapped by Pyretic 823 00:35:11,920 --> 00:35:13,160 and then these are all the new 824 00:35:13,160 --> 00:35:14,120 opcode listings 825 00:35:14,120 --> 00:35:15,240 and equivalent 826 00:35:17,200 --> 00:35:18,820 equivalent for the opcodes.py 827 00:35:19,380 --> 00:35:20,940 it's just the equivalent file for the 828 00:35:21,380 --> 00:35:21,820 unpyc 829 00:35:21,820 --> 00:35:24,300 so we've remapped the bytecode so now we're in a position 830 00:35:24,300 --> 00:35:25,760 that we can understand what the byte streams mean 831 00:35:27,180 --> 00:35:29,020 and we've probably really got to hurry up 832 00:35:31,640 --> 00:35:32,760 so in memory 833 00:35:33,340 --> 00:35:34,400 decompilation versus static 834 00:35:34,400 --> 00:35:36,920 obviously all the decompilers at the moment take files on disk 835 00:35:37,060 --> 00:35:38,860 and decompile them 836 00:35:38,860 --> 00:35:40,700 by accessing those serialized objects 837 00:35:40,700 --> 00:35:42,600 that we spoke about in the marshaled PYC 838 00:35:42,600 --> 00:35:45,080 in memory obviously we're going to access 839 00:35:45,080 --> 00:35:46,780 the bytecode 840 00:35:46,780 --> 00:35:49,160 from the function object co.code 841 00:35:49,160 --> 00:35:50,940 object because then 842 00:35:50,940 --> 00:35:52,580 everything will be automatically unmarshalled 843 00:35:52,580 --> 00:35:54,920 and we can get access 844 00:35:54,920 --> 00:35:57,040 to that at runtime, we're going to have to hurry up 845 00:35:57,760 --> 00:35:58,800 so as I said 846 00:35:58,800 --> 00:35:59,540 a top level 847 00:36:00,740 --> 00:36:02,580 Python object doesn't have a code object 848 00:36:02,580 --> 00:36:04,540 which is a real pain so at runtime 849 00:36:04,540 --> 00:36:06,740 there's no code object for us to decompile 850 00:36:07,180 --> 00:36:08,680 this means that we have to use 851 00:36:09,520 --> 00:36:10,320 I've termed it 852 00:36:11,100 --> 00:36:12,060 source code reconstruction 853 00:36:12,560 --> 00:36:14,640 I'm not sure if that's a proper term or not 854 00:36:14,640 --> 00:36:16,800 but rather than decompiling the stream of bytecode 855 00:36:17,480 --> 00:36:18,580 we're prodding and poking 856 00:36:18,580 --> 00:36:19,980 at a lot of the objects 857 00:36:19,980 --> 00:36:22,160 and asking questions at them in the runtime 858 00:36:22,160 --> 00:36:24,400 and then from the answers to all those questions we've got 859 00:36:24,400 --> 00:36:26,820 have a good guess at what they actually look like 860 00:36:26,820 --> 00:36:27,360 in source code 861 00:36:28,880 --> 00:36:31,180 so when we're working in memory decompiling 862 00:36:31,180 --> 00:36:32,780 if we can get access to a function 863 00:36:32,780 --> 00:36:34,560 we can get access to the code object 864 00:36:34,560 --> 00:36:36,760 and decompile that. 5 minutes, awesome 865 00:36:38,540 --> 00:36:40,540 if we're doing reconstruction 866 00:36:40,930 --> 00:36:42,820 at runtime we need to query a lot of the objects 867 00:36:45,360 --> 00:36:46,500 so we'll talk about 868 00:36:47,920 --> 00:36:49,340 so if this is the source code 869 00:36:50,200 --> 00:36:50,960 these objects 870 00:36:50,960 --> 00:36:52,960 are obviously top level so we need to use reconstruction 871 00:36:52,960 --> 00:36:55,320 on those so we have to query a lot of the objects 872 00:36:55,320 --> 00:36:57,020 this function actually has 873 00:36:57,500 --> 00:36:58,680 it's got a CO code object 874 00:36:58,680 --> 00:37:00,780 so we can just use standard decompilation there 875 00:37:01,560 --> 00:37:03,260 there's some stuff that we can and can't 876 00:37:03,260 --> 00:37:04,000 get out of reconstruction 877 00:37:04,420 --> 00:37:05,980 so obviously you can see here that bar 878 00:37:07,060 --> 00:37:08,140 is calling test function 3 879 00:37:08,140 --> 00:37:10,500 from reconstruction we can never tell that 880 00:37:10,500 --> 00:37:11,960 it's called test function 3 881 00:37:11,960 --> 00:37:13,740 we only get the return from that 882 00:37:14,380 --> 00:37:16,800 similarly with foo that was initially set to 9 883 00:37:16,800 --> 00:37:17,720 and then was set to 10 884 00:37:17,720 --> 00:37:19,620 we'll never know that it was initially set to 9 885 00:37:19,620 --> 00:37:21,700 we only get it when it's at 10 886 00:37:21,700 --> 00:37:23,140 because that's the state from which we're 887 00:37:23,140 --> 00:37:26,420 asking the question of it so we have no pre-state history 888 00:37:27,140 --> 00:37:28,520 so it's not perfect 889 00:37:29,000 --> 00:37:30,300 but remember we're looking for bugs 890 00:37:30,300 --> 00:37:31,380 we're not looking to completely 891 00:37:31,920 --> 00:37:33,840 re-get the python source code 892 00:37:33,840 --> 00:37:36,520 so it's good enough for finding bugs in realistic applications 893 00:37:37,060 --> 00:37:38,940 most of the functionality is actually in 894 00:37:38,940 --> 00:37:41,260 the classes and the functions not at the top level 895 00:37:41,260 --> 00:37:42,400 so if we can get roughly right 896 00:37:42,400 --> 00:37:43,820 it's good enough for finding bugs 897 00:37:43,820 --> 00:37:45,920 so it's not perfect but it's good enough 898 00:37:45,920 --> 00:37:47,280 and that's just saying what I've said 899 00:37:49,780 --> 00:37:51,200 so the Pyretic toolkit 900 00:37:51,200 --> 00:37:52,760 this is what I wrote to kind of 901 00:37:52,760 --> 00:37:54,500 mean that I didn't have to do all this manually 902 00:37:56,000 --> 00:37:57,280 obviously it came from my real need 903 00:37:57,280 --> 00:37:58,140 to actually find some bugs 904 00:37:58,140 --> 00:38:00,000 it's kind of in three sections 905 00:38:00,820 --> 00:38:02,880 there's the decompilation section 906 00:38:02,880 --> 00:38:04,960 and source code reconstruction 907 00:38:04,960 --> 00:38:06,420 that we've just spoke about 908 00:38:07,060 --> 00:38:08,520 it will do three types of decompilation 909 00:38:08,520 --> 00:38:11,040 depending on the kind of obfuscations 910 00:38:11,040 --> 00:38:12,440 which are in place and the access that you have 911 00:38:13,320 --> 00:38:15,380 so there's file system traversal 912 00:38:15,380 --> 00:38:16,580 module object decompile 913 00:38:16,580 --> 00:38:18,400 which means that you're walking the file system 914 00:38:18,400 --> 00:38:20,020 you've got access to the file system 915 00:38:20,020 --> 00:38:21,280 you've got access to their PYCs 916 00:38:21,840 --> 00:38:23,940 and you will take those PYCs 917 00:38:23,940 --> 00:38:26,820 and they also give you access to their un-marshalling 918 00:38:26,820 --> 00:38:28,820 so they haven't restricted access to their un-marshalling 919 00:38:28,820 --> 00:38:30,280 you can take the files off disk 920 00:38:30,280 --> 00:38:33,040 un-marshall them, throw them in memory into the decompiler 921 00:38:33,040 --> 00:38:34,160 and get the source code back out 922 00:38:34,980 --> 00:38:35,380 oh 923 00:38:37,060 --> 00:38:37,940 file system traversal 924 00:38:37,940 --> 00:38:39,180 so you've got access to the code on disk 925 00:38:39,180 --> 00:38:40,780 but you don't have access to the un-marshalling 926 00:38:40,780 --> 00:38:42,940 so this means you have to do the source code reconstruction 927 00:38:45,060 --> 00:38:47,100 and the actual decompile 928 00:38:47,100 --> 00:38:48,640 on the objects that you can access 929 00:38:48,640 --> 00:38:49,640 with the CO code object 930 00:38:50,400 --> 00:38:52,820 again you get pretty good decompilation out of this 931 00:38:52,820 --> 00:38:54,400 but not perfect, not as good as the first case 932 00:38:54,400 --> 00:38:55,960 and then this case is when 933 00:38:55,960 --> 00:38:57,740 you don't have access to anything on disk 934 00:38:57,740 --> 00:38:59,480 you've only got access to the objects in memory 935 00:38:59,480 --> 00:39:00,700 so you've got to do 936 00:39:00,700 --> 00:39:03,620 you're not traversing the file system 937 00:39:03,620 --> 00:39:06,200 so that means you can only decompile anything 938 00:39:07,060 --> 00:39:08,740 instantiated at the point that you're in state 939 00:39:08,740 --> 00:39:11,820 so a lot less code gets decompiled 940 00:39:11,820 --> 00:39:13,340 you can only get access to something 941 00:39:13,340 --> 00:39:15,260 that's actually been instantiated in memory at the time 942 00:39:15,260 --> 00:39:17,340 but you don't have access to anything on disk 943 00:39:17,340 --> 00:39:19,240 so you'll get less code decompiled 944 00:39:19,660 --> 00:39:20,540 but at least you'll get something 945 00:39:23,740 --> 00:39:25,500 this is the opcode remapping 946 00:39:25,500 --> 00:39:26,660 which you saw demoed earlier 947 00:39:26,660 --> 00:39:29,120 so it's all packaged up in a nice easy to use way 948 00:39:29,120 --> 00:39:30,400 so you don't have to do it all by hand 949 00:39:30,940 --> 00:39:33,700 and then there's the re-pdb 950 00:39:33,700 --> 00:39:35,880 which is kind of my version of pdb 951 00:39:35,880 --> 00:39:36,680 it builds on it 952 00:39:37,060 --> 00:39:37,820 superclasses out pdb 953 00:39:37,820 --> 00:39:40,800 but it allows you to access lots of extra functionality 954 00:39:40,800 --> 00:39:42,920 which is useful for assessing the code 955 00:39:43,480 --> 00:39:44,260 at runtime 956 00:39:44,260 --> 00:39:46,700 and things like it calls through to 957 00:39:46,700 --> 00:39:47,960 third party modules like 958 00:39:48,800 --> 00:39:50,940 pycallgraph so you can just get a nice callgraph out 959 00:39:50,940 --> 00:39:52,920 and actually understand what that code's doing 960 00:39:52,920 --> 00:39:53,680 at a very high level 961 00:39:56,180 --> 00:39:57,060 so yeah 962 00:39:57,060 --> 00:39:57,920 some future directions 963 00:39:57,920 --> 00:40:00,400 there's lots of potential to actually take this more 964 00:40:00,900 --> 00:40:02,480 usable by other people rather than just me 965 00:40:04,300 --> 00:40:05,260 I'll just show you 966 00:40:05,260 --> 00:40:06,920 the decompilation of the three types 967 00:40:07,060 --> 00:40:08,400 and then I'll be kicked off 968 00:40:08,400 --> 00:40:08,780 I think 969 00:40:11,460 --> 00:40:12,860 so we've already done 970 00:40:12,860 --> 00:40:13,720 all the remapping 971 00:40:13,720 --> 00:40:14,820 so we've got the opcode 972 00:40:14,820 --> 00:40:17,640 so now we're going to do the three decompilations 973 00:40:17,640 --> 00:40:19,240 so you can see how the source code kind of 974 00:40:19,240 --> 00:40:21,840 differs and degrades when you're doing the pure in memory stuff 975 00:40:23,280 --> 00:40:25,680 so first we'll do the file system traversal 976 00:40:25,680 --> 00:40:27,160 so file system traversal 977 00:40:27,160 --> 00:40:28,800 file system traversal 978 00:40:28,800 --> 00:40:30,100 unmarched and decompile 979 00:40:30,580 --> 00:40:31,900 and this is the 980 00:40:31,900 --> 00:40:33,660 you know the silly test application 981 00:40:33,660 --> 00:40:35,120 which I pulled up 982 00:40:37,060 --> 00:40:37,580 at the time which I compiled 983 00:40:38,480 --> 00:40:39,860 so it's pretty quick 984 00:40:41,400 --> 00:40:42,820 because it was a simple application 985 00:40:42,820 --> 00:40:44,340 so we'll go to our source code 986 00:40:44,340 --> 00:40:46,160 and then this ridiculously long path 987 00:40:52,740 --> 00:40:53,720 yeah we're there 988 00:40:53,720 --> 00:40:56,800 so this is the PYC that's been decompiled 989 00:40:56,800 --> 00:40:57,740 through the first method 990 00:40:57,740 --> 00:40:59,000 you can see it's pretty good 991 00:40:59,620 --> 00:41:02,880 you get some strange things 992 00:41:02,880 --> 00:41:04,360 where things are specified as longs 993 00:41:04,360 --> 00:41:06,520 but it's a pretty 994 00:41:07,060 --> 00:41:07,840 good decompilation 995 00:41:07,840 --> 00:41:09,720 you'll see things change when we're doing 996 00:41:09,720 --> 00:41:11,280 the pure memory stuff 997 00:41:11,280 --> 00:41:12,800 so we'll do this again 998 00:41:12,800 --> 00:41:16,180 with file system mem 999 00:41:18,660 --> 00:41:19,140 decompile 1000 00:41:19,140 --> 00:41:20,240 to the same path 1001 00:41:21,120 --> 00:41:22,200 again pretty quick 1002 00:41:26,680 --> 00:41:28,160 and so it's in a different 1003 00:41:28,720 --> 00:41:29,800 and you've guessed it 1004 00:41:30,300 --> 00:41:31,320 all the way back out 1005 00:41:37,380 --> 00:41:38,100 and we're there 1006 00:41:39,640 --> 00:41:40,420 you'll see here 1007 00:41:40,900 --> 00:41:42,420 things like this was a function call 1008 00:41:42,420 --> 00:41:44,400 but we only get the return value from that function call 1009 00:41:44,400 --> 00:41:45,520 for the reasons that I said earlier 1010 00:41:45,520 --> 00:41:46,880 and you'll see that some of the classes 1011 00:41:46,880 --> 00:41:48,480 kind of have an elongated name 1012 00:41:48,480 --> 00:41:50,000 because that's where we got the 1013 00:41:50,000 --> 00:41:52,420 in memory when we were asking a question 1014 00:41:52,420 --> 00:41:53,080 of that class object 1015 00:41:53,080 --> 00:41:54,520 this is the answer that we got 1016 00:41:54,520 --> 00:41:56,160 so again not perfect 1017 00:41:56,160 --> 00:41:57,720 but good enough for finding bugs definitely 1018 00:41:57,720 --> 00:42:00,720 and then the pure memory decompile 1019 00:42:00,720 --> 00:42:02,460 we need to obviously 1020 00:42:02,460 --> 00:42:03,300 import the object 1021 00:42:03,300 --> 00:42:04,840 so if we import the test app 1022 00:42:05,840 --> 00:42:07,080 and then we do a 1023 00:42:08,660 --> 00:42:10,380 I'll show you it's in 1024 00:42:11,440 --> 00:42:12,660 so you can see that it's 1025 00:42:12,660 --> 00:42:13,400 actually in 1026 00:42:13,400 --> 00:42:16,380 the name space of the debugger 1027 00:42:16,380 --> 00:42:17,880 so if you do a pure 1028 00:42:18,520 --> 00:42:18,920 mem 1029 00:42:21,180 --> 00:42:22,580 decompile on this object 1030 00:42:22,580 --> 00:42:24,200 so obviously we're not passing it a path anymore 1031 00:42:24,200 --> 00:42:26,160 we're passing it an object because we're assuming 1032 00:42:26,160 --> 00:42:27,320 that we don't have access to the path 1033 00:42:27,920 --> 00:42:29,040 it'll do the same thing 1034 00:42:31,460 --> 00:42:31,860 and 1035 00:42:33,200 --> 00:42:34,480 if we go to the mem object 1036 00:42:34,480 --> 00:42:35,560 now we don't have a long path 1037 00:42:36,880 --> 00:42:38,000 and you'll see that 1038 00:42:38,000 --> 00:42:39,020 it's come out again 1039 00:42:39,020 --> 00:42:40,760 we only get the return values 1040 00:42:40,760 --> 00:42:43,140 but it's pretty much similar to the previous one 1041 00:42:43,140 --> 00:42:45,400 so the decompilation is pretty good 1042 00:42:45,400 --> 00:42:46,760 and we had no access to anything on disk 1043 00:42:46,760 --> 00:42:48,240 this was source code coming out of 1044 00:42:48,240 --> 00:42:49,880 an instantiated object in memory 1045 00:42:49,880 --> 00:42:52,340 which obviously is a simple test app 1046 00:42:52,340 --> 00:42:54,240 but it's pretty powerful what you can do with that 1047 00:42:54,240 --> 00:42:55,160 moving forward 1048 00:42:55,640 --> 00:42:58,000 so I think I've completely run out of time 1049 00:43:00,720 --> 00:43:00,880 fail 1050 00:43:04,880 --> 00:43:06,500 so completely run out of time 1051 00:43:06,500 --> 00:43:08,520 are there any questions 1052 00:43:12,580 --> 00:43:14,100 awesome that means nobody followed 1053 00:43:14,100 --> 00:43:14,800 even better 1054 00:43:14,800 --> 00:43:17,340 the other thing I'm going to announce is 1055 00:43:17,340 --> 00:43:19,460 there was a hack cup for a lot of teams 1056 00:43:19,460 --> 00:43:21,780 a lot of hacking teams playing in a football competition 1057 00:43:21,780 --> 00:43:22,640 yesterday 1058 00:43:23,400 --> 00:43:24,820 so these are the results of that 1059 00:43:24,820 --> 00:43:27,360 team ZA won and they won a load of tickets 1060 00:43:27,360 --> 00:43:30,700 to echo party down in our gym 1061 00:43:30,720 --> 00:43:31,660 in Argentina so that's pretty cool 1062 00:43:31,660 --> 00:43:34,340 and Nico who put this cup on 1063 00:43:34,340 --> 00:43:36,160 is going to be throwing it again next year 1064 00:43:36,160 --> 00:43:38,140 so if people are hackers that like football 1065 00:43:38,140 --> 00:43:39,760 which I know is a strange mix because that means 1066 00:43:40,460 --> 00:43:41,940 athleticism and getting out from the basement 1067 00:43:42,860 --> 00:43:44,300 but it will be on next year 1068 00:43:44,300 --> 00:43:45,620 so if you want to enter a team 1069 00:43:45,620 --> 00:43:47,300 get in touch with Nico Weissman 1070 00:43:47,300 --> 00:43:49,020 and he'll give you all the details 1071 00:43:49,400 --> 00:43:50,560 other than that thank you