1 00:00:00,000 --> 00:00:03,500 Without much ado, it is my pleasure to introduce to you Rogan Dawes. 2 00:00:13,860 --> 00:00:17,280 Good morning everyone. Can you hear me fine? Awesome. 3 00:00:18,240 --> 00:00:20,380 Thank you Ming for that introduction. 4 00:00:20,820 --> 00:00:25,500 And thanks to all of you for making this your first talk of DEF CON, or at least of today. 5 00:00:25,500 --> 00:00:29,400 I was a little bit nervous when I saw I had the 10 o'clock slot. 6 00:00:29,580 --> 00:00:32,440 It's like, oh, people are still going to be sleeping after partying last night. 7 00:00:33,240 --> 00:00:37,720 So I appreciate all of you who've come out and chosen to come here. 8 00:00:38,960 --> 00:00:40,200 So what is this talk about? 9 00:00:40,840 --> 00:00:44,680 It's going to be a presentation and demonstration of a tool called Mallet 10 00:00:44,680 --> 00:00:47,320 that I've been working on for the past year or so. 11 00:00:48,220 --> 00:00:54,740 It's a framework for creating intercepting proxies for protocols other than HTTP and HTTPS. 12 00:00:56,400 --> 00:00:59,580 It doesn't operate at the sort of ARP level. 13 00:00:59,820 --> 00:01:03,160 So somebody said, arbitrary proxies, can it do ICMP and ARP? No. 14 00:01:04,040 --> 00:01:08,260 It's not aimed at that level. That would be a bit too low level. 15 00:01:08,960 --> 00:01:15,160 But TCP and UDP proxies, absolutely capable of doing serial. 16 00:01:15,720 --> 00:01:18,480 So any sort of serial kind of links. 17 00:01:19,680 --> 00:01:24,520 And possibly Bluetooth. So it's got a mechanism for arbitrary transports. 18 00:01:24,520 --> 00:01:26,160 So those things can be built in. 19 00:01:26,440 --> 00:01:28,760 So it's written in Java based on the NETI framework. 20 00:01:29,120 --> 00:01:34,280 And it is scriptable in any JSR 223 compatible language. 21 00:01:34,540 --> 00:01:37,260 So we can move on then. 22 00:01:37,780 --> 00:01:40,780 Okay, so this is the obligatory corporate sponsorships slide. 23 00:01:41,040 --> 00:01:43,060 They paid for me to come here, so I've got to tell you about us. 24 00:01:43,280 --> 00:01:45,100 I work for a company called SensePost. 25 00:01:45,860 --> 00:01:50,900 We are a fairly small information security consultancy based out of South Africa. 26 00:01:51,320 --> 00:01:53,260 We have offices in... 27 00:01:53,260 --> 00:01:56,220 Well, our head office is in Pretoria. 28 00:01:57,040 --> 00:02:01,340 We have offices as well in the UK, in London, and in Cape Town. 29 00:02:02,440 --> 00:02:06,540 So we do security analysis, assessments. 30 00:02:06,760 --> 00:02:09,940 We build, we break, and we do training as well. 31 00:02:10,700 --> 00:02:14,880 We've been training at Black Hat for the last 18 years or so. 32 00:02:16,100 --> 00:02:19,180 So we were founded in 2000... 33 00:02:19,180 --> 00:02:21,180 Sorry, 2001. 34 00:02:21,180 --> 00:02:23,600 On the 14th of February, Valentine's Day. 35 00:02:24,180 --> 00:02:26,080 You know, what else are nerds going to be doing, right? 36 00:02:28,060 --> 00:02:30,520 Okay, and if you want to get a hold of me, that's who I am. 37 00:02:33,180 --> 00:02:36,740 So why do we care about intercepting arbitrary protocols? 38 00:02:37,180 --> 00:02:38,440 You know, we've got this... 39 00:02:38,880 --> 00:02:42,820 These tools for intercepting HTTP and HTTPS, and that should be enough, right? 40 00:02:44,920 --> 00:02:49,220 Realistically, communications in various forms are fundamental between... 41 00:02:49,220 --> 00:02:52,640 most of the applications that we actually care about these days. 42 00:02:52,900 --> 00:02:57,060 You know, a standalone application is mostly not particularly interesting. 43 00:02:59,680 --> 00:03:01,460 So most of that traffic is... 44 00:03:01,460 --> 00:03:04,280 Well, a good chunk of it is using HTTP and HTTPS, 45 00:03:04,280 --> 00:03:07,580 and we've got a good set of tools for working with that, 46 00:03:07,640 --> 00:03:10,140 starting from WebScarab almost 20 years ago, 47 00:03:10,900 --> 00:03:15,940 but now, more recently, the burps and the zaps, etc., 48 00:03:15,940 --> 00:03:19,200 are doing a pretty good job of handling that. 49 00:03:19,200 --> 00:03:21,780 Handling HTTP and HTTPS-related protocols. 50 00:03:25,900 --> 00:03:27,700 The question then arises, though, 51 00:03:27,780 --> 00:03:33,240 what about all of those applications that are not using HTTP and HTTPS? 52 00:03:33,820 --> 00:03:35,260 I think a lot of people go, 53 00:03:35,420 --> 00:03:38,560 I don't really know what to do about this. 54 00:03:38,700 --> 00:03:41,060 They go, yeah, it's using SSL. 55 00:03:41,320 --> 00:03:43,200 Let's call it good, you know? 56 00:03:43,820 --> 00:03:45,800 I can't get in the middle of this. 57 00:03:47,100 --> 00:03:48,660 You know, what do you want from me? 58 00:03:52,060 --> 00:03:54,800 Whereas subversion of that communications 59 00:03:54,800 --> 00:03:57,040 could, in very many cases, 60 00:03:57,180 --> 00:04:01,280 result in the kind of pwnage that we saw 61 00:04:01,280 --> 00:04:03,520 back in the early 2000s, 62 00:04:03,540 --> 00:04:07,600 where we saw things like hidden fields used to store, 63 00:04:08,300 --> 00:04:11,060 you know, prices for shopping cart items, 64 00:04:11,240 --> 00:04:12,860 and that sort of thing. 65 00:04:13,980 --> 00:04:16,460 And those things, I think, in many cases, 66 00:04:16,500 --> 00:04:19,180 may exist simply because nobody's looked at them. 67 00:04:19,200 --> 00:04:23,800 So there's definitely great opportunities 68 00:04:23,800 --> 00:04:27,500 for looking at these other protocols 69 00:04:28,070 --> 00:04:32,780 and just seeing what's actually going on. 70 00:04:35,940 --> 00:04:36,800 Okay. 71 00:04:36,860 --> 00:04:40,240 So on the one hand, 72 00:04:40,260 --> 00:04:43,220 we've got a nice orderly protocol like HTTP, 73 00:04:44,130 --> 00:04:47,120 which is text-based, human-readable, 74 00:04:48,160 --> 00:04:50,620 request-response, very simple. 75 00:04:50,840 --> 00:04:54,640 One party asks a question, the server responds, 76 00:04:55,060 --> 00:04:56,040 and that's the end of it. 77 00:04:56,540 --> 00:04:59,080 Very neat and tidy and orderly, 78 00:04:59,160 --> 00:05:03,180 and, you know, it's a simple thing to... 79 00:05:03,180 --> 00:05:04,320 Okay, I'm going to say simple, 80 00:05:04,400 --> 00:05:06,660 but it's relatively simple to intercept. 81 00:05:06,820 --> 00:05:08,380 Yes, there are variables like chunk encoding 82 00:05:08,380 --> 00:05:11,620 and web sockets and et cetera, et cetera, 83 00:05:11,740 --> 00:05:15,640 but in the main, HTTP and HTTPS 84 00:05:15,640 --> 00:05:17,980 is a simple protocol to deal with. 85 00:05:19,020 --> 00:05:20,460 On the other hand, 86 00:05:20,500 --> 00:05:22,880 we've got all of these other protocols 87 00:05:22,880 --> 00:05:24,940 which may have very different rules 88 00:05:25,410 --> 00:05:26,600 for how they operate. 89 00:05:26,840 --> 00:05:28,960 For example, who speaks first? 90 00:05:29,840 --> 00:05:31,620 Does the server speak first 91 00:05:31,620 --> 00:05:32,980 or does the client speak first? 92 00:05:33,600 --> 00:05:36,060 If you look at SSH, for example, 93 00:05:36,200 --> 00:05:37,940 they both speak at the same time. 94 00:05:38,380 --> 00:05:41,460 They both start sending out a message 95 00:05:41,460 --> 00:05:43,700 of what protocol version they send. 96 00:05:43,700 --> 00:05:47,180 So understanding those rules 97 00:05:47,180 --> 00:05:49,120 becomes a fundamental requirement 98 00:05:49,120 --> 00:05:53,340 for building proxies for a particular protocol. 99 00:05:57,880 --> 00:06:01,960 Other details that may arise, 100 00:06:02,300 --> 00:06:03,560 things like response ordering. 101 00:06:03,900 --> 00:06:05,460 If you send a request, 102 00:06:05,860 --> 00:06:08,340 does the response have to come back in order 103 00:06:08,340 --> 00:06:09,620 or do you number them? 104 00:06:09,780 --> 00:06:12,460 Number your request, like, for example, IMAP. 105 00:06:12,460 --> 00:06:14,780 You send a request in IMAP, 106 00:06:14,940 --> 00:06:18,060 you can send a numbered request number one, 107 00:06:18,140 --> 00:06:19,360 numbered request number two, 108 00:06:19,460 --> 00:06:20,980 and get response two back 109 00:06:20,980 --> 00:06:23,140 before you get response number one. 110 00:06:23,320 --> 00:06:25,480 They come back with a number attached to them 111 00:06:25,480 --> 00:06:26,820 so that you can re-associate them, 112 00:06:26,980 --> 00:06:29,400 but it's a very different thing 113 00:06:29,400 --> 00:06:31,300 to what we may be familiar with 114 00:06:31,300 --> 00:06:33,980 using HTTP proxy interceptors. 115 00:06:35,400 --> 00:06:36,940 Other details that arise, 116 00:06:37,260 --> 00:06:38,340 things like timeouts. 117 00:06:39,700 --> 00:06:40,840 In your browser, 118 00:06:40,840 --> 00:06:44,340 you've got a very forgiving client. 119 00:06:45,200 --> 00:06:48,240 How many of you have left burp intercept on, 120 00:06:48,400 --> 00:06:50,180 come back a day later, 121 00:06:50,240 --> 00:06:53,120 and found a thousand requests stacked up 122 00:06:53,120 --> 00:06:55,060 waiting to be fulfilled? 123 00:06:55,480 --> 00:06:56,960 And when you release those requests, 124 00:06:57,120 --> 00:06:57,660 what happens? 125 00:06:57,760 --> 00:06:58,300 Does everything go, 126 00:06:58,380 --> 00:07:00,600 oh, no, I died, I timed out, this is a disaster? 127 00:07:00,940 --> 00:07:03,560 No, they just accept the answer and carry on going. 128 00:07:04,100 --> 00:07:06,040 You know, it's no big deal. 129 00:07:06,860 --> 00:07:07,960 In other protocols, 130 00:07:08,200 --> 00:07:10,000 it can very much be a big deal 131 00:07:10,000 --> 00:07:11,460 with these timeouts built in. 132 00:07:13,180 --> 00:07:15,220 And those timeouts can be on the client side 133 00:07:15,220 --> 00:07:16,500 as well as on the server side. 134 00:07:16,760 --> 00:07:20,180 I think some of you may have heard of 135 00:07:20,180 --> 00:07:21,880 attacks like slow Loris. 136 00:07:22,880 --> 00:07:24,840 So slow Loris is an attack where 137 00:07:24,840 --> 00:07:26,400 you connect to your HTTP server 138 00:07:26,920 --> 00:07:29,160 and you start typing your request 139 00:07:29,160 --> 00:07:30,180 really, really slowly. 140 00:07:30,420 --> 00:07:32,040 So it uses resources 141 00:07:32,780 --> 00:07:34,400 to process this connection. 142 00:07:34,920 --> 00:07:36,560 So you can tie up the server 143 00:07:36,560 --> 00:07:38,220 with many, many long-lived connections. 144 00:07:38,220 --> 00:07:40,900 So the server starts implementing timeouts. 145 00:07:40,940 --> 00:07:43,620 If you take too long to type your request 146 00:07:43,620 --> 00:07:44,840 or to send your request, 147 00:07:45,060 --> 00:07:46,420 that connection's going to drop. 148 00:07:46,780 --> 00:07:49,480 You don't see that when you're interacting 149 00:07:49,480 --> 00:07:53,400 with the server with burp and zap, et cetera, 150 00:07:53,520 --> 00:07:55,420 because that connection to the server 151 00:07:55,420 --> 00:07:56,300 is actually closed 152 00:07:56,300 --> 00:07:58,720 while you're intercepting a request. 153 00:07:59,140 --> 00:08:01,200 It only establishes a new connection 154 00:08:01,200 --> 00:08:02,160 once you say go. 155 00:08:02,820 --> 00:08:04,700 Establish the connection, gets the answer, 156 00:08:04,840 --> 00:08:06,640 and is quite happy to close that connection 157 00:08:06,640 --> 00:08:07,700 immediately afterwards. 158 00:08:08,040 --> 00:08:10,740 The HTTP protocol supports that kind of operation. 159 00:08:11,000 --> 00:08:14,860 It doesn't require these long-lived TCP connections. 160 00:08:17,080 --> 00:08:20,940 Okay, so just to contrast some of the approaches 161 00:08:20,940 --> 00:08:23,300 and the complexities involved 162 00:08:23,300 --> 00:08:28,100 in dealing with protocols other than the familiar. 163 00:08:30,220 --> 00:08:31,940 Okay, so as a result, 164 00:08:32,060 --> 00:08:36,340 most proxies for protocols other than HTTP and HTTPS 165 00:08:36,340 --> 00:08:39,560 tend to be these sort of one-off, complete kludges, 166 00:08:41,160 --> 00:08:45,160 you know, just enough to test that one thing 167 00:08:45,160 --> 00:08:46,580 that the person was trying to do. 168 00:08:47,280 --> 00:08:51,420 No nice user interface or flexibility built into it. 169 00:08:52,440 --> 00:08:55,980 And, you know, they may be pasted as a gist on GitHub 170 00:08:55,980 --> 00:08:57,300 or something along those lines, 171 00:08:57,440 --> 00:08:59,520 but, you know, there's never any kind of community 172 00:08:59,520 --> 00:09:02,740 or reusability or anything like that built in. 173 00:09:02,940 --> 00:09:04,960 And in many cases, you'll find that 174 00:09:04,960 --> 00:09:07,020 these intercepting proxies are a wrapper 175 00:09:07,020 --> 00:09:08,820 around a tool like SOCAT, 176 00:09:10,480 --> 00:09:12,580 the real Swiss Army knife. 177 00:09:13,800 --> 00:09:15,600 Okay, so there is prior work. 178 00:09:16,260 --> 00:09:18,940 People who have tried to create frameworks 179 00:09:18,940 --> 00:09:21,980 or intercepting proxies and put them out there. 180 00:09:22,520 --> 00:09:24,940 OWASP proxy is a tool I wrote around 2008. 181 00:09:26,020 --> 00:09:27,760 It was a programming library. 182 00:09:28,180 --> 00:09:31,680 So I kind of abandoned that fairly early on 183 00:09:31,680 --> 00:09:34,760 because it really didn't fit in as a tool 184 00:09:34,960 --> 00:09:37,020 for people to use. 185 00:09:37,220 --> 00:09:40,840 Rather, it was a supporting infrastructure, if you like. 186 00:09:41,320 --> 00:09:43,740 Martin Swender wrote HatKit proxy 187 00:09:43,740 --> 00:09:45,140 based on top of OWASP proxy. 188 00:09:46,000 --> 00:09:48,120 That didn't get very much traction. 189 00:09:49,180 --> 00:09:51,600 Mallory is probably the most well-known 190 00:09:51,600 --> 00:09:54,400 of the available intercepting proxies 191 00:09:54,400 --> 00:09:55,920 for arbitrary protocols. 192 00:09:56,200 --> 00:09:57,960 That was created around 2010. 193 00:09:58,460 --> 00:10:03,380 I think the last update for that is around 2011. 194 00:10:03,380 --> 00:10:07,100 So it's kind of abandoned and dead. 195 00:10:08,940 --> 00:10:12,680 And it kind of suggests that this is a hard problem 196 00:10:12,680 --> 00:10:14,860 if the people who wrote the tool 197 00:10:15,380 --> 00:10:16,920 aren't even using it themselves. 198 00:10:17,280 --> 00:10:19,680 You would expect them to be making those updates available. 199 00:10:20,980 --> 00:10:23,500 A bunch of others, all of these ones with a slash in them 200 00:10:23,500 --> 00:10:25,100 are referring to GitHub projects. 201 00:10:28,640 --> 00:10:31,360 Most of them, again, kind of clunky 202 00:10:31,360 --> 00:10:32,620 and difficult to get set up. 203 00:10:33,440 --> 00:10:36,200 Mallory is probably amongst the worst culprits 204 00:10:36,200 --> 00:10:38,800 in terms of difficulty in getting it working. 205 00:10:39,180 --> 00:10:41,060 You've got to set up a virtual machine 206 00:10:41,060 --> 00:10:43,080 and then you've got IP tables rules 207 00:10:43,080 --> 00:10:44,220 that you've got to worry about 208 00:10:44,220 --> 00:10:47,200 and routing tables and all of that sort of nonsense. 209 00:10:47,500 --> 00:10:50,180 And it's really a complicated thing 210 00:10:50,180 --> 00:10:51,880 just to get started. 211 00:10:52,220 --> 00:10:54,320 And I think that's part of the problem, right? 212 00:10:54,420 --> 00:10:56,020 Nobody wants to go through the pain 213 00:10:56,020 --> 00:10:58,340 just to intercept their first packet. 214 00:11:01,280 --> 00:11:03,360 So some of my objections 215 00:11:03,360 --> 00:11:06,400 when creating Mallet 216 00:11:06,400 --> 00:11:08,400 was to get around those pain points 217 00:11:08,400 --> 00:11:09,740 and make it simple for people 218 00:11:09,740 --> 00:11:12,120 to actually get started using Mallet. 219 00:11:12,620 --> 00:11:15,940 Once you actually sort of get that initial gratification, 220 00:11:16,240 --> 00:11:17,440 hey, I'm intercepting a packet, 221 00:11:17,700 --> 00:11:19,400 now you can work forward 222 00:11:19,400 --> 00:11:21,660 and start understanding the complexity of the tool 223 00:11:23,020 --> 00:11:24,040 if necessary, 224 00:11:24,320 --> 00:11:27,640 if it needs to get more complex in your case. 225 00:11:28,020 --> 00:11:30,360 And obviously that's a protocol-dependent thing, right? 226 00:11:30,480 --> 00:11:33,340 The complexity of what you're trying to do, 227 00:11:33,340 --> 00:11:33,800 what you're trying to analyze, 228 00:11:34,080 --> 00:11:36,240 will depend on the complexity of that protocol. 229 00:11:37,940 --> 00:11:41,280 The other objective that I was trying to avoid 230 00:11:41,280 --> 00:11:43,260 or trying to achieve 231 00:11:43,260 --> 00:11:46,100 was avoiding reinventing the wheel. 232 00:11:46,520 --> 00:11:49,680 A lot of these existing proxies 233 00:11:49,680 --> 00:11:51,740 come with a very, very limited support 234 00:11:52,280 --> 00:11:54,640 for a variety of protocols. 235 00:11:55,140 --> 00:11:56,600 And what that means is that 236 00:11:56,600 --> 00:11:58,140 if you want to intercept those protocols 237 00:11:58,140 --> 00:12:00,420 or interpret them from a byte stream 238 00:12:00,420 --> 00:12:01,500 into something meaningful, 239 00:12:01,500 --> 00:12:04,260 you have to create those protocol decoders yourself. 240 00:12:06,220 --> 00:12:07,640 And, you know, I don't know about you, 241 00:12:07,700 --> 00:12:08,100 but I'm lazy. 242 00:12:08,300 --> 00:12:11,540 I would much rather not do that work myself. 243 00:12:11,640 --> 00:12:12,760 If somebody's done it already, 244 00:12:13,260 --> 00:12:16,300 let's leverage that and make use of it. 245 00:12:18,760 --> 00:12:20,180 So a few years back, 246 00:12:20,220 --> 00:12:21,580 I stumbled upon the Netty project. 247 00:12:22,000 --> 00:12:23,840 Has anyone used Netty? Anyone here? 248 00:12:24,580 --> 00:12:25,860 Okay, one over there. All right. 249 00:12:26,000 --> 00:12:27,420 So it's a really cool project. 250 00:12:29,780 --> 00:12:32,220 It's an amazingly designed framework, 251 00:12:32,380 --> 00:12:33,220 really high performance. 252 00:12:34,120 --> 00:12:38,100 It's used by some of the largest companies in the world 253 00:12:38,100 --> 00:12:41,280 for extremely high volumes of data processing. 254 00:12:41,860 --> 00:12:45,340 I saw a report by, I think it was Tristan Lee 255 00:12:45,340 --> 00:12:46,380 or Norman Mora, 256 00:12:46,500 --> 00:12:49,480 one of the maintainers of the Netty framework, 257 00:12:49,760 --> 00:12:51,460 that Apple uses it to transfer 258 00:12:51,460 --> 00:12:53,820 something like four petabytes of data a day. 259 00:12:54,660 --> 00:12:57,060 So, yeah, there's a lot of thought, 260 00:12:57,060 --> 00:12:57,980 which has gone into it, 261 00:12:58,020 --> 00:12:59,620 and it's really nice and cleanly designed. 262 00:13:03,120 --> 00:13:05,920 Okay, so this is a sort of overview 263 00:13:05,920 --> 00:13:07,120 of their architecture. 264 00:13:08,920 --> 00:13:10,960 Suffice really to say that there is 265 00:13:10,960 --> 00:13:15,280 a large existing body of protocol support, 266 00:13:16,120 --> 00:13:18,220 and it's a really neat design 267 00:13:18,220 --> 00:13:21,420 that allows you to simply compose your protocol 268 00:13:22,060 --> 00:13:25,440 by including handlers at appropriate places. 269 00:13:27,060 --> 00:13:29,540 And as I say, there is a large body of existing handlers 270 00:13:29,540 --> 00:13:31,380 that you can simply make use of, 271 00:13:31,480 --> 00:13:34,940 rather than having to write all of those from scratch. 272 00:13:35,760 --> 00:13:37,420 The other neat thing about Netty 273 00:13:37,420 --> 00:13:39,080 is that it is transport independent, 274 00:13:39,500 --> 00:13:42,060 so it supports a variety of different transports. 275 00:13:42,060 --> 00:13:43,860 You can intercept TCP, UDP, 276 00:13:44,920 --> 00:13:49,440 SCTP is serial, so UART communications 277 00:13:49,440 --> 00:13:51,000 or UART transports available. 278 00:13:52,280 --> 00:13:54,740 And as I say, I think it's definitely possible 279 00:13:54,740 --> 00:13:56,840 to create a Bluetooth transport 280 00:13:57,600 --> 00:13:58,980 that would allow us to do, like, 281 00:13:59,060 --> 00:14:02,140 Bluetooth low-energy man-in-the-middle work. 282 00:14:04,720 --> 00:14:06,760 All right, so one of the fundamental concepts 283 00:14:06,760 --> 00:14:09,520 that Netty builds on 284 00:14:09,520 --> 00:14:11,600 is this concept of a channel pipeline. 285 00:14:11,960 --> 00:14:14,760 The transport constructs a channel, 286 00:14:14,860 --> 00:14:17,500 so it's a connection from one end to another, 287 00:14:17,640 --> 00:14:21,520 and then there is this pipeline of handlers. 288 00:14:22,480 --> 00:14:24,520 And the pipeline, 289 00:14:24,740 --> 00:14:26,080 starts at the network layer, 290 00:14:26,280 --> 00:14:27,400 you'll have your socket read, 291 00:14:27,640 --> 00:14:29,540 and then a byte buffer, 292 00:14:29,800 --> 00:14:31,120 an array of bytes essentially, 293 00:14:31,340 --> 00:14:34,380 gets passed into the first handler, 294 00:14:34,500 --> 00:14:39,260 which can then choose to process those bytes in some way, 295 00:14:39,360 --> 00:14:40,800 and then pass the result 296 00:14:40,800 --> 00:14:43,480 onto the next handler in the pipeline. 297 00:14:44,220 --> 00:14:45,240 And so you can see, 298 00:14:45,280 --> 00:14:48,620 you can construct fairly arbitrarily complex pipelines. 299 00:14:49,060 --> 00:14:52,100 You might start off with an initial SSL handler, 300 00:14:52,380 --> 00:14:54,120 so it takes in encrypted bytes, 301 00:14:54,740 --> 00:14:55,640 it does that handshaking, 302 00:14:55,860 --> 00:15:00,120 as an example of the inbound-outline handler number two, 303 00:15:00,320 --> 00:15:03,440 you've got encrypted bytes coming in, 304 00:15:04,820 --> 00:15:07,040 the handshake happening with the client, 305 00:15:07,100 --> 00:15:08,720 so more encrypted bytes going out, 306 00:15:08,920 --> 00:15:10,960 and eventually you'll get some plaintext out of that, 307 00:15:11,140 --> 00:15:14,120 and those plaintext bytes will get passed up the pipeline 308 00:15:14,570 --> 00:15:17,920 for other handlers to process. 309 00:15:21,490 --> 00:15:23,080 So how do we turn this into a proxy? 310 00:15:23,080 --> 00:15:24,120 It's really, really easy. 311 00:15:24,240 --> 00:15:26,400 All we do is we bridge two pipelines together, 312 00:15:26,520 --> 00:15:27,600 or two channels together. 313 00:15:28,120 --> 00:15:31,700 And we do that with what I call a relay handler. 314 00:15:32,000 --> 00:15:35,120 It simply takes messages coming in, 315 00:15:35,140 --> 00:15:37,540 or coming out the end of one pipeline, 316 00:15:37,760 --> 00:15:40,900 and copies them over to the input of the next pipeline. 317 00:15:41,320 --> 00:15:43,380 So those get written back out to the network, 318 00:15:43,660 --> 00:15:45,480 any bytes coming in the other direction, 319 00:15:45,740 --> 00:15:46,880 the same thing happens. 320 00:15:47,300 --> 00:15:48,380 So really simple. 321 00:15:49,820 --> 00:15:51,280 Really what we're doing here 322 00:15:51,280 --> 00:15:52,980 is just providing that relay mechanism, 323 00:15:53,280 --> 00:15:58,020 as well as a mechanism to observe what those messages are. 324 00:16:02,100 --> 00:16:05,220 Okay, so I moaned about how difficult it was 325 00:16:05,220 --> 00:16:07,580 to set up Mallory with IP tables rules, 326 00:16:07,860 --> 00:16:12,540 and routing, and virtual machines, and all of that. 327 00:16:13,680 --> 00:16:14,960 There's an existing protocol. 328 00:16:15,320 --> 00:16:17,400 Can anyone here tell me what I'm talking about here? 329 00:16:19,580 --> 00:16:20,140 Socks. 330 00:16:20,400 --> 00:16:20,960 Okay. 331 00:16:21,280 --> 00:16:26,840 So Socks is a protocol designed for specifying to a proxy 332 00:16:26,840 --> 00:16:28,620 where you would like to connect to. 333 00:16:29,460 --> 00:16:31,280 And for a TCP connection, 334 00:16:31,620 --> 00:16:34,980 that is the simplest possible thing that you can achieve. 335 00:16:36,540 --> 00:16:38,280 Makes life really, really easy. 336 00:16:38,360 --> 00:16:44,120 There are many ways of activating a Socks proxy, 337 00:16:44,240 --> 00:16:47,700 whether it is by choosing a Socks proxy in your user agent, 338 00:16:47,840 --> 00:16:48,580 like your browser. 339 00:16:48,720 --> 00:16:50,480 They've got built-in support for Socks. 340 00:16:50,480 --> 00:16:55,580 Or using an intercepting library, 341 00:16:55,860 --> 00:17:02,140 like T-Socks or proxy chains on Linux or OS X. 342 00:17:02,400 --> 00:17:07,600 Or on Windows, there's this really nice library called ProxyCap 343 00:17:07,600 --> 00:17:12,580 that you can use to specify precisely which application it is 344 00:17:12,580 --> 00:17:13,720 that you're trying to Socksify 345 00:17:14,160 --> 00:17:16,400 and intercept that application's traffic. 346 00:17:19,720 --> 00:17:20,400 Okay. 347 00:17:21,040 --> 00:17:25,760 So this is an example of the default graph that Mallet gives you 348 00:17:25,760 --> 00:17:27,360 when you start it up. 349 00:17:27,600 --> 00:17:32,500 The first thing at the top is a socket channel or a listener 350 00:17:32,500 --> 00:17:35,200 that's listening on a TCP port. 351 00:17:36,160 --> 00:17:38,500 And that is just waiting for an inbound connection. 352 00:17:39,780 --> 00:17:45,360 After that, the rectangular blocks are various handlers, 353 00:17:45,380 --> 00:17:47,720 which are in place along that pipeline. 354 00:17:47,980 --> 00:17:50,380 And then finally, the sink represents the Socks. 355 00:17:50,380 --> 00:17:52,480 And that is that outbound connection to its destination. 356 00:17:56,020 --> 00:17:58,040 So having a listener is all very well, 357 00:17:58,200 --> 00:18:01,280 but if we receive an inbound TCP connection, 358 00:18:01,540 --> 00:18:04,260 we don't necessarily know at this point where we want it to go. 359 00:18:04,660 --> 00:18:07,100 Where should we be connecting our outbound to? 360 00:18:07,320 --> 00:18:11,460 So the easiest way of figuring that out is to add the Socks initializer. 361 00:18:11,660 --> 00:18:13,880 And that wraps a bunch of different handlers 362 00:18:13,880 --> 00:18:15,740 that you don't really need to care too much about. 363 00:18:16,140 --> 00:18:19,300 Suffice it to say that it figures out what the destination is. 364 00:18:20,380 --> 00:18:22,560 And makes that available to the relay handler. 365 00:18:24,880 --> 00:18:29,580 The next rectangle in red is an intercept handler. 366 00:18:29,780 --> 00:18:34,700 This is going to intercept the various events that Netty generates 367 00:18:35,380 --> 00:18:39,460 that allows us to see the requests, the various messages, 368 00:18:39,540 --> 00:18:42,540 and the state of the connection at any time. 369 00:18:44,140 --> 00:18:48,100 Next is the relay handler that just copies those messages 370 00:18:48,100 --> 00:18:50,020 across from one pipeline to the other. 371 00:18:50,380 --> 00:18:54,220 And then you can see the arrows are now blue. 372 00:18:54,320 --> 00:18:56,340 So that represents the outbound connection. 373 00:18:57,540 --> 00:19:00,500 Sorry, the arrows are now red on the outbound connection. 374 00:19:01,520 --> 00:19:04,760 We have one more intercept handler that's now representing 375 00:19:05,370 --> 00:19:08,740 the events that are happening on that outbound connection. 376 00:19:09,060 --> 00:19:10,700 And we can capture those as well. 377 00:19:10,880 --> 00:19:15,820 So just for a note, the event capture and recording 378 00:19:15,820 --> 00:19:17,680 happens in an intercept handler. 379 00:19:17,940 --> 00:19:20,360 Those get reported up to the user interface. 380 00:19:22,320 --> 00:19:27,600 This is an example of some of the methods that you have 381 00:19:27,600 --> 00:19:31,800 available to you if you want to manipulate the packet 382 00:19:31,800 --> 00:19:34,720 or the data or the object in transit. 383 00:19:35,580 --> 00:19:38,760 Channel active is fired when the connection is established 384 00:19:38,760 --> 00:19:40,940 and when your handler is added to the pipeline. 385 00:19:41,180 --> 00:19:43,800 So you can use that for setting up some initial state 386 00:19:43,800 --> 00:19:45,980 or maybe triggering an initial message. 387 00:19:47,440 --> 00:19:50,360 Channel read, we have received a message from the channel 388 00:19:50,380 --> 00:19:50,840 from the network. 389 00:19:51,060 --> 00:19:51,900 Here it is. 390 00:19:53,440 --> 00:19:56,580 Write, we have a message going outbound to the network. 391 00:19:56,720 --> 00:19:57,420 This is the message. 392 00:19:57,600 --> 00:19:59,140 You can now process it at this point. 393 00:19:59,560 --> 00:20:03,060 And then user event triggered just gives you a little bit 394 00:20:03,060 --> 00:20:09,060 of a mechanism of passing state messages or event messages. 395 00:20:09,360 --> 00:20:13,640 Things like SSL handshake successful or handshake failed 396 00:20:13,640 --> 00:20:18,460 or something along those lines may be passed as a result 397 00:20:18,460 --> 00:20:20,000 of this event handler. 398 00:20:21,580 --> 00:20:25,400 So here is a really, really simple event handler. 399 00:20:25,540 --> 00:20:30,880 This is taking an inbound text web socket frame, 400 00:20:31,760 --> 00:20:34,780 replacing the text with the uppercase version of it. 401 00:20:35,980 --> 00:20:41,820 So a dead simple example, but it illustrates that I don't need 402 00:20:41,820 --> 00:20:45,020 to worry in my handler about parsing. 403 00:20:45,120 --> 00:20:48,180 I don't need to worry about have we read enough bytes 404 00:20:48,180 --> 00:20:51,500 to actually read a complete text web socket frame. 405 00:20:51,720 --> 00:20:54,520 That's all being handled by other handlers. 406 00:20:54,660 --> 00:20:55,500 Not my problem. 407 00:20:56,000 --> 00:20:59,180 When we get to this point, I've got a complete text web socket frame 408 00:20:59,180 --> 00:21:02,160 and I can process it and pass it on. 409 00:21:02,380 --> 00:21:06,000 And there is very, very little complexity at this level 410 00:21:06,000 --> 00:21:07,520 that you have to worry about. 411 00:21:10,540 --> 00:21:11,060 Okay. 412 00:21:12,840 --> 00:21:13,560 Demo time. 413 00:21:24,740 --> 00:21:25,300 Okay. 414 00:21:27,940 --> 00:21:31,000 So for my demonstration, I'm going to be showing you 415 00:21:31,000 --> 00:21:38,520 a very simple text-based JSON connection. 416 00:21:39,060 --> 00:21:40,680 So it's easy for us to see. 417 00:21:42,720 --> 00:21:45,900 It's going to make a connection to a server that I'm running 418 00:21:45,900 --> 00:21:46,760 on my local machine. 419 00:21:46,760 --> 00:21:48,200 On port 9999. 420 00:21:49,360 --> 00:21:58,380 And if I provide some details, I can send it off to the server 421 00:21:58,380 --> 00:22:03,420 and it does some calculations and gets a message back. 422 00:22:04,260 --> 00:22:04,980 Okay. 423 00:22:05,420 --> 00:22:07,120 Not particularly exciting. 424 00:22:08,100 --> 00:22:11,420 Let's take a look at what's actually going on. 425 00:22:11,900 --> 00:22:13,180 All right. 426 00:22:13,520 --> 00:22:15,440 So let's close that. 427 00:22:18,820 --> 00:22:20,340 And intercept it. 428 00:22:25,120 --> 00:22:29,640 That's all that's needed to do to actually intercept the messages 429 00:22:29,640 --> 00:22:33,480 that are being generated by this client. 430 00:22:34,900 --> 00:22:43,220 So now if I bring my mallet across so that you can see it. 431 00:22:47,640 --> 00:22:49,520 And I go back to my client. 432 00:22:51,040 --> 00:22:53,640 So this is the default graph that I showed you. 433 00:22:54,160 --> 00:22:56,580 Your TCP connection. 434 00:22:57,040 --> 00:22:58,540 It's this thing for a SOX connection. 435 00:22:59,400 --> 00:23:00,960 Intercept, relay, intercept. 436 00:23:01,460 --> 00:23:02,240 And done. 437 00:23:03,780 --> 00:23:09,040 And we can see the data that gets captured on the connections tab. 438 00:23:09,720 --> 00:23:11,520 Source and destination IP address. 439 00:23:11,520 --> 00:23:13,660 And a bunch of other fields. 440 00:23:13,980 --> 00:23:16,180 A number of events that have been seen. 441 00:23:16,780 --> 00:23:18,740 When it was opened and when it was closed. 442 00:23:21,440 --> 00:23:24,080 It's a little bit difficult to show on a small screen. 443 00:23:24,300 --> 00:23:28,440 So that's not all that important anyway. 444 00:23:33,780 --> 00:23:34,300 Okay. 445 00:23:34,400 --> 00:23:35,360 So let's submit that. 446 00:23:35,800 --> 00:23:37,880 And we can see here is our connection. 447 00:23:38,180 --> 00:23:40,440 And there are a whole bunch of events which were triggered. 448 00:23:40,980 --> 00:23:41,500 Okay. 449 00:23:41,500 --> 00:23:42,460 And what that's showing. 450 00:23:44,120 --> 00:23:44,900 Channel register. 451 00:23:45,380 --> 00:23:49,360 These are all reflecting the netty state in essence. 452 00:23:49,460 --> 00:23:51,260 So a number of them you can ignore. 453 00:23:51,580 --> 00:23:56,160 Probably the most useful ones would be things like the user event triggered. 454 00:23:56,360 --> 00:23:57,640 Which gives us some information. 455 00:23:57,880 --> 00:24:01,440 Hey, we're requested to connect to localhost 9999. 456 00:24:01,780 --> 00:24:03,260 That was triggered by our SOX connection. 457 00:24:04,800 --> 00:24:07,720 And then we'll see things like this channel read message. 458 00:24:07,900 --> 00:24:10,480 And that's probably going to be your most interesting. 459 00:24:10,480 --> 00:24:14,560 That is the data which was read from the network. 460 00:24:15,200 --> 00:24:18,440 And in this case we can see we've got a curly brace. 461 00:24:18,980 --> 00:24:20,980 And I'm going to skip that because it's the right. 462 00:24:21,020 --> 00:24:22,080 The outbound right. 463 00:24:23,600 --> 00:24:24,960 On the other channel. 464 00:24:26,220 --> 00:24:27,740 Then we've got a line of data. 465 00:24:29,200 --> 00:24:30,740 A snippet of JSON. 466 00:24:32,180 --> 00:24:34,880 And more JSON. 467 00:24:35,000 --> 00:24:35,520 Etc. 468 00:24:35,680 --> 00:24:35,860 Etc. 469 00:24:35,980 --> 00:24:36,140 Etc. 470 00:24:36,240 --> 00:24:37,960 This is not particularly useful. 471 00:24:38,260 --> 00:24:39,780 I mean it's all broken up into. 472 00:24:39,780 --> 00:24:41,540 Into lots of different pieces. 473 00:24:41,840 --> 00:24:44,780 So it's a real pain to work like this. 474 00:24:46,680 --> 00:24:52,880 Rather let's try and frame this message into beginning and end. 475 00:24:53,060 --> 00:24:56,400 And then combine that into a single thing that we can work with. 476 00:24:57,140 --> 00:25:01,320 So netty amazingly has an existing handler for this. 477 00:25:01,440 --> 00:25:02,900 Not reinventing the wheel. 478 00:25:03,120 --> 00:25:05,120 All it does is it counts braces. 479 00:25:05,540 --> 00:25:08,300 So you'll have an opening curly brace. 480 00:25:08,440 --> 00:25:09,500 And as you count. 481 00:25:09,500 --> 00:25:11,000 As you open more curly braces. 482 00:25:11,080 --> 00:25:12,280 And you get nested further down. 483 00:25:12,380 --> 00:25:13,060 It just counts up. 484 00:25:13,280 --> 00:25:14,820 And as those braces get closed. 485 00:25:15,060 --> 00:25:15,640 It counts down. 486 00:25:15,880 --> 00:25:17,020 When it gets to zero. 487 00:25:17,200 --> 00:25:18,020 End of message. 488 00:25:18,400 --> 00:25:19,820 And it's got some logic for handling. 489 00:25:20,040 --> 00:25:22,420 You know quotes strings with curly braces in. 490 00:25:23,280 --> 00:25:25,220 Recognizing that it's in a quoted context. 491 00:25:25,660 --> 00:25:27,520 And doesn't affect the count. 492 00:25:29,020 --> 00:25:30,040 So let's do that. 493 00:25:30,180 --> 00:25:34,400 I'm going to open a demonstration graph. 494 00:25:37,260 --> 00:25:38,200 Not that one. 495 00:25:47,000 --> 00:25:48,280 I'm going to show you how to do that. 496 00:25:48,520 --> 00:25:53,520 And what we've done is we've just added this additional module. 497 00:25:55,460 --> 00:25:56,860 Or additional handler. 498 00:25:57,340 --> 00:26:00,520 This json object ecoder class that I've just mentioned. 499 00:26:02,000 --> 00:26:05,640 So it's going to decode these bytes. 500 00:26:05,940 --> 00:26:08,660 And just amalgamate them into a single message. 501 00:26:10,500 --> 00:26:12,800 So I added it on the inbound connection. 502 00:26:13,000 --> 00:26:16,060 And I've added another one on the outbound connection as well. 503 00:26:16,180 --> 00:26:21,580 Just in case the server is sending fragmented messages as well. 504 00:26:23,720 --> 00:26:24,480 There it is. 505 00:26:24,980 --> 00:26:25,460 Okay. 506 00:26:25,520 --> 00:26:26,480 Let's run that again. 507 00:26:33,660 --> 00:26:35,560 So we can see that inbound connection. 508 00:26:36,020 --> 00:26:36,760 But now. 509 00:26:36,760 --> 00:26:40,160 Instead of having multiple byte buffs. 510 00:26:40,460 --> 00:26:43,100 Byte arrays or messages read. 511 00:26:43,320 --> 00:26:46,740 You can see we've got a ton of reads and read completes. 512 00:26:47,020 --> 00:26:54,060 No data until that json decoder has actually finished reading the whole packet. 513 00:26:54,460 --> 00:27:00,920 And there we can see the entire message has been amalgamated into a single message. 514 00:27:02,640 --> 00:27:06,020 So that now makes it a lot easier to work with. 515 00:27:06,020 --> 00:27:09,080 We can process this in one piece. 516 00:27:09,200 --> 00:27:12,620 Without having to like step between all the different parts of it. 517 00:27:12,920 --> 00:27:14,600 But you know it's json right. 518 00:27:14,720 --> 00:27:16,580 You know json is a text based protocol. 519 00:27:16,820 --> 00:27:20,500 We don't really want to deal with it using an array of bytes. 520 00:27:20,800 --> 00:27:22,140 That's not particularly friendly. 521 00:27:24,800 --> 00:27:30,180 So what we can do now to simplify our life. 522 00:27:30,520 --> 00:27:35,820 Is translate that message from an array of bytes. 523 00:27:35,820 --> 00:27:36,940 To a string. 524 00:27:37,260 --> 00:27:38,380 A java string. 525 00:27:44,180 --> 00:27:47,680 So we've got our json object decoder that we added previously. 526 00:27:48,260 --> 00:27:51,020 And then once we've decoded that entire message. 527 00:27:51,680 --> 00:27:53,860 We're going to pass that through a. 528 00:27:54,480 --> 00:27:55,400 A string. 529 00:27:55,540 --> 00:27:58,040 A pair of string encoders and decoders. 530 00:27:58,240 --> 00:28:00,120 The string encoder and decoder. 531 00:28:00,220 --> 00:28:02,900 The decoder will simply take an array of bytes. 532 00:28:03,400 --> 00:28:05,640 And transform that into a java string. 533 00:28:06,060 --> 00:28:07,320 In the opposite direction. 534 00:28:07,500 --> 00:28:08,820 You'll have a string coming out. 535 00:28:08,960 --> 00:28:10,520 We want to convert that back to bytes. 536 00:28:10,660 --> 00:28:12,440 Because we can only write bytes to the network. 537 00:28:12,520 --> 00:28:12,960 Not strings. 538 00:28:13,820 --> 00:28:17,480 So the string encoder will encode that string. 539 00:28:17,660 --> 00:28:19,120 As an array of bytes for us. 540 00:28:20,180 --> 00:28:24,180 And you can specify constructor parameters. 541 00:28:24,560 --> 00:28:25,580 Or constructor arguments. 542 00:28:27,380 --> 00:28:30,520 That are needed by our string encoder. 543 00:28:30,660 --> 00:28:31,380 In this case. 544 00:28:31,380 --> 00:28:34,440 We're going to consider our array of bytes. 545 00:28:34,580 --> 00:28:37,840 To be encoded using UTF-8 encoding. 546 00:28:38,300 --> 00:28:39,800 Which is an important detail. 547 00:28:41,960 --> 00:28:43,200 So you can see here. 548 00:28:44,600 --> 00:28:46,020 We've got the class definition. 549 00:28:46,380 --> 00:28:50,220 And that is simply the name of the built in netty class. 550 00:28:51,280 --> 00:28:54,940 That is available for us to use. 551 00:28:55,020 --> 00:28:58,740 So any existing netty handler classes. 552 00:28:59,080 --> 00:29:00,540 We can make use of. 553 00:29:00,540 --> 00:29:01,360 In our pipeline. 554 00:29:01,460 --> 00:29:01,920 Just by. 555 00:29:02,100 --> 00:29:02,660 You know. 556 00:29:02,700 --> 00:29:05,080 Dropping a new object into our graph. 557 00:29:05,260 --> 00:29:08,680 And choosing the class name. 558 00:29:08,900 --> 00:29:11,000 And any parameters that may be required. 559 00:29:16,320 --> 00:29:16,840 Okay. 560 00:29:18,540 --> 00:29:19,640 Let's try that again. 561 00:29:29,840 --> 00:29:34,740 So now you can see that instead of receiving an array of bytes. 562 00:29:35,360 --> 00:29:37,360 We've done a very simple thing. 563 00:29:37,460 --> 00:29:41,060 We've dragged and dropped these additional handlers into place. 564 00:29:41,280 --> 00:29:43,640 And now we've converted those bytes. 565 00:29:44,060 --> 00:29:47,400 Into something that we can actually start using. 566 00:29:47,660 --> 00:29:48,740 And I'll point out. 567 00:29:48,740 --> 00:29:51,160 That there's no programming involved here. 568 00:29:51,260 --> 00:29:54,160 We actually haven't had to write a single line of code. 569 00:29:54,500 --> 00:29:56,240 In order to actually do this. 570 00:29:57,700 --> 00:30:01,160 The graph construction is a matter of. 571 00:30:01,940 --> 00:30:02,580 You know. 572 00:30:02,640 --> 00:30:05,180 Choose the handler that you want from the palette. 573 00:30:06,280 --> 00:30:07,460 Drag it into place. 574 00:30:07,940 --> 00:30:09,940 Like I want to add this in between here. 575 00:30:11,080 --> 00:30:14,040 And it inserts that into the pipeline. 576 00:30:15,560 --> 00:30:18,180 And then you can choose the class that you want. 577 00:30:18,180 --> 00:30:18,760 In this case. 578 00:30:18,800 --> 00:30:21,440 Channel duplex handler is the sort of base class. 579 00:30:21,580 --> 00:30:22,280 An abstract class. 580 00:30:22,540 --> 00:30:23,220 Well not abstract. 581 00:30:23,420 --> 00:30:24,260 It's a complete class. 582 00:30:24,620 --> 00:30:27,560 And then you can just override the various methods. 583 00:30:27,700 --> 00:30:29,900 So this is a do nothing instance. 584 00:30:30,240 --> 00:30:33,920 But it's illustrative of the process. 585 00:30:34,100 --> 00:30:34,300 Right? 586 00:30:38,000 --> 00:30:38,480 Okay. 587 00:30:38,560 --> 00:30:39,340 Back to our connections. 588 00:30:39,680 --> 00:30:42,340 So now we can see that we've received a string. 589 00:30:42,640 --> 00:30:45,060 Which now makes our life a lot more easy. 590 00:30:45,220 --> 00:30:46,920 We can see what's going on. 591 00:30:47,200 --> 00:30:48,160 And we can manipulate it. 592 00:30:48,160 --> 00:30:51,320 We can manipulate this in a much more friendly manner. 593 00:30:53,600 --> 00:30:55,720 So if we were to turn on intercept. 594 00:30:57,080 --> 00:30:59,760 Go back to our client. 595 00:31:03,300 --> 00:31:05,760 You'll see that this connection is now being held. 596 00:31:06,920 --> 00:31:10,600 And we've only got a couple of events that have been triggered initially. 597 00:31:12,900 --> 00:31:14,520 And we can now. 598 00:31:14,520 --> 00:31:18,560 Send those events on. 599 00:31:19,460 --> 00:31:20,160 And. 600 00:31:20,260 --> 00:31:20,800 Oh. 601 00:31:21,170 --> 00:31:21,580 Damn. 602 00:31:22,120 --> 00:31:23,280 What happened? 603 00:31:26,270 --> 00:31:26,820 Time out. 604 00:31:26,920 --> 00:31:27,660 Exactly right. 605 00:31:28,170 --> 00:31:33,500 So our client was unhappy about how long we spent making this. 606 00:31:34,060 --> 00:31:36,480 To forward on that connection. 607 00:31:37,190 --> 00:31:38,260 And as a result. 608 00:31:38,320 --> 00:31:39,700 It's disconnected. 609 00:31:41,150 --> 00:31:41,560 So. 610 00:31:41,640 --> 00:31:43,520 It illustrates once again. 611 00:31:43,520 --> 00:31:44,540 What I was talking about. 612 00:31:44,700 --> 00:31:48,700 The complexity of intercepting protocols that are non. 613 00:31:50,120 --> 00:31:50,960 Non-HTTP. 614 00:31:52,780 --> 00:31:55,080 You have to be aware of things like time outs. 615 00:31:55,240 --> 00:31:55,920 And in some cases. 616 00:31:55,960 --> 00:31:58,040 You may have to be fairly snappy. 617 00:31:58,160 --> 00:32:02,800 When it comes to responding to these intercepted messages. 618 00:32:04,080 --> 00:32:08,820 So the ideal situation is really to script any changes that you have to make. 619 00:32:10,380 --> 00:32:12,100 And I try to make that easy for you. 620 00:32:12,180 --> 00:32:12,580 And I'll show. 621 00:32:12,580 --> 00:32:13,860 Show how that works. 622 00:32:16,060 --> 00:32:16,700 All right. 623 00:32:16,720 --> 00:32:17,340 Let's try it again. 624 00:32:17,700 --> 00:32:18,180 And. 625 00:32:18,280 --> 00:32:20,220 Try and be a little bit snappier about it. 626 00:32:30,600 --> 00:32:31,080 Okay. 627 00:32:31,420 --> 00:32:31,900 Let's. 628 00:32:32,580 --> 00:32:33,520 Send those on. 629 00:32:36,960 --> 00:32:37,840 Client is happy. 630 00:32:37,920 --> 00:32:38,600 Here's our. 631 00:32:40,320 --> 00:32:40,980 Intercepted data. 632 00:32:40,980 --> 00:32:42,640 And I can now change it. 633 00:32:43,260 --> 00:32:43,720 And. 634 00:32:46,500 --> 00:32:47,900 Change some of the data. 635 00:32:47,960 --> 00:32:48,760 That was sent. 636 00:32:53,880 --> 00:32:54,940 And turn off. 637 00:32:55,600 --> 00:32:56,060 Intercept. 638 00:32:56,140 --> 00:32:56,560 And. 639 00:32:56,860 --> 00:32:57,900 There goes the data. 640 00:32:58,280 --> 00:32:59,960 And instead of 46 years old. 641 00:33:00,100 --> 00:33:00,500 Now. 642 00:33:00,620 --> 00:33:01,720 It thinks I am. 643 00:33:02,060 --> 00:33:03,000 26 year old. 644 00:33:03,180 --> 00:33:03,520 Woo hoo. 645 00:33:06,880 --> 00:33:07,340 Okay. 646 00:33:07,460 --> 00:33:07,680 So. 647 00:33:08,180 --> 00:33:09,400 I hope you can see. 648 00:33:09,760 --> 00:33:10,220 That. 649 00:33:10,500 --> 00:33:10,960 Malignancy. 650 00:33:10,960 --> 00:33:10,980 But the thing is. 651 00:33:10,980 --> 00:33:11,900 Is that it actually makes it. 652 00:33:12,760 --> 00:33:13,640 Pretty simple. 653 00:33:13,960 --> 00:33:14,400 To. 654 00:33:14,760 --> 00:33:15,780 Construct these pipelines. 655 00:33:16,560 --> 00:33:17,000 And. 656 00:33:18,280 --> 00:33:18,740 You know. 657 00:33:18,800 --> 00:33:18,900 And. 658 00:33:19,100 --> 00:33:22,160 Start the process of understanding what the protocol looks like. 659 00:33:22,340 --> 00:33:24,480 Not every protocol is going to be as simple as this. 660 00:33:24,760 --> 00:33:25,200 Obviously. 661 00:33:25,420 --> 00:33:25,640 You know. 662 00:33:25,680 --> 00:33:26,780 We're going to have binary protocols. 663 00:33:27,360 --> 00:33:27,560 You know. 664 00:33:27,860 --> 00:33:28,300 Maybe. 665 00:33:28,360 --> 00:33:29,400 SMB for example. 666 00:33:29,580 --> 00:33:32,680 And I would be kind of hesitant to try and intercept SMB. 667 00:33:32,780 --> 00:33:32,900 But. 668 00:33:32,960 --> 00:33:33,540 Who knows. 669 00:33:33,640 --> 00:33:33,840 You know. 670 00:33:33,860 --> 00:33:35,280 You might actually find something interesting. 671 00:33:35,780 --> 00:33:37,140 There would be a fair amount of work. 672 00:33:37,380 --> 00:33:39,480 I'm not trying to take that work away from you. 673 00:33:40,040 --> 00:33:40,480 But. 674 00:33:40,520 --> 00:33:40,940 You're actually. 675 00:33:40,940 --> 00:33:43,080 What I am doing is I'm giving you the framework 676 00:33:43,780 --> 00:33:45,740 to try and make that as easy as possible, 677 00:33:45,880 --> 00:33:48,940 to avoid you reinventing the wheel, 678 00:33:49,160 --> 00:33:52,260 rebuilding this user interface, 679 00:33:53,160 --> 00:33:55,580 this mechanism for you to start drilling down 680 00:33:55,580 --> 00:33:58,000 and understanding what it is that you're doing. 681 00:34:00,460 --> 00:34:06,400 Okay, so there's a detail that I have not shown you. 682 00:34:07,200 --> 00:34:08,460 Here is our server. 683 00:34:12,260 --> 00:34:14,820 It's showing the information that was just received, 684 00:34:15,460 --> 00:34:17,980 but it's also showing that the checksum that it received 685 00:34:18,600 --> 00:34:19,940 no longer validates it. 686 00:34:21,740 --> 00:34:22,980 So now we have a problem. 687 00:34:23,060 --> 00:34:24,120 We've tampered with the packet, 688 00:34:24,380 --> 00:34:28,640 and we haven't reconstructed that MD5 sum 689 00:34:29,180 --> 00:34:31,660 that was being sent as part of that JSON message. 690 00:34:34,020 --> 00:34:34,920 What to do? 691 00:34:36,440 --> 00:34:38,740 Well, obviously there's a detail. 692 00:34:38,920 --> 00:34:40,280 You need to figure out what goes 693 00:34:40,280 --> 00:34:40,920 into that checksum. 694 00:34:40,940 --> 00:34:42,900 In this case, it's the username, 695 00:34:43,200 --> 00:34:46,640 surname, and date of birth concatenated together, 696 00:34:47,180 --> 00:34:50,300 and then converted to bytes and checksum calculated. 697 00:34:52,900 --> 00:34:55,360 But then if we know that, 698 00:34:55,560 --> 00:34:57,520 we can maybe go out to the command line, 699 00:34:57,680 --> 00:34:58,880 and we can recalculate that, 700 00:34:59,020 --> 00:35:00,460 and paste it back in its right place. 701 00:35:00,720 --> 00:35:01,860 But that's a mission, right? 702 00:35:02,100 --> 00:35:03,780 It's not something that you're going to want to do 703 00:35:03,780 --> 00:35:05,540 on every single request. 704 00:35:05,960 --> 00:35:07,520 You're going to want to automate this. 705 00:35:07,920 --> 00:35:10,920 So let's look into how we're going to do this. 706 00:35:10,920 --> 00:35:11,140 We're going to want to do that. 707 00:35:16,500 --> 00:35:20,420 It's all very well having a string version 708 00:35:20,420 --> 00:35:22,760 of a JSON message, 709 00:35:23,000 --> 00:35:25,600 but in some cases, we're going to want to pass it 710 00:35:25,600 --> 00:35:27,360 into something a bit more intelligent, 711 00:35:27,540 --> 00:35:31,120 like provide an actual Java representation of our object. 712 00:35:33,000 --> 00:35:35,620 So let's take a look at the next graph. 713 00:35:43,660 --> 00:35:49,960 What we're doing in this graph is we are adding a script 714 00:35:49,960 --> 00:35:58,220 handler that is making use of the Jackson Java library. 715 00:35:58,580 --> 00:36:03,520 The Jackson library is a JSON parser, or a JSON library, 716 00:36:04,800 --> 00:36:07,900 that is designed to make it easy for you to interact 717 00:36:07,900 --> 00:36:08,700 with JSON objects. 718 00:36:09,340 --> 00:36:10,900 So it can deserialize. 719 00:36:10,920 --> 00:36:12,480 It can deserialize and reserialize JSON objects, 720 00:36:12,720 --> 00:36:15,140 and that's great. 721 00:36:15,240 --> 00:36:15,860 That's what we want. 722 00:36:16,060 --> 00:36:18,260 Once again, this isn't code that I wrote. 723 00:36:18,760 --> 00:36:21,980 Only code I wrote was this tiny little snippet 724 00:36:21,980 --> 00:36:26,080 that tells us when to invoke the Jackson library. 725 00:36:26,720 --> 00:36:33,600 So here we have on an input strip, 726 00:36:33,800 --> 00:36:35,720 we receive an array of bytes. 727 00:36:35,980 --> 00:36:38,800 We convert that into a stream, and we tell Jackson 728 00:36:38,800 --> 00:36:41,380 to read a JSON object from the stream. 729 00:36:41,540 --> 00:36:46,120 We then emit the JSON object into the pipeline 730 00:36:46,120 --> 00:36:48,400 so that it can be processed further. 731 00:36:49,760 --> 00:36:52,240 And similarly, on the outbound step, 732 00:36:52,460 --> 00:36:56,840 we're going to encode the object as a sequence of bytes. 733 00:37:00,120 --> 00:37:01,120 Simple enough. 734 00:37:01,560 --> 00:37:04,560 Back to our client. 735 00:37:05,840 --> 00:37:07,080 Not the client. 736 00:37:07,080 --> 00:37:08,020 Whoops. 737 00:37:09,420 --> 00:37:10,320 Come on. 738 00:37:16,440 --> 00:37:17,640 All right. 739 00:37:19,480 --> 00:37:22,260 So let's take a look at what we got as a result. 740 00:37:22,700 --> 00:37:24,600 Again, we weren't intercepting this one. 741 00:37:27,920 --> 00:37:32,040 But here you can see we've actually got now a Java object 742 00:37:33,080 --> 00:37:36,800 that represents that JSON string. 743 00:37:36,800 --> 00:37:37,800 We had previously. 744 00:37:39,940 --> 00:37:43,620 And of course, nobody wants to worry about writing 745 00:37:43,620 --> 00:37:46,380 or creating a custom editor for every kind of object, 746 00:37:46,560 --> 00:37:47,620 although it can be useful. 747 00:37:48,120 --> 00:37:51,400 Not every object is the easiest thing to drill down 748 00:37:51,400 --> 00:37:52,940 into using Java reflection. 749 00:37:53,960 --> 00:37:57,860 But as a default, I've provided this reflection editor 750 00:37:57,860 --> 00:38:01,080 that interrogates the various properties of the Java object, 751 00:38:01,160 --> 00:38:03,300 so you can drill down into it in that way. 752 00:38:06,900 --> 00:38:09,440 So here you can see the various properties 753 00:38:09,440 --> 00:38:13,360 of that JSON object which was being passed around. 754 00:38:14,020 --> 00:38:14,840 All right. 755 00:38:14,860 --> 00:38:16,020 Let's intercept now. 756 00:38:16,320 --> 00:38:17,600 Play it one more time. 757 00:38:25,220 --> 00:38:28,620 And here is our intercepted JSON node. 758 00:38:33,420 --> 00:38:34,040 Come on. 759 00:38:46,280 --> 00:38:47,500 I'm just going to turn that off. 760 00:38:47,960 --> 00:38:51,840 And once again, having modified the JSON object, 761 00:38:52,880 --> 00:38:53,960 we've reserialized that. 762 00:38:54,000 --> 00:38:56,180 It's now gone out as a stream of bytes finally. 763 00:38:56,860 --> 00:38:57,740 And come back. 764 00:38:58,620 --> 00:39:01,880 And we can see that once again, the client is happy. 765 00:39:02,140 --> 00:39:06,900 But unfortunately, our server is not. 766 00:39:07,000 --> 00:39:08,080 So it's still seeing that it's invalid. 767 00:39:08,260 --> 00:39:11,660 We've done nothing at this point to update the checksum. 768 00:39:16,660 --> 00:39:18,400 Next step, our final graph. 769 00:39:20,620 --> 00:39:21,100 . 770 00:39:27,820 --> 00:39:30,020 I've added one more scripted handler 771 00:39:36,480 --> 00:39:40,700 that is now updating the JSON object. 772 00:39:40,940 --> 00:39:43,360 So when it sees a JSON object coming in, 773 00:39:43,920 --> 00:39:46,360 on the right being written outbound, 774 00:39:46,880 --> 00:39:47,800 receives the message, 775 00:39:47,800 --> 00:39:51,520 it checks that it is in fact a JSON node. 776 00:39:51,680 --> 00:39:53,080 So it's a bit of defensive programming, 777 00:39:53,320 --> 00:39:55,500 making sure that the object that you're dealing with 778 00:39:55,500 --> 00:39:56,700 is in fact what you're expecting. 779 00:39:58,500 --> 00:40:00,880 And then it gets the various properties 780 00:40:00,880 --> 00:40:03,040 which are required to calculate that checksum, 781 00:40:04,740 --> 00:40:05,800 calculates the checksum, 782 00:40:05,840 --> 00:40:07,400 and updates the appropriate node. 783 00:40:10,120 --> 00:40:14,900 So now I can modify any JSON packet 784 00:40:14,900 --> 00:40:16,780 or JSON object coming through 785 00:40:16,780 --> 00:40:18,520 that uses this scheme. 786 00:40:19,100 --> 00:40:20,380 Obviously, details, right? 787 00:40:21,240 --> 00:40:23,540 That uses this encoding scheme that has a name, 788 00:40:24,360 --> 00:40:25,740 surname, and date of birth, 789 00:40:25,920 --> 00:40:26,800 and a checksum field. 790 00:40:27,680 --> 00:40:29,820 And I don't have to worry about that checksum 791 00:40:29,820 --> 00:40:32,040 because it'll be automatically recalculated for me 792 00:40:32,040 --> 00:40:34,140 before it gets written out to the network. 793 00:40:43,400 --> 00:40:43,840 So... 794 00:40:49,180 --> 00:40:50,800 Yeah, taking too long. 795 00:40:53,060 --> 00:40:54,440 Nope, there we have it. 796 00:40:54,640 --> 00:40:55,620 Okay, so once more, 797 00:40:55,720 --> 00:40:58,260 I can drill down into that object, 798 00:40:58,420 --> 00:41:00,380 change whatever I like, 799 00:41:07,120 --> 00:41:08,360 send that off, 800 00:41:10,100 --> 00:41:11,760 and back it comes 801 00:41:12,420 --> 00:41:14,600 with a ridiculous date. 802 00:41:17,580 --> 00:41:18,300 Uh... 803 00:41:22,340 --> 00:41:23,440 Come on. 804 00:41:29,420 --> 00:41:32,220 But, importantly, the server's happy with it. 805 00:41:34,540 --> 00:41:37,040 So this demonstrates reconstructing the protocol 806 00:41:37,440 --> 00:41:39,720 to ensure that it retains its integrity, 807 00:41:40,060 --> 00:41:42,420 that the server or the checksums 808 00:41:42,420 --> 00:41:43,680 and anything else that's required 809 00:41:44,400 --> 00:41:46,480 by the protocol, um, 810 00:41:46,480 --> 00:41:47,580 is retained. 811 00:41:48,240 --> 00:41:50,340 And the process that you can go through 812 00:41:50,340 --> 00:41:52,460 in order to establish, 813 00:41:52,760 --> 00:41:54,520 you know, drill down and see 814 00:41:54,520 --> 00:41:56,200 what's actually going on in this protocol. 815 00:41:59,660 --> 00:42:00,260 Um... 816 00:42:03,040 --> 00:42:06,100 So, mallet can create graphs 817 00:42:06,100 --> 00:42:08,380 of fairly arbitrary complexity, 818 00:42:08,660 --> 00:42:10,040 uh, including branches, 819 00:42:10,180 --> 00:42:12,860 which is a nice detail, I think. 820 00:42:13,320 --> 00:42:16,460 So, for example, I'm going to open the... 821 00:42:16,460 --> 00:42:18,860 the sort of demo graph. 822 00:42:21,960 --> 00:42:23,220 And let's zoom out. 823 00:42:27,100 --> 00:42:28,020 Yeah, come on. 824 00:42:31,480 --> 00:42:32,760 So, in this case, 825 00:42:32,840 --> 00:42:35,880 we've actually got two independent graphs going on. 826 00:42:36,280 --> 00:42:37,840 Uh, one is a TCP listener 827 00:42:37,840 --> 00:42:39,800 and the other is a UDP listener. 828 00:42:40,680 --> 00:42:41,240 Um... 829 00:42:41,240 --> 00:42:44,080 The TCP listener is accepting SOX connections, 830 00:42:44,320 --> 00:42:44,840 as we've seen. 831 00:42:44,840 --> 00:42:46,560 And then there is a branch 832 00:42:46,560 --> 00:42:48,760 that branches depending on the port 833 00:42:48,760 --> 00:42:50,640 that is being requested. 834 00:42:50,940 --> 00:42:52,780 So, this could be a, uh, 835 00:42:52,780 --> 00:42:53,900 regex max on... 836 00:42:53,900 --> 00:42:55,380 match on a particular host name. 837 00:42:55,540 --> 00:42:56,740 Uh, but in this case, 838 00:42:56,760 --> 00:42:59,340 I'm only caring about the port component. 839 00:42:59,640 --> 00:43:01,860 So, if you're connecting on port 443, 840 00:43:01,980 --> 00:43:03,040 it'll go down this branch. 841 00:43:03,160 --> 00:43:04,920 Port 80, it'll go down this branch. 842 00:43:05,100 --> 00:43:07,160 And then anything else will just get 843 00:43:07,160 --> 00:43:09,520 simply relayed as we've seen already. 844 00:43:12,260 --> 00:43:12,780 Um... 845 00:43:12,780 --> 00:43:13,980 For port 443, 846 00:43:13,980 --> 00:43:15,460 the first thing we're going to do 847 00:43:15,460 --> 00:43:17,560 is add an SSL interceptor. 848 00:43:18,260 --> 00:43:21,320 Um, and mallet does have the capability, 849 00:43:21,720 --> 00:43:23,500 uh, by default, 850 00:43:23,600 --> 00:43:25,520 it will automatically generate a certificate 851 00:43:25,520 --> 00:43:28,140 that matches the host name that was requested. 852 00:43:28,420 --> 00:43:30,000 So, much like we've seen with burp, 853 00:43:30,120 --> 00:43:33,160 uh, and web scarab, et cetera, in the past, 854 00:43:33,260 --> 00:43:35,840 um, you don't have to worry about, 855 00:43:35,960 --> 00:43:38,000 you know, clicking through certificate warnings. 856 00:43:38,260 --> 00:43:41,440 All you need to do is install the CACERT, 857 00:43:41,560 --> 00:43:43,360 uh, that mallet generates. 858 00:43:43,980 --> 00:43:45,940 Uh, in your browser or in your user agent 859 00:43:45,940 --> 00:43:47,340 or your certificate store, 860 00:43:47,620 --> 00:43:50,320 and you can make these outbound connections 861 00:43:50,320 --> 00:43:51,020 without, oh, sorry, 862 00:43:51,100 --> 00:43:51,940 you can intercept connections 863 00:43:51,940 --> 00:43:53,600 without triggering those warnings. 864 00:43:55,020 --> 00:43:56,180 Following on from that, 865 00:43:56,240 --> 00:43:58,760 we have an HTTP server codec. 866 00:43:58,800 --> 00:44:00,800 What that's doing is it is decoding 867 00:44:00,800 --> 00:44:03,540 the inbound bytes and, um, 868 00:44:03,600 --> 00:44:06,360 transforming those into an HTTP request object. 869 00:44:07,280 --> 00:44:10,780 Um, well, it's actually two different types of objects. 870 00:44:10,980 --> 00:44:11,960 The first, um, 871 00:44:12,100 --> 00:44:13,960 because NETI is a high-performance object, 872 00:44:13,980 --> 00:44:14,400 it has a high-performance library. 873 00:44:14,600 --> 00:44:16,380 It parses out the headers separately 874 00:44:16,380 --> 00:44:18,260 from the content of the messages. 875 00:44:19,180 --> 00:44:22,240 Um, you don't necessarily want to have to buffer 876 00:44:22,240 --> 00:44:25,140 an entire two-gig file upload into memory. 877 00:44:25,360 --> 00:44:27,180 So, NETI, by default, 878 00:44:27,320 --> 00:44:29,140 will stream chunks of content, 879 00:44:29,380 --> 00:44:31,440 um, up the pipeline, 880 00:44:31,560 --> 00:44:33,000 allowing you to process that dynamically. 881 00:44:33,400 --> 00:44:34,520 For our purposes, 882 00:44:34,780 --> 00:44:36,860 if we're only going to be dealing with small messages, 883 00:44:36,980 --> 00:44:39,420 probably chunking those up into a single object 884 00:44:40,420 --> 00:44:42,560 is a bit friendlier to work with. 885 00:44:43,980 --> 00:44:47,060 So, um, there is this additional, um, 886 00:44:47,240 --> 00:44:48,700 HTTP object decoder. 887 00:44:48,840 --> 00:44:51,080 So what that does is it just aggregates the messages 888 00:44:51,080 --> 00:44:54,400 and the, um, those content chunks 889 00:44:54,810 --> 00:44:56,120 back into a single object. 890 00:44:59,600 --> 00:45:02,140 The next step is, in this case, 891 00:45:02,140 --> 00:45:03,800 a WebSocket upgrade handler. 892 00:45:03,880 --> 00:45:05,600 So just demonstrating, once again, 893 00:45:05,700 --> 00:45:08,280 so complexity of protocol handling 894 00:45:08,280 --> 00:45:11,700 and the ability to do pretty, pretty amazing things 895 00:45:11,700 --> 00:45:13,440 with, uh, a graph. 896 00:45:13,980 --> 00:45:15,800 What this is doing is it is listening for 897 00:45:15,800 --> 00:45:18,340 a successful WebSockets upgrade message. 898 00:45:19,280 --> 00:45:22,420 So if you were to, um, start negotiating 899 00:45:22,420 --> 00:45:24,480 a WebSockets connection with something, 900 00:45:25,480 --> 00:45:27,800 it would be able to monitor that, 901 00:45:27,840 --> 00:45:30,240 hey, there is now a successful WebSocket protocol upgrade. 902 00:45:30,480 --> 00:45:34,140 If I still sit here with an HTTP handler, 903 00:45:34,340 --> 00:45:36,800 I'm going to corrupt these messages entirely 904 00:45:36,800 --> 00:45:38,580 and I'm going to fail to parse them. 905 00:45:38,680 --> 00:45:41,120 So it switches out the HTTP codex 906 00:45:41,120 --> 00:45:43,700 and replaces it with an appropriate, 907 00:45:43,700 --> 00:45:46,000 WebSocket frame decoder instead. 908 00:45:46,460 --> 00:45:48,900 And similarly, on the outbound connection, 909 00:45:50,520 --> 00:45:52,440 we've got, um, 910 00:45:53,800 --> 00:45:56,140 that WebSocket frame upgrade handler. 911 00:45:56,320 --> 00:45:59,360 We've got, um, HTTP object encoders. 912 00:45:59,380 --> 00:46:01,040 In this case, a client encoder. 913 00:46:01,100 --> 00:46:03,320 It's taking an HTTP request object 914 00:46:03,320 --> 00:46:04,500 and writing it out as bytes. 915 00:46:04,700 --> 00:46:08,060 And then eventually, um, the SSL client. 916 00:46:09,320 --> 00:46:12,060 This last item down at the bottom. 917 00:46:12,060 --> 00:46:15,640 The plain HTTP is very similar, 918 00:46:15,720 --> 00:46:18,540 just without the SSL components installed. 919 00:46:21,980 --> 00:46:24,960 On the right-hand side, we've got a UDP protocol handler 920 00:46:24,960 --> 00:46:28,520 demonstrating a manipulation of some DNS messages. 921 00:46:28,960 --> 00:46:31,760 Um, if you were to take a DNS client 922 00:46:31,760 --> 00:46:35,020 and point it to port 105.3 on localhost, 923 00:46:35,240 --> 00:46:39,320 you would send a request for, say, google.com, 924 00:46:39,460 --> 00:46:41,820 and you'll get back an answer for senspose.com. 925 00:46:41,820 --> 00:46:44,180 It's changing the request, 926 00:46:44,520 --> 00:46:47,340 or the details that you requested in that message, 927 00:46:47,560 --> 00:46:49,840 um, to something completely different, 928 00:46:49,940 --> 00:46:52,500 and then rebuilding that DNS response 929 00:46:52,500 --> 00:46:54,600 with a certain amount of protocol knowledge, obviously. 930 00:46:54,860 --> 00:46:57,860 Um, it did take me a while to figure out 931 00:46:57,860 --> 00:46:59,680 that my client's complaining 932 00:46:59,680 --> 00:47:01,240 because it's receiving a response 933 00:47:01,820 --> 00:47:03,460 for senspose.com 934 00:47:03,460 --> 00:47:06,820 when what it was actually asking for was google.com. 935 00:47:06,960 --> 00:47:09,080 So you need to reconstruct the response 936 00:47:09,080 --> 00:47:10,900 and say, hey, yes, this is actually a response 937 00:47:10,900 --> 00:47:12,380 for your request to google.com. 938 00:47:12,640 --> 00:47:13,000 Anyway. 939 00:47:14,320 --> 00:47:18,240 So we have a, um, a fixed target handler here, 940 00:47:18,340 --> 00:47:22,060 which says, at a UDP level, we're not using SOX, 941 00:47:22,260 --> 00:47:23,920 so it needs to know where to send 942 00:47:23,920 --> 00:47:25,280 that outbound connection to. 943 00:47:25,400 --> 00:47:29,860 So here we just say, connect to port 8888, 944 00:47:29,940 --> 00:47:31,580 Google's DNS. 945 00:47:34,640 --> 00:47:39,200 Um, the script handler is just setting up the DNS, um, parsers. 946 00:47:42,580 --> 00:47:45,000 Uh, and the reason for that is, 947 00:47:46,760 --> 00:47:47,860 actually, I could have, 948 00:47:47,900 --> 00:47:50,860 I could have dragged those on as, um, 949 00:47:52,640 --> 00:47:53,480 as blocks. 950 00:47:53,720 --> 00:47:56,280 It's not really a good reason for them not being there. 951 00:47:56,700 --> 00:47:58,320 Um, intercept handler, 952 00:47:58,920 --> 00:48:01,060 logging handler, DNS rewrite. 953 00:48:01,420 --> 00:48:05,680 This is a handler that I wrote in Java. 954 00:48:06,020 --> 00:48:08,740 Um, you can use a variety of scripting languages. 955 00:48:08,840 --> 00:48:10,880 As you've seen, there was the script that was written 956 00:48:10,900 --> 00:48:14,840 in Groovy that did the, um, the update of the JSON objects. 957 00:48:16,640 --> 00:48:20,220 Um, this, it's sometimes easier to write it in Java 958 00:48:20,220 --> 00:48:22,620 simply because you get the, the syntax highlighting 959 00:48:22,620 --> 00:48:24,280 and code completion, and I'm lazy. 960 00:48:25,680 --> 00:48:27,340 Um, and so, yeah. 961 00:48:27,480 --> 00:48:29,720 So that's just that, uh, simple demonstration 962 00:48:29,720 --> 00:48:32,980 of a DNS packet, and let me... 963 00:48:37,900 --> 00:48:39,380 show you how it works. 964 00:48:40,900 --> 00:48:40,980 Ah. 965 00:48:53,940 --> 00:48:55,240 One, two, five, three. 966 00:49:02,940 --> 00:49:03,360 Ah. 967 00:49:07,800 --> 00:49:08,840 Yeah, man, what am I doing? 968 00:49:18,880 --> 00:49:19,800 There we go, sorry. 969 00:49:20,680 --> 00:49:21,680 NSLookup versus Digg. 970 00:49:24,880 --> 00:49:26,800 Uh, I do not have intercept on. 971 00:49:26,900 --> 00:49:27,760 Why is this not working? 972 00:49:28,680 --> 00:49:30,240 Demo gods, don't fail me now. 973 00:49:31,960 --> 00:49:33,500 Yeah, on one, oh, five, three. 974 00:49:36,800 --> 00:49:37,200 Ah. 975 00:49:38,040 --> 00:49:39,340 I'm not connected to the network, 976 00:49:39,340 --> 00:49:40,920 so that's, uh, a good explanation 977 00:49:40,920 --> 00:49:42,040 for why that's not working. 978 00:49:42,420 --> 00:49:45,120 All right, so if I had run that successfully, 979 00:49:45,280 --> 00:49:47,040 you would have seen a response coming back 980 00:49:47,040 --> 00:49:50,400 for SensePost.com instead of, uh, Google.com. 981 00:49:52,720 --> 00:49:56,100 And, um, so that is the end of my presentation. 982 00:49:58,900 --> 00:50:02,360 If you have any questions, 983 00:50:03,120 --> 00:50:04,740 I am not going to be running away. 984 00:50:04,920 --> 00:50:07,940 You can also get hold of me at RoganDoors on Twitter. 985 00:50:07,940 --> 00:50:11,060 The repo is at SensePost. 986 00:50:11,940 --> 00:50:15,020 Um, the GitHub repo is at SensePost slash mallet. 987 00:50:15,160 --> 00:50:18,100 And, um, please send me pull requests 988 00:50:18,100 --> 00:50:19,660 if you have any problems. 989 00:50:21,620 --> 00:50:23,820 Um, thanks for the presentation. 990 00:50:24,300 --> 00:50:28,120 Um, I was wondering whether you maintain some global state, 991 00:50:28,380 --> 00:50:30,560 uh, between the different, uh, sessions. 992 00:50:30,720 --> 00:50:32,400 So you, you, here in this case, 993 00:50:32,440 --> 00:50:35,680 you only had one connection going on to the proxy and so on. 994 00:50:35,680 --> 00:50:37,380 Do you have a case where you have, 995 00:50:37,380 --> 00:50:39,340 where you have multiple, 996 00:50:39,480 --> 00:50:41,660 maybe concurrently from different clients, 997 00:50:41,700 --> 00:50:43,080 you actually maintain some state 998 00:50:43,080 --> 00:50:45,960 that can be then manipulated using a script? 999 00:50:46,440 --> 00:50:49,500 Right, so each connection, um, 1000 00:50:49,580 --> 00:50:51,480 has what they call connection attributes. 1001 00:50:52,600 --> 00:50:56,920 Um, I provide a binding to script, 1002 00:50:57,080 --> 00:50:58,140 you know, for the scripting, 1003 00:50:58,600 --> 00:51:00,080 scripted handlers to access. 1004 00:51:00,360 --> 00:51:02,600 Uh, and you can store state in there, 1005 00:51:02,740 --> 00:51:05,060 and that will get passed between connections. 1006 00:51:05,680 --> 00:51:06,040 Okay. 1007 00:51:13,000 --> 00:51:15,640 So, for example, you could use that to implement something 1008 00:51:15,640 --> 00:51:17,520 like an FTP interceptor. 1009 00:51:17,680 --> 00:51:20,840 You've got a, a control connection that is saying, 1010 00:51:21,040 --> 00:51:23,800 you know, please let me connect to a particular port. 1011 00:51:24,160 --> 00:51:27,100 Um, and then you can make a note of what those, 1012 00:51:27,160 --> 00:51:28,920 uh, what that port is that was, 1013 00:51:28,940 --> 00:51:30,700 uh, that it was told to connect to. 1014 00:51:31,020 --> 00:51:33,580 Um, and then in the next connection coming in, 1015 00:51:33,620 --> 00:51:35,080 you could then put an appropriate, 1016 00:51:35,680 --> 00:51:37,560 um, protocol decoder on that, 1017 00:51:37,620 --> 00:51:38,820 on that connection, 1018 00:51:39,700 --> 00:51:41,480 based on the state that you've maintained. 1019 00:51:44,900 --> 00:51:45,800 Anyone else? 1020 00:51:47,120 --> 00:51:47,960 All right. 1021 00:51:48,200 --> 00:51:49,120 Going white. 1022 00:51:49,380 --> 00:51:50,880 This is Spokadage, everyone. 1023 00:51:51,460 --> 00:51:52,260 Thank you. 1024 00:51:52,700 --> 00:51:53,740 Thanks for coming.