1 00:00:01,620 --> 00:00:04,860 Good morning slash afternoon, depending on what time it actually is, everyone. 2 00:00:05,680 --> 00:00:08,240 As he so illustriously introduced me, I'm Mark Bristow. 3 00:00:09,600 --> 00:00:15,040 I am a SCADA security engineer for a consulting firm who, for various reasons of politics, 4 00:00:15,180 --> 00:00:19,180 decided they did not want to be associated with this talk, so I'm here as an independent researcher. 5 00:00:20,800 --> 00:00:25,420 My day job is I perform security assessments for lots of commercial and government clients, 6 00:00:25,680 --> 00:00:29,000 some of whom are probably actually in this room right now because I saw you at Black Hat. 7 00:00:30,340 --> 00:00:34,660 This is really just kind of a personal project that I started putting together at my old company, actually, 8 00:00:34,760 --> 00:00:38,480 and decided to submit it for a talk, and now I'm here. 9 00:00:39,020 --> 00:00:42,480 And by the end of this talk, I'm actually going to release it as a free and open-source tool. 10 00:00:42,760 --> 00:00:47,080 Well, that's a lie. I haven't uploaded the code yet because I'm afraid to use the Wi-Fi. 11 00:00:51,060 --> 00:00:53,940 So just a quick agenda about what we're going to talk about today. 12 00:00:54,380 --> 00:00:56,960 I'm going to start out with just kind of an introduction to SCADA systems 13 00:00:56,960 --> 00:00:59,980 because I'm sure not everyone in this room is a SCADA security engineer. 14 00:01:00,000 --> 00:01:00,180 I'm not a security expert. 15 00:01:01,360 --> 00:01:06,040 Then I'm going to do a real quick primer about ModScan, why I built the tool, 16 00:01:06,240 --> 00:01:08,960 and what kind of problems I'm trying to solve with the tool. 17 00:01:09,960 --> 00:01:13,740 Then I'm going to talk about the ModBus protocol, which is really actually pretty important 18 00:01:13,740 --> 00:01:16,620 because if you don't understand the actual protocol that I'm talking about here, 19 00:01:16,680 --> 00:01:18,480 you're going to get really confused as to what my tool does. 20 00:01:19,460 --> 00:01:21,160 I'm going to go through a little bit of the history. 21 00:01:21,500 --> 00:01:25,520 I'm going to go over packet construction, some of the more interesting functionality, 22 00:01:26,260 --> 00:01:29,760 and then the communication flow and how errors are handled, which becomes really important. 23 00:01:30,000 --> 00:01:31,040 You'll see that in a little bit. 24 00:01:31,920 --> 00:01:37,420 Then I'm going to talk about ModBus TCP, which honestly is just a wrapper for ModBus into TCP, 25 00:01:37,680 --> 00:01:40,880 although they changed the packet structure and the protocol a little bit. 26 00:01:41,300 --> 00:01:44,220 So I'm going to go over that architecture, packet construction, 27 00:01:44,580 --> 00:01:46,140 and then we're going to look at some packet captures. 28 00:01:46,380 --> 00:01:49,720 Conveniently, Wireshark actually decodes the ModBus protocol, so that's kind of nice. 29 00:01:50,660 --> 00:01:52,740 Then we're going to get into the demo. 30 00:01:53,060 --> 00:01:55,160 I actually have a virtual SCADA network. 31 00:01:55,460 --> 00:01:58,160 I had delusions of grandeur of bringing hardware with me at one point, 32 00:01:58,200 --> 00:01:59,360 but, yeah, that didn't happen. 33 00:02:00,400 --> 00:02:04,220 So we've got a virtual machine that's a SCADA network that we're actually going to do a live demo, 34 00:02:04,340 --> 00:02:06,800 and if the demo gods smile upon me, hopefully everything will work. 35 00:02:07,740 --> 00:02:11,120 And then we're just going to go over some project information, Q&A if we have any time, 36 00:02:11,340 --> 00:02:14,780 and like he said, I think I'm going to be in room 104 after this if you have questions. 37 00:02:17,120 --> 00:02:18,740 So I can't give a talk without a disclaimer. 38 00:02:19,060 --> 00:02:20,000 You know why it's there. 39 00:02:20,360 --> 00:02:22,080 Don't be stupid, okay? 40 00:02:22,440 --> 00:02:25,540 This tool was not written with the intent of being particularly malicious, 41 00:02:25,980 --> 00:02:27,700 but you can still do nasty things. 42 00:02:27,820 --> 00:02:28,420 SCADA systems? 43 00:02:28,780 --> 00:02:29,980 Think like 1980s. 44 00:02:30,000 --> 00:02:30,960 80s technology, okay? 45 00:02:31,200 --> 00:02:33,480 I mean, literally, that's what's going on in most of these places. 46 00:02:34,380 --> 00:02:35,880 They're really fragile and temperamental, 47 00:02:36,020 --> 00:02:38,780 and if you run this on some system, you could easily crash it. 48 00:02:39,060 --> 00:02:42,100 So if you do, you didn't hear it from me. 49 00:02:45,000 --> 00:02:48,420 Okay, so like I said, go over a little primer. 50 00:02:48,560 --> 00:02:49,100 What is SCADA? 51 00:02:49,180 --> 00:02:51,160 How many people in this room know what SCADA is? 52 00:02:52,780 --> 00:02:56,840 How many people in this room would bet a beer on their definition of what a SCADA system was? 53 00:02:57,880 --> 00:02:59,020 Yeah, that's what I thought. 54 00:02:59,140 --> 00:02:59,860 You all know that. 55 00:02:59,860 --> 00:02:59,980 You all know that. 56 00:02:59,980 --> 00:03:01,700 It's Supervisory Control and Data Acquisition, 57 00:03:01,760 --> 00:03:05,140 but you have no clue what that actually means based on the number of hands. 58 00:03:05,280 --> 00:03:05,880 I saw, like, three. 59 00:03:06,100 --> 00:03:06,640 What's that? 60 00:03:08,680 --> 00:03:09,160 Deal. 61 00:03:12,540 --> 00:03:16,180 Anyway, SCADA systems are basically the long definitions up on the slide, 62 00:03:16,280 --> 00:03:21,740 but basically it's a system that takes real-time data inputs from various sensors, local or remote, 63 00:03:22,360 --> 00:03:25,900 uses that stuff to make real-time decisions about controlling a process, 64 00:03:26,160 --> 00:03:29,960 and then goes and controls that process, again, from local or remote locations, 65 00:03:29,980 --> 00:03:30,180 and then goes and controls that process, again, from local or remote locations, 66 00:03:31,020 --> 00:03:34,880 A lot of times you also hear the term ICS or Industrial Control Systems used. 67 00:03:36,100 --> 00:03:37,560 They're virtually synonymous. 68 00:03:37,720 --> 00:03:40,440 There is actually some technical differences between the two terms, 69 00:03:40,520 --> 00:03:42,220 but for the most part they mean the same thing. 70 00:03:45,500 --> 00:03:48,260 So, now we know what SCADA is, where do we find SCADA? 71 00:03:49,220 --> 00:03:50,980 SCADA is touching your lives right now. 72 00:03:51,200 --> 00:03:53,360 The power – the lights are on in here, 73 00:03:53,460 --> 00:03:56,680 which means a power system is running somewhere out in probably Hoover Dam, right? 74 00:03:56,980 --> 00:03:57,600 Guess what? 75 00:03:57,820 --> 00:03:59,960 Hoover Dam is run by a SCADA controller. 76 00:03:59,960 --> 00:04:04,820 control network. Power is one of the biggest ones, but you have stuff like water treatment. 77 00:04:05,000 --> 00:04:10,020 This water bottle was manufactured and they put water in it because why? A SCADA system 78 00:04:10,020 --> 00:04:17,560 did that control. Who hates gas? The price of gas. I know I hate the price of gas. Wow, 79 00:04:17,780 --> 00:04:25,300 no hands? Come on, guys. Wake up. It's noon. It's $4 a gallon. What's up with that? That's 80 00:04:25,300 --> 00:04:29,240 all pipe through pipelines. All those pipelines are controlled with SCADA control systems. 81 00:04:29,840 --> 00:04:35,140 In big cities, I'm from Washington, D.C., and I hate traffic because it sucks up more 82 00:04:35,140 --> 00:04:40,060 of my life than I care to admit. Traffic control systems in big cities are typically done by 83 00:04:40,060 --> 00:04:44,300 SCADA. Yes, believe it or not, in some of those crazy hacker movies like Hackers, there 84 00:04:44,300 --> 00:04:48,000 are some systems that you could actually control the traffic lights if you were so inclined. 85 00:04:49,360 --> 00:04:53,440 This is not a comprehensive list of everywhere there's SCADA, but as you can see, this stuff 86 00:04:53,440 --> 00:04:54,560 touches your life a whole lot. 87 00:04:56,900 --> 00:05:02,720 This here is kind of what SCADA looks like, sort of, in a conceptual manner from my experience 88 00:05:02,720 --> 00:05:07,320 with dealing with clients. Typically, you have a big corporate network, some type of 89 00:05:07,320 --> 00:05:12,060 control network, some field sites down here at the bottom, and a whole bunch of stuff 90 00:05:12,060 --> 00:05:15,840 running in between. You've got all kinds of specialty devices. You've got regular servers, 91 00:05:16,460 --> 00:05:21,320 these big things called PLCs, which are the brains of the operation, along with the SCADA 92 00:05:21,320 --> 00:05:24,760 application controller. Notice how there's different color lines on 93 00:05:24,760 --> 00:05:24,900 this. 94 00:05:25,300 --> 00:05:30,000 On this slide, each color here represents a different protocol. SCADA networks today 95 00:05:30,000 --> 00:05:34,960 are just kind of a big hodgepodge of different things. You've got TCP IP running all over 96 00:05:34,960 --> 00:05:39,440 the place. You've got Modbus serial. You've got Modbus TCP. You've got DNP3, all kinds 97 00:05:39,440 --> 00:05:43,720 of stuff just running around, which creates a big problem for the asset owners because 98 00:05:43,720 --> 00:05:45,700 now they have to deal with all these different protocols. 99 00:05:46,840 --> 00:05:52,480 What's changing about this picture is it's all going blue. Everything is starting to 100 00:05:52,480 --> 00:05:55,280 get touched into standard switch TCP networks. 101 00:05:55,280 --> 00:05:59,960 Well, what that means for all of you and us is that, well, we already know how to break 102 00:05:59,960 --> 00:06:05,720 into regular Ethernet networks pretty good. All those proprietary protocols that are the 103 00:06:05,720 --> 00:06:09,800 things that are supposed to provide the security by obscurity in SCADA, yeah, they don't matter 104 00:06:09,800 --> 00:06:14,080 anymore because we can get in and then, over the TCP, break through the routers, break 105 00:06:14,080 --> 00:06:15,060 through the firewalls, and we're done. 106 00:06:15,900 --> 00:06:21,800 The other thing, too, is some of these firewalls, especially that one right there, not usually 107 00:06:21,800 --> 00:06:22,100 there. 108 00:06:24,560 --> 00:06:28,840 It's great. You own a web server, and all of a sudden, you're on the control end, and 109 00:06:28,840 --> 00:06:30,700 you go, really? Okay. 110 00:06:34,440 --> 00:06:38,880 ModScan. We're not here to talk about SCADA architecture. I just need to give you a primer. 111 00:06:39,660 --> 00:06:43,660 What is ModScan? ModScan is basically a tool that I wrote to 112 00:06:43,660 --> 00:06:49,060 detect open Modbus TCP ports and then identify these things called slave IDs because you 113 00:06:49,060 --> 00:06:51,220 can't address any device without knowing its slave ID. 114 00:06:52,100 --> 00:06:53,680 Then you need to associate that with an IP address. 115 00:06:53,940 --> 00:06:56,400 Well, that's what ModScan does, at least what it does today. 116 00:06:57,620 --> 00:07:03,680 My original intent was to make this a reconnaissance tool, something that administrators could 117 00:07:03,680 --> 00:07:04,680 use to map out their network. 118 00:07:05,180 --> 00:07:10,260 Think really, really, really early versions of Nmap, except it only runs on one port. 119 00:07:12,180 --> 00:07:15,800 Basically, I was going for something kind of a general reconnaissance tool is what I 120 00:07:15,800 --> 00:07:16,300 was looking for. 121 00:07:19,420 --> 00:07:21,500 I keep talking about this Modbus thing. 122 00:07:21,500 --> 00:07:24,200 Has anyone here, like, actually read the Modbus protocol spec? 123 00:07:25,260 --> 00:07:26,460 Wow, I'm impressed. 124 00:07:26,660 --> 00:07:28,000 Like, more hands than I thought I'd see. 125 00:07:28,580 --> 00:07:31,400 Okay, well, then, you guys are going to be really bored for this part of the presentation. 126 00:07:31,560 --> 00:07:31,860 I'm sorry. 127 00:07:33,440 --> 00:07:36,040 So Modbus is old, okay? 128 00:07:36,700 --> 00:07:40,960 Modbus was developed in 1979 by a company called Modicon that doesn't even exist anymore. 129 00:07:41,380 --> 00:07:47,160 Okay, so we're talking this is literally 1979, early 1980s technology we're dealing with. 130 00:07:47,620 --> 00:07:48,620 It's pretty basic. 131 00:07:49,760 --> 00:07:50,640 It is free. 132 00:07:50,640 --> 00:07:51,380 It's open source. 133 00:07:51,560 --> 00:07:56,580 You can go to the Modbus website, which is in my references, and download the protocol spec if you want to. 134 00:07:56,980 --> 00:07:58,880 That's kind of how I got started with this whole thing. 135 00:08:00,440 --> 00:08:03,480 Depending on who you ask, it's the most common protocol out there. 136 00:08:04,060 --> 00:08:06,420 And I'd say that's probably a lie, but, like, a pretty good lie. 137 00:08:07,940 --> 00:08:13,640 Almost every major control system network that I've seen has at least some element of Modbus running on it 138 00:08:13,640 --> 00:08:17,140 because it's very, very prevalent because it's so old and well-established. 139 00:08:18,800 --> 00:08:19,880 And it runs. 140 00:08:19,880 --> 00:08:27,680 Modbus TCP is registered on port 502, so if you ever come across port 502, that's typically what you're looking at if it's registered. 141 00:08:28,240 --> 00:08:29,420 And it comes in two flavors. 142 00:08:29,640 --> 00:08:33,480 There's the RTU flavor, which is a binary flavor that I have actually never actually seen deployed. 143 00:08:34,360 --> 00:08:36,840 Most places are running what's called Modbus ASCII. 144 00:08:37,300 --> 00:08:43,760 They're basically the same thing except for the compression format, and they use different CRC versus LRC checks. 145 00:08:43,960 --> 00:08:45,020 It's not really that big a deal. 146 00:08:45,100 --> 00:08:46,080 They're pretty much the same thing. 147 00:08:48,680 --> 00:08:49,680 So what's a Modbus? 148 00:08:49,680 --> 00:08:50,360 What's a Modbus packet look like? 149 00:08:51,880 --> 00:08:54,320 Modbus packet is broken up into two major sections. 150 00:08:55,140 --> 00:09:00,400 There's the ADU, which is the entire packet, and the PDU, which is the protocol data unit, 151 00:09:00,580 --> 00:09:03,240 which is this kind of green stuff going on over here. 152 00:09:03,600 --> 00:09:04,100 All right? 153 00:09:04,900 --> 00:09:10,760 The only thing that is absolutely written as stone in the spec is that the PDU has to exist 154 00:09:10,760 --> 00:09:13,620 to have this thing with the function code and the data. 155 00:09:13,800 --> 00:09:16,640 That's the only thing that you actually have to have for Modbus. 156 00:09:17,120 --> 00:09:19,660 Now, Modbus Serial has this other concept. 157 00:09:19,680 --> 00:09:22,280 It has a slave ID and has error checking on it. 158 00:09:22,660 --> 00:09:29,020 As you can see, it runs up to 256 bytes, which was actually because this was originally designed 159 00:09:29,020 --> 00:09:35,680 in the 70s as a serial protocol, and 256 was the maximum size of an RS-485 packet could be. 160 00:09:35,820 --> 00:09:37,500 So that's why it's the size that it is. 161 00:09:38,540 --> 00:09:39,580 So important notes. 162 00:09:39,720 --> 00:09:43,660 These function codes, we're going to get into these function codes and these slave IDs pretty good here. 163 00:09:44,560 --> 00:09:47,020 Function codes are valid 1 through 127. 164 00:09:47,020 --> 00:09:55,020 The function codes 128 through 255 are reserved for error codes, and we'll get back to error codes. 165 00:09:55,140 --> 00:09:56,300 Like I said, they become important. 166 00:09:57,520 --> 00:10:02,180 And it's big Indian encoding, which is important because if you look at any of the packet captures 167 00:10:02,180 --> 00:10:05,540 and you're not thinking about that, you'll get confused as to why things don't look like they should. 168 00:10:08,720 --> 00:10:12,360 Now, who sees any type of security relevant stuff in this packet? 169 00:10:14,840 --> 00:10:16,900 Yeah, giggles is about the right thing, right? 170 00:10:17,020 --> 00:10:17,320 There's nothing. 171 00:10:17,640 --> 00:10:19,140 There's no authentication in here. 172 00:10:19,240 --> 00:10:20,580 There's no even place for it. 173 00:10:21,120 --> 00:10:23,520 You know, the inherent protocol has absolutely no... 174 00:10:23,520 --> 00:10:24,920 If you're on the wire, you win. 175 00:10:25,000 --> 00:10:25,800 You can send commands. 176 00:10:27,880 --> 00:10:29,800 Modbus uses a master-slave relationship. 177 00:10:30,560 --> 00:10:32,120 Masters don't have to be authenticated. 178 00:10:32,220 --> 00:10:33,580 They don't have to authenticate to slaves. 179 00:10:33,780 --> 00:10:35,620 You just put out your thing, and it happens. 180 00:10:36,100 --> 00:10:39,240 So you make it down to the actual wire on Modbus. 181 00:10:39,820 --> 00:10:42,900 Pretty much the only thing that's left is you don't know how to address the slaves, 182 00:10:43,020 --> 00:10:44,440 and that's what ModScan solves for you. 183 00:10:44,900 --> 00:10:47,000 So basically, if you can get to the wire and run... 184 00:10:47,020 --> 00:10:49,460 If you can run my tool, you own that SCADA network. 185 00:10:49,840 --> 00:10:50,420 Fun stuff. 186 00:10:53,320 --> 00:10:55,520 So, I said I'd get into the function codes a little bit. 187 00:10:55,680 --> 00:10:56,860 Here's the function code reference. 188 00:10:57,140 --> 00:10:58,240 I'm not going to go through all these. 189 00:10:58,360 --> 00:10:59,540 If you want to read them, read the spec. 190 00:10:59,760 --> 00:11:01,120 I don't want to waste my time on that. 191 00:11:02,840 --> 00:11:05,280 Some important things are read coils, 192 00:11:05,940 --> 00:11:09,580 and these read functions are basically to read data, right? 193 00:11:10,260 --> 00:11:12,080 Which becomes very useful in a scanner, 194 00:11:12,160 --> 00:11:13,380 because we don't want to scan with a write, 195 00:11:13,480 --> 00:11:14,700 because some devices are read-only. 196 00:11:15,340 --> 00:11:16,900 But you can write data. 197 00:11:17,020 --> 00:11:17,540 You can write data to devices, 198 00:11:18,260 --> 00:11:20,780 which can be fun if the device is, say, a breaker, 199 00:11:20,960 --> 00:11:22,280 and you want to trip it. 200 00:11:22,400 --> 00:11:24,820 So you just write, switch a bit somewhere, 201 00:11:25,100 --> 00:11:26,960 and all of a sudden, the power goes out in Nevada. 202 00:11:27,200 --> 00:11:27,540 Awesome. 203 00:11:29,540 --> 00:11:31,220 Another interesting thing here is diagnostics. 204 00:11:32,060 --> 00:11:33,520 We'll get into those in just a second. 205 00:11:36,680 --> 00:11:37,680 Also, where is it? 206 00:11:38,700 --> 00:11:40,600 Function code 11, report slave ID. 207 00:11:40,720 --> 00:11:42,940 This is actually the default function code for ModScan. 208 00:11:44,340 --> 00:11:45,500 Now, you might think, 209 00:11:45,500 --> 00:11:49,000 well, why did you write this tool to discover slave IDs 210 00:11:49,000 --> 00:11:51,640 if you just ask it for the slave ID and it gives it to you? 211 00:11:52,160 --> 00:11:55,260 Well, it doesn't, because what that does... 212 00:11:55,260 --> 00:11:56,760 Oops, I went in a slug. 213 00:11:57,920 --> 00:11:58,400 There we go. 214 00:11:59,240 --> 00:12:02,480 Report slave ID gives you a human-readable text string. 215 00:12:02,640 --> 00:12:05,820 So it says, hey, I'm substation number 57, switch 2. 216 00:12:06,180 --> 00:12:09,020 It doesn't give you back the actual addressing slave ID that you need. 217 00:12:09,420 --> 00:12:11,140 So I don't know why it's called that, 218 00:12:11,240 --> 00:12:12,380 but that's what they call it. 219 00:12:12,520 --> 00:12:14,500 If you actually address it with the wrong slave ID 220 00:12:14,500 --> 00:12:15,300 and ask it for this code, 221 00:12:15,300 --> 00:12:15,920 it gives you an error. 222 00:12:16,240 --> 00:12:18,400 So I didn't really understand that part. 223 00:12:20,160 --> 00:12:21,360 So diagnostic codes. 224 00:12:21,500 --> 00:12:23,080 I said diagnostic codes are pretty interesting. 225 00:12:23,640 --> 00:12:25,180 Code 0 is kind of boring. 226 00:12:25,420 --> 00:12:26,440 It just is an echo. 227 00:12:28,220 --> 00:12:31,320 But code 1 and code 4, 228 00:12:31,840 --> 00:12:32,940 those are pretty interesting, 229 00:12:34,080 --> 00:12:35,220 because code 1, 230 00:12:35,300 --> 00:12:38,540 if you were to just continually broadcast diagnostic code 1 231 00:12:38,540 --> 00:12:40,200 at a ModBus network, 232 00:12:40,440 --> 00:12:40,860 what would happen? 233 00:12:41,860 --> 00:12:43,540 Communications get reset, and then what? 234 00:12:44,260 --> 00:12:45,800 You're DOSing the entire network. 235 00:12:46,000 --> 00:12:47,360 Built right into the protocol for you. 236 00:12:47,440 --> 00:12:48,740 Thank you very much, Modicon. 237 00:12:49,900 --> 00:12:52,360 The other thing you can do is use code 4, 238 00:12:52,560 --> 00:12:54,040 which is force listen-only mode. 239 00:12:54,600 --> 00:12:55,780 Well, why does that matter? 240 00:12:56,000 --> 00:12:57,500 Well, most of these, as I said, these control networks, 241 00:12:58,160 --> 00:13:00,840 they take data in and then make decisions 242 00:13:00,840 --> 00:13:02,900 and then use them to do stuff. 243 00:13:03,580 --> 00:13:05,340 Well, if you cut off the data feed, 244 00:13:05,760 --> 00:13:07,820 the SCADA control system goes, 245 00:13:07,920 --> 00:13:09,340 oh, my God, everything just crashed, 246 00:13:09,420 --> 00:13:10,700 and who knows what happens. 247 00:13:11,480 --> 00:13:13,360 So that's also a kind of interesting little, 248 00:13:13,360 --> 00:13:14,420 little tidbit that they added. 249 00:13:15,000 --> 00:13:15,980 And then, of course, you've got, 250 00:13:16,420 --> 00:13:19,220 they built in your obfuscation for you 251 00:13:19,220 --> 00:13:22,080 with clear counters and diagnostic registers 252 00:13:22,080 --> 00:13:25,140 and clear the overrun counters, 253 00:13:25,280 --> 00:13:27,440 so if you were banging against the bytes for a little while 254 00:13:27,440 --> 00:13:29,180 and you want to clear all the logs, 255 00:13:29,340 --> 00:13:30,880 that's built right into the protocol, no problems. 256 00:13:34,820 --> 00:13:36,680 So, now we know what packets look like. 257 00:13:36,760 --> 00:13:38,580 Let's talk about COM for a little bit, okay? 258 00:13:38,880 --> 00:13:41,340 COM for this protocol is, again, really simple. 259 00:13:41,940 --> 00:13:42,940 Basically, like I said, 260 00:13:42,940 --> 00:13:44,020 you've got a master and a slave. 261 00:13:44,620 --> 00:13:47,200 Masters are the only one that can start communications. 262 00:13:47,640 --> 00:13:49,380 Slaves cannot under any circumstances. 263 00:13:49,820 --> 00:13:50,880 Although some vendors, 264 00:13:51,040 --> 00:13:54,640 and the truth about Modbus is that 265 00:13:54,640 --> 00:13:56,420 some vendors implement things differently, 266 00:13:56,520 --> 00:13:57,760 so some have added that functionality, 267 00:13:58,040 --> 00:14:00,960 but in the spec, only masters can start the talk. 268 00:14:01,520 --> 00:14:04,380 So, basically, a master sends a request out to a slave, 269 00:14:04,540 --> 00:14:06,820 slave does something, slave responds. 270 00:14:07,300 --> 00:14:08,100 Pretty simple. 271 00:14:10,980 --> 00:14:12,520 So, what happens when, I said, 272 00:14:12,520 --> 00:14:14,480 errors are important for my tool, okay? 273 00:14:14,680 --> 00:14:16,140 So, what happens when errors occur? 274 00:14:16,360 --> 00:14:18,040 It looks just like the last one. 275 00:14:18,400 --> 00:14:20,400 The master sends out a request 276 00:14:20,400 --> 00:14:22,360 that's improperly formatted or something. 277 00:14:22,960 --> 00:14:24,900 The error is detected at the slave level. 278 00:14:25,420 --> 00:14:28,460 The slave reports an error back in a special format, 279 00:14:28,600 --> 00:14:29,660 which is a little bit different. 280 00:14:29,920 --> 00:14:32,300 So, you have the slave ID, you have this EFN, 281 00:14:32,960 --> 00:14:35,220 then you have the error code where the data should be, 282 00:14:35,300 --> 00:14:36,860 and then you have the error check. 283 00:14:37,480 --> 00:14:41,600 That EFN is just the function code plus 80 hex, 284 00:14:41,600 --> 00:14:46,440 which is why I said earlier that 128 through 255 were reserved 285 00:14:46,440 --> 00:14:49,940 because it's the error half of all the actual function codes 286 00:14:49,940 --> 00:14:50,800 that are being used. 287 00:14:51,820 --> 00:14:54,000 This basically becomes the foundation 288 00:14:54,000 --> 00:14:55,040 of how my scanner works. 289 00:14:56,900 --> 00:14:58,920 So, the error codes are defined in the spec. 290 00:14:59,120 --> 00:14:59,960 I'm not going to go over them. 291 00:15:00,060 --> 00:15:00,760 They don't really matter. 292 00:15:01,400 --> 00:15:04,100 The only one that matters that you'll see in the slides 293 00:15:04,100 --> 00:15:05,860 is error code three, which means illegal data value. 294 00:15:06,300 --> 00:15:10,000 So, okay, kind of recap real quick. 295 00:15:10,840 --> 00:15:11,500 Valid slave. 296 00:15:11,600 --> 00:15:13,180 Slaves one through 247. 297 00:15:13,800 --> 00:15:16,200 Okay, why they didn't run that all the way up to 255, 298 00:15:16,320 --> 00:15:17,700 I really have absolutely no idea. 299 00:15:17,820 --> 00:15:19,360 It fits in one byte, but whatever. 300 00:15:20,660 --> 00:15:22,640 The slave ID has to be unique on every bus 301 00:15:23,020 --> 00:15:24,800 because, remember, this is serial protocol, right? 302 00:15:24,900 --> 00:15:30,000 So, if you have two mod bus serial networks next to each other, 303 00:15:30,120 --> 00:15:31,860 they don't care what the other one's doing. 304 00:15:33,440 --> 00:15:36,560 But on the same bus, you can't have two devices with the same slave ID. 305 00:15:36,660 --> 00:15:39,520 Otherwise, oh, no, two devices are going to respond at the same time 306 00:15:39,520 --> 00:15:40,960 and you have a race condition and you don't know what's going to happen. 307 00:15:42,400 --> 00:15:46,460 So, masters, if you look back in that slide, 308 00:15:47,720 --> 00:15:50,240 the masters don't announce themselves in any way. 309 00:15:50,340 --> 00:15:51,380 They just send something on the wire. 310 00:15:51,520 --> 00:15:53,380 See, there's no, like, master ID in this packet. 311 00:15:53,520 --> 00:15:54,280 There's just slave ID. 312 00:15:54,620 --> 00:15:57,220 So, masters don't have to authenticate in any fashion. 313 00:15:58,680 --> 00:16:00,600 And you can only run one request at a time. 314 00:16:00,680 --> 00:16:02,820 It's really highly timing-based. 315 00:16:03,380 --> 00:16:07,300 The good news is that mod bus straight is not really run a whole lot. 316 00:16:07,400 --> 00:16:09,300 I've seen it in some systems, but it's not everywhere. 317 00:16:10,300 --> 00:16:11,240 But you could actually, 318 00:16:11,240 --> 00:16:13,540 if someone's running just straight mod bus, 319 00:16:13,640 --> 00:16:15,380 you can do really nasty things to them pretty easy. 320 00:16:15,500 --> 00:16:16,600 It's pretty bad. 321 00:16:19,060 --> 00:16:20,200 So, that's mod bus. 322 00:16:21,160 --> 00:16:22,460 My tool runs on my bus TCP, 323 00:16:23,340 --> 00:16:25,900 which I said is basically just a wrapper for mod bus. 324 00:16:26,460 --> 00:16:26,820 Okay? 325 00:16:28,920 --> 00:16:31,240 What they did is they had to modify the protocol a little bit 326 00:16:31,240 --> 00:16:33,320 because, like, things like checksum, why do we care? 327 00:16:33,440 --> 00:16:36,460 The TCP IP layer will take care of all the checksum for us. 328 00:16:36,540 --> 00:16:37,520 So, why are we going to add that? 329 00:16:38,780 --> 00:16:40,100 We introduced gateways. 330 00:16:40,100 --> 00:16:44,760 These become really important because a gateway basically takes a mod bus TCP connection, 331 00:16:45,100 --> 00:16:51,140 gives it one IP address, and then it'll allow you to attach a whole ring of slaves to that. 332 00:16:51,220 --> 00:16:55,500 So, you can put up to 247 slaves attached to one gateway, 333 00:16:55,680 --> 00:16:57,320 now hiding behind one IP address, 334 00:16:58,740 --> 00:17:00,860 which is how a lot of these systems are kind of put together. 335 00:17:01,660 --> 00:17:05,160 As I said before, port 502 is still the port for this, 336 00:17:05,240 --> 00:17:09,080 but basically everything else is completely unchanged in the protocol. 337 00:17:09,260 --> 00:17:10,080 The end of the day. 338 00:17:10,100 --> 00:17:11,120 The underlying packet is the same. 339 00:17:13,940 --> 00:17:17,400 So, a mod bus TCP architecture looks kind of like that last slide, 340 00:17:17,560 --> 00:17:18,620 but just a little bit different. 341 00:17:18,940 --> 00:17:22,220 You've got the corporate LAN, then you've got the firewall, hopefully, 342 00:17:23,040 --> 00:17:25,900 and then you have your control center, which has all your fun toys in it, 343 00:17:25,980 --> 00:17:28,600 and that's where you take all of your VPs and all that kind of stuff. 344 00:17:29,340 --> 00:17:31,780 And then, if you notice, we've got blue running over here. 345 00:17:32,040 --> 00:17:34,460 The blue is all the mod bus TCP connections, 346 00:17:34,680 --> 00:17:37,280 and that can run throughout the entire network over your regular Ethernet. 347 00:17:37,660 --> 00:17:39,700 And then, occasionally, you have your gateways 348 00:17:39,700 --> 00:17:42,520 that typically talk to, like, kind of more legacy devices, 349 00:17:42,780 --> 00:17:46,960 like things that are high capital, like generators and those kinds of things, transformers. 350 00:17:47,420 --> 00:17:49,240 They might only speak regular mod bus. 351 00:17:49,380 --> 00:17:53,180 You just kind of drop a gateway in there so that you can talk to them over the regular network. 352 00:17:56,060 --> 00:17:57,940 You're seeing from the device vendor standpoint 353 00:17:58,660 --> 00:18:03,360 that a lot of vendors are adding mod bus TCP directly into their products. 354 00:18:03,720 --> 00:18:07,060 So, most of this stuff is going away. 355 00:18:07,620 --> 00:18:09,680 But it's definitely still out there, because, like I said, 356 00:18:09,680 --> 00:18:11,000 they're high capital investment items. 357 00:18:11,120 --> 00:18:12,280 They don't turn over very fast. 358 00:18:15,600 --> 00:18:17,460 So, this is a mod bus TCP packet. 359 00:18:18,780 --> 00:18:20,920 It's broken up, again, really into two major sections. 360 00:18:21,360 --> 00:18:23,700 There's the PDU, which is the thing from last time. 361 00:18:23,780 --> 00:18:25,800 Remember, I said that's the only thing that you have to have for mod bus? 362 00:18:26,000 --> 00:18:27,760 Well, that's unchanged, okay? 363 00:18:27,960 --> 00:18:30,540 And then, you have this thing, which is the MBAP, 364 00:18:30,640 --> 00:18:36,060 with the mod bus application protocol handler, which has all of your routing information. 365 00:18:36,340 --> 00:18:39,600 You have a transaction ID and a protocol ID, which, for some reason, 366 00:18:39,680 --> 00:18:40,420 is always zero. 367 00:18:41,600 --> 00:18:42,000 Always. 368 00:18:42,180 --> 00:18:43,900 You never change them for any reason. 369 00:18:43,960 --> 00:18:45,740 I don't understand why they're there, but... 370 00:18:46,580 --> 00:18:46,980 Okay. 371 00:18:47,100 --> 00:18:49,020 So, you just put in zeros for the first couple bits. 372 00:18:49,220 --> 00:18:49,420 Yeah? 373 00:18:50,120 --> 00:18:50,740 Oh, good. 374 00:18:52,100 --> 00:18:52,500 Good. 375 00:19:05,660 --> 00:19:05,820 Okay. 376 00:19:06,000 --> 00:19:06,960 That guy knows. 377 00:19:07,880 --> 00:19:08,760 Thank you, that guy. 378 00:19:09,220 --> 00:19:09,660 Appreciate it. 379 00:19:11,300 --> 00:19:12,660 Yeah, you get a beer, too. 380 00:19:15,780 --> 00:19:16,700 Oh, sorry. 381 00:19:16,860 --> 00:19:17,360 Yeah, my bad. 382 00:19:17,940 --> 00:19:21,380 What he said was that, originally, they were going to have multiple protocols 383 00:19:21,380 --> 00:19:24,340 that were going to be attached to this, so that's why the protocol ID was there, 384 00:19:24,460 --> 00:19:27,940 but that they only ever developed one protocol, and so that's why it's always zero. 385 00:19:28,020 --> 00:19:29,000 Did I surmise appropriately? 386 00:19:29,840 --> 00:19:30,460 Close enough? 387 00:19:30,600 --> 00:19:31,080 Okay, good. 388 00:19:31,820 --> 00:19:35,920 So, anyway, more importantly, again, still big Indian encoding. 389 00:19:36,920 --> 00:19:37,780 PDU is the same. 390 00:19:38,960 --> 00:19:42,160 But you have this kind of additional header here with a length. 391 00:19:42,380 --> 00:19:45,960 And notice that the slave ID is, again, in the extra addressing. 392 00:19:48,440 --> 00:19:50,320 So, that's what it looks like conceptually. 393 00:19:52,040 --> 00:19:57,180 Let's look at an actual request response going on from something on a packet capture I did a while ago. 394 00:19:57,800 --> 00:20:00,780 So, if you look at the request, this is obviously straight out of Wireshark. 395 00:20:01,880 --> 00:20:06,580 Wireshark will decode this into, like, more readable format, but I prefer to read it in the hex because I'm a geek. 396 00:20:07,940 --> 00:20:11,960 So, you've got your transaction ID, protocol ID, your length, which you actually have to calculate. 397 00:20:12,160 --> 00:20:13,040 It's not that hard. 398 00:20:13,380 --> 00:20:15,760 Your slave ID, and then your function. 399 00:20:16,280 --> 00:20:22,540 In this particular case, I ran diagnostic code zero, which is echo, 400 00:20:22,700 --> 00:20:26,140 and then 0535, which is leet and hex, in case anyone doesn't know. 401 00:20:27,580 --> 00:20:33,740 The request gets sent out by the master, and then a response looks like this. 402 00:20:34,380 --> 00:20:36,120 Again, you have 0000. 403 00:20:36,380 --> 00:20:37,400 You get your length. 404 00:20:37,940 --> 00:20:39,440 Your slave ID gets returned. 405 00:20:40,460 --> 00:20:46,280 Then notice, since this was a valid response, the function code gets returned as the one that was sent in. 406 00:20:46,360 --> 00:20:46,720 That's important. 407 00:20:47,380 --> 00:20:51,080 And then, since this was diagnostic, you get diagnostic code and data. 408 00:20:52,580 --> 00:20:56,260 So, the important part of this is that it looks exactly like the request, right? 409 00:20:56,300 --> 00:21:01,200 Because that function code is the only thing that will let you know if there was any type of error or that it came from the slave. 410 00:21:04,260 --> 00:21:07,120 So, what happens when we do something bad, right? 411 00:21:07,220 --> 00:21:07,760 That's what we're going to do. 412 00:21:07,780 --> 00:21:08,780 That's what we're all interested in, anyway. 413 00:21:10,680 --> 00:21:12,100 All this stuff stays the same. 414 00:21:12,600 --> 00:21:14,620 Your function, now this is the request. 415 00:21:14,840 --> 00:21:19,800 So, the function code, we put in a valid function code, but with a diagnostic code of FF, which is not in the spec. 416 00:21:20,440 --> 00:21:20,760 Okay? 417 00:21:20,840 --> 00:21:22,700 So, we're going to send it something that it doesn't expect. 418 00:21:25,220 --> 00:21:29,880 What happens is, is you get your transaction ID, protocol ID, length, slave ID, et cetera. 419 00:21:30,320 --> 00:21:33,720 But if you look at the function code, and that's actually kind of hard to read on that screen. 420 00:21:34,080 --> 00:21:34,400 Sorry. 421 00:21:34,880 --> 00:21:36,500 That's 88 there, right? 422 00:21:36,940 --> 00:21:37,640 So, we got that. 423 00:21:37,640 --> 00:21:41,200 We got that function code back, and it had 80 hex added to it. 424 00:21:41,480 --> 00:21:43,520 And that's really the key to how this all works. 425 00:21:43,640 --> 00:21:50,720 Because if you send a request with an invalid slave ID, it errors or ignores you, right? 426 00:21:51,560 --> 00:21:59,120 So, if your flow is this, if you either error or get it error or ignored when there's an improper ID, 427 00:21:59,560 --> 00:22:02,480 but when the proper ID is sent, you get that function code back. 428 00:22:02,700 --> 00:22:06,840 Now, we've got a binary switch where we can say, okay, well, if we meet these conditions, 429 00:22:06,840 --> 00:22:08,000 it's not the right one. 430 00:22:08,120 --> 00:22:09,900 If we meet this condition, it's the right one. 431 00:22:10,060 --> 00:22:13,180 And now we have a fundamental basis for how to write do mapping. 432 00:22:18,080 --> 00:22:20,860 So, back to my tool, enough with the boring lesson. 433 00:22:22,580 --> 00:22:27,720 Basically, mod scan will, the only required parameter for it is an IP range. 434 00:22:28,260 --> 00:22:32,580 It'll scan the IP range on whatever port you ask it to, although it defaults to 502. 435 00:22:33,220 --> 00:22:35,540 You can run ModBus on another port if you want to. 436 00:22:35,640 --> 00:22:36,240 It's your choice. 437 00:22:37,420 --> 00:22:40,060 When it finds an open port, it's pretty simple. 438 00:22:40,140 --> 00:22:41,660 It just does a slave ID brute force. 439 00:22:42,800 --> 00:22:47,420 It's not very elegant, but it gets the job done, and it's surprisingly faster than you might think it would be. 440 00:22:48,660 --> 00:22:51,560 By default, it stops at the first slave ID that it finds. 441 00:22:52,640 --> 00:22:55,360 That's just because I chose to do it that way to be more efficient. 442 00:22:55,520 --> 00:22:58,880 But again, for gateways, that doesn't really fly, and we'll take a look at that in a little bit. 443 00:22:59,680 --> 00:23:04,320 And it outputs in just a IP colon port tab slave ID format, 444 00:23:04,940 --> 00:23:06,340 because that's what I felt like. 445 00:23:07,840 --> 00:23:12,480 I intend on adding actual CSVs or something like that to it, 446 00:23:12,560 --> 00:23:14,760 but I am just way too lazy right now to get that done. 447 00:23:17,080 --> 00:23:19,520 So, you get a whole bunch of switches and options on the tool. 448 00:23:19,820 --> 00:23:23,880 Again, it's written in Python, but Python has a nice little options handler. 449 00:23:24,060 --> 00:23:24,380 Beautiful. 450 00:23:25,540 --> 00:23:27,420 You can set the port, the timeout. 451 00:23:28,480 --> 00:23:30,700 A, this is important, the aggressive mode. 452 00:23:31,320 --> 00:23:34,620 I said before that default mode is it stops at the first slave ID. 453 00:23:35,080 --> 00:23:36,380 Well, in aggressive mode, 454 00:23:36,840 --> 00:23:38,740 it enumerates the entire address space every time. 455 00:23:39,320 --> 00:23:42,000 So, if it finds the slave ID, it just assumes that it's at a gateway 456 00:23:42,000 --> 00:23:43,380 because there's no other way of detecting it, 457 00:23:43,460 --> 00:23:44,960 and just runs through the entire list. 458 00:23:45,940 --> 00:23:47,740 So, if you're dealing with a place with gateways, 459 00:23:47,860 --> 00:23:49,640 you have to pretty much use the aggressive mode. 460 00:23:50,840 --> 00:23:51,960 You can change the function. 461 00:23:52,540 --> 00:23:53,480 Why is that important? 462 00:23:54,340 --> 00:23:55,500 Because, like I said before, 463 00:23:55,660 --> 00:24:01,000 Modbus is kind of like every vendor does their own little special implementations 464 00:24:01,000 --> 00:24:02,080 and that kind of stuff with Modbus. 465 00:24:03,800 --> 00:24:06,360 So, function code 17, which is what I default to, 466 00:24:06,840 --> 00:24:07,560 report slave ID 1, 467 00:24:07,720 --> 00:24:10,240 might not actually be implemented on particular devices. 468 00:24:10,740 --> 00:24:11,400 It's possible. 469 00:24:11,920 --> 00:24:14,600 So, maybe you want to use read to streets or read coils 470 00:24:14,600 --> 00:24:17,460 or a diagnostic code to do your scanning. 471 00:24:17,880 --> 00:24:19,740 The principle of the tool will still work. 472 00:24:19,900 --> 00:24:22,180 If it's the wrong slave ID, it'll come back with an error. 473 00:24:22,560 --> 00:24:24,680 So, you can really use any function code. 474 00:24:24,800 --> 00:24:26,880 You could even use a write function code if you wanted to, 475 00:24:27,400 --> 00:24:29,580 and it would come back and report all the slave IDs. 476 00:24:29,860 --> 00:24:32,720 Although, you probably just made a real big mess in someone's skating network, 477 00:24:32,900 --> 00:24:34,420 but what you do with my tool is your own business. 478 00:24:35,120 --> 00:24:35,520 Okay? 479 00:24:36,980 --> 00:24:39,820 If you're using the F, you've got to use D 480 00:24:39,820 --> 00:24:43,280 because now, in order to send a properly formatted packet, 481 00:24:44,020 --> 00:24:47,100 you have to have the right data to back up the function code that you sent. 482 00:24:47,640 --> 00:24:49,820 So, basically, if you're going to switch the function, 483 00:24:50,280 --> 00:24:54,020 you better read the Modbus spec because you're not going to get it right. 484 00:24:56,060 --> 00:24:59,780 It's not that hard, but the tool doesn't have a nice way of handling all that. 485 00:24:59,860 --> 00:25:02,360 I was not running out to make a Modbus client tool. 486 00:25:02,660 --> 00:25:05,780 This is a scanner, and it's pretty basic. 487 00:25:06,840 --> 00:25:08,640 And, of course, we've got verbose and debug modes, 488 00:25:09,760 --> 00:25:10,840 which are mainly for me. 489 00:25:14,180 --> 00:25:15,180 So, demo time. 490 00:25:16,600 --> 00:25:17,460 Put all this down. 491 00:25:24,360 --> 00:25:25,740 So, as I said before, 492 00:25:28,540 --> 00:25:32,320 I have a little skating network that I put together in Ubuntu Virtual Machine 493 00:25:32,320 --> 00:25:35,640 that we can run for the demo because hardware was just impractical. 494 00:25:37,180 --> 00:25:41,180 This actually uses a tool I have kind of running in the background 495 00:25:41,180 --> 00:25:44,340 called Modsac by a company called WingPath out in the UK. 496 00:25:45,020 --> 00:25:46,100 So, give them some props. 497 00:25:46,280 --> 00:25:47,200 Their tool is actually pretty nice, 498 00:25:47,800 --> 00:25:49,100 and I'm using it for their demo, 499 00:25:49,160 --> 00:25:52,180 and they were nice enough to help me out when this stopped working like yesterday. 500 00:25:54,320 --> 00:25:54,680 Oops. 501 00:25:55,460 --> 00:25:58,020 And let me just make sure that my packet capture is running. 502 00:26:00,200 --> 00:26:00,560 Okay. 503 00:26:00,680 --> 00:26:01,200 We're good to go. 504 00:26:04,900 --> 00:26:06,080 So, let's take a look. 505 00:26:06,380 --> 00:26:06,740 Okay. 506 00:26:06,840 --> 00:26:06,920 Let's run the tool. 507 00:26:08,660 --> 00:26:09,600 Let's find my mouse. 508 00:26:10,280 --> 00:26:10,620 Okay. 509 00:26:13,380 --> 00:26:15,580 So, to run the tool, there you go. 510 00:26:15,680 --> 00:26:16,700 Wow, that was awesome. 511 00:26:17,100 --> 00:26:17,460 Right? 512 00:26:17,580 --> 00:26:19,900 If you run it with no options, it gives you all the options, 513 00:26:19,960 --> 00:26:22,100 just like every other tool in the world is supposed to do. 514 00:26:22,200 --> 00:26:23,840 Well, it works just like you expect it to. 515 00:26:24,940 --> 00:26:26,620 That's the same option list as we saw before, 516 00:26:26,820 --> 00:26:27,900 so I'm not going to go over it. 517 00:26:29,840 --> 00:26:32,060 Modscanning, its most basic interpretation, 518 00:26:32,660 --> 00:26:34,580 is you want to run it against a single host. 519 00:26:35,560 --> 00:26:36,280 So, let's... 520 00:26:36,840 --> 00:26:39,080 Give that a try, and hopefully everything will work. 521 00:26:47,740 --> 00:26:48,420 Yay, it worked. 522 00:26:49,180 --> 00:26:49,560 Okay. 523 00:26:49,640 --> 00:26:51,880 So, when you're running against a single one host... 524 00:26:52,540 --> 00:26:54,020 I don't know if I can scroll that. 525 00:26:56,240 --> 00:26:58,160 There, if you can't see it, now you'll probably be able to. 526 00:26:58,440 --> 00:26:59,560 Can everyone read that? 527 00:26:59,660 --> 00:27:00,600 Is that big enough font? 528 00:27:01,020 --> 00:27:01,400 Okay. 529 00:27:02,320 --> 00:27:04,300 So, basically, if you're running against one host, 530 00:27:04,380 --> 00:27:05,400 it's not particularly interesting. 531 00:27:05,660 --> 00:27:06,560 It just starts... 532 00:27:06,560 --> 00:27:07,020 It starts the scan, 533 00:27:07,640 --> 00:27:09,880 and then when it finds the valid slave ID, 534 00:27:10,040 --> 00:27:12,760 it tells you the IP address and the slave ID associated with it. 535 00:27:13,220 --> 00:27:15,280 Like I said, it's just a pretty basic mapping tool. 536 00:27:15,380 --> 00:27:16,420 That's really all I was going for. 537 00:27:17,640 --> 00:27:20,100 Running against one host is pretty uninteresting. 538 00:27:21,520 --> 00:27:24,260 So, we can run it against multiple hosts... 539 00:27:27,640 --> 00:27:28,840 using slash notation. 540 00:27:31,480 --> 00:27:33,860 Now, this is going to take about 30 seconds to run. 541 00:27:34,260 --> 00:27:35,580 I'm doing a slash 26, 542 00:27:35,800 --> 00:27:36,500 because it takes... 543 00:27:37,520 --> 00:27:38,880 On average, in my testing, 544 00:27:38,980 --> 00:27:40,600 it takes about two minutes and nine seconds 545 00:27:40,600 --> 00:27:41,640 to do a whole class D 546 00:27:41,640 --> 00:27:42,940 almost exactly every time, 547 00:27:43,020 --> 00:27:43,700 which is actually kind of cool. 548 00:27:44,680 --> 00:27:47,500 But that's a lot slower than I want to do for this talk. 549 00:27:48,480 --> 00:27:51,020 But it can do any IP address range you want to give it. 550 00:27:51,420 --> 00:27:52,340 It'll just suck it up. 551 00:27:53,620 --> 00:27:55,500 So, as you can see, we're getting some results here. 552 00:27:56,080 --> 00:27:57,240 The first one we already saw, 553 00:27:57,840 --> 00:27:59,320 .21 has a slave ID of 8. 554 00:28:00,140 --> 00:28:02,020 But there's other devices sitting on this network. 555 00:28:02,540 --> 00:28:04,900 We've got one at .22 with a slave ID of 30, 556 00:28:05,080 --> 00:28:06,540 .25, 7, etc., etc. 557 00:28:06,560 --> 00:28:07,620 You can read the slide. 558 00:28:07,800 --> 00:28:08,520 Maybe you can't. 559 00:28:09,080 --> 00:28:09,640 There you go. 560 00:28:10,620 --> 00:28:12,200 And then it tells you when the scan's done, 561 00:28:13,120 --> 00:28:14,380 which originally wasn't in there, 562 00:28:14,440 --> 00:28:15,820 but someone told me that, 563 00:28:15,860 --> 00:28:17,180 hey, I don't know when it's finished. 564 00:28:17,420 --> 00:28:18,120 I was like, oh, okay. 565 00:28:18,220 --> 00:28:18,760 That's a good point. 566 00:28:19,780 --> 00:28:22,440 So, pretty basic when you're running it with no options. 567 00:28:22,600 --> 00:28:24,280 It just will sit there and scan the whole network 568 00:28:24,280 --> 00:28:25,340 and map everything out for you. 569 00:28:26,220 --> 00:28:31,040 Now, I said before that it has two basic modes operations, 570 00:28:31,460 --> 00:28:33,000 the aggressive mode and standard mode. 571 00:28:34,600 --> 00:28:36,100 Well, as you can see here, 572 00:28:36,780 --> 00:28:41,920 on port IP25, we only have one slave ID come back, right? 573 00:28:43,780 --> 00:28:46,240 Well, if we run this entire network again, 574 00:28:46,440 --> 00:28:48,040 but do it in aggressive mode, 575 00:28:51,400 --> 00:28:53,380 oops, oh, okay, I just did one IP there. 576 00:28:53,460 --> 00:28:53,720 Never mind. 577 00:28:54,080 --> 00:28:55,280 I changed my demo, sorry. 578 00:28:56,280 --> 00:28:59,080 If we run against the number 25, 579 00:28:59,260 --> 00:29:01,980 we actually see that there's four devices sitting behind that. 580 00:29:02,240 --> 00:29:04,200 So, that's obviously a gateway, right? 581 00:29:04,360 --> 00:29:06,400 And that's really the difference between the two modes, 582 00:29:06,560 --> 00:29:07,560 is that this one went through 583 00:29:07,560 --> 00:29:09,460 and then tried to enumerate every single slave ID, 584 00:29:09,620 --> 00:29:11,240 whereas the other one stopped at the first one. 585 00:29:14,040 --> 00:29:16,080 So, if you want to see it actually working, 586 00:29:16,740 --> 00:29:17,620 which I don't know about you, 587 00:29:17,680 --> 00:29:20,400 but I hate tools that just sit there and like starting 588 00:29:21,300 --> 00:29:22,840 and gives you absolutely no status. 589 00:29:23,040 --> 00:29:24,580 Well, sometimes you're going to have to do that. 590 00:29:24,660 --> 00:29:26,760 There's not a lot you can do with a command line tool. 591 00:29:29,080 --> 00:29:30,300 But if you want to see it working, 592 00:29:30,440 --> 00:29:31,500 you just put it in verbose mode. 593 00:29:35,520 --> 00:29:36,400 And it'll sit there, 594 00:29:36,400 --> 00:29:38,880 and tell you, well, it failed to connect to all these IPs 595 00:29:38,880 --> 00:29:40,700 on port 502, et cetera, et cetera, et cetera. 596 00:29:40,760 --> 00:29:41,680 And it'll sit there and run out. 597 00:29:42,080 --> 00:29:43,520 And you'll see in a couple seconds here 598 00:29:43,520 --> 00:29:44,780 that it'll come up on one that's valid. 599 00:29:45,520 --> 00:29:47,200 And it gives you the same output as before. 600 00:29:47,420 --> 00:29:49,260 Just verbose mode gives you this extra information. 601 00:29:56,000 --> 00:29:57,200 So, I mentioned, too, 602 00:29:57,260 --> 00:29:59,360 that my tool can support multiple function codes. 603 00:29:59,740 --> 00:30:00,900 And that that can be important 604 00:30:00,900 --> 00:30:04,260 because not every vendor supports the same stuff, right? 605 00:30:04,700 --> 00:30:05,980 Well, here's an example of that. 606 00:30:07,000 --> 00:30:08,120 So, we're going to do this. 607 00:30:08,220 --> 00:30:09,460 We're going to do function code. 608 00:30:12,660 --> 00:30:13,920 We're going to do function code four. 609 00:30:14,300 --> 00:30:16,500 So, PY. 610 00:30:17,640 --> 00:30:20,620 You just do an F4, okay? 611 00:30:21,900 --> 00:30:24,320 But like I said before, we have to push in our own data 612 00:30:24,320 --> 00:30:26,000 because now the data that's in there by default 613 00:30:26,000 --> 00:30:26,840 won't work anymore. 614 00:30:27,540 --> 00:30:31,700 So, the data has to be entered in Unicode escaped hex. 615 00:30:32,400 --> 00:30:33,540 So, we do this. 616 00:30:42,300 --> 00:30:43,320 No, I don't. 617 00:30:43,320 --> 00:30:43,740 Thank you. 618 00:30:44,260 --> 00:30:46,680 This is why I should just use the copy and paste, 619 00:30:46,760 --> 00:30:47,720 but I kind of wanted to explain this. 620 00:30:49,380 --> 00:30:49,920 That's better. 621 00:30:50,160 --> 00:30:50,680 Thank you. 622 00:30:50,800 --> 00:30:51,060 Thank you. 623 00:30:53,260 --> 00:30:54,660 So, 0, 0, 0, 0. 624 00:30:55,300 --> 00:30:56,480 Did I do my X yet? 625 00:30:56,540 --> 00:30:56,700 Okay. 626 00:30:58,040 --> 00:31:00,020 0, 0, 0, 1. 627 00:31:00,220 --> 00:31:00,540 Okay. 628 00:31:00,860 --> 00:31:01,920 Well, what does that mean? 629 00:31:02,160 --> 00:31:02,440 Okay. 630 00:31:02,460 --> 00:31:03,680 Well, so, for reading registers, 631 00:31:04,480 --> 00:31:05,680 basically what you do is you say, 632 00:31:05,760 --> 00:31:06,280 the first, 633 00:31:06,400 --> 00:31:08,800 the first two bytes there are what address 634 00:31:08,800 --> 00:31:09,680 do we want to start at? 635 00:31:09,760 --> 00:31:10,680 In this case, address 0. 636 00:31:11,160 --> 00:31:12,800 And then the next number is 637 00:31:12,800 --> 00:31:14,240 how many addresses do you want to read? 638 00:31:14,540 --> 00:31:15,420 1, okay? 639 00:31:15,800 --> 00:31:17,120 Nothing too special about that. 640 00:31:17,760 --> 00:31:19,680 And then we're going to just scan one of the devices. 641 00:31:21,700 --> 00:31:22,980 16, 2, 3, 1. 642 00:31:25,840 --> 00:31:26,240 21. 643 00:31:29,740 --> 00:31:30,140 Okay. 644 00:31:30,560 --> 00:31:31,760 I'm going to copy and paste it. 645 00:31:36,400 --> 00:31:37,280 As you can see, though, 646 00:31:37,300 --> 00:31:39,040 you have to know a little bit something about the protocol 647 00:31:39,040 --> 00:31:41,980 and actually to use the function code spec. 648 00:31:42,240 --> 00:31:43,580 Otherwise, it won't work right. 649 00:31:43,960 --> 00:31:46,340 And look, it looks just like the very first thing we did, 650 00:31:46,400 --> 00:31:49,220 except for if we come over here to, 651 00:31:50,700 --> 00:31:52,200 did I lose my Skeeter? 652 00:31:52,740 --> 00:31:53,400 Oh, there it is. 653 00:31:54,400 --> 00:31:56,620 Come over here and look at the function codes that we're running. 654 00:31:57,740 --> 00:32:01,140 We can actually pull up the query, maybe. 655 00:32:02,000 --> 00:32:02,360 Maybe. 656 00:32:08,220 --> 00:32:09,640 I can't read that from over here, 657 00:32:09,720 --> 00:32:11,460 but that should say that it's running on function code 4, 658 00:32:11,580 --> 00:32:13,120 but I can't actually see it from over here. 659 00:32:13,320 --> 00:32:15,660 So I lose for being the presenter 660 00:32:15,660 --> 00:32:16,740 and not being able to see the screen. 661 00:32:18,380 --> 00:32:19,520 But it should have come up 662 00:32:19,520 --> 00:32:21,000 and actually showed in the packet capture 663 00:32:21,000 --> 00:32:22,800 that, yeah, we were really using a different function code. 664 00:32:22,960 --> 00:32:24,740 So just basically, yes, it works. 665 00:32:25,600 --> 00:32:28,220 So, okay, function code 4 is not really all that interesting. 666 00:32:29,460 --> 00:32:31,920 But what happens if we use that diagnostic code, 667 00:32:32,000 --> 00:32:32,600 function code 8? 668 00:32:36,640 --> 00:32:37,780 And do this instead. 669 00:32:44,220 --> 00:32:46,160 Now, instead of just trying to read registers, 670 00:32:46,300 --> 00:32:47,840 it just went through and did echo data back. 671 00:32:49,060 --> 00:32:51,540 Well, something that's particularly interesting about function code 8 672 00:32:51,540 --> 00:32:54,280 is that you can do stuff like the reset communications and stuff, 673 00:32:54,420 --> 00:32:55,940 and I basically just could have DOSed that 674 00:32:55,940 --> 00:32:57,960 as opposed to doing what I was doing. 675 00:32:58,160 --> 00:32:59,940 Which I'm not going to do because it'll break my demo 676 00:32:59,940 --> 00:33:01,340 and then I won't be able to do the next thing. 677 00:33:01,980 --> 00:33:04,520 But as you can see, you can kind of use it as a client as well, 678 00:33:04,600 --> 00:33:06,180 which is just a nice little feature. 679 00:33:06,180 --> 00:33:06,680 That I added. 680 00:33:08,640 --> 00:33:10,780 If you are trying to use it as a client, though, 681 00:33:11,520 --> 00:33:13,680 your debug mode becomes valuable 682 00:33:15,620 --> 00:33:17,840 because when you run it with debug mode enabled, 683 00:33:19,400 --> 00:33:20,680 you actually get all the, 684 00:33:20,800 --> 00:33:23,600 it just prints out all the actual raw packets that got back. 685 00:33:23,800 --> 00:33:24,920 So you can actually go through and read 686 00:33:24,920 --> 00:33:26,900 and see what's going on and do all that kind of stuff. 687 00:33:27,140 --> 00:33:28,320 Which, if you're doing testing, 688 00:33:28,420 --> 00:33:29,820 is actually really, really useful. 689 00:33:30,440 --> 00:33:32,300 But not something that absolutely everyone's 690 00:33:32,300 --> 00:33:33,260 going to want to use the tool with. 691 00:33:33,860 --> 00:33:34,220 So. 692 00:33:35,000 --> 00:33:35,360 So. 693 00:33:40,380 --> 00:33:44,680 No, because remember, the tool's objective is to figure that out for you. 694 00:33:45,400 --> 00:33:47,520 And I did not feel like writing a full-fledged client. 695 00:33:47,800 --> 00:33:50,240 Would it be easy to modify the code to make that happen? 696 00:33:50,360 --> 00:33:50,660 Yeah. 697 00:33:51,480 --> 00:33:54,420 But, you know, that's not what I was going for with the thing. 698 00:33:55,000 --> 00:33:56,500 Any other questions about, like, the tool? 699 00:33:56,580 --> 00:33:57,580 Because I'm pretty much done with the demo part. 700 00:33:58,640 --> 00:33:59,000 Okay. 701 00:33:59,120 --> 00:33:59,300 Okay. 702 00:34:02,440 --> 00:34:04,340 So if I can find my presentation again. 703 00:34:13,040 --> 00:34:15,900 So, the thing that probably some of you were sitting here waiting for, 704 00:34:16,040 --> 00:34:16,920 where does the code live? 705 00:34:17,640 --> 00:34:20,300 It's going to live at modscan.googlecode.com 706 00:34:20,300 --> 00:34:23,640 as soon as I, like I said, get around to uploading it and on a Wi-Fi. 707 00:34:23,720 --> 00:34:24,960 Probably want to get back to Caesars 708 00:34:24,960 --> 00:34:26,800 because I'm not using the Internet here. 709 00:34:27,520 --> 00:34:28,700 This is my good laptop. 710 00:34:28,880 --> 00:34:29,080 No. 711 00:34:29,760 --> 00:34:30,120 Yeah. 712 00:34:31,740 --> 00:34:34,780 So, like I said, I kind of developed the tool 713 00:34:34,780 --> 00:34:35,920 to kind of be in a network. 714 00:34:35,920 --> 00:34:37,260 Work in enumeration, scanning tool. 715 00:34:37,720 --> 00:34:39,680 Yes, there's lots of other things you could do with it. 716 00:34:40,580 --> 00:34:41,940 That's your business, like I said. 717 00:34:43,180 --> 00:34:46,060 It also would actually be useful for doing IDS testing. 718 00:34:46,400 --> 00:34:51,260 There actually are Modbus IDS, like, snort signatures out there. 719 00:34:51,620 --> 00:34:53,100 And I haven't actually done this, 720 00:34:53,160 --> 00:34:55,080 but it would be kind of interesting to run my tool against those 721 00:34:55,080 --> 00:34:56,740 and kind of see what happens. 722 00:34:58,040 --> 00:34:59,880 Another thing that you can use it for is asset management. 723 00:35:00,280 --> 00:35:02,700 Too many of my clients tell me that they don't really know 724 00:35:02,700 --> 00:35:04,460 all the gear they have and where it's all addressed 725 00:35:04,460 --> 00:35:05,380 and all that kind of stuff. 726 00:35:05,920 --> 00:35:07,560 Which, frankly, scares me a little bit 727 00:35:07,560 --> 00:35:09,060 based on who those clients are. 728 00:35:09,460 --> 00:35:10,940 So, whatever. 729 00:35:12,400 --> 00:35:14,380 You could also use it as a really cheap way 730 00:35:14,380 --> 00:35:15,940 of issuing a whole lot of bulk commands 731 00:35:15,940 --> 00:35:17,800 in a very, very noisy way. 732 00:35:19,820 --> 00:35:21,860 So, what are the problems with ModScan? 733 00:35:22,040 --> 00:35:24,520 Like I said, this is a really, like, alpha product. 734 00:35:25,400 --> 00:35:28,340 This is the first thing I ever wrote in Python in my entire life. 735 00:35:28,780 --> 00:35:30,780 So, from a code perspective, it sucks. 736 00:35:31,360 --> 00:35:33,460 It's also the first scanner I ever wrote. 737 00:35:33,640 --> 00:35:35,740 So, from a scanning perspective, it sucks. 738 00:35:35,740 --> 00:35:36,940 It sucks probably pretty bad, too. 739 00:35:37,040 --> 00:35:38,200 It's not very effective. 740 00:35:39,940 --> 00:35:42,340 The port scanning parts of it are really noisy 741 00:35:42,340 --> 00:35:43,180 and not really efficient. 742 00:35:43,580 --> 00:35:45,640 Root forcing, I can't think of a better way to do it. 743 00:35:45,980 --> 00:35:48,460 Maybe one of you has a better theory on this, 744 00:35:48,560 --> 00:35:51,060 but it's the only way I can think of enumerating slave IDs. 745 00:35:52,600 --> 00:35:54,560 Another thing, too, is it doesn't interpret those error codes. 746 00:35:54,900 --> 00:35:57,440 There are some conditions where, for example, 747 00:35:57,520 --> 00:36:02,380 if you're trying to read coil zero, right, 748 00:36:02,460 --> 00:36:03,580 and there is no coil zero, 749 00:36:03,680 --> 00:36:05,540 it's a safe assumption that coil zero exists. 750 00:36:05,740 --> 00:36:07,560 But if there is no coil zero, 751 00:36:07,740 --> 00:36:09,820 you'll get an error code in that error message 752 00:36:09,820 --> 00:36:12,300 that'll tell you that, yes, you actually addressed me properly, 753 00:36:12,500 --> 00:36:14,440 but I don't have that particular device. 754 00:36:16,020 --> 00:36:18,300 So, if we was actually interpreting the error codes, 755 00:36:18,460 --> 00:36:22,200 I could get a lot more, I could lower my false negative rate, 756 00:36:22,740 --> 00:36:24,440 which is something I'd like to do in the future 757 00:36:26,180 --> 00:36:28,120 in my enhancements, which is, like I said, 758 00:36:28,180 --> 00:36:28,980 interpret the error codes. 759 00:36:29,340 --> 00:36:31,420 I'd like to implement it in something like SCAPI. 760 00:36:31,720 --> 00:36:33,960 Is anyone in the room familiar with SCAPI? 761 00:36:34,720 --> 00:36:35,120 Anyone? 762 00:36:35,200 --> 00:36:35,320 Anyone? 763 00:36:35,320 --> 00:36:37,000 Can you give me some good documentation for SCAPI? 764 00:36:38,100 --> 00:36:38,940 Come see me. 765 00:36:39,080 --> 00:36:40,020 Oh, it's you. 766 00:36:40,140 --> 00:36:40,340 Okay. 767 00:36:40,820 --> 00:36:41,260 Yes. 768 00:36:41,600 --> 00:36:42,980 Talk to me later. 769 00:36:43,580 --> 00:36:46,280 Because I was not able to find any good documentation 770 00:36:46,280 --> 00:36:48,320 nor figure it out in less than 10 minutes, 771 00:36:48,440 --> 00:36:49,900 and therefore that's about my attention span. 772 00:36:50,680 --> 00:36:52,120 So, yeah, go ahead. 773 00:36:54,220 --> 00:36:56,160 I can't hear a word you're saying. 774 00:37:00,680 --> 00:37:02,220 Funny you should mention that. 775 00:37:02,520 --> 00:37:04,960 The con gods decided that, 776 00:37:05,320 --> 00:37:07,260 Fyodor would sit next to me at lunch on Wednesday 777 00:37:07,900 --> 00:37:11,460 and told me all about the fact that he now lets you write plug-ins for NMAP. 778 00:37:11,880 --> 00:37:14,000 So, that's actually one of the things I might do in the future 779 00:37:14,000 --> 00:37:17,520 is implement an NMAP plug-in for, basically, 780 00:37:17,580 --> 00:37:18,660 that's a copy of ModScan. 781 00:37:20,300 --> 00:37:21,960 I don't want to just do it as a plug-in 782 00:37:21,960 --> 00:37:23,060 because the next thing I want to do 783 00:37:23,060 --> 00:37:24,400 is actually add different protocol support. 784 00:37:25,100 --> 00:37:26,520 So, instead of just doing ModBus, 785 00:37:26,520 --> 00:37:28,820 do, like, DNP3 or some of the other SCADA protocols 786 00:37:28,820 --> 00:37:30,880 and kind of make it a little bit more of a robust scanner. 787 00:37:31,360 --> 00:37:33,760 Again, like, the base code for this tool, 788 00:37:33,880 --> 00:37:34,860 I wrote it in, like, four hours. 789 00:37:35,320 --> 00:37:36,860 So, it's pretty basic. 790 00:37:37,680 --> 00:37:39,140 Another thing I'd really like to do 791 00:37:39,140 --> 00:37:40,080 is do fingerprinting. 792 00:37:41,100 --> 00:37:44,020 Is anyone here from, like, a SCADA vendor 793 00:37:44,020 --> 00:37:45,480 or have a lot of assets 794 00:37:45,480 --> 00:37:47,220 or, like, a lab they want to let me borrow 795 00:37:47,220 --> 00:37:47,900 for a little while? 796 00:37:49,440 --> 00:37:51,620 Because the problem with writing a fingerprinting database 797 00:37:51,620 --> 00:37:53,840 is you have to actually, like, have the thing, 798 00:37:54,660 --> 00:37:55,640 which is expensive. 799 00:37:55,780 --> 00:37:57,620 I mean, if anyone knows anything about SCADA, 800 00:37:57,680 --> 00:37:59,640 like, this gear is, like, in the millions of dollars, 801 00:37:59,720 --> 00:38:01,200 some of it, so, yeah. 802 00:38:02,300 --> 00:38:02,700 Yeah. 803 00:38:06,540 --> 00:38:08,000 Yeah, I'm thinking my day job 804 00:38:08,000 --> 00:38:09,880 would probably really not get along with me 805 00:38:09,880 --> 00:38:11,800 if I did that, but, yeah, 806 00:38:11,920 --> 00:38:12,600 I thought about that, too. 807 00:38:13,880 --> 00:38:15,220 But, also, I'm, you know, 808 00:38:15,260 --> 00:38:16,600 it's an open source project. 809 00:38:16,800 --> 00:38:19,880 If anyone wants to help me write it and make it better, 810 00:38:19,980 --> 00:38:20,860 give me some suggestions. 811 00:38:21,240 --> 00:38:22,720 You know, please do contact me, 812 00:38:22,760 --> 00:38:24,440 especially if you have gear that I can test, 813 00:38:25,100 --> 00:38:27,120 because I would love to be able to do that 814 00:38:27,120 --> 00:38:29,060 and make it a much more robust tool. 815 00:38:32,300 --> 00:38:33,660 So, just quick, actually, 816 00:38:33,760 --> 00:38:35,260 wow, I ran short for a change. 817 00:38:35,320 --> 00:38:36,400 Go figure. 818 00:38:36,740 --> 00:38:39,180 I just wanted to put up my references 819 00:38:39,180 --> 00:38:41,300 and some thanks to Kathleen Whalen, 820 00:38:41,440 --> 00:38:42,620 Jim Kelly, and Doug Wilson, 821 00:38:42,840 --> 00:38:45,160 who helped me put this presentation or tool together 822 00:38:45,160 --> 00:38:46,860 in various ways. 823 00:38:48,540 --> 00:38:50,900 And my contact information's here. 824 00:38:51,200 --> 00:38:53,320 My blog is onelittlewindow.org. 825 00:38:53,400 --> 00:38:54,560 I actually share it with Doug Wilson, 826 00:38:54,720 --> 00:38:55,920 so you can hit us up there. 827 00:38:56,260 --> 00:38:57,180 Send me an email. 828 00:38:57,760 --> 00:38:58,560 Yeah, whatever. 829 00:38:59,180 --> 00:39:00,300 So, any more questions? 830 00:39:00,420 --> 00:39:01,560 Because I actually have extra time. 831 00:39:21,440 --> 00:39:22,940 So, the question was, 832 00:39:23,060 --> 00:39:25,920 is there any hope for a more secure protocol? 833 00:39:26,200 --> 00:39:26,680 And that's funny, 834 00:39:26,820 --> 00:39:29,380 because outside of the SCADA talk at DEF CON, 835 00:39:29,620 --> 00:39:30,680 or sorry, at Black Hat, 836 00:39:30,860 --> 00:39:32,740 we were having a conversation about how, 837 00:39:32,880 --> 00:39:34,660 like, we as an industry need to get together 838 00:39:34,660 --> 00:39:35,300 and start a new protocol. 839 00:39:35,300 --> 00:39:36,240 And start writing a better protocol 840 00:39:36,240 --> 00:39:37,940 and start mandating it. 841 00:39:38,980 --> 00:39:40,080 So, the answer is no, 842 00:39:40,220 --> 00:39:41,880 but we'd like to see it go that way. 843 00:39:44,140 --> 00:39:45,180 So, anyone else? 844 00:39:45,320 --> 00:39:46,640 I can't even really see hands. 845 00:39:46,800 --> 00:39:47,800 So, if anyone else has questions, 846 00:39:47,880 --> 00:39:48,560 just come see me, 847 00:39:48,580 --> 00:39:50,220 because I can't really see you at this point. 848 00:39:50,320 --> 00:39:50,940 You're too early. 849 00:39:51,120 --> 00:39:51,980 You can't finish yet. 850 00:39:52,020 --> 00:39:52,420 I know. 851 00:39:53,220 --> 00:39:53,780 I'm sorry. 852 00:39:54,820 --> 00:39:56,020 I talk faster.