WEBVTT

00:00.000 --> 00:01.760
Are you ready back there?

00:01.760 --> 00:02.680
You good to go?

00:03.620 --> 00:04.940
Take that as a yes.

00:05.140 --> 00:11.200
I don't see anyone else coming in so I'm going to start a couple minutes early because I've got a lot of slides to get through and no projector.

00:11.320 --> 00:14.280
So I'm Craig Hefner.

00:15.320 --> 00:16.940
I wrote Ben Walk.

00:16.940 --> 00:21.680
I work for NetRise doing a bunch of firmware reverse engineering, security analysis.

00:22.520 --> 00:26.020
I work at Greyhat Academy where we teach firmware reverse engineering.

00:26.060 --> 00:29.500
So pulling firmware apart is near and dear to my heart.

00:31.580 --> 00:39.100
Used to be way back in the day when I was a young lad, most firmware, especially stuff like consumer stuff, was never encrypted, right?

00:39.100 --> 00:44.300
You could just run it through a tool like Ben Walk or look at it in a hex editor and kind of figure out what's in it and pull it apart.

00:45.220 --> 00:54.120
More and more these days we're seeing a lot of vendors encrypting their firmware, even for small, insignificant products.

00:54.320 --> 01:02.200
So it's really annoying because if you want to take it apart, you want to modify it, you want to analyze it, you want to figure out what's in it, it makes it a little hard to do when it's encrypted.

01:02.800 --> 01:09.680
So I'm going to walk through a few examples and these are generally examples of how not to encrypt your firmware if you are encrypting firmware.

01:10.900 --> 01:15.540
And I'm going to pick on D-Link a lot because they made a lot of mistakes, so it's on them.

01:16.380 --> 01:25.660
So the first device I want to talk about is a D-Link DAP1620, tiny little Wi-Fi access point slash Wi-Fi extender.

01:26.720 --> 01:28.400
They do encrypt their firmware.

01:28.840 --> 01:34.860
If you look at the firmware header, you'll see they have like a basic header there and then you see salted.

01:34.900 --> 01:38.880
If you see salted with two underscores, that's open SSL.

01:38.880 --> 01:51.260
When you encrypt something with open SSL and you use a pass phrase for the encryption, it will put a little header in there that says salted and then the next eight bytes will be the actual random salt that's generated.

01:52.080 --> 01:55.320
So whenever you see that, you're like, okay, well, they encrypted this with open SSL.

01:55.980 --> 01:58.880
So downloaded the firmware, it was all encrypted.

01:59.280 --> 02:01.800
Poked around on the website, they had a GPL release for it.

02:01.800 --> 02:03.560
Okay, let's see what that is.

02:03.700 --> 02:05.360
Pulled down the GPL release.

02:06.260 --> 02:09.160
It was a Linux based firmware, so they had to comply with GPL.

02:09.760 --> 02:15.260
Probably what they didn't have to do was include the build script that had the open SSL command to do the encryption and decryption.

02:17.380 --> 02:19.080
It was fairly straightforward.

02:19.640 --> 02:21.360
I do wish the slides were working.

02:21.580 --> 02:33.660
But basically they were just doing a SHA hash of the hardware ID and the model name and then using that as the key or the pass phrase to do the encryption.

02:33.660 --> 02:36.540
And it's symmetric so same key works for decryption.

02:37.280 --> 02:48.860
The only small hiccup was that if you just take their open SSL command and run it as is on any kind of a modern system, it doesn't work.

02:49.060 --> 02:53.540
Because the key you provide is not actually the encryption key.

02:53.540 --> 02:55.020
It is a pass phrase.

02:55.040 --> 03:02.380
So the pass phrase and the salts are used to generate the actual encryption key by open SSL.

03:02.700 --> 03:06.380
And it does that by running it through a series of hashing functions.

03:06.440 --> 03:10.820
And if you don't explicitly tell it what hashing function to use, it uses the default.

03:11.340 --> 03:13.800
The default today is SHA-256.

03:13.960 --> 03:16.200
Many years ago it was MD5.

03:16.400 --> 03:21.060
They were using an old version of open SSL so you had to explicitly tell it to use MD5.

03:21.760 --> 03:27.600
So if you are familiar enough with your tools to know that they change over time and how they've changed, that's helpful.

03:28.640 --> 03:33.540
But it's also nice to look through GPL code for encryption keys.

03:34.840 --> 03:36.940
Second example of what not to do.

03:37.220 --> 03:39.240
A D-link 2610.

03:39.240 --> 03:41.460
This is a little bit higher end.

03:41.460 --> 03:44.500
More for the small business commercial sector.

03:46.440 --> 03:49.640
And they had a lot of firmware available for download.

03:50.580 --> 03:52.180
Didn't have any GPL code.

03:52.280 --> 03:55.400
But they had a bunch of firmware images over the years that they released.

03:55.400 --> 04:00.680
So I pulled them all down, looked at the most recent one, and it was like, okay, this is like super high entropy.

04:00.680 --> 04:03.440
There's nothing in here that looks like it makes any sense.

04:03.440 --> 04:04.800
It's probably encrypted.

04:05.000 --> 04:08.120
Looked at the very first firmware they ever released for it.

04:08.300 --> 04:09.420
Not encrypted.

04:09.620 --> 04:12.080
Had a squash file system in there.

04:12.080 --> 04:15.860
You could pull it apart and pull all the files out very easily.

04:16.100 --> 04:17.860
So here's the problem.

04:17.860 --> 04:24.640
If your device was not originally designed to accept an encrypted firmware, you can't just encrypt the firmware and give it to the device.

04:24.640 --> 04:26.160
It doesn't know what to do with it.

04:26.160 --> 04:33.100
So you have to have an intermediate firmware that is not encrypted but accepts future encrypted firmware.

04:33.380 --> 04:42.220
So you have an unencrypted firmware that can decrypt future firmware, which kind of defeats the point of encryption, but that's okay.

04:42.680 --> 04:52.020
So looking at the most recent release that was not encrypted for this device, there was a new file added called ENC image.

04:52.960 --> 04:54.720
Let's take a look at that.

04:56.460 --> 04:59.240
Basically they had two pieces of data.

04:59.240 --> 05:02.560
One piece that just based off of the names and the files.

05:02.560 --> 05:06.640
They called FW sign data and another piece called image sign.

05:07.780 --> 05:16.900
And all they really did is they did a rolling XOR of these two strings in order to generate the key and the IV to do AES encryption.

05:18.100 --> 05:21.560
So you can go through, you can reverse engineer the rolling XOR.

05:21.560 --> 05:24.180
It really wasn't super complicated.

05:25.200 --> 05:29.060
It was like maybe 15 lines of Python code to reimplement.

05:29.740 --> 05:31.340
So you can use that.

05:31.840 --> 05:37.720
The actual strings that they use are in the ENC image binary itself as well as in the firmware header.

05:37.720 --> 05:40.420
So you have both pieces of information you need.

05:41.400 --> 05:42.720
And then you can decrypt it.

05:42.720 --> 05:51.720
The other thing you can do if you're lazy is just run the ENC image binary in QMU and give it the path to the encrypted file and it will decrypt it for you.

05:52.080 --> 05:53.700
So that's fun, too.

05:55.680 --> 06:00.640
Both of these examples are, you know, kind of lame.

06:01.240 --> 06:04.620
The vendor basically docs themselves in both instances.

06:04.800 --> 06:05.860
But it does happen.

06:05.860 --> 06:09.780
Super nice when it does for us, maybe not for the vendor.

06:10.960 --> 06:13.000
But D-Link did learn their lesson.

06:13.000 --> 06:18.300
They got a little more creative with their most recent line of devices.

06:18.940 --> 06:31.440
So if you look at pretty much any of their current consumer access points in their product line, what we're about to talk about will apply to all of them because they're all running basically the same firmware.

06:31.720 --> 06:39.360
But I started with the cheapest one because I'm cheap and why buy the $100 one when I can buy the $30 one and look at that.

06:39.780 --> 06:45.200
So it's a D-Link E15, again, very small little access point slash range extender.

06:46.820 --> 06:50.420
And all of the firmware was open SSL encrypted.

06:50.820 --> 06:51.360
Okay.

06:51.360 --> 06:52.180
Fine.

06:53.040 --> 06:54.720
Went to look for GPL releases.

06:54.720 --> 06:56.640
They did have a GPL release on their website.

06:56.640 --> 07:00.900
But when you click the download link it just takes you to their Taiwanese home page.

07:01.620 --> 07:03.740
So I was like, okay, screw you guys.

07:05.300 --> 07:07.940
So bought the device, pulled it apart.

07:08.720 --> 07:12.920
First thing I noticed they had a little four pin header on the board.

07:13.060 --> 07:16.740
Very indicative of UART slash serial ports.

07:16.900 --> 07:19.620
They even labeled all the pins, transmit, receive, ground.

07:19.940 --> 07:20.980
Very nice of them.

07:21.000 --> 07:21.880
Appreciative.

07:22.260 --> 07:24.360
So I was like, okay, maybe this will be easy.

07:24.780 --> 07:26.240
Well, turn it on.

07:26.380 --> 07:27.540
I get a boot log.

07:28.120 --> 07:29.040
No console.

07:29.040 --> 07:35.120
I'm like, okay, well, maybe I can break into the boot loader.

07:35.340 --> 07:36.720
They're using U-boot.

07:36.940 --> 07:43.460
If you pay attention to the boot messages on U-boot, there's a line that comes up that says hit any key to stop auto boot.

07:43.860 --> 07:46.820
You do that and it drops you into a little boot loader shell.

07:46.960 --> 07:47.900
It's like, cool.

07:49.020 --> 07:54.220
Maybe I can force the system to kick off a shell for me.

07:54.420 --> 08:00.560
If you're familiar at all with how the Linux kernel works, it takes some arguments when it boots up.

08:00.560 --> 08:02.780
Those arguments are stored in the boot loader.

08:03.280 --> 08:10.280
One of those arguments is the path to the first binary, the init binary, that the kernel should kick off.

08:10.280 --> 08:13.980
Usually that's init, but you can make it anything you want.

08:13.980 --> 08:16.700
So I set init equals slash bit SH.

08:17.140 --> 08:18.340
It did boot up.

08:18.340 --> 08:21.400
It did give me a shell prompt, but I couldn't interact with it.

08:22.820 --> 08:26.900
So and clearly the serial port was working because I could interact with U-boot.

08:26.980 --> 08:28.940
So they did something there.

08:28.940 --> 08:32.180
I was like, okay, well, let me go back to U-boot and see what I can do.

08:32.500 --> 08:37.660
U-boot, if you haven't played around with it, super, super fully featured boot loader.

08:38.000 --> 08:39.040
It's really awesome.

08:39.440 --> 08:48.160
So there are a few commands that may not always be compiled in, but they did have a couple of really useful ones built into this version of U -boot.

08:48.160 --> 08:54.520
One is UBI, which allowed me to read sections of the flash chip into memory.

08:55.600 --> 09:03.240
And the other was TFTP put, which allowed me to write sections of memory over TFTP back to my laptop.

09:03.300 --> 09:12.640
So I pulled in a section of flash that was based on the flash layout in U-boot, supposed to be the file system.

09:13.380 --> 09:19.860
Read that into memory with UBI command and then used TFTP put to send that over to my laptop.

09:22.120 --> 09:22.980
It was unencrypted.

09:22.980 --> 09:24.440
It was a squash FS image.

09:24.580 --> 09:25.180
Awesome.

09:26.000 --> 09:34.960
At this point, if my only goal was just to get that firmware version of that device unencrypted and look at the file system, I'm done.

09:35.680 --> 09:36.720
But I was kind of curious.

09:36.720 --> 09:42.400
I was like, okay, well, maybe I can figure out how they're doing the keys and maybe decrypt some of the other firmware versions.

09:43.400 --> 09:47.300
So started poking around at some of the binaries inside that squash FS image.

09:47.720 --> 09:49.600
They had a binary called commander.

09:50.520 --> 09:52.180
Again, a really interesting name.

09:52.620 --> 10:03.200
And all of it was a compiled program, but whoever wrote this looks like they were a bash script person because everything was just shelling out to other commands .

10:03.460 --> 10:06.320
So you can just run strings on it and pretty much see everything that it's doing.

10:07.740 --> 10:17.160
And if you grep for firmware or key or anything like that, you'll see that there was a handful of commands where basically they're reading the key from somewhere on flash.

10:17.160 --> 10:24.180
They had like these kind of encoded offsets that didn't really correspond to actual flash addresses.

10:24.180 --> 10:30.100
So I don't know what the translation was there, but they were reading something in and they were writing it to the temp directory.

10:30.140 --> 10:36.280
So in the temp directory, under temp photo key.firmware is where they were keeping the firmware key.

10:36.280 --> 10:41.800
So I said, okay, well, if I can actually get access to the live running system, it should be sitting in temp.

10:42.220 --> 10:43.940
Maybe I can just pull it from there.

10:45.280 --> 10:47.640
So I started poking around with some of the binaries.

10:48.220 --> 10:52.900
I do work at a firmware company, so we have some fun internal tools.

10:52.900 --> 10:54.460
I threw some of the binaries through.

10:54.540 --> 10:59.580
And one of the interesting things that popped out was the web server had a potential command injection.

11:01.200 --> 11:12.040
And basically what they're doing is they have some custom binary called FOTA and it accepts several arguments, one of them being your time zone.

11:12.540 --> 11:24.060
So if you go to the web interface in the admin console, I guess it's not a console, but the admin interface, one of the things you can set is TZ location.

11:24.480 --> 11:30.780
And normally this is a drop down box that's like, you know, Africa, New York, whatever your time zone is.

11:31.540 --> 11:35.520
I told it my time zone was back tick Telnet D back tick.

11:36.600 --> 11:38.140
It accepted it.

11:38.140 --> 11:39.880
It kicked off a Telnet server.

11:40.820 --> 11:46.200
So I was able to Telnet in, drop me to a root shell, and I was able to pull the key for that device.

11:47.460 --> 11:50.520
And that key worked for all the other firmware for that device.

11:50.520 --> 11:54.460
So I could decrypt the current firmware as well as all the previous releases.

11:55.120 --> 12:05.020
What was really fun about this one is every device in the product line that I could actually get my hands on is running the same base firmware.

12:05.360 --> 12:08.300
And Amazon has a very lenient return policy.

12:09.160 --> 12:16.880
So I bought every single one I could get off of Amazon, plugged it in, popped a shell, pulled the key, packaged it back up, sent it back to Amazon.

12:17.640 --> 12:21.300
So I have a bunch of keys for a bunch of D-link devices now.

12:21.320 --> 12:22.900
I can just decrypt all their firmware.

12:24.500 --> 12:29.460
I did put this into a tool called D-link, D-E-L-I-N-K.

12:30.640 --> 12:33.360
And it's also been integrated into the latest version of Binwalk.

12:33.360 --> 12:40.700
So if you run Binwalk on any of D-link's devices, it will tell you it's encrypted and then it will extract it for you.

12:40.700 --> 12:41.640
So that's nice.

12:42.780 --> 12:44.280
But that's the talk.

12:45.500 --> 12:47.620
So open it up to Q&A.

12:47.620 --> 12:48.860
Anyone have any questions?

12:52.600 --> 12:53.320
Yes.

13:00.500 --> 13:02.920
I'm talking fast because I only have 30 minutes.

13:02.940 --> 13:06.900
Took a little bit longer than I might let on, especially that last one.

13:07.660 --> 13:14.780
When you're looking at something like, oh, they've leaked their own keys in like a GPL release or something, I mean, that's pretty quick.

13:14.780 --> 13:17.420
I mean, it takes, it's a couple of grips.

13:18.480 --> 13:19.820
It's not that difficult.

13:20.020 --> 13:25.020
When you're talking about actually reverse engineering, I mean, that always takes a while, right?

13:25.020 --> 13:28.740
I mean, I'm up here saying, oh, yeah, I use these U-boot commands.

13:29.360 --> 13:35.680
I had to figure out which commands to use, where I could actually read data into memory without overwriting some other crap that was important.

13:36.320 --> 13:40.780
I had to figure out the flash layout so I knew where from flash I needed to read.

13:41.880 --> 13:43.200
It's a little time consuming.

13:43.580 --> 13:44.900
You're going to go down rabbit holes.

13:45.060 --> 13:46.420
Don't pan out.

13:47.720 --> 13:54.020
For the D-link stuff, I'd say I probably worked on that in my free time for about a week, week and a half.

13:54.580 --> 13:59.880
But again, once I had it, I could pull all the keys for all of their stuff.

13:59.880 --> 14:02.200
So I was like, okay, that's worth it.

14:02.200 --> 14:03.200
Yep.

14:09.980 --> 14:12.000
So I did consider that.

14:12.000 --> 14:15.720
I mean, the first thing I thought was, oh, it looks like a SPI flash chip.

14:15.920 --> 14:18.140
You know, I can just pull that and read off of it.

14:19.460 --> 14:25.280
I am glad I went down this route instead just because it was a little more scalable once I figured out.

14:26.220 --> 14:31.980
Now, if I had pulled the flash chip, I might have been able to get the key a little more easily.

14:32.840 --> 14:42.080
From what I saw when I was reverse engineering some of the binaries, it looks like the keys get stored in some obfuscated manner potentially, though.

14:42.780 --> 14:46.780
So I may not have been able to pull the keys directly from the flash chip.

14:47.040 --> 14:50.140
But I did not investigate further, so that may or may not be true.

14:50.740 --> 15:06.020
But, yeah, a lot of times you'll have a device where either they don't have a serial port or J tag or if they do, it's really obscured on the board and a lot of times it's easier to just desolder the flash chip, put it in a reader and just read everything off.

15:06.020 --> 15:06.780
Absolutely.

15:13.580 --> 15:16.260
Like they encrypt at the flash layer?

15:16.520 --> 15:17.040
Yes.

15:17.040 --> 15:24.940
So, now what I will say is I haven't dealt with anything where they have an off board decryption.

15:25.000 --> 15:27.060
They have some custom hardware I see.

15:28.260 --> 15:38.900
But in those cases, what I've seen is there will be some stub at the very beginning that decrypts it on boot and so you have to reverse engineer that.

15:39.400 --> 15:42.720
That can be a lot more difficult for a couple of reasons.

15:42.720 --> 15:52.940
First of all, the ones that I've looked at are usually home grown encryption, which is not cryptographically secure, but a huge pain in the ass to reverse engineer.

15:54.260 --> 15:55.520
So that takes some time.

15:56.460 --> 16:02.820
But, yeah, there has to be something at some point that's going to decrypt the code into something the processor can understand.

16:03.360 --> 16:15.060
So either they have to have some code stub that does that or they have to have something else on an off board chip that will do the decryption.

16:15.820 --> 16:19.860
That's kind of the Achilles heel of encrypting with anything hardware.

16:20.940 --> 16:28.180
If you have a device that uses the encrypted firmware and you sell it to me, you just sold me a decryption device.

16:28.260 --> 16:35.580
Now, how easily I can use it to do what I want is another question, but by its very fundamental nature, that's what it is.

16:35.740 --> 16:40.620
So, you know, with enough time, effort and resources, you can break anything.

16:40.940 --> 16:43.400
But that's, I guess, everything, right?

16:44.260 --> 16:45.500
Was there another question?

16:51.840 --> 16:54.240
So, Giger is great.

16:54.680 --> 16:58.260
I think that's kind of become a huge standard just because it's free.

16:58.620 --> 17:00.840
Personally, I prefer Binary Ninja.

17:01.080 --> 17:06.620
It costs a little bit more, but it's not prohibitively expensive.

17:07.680 --> 17:10.480
IDA, if you get your employer to buy it for you is awesome.

17:11.460 --> 17:17.320
I used to use IDA all the time, but Binary Ninja has gotten pretty good, and so has Giger.

17:17.880 --> 17:21.360
I can't justify the cost, so I don't use it anymore.

17:22.560 --> 17:24.120
But, yeah, those are the big ones.

17:26.220 --> 17:32.300
I mean, some people prefer Radare, I never know how to pronounce it.

17:32.300 --> 17:34.200
It's spelled radar with an E on the end.

17:35.820 --> 17:37.900
So a lot of people like that one as well.

17:38.880 --> 17:39.280
So, yeah.

17:48.860 --> 17:50.900
So, BINROC has a bit of a sort of past.

17:50.900 --> 17:53.120
It's technically owned by Microsoft right now.

17:54.340 --> 17:57.700
So when I created BINROC, it was my own personal project.

17:58.580 --> 18:05.560
The company I was working for at the time, small company, they were like, hey, can we buy it from you for a couple hundred bucks?

18:05.560 --> 18:07.600
I'm like, yeah, sure, I don't care.

18:08.380 --> 18:12.000
Then we got acquired by Microsoft, so Microsoft technically owns it now.

18:12.420 --> 18:16.200
I'm still the sole maintainer on it, even though I'm not at Microsoft anymore.

18:17.560 --> 18:22.480
But I can't put them into BINROC, because it is technically owned by Microsoft.

18:22.500 --> 18:30.380
They will get a takedown notice, and they do take that seriously, which is why I put it in a separate project that is built at runtime, that it pulls down and builds at runtime.

18:30.620 --> 18:37.800
So they can go to Microsoft all they want, but it's on my personal, the keys are on my personal GitHub, so they have to deal with me.

18:55.330 --> 18:55.850
Yep.

18:55.850 --> 19:01.390
Not at this time.

19:02.530 --> 19:17.070
So any time I see something that, even if it's not something that I can decrypt necessarily, if there's something I can signature on to indicate that, hey, this is some sort of encryption or obfuscation that I've seen before, I do try to put it in there.

19:18.110 --> 19:21.750
But it's not a guarantee that you can decrypt it necessarily, right?

19:22.390 --> 19:29.010
You might not have the key for that particular device, but you at least get some output that's like, hey, I think this is encrypted.

19:30.370 --> 19:39.710
If you run against something where you run some tools and it's like, hey, none of these tools are finding anything, a really good thing to look at is the entropy.

19:40.890 --> 19:51.030
If you do an entropy analysis and BIN walk does this, it doesn't matter which one you use, if it's a flat line all the way across, it's probably encrypted.

19:51.170 --> 19:57.010
Or they're using some sort of good compression and obfuscated it.

19:57.010 --> 19:58.990
Or maybe it's some custom thing they wrote.

19:58.990 --> 20:03.210
But, yeah, high entropy is usually encryption or decent compression.

20:08.260 --> 20:09.400
Nothing else?

20:28.260 --> 20:29.460
All right.

20:29.460 --> 20:30.640
Thanks for coming out, everybody.

20:30.640 --> 20:31.560
Appreciate it.

20:36.560 --> 20:39.820
Hopefully the recording picked up some of the slides.

20:40.020 --> 20:42.840
I was kind of jumping through them because I wasn't really presenting them.

20:42.840 --> 20:48.040
But, yeah, if anybody wants a copy of the slides, come see me.

20:48.040 --> 20:49.240
I'll give you my e-mail.

20:49.480 --> 20:50.660
We'll post them up somewhere.

20:51.220 --> 20:51.640
Yeah.

20:51.640 --> 20:52.540
Have a great time.

