start	end	text
0	1760	Are you ready back there?
1760	2680	You good to go?
3620	4940	Take that as a yes.
5140	11200	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.
11320	14280	So I'm Craig Hefner.
15320	16940	I wrote Ben Walk.
16940	21680	I work for NetRise doing a bunch of firmware reverse engineering, security analysis.
22520	26020	I work at Greyhat Academy where we teach firmware reverse engineering.
26060	29500	So pulling firmware apart is near and dear to my heart.
31580	39100	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?
39100	44300	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.
45220	54120	More and more these days we're seeing a lot of vendors encrypting their firmware, even for small, insignificant products.
54320	62200	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.
62800	69680	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.
70900	75540	And I'm going to pick on D-Link a lot because they made a lot of mistakes, so it's on them.
76380	85660	So the first device I want to talk about is a D-Link DAP1620, tiny little Wi-Fi access point slash Wi-Fi extender.
86720	88400	They do encrypt their firmware.
88840	94860	If you look at the firmware header, you'll see they have like a basic header there and then you see salted.
94900	98880	If you see salted with two underscores, that's open SSL.
98880	111260	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.
112080	115320	So whenever you see that, you're like, okay, well, they encrypted this with open SSL.
115980	118880	So downloaded the firmware, it was all encrypted.
119280	121800	Poked around on the website, they had a GPL release for it.
121800	123560	Okay, let's see what that is.
123700	125360	Pulled down the GPL release.
126260	129160	It was a Linux based firmware, so they had to comply with GPL.
129760	135260	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.
137380	139080	It was fairly straightforward.
139640	141360	I do wish the slides were working.
141580	153660	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.
153660	156540	And it's symmetric so same key works for decryption.
157280	168860	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.
169060	173540	Because the key you provide is not actually the encryption key.
173540	175020	It is a pass phrase.
175040	182380	So the pass phrase and the salts are used to generate the actual encryption key by open SSL.
182700	186380	And it does that by running it through a series of hashing functions.
186440	190820	And if you don't explicitly tell it what hashing function to use, it uses the default.
191340	193800	The default today is SHA-256.
193960	196200	Many years ago it was MD5.
196400	201060	They were using an old version of open SSL so you had to explicitly tell it to use MD5.
201760	207600	So if you are familiar enough with your tools to know that they change over time and how they've changed, that's helpful.
208640	213540	But it's also nice to look through GPL code for encryption keys.
214840	216940	Second example of what not to do.
217220	219240	A D-link 2610.
219240	221460	This is a little bit higher end.
221460	224500	More for the small business commercial sector.
226440	229640	And they had a lot of firmware available for download.
230580	232180	Didn't have any GPL code.
232280	235400	But they had a bunch of firmware images over the years that they released.
235400	240680	So I pulled them all down, looked at the most recent one, and it was like, okay, this is like super high entropy.
240680	243440	There's nothing in here that looks like it makes any sense.
243440	244800	It's probably encrypted.
245000	248120	Looked at the very first firmware they ever released for it.
248300	249420	Not encrypted.
249620	252080	Had a squash file system in there.
252080	255860	You could pull it apart and pull all the files out very easily.
256100	257860	So here's the problem.
257860	264640	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.
264640	266160	It doesn't know what to do with it.
266160	273100	So you have to have an intermediate firmware that is not encrypted but accepts future encrypted firmware.
273380	282220	So you have an unencrypted firmware that can decrypt future firmware, which kind of defeats the point of encryption, but that's okay.
282680	292020	So looking at the most recent release that was not encrypted for this device, there was a new file added called ENC image.
292960	294720	Let's take a look at that.
296460	299240	Basically they had two pieces of data.
299240	302560	One piece that just based off of the names and the files.
302560	306640	They called FW sign data and another piece called image sign.
307780	316900	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.
318100	321560	So you can go through, you can reverse engineer the rolling XOR.
321560	324180	It really wasn't super complicated.
325200	329060	It was like maybe 15 lines of Python code to reimplement.
329740	331340	So you can use that.
331840	337720	The actual strings that they use are in the ENC image binary itself as well as in the firmware header.
337720	340420	So you have both pieces of information you need.
341400	342720	And then you can decrypt it.
342720	351720	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.
352080	353700	So that's fun, too.
355680	360640	Both of these examples are, you know, kind of lame.
361240	364620	The vendor basically docs themselves in both instances.
364800	365860	But it does happen.
365860	369780	Super nice when it does for us, maybe not for the vendor.
370960	373000	But D-Link did learn their lesson.
373000	378300	They got a little more creative with their most recent line of devices.
378940	391440	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.
391720	399360	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.
399780	405200	So it's a D-Link E15, again, very small little access point slash range extender.
406820	410420	And all of the firmware was open SSL encrypted.
410820	411360	Okay.
411360	412180	Fine.
413040	414720	Went to look for GPL releases.
414720	416640	They did have a GPL release on their website.
416640	420900	But when you click the download link it just takes you to their Taiwanese home page.
421620	423740	So I was like, okay, screw you guys.
425300	427940	So bought the device, pulled it apart.
428720	432920	First thing I noticed they had a little four pin header on the board.
433060	436740	Very indicative of UART slash serial ports.
436900	439620	They even labeled all the pins, transmit, receive, ground.
439940	440980	Very nice of them.
441000	441880	Appreciative.
442260	444360	So I was like, okay, maybe this will be easy.
444780	446240	Well, turn it on.
446380	447540	I get a boot log.
448120	449040	No console.
449040	455120	I'm like, okay, well, maybe I can break into the boot loader.
455340	456720	They're using U-boot.
456940	463460	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.
463860	466820	You do that and it drops you into a little boot loader shell.
466960	467900	It's like, cool.
469020	474220	Maybe I can force the system to kick off a shell for me.
474420	480560	If you're familiar at all with how the Linux kernel works, it takes some arguments when it boots up.
480560	482780	Those arguments are stored in the boot loader.
483280	490280	One of those arguments is the path to the first binary, the init binary, that the kernel should kick off.
490280	493980	Usually that's init, but you can make it anything you want.
493980	496700	So I set init equals slash bit SH.
497140	498340	It did boot up.
498340	501400	It did give me a shell prompt, but I couldn't interact with it.
502820	506900	So and clearly the serial port was working because I could interact with U-boot.
506980	508940	So they did something there.
508940	512180	I was like, okay, well, let me go back to U-boot and see what I can do.
512500	517660	U-boot, if you haven't played around with it, super, super fully featured boot loader.
518000	519040	It's really awesome.
519440	528160	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.
528160	534520	One is UBI, which allowed me to read sections of the flash chip into memory.
535600	543240	And the other was TFTP put, which allowed me to write sections of memory over TFTP back to my laptop.
543300	552640	So I pulled in a section of flash that was based on the flash layout in U-boot, supposed to be the file system.
553380	559860	Read that into memory with UBI command and then used TFTP put to send that over to my laptop.
562120	562980	It was unencrypted.
562980	564440	It was a squash FS image.
564580	565180	Awesome.
566000	574960	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.
575680	576720	But I was kind of curious.
576720	582400	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.
583400	587300	So started poking around at some of the binaries inside that squash FS image.
587720	589600	They had a binary called commander.
590520	592180	Again, a really interesting name.
592620	603200	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 .
603460	606320	So you can just run strings on it and pretty much see everything that it's doing.
607740	617160	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.
617160	624180	They had like these kind of encoded offsets that didn't really correspond to actual flash addresses.
624180	630100	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.
630140	636280	So in the temp directory, under temp photo key.firmware is where they were keeping the firmware key.
636280	641800	So I said, okay, well, if I can actually get access to the live running system, it should be sitting in temp.
642220	643940	Maybe I can just pull it from there.
645280	647640	So I started poking around with some of the binaries.
648220	652900	I do work at a firmware company, so we have some fun internal tools.
652900	654460	I threw some of the binaries through.
654540	659580	And one of the interesting things that popped out was the web server had a potential command injection.
661200	672040	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.
672540	684060	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.
684480	690780	And normally this is a drop down box that's like, you know, Africa, New York, whatever your time zone is.
691540	695520	I told it my time zone was back tick Telnet D back tick.
696600	698140	It accepted it.
698140	699880	It kicked off a Telnet server.
700820	706200	So I was able to Telnet in, drop me to a root shell, and I was able to pull the key for that device.
707460	710520	And that key worked for all the other firmware for that device.
710520	714460	So I could decrypt the current firmware as well as all the previous releases.
715120	725020	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.
725360	728300	And Amazon has a very lenient return policy.
729160	736880	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.
737640	741300	So I have a bunch of keys for a bunch of D-link devices now.
741320	742900	I can just decrypt all their firmware.
744500	749460	I did put this into a tool called D-link, D-E-L-I-N-K.
750640	753360	And it's also been integrated into the latest version of Binwalk.
753360	760700	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.
760700	761640	So that's nice.
762780	764280	But that's the talk.
765500	767620	So open it up to Q&A.
767620	768860	Anyone have any questions?
772600	773320	Yes.
780500	782920	I'm talking fast because I only have 30 minutes.
782940	786900	Took a little bit longer than I might let on, especially that last one.
787660	794780	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.
794780	797420	I mean, it takes, it's a couple of grips.
798480	799820	It's not that difficult.
800020	805020	When you're talking about actually reverse engineering, I mean, that always takes a while, right?
805020	808740	I mean, I'm up here saying, oh, yeah, I use these U-boot commands.
809360	815680	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.
816320	820780	I had to figure out the flash layout so I knew where from flash I needed to read.
821880	823200	It's a little time consuming.
823580	824900	You're going to go down rabbit holes.
825060	826420	Don't pan out.
827720	834020	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.
834580	839880	But again, once I had it, I could pull all the keys for all of their stuff.
839880	842200	So I was like, okay, that's worth it.
842200	843200	Yep.
849980	852000	So I did consider that.
852000	855720	I mean, the first thing I thought was, oh, it looks like a SPI flash chip.
855920	858140	You know, I can just pull that and read off of it.
859460	865280	I am glad I went down this route instead just because it was a little more scalable once I figured out.
866220	871980	Now, if I had pulled the flash chip, I might have been able to get the key a little more easily.
872840	882080	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.
882780	886780	So I may not have been able to pull the keys directly from the flash chip.
887040	890140	But I did not investigate further, so that may or may not be true.
890740	906020	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.
906020	906780	Absolutely.
913580	916260	Like they encrypt at the flash layer?
916520	917040	Yes.
917040	924940	So, now what I will say is I haven't dealt with anything where they have an off board decryption.
925000	927060	They have some custom hardware I see.
928260	938900	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.
939400	942720	That can be a lot more difficult for a couple of reasons.
942720	952940	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.
954260	955520	So that takes some time.
956460	962820	But, yeah, there has to be something at some point that's going to decrypt the code into something the processor can understand.
963360	975060	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.
975820	979860	That's kind of the Achilles heel of encrypting with anything hardware.
980940	988180	If you have a device that uses the encrypted firmware and you sell it to me, you just sold me a decryption device.
988260	995580	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.
995740	1000620	So, you know, with enough time, effort and resources, you can break anything.
1000940	1003400	But that's, I guess, everything, right?
1004260	1005500	Was there another question?
1011840	1014240	So, Giger is great.
1014680	1018260	I think that's kind of become a huge standard just because it's free.
1018620	1020840	Personally, I prefer Binary Ninja.
1021080	1026620	It costs a little bit more, but it's not prohibitively expensive.
1027680	1030480	IDA, if you get your employer to buy it for you is awesome.
1031460	1037320	I used to use IDA all the time, but Binary Ninja has gotten pretty good, and so has Giger.
1037880	1041360	I can't justify the cost, so I don't use it anymore.
1042560	1044120	But, yeah, those are the big ones.
1046220	1052300	I mean, some people prefer Radare, I never know how to pronounce it.
1052300	1054200	It's spelled radar with an E on the end.
1055820	1057900	So a lot of people like that one as well.
1058880	1059280	So, yeah.
1068860	1070900	So, BINROC has a bit of a sort of past.
1070900	1073120	It's technically owned by Microsoft right now.
1074340	1077700	So when I created BINROC, it was my own personal project.
1078580	1085560	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?
1085560	1087600	I'm like, yeah, sure, I don't care.
1088380	1092000	Then we got acquired by Microsoft, so Microsoft technically owns it now.
1092420	1096200	I'm still the sole maintainer on it, even though I'm not at Microsoft anymore.
1097560	1102480	But I can't put them into BINROC, because it is technically owned by Microsoft.
1102500	1110380	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.
1110620	1117800	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.
1135330	1135850	Yep.
1135850	1141390	Not at this time.
1142530	1157070	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.
1158110	1161750	But it's not a guarantee that you can decrypt it necessarily, right?
1162390	1169010	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.
1170370	1179710	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.
1180890	1191030	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.
1191170	1197010	Or they're using some sort of good compression and obfuscated it.
1197010	1198990	Or maybe it's some custom thing they wrote.
1198990	1203210	But, yeah, high entropy is usually encryption or decent compression.
1208260	1209400	Nothing else?
1228260	1229460	All right.
1229460	1230640	Thanks for coming out, everybody.
1230640	1231560	Appreciate it.
1236560	1239820	Hopefully the recording picked up some of the slides.
1240020	1242840	I was kind of jumping through them because I wasn't really presenting them.
1242840	1248040	But, yeah, if anybody wants a copy of the slides, come see me.
1248040	1249240	I'll give you my e-mail.
1249480	1250660	We'll post them up somewhere.
1251220	1251640	Yeah.
1251640	1252540	Have a great time.
