How's everybody doing? Yay, come on. How's everybody doing? This is DefCon. Way better. Okay. Okay. So, today we have a video presentation in track two. It's going to be remote code execution by MIDI messaging. Enjoy. Hello, DefCon 33. My name is Anna Antonenko. And unfortunately, I couldn't be here with you today. Just some visa issues. So, I'm going to be presenting my talk via video. My talk is titled Remote Code Execution via MIDI Messages. It's about exploiting a vulnerability, rather a backdoor that I found in a line of Yamaha products. About some stuff about me, you can find me online on my website. I use the same nickname practically everywhere. Here's my email on the slide here. I am a forward developer at Flipper Devices. I write code in C. But my employer does not necessarily reflect my opinions. All research was done in my own time. I really like Erlang and Elixir, their languages for the Beam virtual machine. I'm working on an operating system written in Erlang in my spare time. I really like electronics, both analog and digital, but digital is my specialty. And I like music. I like writing music, I like playing music, and listening to music, of course. I prefer to think of myself as a decent forward engineer, but not a great reverse engineer. So hopefully, those of you who are like me can learn something from this talk, from this perspective. First, I'm going to show you a little demo about what we're going to be talking today. But this demo needs a bit of introduction. So there's this protocol called MIDI. It emerged in the 80s, and it's a really simple protocol. It describes music generation parameters, basically. So one device can tell another to play a note, or release a note, or change some effect parameter, or something like that. And the other device will generate audio based on that data. But apparently, even a protocol as simple as that can have backdoors. So this is a demo. 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. And just by sending it uniquely, carefully crafted MIDI messages, we're going to wait for the demo to finish, and we'll continue. So what is this talk going to be about? It's going to be about hardware reverse engineering. We're going to be dealing with an undocumented chip. We're going to be accessing its debug interfaces. We're going to be reversing some schematics. We're going to be doing firmware reverse engineering. We're going to find out how to look for the stuff that we need and make sense of disassembled and decompiled code. And of course, we're going to find a backdoor in the firmware and exploit it to see this cool thing. And of course, it's about a story that begins with me having this synthesizer, this dirty instrument. 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. So this is the main board of the synthesizer. 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. So these two on the bottom are flash chips. This is a USB host controller. You can plug in a flash drive into this USB host and that chip handles that. This is the RAM. And this is the LCD controller. There's a 16 megabytes flash for what I'm presuming is the firmware. And if I remember correctly, a 2 megabyte flash for user data. They are separate. And this is the largest chip. This is probably the CPU. That main chip is labeled Yamaha SW-L01U. And of course, this synthesizer is made by Yamaha, so I guess this is a proprietary chip. Also, there is absolutely no information about it online. If you google that part number, the only thing that comes up is an AliExpress listing for it, if you want to buy one. But absolutely no documentation. So it's gonna be quite hard to approach it without documentation. Also, I stumbled upon an article that suggested that it might be based on a Super H architecture from Hitachi. And this was my first time hearing about it. So of course, I don't know the architecture. I have no documentation. I'm not gonna even try to access it, you know. So I just put it back together, finished my cleaning, and call it a day. Until two years later, I found a service manual for a similar synthesizer. The E443, not the E433. They're quite similar. They have the same external appearance. They have the same board layout, essentially the same chips, including the CPU. But the later model has an audio input, while mine doesn't. And also it has an ADC analog-to-digital converter, while mine doesn't. Also, component numbering is different, but not too big of an issue. The two most important things that this service manual has are full schematics and full board layouts for all the boards. That's nice. That's the first thing. And the second thing is pinouts of all of the chips. And for most of the chips, I was able to google their part numbers and find out their pinouts. And usually they are standardized, like in the case of flash chips. But it also had a full pinout of the CPU. So here on the slide, you can see that page with the pinout of the CPU, that complete 176-pin chip. 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. So how can we approach this chip? 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. As forward engineers, we typically want to have some kind of debugging for our program and some text output for logs. And in the hardware world, there are common interfaces for those. For debugging, that would be JTAG or SWG for ARM chips, and so on. There are lots of those interfaces, actually. But JTAG is by far the most common. And for simple text output for logs, for example, it would be UART. I decided to start with UART because I figured that accessing the logs would be easier. But spoiler alert, no, it wasn't. In total, there are five UART-related pins on this chip per the pinout. Two transmit pins, two receive pins, and one optional clock pin. So, of those five pins, only the transmit and receive pins are of interest to us. And of those, are only the transmit pins of interest to us, because we just want to access the logs. And of those four pins, three pins are just used as general-purpose IOs, not for UART. And the remaining fourth pin is a transmit pin, and it was completely silent. No matter how times I rebooted the device, it stayed completely silent, no matter what I did to it. But we shouldn't give up so easily, because I found two other very interesting pins. One was named PROTN, and one was named TESTN. 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. It still remained silent in all four modes. So, let's move on to debugging then. Like I said, JTAG is the most common debugging interface, and there it is on our chip. Those five pins make up the JTAG interface. And one thing about JTAG is that it is very abstract, but although it is very common, it is also very abstract. 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. 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. And there is a tiny, tiny, tiny risk. I don't want to scare you, even though I scared myself. There's a tiny, tiny chance that if you use the wrong file, you may irreversibly damage the device. But it is only applies to very, very rare cases. So, don't listen to me, okay? 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. The specification does not say that it has to exist, and doesn't say that if it does exist, where you can find it. But if it does exist, it must be 32-bit. That's the only thing that the specification says. Fortunately for us, there are conventions on how to find that register, and software will do that for you. We'll look at that in a minute. So, let's talk tools. You need basically two tools. One, you need a hardware debug probe that is an adapter between your computer and the JTAG interface on your device. That's called also a JTAG cable. There are a lot of them. You can see them listed on the slide if you're interested. And you need one specific software tool called OpenOSD. It is basically a driver for your JTAG adapter. It is an adapter between the JTAG adapter and GDB, or your favorite debugger of choice. 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. So, here's the configuration for OpenOSD. Here's the logs from the OpenOSD. I highlighted the important parts in red. And one, it says that auto-probing might not work because of the stuff that I mentioned earlier. And second, that it was able, it was successful in finding a device with an ID code of 3F0F0F0F. And that ID code is suspiciously pretty. It is very pretty. I triple-checked, quadruple-checked my wiring. I quadruple-checked my config and power supplies and everything. And the ID code was the same. So, I figured that must be it. So, I googled that ID code and it belonged to a lot of devices from different manufacturers, including Atmo, Analog Devices, and STMicroelectronics. And the common thing between all of those devices was that they were all based on an ARM7 core. And a footnote, ARM7 is not the same thing as ARMv7. If I recall correctly, ARM7 is ARMv5. I'm not sure. Don't quote me on that. But let's try telling OpenOCD that we're dealing with an ARM7 device. So, I did. These two red lines in the config are new ones. And OpenOCD didn't give us any errors and told us that it is listening for GDB connections. So, let's try connecting with GDB. And from my point of view, this is a success because of three things. First, my board was connected to a lab bench power supply, which was reporting the current draw to me. And when I stopped program execution in GDB by pressing control C, the current draw dropped from 114 to 98 milliamps. And vice versa, when I continued the execution by the command C, it rose again to 114. That's the first thing. The second thing is that the interrupt vectors all made sense. If you don't know what those are, we're going to talk about that in a minute. And, well, they are ARM instructions and they are jumps, which I would expect them to be. And the third thing is that the addresses made sense. When I randomly stopped execution, when the synth was just running normally, the addresses were contained to a neighboring... they were neighbors, basically. Those three things indicated to me that we successfully gained debug access on the chip. It was that easy. 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. But if you're dealing with something like that, it's not going to be that easy. You're going to be using much more advanced hardware exploits to first unlock the debug interface and then access it. But provided we have done that, we can now extract the firmware. So for that, we need to look at the reset vectors. I'm sorry, at the interrupt vectors. Like I promised, in almost all modern computer architectures, there's a thing called an interrupt. And it is an extraordinary event that must interrupt the program immediately. 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. A division by zero is an exception, which is an interrupt, for example. And whenever an interrupt happens, the CPU executes the interrupt vector. 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. And in embedded development, unlike, you know, big computer development, desktop computer OS development, this vector table is usually read-only. So it is usually placed in the flash memory. So if the vector table starts at address zero, it only made sense to me that a flash must also start at address zero. And I know from the markings on the flash chip that it is 16 megabytes in size. So I decided to dump 16 megabytes starting from address zero into a file. Immediately, I thought that the result was good. Well, when reversing, it is helpful to look at the strings in the file that you're reversing. Any decent reverse engineering tool will search for the human readable strings in the file that you've given it. And they are very helpful because they are immediately recognizable and understandable, unlike machine code to us humans. So the particular string that told me that I was on the right track was SWL01U internal, which is a very good sign. But a bad sign was that all of those strings repeated every 64 kilobytes with the same offset. So every string was repeated 256 times, evenly spaced throughout the image that I've dumped. And this hints to me that I've captured some memory, 64 kilobytes in size, that is located at address zero. And because of the string internal, I guessed that it might be the internal read-only memory inside of the chip. And if we look at the reset vector, which is an interrupt vector for a reset, i.e. 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. So I dumped 16 megabytes starting from there, and the data there made even more sense. I saw lots of strings that I recognized just from using the instrument, like voice names, UI messages, and et cetera. 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. It definitely meant that I was on the right track. So let's play Minesweeper. This is relevant to the topic of my talk, by the way. I think that reverse engineering is very similar to Minesweeper. When you play Minesweeper, you start with an empty field. You don't know the states of any of the cells. And first you click a random cell that by the rules of the game must not have a mine in it. And as you do that, you automatically discover the states of neighboring cells. And of course, you can deduce which state, which cells must have a mine. You can flag them, not to accidentally click them. And you repeat this until you've discovered everything there is to discover on the board. And I think that reverse engineering is very similar. You start without knowing what any of the functions or any instructions do. You have an entry point or multiple entry points that you can understand the meaning of as they are. So for example, a reset vector or the human readable strings. Those are very useful entry points. And when you look at the functions that use these strings, you learn something about them. 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. And this knowledge propagates through the entire binary image. 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. I will, however, present you one particular subsystem in the forever, and that is relevant to this exploit, the shell. 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. There are a lot of strings that hint to me that they might be some kind of list of commands for a shell. For example, help?info, ver, perf, on, perf, off, perfdisp, memory reads, memory writes, maybe. They look like GDB commands. G, D, S, M, M, B, M, W, M, L. This is syntax similar to GDB syntax, so maybe they are, maybe they're not. And this is how all of this data links together. 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. The handler function for the commands help and question mark is the same, and it uses the command table. And that command table is also used by a separate function, an input handler, which is somehow getting its input from the outside world. And all those handler functions print stuff using the same set of functions. So there's the main one, print, which just prints a string. 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. 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. In the meantime, I want to show the input handler because it is very funny. It is essentially a state machine with three states 0, 1, and 2. When it is in state 0, it asks for a password. 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. And in state 2, it executes our command. 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. So let's list out potential candidates for the interfaces that the shell might be running on top of. First off, UART. 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. So definitely not Harvard UART. Implemented in the chip. It could be software UART. But in any case, UART is ready to transmit bytes as is. They do not require great manipulation. So if I look in that print function, I expect to see just plain data copying or something like that. Or maybe a loop waiting for a bit in a register to clear, something like that. 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. And that DCC uses special instructions. So if I look in that print function and it uses those special instructions, then it's DCC. And it could be USB, but if we print out the descriptor for the USB interface, we only see MIDI. We don't see a serial port, a virtual serial port, or virtual networking, or anything like that, just MIDI. So it's unlikely to be USB. And it's unlikely to be UART. Probably use JTAG, but we have to look in that shell print function to find out. And that print function does a lot of stuff. So if we feed that function the string hello world, of course, it is represented as ASCII, those hex bytes. What it's doing is it's taking eight byte chunks. I'm sorry, I highlighted seven bytes. Eight byte chunks of the input string. 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. So this 48 is translated into 0408. That is done for every byte in the chunk. And then a fixed eight byte header is appended, and a fixed one byte footer is appended. So string hello world is transmitted as these two blocks. 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. The MIDI specification says that a system exclusive message starts with an F0 and ends with an F7. 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. In order to identify that manufacturer, an ID is assigned to them, either one byte or three bytes. And if so, that 43 there must be the manufacturer ID according to the specification. And in fact, if we look at what ID that corresponds to, it is Yamaha. And my synthesizer is made by Yamaha, so that makes sense. 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. And one more thing about why it is breaking up the bytes into two bytes. It is because MIDI does not permit data to be transmitted if it has the highest bit set to one. Because any byte that is sent over MIDI with the highest bit sent to one is a status byte. 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. So they figured they might want at some point in the future to transmit maybe UTF-8 over the shell. So they must future-proof by breaking up the bytes into four bits so that the highest bit is always guaranteed to be zero. We know the format of outgoing messages, but we don't yet know the format of incoming messages. 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. And well, it did. Here's the shell. Here's that Python script that is accessing a shell. We say login. It is asking us for a password, which we type in. It says login. Okay. We rank help. And ladies and gentlemen, we got it. We got the shell. And like I mentioned, a couple of commands look to me like they were memory write commands. So we can exploit that. If I execute this ML command with an address in hexadecimal and a data of word 32-bit word in hexadecimal. And then with my debugger, I look at that memory location. I see the value that I want written there. And that address is in RAM. So it is rewritable. And that is very good. That means that we can override the memory of this synthesizer via just many messages. 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. We can assemble it into binary data, but then transform this binary data into write commands for the shell. 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. And we see the string hello world on display. 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. The memory write commands are like, they are right there. 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. And it looks like follows. So, how can we exploit all this to play Bad Apple on the display? How can we use a textual display to display graphics? Well, turns out we can't really, because like I said, this display controller is only designed to display text. It is designed for two lines of eight characters each, and each character is five pixels wide by eight pixels tall. But if you look at the display closely, it has a lot of segments, non-textual segments. 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. And this is an area with the notation, and the synth status, and all this other crap. How is the synth accessing those segments and manipulating them in a custom way if the LCD controller only supports text? Well, this is the LCD grid as the controller expects to see it. It is basically 40 segments per line arranged in 16 lines. And this is what the synthesizer is using it as. It is using the top row as text, as intended. On the bottom row, every segment in the virtual bottom row is wired to an actual segment on the display. And what I drew here is non-garbage. 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. So, somehow the CPU is telling the LCD controller to display custom graphics. And how does it do that? And if it does do that, how can we do the same? Well, here is a simplified schematic diagram of the LCD controller, with the powers that matter to us. The most important memory is the display data RAM, the DRAM. It is holding the ASCII characters that are currently displayed on screen. And there's... the second most important memory is CGROM. It is translating ASCII characters into actual graphics. 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. There's also the CGRAM. 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. They refer to the same area in the CGRAM. And in order to play a video, this is a frame from Bad Apple. First, our display is 40 by 16 pixels. So, we... first, we take a slim window of the video, and we break it up into 8 portions of 5 by 8 pixels. 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. But the synthesizer fights back, of course, because it's got its own custom stuff to be displayed in its custom segments. So, I'm not going to go into detail here. Unfortunately, I don't have enough time. But, essentially, there's a real-time operating system that is running on this synthesizer. And it's got, essentially, a task table which lists out all the tasks that are running on the synthesizer. 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. 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. So, this is, essentially, how to play Bad Apple on the synthesizer. And a little addendum. 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, released in 2012, and the latest one is E473, released in 2022. And this expands beyond just educational synthesizer. The DGX digital piano, released in 2016, has the same shell, the same passwords, hashtag 000. 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. Shout out to these guys, by the way, MarisaChan, Curler, RPL1846, and Cintra. 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. This is a big issue, I think. And a little summary on the DSP. So, in this chip, there's a DSP that is separate from the ARM core. It is running independently. Whenever I pause the ARM core, this DSP continues to produce sound. And it uses memory map register starting at address FE0000000, six zeros. There are two areas, as hex 400 bytes in size. There's a global configuration area. There's a voice parameter area. There are 32 voices. There are 16 16-bit parameters per voice. You can look the formulas up on slides. 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. And here's one global register that I was able to figure out the meaning of. Again, you can look at the slide. This is very boring, but nevertheless important, I think. And here's the list of references. The article from before that mentioned this chip might be based on SuperH, even though it's not entirely correct. There's also lots of useful info on that website. Go read it. There's the MIDI specification, the ARM documentation, and that's it. Thank you very much, and thank you very much to Alla and Petya here behind the cameras that made this video possible. Goodbye!