1 00:00:00,000 --> 00:00:01,680 How's everybody doing? 2 00:00:02,820 --> 00:00:03,840 Yay, come on. 3 00:00:03,840 --> 00:00:04,360 How's everybody doing? 4 00:00:04,360 --> 00:00:05,000 This is DefCon. 5 00:00:05,900 --> 00:00:06,940 Way better. 6 00:00:06,940 --> 00:00:07,520 Okay. 7 00:00:07,520 --> 00:00:07,800 Okay. 8 00:00:07,800 --> 00:00:10,360 So, today we have a video presentation in track two. 9 00:00:10,360 --> 00:00:12,920 It's going to be remote code execution by MIDI messaging. 10 00:00:13,500 --> 00:00:14,380 Enjoy. 11 00:00:17,240 --> 00:00:19,100 Hello, DefCon 33. 12 00:00:19,340 --> 00:00:20,940 My name is Anna Antonenko. 13 00:00:20,940 --> 00:00:23,660 And unfortunately, I couldn't be here with you today. 14 00:00:23,660 --> 00:00:24,920 Just some visa issues. 15 00:00:24,920 --> 00:00:28,540 So, I'm going to be presenting my talk via video. 16 00:00:28,540 --> 00:00:31,880 My talk is titled Remote Code Execution via MIDI Messages. 17 00:00:31,880 --> 00:00:38,140 It's about exploiting a vulnerability, rather a backdoor that I found in a line of Yamaha products. 18 00:00:39,960 --> 00:00:43,300 About some stuff about me, you can find me online on my website. 19 00:00:43,300 --> 00:00:45,940 I use the same nickname practically everywhere. 20 00:00:45,960 --> 00:00:47,900 Here's my email on the slide here. 21 00:00:48,940 --> 00:00:51,900 I am a forward developer at Flipper Devices. 22 00:00:51,900 --> 00:00:53,300 I write code in C. 23 00:00:53,300 --> 00:00:59,160 But my employer does not necessarily reflect my opinions. 24 00:00:59,160 --> 00:01:02,860 All research was done in my own time. 25 00:01:03,020 --> 00:01:08,040 I really like Erlang and Elixir, their languages for the Beam virtual machine. 26 00:01:08,580 --> 00:01:13,880 I'm working on an operating system written in Erlang in my spare time. 27 00:01:13,880 --> 00:01:19,160 I really like electronics, both analog and digital, but digital is my specialty. 28 00:01:19,260 --> 00:01:21,060 And I like music. 29 00:01:21,060 --> 00:01:25,240 I like writing music, I like playing music, and listening to music, of course. 30 00:01:26,400 --> 00:01:32,020 I prefer to think of myself as a decent forward engineer, but not a great reverse engineer. 31 00:01:32,020 --> 00:01:40,640 So hopefully, those of you who are like me can learn something from this talk, from this perspective. 32 00:01:42,240 --> 00:01:48,080 First, I'm going to show you a little demo about what we're going to be talking today. 33 00:01:48,700 --> 00:01:51,780 But this demo needs a bit of introduction. 34 00:01:52,220 --> 00:01:54,660 So there's this protocol called MIDI. 35 00:01:54,660 --> 00:01:58,780 It emerged in the 80s, and it's a really simple protocol. 36 00:01:58,780 --> 00:02:03,080 It describes music generation parameters, basically. 37 00:02:03,080 --> 00:02:12,320 So one device can tell another to play a note, or release a note, or change some effect parameter, or something like that. 38 00:02:12,360 --> 00:02:16,320 And the other device will generate audio based on that data. 39 00:02:18,080 --> 00:02:22,920 But apparently, even a protocol as simple as that can have backdoors. 40 00:02:24,440 --> 00:02:26,890 So this is a demo. 41 00:02:28,380 --> 00:02:38,790 Here on the bottom, we have a Python script that is sending MIDI commands to a synthesizer, which is on the top, and it is playing by Apple. 42 00:02:38,790 --> 00:02:50,090 And just by sending it uniquely, carefully crafted MIDI messages, we're going to wait for the demo to finish, and we'll continue. 43 00:03:04,260 --> 00:03:06,960 So what is this talk going to be about? 44 00:03:06,960 --> 00:03:09,240 It's going to be about hardware reverse engineering. 45 00:03:09,240 --> 00:03:11,480 We're going to be dealing with an undocumented chip. 46 00:03:11,480 --> 00:03:15,340 We're going to be accessing its debug interfaces. 47 00:03:15,340 --> 00:03:17,360 We're going to be reversing some schematics. 48 00:03:18,120 --> 00:03:20,400 We're going to be doing firmware reverse engineering. 49 00:03:20,400 --> 00:03:27,680 We're going to find out how to look for the stuff that we need and make sense of disassembled and decompiled code. 50 00:03:27,680 --> 00:03:35,640 And of course, we're going to find a backdoor in the firmware and exploit it to see this cool thing. 51 00:03:35,640 --> 00:03:44,220 And of course, it's about a story that begins with me having this synthesizer, this dirty instrument. 52 00:03:45,120 --> 00:04:00,080 Well, it got dirty over the years of me using it, so I decided it's probably best to tear it apart, tear it down and clean it, and find out what it makes it tick while we're at it. 53 00:04:00,200 --> 00:04:04,200 So this is the main board of the synthesizer. 54 00:04:04,200 --> 00:04:15,720 It's got a few large chips, and just by googling the part numbers or by guessing simply, these are the functions that these chips perform. 55 00:04:15,720 --> 00:04:19,680 So these two on the bottom are flash chips. 56 00:04:19,820 --> 00:04:21,800 This is a USB host controller. 57 00:04:21,800 --> 00:04:27,480 You can plug in a flash drive into this USB host and that chip handles that. 58 00:04:27,480 --> 00:04:29,600 This is the RAM. 59 00:04:30,320 --> 00:04:32,240 And this is the LCD controller. 60 00:04:32,240 --> 00:04:36,960 There's a 16 megabytes flash for what I'm presuming is the firmware. 61 00:04:36,960 --> 00:04:42,520 And if I remember correctly, a 2 megabyte flash for user data. 62 00:04:42,900 --> 00:04:44,520 They are separate. 63 00:04:45,440 --> 00:04:49,320 And this is the largest chip. 64 00:04:49,320 --> 00:04:50,840 This is probably the CPU. 65 00:04:52,720 --> 00:04:58,320 That main chip is labeled Yamaha SW-L01U. 66 00:04:58,320 --> 00:05:04,540 And of course, this synthesizer is made by Yamaha, so I guess this is a proprietary chip. 67 00:05:04,540 --> 00:05:07,860 Also, there is absolutely no information about it online. 68 00:05:07,860 --> 00:05:16,920 If you google that part number, the only thing that comes up is an AliExpress listing for it, if you want to buy one. 69 00:05:16,920 --> 00:05:18,760 But absolutely no documentation. 70 00:05:18,760 --> 00:05:23,960 So it's gonna be quite hard to approach it without documentation. 71 00:05:23,960 --> 00:05:33,580 Also, I stumbled upon an article that suggested that it might be based on a Super H architecture from Hitachi. 72 00:05:33,580 --> 00:05:35,720 And this was my first time hearing about it. 73 00:05:35,720 --> 00:05:38,340 So of course, I don't know the architecture. 74 00:05:38,340 --> 00:05:39,800 I have no documentation. 75 00:05:40,340 --> 00:05:43,700 I'm not gonna even try to access it, you know. 76 00:05:43,700 --> 00:05:49,900 So I just put it back together, finished my cleaning, and call it a day. 77 00:05:49,900 --> 00:05:54,860 Until two years later, I found a service manual for a similar synthesizer. 78 00:05:54,860 --> 00:05:57,620 The E443, not the E433. 79 00:05:58,640 --> 00:06:00,460 They're quite similar. 80 00:06:00,460 --> 00:06:02,820 They have the same external appearance. 81 00:06:02,820 --> 00:06:09,440 They have the same board layout, essentially the same chips, including the CPU. 82 00:06:09,860 --> 00:06:14,400 But the later model has an audio input, while mine doesn't. 83 00:06:14,400 --> 00:06:19,940 And also it has an ADC analog-to-digital converter, while mine doesn't. 84 00:06:21,040 --> 00:06:26,360 Also, component numbering is different, but not too big of an issue. 85 00:06:27,000 --> 00:06:37,400 The two most important things that this service manual has are full schematics and full board layouts for all the boards. 86 00:06:37,400 --> 00:06:38,120 That's nice. 87 00:06:38,120 --> 00:06:38,880 That's the first thing. 88 00:06:38,880 --> 00:06:42,220 And the second thing is pinouts of all of the chips. 89 00:06:42,260 --> 00:06:48,400 And for most of the chips, I was able to google their part numbers and find out their pinouts. 90 00:06:48,940 --> 00:06:53,800 And usually they are standardized, like in the case of flash chips. 91 00:06:53,840 --> 00:06:57,480 But it also had a full pinout of the CPU. 92 00:06:57,480 --> 00:07:06,580 So here on the slide, you can see that page with the pinout of the CPU, that complete 176-pin chip. 93 00:07:06,580 --> 00:07:15,620 These pieces of information are very helpful to us, as a lot can be deduced about the chip just from its pinout and the schematic that it's in. 94 00:07:15,860 --> 00:07:19,260 So how can we approach this chip? 95 00:07:19,560 --> 00:07:29,200 Well, when reversing, I think that it is helpful to look at it, to think that you're reversing from the perspective of a forward engineer. 96 00:07:29,300 --> 00:07:38,000 As forward engineers, we typically want to have some kind of debugging for our program and some text output for logs. 97 00:07:38,000 --> 00:07:42,900 And in the hardware world, there are common interfaces for those. 98 00:07:42,900 --> 00:07:48,580 For debugging, that would be JTAG or SWG for ARM chips, and so on. 99 00:07:48,960 --> 00:07:51,140 There are lots of those interfaces, actually. 100 00:07:51,140 --> 00:07:54,180 But JTAG is by far the most common. 101 00:07:54,440 --> 00:07:59,220 And for simple text output for logs, for example, it would be UART. 102 00:07:59,780 --> 00:08:05,520 I decided to start with UART because I figured that accessing the logs would be easier. 103 00:08:05,780 --> 00:08:09,660 But spoiler alert, no, it wasn't. 104 00:08:10,040 --> 00:08:16,760 In total, there are five UART-related pins on this chip per the pinout. 105 00:08:17,200 --> 00:08:23,120 Two transmit pins, two receive pins, and one optional clock pin. 106 00:08:23,120 --> 00:08:29,620 So, of those five pins, only the transmit and receive pins are of interest to us. 107 00:08:29,620 --> 00:08:36,820 And of those, are only the transmit pins of interest to us, because we just want to access the logs. 108 00:08:36,820 --> 00:08:45,660 And of those four pins, three pins are just used as general-purpose IOs, not for UART. 109 00:08:45,660 --> 00:08:51,080 And the remaining fourth pin is a transmit pin, and it was completely silent. 110 00:08:51,080 --> 00:08:57,080 No matter how times I rebooted the device, it stayed completely silent, no matter what I did to it. 111 00:08:59,280 --> 00:09:04,800 But we shouldn't give up so easily, because I found two other very interesting pins. 112 00:09:04,800 --> 00:09:08,540 One was named PROTN, and one was named TESTN. 113 00:09:08,540 --> 00:09:21,620 And they stood for prototype mode and test mode, so I figured I should try looking at the UART in all four combinations of those two pins, and no success. 114 00:09:22,280 --> 00:09:25,040 It still remained silent in all four modes. 115 00:09:26,100 --> 00:09:28,740 So, let's move on to debugging then. 116 00:09:29,420 --> 00:09:34,380 Like I said, JTAG is the most common debugging interface, and there it is on our chip. 117 00:09:35,760 --> 00:09:38,960 Those five pins make up the JTAG interface. 118 00:09:38,960 --> 00:09:45,860 And one thing about JTAG is that it is very abstract, but although it is very common, it is also very abstract. 119 00:09:46,960 --> 00:09:57,480 You can connect to this JTAG interface, but unless you have a precise description, the internals of the chip in a special language, you will not be able to do anything with it. 120 00:09:57,480 --> 00:10:09,200 Of course, there are such descriptions for popular CPU architectures, but unless you know definitively what architecture it is based on, you cannot access it. 121 00:10:09,520 --> 00:10:12,560 And there is a tiny, tiny, tiny risk. 122 00:10:12,560 --> 00:10:16,700 I don't want to scare you, even though I scared myself. 123 00:10:16,760 --> 00:10:23,900 There's a tiny, tiny chance that if you use the wrong file, you may irreversibly damage the device. 124 00:10:23,900 --> 00:10:28,860 But it is only applies to very, very rare cases. 125 00:10:29,100 --> 00:10:31,920 So, don't listen to me, okay? 126 00:10:33,400 --> 00:10:44,660 The thing about JTAG is that, like I said, it's very abstract, and it says that there might be a 32-bit register called an ID code. 127 00:10:45,280 --> 00:10:53,180 The specification does not say that it has to exist, and doesn't say that if it does exist, where you can find it. 128 00:10:53,180 --> 00:10:55,520 But if it does exist, it must be 32-bit. 129 00:10:55,520 --> 00:10:58,320 That's the only thing that the specification says. 130 00:10:59,040 --> 00:11:05,140 Fortunately for us, there are conventions on how to find that register, and software will do that for you. 131 00:11:05,140 --> 00:11:07,580 We'll look at that in a minute. 132 00:11:07,860 --> 00:11:09,700 So, let's talk tools. 133 00:11:09,700 --> 00:11:11,520 You need basically two tools. 134 00:11:11,520 --> 00:11:19,700 One, you need a hardware debug probe that is an adapter between your computer and the JTAG interface on your device. 135 00:11:19,700 --> 00:11:21,760 That's called also a JTAG cable. 136 00:11:21,760 --> 00:11:22,900 There are a lot of them. 137 00:11:22,900 --> 00:11:25,620 You can see them listed on the slide if you're interested. 138 00:11:25,900 --> 00:11:29,780 And you need one specific software tool called OpenOSD. 139 00:11:29,780 --> 00:11:32,620 It is basically a driver for your JTAG adapter. 140 00:11:32,620 --> 00:11:39,460 It is an adapter between the JTAG adapter and GDB, or your favorite debugger of choice. 141 00:11:39,540 --> 00:11:53,140 So, I just connected a Chinese knockoff J-Link adapter to my precious board and launched OpenOSD because it reports the ID code by default if it is able to find it. 142 00:11:53,280 --> 00:11:55,860 So, here's the configuration for OpenOSD. 143 00:11:56,300 --> 00:11:58,360 Here's the logs from the OpenOSD. 144 00:11:58,360 --> 00:12:00,540 I highlighted the important parts in red. 145 00:12:00,540 --> 00:12:05,820 And one, it says that auto-probing might not work because of the stuff that I mentioned earlier. 146 00:12:05,820 --> 00:12:13,960 And second, that it was able, it was successful in finding a device with an ID code of 3F0F0F0F. 147 00:12:13,960 --> 00:12:17,400 And that ID code is suspiciously pretty. 148 00:12:17,400 --> 00:12:18,360 It is very pretty. 149 00:12:18,360 --> 00:12:21,340 I triple-checked, quadruple-checked my wiring. 150 00:12:21,340 --> 00:12:27,040 I quadruple-checked my config and power supplies and everything. 151 00:12:27,040 --> 00:12:29,680 And the ID code was the same. 152 00:12:29,680 --> 00:12:32,800 So, I figured that must be it. 153 00:12:32,800 --> 00:12:44,640 So, I googled that ID code and it belonged to a lot of devices from different manufacturers, including Atmo, Analog Devices, and STMicroelectronics. 154 00:12:44,640 --> 00:12:52,220 And the common thing between all of those devices was that they were all based on an ARM7 core. 155 00:12:52,580 --> 00:12:57,240 And a footnote, ARM7 is not the same thing as ARMv7. 156 00:12:58,320 --> 00:13:02,020 If I recall correctly, ARM7 is ARMv5. 157 00:13:02,020 --> 00:13:02,700 I'm not sure. 158 00:13:02,700 --> 00:13:03,940 Don't quote me on that. 159 00:13:04,420 --> 00:13:09,800 But let's try telling OpenOCD that we're dealing with an ARM7 device. 160 00:13:10,180 --> 00:13:11,120 So, I did. 161 00:13:11,380 --> 00:13:14,620 These two red lines in the config are new ones. 162 00:13:14,800 --> 00:13:20,640 And OpenOCD didn't give us any errors and told us that it is listening for GDB connections. 163 00:13:20,640 --> 00:13:23,540 So, let's try connecting with GDB. 164 00:13:24,380 --> 00:13:32,440 And from my point of view, this is a success because of three things. 165 00:13:32,440 --> 00:13:39,580 First, my board was connected to a lab bench power supply, which was reporting the current draw to me. 166 00:13:39,580 --> 00:13:51,200 And when I stopped program execution in GDB by pressing control C, the current draw dropped from 114 to 98 milliamps. 167 00:13:51,920 --> 00:13:57,780 And vice versa, when I continued the execution by the command C, it rose again to 114. 168 00:13:57,780 --> 00:13:59,140 That's the first thing. 169 00:13:59,140 --> 00:14:03,020 The second thing is that the interrupt vectors all made sense. 170 00:14:03,160 --> 00:14:06,760 If you don't know what those are, we're going to talk about that in a minute. 171 00:14:07,800 --> 00:14:14,280 And, well, they are ARM instructions and they are jumps, which I would expect them to be. 172 00:14:14,320 --> 00:14:17,160 And the third thing is that the addresses made sense. 173 00:14:17,160 --> 00:14:26,840 When I randomly stopped execution, when the synth was just running normally, the addresses were contained to a neighboring... 174 00:14:26,840 --> 00:14:28,900 they were neighbors, basically. 175 00:14:30,100 --> 00:14:37,040 Those three things indicated to me that we successfully gained debug access on the chip. 176 00:14:37,120 --> 00:14:38,740 It was that easy. 177 00:14:40,000 --> 00:14:49,920 Unfortunately, it isn't usually that easy because... well, in our case, it was because a synthesizer, I imagine, is not a high security device. 178 00:14:49,920 --> 00:14:53,500 But if you're dealing with something like that, it's not going to be that easy. 179 00:14:53,500 --> 00:15:01,800 You're going to be using much more advanced hardware exploits to first unlock the debug interface and then access it. 180 00:15:01,800 --> 00:15:06,340 But provided we have done that, we can now extract the firmware. 181 00:15:06,400 --> 00:15:09,800 So for that, we need to look at the reset vectors. 182 00:15:09,800 --> 00:15:12,080 I'm sorry, at the interrupt vectors. 183 00:15:12,740 --> 00:15:20,100 Like I promised, in almost all modern computer architectures, there's a thing called an interrupt. 184 00:15:20,100 --> 00:15:26,580 And it is an extraordinary event that must interrupt the program immediately. 185 00:15:27,180 --> 00:15:40,220 It may be an external hardware request, an external chip requiring immediate attention of the code, or a software fault, or a hardware fault, or something like that. 186 00:15:40,220 --> 00:15:44,480 A division by zero is an exception, which is an interrupt, for example. 187 00:15:45,280 --> 00:15:51,000 And whenever an interrupt happens, the CPU executes the interrupt vector. 188 00:15:51,260 --> 00:16:05,100 And in ARM7, the interrupt vector table is located at address zero, basically consists of a list of ARM instructions that the CPU executes whenever a specific interrupt fires. 189 00:16:05,100 --> 00:16:16,080 And in embedded development, unlike, you know, big computer development, desktop computer OS development, this vector table is usually read-only. 190 00:16:16,080 --> 00:16:19,240 So it is usually placed in the flash memory. 191 00:16:19,780 --> 00:16:27,620 So if the vector table starts at address zero, it only made sense to me that a flash must also start at address zero. 192 00:16:27,620 --> 00:16:32,380 And I know from the markings on the flash chip that it is 16 megabytes in size. 193 00:16:32,380 --> 00:16:37,820 So I decided to dump 16 megabytes starting from address zero into a file. 194 00:16:38,220 --> 00:16:42,420 Immediately, I thought that the result was good. 195 00:16:43,680 --> 00:16:49,060 Well, when reversing, it is helpful to look at the strings in the file that you're reversing. 196 00:16:49,080 --> 00:16:56,760 Any decent reverse engineering tool will search for the human readable strings in the file that you've given it. 197 00:16:57,520 --> 00:17:05,420 And they are very helpful because they are immediately recognizable and understandable, unlike machine code to us humans. 198 00:17:05,800 --> 00:17:17,100 So the particular string that told me that I was on the right track was SWL01U internal, which is a very good sign. 199 00:17:17,100 --> 00:17:25,000 But a bad sign was that all of those strings repeated every 64 kilobytes with the same offset. 200 00:17:25,000 --> 00:17:31,980 So every string was repeated 256 times, evenly spaced throughout the image that I've dumped. 201 00:17:33,040 --> 00:17:43,820 And this hints to me that I've captured some memory, 64 kilobytes in size, that is located at address zero. 202 00:17:43,820 --> 00:17:54,060 And because of the string internal, I guessed that it might be the internal read-only memory inside of the chip. 203 00:17:54,780 --> 00:18:03,700 And if we look at the reset vector, which is an interrupt vector for a reset, i.e. 204 00:18:03,700 --> 00:18:17,180 when the CPU first powers up or is rebooted, that vector jumps to address 02000000, which I figured must be the flash this time actually for real. 205 00:18:18,040 --> 00:18:24,520 So I dumped 16 megabytes starting from there, and the data there made even more sense. 206 00:18:24,680 --> 00:18:36,740 I saw lots of strings that I recognized just from using the instrument, like voice names, UI messages, and et cetera. 207 00:18:37,140 --> 00:18:53,300 And when I had played the binary fur image back in Audacity, I heard at specific points a piano and strings and a guitar, which are samples that the synthesizer uses, I guess, which was very cool. 208 00:18:53,300 --> 00:18:55,780 It definitely meant that I was on the right track. 209 00:18:55,960 --> 00:18:59,100 So let's play Minesweeper. 210 00:18:59,700 --> 00:19:03,240 This is relevant to the topic of my talk, by the way. 211 00:19:03,240 --> 00:19:08,340 I think that reverse engineering is very similar to Minesweeper. 212 00:19:09,260 --> 00:19:13,940 When you play Minesweeper, you start with an empty field. 213 00:19:13,940 --> 00:19:16,340 You don't know the states of any of the cells. 214 00:19:17,200 --> 00:19:25,600 And first you click a random cell that by the rules of the game must not have a mine in it. 215 00:19:25,640 --> 00:19:31,580 And as you do that, you automatically discover the states of neighboring cells. 216 00:19:31,720 --> 00:19:36,400 And of course, you can deduce which state, which cells must have a mine. 217 00:19:36,400 --> 00:19:39,540 You can flag them, not to accidentally click them. 218 00:19:39,540 --> 00:19:45,400 And you repeat this until you've discovered everything there is to discover on the board. 219 00:19:45,720 --> 00:19:49,620 And I think that reverse engineering is very similar. 220 00:19:49,620 --> 00:19:54,460 You start without knowing what any of the functions or any instructions do. 221 00:19:54,800 --> 00:20:01,460 You have an entry point or multiple entry points that you can understand the meaning of as they are. 222 00:20:01,460 --> 00:20:06,740 So for example, a reset vector or the human readable strings. 223 00:20:06,800 --> 00:20:09,300 Those are very useful entry points. 224 00:20:09,300 --> 00:20:20,880 And when you look at the functions that use these strings, you learn something about them. 225 00:20:20,880 --> 00:20:28,720 And once you learn something about a function, you know more about the functions that call it and more functions that are called by it. 226 00:20:28,720 --> 00:20:32,900 And this knowledge propagates through the entire binary image. 227 00:20:32,980 --> 00:20:43,140 But this process is unfortunately quite boring, and I won't present the entirety of it, the many sleepless nights that I've spent discovering all this. 228 00:20:43,140 --> 00:20:54,280 I will, however, present you one particular subsystem in the forever, and that is relevant to this exploit, the shell. 229 00:20:56,800 --> 00:21:10,300 And when looking at a string section in Ghidra, which is my RE tool of choice, I started out with Cutter, but I switched to Ghidra eventually, and I like it. 230 00:21:10,800 --> 00:21:17,340 There are a lot of strings that hint to me that they might be some kind of list of commands for a shell. 231 00:21:17,340 --> 00:21:26,380 For example, help?info, ver, perf, on, perf, off, perfdisp, memory reads, memory writes, maybe. 232 00:21:26,420 --> 00:21:28,440 They look like GDB commands. 233 00:21:28,440 --> 00:21:31,480 G, D, S, M, M, B, M, W, M, L. 234 00:21:32,740 --> 00:21:38,000 This is syntax similar to GDB syntax, so maybe they are, maybe they're not. 235 00:21:38,820 --> 00:21:41,860 And this is how all of this data links together. 236 00:21:41,860 --> 00:21:52,140 There's a command table, which consists of a list of pairs of a command name and a pointer to a function that handles the command. 237 00:21:53,800 --> 00:22:01,220 The handler function for the commands help and question mark is the same, and it uses the command table. 238 00:22:01,560 --> 00:22:10,140 And that command table is also used by a separate function, an input handler, which is somehow getting its input from the outside world. 239 00:22:10,180 --> 00:22:16,300 And all those handler functions print stuff using the same set of functions. 240 00:22:16,300 --> 00:22:19,580 So there's the main one, print, which just prints a string. 241 00:22:19,700 --> 00:22:32,640 There are also functions that print predefined strings, for example, print prompt prints a greater than character together with a space, or print okay, which just prints okay. 242 00:22:32,700 --> 00:22:44,080 And I'm not sure yet how the input handler gets its data from the outside or how the print function gets get its data to the outside, but we'll figure that out. 243 00:22:44,940 --> 00:22:51,440 In the meantime, I want to show the input handler because it is very funny. 244 00:22:51,440 --> 00:22:55,960 It is essentially a state machine with three states 0, 1, and 2. 245 00:22:56,040 --> 00:23:00,640 When it is in state 0, it asks for a password. 246 00:23:01,260 --> 00:23:12,260 If we type the string login, if it is in state 1, it expects to see that password and transitions us to state 2 if it is correct, otherwise back to state 0. 247 00:23:12,260 --> 00:23:14,580 And in state 2, it executes our command. 248 00:23:14,580 --> 00:23:31,260 So we know that in order to run any commands with the shell, we first need to type in the string login, followed by the password, hashtag 000, which is hard-coded in the code, which I thought was funny. 249 00:23:31,260 --> 00:23:39,820 So let's list out potential candidates for the interfaces that the shell might be running on top of. 250 00:23:39,820 --> 00:23:41,660 First off, UART. 251 00:23:41,660 --> 00:23:54,640 We've looked at UART before, and both receive pins were used up, and one of the transmit pins was used up, and the remaining transmit pin was completely silent. 252 00:23:55,640 --> 00:23:58,540 So definitely not Harvard UART. 253 00:23:58,820 --> 00:23:59,940 Implemented in the chip. 254 00:23:59,940 --> 00:24:01,660 It could be software UART. 255 00:24:02,520 --> 00:24:08,260 But in any case, UART is ready to transmit bytes as is. 256 00:24:08,480 --> 00:24:11,100 They do not require great manipulation. 257 00:24:11,340 --> 00:24:19,120 So if I look in that print function, I expect to see just plain data copying or something like that. 258 00:24:19,180 --> 00:24:24,020 Or maybe a loop waiting for a bit in a register to clear, something like that. 259 00:24:25,200 --> 00:24:42,620 And this could be JTAG, the ARM7 specification that says that there is something called the DCC, debug communications channel, which is a bidirectional channel that lets a program running on the chip and a debugger on the computer to exchange arbitrary data. 260 00:24:42,940 --> 00:24:46,080 And that DCC uses special instructions. 261 00:24:46,080 --> 00:24:51,560 So if I look in that print function and it uses those special instructions, then it's DCC. 262 00:24:51,560 --> 00:25:01,440 And it could be USB, but if we print out the descriptor for the USB interface, we only see MIDI. 263 00:25:01,720 --> 00:25:08,480 We don't see a serial port, a virtual serial port, or virtual networking, or anything like that, just MIDI. 264 00:25:08,480 --> 00:25:10,740 So it's unlikely to be USB. 265 00:25:11,100 --> 00:25:12,980 And it's unlikely to be UART. 266 00:25:12,980 --> 00:25:18,980 Probably use JTAG, but we have to look in that shell print function to find out. 267 00:25:18,980 --> 00:25:22,680 And that print function does a lot of stuff. 268 00:25:22,680 --> 00:25:31,520 So if we feed that function the string hello world, of course, it is represented as ASCII, those hex bytes. 269 00:25:31,600 --> 00:25:35,460 What it's doing is it's taking eight byte chunks. 270 00:25:35,680 --> 00:25:38,380 I'm sorry, I highlighted seven bytes. 271 00:25:38,380 --> 00:25:40,460 Eight byte chunks of the input string. 272 00:25:40,460 --> 00:25:49,420 It is taking the upper and lower half of each byte in that chunk, and it is placing that into its separate byte on the output. 273 00:25:49,420 --> 00:25:53,700 So this 48 is translated into 0408. 274 00:25:53,980 --> 00:25:57,060 That is done for every byte in the chunk. 275 00:25:57,060 --> 00:26:05,280 And then a fixed eight byte header is appended, and a fixed one byte footer is appended. 276 00:26:06,940 --> 00:26:12,840 So string hello world is transmitted as these two blocks. 277 00:26:13,560 --> 00:26:30,760 And immediately, if we look at that last block, that second block, immediately the first and last bytes of that block rang a bell, because these look to me like MIDI system exclusive message markers. 278 00:26:31,160 --> 00:26:38,380 The MIDI specification says that a system exclusive message starts with an F0 and ends with an F7. 279 00:26:38,440 --> 00:26:50,540 And it is used, it can be used by manufacturers to define a system exclusive message, so a message that is exclusive to a particular manufacturer. 280 00:26:50,540 --> 00:26:58,320 In order to identify that manufacturer, an ID is assigned to them, either one byte or three bytes. 281 00:26:58,640 --> 00:27:04,100 And if so, that 43 there must be the manufacturer ID according to the specification. 282 00:27:04,300 --> 00:27:10,760 And in fact, if we look at what ID that corresponds to, it is Yamaha. 283 00:27:11,140 --> 00:27:15,480 And my synthesizer is made by Yamaha, so that makes sense. 284 00:27:15,480 --> 00:27:32,380 So I'm guessing those six remaining bytes in the, those six remaining bytes in the fixed eight byte header are some kind of Yamaha specific shell signature that says that the rest of the data in the packets is shell data. 285 00:27:32,380 --> 00:27:40,820 And one more thing about why it is breaking up the bytes into two bytes. 286 00:27:40,880 --> 00:27:50,580 It is because MIDI does not permit data to be transmitted if it has the highest bit set to one. 287 00:27:50,580 --> 00:27:57,720 Because any byte that is sent over MIDI with the highest bit sent to one is a status byte. 288 00:27:57,720 --> 00:28:06,200 So for example, F0 and F7 are status bytes, the rest of them are data bytes, and they are not allowed to have the highest bit set to zero. 289 00:28:06,200 --> 00:28:14,380 So they figured they might want at some point in the future to transmit maybe UTF-8 over the shell. 290 00:28:14,380 --> 00:28:24,640 So they must future-proof by breaking up the bytes into four bits so that the highest bit is always guaranteed to be zero. 291 00:28:25,820 --> 00:28:31,440 We know the format of outgoing messages, but we don't yet know the format of incoming messages. 292 00:28:31,440 --> 00:28:43,840 But I decided to make a little Python script that sends data to the synthesizer in the same format that expects them to arrive, just to see if that works. 293 00:28:43,840 --> 00:28:45,720 And well, it did. 294 00:28:45,720 --> 00:28:48,640 Here's the shell. 295 00:28:48,640 --> 00:28:51,500 Here's that Python script that is accessing a shell. 296 00:28:51,500 --> 00:28:52,780 We say login. 297 00:28:52,780 --> 00:28:55,640 It is asking us for a password, which we type in. 298 00:28:55,640 --> 00:28:56,200 It says login. 299 00:28:56,200 --> 00:28:57,120 Okay. 300 00:28:57,120 --> 00:28:58,320 We rank help. 301 00:28:58,320 --> 00:29:03,320 And ladies and gentlemen, we got it. 302 00:29:03,320 --> 00:29:04,900 We got the shell. 303 00:29:06,360 --> 00:29:13,940 And like I mentioned, a couple of commands look to me like they were memory write commands. 304 00:29:13,940 --> 00:29:15,880 So we can exploit that. 305 00:29:16,100 --> 00:29:24,880 If I execute this ML command with an address in hexadecimal and a data of word 32-bit word in hexadecimal. 306 00:29:24,880 --> 00:29:29,960 And then with my debugger, I look at that memory location. 307 00:29:29,960 --> 00:29:34,200 I see the value that I want written there. 308 00:29:34,200 --> 00:29:37,120 And that address is in RAM. 309 00:29:37,120 --> 00:29:38,680 So it is rewritable. 310 00:29:38,680 --> 00:29:40,820 And that is very good. 311 00:29:40,880 --> 00:29:48,320 That means that we can override the memory of this synthesizer via just many messages. 312 00:29:48,320 --> 00:30:03,820 We do not have to have a JTAG connected to it, which means that we can write a malicious payload for this synthesizer in ARM assembly that is asking the firmware to write a string on the display. 313 00:30:03,820 --> 00:30:11,700 We can assemble it into binary data, but then transform this binary data into write commands for the shell. 314 00:30:11,700 --> 00:30:22,220 We can then transform those write commands into system-exclusive messages that the synthesizer expects, write them into a MIDI file, and just play back that MIDI file. 315 00:30:22,220 --> 00:30:25,220 And we see the string hello world on display. 316 00:30:25,280 --> 00:30:47,160 Of course, one part I forgot to mention is that that last one, that last memory write command is a overwrite of the return address on the stack, which is binary exploitation 101, except we don't have to find any buffer overflow vulnerability or anything like that. 317 00:30:47,160 --> 00:30:50,980 The memory write commands are like, they are right there. 318 00:30:51,240 --> 00:31:12,260 One thing we could also do is make a Python script that takes in an innocent MIDI file with music and a list of scary stuff to print on the display, and it spits out a scary MIDI file that plays scary music and displays scary stuff on the display. 319 00:31:12,260 --> 00:31:14,960 And it looks like follows. 320 00:31:43,710 --> 00:31:48,890 So, how can we exploit all this to play Bad Apple on the display? 321 00:31:49,470 --> 00:31:53,750 How can we use a textual display to display graphics? 322 00:31:53,810 --> 00:32:00,510 Well, turns out we can't really, because like I said, this display controller is only designed to display text. 323 00:32:00,510 --> 00:32:08,190 It is designed for two lines of eight characters each, and each character is five pixels wide by eight pixels tall. 324 00:32:08,190 --> 00:32:13,830 But if you look at the display closely, it has a lot of segments, non-textual segments. 325 00:32:13,830 --> 00:32:26,370 So, for example, here's the text area, and here's a custom seven segment area, and here's also a segment area, and this is a very custom area with the keys of the synthesizer. 326 00:32:26,370 --> 00:32:32,470 And this is an area with the notation, and the synth status, and all this other crap. 327 00:32:33,030 --> 00:32:43,270 How is the synth accessing those segments and manipulating them in a custom way if the LCD controller only supports text? 328 00:32:43,270 --> 00:32:49,730 Well, this is the LCD grid as the controller expects to see it. 329 00:32:49,730 --> 00:32:55,130 It is basically 40 segments per line arranged in 16 lines. 330 00:32:55,830 --> 00:33:01,910 And this is what the synthesizer is using it as. 331 00:33:01,910 --> 00:33:05,210 It is using the top row as text, as intended. 332 00:33:05,310 --> 00:33:12,510 On the bottom row, every segment in the virtual bottom row is wired to an actual segment on the display. 333 00:33:12,790 --> 00:33:15,550 And what I drew here is non-garbage. 334 00:33:16,130 --> 00:33:25,530 This is actually... when I copy the data that is displayed in the bottom row to the top row, this is what the display tells me. 335 00:33:26,250 --> 00:33:32,890 So, somehow the CPU is telling the LCD controller to display custom graphics. 336 00:33:33,470 --> 00:33:35,630 And how does it do that? 337 00:33:35,630 --> 00:33:39,550 And if it does do that, how can we do the same? 338 00:33:40,590 --> 00:33:46,710 Well, here is a simplified schematic diagram of the LCD controller, with the powers that matter to us. 339 00:33:46,710 --> 00:33:50,390 The most important memory is the display data RAM, the DRAM. 340 00:33:50,710 --> 00:33:56,070 It is holding the ASCII characters that are currently displayed on screen. 341 00:33:56,070 --> 00:33:59,190 And there's... the second most important memory is CGROM. 342 00:33:59,190 --> 00:34:02,930 It is translating ASCII characters into actual graphics. 343 00:34:03,130 --> 00:34:15,830 So, we have to only load ASCII once into the DDRAM, and the display will keep updating itself without any iteration from the CPU using that CGROM. 344 00:34:15,830 --> 00:34:17,770 There's also the CGRAM. 345 00:34:17,770 --> 00:34:35,090 We can load custom character data into the CGRAM, up to 8 custom characters, 5 pixels by 8 pixels, and it will display them if we tell it to display characters numbered 0 through 7 or 8 through 15. 346 00:34:35,090 --> 00:34:38,930 They refer to the same area in the CGRAM. 347 00:34:40,250 --> 00:34:45,750 And in order to play a video, this is a frame from Bad Apple. 348 00:34:45,750 --> 00:34:50,350 First, our display is 40 by 16 pixels. 349 00:34:50,350 --> 00:35:02,330 So, we... first, we take a slim window of the video, and we break it up into 8 portions of 5 by 8 pixels. 350 00:35:02,330 --> 00:35:15,690 And we tell the DDRAM to display those custom characters once, and then we constantly update the RAM of the LCD controller to basically play a video. 351 00:35:16,330 --> 00:35:23,550 But the synthesizer fights back, of course, because it's got its own custom stuff to be displayed in its custom segments. 352 00:35:23,810 --> 00:35:26,290 So, I'm not going to go into detail here. 353 00:35:26,290 --> 00:35:28,230 Unfortunately, I don't have enough time. 354 00:35:28,230 --> 00:35:33,190 But, essentially, there's a real-time operating system that is running on this synthesizer. 355 00:35:33,330 --> 00:35:40,790 And it's got, essentially, a task table which lists out all the tasks that are running on the synthesizer. 356 00:35:40,790 --> 00:35:51,690 And if you make a copy of that table in RAM and overwrite specific entries, you can effectively disable that part of the firmware without actually overwriting the flash chip. 357 00:35:51,710 --> 00:36:03,950 And I don't want to overwrite the flash chip, because if I do something wrong, I want to be able to just unplug the synthesizer from the wall and plug it back in, and it continues to work normally. 358 00:36:04,730 --> 00:36:12,330 So, this is, essentially, how to play Bad Apple on the synthesizer. 359 00:36:12,870 --> 00:36:15,310 And a little addendum. 360 00:36:15,630 --> 00:36:35,390 After I published the original article online, a lot of people online tested this exploit, this shell exploit, and it turns out the range of Yamaha devices susceptible to this attack is very wide, with the earliest product being the E433, the one that I have, 361 00:36:35,390 --> 00:36:42,990 released in 2012, and the latest one is E473, released in 2022. 362 00:36:42,990 --> 00:36:48,270 And this expands beyond just educational synthesizer. 363 00:36:49,150 --> 00:36:58,730 The DGX digital piano, released in 2016, has the same shell, the same passwords, hashtag 000. 364 00:36:59,570 --> 00:37:17,210 It's also possible that Yamaha drums, namely model P125, is susceptible to it because the same strings were found in its firmware image, even though the person that found it didn't have the device on hand. 365 00:37:17,210 --> 00:37:24,630 Shout out to these guys, by the way, MarisaChan, Curler, RPL1846, and Cintra. 366 00:37:25,110 --> 00:37:38,870 There's an ongoing discussion on GitHub about those things, so I think it's safe to say that basically every Yamaha product has this shell, which is vulnerable to arbitrary memory rights. 367 00:37:39,650 --> 00:37:43,050 This is a big issue, I think. 368 00:37:44,110 --> 00:37:47,510 And a little summary on the DSP. 369 00:37:47,510 --> 00:37:51,990 So, in this chip, there's a DSP that is separate from the ARM core. 370 00:37:51,990 --> 00:37:53,410 It is running independently. 371 00:37:53,410 --> 00:37:58,390 Whenever I pause the ARM core, this DSP continues to produce sound. 372 00:37:58,870 --> 00:38:06,670 And it uses memory map register starting at address FE0000000, six zeros. 373 00:38:07,130 --> 00:38:10,810 There are two areas, as hex 400 bytes in size. 374 00:38:10,810 --> 00:38:12,370 There's a global configuration area. 375 00:38:12,370 --> 00:38:14,450 There's a voice parameter area. 376 00:38:14,450 --> 00:38:16,570 There are 32 voices. 377 00:38:16,570 --> 00:38:19,570 There are 16 16-bit parameters per voice. 378 00:38:19,570 --> 00:38:22,670 You can look the formulas up on slides. 379 00:38:24,150 --> 00:38:36,530 If you're interested in details, here's the list of voice parameters that I was able to figure out the meaning of the pitch register, the decay, attack, and volume and panning register. 380 00:38:36,530 --> 00:38:40,830 And here's one global register that I was able to figure out the meaning of. 381 00:38:40,830 --> 00:38:42,090 Again, you can look at the slide. 382 00:38:42,090 --> 00:38:48,770 This is very boring, but nevertheless important, I think. 383 00:38:48,770 --> 00:38:50,270 And here's the list of references. 384 00:38:50,670 --> 00:38:57,890 The article from before that mentioned this chip might be based on SuperH, even though it's not entirely correct. 385 00:38:57,890 --> 00:39:00,930 There's also lots of useful info on that website. 386 00:39:00,930 --> 00:39:02,010 Go read it. 387 00:39:02,770 --> 00:39:08,190 There's the MIDI specification, the ARM documentation, and that's it. 388 00:39:08,190 --> 00:39:15,590 Thank you very much, and thank you very much to Alla and Petya here behind the cameras that made this video possible. 389 00:39:15,710 --> 00:39:16,550 Goodbye!