WEBVTT

00:00.000 --> 00:01.680
How's everybody doing?

00:02.820 --> 00:03.840
Yay, come on.

00:03.840 --> 00:04.360
How's everybody doing?

00:04.360 --> 00:05.000
This is DefCon.

00:05.900 --> 00:06.940
Way better.

00:06.940 --> 00:07.520
Okay.

00:07.520 --> 00:07.800
Okay.

00:07.800 --> 00:10.360
So, today we have a video presentation in track two.

00:10.360 --> 00:12.920
It's going to be remote code execution by MIDI messaging.

00:13.500 --> 00:14.380
Enjoy.

00:17.240 --> 00:19.100
Hello, DefCon 33.

00:19.340 --> 00:20.940
My name is Anna Antonenko.

00:20.940 --> 00:23.660
And unfortunately, I couldn't be here with you today.

00:23.660 --> 00:24.920
Just some visa issues.

00:24.920 --> 00:28.540
So, I'm going to be presenting my talk via video.

00:28.540 --> 00:31.880
My talk is titled Remote Code Execution via MIDI Messages.

00:31.880 --> 00:38.140
It's about exploiting a vulnerability, rather a backdoor that I found in a line of Yamaha products.

00:39.960 --> 00:43.300
About some stuff about me, you can find me online on my website.

00:43.300 --> 00:45.940
I use the same nickname practically everywhere.

00:45.960 --> 00:47.900
Here's my email on the slide here.

00:48.940 --> 00:51.900
I am a forward developer at Flipper Devices.

00:51.900 --> 00:53.300
I write code in C.

00:53.300 --> 00:59.160
But my employer does not necessarily reflect my opinions.

00:59.160 --> 01:02.860
All research was done in my own time.

01:03.020 --> 01:08.040
I really like Erlang and Elixir, their languages for the Beam virtual machine.

01:08.580 --> 01:13.880
I'm working on an operating system written in Erlang in my spare time.

01:13.880 --> 01:19.160
I really like electronics, both analog and digital, but digital is my specialty.

01:19.260 --> 01:21.060
And I like music.

01:21.060 --> 01:25.240
I like writing music, I like playing music, and listening to music, of course.

01:26.400 --> 01:32.020
I prefer to think of myself as a decent forward engineer, but not a great reverse engineer.

01:32.020 --> 01:40.640
So hopefully, those of you who are like me can learn something from this talk, from this perspective.

01:42.240 --> 01:48.080
First, I'm going to show you a little demo about what we're going to be talking today.

01:48.700 --> 01:51.780
But this demo needs a bit of introduction.

01:52.220 --> 01:54.660
So there's this protocol called MIDI.

01:54.660 --> 01:58.780
It emerged in the 80s, and it's a really simple protocol.

01:58.780 --> 02:03.080
It describes music generation parameters, basically.

02:03.080 --> 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.

02:12.360 --> 02:16.320
And the other device will generate audio based on that data.

02:18.080 --> 02:22.920
But apparently, even a protocol as simple as that can have backdoors.

02:24.440 --> 02:26.890
So this is a demo.

02:28.380 --> 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.

02:38.790 --> 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.

03:04.260 --> 03:06.960
So what is this talk going to be about?

03:06.960 --> 03:09.240
It's going to be about hardware reverse engineering.

03:09.240 --> 03:11.480
We're going to be dealing with an undocumented chip.

03:11.480 --> 03:15.340
We're going to be accessing its debug interfaces.

03:15.340 --> 03:17.360
We're going to be reversing some schematics.

03:18.120 --> 03:20.400
We're going to be doing firmware reverse engineering.

03:20.400 --> 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.

03:27.680 --> 03:35.640
And of course, we're going to find a backdoor in the firmware and exploit it to see this cool thing.

03:35.640 --> 03:44.220
And of course, it's about a story that begins with me having this synthesizer, this dirty instrument.

03:45.120 --> 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.

04:00.200 --> 04:04.200
So this is the main board of the synthesizer.

04:04.200 --> 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.

04:15.720 --> 04:19.680
So these two on the bottom are flash chips.

04:19.820 --> 04:21.800
This is a USB host controller.

04:21.800 --> 04:27.480
You can plug in a flash drive into this USB host and that chip handles that.

04:27.480 --> 04:29.600
This is the RAM.

04:30.320 --> 04:32.240
And this is the LCD controller.

04:32.240 --> 04:36.960
There's a 16 megabytes flash for what I'm presuming is the firmware.

04:36.960 --> 04:42.520
And if I remember correctly, a 2 megabyte flash for user data.

04:42.900 --> 04:44.520
They are separate.

04:45.440 --> 04:49.320
And this is the largest chip.

04:49.320 --> 04:50.840
This is probably the CPU.

04:52.720 --> 04:58.320
That main chip is labeled Yamaha SW-L01U.

04:58.320 --> 05:04.540
And of course, this synthesizer is made by Yamaha, so I guess this is a proprietary chip.

05:04.540 --> 05:07.860
Also, there is absolutely no information about it online.

05:07.860 --> 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.

05:16.920 --> 05:18.760
But absolutely no documentation.

05:18.760 --> 05:23.960
So it's gonna be quite hard to approach it without documentation.

05:23.960 --> 05:33.580
Also, I stumbled upon an article that suggested that it might be based on a Super H architecture from Hitachi.

05:33.580 --> 05:35.720
And this was my first time hearing about it.

05:35.720 --> 05:38.340
So of course, I don't know the architecture.

05:38.340 --> 05:39.800
I have no documentation.

05:40.340 --> 05:43.700
I'm not gonna even try to access it, you know.

05:43.700 --> 05:49.900
So I just put it back together, finished my cleaning, and call it a day.

05:49.900 --> 05:54.860
Until two years later, I found a service manual for a similar synthesizer.

05:54.860 --> 05:57.620
The E443, not the E433.

05:58.640 --> 06:00.460
They're quite similar.

06:00.460 --> 06:02.820
They have the same external appearance.

06:02.820 --> 06:09.440
They have the same board layout, essentially the same chips, including the CPU.

06:09.860 --> 06:14.400
But the later model has an audio input, while mine doesn't.

06:14.400 --> 06:19.940
And also it has an ADC analog-to-digital converter, while mine doesn't.

06:21.040 --> 06:26.360
Also, component numbering is different, but not too big of an issue.

06:27.000 --> 06:37.400
The two most important things that this service manual has are full schematics and full board layouts for all the boards.

06:37.400 --> 06:38.120
That's nice.

06:38.120 --> 06:38.880
That's the first thing.

06:38.880 --> 06:42.220
And the second thing is pinouts of all of the chips.

06:42.260 --> 06:48.400
And for most of the chips, I was able to google their part numbers and find out their pinouts.

06:48.940 --> 06:53.800
And usually they are standardized, like in the case of flash chips.

06:53.840 --> 06:57.480
But it also had a full pinout of the CPU.

06:57.480 --> 07:06.580
So here on the slide, you can see that page with the pinout of the CPU, that complete 176-pin chip.

07:06.580 --> 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.

07:15.860 --> 07:19.260
So how can we approach this chip?

07:19.560 --> 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.

07:29.300 --> 07:38.000
As forward engineers, we typically want to have some kind of debugging for our program and some text output for logs.

07:38.000 --> 07:42.900
And in the hardware world, there are common interfaces for those.

07:42.900 --> 07:48.580
For debugging, that would be JTAG or SWG for ARM chips, and so on.

07:48.960 --> 07:51.140
There are lots of those interfaces, actually.

07:51.140 --> 07:54.180
But JTAG is by far the most common.

07:54.440 --> 07:59.220
And for simple text output for logs, for example, it would be UART.

07:59.780 --> 08:05.520
I decided to start with UART because I figured that accessing the logs would be easier.

08:05.780 --> 08:09.660
But spoiler alert, no, it wasn't.

08:10.040 --> 08:16.760
In total, there are five UART-related pins on this chip per the pinout.

08:17.200 --> 08:23.120
Two transmit pins, two receive pins, and one optional clock pin.

08:23.120 --> 08:29.620
So, of those five pins, only the transmit and receive pins are of interest to us.

08:29.620 --> 08:36.820
And of those, are only the transmit pins of interest to us, because we just want to access the logs.

08:36.820 --> 08:45.660
And of those four pins, three pins are just used as general-purpose IOs, not for UART.

08:45.660 --> 08:51.080
And the remaining fourth pin is a transmit pin, and it was completely silent.

08:51.080 --> 08:57.080
No matter how times I rebooted the device, it stayed completely silent, no matter what I did to it.

08:59.280 --> 09:04.800
But we shouldn't give up so easily, because I found two other very interesting pins.

09:04.800 --> 09:08.540
One was named PROTN, and one was named TESTN.

09:08.540 --> 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.

09:22.280 --> 09:25.040
It still remained silent in all four modes.

09:26.100 --> 09:28.740
So, let's move on to debugging then.

09:29.420 --> 09:34.380
Like I said, JTAG is the most common debugging interface, and there it is on our chip.

09:35.760 --> 09:38.960
Those five pins make up the JTAG interface.

09:38.960 --> 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.

09:46.960 --> 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.

09:57.480 --> 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.

10:09.520 --> 10:12.560
And there is a tiny, tiny, tiny risk.

10:12.560 --> 10:16.700
I don't want to scare you, even though I scared myself.

10:16.760 --> 10:23.900
There's a tiny, tiny chance that if you use the wrong file, you may irreversibly damage the device.

10:23.900 --> 10:28.860
But it is only applies to very, very rare cases.

10:29.100 --> 10:31.920
So, don't listen to me, okay?

10:33.400 --> 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.

10:45.280 --> 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.

10:53.180 --> 10:55.520
But if it does exist, it must be 32-bit.

10:55.520 --> 10:58.320
That's the only thing that the specification says.

10:59.040 --> 11:05.140
Fortunately for us, there are conventions on how to find that register, and software will do that for you.

11:05.140 --> 11:07.580
We'll look at that in a minute.

11:07.860 --> 11:09.700
So, let's talk tools.

11:09.700 --> 11:11.520
You need basically two tools.

11:11.520 --> 11:19.700
One, you need a hardware debug probe that is an adapter between your computer and the JTAG interface on your device.

11:19.700 --> 11:21.760
That's called also a JTAG cable.

11:21.760 --> 11:22.900
There are a lot of them.

11:22.900 --> 11:25.620
You can see them listed on the slide if you're interested.

11:25.900 --> 11:29.780
And you need one specific software tool called OpenOSD.

11:29.780 --> 11:32.620
It is basically a driver for your JTAG adapter.

11:32.620 --> 11:39.460
It is an adapter between the JTAG adapter and GDB, or your favorite debugger of choice.

11:39.540 --> 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.

11:53.280 --> 11:55.860
So, here's the configuration for OpenOSD.

11:56.300 --> 11:58.360
Here's the logs from the OpenOSD.

11:58.360 --> 12:00.540
I highlighted the important parts in red.

12:00.540 --> 12:05.820
And one, it says that auto-probing might not work because of the stuff that I mentioned earlier.

12:05.820 --> 12:13.960
And second, that it was able, it was successful in finding a device with an ID code of 3F0F0F0F.

12:13.960 --> 12:17.400
And that ID code is suspiciously pretty.

12:17.400 --> 12:18.360
It is very pretty.

12:18.360 --> 12:21.340
I triple-checked, quadruple-checked my wiring.

12:21.340 --> 12:27.040
I quadruple-checked my config and power supplies and everything.

12:27.040 --> 12:29.680
And the ID code was the same.

12:29.680 --> 12:32.800
So, I figured that must be it.

12:32.800 --> 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.

12:44.640 --> 12:52.220
And the common thing between all of those devices was that they were all based on an ARM7 core.

12:52.580 --> 12:57.240
And a footnote, ARM7 is not the same thing as ARMv7.

12:58.320 --> 13:02.020
If I recall correctly, ARM7 is ARMv5.

13:02.020 --> 13:02.700
I'm not sure.

13:02.700 --> 13:03.940
Don't quote me on that.

13:04.420 --> 13:09.800
But let's try telling OpenOCD that we're dealing with an ARM7 device.

13:10.180 --> 13:11.120
So, I did.

13:11.380 --> 13:14.620
These two red lines in the config are new ones.

13:14.800 --> 13:20.640
And OpenOCD didn't give us any errors and told us that it is listening for GDB connections.

13:20.640 --> 13:23.540
So, let's try connecting with GDB.

13:24.380 --> 13:32.440
And from my point of view, this is a success because of three things.

13:32.440 --> 13:39.580
First, my board was connected to a lab bench power supply, which was reporting the current draw to me.

13:39.580 --> 13:51.200
And when I stopped program execution in GDB by pressing control C, the current draw dropped from 114 to 98 milliamps.

13:51.920 --> 13:57.780
And vice versa, when I continued the execution by the command C, it rose again to 114.

13:57.780 --> 13:59.140
That's the first thing.

13:59.140 --> 14:03.020
The second thing is that the interrupt vectors all made sense.

14:03.160 --> 14:06.760
If you don't know what those are, we're going to talk about that in a minute.

14:07.800 --> 14:14.280
And, well, they are ARM instructions and they are jumps, which I would expect them to be.

14:14.320 --> 14:17.160
And the third thing is that the addresses made sense.

14:17.160 --> 14:26.840
When I randomly stopped execution, when the synth was just running normally, the addresses were contained to a neighboring...

14:26.840 --> 14:28.900
they were neighbors, basically.

14:30.100 --> 14:37.040
Those three things indicated to me that we successfully gained debug access on the chip.

14:37.120 --> 14:38.740
It was that easy.

14:40.000 --> 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.

14:49.920 --> 14:53.500
But if you're dealing with something like that, it's not going to be that easy.

14:53.500 --> 15:01.800
You're going to be using much more advanced hardware exploits to first unlock the debug interface and then access it.

15:01.800 --> 15:06.340
But provided we have done that, we can now extract the firmware.

15:06.400 --> 15:09.800
So for that, we need to look at the reset vectors.

15:09.800 --> 15:12.080
I'm sorry, at the interrupt vectors.

15:12.740 --> 15:20.100
Like I promised, in almost all modern computer architectures, there's a thing called an interrupt.

15:20.100 --> 15:26.580
And it is an extraordinary event that must interrupt the program immediately.

15:27.180 --> 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.

15:40.220 --> 15:44.480
A division by zero is an exception, which is an interrupt, for example.

15:45.280 --> 15:51.000
And whenever an interrupt happens, the CPU executes the interrupt vector.

15:51.260 --> 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.

16:05.100 --> 16:16.080
And in embedded development, unlike, you know, big computer development, desktop computer OS development, this vector table is usually read-only.

16:16.080 --> 16:19.240
So it is usually placed in the flash memory.

16:19.780 --> 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.

16:27.620 --> 16:32.380
And I know from the markings on the flash chip that it is 16 megabytes in size.

16:32.380 --> 16:37.820
So I decided to dump 16 megabytes starting from address zero into a file.

16:38.220 --> 16:42.420
Immediately, I thought that the result was good.

16:43.680 --> 16:49.060
Well, when reversing, it is helpful to look at the strings in the file that you're reversing.

16:49.080 --> 16:56.760
Any decent reverse engineering tool will search for the human readable strings in the file that you've given it.

16:57.520 --> 17:05.420
And they are very helpful because they are immediately recognizable and understandable, unlike machine code to us humans.

17:05.800 --> 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.

17:17.100 --> 17:25.000
But a bad sign was that all of those strings repeated every 64 kilobytes with the same offset.

17:25.000 --> 17:31.980
So every string was repeated 256 times, evenly spaced throughout the image that I've dumped.

17:33.040 --> 17:43.820
And this hints to me that I've captured some memory, 64 kilobytes in size, that is located at address zero.

17:43.820 --> 17:54.060
And because of the string internal, I guessed that it might be the internal read-only memory inside of the chip.

17:54.780 --> 18:03.700
And if we look at the reset vector, which is an interrupt vector for a reset, i.e.

18:03.700 --> 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.

18:18.040 --> 18:24.520
So I dumped 16 megabytes starting from there, and the data there made even more sense.

18:24.680 --> 18:36.740
I saw lots of strings that I recognized just from using the instrument, like voice names, UI messages, and et cetera.

18:37.140 --> 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.

18:53.300 --> 18:55.780
It definitely meant that I was on the right track.

18:55.960 --> 18:59.100
So let's play Minesweeper.

18:59.700 --> 19:03.240
This is relevant to the topic of my talk, by the way.

19:03.240 --> 19:08.340
I think that reverse engineering is very similar to Minesweeper.

19:09.260 --> 19:13.940
When you play Minesweeper, you start with an empty field.

19:13.940 --> 19:16.340
You don't know the states of any of the cells.

19:17.200 --> 19:25.600
And first you click a random cell that by the rules of the game must not have a mine in it.

19:25.640 --> 19:31.580
And as you do that, you automatically discover the states of neighboring cells.

19:31.720 --> 19:36.400
And of course, you can deduce which state, which cells must have a mine.

19:36.400 --> 19:39.540
You can flag them, not to accidentally click them.

19:39.540 --> 19:45.400
And you repeat this until you've discovered everything there is to discover on the board.

19:45.720 --> 19:49.620
And I think that reverse engineering is very similar.

19:49.620 --> 19:54.460
You start without knowing what any of the functions or any instructions do.

19:54.800 --> 20:01.460
You have an entry point or multiple entry points that you can understand the meaning of as they are.

20:01.460 --> 20:06.740
So for example, a reset vector or the human readable strings.

20:06.800 --> 20:09.300
Those are very useful entry points.

20:09.300 --> 20:20.880
And when you look at the functions that use these strings, you learn something about them.

20:20.880 --> 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.

20:28.720 --> 20:32.900
And this knowledge propagates through the entire binary image.

20:32.980 --> 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.

20:43.140 --> 20:54.280
I will, however, present you one particular subsystem in the forever, and that is relevant to this exploit, the shell.

20:56.800 --> 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.

21:10.800 --> 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.

21:17.340 --> 21:26.380
For example, help?info, ver, perf, on, perf, off, perfdisp, memory reads, memory writes, maybe.

21:26.420 --> 21:28.440
They look like GDB commands.

21:28.440 --> 21:31.480
G, D, S, M, M, B, M, W, M, L.

21:32.740 --> 21:38.000
This is syntax similar to GDB syntax, so maybe they are, maybe they're not.

21:38.820 --> 21:41.860
And this is how all of this data links together.

21:41.860 --> 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.

21:53.800 --> 22:01.220
The handler function for the commands help and question mark is the same, and it uses the command table.

22:01.560 --> 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.

22:10.180 --> 22:16.300
And all those handler functions print stuff using the same set of functions.

22:16.300 --> 22:19.580
So there's the main one, print, which just prints a string.

22:19.700 --> 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.

22:32.700 --> 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.

22:44.940 --> 22:51.440
In the meantime, I want to show the input handler because it is very funny.

22:51.440 --> 22:55.960
It is essentially a state machine with three states 0, 1, and 2.

22:56.040 --> 23:00.640
When it is in state 0, it asks for a password.

23:01.260 --> 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.

23:12.260 --> 23:14.580
And in state 2, it executes our command.

23:14.580 --> 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.

23:31.260 --> 23:39.820
So let's list out potential candidates for the interfaces that the shell might be running on top of.

23:39.820 --> 23:41.660
First off, UART.

23:41.660 --> 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.

23:55.640 --> 23:58.540
So definitely not Harvard UART.

23:58.820 --> 23:59.940
Implemented in the chip.

23:59.940 --> 24:01.660
It could be software UART.

24:02.520 --> 24:08.260
But in any case, UART is ready to transmit bytes as is.

24:08.480 --> 24:11.100
They do not require great manipulation.

24:11.340 --> 24:19.120
So if I look in that print function, I expect to see just plain data copying or something like that.

24:19.180 --> 24:24.020
Or maybe a loop waiting for a bit in a register to clear, something like that.

24:25.200 --> 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.

24:42.940 --> 24:46.080
And that DCC uses special instructions.

24:46.080 --> 24:51.560
So if I look in that print function and it uses those special instructions, then it's DCC.

24:51.560 --> 25:01.440
And it could be USB, but if we print out the descriptor for the USB interface, we only see MIDI.

25:01.720 --> 25:08.480
We don't see a serial port, a virtual serial port, or virtual networking, or anything like that, just MIDI.

25:08.480 --> 25:10.740
So it's unlikely to be USB.

25:11.100 --> 25:12.980
And it's unlikely to be UART.

25:12.980 --> 25:18.980
Probably use JTAG, but we have to look in that shell print function to find out.

25:18.980 --> 25:22.680
And that print function does a lot of stuff.

25:22.680 --> 25:31.520
So if we feed that function the string hello world, of course, it is represented as ASCII, those hex bytes.

25:31.600 --> 25:35.460
What it's doing is it's taking eight byte chunks.

25:35.680 --> 25:38.380
I'm sorry, I highlighted seven bytes.

25:38.380 --> 25:40.460
Eight byte chunks of the input string.

25:40.460 --> 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.

25:49.420 --> 25:53.700
So this 48 is translated into 0408.

25:53.980 --> 25:57.060
That is done for every byte in the chunk.

25:57.060 --> 26:05.280
And then a fixed eight byte header is appended, and a fixed one byte footer is appended.

26:06.940 --> 26:12.840
So string hello world is transmitted as these two blocks.

26:13.560 --> 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.

26:31.160 --> 26:38.380
The MIDI specification says that a system exclusive message starts with an F0 and ends with an F7.

26:38.440 --> 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.

26:50.540 --> 26:58.320
In order to identify that manufacturer, an ID is assigned to them, either one byte or three bytes.

26:58.640 --> 27:04.100
And if so, that 43 there must be the manufacturer ID according to the specification.

27:04.300 --> 27:10.760
And in fact, if we look at what ID that corresponds to, it is Yamaha.

27:11.140 --> 27:15.480
And my synthesizer is made by Yamaha, so that makes sense.

27:15.480 --> 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.

27:32.380 --> 27:40.820
And one more thing about why it is breaking up the bytes into two bytes.

27:40.880 --> 27:50.580
It is because MIDI does not permit data to be transmitted if it has the highest bit set to one.

27:50.580 --> 27:57.720
Because any byte that is sent over MIDI with the highest bit sent to one is a status byte.

27:57.720 --> 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.

28:06.200 --> 28:14.380
So they figured they might want at some point in the future to transmit maybe UTF-8 over the shell.

28:14.380 --> 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.

28:25.820 --> 28:31.440
We know the format of outgoing messages, but we don't yet know the format of incoming messages.

28:31.440 --> 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.

28:43.840 --> 28:45.720
And well, it did.

28:45.720 --> 28:48.640
Here's the shell.

28:48.640 --> 28:51.500
Here's that Python script that is accessing a shell.

28:51.500 --> 28:52.780
We say login.

28:52.780 --> 28:55.640
It is asking us for a password, which we type in.

28:55.640 --> 28:56.200
It says login.

28:56.200 --> 28:57.120
Okay.

28:57.120 --> 28:58.320
We rank help.

28:58.320 --> 29:03.320
And ladies and gentlemen, we got it.

29:03.320 --> 29:04.900
We got the shell.

29:06.360 --> 29:13.940
And like I mentioned, a couple of commands look to me like they were memory write commands.

29:13.940 --> 29:15.880
So we can exploit that.

29:16.100 --> 29:24.880
If I execute this ML command with an address in hexadecimal and a data of word 32-bit word in hexadecimal.

29:24.880 --> 29:29.960
And then with my debugger, I look at that memory location.

29:29.960 --> 29:34.200
I see the value that I want written there.

29:34.200 --> 29:37.120
And that address is in RAM.

29:37.120 --> 29:38.680
So it is rewritable.

29:38.680 --> 29:40.820
And that is very good.

29:40.880 --> 29:48.320
That means that we can override the memory of this synthesizer via just many messages.

29:48.320 --> 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.

30:03.820 --> 30:11.700
We can assemble it into binary data, but then transform this binary data into write commands for the shell.

30:11.700 --> 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.

30:22.220 --> 30:25.220
And we see the string hello world on display.

30:25.280 --> 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.

30:47.160 --> 30:50.980
The memory write commands are like, they are right there.

30:51.240 --> 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.

31:12.260 --> 31:14.960
And it looks like follows.

31:43.710 --> 31:48.890
So, how can we exploit all this to play Bad Apple on the display?

31:49.470 --> 31:53.750
How can we use a textual display to display graphics?

31:53.810 --> 32:00.510
Well, turns out we can't really, because like I said, this display controller is only designed to display text.

32:00.510 --> 32:08.190
It is designed for two lines of eight characters each, and each character is five pixels wide by eight pixels tall.

32:08.190 --> 32:13.830
But if you look at the display closely, it has a lot of segments, non-textual segments.

32:13.830 --> 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.

32:26.370 --> 32:32.470
And this is an area with the notation, and the synth status, and all this other crap.

32:33.030 --> 32:43.270
How is the synth accessing those segments and manipulating them in a custom way if the LCD controller only supports text?

32:43.270 --> 32:49.730
Well, this is the LCD grid as the controller expects to see it.

32:49.730 --> 32:55.130
It is basically 40 segments per line arranged in 16 lines.

32:55.830 --> 33:01.910
And this is what the synthesizer is using it as.

33:01.910 --> 33:05.210
It is using the top row as text, as intended.

33:05.310 --> 33:12.510
On the bottom row, every segment in the virtual bottom row is wired to an actual segment on the display.

33:12.790 --> 33:15.550
And what I drew here is non-garbage.

33:16.130 --> 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.

33:26.250 --> 33:32.890
So, somehow the CPU is telling the LCD controller to display custom graphics.

33:33.470 --> 33:35.630
And how does it do that?

33:35.630 --> 33:39.550
And if it does do that, how can we do the same?

33:40.590 --> 33:46.710
Well, here is a simplified schematic diagram of the LCD controller, with the powers that matter to us.

33:46.710 --> 33:50.390
The most important memory is the display data RAM, the DRAM.

33:50.710 --> 33:56.070
It is holding the ASCII characters that are currently displayed on screen.

33:56.070 --> 33:59.190
And there's... the second most important memory is CGROM.

33:59.190 --> 34:02.930
It is translating ASCII characters into actual graphics.

34:03.130 --> 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.

34:15.830 --> 34:17.770
There's also the CGRAM.

34:17.770 --> 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.

34:35.090 --> 34:38.930
They refer to the same area in the CGRAM.

34:40.250 --> 34:45.750
And in order to play a video, this is a frame from Bad Apple.

34:45.750 --> 34:50.350
First, our display is 40 by 16 pixels.

34:50.350 --> 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.

35:02.330 --> 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.

35:16.330 --> 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.

35:23.810 --> 35:26.290
So, I'm not going to go into detail here.

35:26.290 --> 35:28.230
Unfortunately, I don't have enough time.

35:28.230 --> 35:33.190
But, essentially, there's a real-time operating system that is running on this synthesizer.

35:33.330 --> 35:40.790
And it's got, essentially, a task table which lists out all the tasks that are running on the synthesizer.

35:40.790 --> 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.

35:51.710 --> 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.

36:04.730 --> 36:12.330
So, this is, essentially, how to play Bad Apple on the synthesizer.

36:12.870 --> 36:15.310
And a little addendum.

36:15.630 --> 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,

36:35.390 --> 36:42.990
released in 2012, and the latest one is E473, released in 2022.

36:42.990 --> 36:48.270
And this expands beyond just educational synthesizer.

36:49.150 --> 36:58.730
The DGX digital piano, released in 2016, has the same shell, the same passwords, hashtag 000.

36:59.570 --> 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.

37:17.210 --> 37:24.630
Shout out to these guys, by the way, MarisaChan, Curler, RPL1846, and Cintra.

37:25.110 --> 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.

37:39.650 --> 37:43.050
This is a big issue, I think.

37:44.110 --> 37:47.510
And a little summary on the DSP.

37:47.510 --> 37:51.990
So, in this chip, there's a DSP that is separate from the ARM core.

37:51.990 --> 37:53.410
It is running independently.

37:53.410 --> 37:58.390
Whenever I pause the ARM core, this DSP continues to produce sound.

37:58.870 --> 38:06.670
And it uses memory map register starting at address FE0000000, six zeros.

38:07.130 --> 38:10.810
There are two areas, as hex 400 bytes in size.

38:10.810 --> 38:12.370
There's a global configuration area.

38:12.370 --> 38:14.450
There's a voice parameter area.

38:14.450 --> 38:16.570
There are 32 voices.

38:16.570 --> 38:19.570
There are 16 16-bit parameters per voice.

38:19.570 --> 38:22.670
You can look the formulas up on slides.

38:24.150 --> 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.

38:36.530 --> 38:40.830
And here's one global register that I was able to figure out the meaning of.

38:40.830 --> 38:42.090
Again, you can look at the slide.

38:42.090 --> 38:48.770
This is very boring, but nevertheless important, I think.

38:48.770 --> 38:50.270
And here's the list of references.

38:50.670 --> 38:57.890
The article from before that mentioned this chip might be based on SuperH, even though it's not entirely correct.

38:57.890 --> 39:00.930
There's also lots of useful info on that website.

39:00.930 --> 39:02.010
Go read it.

39:02.770 --> 39:08.190
There's the MIDI specification, the ARM documentation, and that's it.

39:08.190 --> 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.

39:15.710 --> 39:16.550
Goodbye!

