1 00:00:00,000 --> 00:00:07,420 So, a couple of years ago, I did run a Mac, and I was working with the MSP430 microcontroller. 2 00:00:08,400 --> 00:00:16,200 This was before virtualization had gotten to the point that it's at today, where you can actually share a USB device with your virtual operating system. 3 00:00:17,160 --> 00:00:20,260 And it was just before Bootcamp came out. 4 00:00:20,640 --> 00:00:28,420 So, I had this Macintosh, it was running OSX, it was Intel-compatible, and there was not a damn thing I could do to debug an MSP430 on it. 5 00:00:28,420 --> 00:00:42,260 Because the MSP430 requires a driver for its USB-to-serial chip, sorry, the MSP430 JTAG programmer requires a driver for a USB-to-serial chip that's inside of it. 6 00:00:43,260 --> 00:00:46,000 Linux had this driver, OSX did not. 7 00:00:47,080 --> 00:00:53,320 Even if OSX did, there's no Macintosh application for debugging the MSP430 chips. 8 00:00:53,980 --> 00:00:56,360 And the Linux one required a closed source library. 9 00:00:56,360 --> 00:01:06,860 So, even though you could debug a chip under GCC and Linux, there was nothing that you could do to debug that same chip from a Macintosh workstation. 10 00:01:08,620 --> 00:01:13,520 And then new chips were released, which weren't supported on Linux. 11 00:01:14,060 --> 00:01:26,340 They began to let the GCC, so the GDB, the GNU debugger library, this closed source library that would allow for GDB to debug an MSP430. 12 00:01:27,220 --> 00:01:32,220 They began to allow it to atrophy, and they stopped patching it. 13 00:01:32,340 --> 00:01:38,360 So today, I have several MSP430 devices that have been given to me by friends or that I've purchased, 14 00:01:38,600 --> 00:01:47,740 which I cannot debug or develop for using the official tools because the official tools are out of date, and I run Linux. 15 00:01:48,700 --> 00:01:51,680 If I ran Mac or OpenBSD, I would have further issues. 16 00:01:53,040 --> 00:01:55,920 So today, I suffer through X11 hell. 17 00:01:56,360 --> 00:02:02,240 Every time I get on stage, just so that I can retain compatibility with that one piece of hardware without running Windows. 18 00:02:04,360 --> 00:02:12,640 So to remedy this, I've begun building my own JTAG adapter that's open source and that supports as many chips as I can get my hands on. 19 00:02:13,460 --> 00:02:16,320 In some cases, this can be done with the official documentation. 20 00:02:17,420 --> 00:02:20,860 The basics of how JTAG is supposed to work are very well documented. 21 00:02:22,140 --> 00:02:24,380 It's very simple in the hardware sense. 22 00:02:24,540 --> 00:02:26,040 So while it's a low-level protocol, 23 00:02:27,080 --> 00:02:33,200 the method with which you send an instruction or send data is very standardized. 24 00:02:36,740 --> 00:02:42,200 So these are the badges from NeighborCon, which some of you might have attended over the past few days. 25 00:02:42,380 --> 00:02:48,820 It was a two-day conference that took place during Black Hat, but it was a lot more neighborly and was free to attend. 26 00:02:49,760 --> 00:02:52,740 And at NeighborCon, we gave away 180 of these. 27 00:02:56,360 --> 00:02:57,440 Just a quick disclaimer. 28 00:03:01,840 --> 00:03:05,480 Dealing with these things at such a low level will make you dyslexic. 29 00:03:05,760 --> 00:03:07,980 I wasn't when I began working on this. 30 00:03:08,120 --> 00:03:15,920 But then I had to deal with differences between Big Endian and Little Endian for the way in which a byte fit into a word. 31 00:03:16,620 --> 00:03:20,420 And beyond that, I had to deal with the ordering of the bits within a byte. 32 00:03:21,660 --> 00:03:24,280 Because in some cases, the most significant bit comes first. 33 00:03:24,320 --> 00:03:26,340 And in some cases, the less significant bit. 34 00:03:26,360 --> 00:03:34,740 Even in JTAG on the MSP430, data comes LSB first and instructions come MSB first. 35 00:03:35,180 --> 00:03:37,340 Or vice versa. I can't really remember right now. 36 00:03:38,660 --> 00:03:41,940 And then some things occur on the rising edge of a clock. 37 00:03:42,080 --> 00:03:43,740 Some things happen on the falling edge of a clock. 38 00:03:44,060 --> 00:03:48,800 In SPI, you write your output on the rising edge and you sample your input on the falling edge. 39 00:03:50,300 --> 00:03:53,220 But then some SPI chips do it the other way around. 40 00:03:53,460 --> 00:03:53,880 And there's... 41 00:03:55,260 --> 00:03:56,140 It's just... 42 00:03:56,920 --> 00:03:58,220 It's just hell to deal with. 43 00:03:58,540 --> 00:04:01,440 And then some things are inverted and some things are uninverted. 44 00:04:01,680 --> 00:04:07,260 So, for example, the reset pin is actually reset with a bar over it or an exclamation point before it. 45 00:04:07,460 --> 00:04:08,840 And that means that it's inverted. 46 00:04:09,080 --> 00:04:13,680 So the chip is held in its reset state when the reset pin is low. 47 00:04:14,060 --> 00:04:17,660 And it's in its normal non-reset state when the reset pin is high. 48 00:04:18,460 --> 00:04:22,120 This is done for reasons of physics and hardware convenience. 49 00:04:22,700 --> 00:04:22,960 And... 50 00:04:24,180 --> 00:04:25,680 So I'm going to screw... 51 00:04:26,360 --> 00:04:27,280 Some of this minutiae up. 52 00:04:27,480 --> 00:04:31,660 If you're trying to build JTAG based upon my spoken documentation at this conference, 53 00:04:31,900 --> 00:04:33,880 you're going to get a couple of things backward. 54 00:04:34,260 --> 00:04:37,380 So please double-check the minutiae in writing. 55 00:04:39,360 --> 00:04:41,440 JTAG itself is a testing standard. 56 00:04:41,680 --> 00:04:44,220 The idea is that if you're manufacturing circuit boards, 57 00:04:44,440 --> 00:04:47,380 or when this first came out, computers, 58 00:04:48,140 --> 00:04:50,880 you have many different chips on these boards. 59 00:04:51,140 --> 00:04:54,420 And you have to know that the chips are wired to one another properly. 60 00:04:54,600 --> 00:04:56,240 That you have good solder. 61 00:04:56,360 --> 00:04:57,380 You have good solder joints and all of that stuff. 62 00:04:57,980 --> 00:05:03,380 So, decades ago, they began to use shift registers on the edges of the boards. 63 00:05:03,520 --> 00:05:08,140 So that you could debug an individual board in isolation 64 00:05:08,580 --> 00:05:11,520 without removing it from the containing computer. 65 00:05:12,120 --> 00:05:15,280 This was back when a single computer chip would be made of stacked boards 66 00:05:15,960 --> 00:05:19,460 rather than a single chip of custom silicon. 67 00:05:19,860 --> 00:05:23,060 And when they began to build chips in silicon, 68 00:05:24,060 --> 00:05:25,680 they needed this even more. 69 00:05:25,680 --> 00:05:30,920 Because if you manufacture a chip that's too small for a technician to stick needles into, 70 00:05:31,500 --> 00:05:32,960 without extreme efforts, 71 00:05:34,960 --> 00:05:39,000 and it's running too quickly for things to be caught in a scope, 72 00:05:39,140 --> 00:05:40,680 and all of these other issues, 73 00:05:41,400 --> 00:05:45,280 what happens when you do a production run and it doesn't work? 74 00:05:46,880 --> 00:05:50,260 You'll find yourself with 10,000 units that are utterly worthless. 75 00:05:50,700 --> 00:05:53,240 No idea of how to repair them or rebrand them, 76 00:05:55,680 --> 00:05:56,620 or anything like that. 77 00:05:56,720 --> 00:05:59,020 And more importantly, you don't know where you screwed up. 78 00:05:59,880 --> 00:06:03,860 So the next time you try it, it's likely that you'll have the same problem. 79 00:06:04,300 --> 00:06:07,900 So while in software, recompiling takes a couple of minutes, 80 00:06:07,940 --> 00:06:13,200 or a couple of hours for an extraordinarily large operating system, 81 00:06:13,740 --> 00:06:17,060 in the case of hardware, you're looking at a turnaround time of months 82 00:06:17,060 --> 00:06:19,000 between deciding that you made a mistake 83 00:06:19,560 --> 00:06:22,900 and actually getting to try the next iteration of your design. 84 00:06:24,420 --> 00:06:24,900 So, 85 00:06:24,900 --> 00:06:27,220 you need some sort of debugging, 86 00:06:27,690 --> 00:06:30,540 both to verify that what you intended to put into the design 87 00:06:31,090 --> 00:06:33,620 was fabricated correctly in hardware, 88 00:06:33,860 --> 00:06:39,360 and to determine that the software that you're running works properly. 89 00:06:39,820 --> 00:06:42,080 Because when computers began to come up in this form, 90 00:06:42,690 --> 00:06:47,280 it was necessary to single-step and to debug the software that they were running, 91 00:06:47,420 --> 00:06:51,700 even though the chips themselves had too little memory to store a debug monitor, 92 00:06:52,980 --> 00:06:53,520 or 93 00:06:53,520 --> 00:06:55,960 an operating system like Linux. 94 00:06:56,300 --> 00:07:03,160 So the JTAG machine exists within almost every chip 95 00:07:03,520 --> 00:07:07,340 for the sole purpose of debugging it and ensuring that it was manufactured correctly 96 00:07:07,340 --> 00:07:10,000 for programming it, for all of these different things. 97 00:07:10,400 --> 00:07:15,940 And because it has to exist in hardware for the hardware to be manufactured, 98 00:07:16,260 --> 00:07:18,780 it's a very interesting thing to play with as a hacker, 99 00:07:18,920 --> 00:07:20,840 because you can take the chip apart. 100 00:07:21,340 --> 00:07:22,320 You can... 101 00:07:23,520 --> 00:07:24,740 run instructions on it. 102 00:07:24,820 --> 00:07:26,080 You can debug software on it. 103 00:07:26,160 --> 00:07:27,980 You can do all of these crazy things 104 00:07:28,960 --> 00:07:33,060 that are useful for development, but also for breaking into things. 105 00:07:33,380 --> 00:07:37,640 Because you can, say, single-step a program on physical hardware, 106 00:07:38,700 --> 00:07:40,080 waiting to see what happens. 107 00:07:40,180 --> 00:07:42,580 If you're trying to fuzz, say, a wireless sensor, 108 00:07:42,780 --> 00:07:47,660 you can run it at one one-thousandth of real time, 109 00:07:48,480 --> 00:07:49,880 and the device will still work. 110 00:07:49,980 --> 00:07:53,260 The radio doesn't care how slowly the microcontroller speaks to it. 111 00:07:53,520 --> 00:07:56,460 The application itself will chug along. 112 00:07:59,080 --> 00:08:02,740 So you can fuzz these devices and actually watch the stack grow 113 00:08:02,740 --> 00:08:05,260 just as you would in a simulator, but on actual hardware 114 00:08:05,260 --> 00:08:09,920 without having to reimplement all of the hardware and simulation. 115 00:08:12,080 --> 00:08:15,420 So you can use this to program a device, to read the programming out of it. 116 00:08:15,620 --> 00:08:20,720 If you purchase a device, you can dump its firmware 117 00:08:20,720 --> 00:08:22,760 and then begin to analyze it and look for problems. 118 00:08:22,760 --> 00:08:24,240 Or you can clone it. 119 00:08:24,300 --> 00:08:28,480 You can manufacture your own unit with similar hardware 120 00:08:28,480 --> 00:08:32,260 and then flash the original software into it. 121 00:08:33,060 --> 00:08:36,580 And the access control mechanisms of this are only intended to prevent that. 122 00:08:36,720 --> 00:08:38,420 They don't really care that you might be able to debug. 123 00:08:38,720 --> 00:08:40,340 They don't really care that you might be able to view RAM. 124 00:08:40,660 --> 00:08:45,280 All they worry about is that you might be able to copy the contents of flash memory 125 00:08:45,280 --> 00:08:51,620 into another chip and then execute that or sell it, making a counterfeit product. 126 00:08:52,900 --> 00:08:55,160 So this is my DEFCON badge. 127 00:08:56,000 --> 00:09:00,640 And to hack this, I decided I'd actually hack the Freescale JTAG standard 128 00:09:03,100 --> 00:09:07,340 because you can't program the Freescale chips very easily from Linux. 129 00:09:08,900 --> 00:09:12,200 It's the same situation that the MSP430 had when I began to work on it. 130 00:09:13,740 --> 00:09:20,120 So if you see these little testing pads on the back of your badge near the chip, 131 00:09:20,220 --> 00:09:21,640 and you can look at your own badge for this. 132 00:09:21,640 --> 00:09:23,780 You don't have to rely on the screen. 133 00:09:24,900 --> 00:09:26,140 These are wired up. 134 00:09:26,260 --> 00:09:30,760 This is a diagram that someone lent me in the hardware hacking village. 135 00:09:31,060 --> 00:09:33,960 I'm not sure where he got it, but probably from your CD. 136 00:09:34,720 --> 00:09:41,180 And if you wire all of the pins up correctly, you can then speak to your badge externally. 137 00:09:41,820 --> 00:09:45,500 And unlike the bootloader, which connects to the serial pins, 138 00:09:45,760 --> 00:09:48,700 I think it's those two there. 139 00:09:48,960 --> 00:09:51,620 Unlike the bootloader, this never goes away. 140 00:09:52,800 --> 00:09:55,680 So even though you might be able to damage the bootloader of your chip, 141 00:09:56,360 --> 00:09:59,920 through software, there's nothing that you can do to hose JTAG. 142 00:10:00,260 --> 00:10:02,760 And if you put a fresh chip onto your board, 143 00:10:04,180 --> 00:10:07,440 after the lecture, we'll be blowing mine up in the name of science. 144 00:10:08,320 --> 00:10:10,420 So I'll need to swap it out with another chip. 145 00:10:10,760 --> 00:10:14,040 And when I put that fresh chip onto my board, it won't have a bootloader. 146 00:10:14,160 --> 00:10:17,140 And it won't have the software that it currently has. 147 00:10:17,260 --> 00:10:18,700 It won't be blinking, it won't be running anything. 148 00:10:19,920 --> 00:10:21,620 So the only way to flash news, 149 00:10:21,640 --> 00:10:23,160 and put new software onto it, is by JTAG. 150 00:10:28,000 --> 00:10:31,480 And this being undocumented, there's a little challenge 151 00:10:31,480 --> 00:10:34,160 in figuring out exactly how JTAG works for it. 152 00:10:34,620 --> 00:10:36,420 Freescale intends to publish it at some point, 153 00:10:36,460 --> 00:10:39,980 but for now it's a black box that can only be had by an NDA 154 00:10:39,980 --> 00:10:44,420 that I can't sign because I intend to release my implementation as open source. 155 00:10:46,340 --> 00:10:50,320 So then, once this connector is on, you can run it to a JTAG debugger. 156 00:10:51,680 --> 00:10:52,860 Or an adapter. 157 00:10:53,180 --> 00:10:57,280 This adapter will be connected to by your development environment. 158 00:10:57,540 --> 00:11:01,960 Whether it's a commercial IDE, or whether you're using open source tools. 159 00:11:04,240 --> 00:11:09,380 The standard itself only tells you how to populate two registers 160 00:11:09,380 --> 00:11:11,840 and then send a trigger for execution. 161 00:11:13,140 --> 00:11:18,060 So when I say that it's a standard, I don't mean that chips can be read and written in a standard way. 162 00:11:19,260 --> 00:11:20,900 So the standard verbs 163 00:11:20,900 --> 00:11:24,440 only allow you to pass traffic from one chip to the next, 164 00:11:24,960 --> 00:11:26,540 or to identify a given chip. 165 00:11:27,000 --> 00:11:29,700 And there are a few others that are semi-standard that might be supported. 166 00:11:30,060 --> 00:11:33,020 In the case of the MSP430, no other verbs are supported 167 00:11:33,020 --> 00:11:34,800 that are in the standard set. 168 00:11:35,700 --> 00:11:38,640 So if you want to read or write memory, if you want to blow the JTAG fuse, 169 00:11:38,920 --> 00:11:40,360 especially if you want to write flash memory, 170 00:11:40,660 --> 00:11:43,840 you have to understand how the chip works. 171 00:11:44,560 --> 00:11:48,960 How it expects to be told what programming method to use. 172 00:11:50,900 --> 00:11:54,800 So to quickly review, and I swear that this will be brief, 173 00:11:56,380 --> 00:11:58,140 there's the serial peripheral interface. 174 00:11:59,080 --> 00:12:04,260 The way that this works is that you send one line to tell the chip that you're ready to speak to it. 175 00:12:04,780 --> 00:12:06,260 And then it begins counting bits. 176 00:12:06,860 --> 00:12:09,420 You send it eight bits, and at the very end of it, 177 00:12:09,520 --> 00:12:12,160 it might do something, or it might just wait for more traffic. 178 00:12:12,960 --> 00:12:16,440 And every time you send a bit, for every bit that you send, rather, 179 00:12:16,520 --> 00:12:17,620 you receive a bit. 180 00:12:18,640 --> 00:12:20,640 So it's always an exchange of information. 181 00:12:21,180 --> 00:12:26,060 If you want to write one byte to an SD card, for example, 182 00:12:26,220 --> 00:12:27,980 you're also receiving a byte. 183 00:12:28,520 --> 00:12:31,380 And it might be garbage, but it is received nonetheless. 184 00:12:31,760 --> 00:12:34,580 And that allows for a nice trick that I'll explain near the end. 185 00:12:36,000 --> 00:12:39,200 JTAG works similarly, except that you've got two registers instead of one. 186 00:12:39,320 --> 00:12:42,120 And you actually mark when you're finished writing that register. 187 00:12:43,400 --> 00:12:46,680 So you can send both an instruction and a data. 188 00:12:46,980 --> 00:12:49,660 And the data value will be of a fixed width, 189 00:12:49,760 --> 00:12:50,880 but the width is unique to each one. 190 00:12:50,900 --> 00:12:52,420 As is the instruction. 191 00:12:52,740 --> 00:12:56,580 So while an instruction might be eight bits wide on an MSP430, 192 00:12:56,760 --> 00:12:58,800 on other chips it could be 16 bits. 193 00:12:59,480 --> 00:13:01,200 And the data register varies more. 194 00:13:02,340 --> 00:13:08,580 Some FPGAs have odd widths, 237 bits of width to the data register. 195 00:13:10,960 --> 00:13:15,000 And you have to determine these widths before you can begin to reverse engineer 196 00:13:15,000 --> 00:13:18,800 the JTAG protocol of a given chip, of a given family. 197 00:13:20,120 --> 00:13:20,640 . 198 00:13:22,260 --> 00:13:25,140 So the good FET is an open debugger. 199 00:13:25,180 --> 00:13:29,160 It supports the VSD license on both its hardware and its software. 200 00:13:29,500 --> 00:13:33,300 If you need something more liberal, just ask me for it and feel free. 201 00:13:33,380 --> 00:13:36,340 If you want to make your own production run, do it. 202 00:13:36,800 --> 00:13:38,200 You have every right to. 203 00:13:38,300 --> 00:13:43,600 I only ask that you send a couple of neighborly gift units to myself. 204 00:13:44,020 --> 00:13:46,180 And I don't even legally require that. 205 00:13:46,980 --> 00:13:50,740 As far as the architecture goes, you've got C running inside of an MSP430. 206 00:13:50,900 --> 00:13:52,540 This is compiled by GCC. 207 00:13:53,680 --> 00:14:00,300 To compile the firmware, link it, install it, and restart the machine to be executing it, 208 00:14:00,420 --> 00:14:01,440 you just type make install. 209 00:14:03,200 --> 00:14:05,780 It's quite neighborly in that regard if you're used to the Unix environment. 210 00:14:06,000 --> 00:14:13,060 Because you don't need to deal with the embedded development tools or IDEs that are specific to each chip. 211 00:14:13,300 --> 00:14:17,020 So you don't have to know anything about the MSP430, which I use on this board, 212 00:14:17,160 --> 00:14:19,200 in order to begin adding support for new protocols. 213 00:14:19,200 --> 00:14:22,640 And then I use Python for the workstation code. 214 00:14:24,040 --> 00:14:28,100 And the Python is written in a style that it's very easy to copy or port. 215 00:14:28,960 --> 00:14:32,720 I don't use proper class inheritance or things like that, 216 00:14:32,800 --> 00:14:37,580 because it hides the code that you're looking for when you're trying to copy something. 217 00:14:39,520 --> 00:14:40,960 And then you use it like this. 218 00:14:41,060 --> 00:14:46,240 You say good fit dot and then the name of the chip to choose your Python script. 219 00:14:47,520 --> 00:14:48,440 Followed by a verb. 220 00:14:48,440 --> 00:14:50,620 Followed by maybe a file name and a range. 221 00:14:51,880 --> 00:14:57,220 So here I'm asking it to dump from data memory of a chip con radio 222 00:14:57,900 --> 00:15:01,480 to a file called foo from dead to beef. 223 00:15:03,760 --> 00:15:05,260 Sorry, from dead to deaf. 224 00:15:05,460 --> 00:15:09,500 And having done that, I wind up with this Intel hex file called foo. 225 00:15:09,680 --> 00:15:14,260 And when I call the head function on it, you see that it's all ones. 226 00:15:14,520 --> 00:15:18,420 This is because I chose a region of RAM that doesn't actually exist on the target chip. 227 00:15:18,440 --> 00:15:21,440 But I could dump software in the same way. 228 00:15:22,620 --> 00:15:25,200 And I can do it for multiple chips with the same adapter. 229 00:15:25,660 --> 00:15:32,800 So while that was taken with a chip con radio that supports a non-JTAG debugging standard, 230 00:15:33,200 --> 00:15:37,480 the same thing works with say the MSP430 with a rather awkward JTAG standard. 231 00:15:38,300 --> 00:15:40,320 Or JTAG debugging implementation. 232 00:15:43,320 --> 00:15:46,220 Now the pins on SPI are rather simple. 233 00:15:46,360 --> 00:15:47,880 You just have master in, slave out. 234 00:15:47,880 --> 00:15:50,280 Which is how the chip receives input. 235 00:15:50,880 --> 00:15:53,640 Sorry, how the board receives input from the chip. 236 00:15:54,280 --> 00:15:57,360 Master out, slave in, which is how the chip receives input from the board. 237 00:15:57,600 --> 00:16:00,040 A clock line and a slave select line. 238 00:16:01,120 --> 00:16:04,140 You write data on the rising edge, you read it on the falling edge. 239 00:16:04,580 --> 00:16:06,800 And it's full duplex by necessity. 240 00:16:07,540 --> 00:16:10,820 So every time you send something, you have to receive something as well. 241 00:16:12,100 --> 00:16:14,780 To send a single bit, let's say we wanted to... 242 00:16:18,100 --> 00:16:20,540 So if we want to send a bit, we also receive a bit. 243 00:16:20,660 --> 00:16:22,880 And if we just want to send a single bit in a session, 244 00:16:23,120 --> 00:16:25,460 let's say this SPI device was a single bit wide, 245 00:16:25,640 --> 00:16:31,540 we would drop the clock, then output our bit at this edge, 246 00:16:31,660 --> 00:16:34,280 then raise the clock to sample it. 247 00:16:34,460 --> 00:16:37,340 So both the master and the slave write some bit here, 248 00:16:38,100 --> 00:16:39,740 and then they read it here. 249 00:16:39,960 --> 00:16:41,560 And that's how they exchange their information. 250 00:16:41,940 --> 00:16:47,560 So neither knows what the other is writing until the transaction is completed. 251 00:16:48,080 --> 00:16:50,200 If you compare this to a regular serial line, 252 00:16:50,300 --> 00:16:52,920 it's a lot simpler, even though it requires more wires. 253 00:16:53,300 --> 00:16:55,800 On a regular serial line, you have timing. 254 00:16:55,960 --> 00:16:59,020 So you have to know exactly how wide each bit will be. 255 00:16:59,460 --> 00:17:02,520 You can't pause it in the middle, you can't use non-standard timing. 256 00:17:02,800 --> 00:17:05,320 You can't do any of the things that make SPI easier. 257 00:17:06,620 --> 00:17:08,660 Instead, what you do is you keep the line high 258 00:17:09,260 --> 00:17:10,680 until you're ready to transmit. 259 00:17:10,840 --> 00:17:13,660 Then you send a zero, followed by your actual traffic. 260 00:17:15,200 --> 00:17:17,560 So in this case, we would have the zero here 261 00:17:17,880 --> 00:17:20,260 as the start bit, and then just to its right, 262 00:17:20,320 --> 00:17:24,280 this first upper bar, that would be our one bit of data, 263 00:17:27,080 --> 00:17:30,720 followed by a stop bit, which is always a one, and then nothing. 264 00:17:30,960 --> 00:17:33,760 But you couldn't really tell where this ended on a scope visually. 265 00:17:34,700 --> 00:17:38,040 I mean, you don't know which of these would be ones. 266 00:17:38,400 --> 00:17:39,860 You don't know how wide this is. 267 00:17:40,900 --> 00:17:43,280 This might be five bits for all you know, or it might be eight. 268 00:17:45,500 --> 00:17:47,720 And it requires each device to have a standard 269 00:17:47,880 --> 00:17:51,100 clock. So if you remember dialing into 270 00:17:51,100 --> 00:17:54,480 bulletin board systems, if you typed in 2400 8N1, 271 00:17:55,020 --> 00:17:57,460 that would be eight bits wide, no parity 272 00:17:57,460 --> 00:18:01,000 bit, one stop bit at 2400 baud. 273 00:18:01,060 --> 00:18:03,400 And you had to know exactly how fast you were communicating. 274 00:18:05,360 --> 00:18:07,040 JTAG doesn't care about speed. 275 00:18:07,260 --> 00:18:10,460 You can run it faster or slower, and the slave 276 00:18:10,460 --> 00:18:12,560 will automatically match to the master. 277 00:18:14,000 --> 00:18:17,160 And you have two registers to populate. You have an instruction register 278 00:18:17,160 --> 00:18:20,500 and a data register. You don't know the instructions 279 00:18:20,500 --> 00:18:23,080 set, and you don't know the register widths until you actually 280 00:18:23,080 --> 00:18:25,580 look up the documentation for a chip, which might be secret. 281 00:18:27,500 --> 00:18:29,520 And it's implemented in hardware, so there's no 282 00:18:29,520 --> 00:18:33,000 actual program receiving the JTAG sequences 283 00:18:33,000 --> 00:18:34,960 on the target device. 284 00:18:36,060 --> 00:18:38,520 Instead, it is interpreted by a state machine. 285 00:18:38,900 --> 00:18:42,020 And you push your way around the state machine by the TMS line. 286 00:18:42,700 --> 00:18:43,840 Let me grab some water. 287 00:18:45,620 --> 00:18:46,960 Someone would grab me a beer. 288 00:18:47,020 --> 00:18:47,740 That would be neighborly. 289 00:18:52,460 --> 00:18:53,300 Blue Moon or 290 00:18:53,300 --> 00:18:53,760 Sierra Nevada. 291 00:18:57,500 --> 00:18:57,980 So, 292 00:18:59,880 --> 00:19:02,460 this defers from SPI in that you have two shift 293 00:19:02,460 --> 00:19:05,780 registers instead of one, and that you actually say 294 00:19:05,780 --> 00:19:09,460 at some point, I am done transmitting. I'm done transmitting 295 00:19:09,460 --> 00:19:12,420 this register. You never say that in SPI. 296 00:19:12,420 --> 00:19:16,060 By the time you raise the slave select line, you're saying 297 00:19:16,060 --> 00:19:17,820 this transaction is over. I'm not speaking to you anymore. 298 00:19:18,040 --> 00:19:20,940 I don't care about any reply. I'm never going to hear from you again. 299 00:19:23,240 --> 00:19:25,260 So, this allows JTAG 300 00:19:25,260 --> 00:19:27,940 to have more complicated commands 301 00:19:27,940 --> 00:19:31,260 and things like that. The pins themselves 302 00:19:31,260 --> 00:19:32,460 are just output input. 303 00:19:34,220 --> 00:19:37,400 There's a clock line and also a machine select line, 304 00:19:37,600 --> 00:19:41,060 which is how you move around the thing. And then 305 00:19:41,060 --> 00:19:43,440 there are two wire variants of this, 306 00:19:44,180 --> 00:19:46,960 such as SPI by wire. SPI by wire works by having 307 00:19:46,960 --> 00:19:50,100 just a clock line and an everything line. 308 00:19:50,620 --> 00:19:52,620 The everything line multiplexes between 309 00:19:53,640 --> 00:19:56,820 the four signal lines on the clock. So, you'll send one, 310 00:19:56,840 --> 00:19:58,140 then the other, then the other, then the other. 311 00:19:59,800 --> 00:20:03,080 The state machine itself is called a TAP. It has the two registers 312 00:20:03,080 --> 00:20:05,640 that we discussed. There's also a run and idle state, 313 00:20:06,100 --> 00:20:08,840 which is what happens when the chip is held 314 00:20:09,320 --> 00:20:10,540 single stepping instructions. 315 00:20:12,580 --> 00:20:13,700 If it's paused. 316 00:20:14,780 --> 00:20:18,260 In this state, the chip isn't doing very much, 317 00:20:18,360 --> 00:20:21,060 but it has time to work. So, sometimes 318 00:20:21,060 --> 00:20:23,820 when you're writing flash memory, you might go through the run idle state 319 00:20:23,820 --> 00:20:27,660 at a particular frequency. In a bit, I'll get to 320 00:20:27,660 --> 00:20:30,200 how you can damage the chip by setting the wrong frequency. 321 00:20:30,560 --> 00:20:32,700 And perhaps some of you can figure out how to work that into an exploit. 322 00:20:33,320 --> 00:20:35,700 There's also a fuse, which denies your access. 323 00:20:38,840 --> 00:20:41,620 It's blown in hardware. This means that you're physically 324 00:20:41,620 --> 00:20:45,120 locked out of the device. You're unable to 325 00:20:45,120 --> 00:20:48,320 read or write it. You're not allowed to send any commands 326 00:20:48,320 --> 00:20:51,240 except for the one that forwards data onto the next device in the chain. 327 00:20:52,060 --> 00:20:54,020 And if you can get past this fuse, 328 00:20:54,620 --> 00:20:56,780 then you've broken the copy protection of the chip. 329 00:20:57,200 --> 00:21:00,380 You're able to access information that it expects to hold privately. 330 00:21:01,440 --> 00:21:03,240 Further, if the fuse is in software, 331 00:21:05,580 --> 00:21:08,420 then instead of physically blowing something up, 332 00:21:08,840 --> 00:21:11,860 instead you're waiting until boot time, 333 00:21:12,060 --> 00:21:13,780 and then you're reading a value out of flash memory, 334 00:21:14,020 --> 00:21:18,260 and that is setting a control register, which denies your access. 335 00:21:18,540 --> 00:21:21,240 When this happens, you can sometimes break the chip by 336 00:21:21,240 --> 00:21:23,780 slowly ramping its voltage up, 337 00:21:25,520 --> 00:21:27,860 or by violently jerking it when you start it, 338 00:21:27,900 --> 00:21:30,060 or by starting it without a clock 339 00:21:30,940 --> 00:21:31,440 , 340 00:21:34,500 --> 00:21:37,720 such that it never gets a chance to read the 341 00:21:40,140 --> 00:21:43,180 fuse setting until after you've already done something malicious. 342 00:21:43,900 --> 00:21:45,740 The state machine itself looks like this. 343 00:21:45,980 --> 00:21:47,980 You've got your run check state, 344 00:21:48,200 --> 00:21:51,760 or fuse check state, where it actually looks to see 345 00:21:51,760 --> 00:21:53,100 whether or not you're allowed to enter. 346 00:21:55,520 --> 00:21:58,000 This is followed by the run test idle state. 347 00:21:58,300 --> 00:22:00,680 And you see ones and zeros in this diagram. 348 00:22:00,880 --> 00:22:02,020 That's the value of TMS. 349 00:22:03,320 --> 00:22:08,060 And you advance from one state to the next on any given TCK cycle. 350 00:22:09,320 --> 00:22:11,440 So here we've got a zero, 351 00:22:11,600 --> 00:22:14,860 and then it's a bit obscured by the X11 garbage. 352 00:22:15,640 --> 00:22:18,880 But if you see the zero with the arrows circling around it, 353 00:22:18,940 --> 00:22:21,800 that's jumping out of run test back into itself. 354 00:22:22,320 --> 00:22:24,300 So if you want to stay in any given state, 355 00:22:24,480 --> 00:22:28,560 you have to be in a state that allows you that luxury. 356 00:22:29,480 --> 00:22:31,040 And then you have to know 357 00:22:31,040 --> 00:22:33,160 whether it's a one or a zero that loops back. 358 00:22:34,680 --> 00:22:38,040 So from here, if on the first edge TMS 359 00:22:38,840 --> 00:22:42,180 is high, you'll move into the select data register state. 360 00:22:44,280 --> 00:22:45,780 Then you can set it either 361 00:22:45,780 --> 00:22:48,580 high to move into the select instruction register state, 362 00:22:48,720 --> 00:22:51,660 or low to fall down into the capture data register state. 363 00:22:52,560 --> 00:22:54,540 And this is arranged so that you can actually 364 00:22:54,540 --> 00:22:57,800 navigate back to the run test idle state from anywhere, 365 00:22:59,240 --> 00:23:00,740 not knowing where you are. 366 00:23:01,320 --> 00:23:03,460 So if you were to fall into a random state, 367 00:23:03,600 --> 00:23:04,720 you can move back to the beginning. 368 00:23:05,580 --> 00:23:08,300 This isn't terribly relevant on a modern JTAG device, 369 00:23:08,300 --> 00:23:10,160 because very few clock edges are missed. 370 00:23:10,580 --> 00:23:12,260 But if you're trying to break into a device, 371 00:23:12,420 --> 00:23:14,160 you can hit it with a flash bulb. 372 00:23:14,860 --> 00:23:16,420 And on those without protection, 373 00:23:16,560 --> 00:23:18,080 sometimes you fall into a random state. 374 00:23:18,280 --> 00:23:21,500 You might find yourself here in the exit data register state, 375 00:23:22,480 --> 00:23:24,180 moving into update data register, 376 00:23:24,340 --> 00:23:26,540 where it would maybe read a byte of memory 377 00:23:26,540 --> 00:23:27,640 by mistake or something. 378 00:23:28,620 --> 00:23:30,540 But if you can move back to the beginning, 379 00:23:30,660 --> 00:23:32,040 then you can start sending Kliegen commands. 380 00:23:33,080 --> 00:23:35,180 Then you can start breaking into the chip. 381 00:23:36,680 --> 00:23:37,840 The fuse check itself 382 00:23:37,840 --> 00:23:39,360 is performed in an interesting way. 383 00:23:39,520 --> 00:23:42,960 You've got these lines here on TMI. 384 00:23:43,200 --> 00:23:44,980 And you have to have two pulses, 385 00:23:45,060 --> 00:23:46,100 and it has to end high. 386 00:23:47,040 --> 00:23:48,640 Why does it have to be two pulses? 387 00:23:49,160 --> 00:23:53,280 And we'll get back to that in a minute, 388 00:23:53,320 --> 00:23:54,520 but what happens if you send zero, 389 00:23:54,600 --> 00:23:55,800 and what happens if you send a million? 390 00:23:57,640 --> 00:24:00,680 Is it more likely to fail with one or with 100, 391 00:24:00,800 --> 00:24:02,840 and which way is it likely to fail? 392 00:24:04,780 --> 00:24:07,380 The instructions themselves are here. 393 00:24:08,840 --> 00:24:11,340 You can read and write registers. 394 00:24:11,600 --> 00:24:13,240 You can capture data values. 395 00:24:13,800 --> 00:24:15,840 You can push things into different registers. 396 00:24:16,100 --> 00:24:20,800 And everything else is done as a combination of these macros. 397 00:24:21,320 --> 00:24:23,940 So in the documentation for the MSP430, 398 00:24:24,140 --> 00:24:25,960 which is incomplete but still quite helpful, 399 00:24:26,320 --> 00:24:30,820 you'll see that it sends the prepare blow fuse instruction 400 00:24:30,820 --> 00:24:32,120 into the instruction register. 401 00:24:32,740 --> 00:24:34,880 Then you apply the programming voltage, 402 00:24:35,080 --> 00:24:36,880 which will be several volts more 403 00:24:36,880 --> 00:24:38,340 than the chip normally takes, 404 00:24:39,100 --> 00:24:40,780 as is necessary to blow the fuse. 405 00:24:41,900 --> 00:24:43,340 You follow that by the command 406 00:24:43,340 --> 00:24:44,960 that actually blows the fuse. 407 00:24:45,340 --> 00:24:46,640 Then you wait a millisecond 408 00:24:46,640 --> 00:24:48,680 and you remove the programming voltage. 409 00:24:48,940 --> 00:24:50,360 And the interesting thing is at the end, 410 00:24:50,380 --> 00:24:51,020 you're told to reset. 411 00:24:52,200 --> 00:24:53,200 Because if you don't reset, 412 00:24:53,360 --> 00:24:55,100 you don't actually leave the JTAG state, 413 00:24:55,280 --> 00:24:57,340 and you won't even know that the fuse is blown 414 00:24:57,340 --> 00:24:59,980 because it's not checked anywhere except at the beginning. 415 00:25:01,880 --> 00:25:02,800 So you can glitch it. 416 00:25:02,800 --> 00:25:04,800 You can drop voltage very quickly 417 00:25:05,380 --> 00:25:06,540 and then raise it very quickly. 418 00:25:06,680 --> 00:25:08,020 So that the reference voltages change. 419 00:25:09,220 --> 00:25:11,300 And then whatever is being sampled as an input 420 00:25:11,300 --> 00:25:13,020 to a latching flip-flop 421 00:25:13,700 --> 00:25:16,260 is sometimes misread, missampled, 422 00:25:16,300 --> 00:25:17,140 and it will latch incorrectly. 423 00:25:18,620 --> 00:25:21,120 So if you do this at power-up on a chip 424 00:25:21,120 --> 00:25:23,020 that reads the value out of flash memory, 425 00:25:23,080 --> 00:25:24,800 you might wind up with all ones or all zeros 426 00:25:25,320 --> 00:25:26,740 held in that register instead. 427 00:25:28,380 --> 00:25:29,900 This is a probabilistic attack. 428 00:25:30,120 --> 00:25:32,060 It won't work very well on any given attempt, 429 00:25:32,060 --> 00:25:33,420 but you can try it a hundred times. 430 00:25:34,480 --> 00:25:35,520 A thousand times. 431 00:25:35,780 --> 00:25:37,240 This is what it looks like on a scope. 432 00:25:38,120 --> 00:25:39,820 So going back to the diagram, 433 00:25:40,040 --> 00:25:43,660 and you can see in this line here 434 00:25:43,660 --> 00:25:45,640 the actual pulses that are required. 435 00:25:45,960 --> 00:25:47,200 You are told to send two. 436 00:25:49,760 --> 00:25:51,520 So there are two ways of implementing this. 437 00:25:51,960 --> 00:25:54,780 You can either have it default to your failing the fuse check 438 00:25:55,660 --> 00:25:58,220 and then transition into passing once it succeeds. 439 00:25:58,920 --> 00:26:01,820 Such that if any of the pulses say 440 00:26:02,060 --> 00:26:04,020 that you are allowed through, you become allowed through. 441 00:26:05,220 --> 00:26:08,000 Or you can begin in a pass state 442 00:26:08,000 --> 00:26:10,160 and then fall into a failure state. 443 00:26:12,460 --> 00:26:14,740 Such that if you try it zero times, 444 00:26:14,940 --> 00:26:16,300 you automatically fall through. 445 00:26:16,700 --> 00:26:19,720 In the first case, you can just try a million times, 446 00:26:19,880 --> 00:26:21,120 and you keep on glitching them, 447 00:26:21,260 --> 00:26:23,360 and if you ever succeed, then in the end 448 00:26:23,360 --> 00:26:24,980 you've broken your way into the chip. 449 00:26:25,740 --> 00:26:29,140 In the second case, you can just not try anything. 450 00:26:29,920 --> 00:26:31,780 Just never send edges. 451 00:26:32,060 --> 00:26:35,360 And when implementing the JTAG adapter, 452 00:26:36,000 --> 00:26:38,380 a second beer, this is extra neighborly. 453 00:26:38,800 --> 00:26:39,960 Thank you kindly, neighbor. 454 00:26:44,580 --> 00:26:46,260 So because this is hardware, 455 00:26:46,380 --> 00:26:49,060 and because it's implemented by a hardware engineer 456 00:26:49,060 --> 00:26:51,760 who's never had to worry about security 457 00:26:51,760 --> 00:26:55,600 in the way that it's commonly dealt with in IT. 458 00:26:55,740 --> 00:26:59,720 He's never had to deal with one of his chips being broken into 459 00:26:59,720 --> 00:27:01,300 and had to come in work... 460 00:27:01,300 --> 00:27:03,840 come into work early on a Sunday morning because of it. 461 00:27:05,660 --> 00:27:09,040 He really doesn't care as long as he gets paid at the end of the project. 462 00:27:10,060 --> 00:27:11,860 And even if he does care, 463 00:27:11,900 --> 00:27:13,560 he doesn't think that anyone's looking at how 464 00:27:14,540 --> 00:27:16,700 the JTAG implementation of his chip works. 465 00:27:17,440 --> 00:27:19,980 The only exceptions being smart cards. 466 00:27:20,180 --> 00:27:24,820 And even then, they still make mistakes. 467 00:27:25,060 --> 00:27:31,280 And they still design machines as if all of the rules of the game 468 00:27:31,280 --> 00:27:32,500 of digital logic were followed. 469 00:27:33,600 --> 00:27:37,300 When that's not true if the attacker can change the analog supply voltage. 470 00:27:37,500 --> 00:27:38,340 He can change it quickly. 471 00:27:39,640 --> 00:27:44,920 Because these are analog transistors implementing a digital computer. 472 00:27:45,260 --> 00:27:48,720 They still behave as analog transistors. 473 00:27:50,200 --> 00:27:52,640 So back to the state machine itself. 474 00:27:54,440 --> 00:27:58,500 You can see that each of these two columns is used for populating a register. 475 00:27:58,500 --> 00:28:02,220 And you can see that the two columns are pretty much just copies of each other. 476 00:28:02,780 --> 00:28:06,120 But one is further out from the idle state. 477 00:28:06,380 --> 00:28:08,660 If you wanted to add a third register you could 478 00:28:09,280 --> 00:28:11,140 just by putting it further out here. 479 00:28:14,440 --> 00:28:17,780 And then there are primitive sequences that you use for these exchanges. 480 00:28:18,340 --> 00:28:21,960 So inside of the firmware of the good FET there's a command JTAG TRANS 8. 481 00:28:23,320 --> 00:28:25,820 It takes in a byte and it returns a byte. 482 00:28:25,820 --> 00:28:29,300 And all this is doing is swapping that byte out. 483 00:28:29,460 --> 00:28:31,100 And this is called by two functions. 484 00:28:31,400 --> 00:28:33,560 DRSHIFT 16 and IRSHIFT 8. 485 00:28:34,860 --> 00:28:38,660 Which populate the 16 bit data register of the MSP430. 486 00:28:38,800 --> 00:28:42,040 And the 8 bit instruction register of the MSP430. 487 00:28:43,380 --> 00:28:46,340 The instruction register should actually swap the byte around. 488 00:28:47,200 --> 00:28:52,580 Because the bytes are sent in opposite directions. 489 00:28:52,640 --> 00:28:54,360 One of them comes most significant bit first. 490 00:28:54,580 --> 00:28:55,800 The other comes least significant bit first. 491 00:28:55,820 --> 00:28:59,380 And from these you build higher level functions. 492 00:28:59,640 --> 00:29:03,940 There's JTAG 430, SETPC, HULP CPU, RELEASE CPU. 493 00:29:04,160 --> 00:29:06,060 All of the different things that you would need in a debugger. 494 00:29:06,820 --> 00:29:08,980 And these are documented but others are not. 495 00:29:09,300 --> 00:29:13,040 I haven't yet figured out how to set a hardware breakpoint. 496 00:29:14,900 --> 00:29:19,320 I haven't yet figured out how to read registers other than the program counter. 497 00:29:21,660 --> 00:29:24,100 I'm working on it and I've got the recordings to do it. 498 00:29:24,100 --> 00:29:27,600 But the point is that those things are not in the documentation. 499 00:29:28,580 --> 00:29:30,400 And getting them can be a bit fun. 500 00:29:32,580 --> 00:29:37,860 It's also worth noting that writing flash memory is very different from anything else you might do over JTAG. 501 00:29:38,240 --> 00:29:40,880 On most chips you have to send a debugging waveform. 502 00:29:41,380 --> 00:29:43,940 Not a debugging waveform, a clocking waveform. 503 00:29:45,300 --> 00:29:47,500 And it has to be of a specific frequency. 504 00:29:48,220 --> 00:29:49,560 Within a particular tolerance. 505 00:29:51,140 --> 00:29:53,620 I'm sure you're aware that solid state disks. 506 00:29:53,620 --> 00:29:56,720 Were initially decried as unreliable. 507 00:29:56,760 --> 00:29:58,740 Because flash memory could only be written so many times. 508 00:29:59,080 --> 00:30:01,220 And that this is managed by write controllers. 509 00:30:01,420 --> 00:30:06,700 That actually map the physical blocks of the flash memory to virtual blocks. 510 00:30:06,900 --> 00:30:07,840 That your PC sees. 511 00:30:08,660 --> 00:30:10,140 To do wear leveling. 512 00:30:10,560 --> 00:30:14,000 So that if you write the same file in the same location a million times. 513 00:30:14,140 --> 00:30:15,080 It doesn't destroy the disk. 514 00:30:16,540 --> 00:30:19,380 If your timing is wrong when writing flash. 515 00:30:19,540 --> 00:30:20,980 You can actually destroy the disk early. 516 00:30:21,720 --> 00:30:23,420 Which gives you a chance to experiment. 517 00:30:23,620 --> 00:30:24,920 With exactly how it's destroyed. 518 00:30:25,660 --> 00:30:27,480 And I'll get to that in a bit. 519 00:30:27,980 --> 00:30:30,160 This is how the program counter is set. 520 00:30:31,900 --> 00:30:35,820 First you send the control register. 521 00:30:36,200 --> 00:30:38,480 And you set that to 3401. 522 00:30:38,620 --> 00:30:40,020 This is just a magic constant. 523 00:30:40,240 --> 00:30:42,340 But the individual bits are defined in the documentation. 524 00:30:43,200 --> 00:30:45,440 Following that you shift the instruction register. 525 00:30:45,940 --> 00:30:49,960 And you tell it that you want to execute 4030. 526 00:30:50,100 --> 00:30:51,660 To load the instruction register. 527 00:30:53,620 --> 00:30:55,440 Sorry to load the program counter. 528 00:30:56,060 --> 00:30:58,900 From there you send these clock pulses. 529 00:30:59,040 --> 00:31:04,100 And then you write in the value that you want in the program counter. 530 00:31:04,580 --> 00:31:06,300 Through DR shift 16. 531 00:31:06,980 --> 00:31:08,920 You've got to clear the line after that. 532 00:31:09,020 --> 00:31:13,300 And then you have to shift in the instruction register command. 533 00:31:13,420 --> 00:31:17,020 To capture the value that is held within the data register. 534 00:31:17,080 --> 00:31:18,500 To the program counter. 535 00:31:20,800 --> 00:31:23,480 So this is rather complicated. 536 00:31:23,480 --> 00:31:28,040 And it's not how things would behave if you were receiving commands in software. 537 00:31:28,460 --> 00:31:29,780 If you were receiving them in software. 538 00:31:29,960 --> 00:31:33,360 You would just have say a single verb to say that you wanted to set the program counter. 539 00:31:33,520 --> 00:31:34,320 And then you'd have the value itself. 540 00:31:35,520 --> 00:31:41,440 But there's considerable value in being able to automatically come up with this function for a given chip. 541 00:31:42,060 --> 00:31:49,040 And it's not so complicated that it would be impossible to exhaust all possible ways of doing it. 542 00:31:51,120 --> 00:31:52,940 Now for writing the flash pulses. 543 00:31:53,860 --> 00:31:57,860 On the MSP430 you need 350 kilohertz give or take 100. 544 00:31:59,060 --> 00:32:01,800 And writing this in PRC is quite difficult. 545 00:32:02,520 --> 00:32:04,600 TIE tries it in their implementation. 546 00:32:05,180 --> 00:32:09,220 And between versions of their JTAG debugger. 547 00:32:09,680 --> 00:32:12,820 The flash will actually be written with different timing. 548 00:32:12,940 --> 00:32:16,360 Because the optimizations that the embedded compiler supports have changed. 549 00:32:17,320 --> 00:32:19,360 This must be hell for them to maintain and debug. 550 00:32:21,160 --> 00:32:23,300 So in my implementation I went the other route. 551 00:32:23,480 --> 00:32:24,820 And I did it in pure assembly. 552 00:32:27,080 --> 00:32:30,320 And in the red, which is quite difficult to see on the screen. 553 00:32:30,440 --> 00:32:34,400 You can see that I actually counted the number of clock cycles that these instructions would take. 554 00:32:35,060 --> 00:32:38,300 To figure out how long each body of the loop took. 555 00:32:38,620 --> 00:32:41,840 And I calculated that at 3.68 megahertz. 556 00:32:42,980 --> 00:32:47,680 I had 7 to 14 clock cycles allowed per iteration of the loop. 557 00:32:47,800 --> 00:32:49,660 In order to get the right timing width. 558 00:32:50,340 --> 00:32:52,280 My initial implementation was in C. 559 00:32:52,280 --> 00:32:53,620 I thought it's a square wave. 560 00:32:53,740 --> 00:32:55,480 What the hell does it matter what speed it is. 561 00:32:56,200 --> 00:32:58,960 And in doing so I damaged the flash. 562 00:32:59,940 --> 00:33:02,620 And this frequency can come internally or externally. 563 00:33:02,860 --> 00:33:05,840 But in either case you have to set it somewhere. 564 00:33:05,960 --> 00:33:09,220 You have to make the internal clock be divided by the proper value. 565 00:33:09,780 --> 00:33:12,700 Or you have to provide an external waveform of the proper value. 566 00:33:13,200 --> 00:33:18,180 And this is a hell of a lot harder to figure out without having observed it. 567 00:33:18,980 --> 00:33:21,380 Because it might be too high or too low. 568 00:33:23,040 --> 00:33:25,220 I mean how many attempts do you get? 569 00:33:25,460 --> 00:33:28,480 If you guessed this wrong seven times you've destroyed your chip. 570 00:33:28,900 --> 00:33:30,640 You no longer have anything to play with. 571 00:33:32,280 --> 00:33:35,480 So when they're damaged writing becomes sort of semi-permanent. 572 00:33:35,500 --> 00:33:37,940 And it creates erratic writes on future attempts. 573 00:33:38,580 --> 00:33:40,120 So on one of the chips that I damaged. 574 00:33:42,000 --> 00:33:47,540 I wrote DEAD with slow timing. 575 00:33:47,720 --> 00:33:50,260 My clock was 35 kilohertz instead of 350. 576 00:33:51,520 --> 00:33:56,620 And then I erased that entire segment including this word. 577 00:33:56,900 --> 00:33:59,040 When you erase flash memory it goes up to ones. 578 00:33:59,160 --> 00:34:04,120 And then when you write it you're actually clearing out individual bits to get zeros. 579 00:34:05,860 --> 00:34:08,600 If I wrote anywhere else within the same segment. 580 00:34:09,140 --> 00:34:11,940 Suddenly the erasure for this word would fall apart. 581 00:34:12,400 --> 00:34:16,180 And the next time I read it back I would see the original value that I'd written previously. 582 00:34:17,160 --> 00:34:19,300 And then if I were to write anything else there. 583 00:34:19,300 --> 00:34:20,920 The two words would be bitwise anded. 584 00:34:21,140 --> 00:34:25,460 So if you write DEAD and BEEF in the same address you get 9 EAD. 585 00:34:27,480 --> 00:34:33,040 You wind up with a zero in the result if there's a zero in either of the initial values. 586 00:34:33,600 --> 00:34:35,540 But this is after an erasure. 587 00:34:36,180 --> 00:34:41,860 This is after the programmer assumes that the region has been cleared. 588 00:34:42,920 --> 00:34:45,940 And this would even have passed a verification for the erasure. 589 00:34:46,560 --> 00:34:48,540 Because just after the erasure. 590 00:34:48,540 --> 00:34:49,320 It was once. 591 00:34:50,340 --> 00:34:54,780 It would not pass a verification after the second word had been written. 592 00:34:54,840 --> 00:34:55,740 After BEEF was written. 593 00:34:57,600 --> 00:35:00,100 So it might be possible to use this into a sort of exploit. 594 00:35:00,320 --> 00:35:08,880 On the MSP430X2 which is the very most recent chip from Texas Instruments. 595 00:35:09,120 --> 00:35:13,620 You can actually kill the last word of flash memory. 596 00:35:14,880 --> 00:35:16,700 Not quite the last but near the last. 597 00:35:16,860 --> 00:35:18,520 Which controls JTAGX. 598 00:35:18,540 --> 00:35:21,940 So I can take an MSP430 and I can damage a word of its memory. 599 00:35:22,160 --> 00:35:27,480 And then there's nothing that you can do to that same chip to lock me out of JTAG. 600 00:35:29,280 --> 00:35:32,460 Now working this into a meaningful exploit is rather difficult. 601 00:35:32,860 --> 00:35:37,320 Because I would have to do this to a chip, sell the chip to your factory. 602 00:35:37,640 --> 00:35:42,660 Then grab the unit that included that chip out of the factory to steal your design or something like that. 603 00:35:42,900 --> 00:35:45,000 And there are easier ways of extracting such things. 604 00:35:45,780 --> 00:35:47,980 But it's still worth thinking about. 605 00:35:48,700 --> 00:35:51,500 And then there are physical questions to answer about each chip. 606 00:35:51,760 --> 00:35:53,600 For example, how wide are the registers? 607 00:35:54,280 --> 00:35:57,100 On the MSP430 the instruction register is 8 bits wide. 608 00:35:57,920 --> 00:36:04,220 And the data register is 16 bits wide. 609 00:36:04,560 --> 00:36:06,880 On newer MSP430s it's actually 20. 610 00:36:08,440 --> 00:36:11,720 So it's handy to be able to figure out such things quickly. 611 00:36:12,800 --> 00:36:13,940 And you can do this. 612 00:36:14,220 --> 00:36:16,160 Because the registers are cyclical. 613 00:36:16,760 --> 00:36:18,800 It's shifting in on one end and out on the other. 614 00:36:18,940 --> 00:36:22,720 You're writing the most significant bit as you're reading the least significant bit. 615 00:36:23,140 --> 00:36:24,280 Again with the dyslexia. 616 00:36:24,300 --> 00:36:25,780 But it makes sense if you view it as a ring. 617 00:36:26,280 --> 00:36:29,640 Data comes in one side, it shifts all the way around, and then it comes out the end. 618 00:36:31,340 --> 00:36:34,300 So if you shift a thousand zeros into that register. 619 00:36:34,960 --> 00:36:36,560 And then you shift a single one in. 620 00:36:36,660 --> 00:36:40,860 And you count the number of shifts it takes for that one to come back out. 621 00:36:41,160 --> 00:36:43,120 You can determine the width of that register. 622 00:36:45,140 --> 00:36:49,020 And it won't execute the instruction that you send it. 623 00:36:49,080 --> 00:36:51,540 Because unlike SPI you actually tell it when you're finished. 624 00:36:53,160 --> 00:36:57,320 So you don't have to worry about accidentally shifting in a command that will destroy the chip. 625 00:36:57,440 --> 00:36:59,160 Because you're never telling it to execute the command. 626 00:37:01,680 --> 00:37:07,400 So in this case you can just keep spinning around in this region of the data register. 627 00:37:08,020 --> 00:37:10,040 Until you've figured out how wide it is. 628 00:37:11,320 --> 00:37:14,680 And you can automate figuring out how to read from memory. 629 00:37:14,920 --> 00:37:20,100 By flashing a chip with a particular value at a particular address using the official tools. 630 00:37:20,660 --> 00:37:25,740 Then running back and trying every instruction until you can get it back out. 631 00:37:28,080 --> 00:37:30,200 And you can automate this for reading. 632 00:37:30,340 --> 00:37:33,540 You can't automate it for flashing because of how complicated it is. 633 00:37:33,580 --> 00:37:35,620 And how many chips you would destroy in the process. 634 00:37:38,820 --> 00:37:40,020 But it works. 635 00:37:40,040 --> 00:37:43,220 If you're unable to do that you can still look up in the documentation of the chip. 636 00:37:43,340 --> 00:37:45,700 How to write flash memory from inside of the chip. 637 00:37:45,880 --> 00:37:47,540 How to write a program that will write flash memory. 638 00:37:47,880 --> 00:37:49,960 And on the chips that allow you to execute from RAM. 639 00:37:50,680 --> 00:37:53,340 Which are the minority but not a small minority. 640 00:37:54,740 --> 00:37:57,060 You can just write a program to write to flash memory. 641 00:37:57,280 --> 00:37:59,520 Throw that program into RAM and execute it there. 642 00:38:00,260 --> 00:38:03,100 On some chips officially you're even supposed to write flash memory. 643 00:38:03,280 --> 00:38:04,980 By debugging individual instructions. 644 00:38:05,620 --> 00:38:09,140 It's telling the CPU run this command, run this command, run this command, run this command. 645 00:38:10,040 --> 00:38:11,960 While they're all CPU instructions. 646 00:38:12,120 --> 00:38:13,060 Not JTAG instructions. 647 00:38:17,140 --> 00:38:19,020 So because it isn't very elegant. 648 00:38:19,200 --> 00:38:22,220 And because software can't keep up with sniffing the traffic. 649 00:38:22,360 --> 00:38:25,420 You can use either a logic analyzer or a tap emulator. 650 00:38:26,280 --> 00:38:31,500 This is a logic analyzer recording of an MSP430 being written by the spy by wire protocol. 651 00:38:32,720 --> 00:38:38,680 Every one of these clumps of clock edges. 652 00:38:40,040 --> 00:38:45,900 Is actually a sequence of four values being written. 653 00:38:47,000 --> 00:38:51,500 Because on each edge you choose one of the four lines. 654 00:38:51,600 --> 00:38:52,820 And you write it out to the target. 655 00:38:54,520 --> 00:38:56,380 So if you decode this by script. 656 00:38:56,680 --> 00:38:59,820 A friend of mine wrote scripts to handle it. 657 00:38:59,900 --> 00:39:01,640 It's quite the neighbor to do that for me. 658 00:39:02,820 --> 00:39:05,920 You can actually dump it out and see what's being shifted in and where. 659 00:39:08,020 --> 00:39:10,000 And it's also possible to build. 660 00:39:10,040 --> 00:39:11,180 A tap emulator. 661 00:39:12,160 --> 00:39:16,620 If you had say ten chips or let's say two chips. 662 00:39:17,040 --> 00:39:20,260 Of the same model and you wanted to program them at the same time. 663 00:39:20,620 --> 00:39:22,320 You could just connect them in parallel. 664 00:39:22,680 --> 00:39:25,140 But leave one of the TDO pins disconnected. 665 00:39:25,840 --> 00:39:28,640 Then you could send whatever traffic you liked into the chip. 666 00:39:29,840 --> 00:39:31,860 The commands to read and write flash memory. 667 00:39:32,300 --> 00:39:35,040 And while you will only hear responses from one of those chips. 668 00:39:35,140 --> 00:39:36,100 Both chips will be written. 669 00:39:37,940 --> 00:39:38,980 So in the same way. 670 00:39:38,980 --> 00:39:40,660 You could run it out to a state machine. 671 00:39:40,820 --> 00:39:43,020 That does nothing but capture the transactions and hardware. 672 00:39:43,300 --> 00:39:44,840 And then flush them out to SRAM. 673 00:39:45,740 --> 00:39:47,620 And when you're done programming the chip. 674 00:39:47,920 --> 00:39:51,020 Or when you're done doing a particular small action on the chip. 675 00:39:51,160 --> 00:39:53,000 You can read the contents of that SRAM. 676 00:39:53,080 --> 00:39:54,860 To figure out exactly. 677 00:39:55,500 --> 00:39:58,280 How the command that you asked the debugger to do was performed. 678 00:39:59,240 --> 00:40:03,340 And in doing so you can reverse engineer the JTAG protocol of a given chip. 679 00:40:04,640 --> 00:40:06,980 So prototype boards are available for free. 680 00:40:06,980 --> 00:40:09,240 They can presently program the MSP430. 681 00:40:09,560 --> 00:40:12,960 The ChipCon2430 and a few other chips. 682 00:40:13,160 --> 00:40:17,480 As well as SPI flash ROMs and ITC EPROMs. 683 00:40:18,400 --> 00:40:22,880 The EagleCAD schematics and layout and Gerbers and firmware. 684 00:40:23,180 --> 00:40:27,440 And even the client are all available for free under the BSD license. 685 00:40:27,780 --> 00:40:30,880 You can grab them from goodfet.sourceforge.net. 686 00:40:31,760 --> 00:40:32,940 And if you'll. 687 00:40:33,060 --> 00:40:34,160 Do we have the question room? 688 00:40:37,140 --> 00:40:41,400 5 minutes 105 to 106. 689 00:40:42,240 --> 00:40:45,220 Okay so if you'll follow me to room 106. 690 00:40:45,680 --> 00:40:49,640 We, a neighbor and I in the hardware hacking village. 691 00:40:50,320 --> 00:40:52,920 Assembled a stun gun out of a camera. 692 00:40:54,620 --> 00:40:58,980 So we're trying to see exactly how much voltage you can run through the JTAG pins. 693 00:40:59,420 --> 00:41:01,620 Without damaging the rest of the chip. 694 00:41:04,760 --> 00:41:06,840 We haven't quite figured out. 695 00:41:06,840 --> 00:41:07,460 What this will do. 696 00:41:07,640 --> 00:41:09,180 So it might blow up the chip entirely. 697 00:41:09,340 --> 00:41:11,300 Or it might just break the JTAG pins. 698 00:41:11,500 --> 00:41:14,140 But it stands to reason that if you could write. 699 00:41:14,260 --> 00:41:20,440 If you could burn out the input and output pins of the JTAG state machine. 700 00:41:21,900 --> 00:41:25,640 Then when an attacker tries to break into the chip through JTAG. 701 00:41:27,160 --> 00:41:28,760 He'll have no way to communicate with it. 702 00:41:29,340 --> 00:41:33,560 The Ember EM250 vulnerability that I presented at Black Hat recently. 703 00:41:33,860 --> 00:41:35,040 Could be fixed by this. 704 00:41:35,160 --> 00:41:35,400 Because. 705 00:41:35,400 --> 00:41:38,440 You can't connect to the chip externally. 706 00:41:40,360 --> 00:41:41,800 Once the pins are broken. 707 00:41:42,580 --> 00:41:44,540 In any case sparks are going to fly. 708 00:41:44,860 --> 00:41:47,460 And this thing shocks like living hell. 709 00:41:50,020 --> 00:41:51,660 Yeah so room 106. 710 00:41:51,880 --> 00:41:54,320 And we can dump that and then blow up a chip. 711 00:41:54,800 --> 00:41:57,300 Thank you for your time and be neighborly.