Battery Firmware Hacking
Miller, Charlie
Ever wonder how your laptop battery knows when to stop charging when it is plugged into the wall, but the computer is powered off? Modern computers are no longer just composed of a single processor. Computers possess many other embedded microprocessors. Researchers are only recently considering the security implications of multiple processors, multiple pieces of embedded memory, etc. This paper takes an in depth look at a common embedded controller used in Lithium Ion and Lithium Polymer batteries, in particular, this controller is used in a large number of MacBook, MacBook Pro, and MacBook Air laptop computers. In this talk, I will demonstrate how the embedded controller works. I will reverse engineer the firmware and the firmware flashing process for a particular smart battery controller. In particular, I will show how to completely reprogram the smart battery by modifying the firmware on it. Also, I will show how to disable the firmware checksum so you can make changes. I present a simple API that can be used to read values from the smart battery as well as reprogram the firmware. Being able to control the working smart battery and smart battery host may be enough to cause safety issues, such as overcharging or fire. Charlie Miller is Principal Research Consultant at Accuvant Labs. He was the first with a public remote exploit for both the iPhone and the G1 Android phone. He won the CanSecWest Pwn2Own competition for the last four years. He has authored two information security books and holds a PhD from the University of Notre Dame.
 Alright, so if you can tell if you follow me on Twitter, I have a terrible cold. I've had awful Defcon Been sick the whole time. I didn't get to play soccer. I haven't you know I've been sober for 72 hours and I can barely talk So I'm gonna do my best and you know bear with me, but I do have a backup plan So here's my backup plan. Let me see here If I start to phase out here, you'll be here this Welcome to my battery hacking talk Is that good? Should I do that instead? All right, I'll try it if I get too nasally. I'm gonna go back to the computer. So so hopefully that won't happen Okay, so who am I? And sorry about the screen. I guess they had to move it up. So so people in the front could see So I used to work at the NSA for five years doing secret stuff And then when the iPhone came out I hacked it and when the Android phone came out the G1 phone I hacked that one phone on last four years. I've been sporting the stylist jacket So if you see a guy, you know wearing a white jacket for some reason, that's probably me So everyone who won this year got the jacket for some reason I'm the only one who wears it written a couple books and that sort of stuff and then the most important thing is there's an info So comic that has my name in it. So I feel pretty cool about that. Although. I don't I don't understand this particular comic But maybe that's because it's about me Okay, so why am I why am I here? Why did I decided to do battery firmware hacking? so Last year I came to to black hat and Defconn and I saw Barmy Jack's talk and I thought it was really awesome So I was like, this is cool that you know, it's easy to explain to someone you walk up to an ATM and it gives you money Right, this is this is what it's all about. It's not like hey I can make calculator pop up on your computer like it's just not impressive and You know, that's what I usually do. I usually do less impressive stuff than that It's like I can make this bit change or something right so So then I was like I need to do something cool like Barney me So I came across this YouTube video of this, you know computer like catching on fire and like the flames just going feet Hi, I was like I want to do that. So So I set out like in September last year like right after black hat I was like, I'm gonna see if someone remotely could blow up my computer and And you're gonna find out I actually didn't do that, but I made a lot of progress and so it's sort of interesting anyway and Maybe someone who's a little smarter than me can actually finish it up for me and also I'm kind of a chicken So you'll see that you see about that too Yes, this is the spoilers. You know despite what you might have read in the paper. I actually didn't blow up anything Mostly well, you'll see there's a lot of layers of security. I totally bust one of them and and then I got kind of scared so There's there's like all these parameters you can switch and you got charge it and discharge it and maybe overnight and I work Out of my home and you know, so anyway, it's kind of a chicken shit, but you'll see that it's kind of cool So what's the talk about so mostly I'll start out with a little sort of intro on how Batteries work and that sort of thing and then I'm gonna basically tell you the story of like seven months of my life looking into this and You know, it's it's mostly a story about a guy who's good at software hacking who tried to do hardware hacking So the good thing about that is, you know, probably not you guys who are in hardware hacking village Which I should actually go to and hang out But the guys who do software you'll probably learn some stuff Hopefully about how to do hardware because that's what I had to learn and then I'll talk about like, you know This so what question since I didn't blow up something something. What did I do and what could I do? All right, so so when I started this whole thing, I didn't know anything about batteries or you know chargers or really anything about hardware So I'm giving you a little bit of a spoiler here because just to sort of make sense of what I'm going But I didn't know this when I started so there's this document called the smart battery specification And what it it does is it outlines exactly how batteries and chargers and computers are supposed to communicate with each other And so this is this sort of justifies, you know, what I went after so it says, you know safety is a primary concern It's great the primary concept is that the primary intelligence is supposed to be in the battery itself so You know the computer smart it doesn't have to be the charger doesn't have to be smart What's supposed to be smart in this whole thing is the battery so the brains are supposed to be in the battery and the reason is because When the the battery knows the most about the battery so it knows how much charge it's gotten it knows how much You know how much it's got stored in it and it knows what its temperature is and knows what kind of chemistry it has so the The battery is supposed to have to be the brains of the operation and everything else is supposed to rely on that And so that's why I attack the brains So I'm going to go after the little the little chip on the battery Okay, so What so what are smart batteries right? I just thought these were like duracelles well they're they're duracelles with a chip So are actually more than one chip you'll see so There's just a you know There's chemistry cells and then there's some some chips and this main job is to make sure that it's it gets charged and it doesn't blow up and if you if you have a MacBook like me and you hit You know the the more information about your system you end up with a screen that looks like that if you can read it and on this one On when I started it didn't say this one this one actually if you look it says device name BQ 20z 451 That's the model the chip from from Texas Instruments on this battery But anyway this this information is obtained from the battery by the the operating system asking the battery and the battery telling it All right, so like you know what what could go wrong with a battery Right, so you could you could make their battery not work anymore and this is super easy. I do it all the time never on purpose though and then You could you could reprogram it to you know why and this is what I do Maybe this would allow What what the engineers called thermal runaway and I call like explosions But I didn't do it so I can't say and then the other thing is you can imagine like just talking to you know have the battery talking non-stop to the To the you know OS like you know Dan Kaminsky Defcon talk and then eventually the OS is gonna get bogged down Dang everyone loves Dan Kaminsky at black hat that would have nailed it. Okay, so And then you can imagine it also if there was a bug in the kernel It could actually attack the kernel from the battery and you figure like you know Apple can barely Write secure software when they know someone's gonna attack them But when they're like, you know reading values from a battery They really probably didn't pay attention and then you you're also on the same bus as like a TPM chip or the bio So you could sniff all sorts of other interesting things That you wouldn't normally have access to because you're at this other spot and hardware I see there's things that you might be able to do with this Okay, so so you know the story was again, you know after black hat I sat around you know mopey trying to think of something interesting to do and eventually I was like yeah Let's see if I can blow up a computer So I was like well, you know, I wish I knew something about hardware So I said I know I have a computer and I have a battery. What else? Well, let's just use Google That's what a lot of this talk is I don't know what I'm doing. So I Google stuff So so in this case I Google MacBook battery firmware, right? That seems like a reasonable thing to start and sure enough the very first hit is about a firmware upgrade that they that Apple put out in 2009 and I was like sweet at software I can look at this I understand this so I downloaded it checked it out reverse engineered it and I found out a few things so one thing is that you know the the computer is trying to talk to the battery and it just wraps This function Io connect method structure is structure zero function over and over and over it just keeps calling this and turns out This is a function that you use in OS X to talk to kernel drivers like texts So so this is the way you do it and the particular driver they're talking to is called Apple smart battery manager So the you know that your user land program it has to have root privileges is but it doesn't have to like have you know Wires plug into hardware anything it can it can talk to the battery by using calling this this method So then you know, so I was like okay cool So I noticed that there's this sort of associated source code bundle on the Apple website called Apple smart battery I actually it's called power management package you can download it has like some cool source in it But it doesn't build like they don't they give you enough just to like say that's open source, but not enough to actually use it So but the cool thing is they have a bunch of header files And you can start to read some interesting stuff about like some of the commands that that this this firmware upgrade is doing So what's the firmware upgrade do well based on that header file I can see it does things like It looks at the device name and compares it to a list of things to see whether it this is something it should be upgrading Looks at the firmware version looks at the pack I code again making sure it's something, you know the right one to upgrade And then some things that it's not in the header files I don't know what those things are and then I came across this and I was like that's weird It doesn't look like the rest of the program instead of just calling that function over and over It's got these like weird numbers like 414 and 3672 like these stick out to me Now like I wonder what that is so they're like well, you know, I don't know I'll Google so so you Google that number and And and the very first thing is it's like the default is 36720414 and It's like this is entered by sending the data for 1 4 to address zero and it's like look back at here Look, that's what it's doing and then it's like and then immediately after saying 3617. That's exactly what it's doing So what does it mean and then you can look down a little bit? And it's like the default code is set to unsealed to full and it's like talking about like, you know All sorts of stuff and so you Google a little bit more and I come across this document from Texas instruments That talks about you know, the unsealed key is this magical number. I see I was like, oh, okay It's like, you know unsealing the battery and then I was like, oh look, there's this other thing called full access key that sounds even more important and It's it's ff ff ff. I was like, well, let me try that. Hey, it worked so So this is basically the whole talk is a slide is you know You know they use this chip and you can download the spec and they didn't change the passwords on it So it allows it allows you to and this is on every Apple laptop. I've ever seen I have a bunch of them and Apparently there's a bunch of other computers that use the same chip But I don't know if they've actually changed the password. So I mean like the chips. Okay. It's just Apple is stupid and didn't change So anyway, so now I was like sort of in business because a I know I kind of I know it comes from Texas instruments because that's where I got the doc from I Know Apple is done, but I already kind of knew that and then That's that's where I'm at right now. So I'm feeling pretty good But I still don't know exactly what chip. I just know it came from Texas instruments So I had to figure out what that was and you should know by now that like, you know I could have just like opened the battery looked at the chip, but I'm totally scared to touch stuff So I've still wanted to find a new software way to do it And so I did was there's a way you could query the battery for particular types of data with these different what they call subclass IDs And so I just did that I said, okay Give me your data for subclass ID zero one two three four And it would give me a certain amount of data for each one and all the Texas instruments chips They have documentation on what that's supposed to return and each one returns something a little bit different And so I just you know saw what mine was and I looked at every single doc To see which one had sort of the same kind of it returned the right amount of data and I found out what chip it was by doing that It's a Texas oh right here. I won't give away the story yet. So anyway, this is what my return subclass zero was 22 bytes So you can see like what kind of stuff this is this is the kind of stuff like you're messing with now that you probably shouldn't be So it says like first level safety is subclass ID zero so returns 22 bytes subclass ID one is first level safety current the other one was was voltage and then you keep going then you've got like second level safety and then You know single safety current single safety bolt and so on and it turns out it matches exactly this Texas instruments chip called Ti BQ 20s 80 and so all the MacBook and MacBook Air's and MacBook Pros and all these Have some sort of chip really close to this not not all necessarily the 20s 80, but all this stuff. I'm talking about all works on all them Okay, so that was my like totally weenie way to figure out what it was and then eventually I got it was like well, I should at least get my hands a little bit dirty So I took a battery and I ripped it apart and so if you take a battery out of your computer It looks like this and there's these little screws So you unscrew it and then you just like rip off the plastic part And this is what you see on the inside so you got six with the impalmer cells And then on the end I go over here is like where all the electronic stuff is so then you rip that off and it's like well There's lights on and I'm touching it. It's like for software got really creepy So there's like chips and stuff and you can open it up even more and then you can start to read the labels on the chips So there's one that's called BQ 29312 you can download information about that this job is basically it does safety stuff And then so you know let's this talk, you know there was a newspaper and all that stuff and Travis Goodspeed who's like a total hardware nut Just because he's curious he takes one of his batteries and he you know rips apart the chips He puts an acid bath and x-rays it and this is some pictures he did just because he's a super curious guy So this is what the chip looks like under x-ray And then on the other side of that board you got the BQ 2080 which is the brains of the operation And then this other chip 29412 which is another one of these chips that does safety checks So and then here is the BQ 2080 thanks to Travis for these pictures Pretty cool So anyway so the BQ 2080 is the main brains that's the chip you can talk to through that driver The other two chips you can't talk to directly they're in charge of some safety stuff But what you can do is you can set the parameters in the 2080 and it will push out those parameters to the other chips You can configure those other chips you just can't talk to them directly but that's all you really need So like you can say oh yeah you're in charge of the second level safety suite Yeah yeah don't go off until you're at like you know five million degrees and then everything's bad But until then it's cool so you can still mess with those other chips Oops I skipped this slide So this is the show again like it took me that long to figure out what kind of chip it was Actually if I would have paid attention the actual header file they'd tell me exactly what it was Oh well that's why you know that's why we do it and that's why it took me seven months instead of my normal project which was like two weeks Alright so where am I at now so I know you know I actually took it apart so I know what kind of hardware it is I can download information about it I know sort of I can see how the this firmware updater talks to it so I can you know probably figure out how to talk to it But I don't really know like what kind of things to say to it So back to this document the smart battery specification It describes exactly how this sort of communication works So there's three components there's the battery which is in the middle and that's what we've talked about so far There's the charger which is this thing that you know decides somehow how much charge to deliver to the battery or not to charge it And then there's the host which is like the computer or the thing that needs to get power So basically this is the way they can communicate it's on this bus the SM bus And the SM bus is based on I2C which Stephen Ridley gave this really cool talk about There's lots of things in the electronic role based on I2C Anyway so they talk to each other and it's sort of complicated but I wrote this API that makes it easy to talk to the battery So and I released it you can now load it and play with your battery but just be careful that you don't break it Or at least don't blame me you can break it if you want So anyway so you can do things now like read a word like read the serial number from the thing You can unseal it if you know the password which we do You can write a word so you can change what the manufacturer date was You can change the device name which is what I'm doing here And then you can read data flash which is like all that stuff I was talking about about getting the idea stuff So here I'm reading some data flash whatever 57 was I don't know which one it was Then you can get full access which I'll talk about in a minute which is like the equivalent of like root or whatever on the battery And then you can seal things back up when you're done So what kind of things can you can you know a computer or can we say to the battery So there's this document that says exactly what to do so there's these things called standard commands which is the first column You can do things like ask it what's temperature, what's voltages, how much charge it wants stuff like that And then there's in the next column extended commands so these are things like a little more complicated And then there's other things you need more privileges to do so like you can set passwords You can control the FETs which is the little thing that physically closes to make electricity run into the battery And then you know keys and all that kind of stuff And then there's this data flash this is all just like data like that you can configure the device with So there's lots of like interesting things you can imagine playing with right So you can you know change the chemistry you can change the you know all these thresholds where like bad things are supposed to be happening And so I did that right you know the first thing I do is like you know how about no over voltage protection let's change that But what happens is the battery still just you know it gets its charge and it knows when it's done and it stops So just changing those thresholds those are like safety features when things go wrong If you don't make things go wrong somehow then you don't need those thresholds they're not ever they're not ever hit So you have to do something more so you know we talked about how we can unseal it and then we can get full access But there's other modes that are even more powerful so there's like boot run mode which I'll talk about in a second And that gives you like very low level access like you're sitting near the TI guy sitting in the factory So these are all the different modes that you can put the battery in So sealed that's how it's supposed to ship from the factory let me see if I have slides yeah I'll just go in So I couldn't remember because so the problem with this talk is it was at blackout it was 75 minutes and now it's 50 minutes And I can't remember for sure what I cut so bear with me Okay so sealed I guess I thought this was really important because I didn't cut it So it comes from the factory you're not supposed to mess with it this is how your batteries are supposed to behave You can't change anything you can't configure it you can't do anything So you can only do the standard commands which was that first column and even there you can only read them You can't write to those so you can't set anything if you unseal it which is what the former upgrade did So in a sense it wasn't bad that they use the default unseal command because I could have just reverse engineered anything they chose The bad thing is that they sort of gave me the idea to try the default for the full access and then they didn't change that Anyway so in the unsealed mode you can then access the some of the extended commands you can read the data flash and write the data flash At that point you can start to do some real configuration of the thing And this is again what the battery firmware upgrades go to So full access mode then you have all SPS commands so again this is like root All SPS commands you can write to any of them that allow you to write You can enter these other like super privileged access modes boot ROM and configuration mode And the Apple firmware upgrades don't even need to access that So one of the modes is called configuration mode and this is basically the way that you can configure all the little internal sensors and stuff So the little internal sensor might think it has 2000, I don't know So it measures the amount of current passing into it and it thinks it's 20 milliamps but really it's 22 and so you tell it that And it configures itself to know that oh yeah that was 22 and so you can imagine definitely screwing with it in this mode I didn't mess with that mode So the things you can configure are number of cells so that means like 6 in this case Current, how much current and you're giving it, how much voltage it has and what its temperature is So these are the things that this document I found tells you to do I was like gee I wonder if there's any other calibrations you can do besides those because this sounds like really fun And I don't know and it turns out if you Google what other calibrations for BQ20 Z80 besides temperature and voltage Google actually doesn't help in that case So I just went to the store so I asked the Texas Instruments people So I get on the board and I'm like hi yes can I do anything besides this and this really nice employee named Jackie writes back And she's like no only current voltage and temperature I'm like okay thanks and the only reason I put this up is I think it's really cool because they have these message boards there And you earn points and it's like a video game and like I have 105 points at this point in this game And it says I'm a prodigy and I like that the game realized that you know while I don't know much about batteries I have a lot of potential So I felt really good about that Okay so then the other mode that I deal with a lot is called boot-run mode And this allows low level access and in the regular document that talks about SPS it doesn't talk about this at all Because really you're not supposed to be doing this unless you know what you're doing and obviously I don't So I had to buy some hardware to help me out So and again this is something I bought just to help figure out things you don't actually need this to do all the sort of attacks I talk about So I bought this evaluation module and it's basically the same exact thing that's on the battery except it's like a big board You can look at it and you can play with it and the most important thing is it comes with Windows software that talks over USB to the chip And you know we'll like program it and stuff And so I can just sort of emulate what it does So this is what it looked like at my house sitting on my like dusty floor And then you'll also notice there's this radio shack you know device that I bought there And I was like well I'm a child of the 80s or whatever so I was like those dudes at Radio Shack they know a lot So I went in there and I was like so I was sort of hint around at my project you know I'm like well you know I work for a consulting company And we're evaluating the safety of batteries and tell me about batteries and what do I need to test batteries And the guy was like can I interest you in a mobile phone you know So it was not helpful at all So yeah and it was amazing but there's a lot of people who still shop there surprisingly So anyway this is what the software looks like and this is where I got the little guy for the title screen It gets really cool they have like a you know mascot for battery technology It's like a clippy with a backpack on So this is what the screen looks like and this allows you like the ragui to change all the SPS commands And it shows you all the you know bit fields and what they mean and stuff And you know so here's the data flash and you know again it tells you what each one means What the value is what it changes to but of course the first button that I push when I see it is the pro button Right so it says like this screen is only for advanced users and you know I'm certainly an advanced user So it lets you do things like send raw commands and then the best thing is it lets you flash the firmware Which is something I really want to do so then what I wanted to do is see how it's doing all these things And then write some you know write a driver or something for OSX to let me do it Unfortunately it's a Windows program and I'm trying to do things on max But anyway the evaluation kit comes with this SREC file which has the firmware on it And so I want to reprogram the thing and sniff what it does to know how to talk to it on this little level Because there's no documentation I've seen yet that tells you that So I Google I look around and there's this SREC file it says it's an encrypted or the sync file That's what the ENC is for encrypted So it's an encrypted SREC file and so like I reverse engineered the encryption it's like XOR But you know to give it you know some credit it's not just XOR with a byte It's like XOR with a byte and the previous byte so it's like you know sort of fancy Anyway the firmware file has header stuff it has all the data flash So those are all the configuration parameters it has all the instructions and then a bunch of checksums and stuff So I wrote a PID bug script to intercept the USB traffic and then I could see exactly the sorts And then by comparing it to the things I saw when I would just do a single byte write and all that kind of things in the GUI I could figure out sort of what was going on So for example I figured out like oh to read a word apparently you send the command 8 So write a word you send the command 4 and so on So then I'm back to Google right so I Google SMBus bootrom read 8 write 4 So I was like I don't know how anyone did any research before Google And no Google did not pay me for this talk I should have you know had Microsoft pay me I could say yeah I used Bing Bing did it for me So anyway I came across this document that talks exactly about like how to do talk on the bootrom level And also it talked about how the firmware and the instructions were laid out so that was like really useful too So now when I sniff the reprogramming I see these are the steps it does so it erases everything Instructions and the data and then I can see like how much data it expects Then it reprograms row by row the data and then it reprograms the instructions so there's 300 hex 300 rows of instructions Cool And then I can see the data too so like I could piece together by the data it's sending what the firmware looks like So I pretty much understand now how to write in bootrom to reprogram it just by sniffing I could probably figure out how to read you know how to dump the firmware but at this point in the story I haven't I could get the data flash But the problem is so I could either have the firmware I could either probably dump the firmware Or I could just watch the firmware being uploaded from the SREC file but I don't know like what the hell is in there So that's the next thing I want to do so I want to disassemble it and reverse engineer see how it works But the problem is I don't know what kind of chip it is I know Texas Instruments makes it but I don't know what kind of like you know assembly it takes So you know I do this stupid thing which is I load it into Ida Pro and I select each processor one by one and see what happens And they all suck so it's none of the ones that are in Ida Pro So I'm back to asking Texas Instruments So I'm like hey does anyone know what kind of you know what kind of processor this is you know thank you and I try to be like really nice And I'm like you know and I'm not lying I'm saying I'm Charlie Miller you know if they Google they'll see who I am and you know So I'm just my approach is just to be nice and hopefully Jackie will give me a nice answer But instead of Jackie this time I get this asshole named Doug Williams and he says proprietary that's it one word answer I'm like don't you know I'm a prodigy So anyway I'm like oh thank you for your kind response can you give me a little more information like do you mean this is something you just don't want to tell me Or do you mean like you made this in your basement and like no one knows what the hell it is And he says this is the first thing we have customers you know Apple probably who create their own firmware But we don't disclose this data because we want to protect our intellectual property Sorry so I'm like well okay I'll just steal your intellectual property So I just take the binary I stick it into a hex editor and I just start staring at it right This is the worst job in the world And actually someone asked me about this part and when I gave this talk in black hat and they were like wow you like some kind of genius for looking at this I'm like no this is the beauty of like you know the cooking show where the guy throws a bunch of stuff to cook the oven and they're like hey it's done right So like this was like really hard but in the slide deck it's like oh yeah it was so obvious right but this was actually like took me like a week it was really hard But you know for your benefit it looks like really cool like I'm awesome So anyway so what do you notice in this anyone want to beat me to the punch so is there anything special can you read the bytes at all Anything stick out oh I already said and don't look at that bullet if you want to play my game Alright so you'll notice there's threes right that's the only thing that you really notice is there's some threes Okay cool threes and anyone notice like you know something special about the threes Sorry well they're spread out a certain amount so it turns out the threes are separated by two bytes each so it's like oh well that's sort of interesting So that's like well maybe then it's aligned somehow on you know three byte widths so you know adjust the hex editor a little bit And then you start to look hey look sure enough if you look down this columns there's this feature that the high nibble of the third byte is always zero one two or three Okay so maybe and then you can see this is true for all the columns so maybe this is a 22 bit word so I gotta find some sort of chip that has 22 bit length instructions Okay I'm in good shape and you know that's probably pretty rare Okay and then the final clue is I looked at the end of the firmware and so what you see here is the very last so there's lots of these things at the end that say 3FFFFFF And the very last thing before that is 23FFFFFFFF So here's the 23FFFFFFFF Like what would you think would be the very last instruction in something that you disassembled Ah see I am smart I knew the answer and I don't think I've heard the right answer yet keep going What's at the end of like big functions little functions? Return right so I'm like well maybe this is a return because you know that's how I end my functions And then any guesses what 3FFFFFF is? What's that? Ah I heard it so what do you put at the end when you don't want to do anything? No ops so that's what I'm thinking right so no no no no no no no So let's see if I'm right so the first thing to see if I have maybe I'm just like totally crazy So I was like well let's search for this thing that might be a return in the whole file Okay so there's 410 times it shows up cool and they're all sort of like randomly spread about also cool And also I'm very happy to say that they didn't like encode or encrypt the firmware that would have really made this sort of analyzing in a hex editor harder So anyway I'm pretty sure this thing is a return by now and those other things are no ops I know 23 bit instructions so probably that might be enough to go back to my best friend Google But my Google foo was weak I could not find it But my co-worker Deon Losacus he came up with a Google search that did work So he searched for 23FFFF, 3FFFF22 bit I don't know why I didn't think of that So anyway I come across this document you know to be fair to show how hard this research was it wasn't the top hit it was the second hit So you see things like it says blah blah 3FFFF is a no op you know return is 23FFFF so like this is pretty much business So you look in there and it says something about cool risk and then you Google for that and you come across the document And you find out that the BQ20Z80 is a cool risk C816 chip or at least something so close that it doesn't matter So at this point we're in good shape So I can read all about it and yeah so who saw Hackers last night? It's a great movie and you know this is the best line I think from it Angelina and Julie talking about risk architecture is like a total awesome thing So anyway so this particular chip it's 8 bit it has this Harvard risk architecture You can see the data flash 64k 64k flash instructions each 22 bits There's 8 bit registers and of course Ida Pro doesn't support it So you know this is what the registers look like and luckily since it's risk and risk is going to change everything There's not too many instructions and so this is what there are there's like you know 40 or something And so basically all you have to do if you like Ida Pro like me is go in and you can write in Python Ida Pro processor script And tell it what each of these instructions means and what it looks like and then it can disassemble it So that's what I did so this is another 2 weeks of my life in one slide So you just go in this table in the document it explains exactly what the bits are you translate it into Python And then you've got a new setting on your Ida Pro that says Texas Instruments Cascades BQ20s80 And I'm really this is all released to so you can download it and disassemble and see how these things work now Oh, I sorry you can't they're proprietary So so anyway now if you if you pop in Ida Pro you it's risk so you have to make a separate data section for it And then you're good to go and disassemble But the problem is that it didn't do that great job But I just can help it out so I know that it's you know the instructions are fixed link they're all going to be you know 3 by Aligned so I just want to find that script to go through and disassemble every single instruction because I know that that can be any data Because of the risk architecture so then now in business and disassembled the whole thing pretty much And I can start reading it and seeing what how this how this is you know code on the battery what it does So if you can if you can see that so so basically this is some some function that takes in those SPS commands that you can ask I for the temperature and stuff it does the operation and it sends it back to the computers this is exactly what we would kind of expect to see So this one if you if you can see it so on the one on the left it says you know so it's SPS command 21 which is the device name So it does some calculations and then sends it back this one is 20 which is like the manufacturer name sends it back and so forth Okay cool so so where are we now I can I can disassemble the firmware I know what's going on the battery but turns out things are going to go wrong for me very soon So I can disassemble it I can I can dump the flash cool What else can I do oh so then I started dump the flash right and I noticed that I'm not getting identical results when I flat when I dump the flash each time and you know that's sort of weird but and whatever And then the bad things are to happen right so then I like well let's let's let's make some changes to the firmware that's really what I want to do I do it The battery doesn't work anymore so that's bad the other thing wasn't so bad but this is bad if I try it so I like well maybe you have to do exactly what the Texas Instruments like GUI does So I do exactly what it does right so you know I'm sort of stuck And so I start to you know I get on the Apple store and I start ordering these batteries because I'm going through them so quick like faster than they can ship them to me I'm bricking them So and they're like $129 they're pretty expensive so I have a ton of these things and this I have just piles of them in my house that don't work anymore so it's a very expensive hobby Lucky I have a corporate credit card so I had one idea I was like hey I got an idea they have like on eBay they sell these batteries that you know some dude in China makes in this basement And you know they let's buy one maybe it's the same thing and then it'll be cheaper so I bought it and yeah I looked at it it's got the Texas Instruments chip and as far as I can tell it's pretty much the same But he changed the password on it so I couldn't hack it So anyway so the $40 battery was like super safe I was like oh well Okay so to start to fix my problems and stop buying batteries I like I wondered if like the Apple store like Traxxas is like this guy has bought like 10 batteries what's he doing And then like you should have a special like and he's Charlie Miller you know he hates Apple he's up to something so anyway it turns out by experimentation I found out that when I do the reeds from the firmware they're not always reliable and they don't error out or anything They just return like random crap sometimes and so what I did is now I just have a change the API that I wrote to like it reads it a few times and it makes sure that it always agrees and then it's okay And so once I do that then everything starts to go much better for me so I can you know consecutive dumps of the firmware like agree so this is good Alright so then the next problem by the way this is like the worst thing that ever happens to me in my research if you can see it the little X on the battery so when you see that that means you're screwed So that means you've ripped your battery well if you're doing my stuff otherwise it just means you probably don't have a plug in quite tight enough Well the other thing so speaking of the Apple store before I so the one thing you can do from this research I didn't blow up batteries but I know how to like manipulate them so if ever anyone has a battery that isn't covered under warranty anymore I can hook you up So I can you can go in and you can change when it was manufactured you can change how many cycles it's ever seen so you can make it look like it's like five days old you're like dude I just bought this battery like five days ago and it's not working I don't know what the problem is Well it's good so I help out the community when I can Alright so then I start so now I'm confident I can read firmware and I can write firmware but still it's not working I'm getting this stupid X that says I've done something wrong And so I look at the traffic and I figure out what it is and it turns out that it's this particular flag is set the data flash failure flag total failure so it's so you read it blah blah blah and it's like number one after a full reset the instruction flash checks I'm does not verify Ah crap there's a checksum I've messed it up so I need to figure that out but luckily I have the firmware so I can reverse engineer how they do that So it turns out that there's these particular functions that I can't change that are in the realm and one of them is in charge of computing the checksum so I just need to figure out who's using that function In an Ida Pro there's only two references to it one is to some SBS command that Google will tell you has something to do with checksumming and the other one is some function I haven't seen yet so that's my candidate so I check that out If you look at the Ida Pro double that it does something like it reads in and you know four bytes from data flash and then it compares it and if it's zero these four bytes are zero then it just goes to the end of function Otherwise it switches the ending this calls the checksum and then compares byte for byte if it's true and if it's not true it goes to the bad code and if it is true if they do agree then it goes to the end of the function So anyone have an idea what we should set those four bytes in memory to? Zero So if you set to zero then it doesn't compute the checksum anymore and you can make any changes you want But then so that was the older version of firmware some of the newer versions of firmware like they got tricky and they encode or they probably would say they encrypt the checksum And really I think they just did this because you might accidentally get zeros there someday but you wouldn't actually get this weird value So they do some encoding of the thing but the same basic principle applies that if you set these four bytes instead of the zero to an encoded zero then it won't check the checksum anymore So you can just do a raw data flash write to the device of zeros for those four bytes and it won't check the checksum anymore But since I'm totally scared to go into boot run mode with all my dead batteries I figured out a way to do it without going to boot run mode You can just figure out that there's this undocumented subclass 57 you can read and the checksum is actually in there so you can change those four bytes just with using extended SPS commands So now I can just freely patch the firmware and do whatever I want so what do I want to do? Well I can make it lie so now when you ask for the temperature I can make a return on anything I want But what I need to do is understand what things are asked of the battery and what things you know so those are I don't even know what to lie about So I bought some more stuff plugged it in and I was like okay now I'm going to sniff the traffic the battery has these six little grooves and then maybe a couple other little spots where there's wires Two of these are going to be S and BUS I don't know which two so I was like well I know I'll buy these little probes I'll hook them up between the things and you know I'll monitor what's going on This was a disaster so I did this and I must have touched the wrong thing at some time but the computer just didn't work anymore So apparently I shorted something on the main logic board and so I had to take it into the Apple store which I do this a lot I go to the Apple store and I'm like yeah this isn't working anymore And they're like what did you do? I was like I don't know I just turned it on and it doesn't work so they replaced the logic board and then everything was good again So yeah I never tell them what I do Okay so then I said well the problem was you know all these loose wires and stuff so I'm going to build my own special cable that connects the battery to the computer And so you know for someone who doesn't do any hardware this is like a big deal it took me like five days soldering and you know all this stuff I plugged in and it didn't work Piece of crap so that didn't work so finally I figured out the right way to do it is you just move the keyboard a little bit the battery can be sitting where it's supposed to sit You can still see the wires and you can hook little you know props to it and this is the right way to go So then I hook up this logic analyzer and I can see exactly which cables have the SM bus traffic And then you can actually ask it to decode it and you can see the actual SM bus right so it's like a right of eight which is temperature and then it reads the values so B73 so I can see what the temperature response is And then you know that's cool for just like show but then there's this other thing called a Beagle that will record all this information for a long time You get like cool gooey like this and you can see still that some bus traffic you can record it for like an hour while you're charging your battery and see all the things that are ever asked of the battery And so these are the exact things that the with the computer off and the charging that asked for battery status temperature charging current current voltage battery mode relative state of change Remaining capacity full charge capacity and of those only five ever change so the rest of them are just like yeah everything's cool or whatever so temperature changes but probably that's not so important Current which is the amount of current it's delivering well okay then voltage remaining charge or main capacity and relative state of charge so these are the things that you could guess the charge is asking for in order to figure out how much charge to deliver it So I'm gonna lie about those things so I gotta wrap this up quick but basically these are the things I can imagine doing now so where am I at I can make the battery do anything I want So what could what you know so what right so breaking the battery is super easy I'm an expert at it I'm not even just a project I'm a full blown expert So here is some code that with my API you can do that all this does is a race is all flash definitely not gonna recover from that So you can make firmware changes like we've been talking about so you can change all the things that are queried to lie so like no matter how much charge it gets you always like hey I'm 50% full keep it coming Maybe something bad would happen there so this is basically like a function that deals with SPS commands and I want to change the ones like so like remaining capacity which is SPS command F Instead of F I'm gonna say you want to know F I'll tell you 1B that's manufacturer date and same thing if you ask me for full charge capacity I'll tell you the serial number So I'm gonna just make changes to the firmware and the reason is because manufacturer date and serial number are both words which is the same thing as full charge capacity But they're never queried and you can send them any value you want so then you can control exactly what the battery is reporting and you can do this live while the battery is charging without any wires or anything just remotely with root access So this is what the code look like that handles that case and then here's the API call to patch the firmware and then it just changes that code to this code which just changes the SPS command and then jumps to the other case So now when I query remaining capacity you know surprisingly it's exactly the same as the manufacturer date and same thing full charge capacity you know coincidentally is exactly the same as serial number Okay so now I don't show it here but I go through and I have a white paper that goes in way more detail than I can hear but I go and I change the firmware to live on every single thing that's queried So now over time it doesn't it doesn't change what I want to I can change it on the fly and I did verify that you know these changes that I make do affect how much current is delivered to the battery but in like some sort of weird non like super obvious way So anyway this is where I like started to get scared and I tell the story that I was driving you know to a soccer game or something and I saw like fire trucks and stuff going the other direction and I was like oh shit I was just like messing with this stuff when I left and so I call home and you know my wife answers I'm like well that's good she answered the phone and then I was like you know that computer upstairs in my office can you go unplug that So she did that anyway so I mentioned that there's there's various layers of defense besides this chip that I'm like owning is the main guy in charge of safety but there's other things that may prevent the big explosion So there's these like thermal cutoffs and you see so it's like these little cells that they get hot they melt and then you can't get electricity to the cells anymore But you know the good thing about the batteries I can't mess with it the off-market one the bad thing is I didn't see these little things on the off-market battery The other thing you can imagine again is attacking the kernel from the battery so this would be like a way to have persistent malware for example I didn't look for a particular bug again like knowing Apple it's hard to imagine there's not a bug there So you can imagine writing a fuzzer in cool risk assembly and changing the firmware and having the battery literally fuzzing the OS that is crazy but it would be awesome The easier way is you can just do it in hardware just hook wires in there and emulate the fuzz and Travis gets beat it's talking about he already has something that basically does that So then if you know I don't really think there's much risk of anything bad happening to you from this without someone else doing something you know a lot more work But if you're the super paranoid type which probably 90% of people in this room are pretty paranoid I did I release a tool called caulk gun which basically goes in and changes the passwords to values nobody knows So source code basically looks like this it's just like yeah it gets full access changes the password and that's it So here's all the people who helped me on this talk you know I'm such a hardware noob that I couldn't get very far so thanks to all those guys Here's where you can download the paper the slides the tools I have I had a pro scripts I have the firmware IDB files everything you want to like learn and play with and that's it Thanks a lot.
 Alright, so if you can tell if you follow me on Twitter, I have a terrible cold. I've had awful Defcon  Been sick the whole time. I didn't get to play soccer. I haven't you know  I've been sober for 72 hours and I can barely talk  So I'm gonna do my best and you know bear with me, but I do have a backup plan  So here's my backup plan. Let me see here  If I start to phase out here, you'll be here this  Welcome to my battery hacking talk  Is that good? Should I do that instead?  All right, I'll try it if I get too nasally. I'm gonna go back to the computer. So so hopefully that won't happen  Okay, so who am I?  And sorry about the screen. I guess they had to move it up. So so people in the front could see  So I used to work at the NSA for five years doing secret stuff  And then when the iPhone came out I hacked it and when the Android phone came out the G1 phone  I hacked that one phone on last four years. I've been sporting the stylist jacket  So if you see a guy, you know wearing a white jacket for some reason, that's probably me  So everyone who won this year got the jacket for some reason I'm the only one who wears it  written a couple books and that sort of stuff and then the most important thing is there's an info  So comic that has my name in it. So I feel pretty cool about that. Although. I don't I don't understand this particular comic  But maybe that's because it's about me  Okay, so why am I why am I here? Why did I decided to do battery firmware hacking?  so  Last year I came to to black hat and Defconn and I saw Barmy Jack's talk and I thought it was really awesome  So I was like, this is cool that you know, it's easy to explain to someone you walk up to an ATM and it gives you money  Right, this is this is what it's all about. It's not like hey  I can make calculator pop up on your computer like it's just not impressive and  You know, that's what I usually do. I usually do less impressive stuff than that  It's like I can make this bit change or something right so  So then I was like I need to do something cool like Barney me  So I came across this YouTube video of this, you know computer like catching on fire and like the flames just going feet  Hi, I was like I want to do that. So  So I set out like in September last year like right after black hat  I was like, I'm gonna see if someone remotely could blow up my computer and  And you're gonna find out I actually didn't do that, but I made a lot of progress and so it's sort of interesting anyway and  Maybe someone who's a little smarter than me can actually finish it up for me and also I'm kind of a chicken  So you'll see that you see about that too  Yes, this is the spoilers. You know despite what you might have read in the paper. I actually didn't blow up anything  Mostly well, you'll see there's a lot of layers of security. I totally bust one of them and  and then I got kind of scared so  There's there's like all these parameters you can switch and you got charge it and discharge it and maybe overnight and I work  Out of my home and you know, so anyway, it's kind of a chicken shit, but you'll see that it's kind of cool  So what's the talk about so mostly I'll start out with a little sort of intro on how  Batteries work and that sort of thing and then I'm gonna basically tell you the story of like seven months of my life looking into this and  You know, it's it's mostly a story about a guy who's good at software hacking who tried to do hardware hacking  So the good thing about that is, you know, probably not you guys who are in hardware hacking village  Which I should actually go to and hang out  But the guys who do software you'll probably learn some stuff  Hopefully about how to do hardware because that's what I had to learn and then I'll talk about like, you know  This so what question since I didn't blow up something something. What did I do and what could I do?  All right, so so when I started this whole thing, I didn't know anything about batteries or you know chargers or really anything about hardware  So I'm giving you a little bit of a spoiler here because just to sort of make sense of what I'm going  But I didn't know this when I started so there's this document called the smart battery specification  And what it it does is it outlines exactly how batteries and chargers and computers are supposed to communicate with each other  And so this is this sort of justifies, you know, what I went after so it says, you know safety is a primary concern  It's great  the primary  concept is that the primary intelligence is supposed to be in the battery itself so  You know the computer smart it doesn't have to be the charger doesn't have to be smart  What's supposed to be smart in this whole thing is the battery so the brains are supposed to be in the battery and  the reason is because  When the the battery knows the most about the battery so it knows how much charge it's gotten it knows how much  You know how much it's got stored in it and it knows what its temperature is and knows what kind of chemistry it has so the  The battery is supposed to have to be the brains of the operation and everything else is supposed to rely on that  And so that's why I attack the brains  So I'm going to go after the little the little chip on the battery  Okay, so  What so what are smart batteries right? I just thought these were like duracelles well they're they're duracelles with a chip  So are actually more than one chip you'll see so  There's just a you know  There's chemistry cells and then there's some some chips and this main job is to make sure that it's it gets charged and it doesn't blow up  and  if you if you have a MacBook like me and you hit  You know the the more information about your system you end up with a screen that looks like that if you can read it and on this one  On when I started it didn't say this one this one actually if you look it says device name BQ 20z 451  That's the model the chip from from Texas Instruments on this battery  But anyway this this information is obtained from the battery by the the operating system asking the battery and the battery telling it  All right, so like you know what what could go wrong with a battery  Right, so you could you could make their battery not work anymore and this is super easy. I do it all the time  never on purpose though  and  then  You could you could reprogram it to you know why and this is what I do  Maybe this would allow  What what the engineers called thermal runaway and I call like explosions  But I didn't do it so I can't say and then the other thing is you can imagine like just talking to you know  have the battery talking non-stop to the  To the you know OS like you know Dan Kaminsky Defcon talk and then eventually the OS is gonna get bogged down  Dang everyone loves Dan Kaminsky at black hat that would have nailed it. Okay, so  And then you can imagine it also if there was a bug in the kernel  It could actually attack the kernel from the battery and you figure like you know Apple can barely  Write secure software when they know someone's gonna attack them  But when they're like, you know reading values from a battery  They really probably didn't pay attention and then you you're also on the same bus as like a TPM chip or the bio  So you could sniff all sorts of other interesting things  That you wouldn't normally have access to because you're at this other spot and hardware  I see there's things that you might be able to do with this  Okay, so so you know the story was again, you know after black hat  I sat around you know mopey trying to think of something interesting to do and eventually I was like yeah  Let's see if I can blow up a computer  So I was like well, you know, I wish I knew something about hardware  So I said I know I have a computer and I have a battery. What else? Well, let's just use Google  That's what a lot of this talk is I don't know what I'm doing. So I Google stuff  So so in this case I Google MacBook battery firmware, right? That seems like a reasonable thing to start  and sure enough the very first hit is about a  firmware upgrade that they that Apple put out in  2009 and I was like sweet at software I can look at this I understand this so I downloaded it checked it out  reverse engineered it and  I found out a few things so one thing is that you know the the computer is trying to talk to the battery and it just wraps  This function Io connect method structure is structure zero function over and over and over it just keeps calling this and turns out  This is a function that you use in OS X to talk to kernel drivers like texts  So so this is the way you do it and the particular driver they're talking to is called Apple smart battery manager  So the you know that your user land program it has to have root privileges is but it doesn't have to like have you know  Wires plug into hardware anything it can it can talk to the battery by using calling this this method  So then you know, so I was like okay cool  So I noticed that there's this sort of associated source code bundle on the Apple website called Apple smart battery  I actually it's called power management package you can download it has like some cool source in it  But it doesn't build like they don't they give you enough just to like say that's open source, but not enough to actually use it  So but the cool thing is they have a bunch of header files  And you can start to read some interesting stuff about like some of the commands that that this this firmware upgrade is doing  So what's the firmware upgrade do well based on that header file I can see it does things like  It looks at the device name and compares it to a list of things to see whether it this is something it should be upgrading  Looks at the firmware version looks at the pack I code again making sure it's something, you know the right one to upgrade  And then some things that it's not in the header files  I don't know what those things are and then I came across this and I was like that's weird  It doesn't look like the rest of the program instead of just calling that function over and over  It's got these like weird numbers like 414 and 3672 like these stick out to me  Now like I wonder what that is so they're like well, you know, I don't know I'll Google so so you Google that number and  And and the very first thing is it's like the default is 36720414 and  It's like this is entered by sending the data for 1 4 to address zero and it's like look back at here  Look, that's what it's doing and then it's like and then immediately after saying 3617. That's exactly what it's doing  So what does it mean and then you can look down a little bit?  And it's like the default code is set to unsealed to full and it's like talking about like, you know  All sorts of stuff and so you Google a little bit more and I come across this document from Texas instruments  That talks about you know, the unsealed key is this magical number. I see I was like, oh, okay  It's like, you know unsealing the battery and then I was like, oh look, there's this other thing called full access key  that sounds even more important and  It's it's ff ff ff. I was like, well, let me try that. Hey, it worked so  So this is basically the whole talk is a slide is you know  You know they use this chip and you can download the spec and they didn't change the passwords on it  So it allows it allows you to and this is on every Apple laptop. I've ever seen I have a bunch of them and  Apparently there's a bunch of other computers that use the same chip  But I don't know if they've actually changed the password. So I mean like the chips. Okay. It's just Apple is stupid and didn't change  So anyway, so now I was like sort of in business because a I know I kind of I know it comes from Texas instruments because that's where I got the  doc from I  Know Apple is done, but I already kind of knew that and then  That's that's where I'm at right now. So I'm feeling pretty good  But I still don't know exactly what chip. I just know it came from Texas instruments  So I had to figure out what that was and you should know by now that like, you know  I could have just like opened the battery looked at the chip, but I'm totally scared to touch stuff  So I've still wanted to find a new software way to do it  And so I did was there's a way you could query the battery for particular types of data with these different what they call subclass IDs  And so I just did that I said, okay  Give me your data for subclass ID zero one two three four  And it would give me a certain amount of data for each one and all the Texas instruments chips  They have documentation on what that's supposed to return and each one returns something a little bit different  And so I just you know saw what mine was and I looked at every single doc  To see which one had sort of the same kind of it returned the right amount of data and I found out what chip it was by doing that  It's a  Texas oh right here. I won't give away the story yet. So anyway, this is what my return subclass zero was 22 bytes  So you can see like what kind of stuff this is this is the kind of stuff like you're messing with now that you probably shouldn't be  So it says like first level safety is subclass ID zero so returns 22 bytes subclass  ID one is first level safety current the other one was was voltage and then you keep going then you've got like second level safety and then  You know single safety current single safety bolt and so on and it turns out it matches exactly this Texas instruments chip called  Ti BQ 20s 80 and so all the MacBook and MacBook Air's and MacBook Pros and all these  Have some sort of chip really close to this not not all necessarily the 20s 80, but all this stuff. I'm talking about all works on all  them  Okay, so that was my like totally weenie way to figure out what it was and then eventually  I got it was like well, I should at least get my hands a little bit dirty  So I took a battery and I ripped it apart and so if you take a battery out of your computer  It looks like this and there's these little screws  So you unscrew it and then you just like rip off the plastic part  And this is what you see on the inside so you got six with the impalmer cells  And then on the end I go over here is like where all the electronic stuff is so then you rip that off and it's like well  There's lights on and I'm touching it. It's like for software got really creepy  So there's like chips and stuff and you can open it up even more and then you can start to read the labels on the chips  So there's one that's called BQ 29312 you can download information about that this job is basically it does safety stuff  And then so you know let's this talk, you know there was a newspaper and all that stuff and Travis Goodspeed who's like a total hardware nut  Just because he's curious he takes one of his batteries and he you know rips apart the chips  He puts an acid bath and x-rays it and this is some pictures he did just because he's a super curious guy  So this is what the chip looks like under x-ray  And then on the other side of that board you got the BQ 2080 which is the brains of the operation  And then this other chip 29412 which is another one of these chips that does safety checks  So and then here is the BQ 2080 thanks to Travis for these pictures  Pretty cool  So anyway so the BQ 2080 is the main brains that's the chip you can talk to through that driver  The other two chips you can't talk to directly they're in charge of some safety stuff  But what you can do is you can set the parameters in the 2080 and it will push out those parameters to the other chips  You can configure those other chips you just can't talk to them directly but that's all you really need  So like you can say oh yeah you're in charge of the second level safety suite  Yeah yeah don't go off until you're at like you know five million degrees and then everything's bad  But until then it's cool so you can still mess with those other chips  Oops I skipped this slide  So this is the show again like it took me that long to figure out what kind of chip it was  Actually if I would have paid attention the actual header file they'd tell me exactly what it was  Oh well that's why you know that's why we do it and that's why it took me seven months instead of my normal project which was like two weeks  Alright so where am I at now so I know you know I actually took it apart so I know what kind of hardware it is  I can download information about it  I know sort of I can see how the this firmware updater talks to it so I can you know probably figure out how to talk to it  But I don't really know like what kind of things to say to it  So back to this document the smart battery specification  It describes exactly how this sort of communication works  So there's three components there's the battery which is in the middle and that's what we've talked about so far  There's the charger which is this thing that you know decides somehow how much charge to deliver to the battery or not to charge it  And then there's the host which is like the computer or the thing that needs to get power  So basically this is the way they can communicate it's on this bus the SM bus  And the SM bus is based on I2C which Stephen Ridley gave this really cool talk about  There's lots of things in the electronic role based on I2C  Anyway so they talk to each other and it's sort of complicated but I wrote this API that makes it easy to talk to the battery  So and I released it you can now load it and play with your battery but just be careful that you don't break it  Or at least don't blame me you can break it if you want  So anyway so you can do things now like read a word like read the serial number from the thing  You can unseal it if you know the password which we do  You can write a word so you can change what the manufacturer date was  You can change the device name which is what I'm doing here  And then you can read data flash which is like all that stuff I was talking about about getting the idea stuff  So here I'm reading some data flash whatever 57 was I don't know which one it was  Then you can get full access which I'll talk about in a minute which is like the equivalent of like root or whatever on the battery  And then you can seal things back up when you're done  So what kind of things can you can you know a computer or can we say to the battery  So there's this document that says exactly what to do so there's these things called standard commands which is the first column  You can do things like ask it what's temperature, what's voltages, how much charge it wants stuff like that  And then there's in the next column extended commands so these are things like a little more complicated  And then there's other things you need more privileges to do so like you can set passwords  You can control the FETs which is the little thing that physically closes to make electricity run into the battery  And then you know keys and all that kind of stuff  And then there's this data flash this is all just like data like that you can configure the device with  So there's lots of like interesting things you can imagine playing with right  So you can you know change the chemistry you can change the you know all these thresholds where like bad things are supposed to be happening  And so I did that right you know the first thing I do is like you know how about no over voltage protection let's change that  But what happens is the battery still just you know it gets its charge and it knows when it's done and it stops  So just changing those thresholds those are like safety features when things go wrong  If you don't make things go wrong somehow then you don't need those thresholds they're not ever they're not ever hit  So you have to do something more so you know we talked about how we can unseal it and then we can get full access  But there's other modes that are even more powerful so there's like boot run mode which I'll talk about in a second  And that gives you like very low level access like you're sitting near the TI guy sitting in the factory  So these are all the different modes that you can put the battery in  So sealed that's how it's supposed to ship from the factory let me see if I have slides yeah I'll just go in  So I couldn't remember because so the problem with this talk is it was at blackout it was 75 minutes and now it's 50 minutes  And I can't remember for sure what I cut so bear with me  Okay so sealed I guess I thought this was really important because I didn't cut it  So it comes from the factory you're not supposed to mess with it this is how your batteries are supposed to behave  You can't change anything you can't configure it you can't do anything  So you can only do the standard commands which was that first column and even there you can only read them  You can't write to those so you can't set anything if you unseal it which is what the former upgrade did  So in a sense it wasn't bad that they use the default unseal command because I could have just reverse engineered anything they chose  The bad thing is that they sort of gave me the idea to try the default for the full access and then they didn't change that  Anyway so in the unsealed mode you can then access the some of the extended commands you can read the data flash and write the data flash  At that point you can start to do some real configuration of the thing  And this is again what the battery firmware upgrades go to  So full access mode then you have all SPS commands so again this is like root  All SPS commands you can write to any of them that allow you to write  You can enter these other like super privileged access modes boot ROM and configuration mode  And the Apple firmware upgrades don't even need to access that  So one of the modes is called configuration mode and this is basically the way that you can configure all the little internal sensors and stuff  So the little internal sensor might think it has 2000, I don't know  So it measures the amount of current passing into it and it thinks it's 20 milliamps but really it's 22 and so you tell it that  And it configures itself to know that oh yeah that was 22 and so you can imagine definitely screwing with it in this mode  I didn't mess with that mode  So the things you can configure are number of cells so that means like 6 in this case  Current, how much current and you're giving it, how much voltage it has and what its temperature is  So these are the things that this document I found tells you to do  I was like gee I wonder if there's any other calibrations you can do besides those because this sounds like really fun  And I don't know and it turns out if you Google what other calibrations for BQ20 Z80 besides temperature and voltage  Google actually doesn't help in that case  So I just went to the store so I asked the Texas Instruments people  So I get on the board and I'm like hi yes can I do anything besides this and this really nice employee named Jackie writes back  And she's like no only current voltage and temperature  I'm like okay thanks and the only reason I put this up is I think it's really cool because they have these message boards there  And you earn points and it's like a video game and like I have 105 points at this point in this game  And it says I'm a prodigy and I like that the game realized that you know while I don't know much about batteries I have a lot of potential  So I felt really good about that  Okay so then the other mode that I deal with a lot is called boot-run mode  And this allows low level access and in the regular document that talks about SPS it doesn't talk about this at all  Because really you're not supposed to be doing this unless you know what you're doing and obviously I don't  So I had to buy some hardware to help me out  So and again this is something I bought just to help figure out things you don't actually need this to do all the sort of attacks I talk about  So I bought this evaluation module and it's basically the same exact thing that's on the battery except it's like a big board  You can look at it and you can play with it and the most important thing is it comes with Windows software that talks over USB to the chip  And you know we'll like program it and stuff  And so I can just sort of emulate what it does  So this is what it looked like at my house sitting on my like dusty floor  And then you'll also notice there's this radio shack you know device that I bought there  And I was like well I'm a child of the 80s or whatever so I was like those dudes at Radio Shack they know a lot  So I went in there and I was like so I was sort of hint around at my project you know I'm like well you know I work for a consulting company  And we're evaluating the safety of batteries and tell me about batteries and what do I need to test batteries  And the guy was like can I interest you in a mobile phone you know  So it was not helpful at all  So yeah and it was amazing but there's a lot of people who still shop there surprisingly  So anyway this is what the software looks like and this is where I got the little guy for the title screen  It gets really cool they have like a you know mascot for battery technology  It's like a clippy with a backpack on  So this is what the screen looks like and this allows you like the ragui to change all the SPS commands  And it shows you all the you know bit fields and what they mean and stuff  And you know so here's the data flash and you know again it tells you what each one means  What the value is what it changes to but of course the first button that I push when I see it is the pro button  Right so it says like this screen is only for advanced users and you know I'm certainly an advanced user  So it lets you do things like send raw commands and then the best thing is it lets you flash the firmware  Which is something I really want to do so then what I wanted to do is see how it's doing all these things  And then write some you know write a driver or something for OSX to let me do it  Unfortunately it's a Windows program and I'm trying to do things on max  But anyway the evaluation kit comes with this SREC file which has the firmware on it  And so I want to reprogram the thing and sniff what it does to know how to talk to it on this little level  Because there's no documentation I've seen yet that tells you that  So I Google I look around and there's this SREC file it says it's an encrypted or the sync file  That's what the ENC is for encrypted  So it's an encrypted SREC file and so like I reverse engineered the encryption it's like XOR  But you know to give it you know some credit it's not just XOR with a byte  It's like XOR with a byte and the previous byte so it's like you know sort of fancy  Anyway the firmware file has header stuff it has all the data flash  So those are all the configuration parameters it has all the instructions and then a bunch of checksums and stuff  So I wrote a PID bug script to intercept the USB traffic and then I could see exactly the sorts  And then by comparing it to the things I saw when I would just do a single byte write and all that kind of things in the GUI  I could figure out sort of what was going on  So for example I figured out like oh to read a word apparently you send the command 8  So write a word you send the command 4 and so on  So then I'm back to Google right so I Google SMBus bootrom read 8 write 4  So I was like I don't know how anyone did any research before Google  And no Google did not pay me for this talk I should have you know had Microsoft pay me I could say yeah I used Bing  Bing did it for me  So anyway I came across this document that talks exactly about like how to do talk on the bootrom level  And also it talked about how the firmware and the instructions were laid out so that was like really useful too  So now when I sniff the reprogramming I see these are the steps it does so it erases everything  Instructions and the data and then I can see like how much data it expects  Then it reprograms row by row the data and then it reprograms the instructions so there's 300 hex 300 rows of instructions  Cool  And then I can see the data too so like I could piece together by the data it's sending what the firmware looks like  So I pretty much understand now how to write in bootrom to reprogram it just by sniffing  I could probably figure out how to read you know how to dump the firmware but at this point in the story I haven't  I could get the data flash  But the problem is so I could either have the firmware I could either probably dump the firmware  Or I could just watch the firmware being uploaded from the SREC file but I don't know like what the hell is in there  So that's the next thing I want to do so I want to disassemble it and reverse engineer see how it works  But the problem is I don't know what kind of chip it is I know Texas Instruments makes it but I don't know what kind of like you know assembly it takes  So you know I do this stupid thing which is I load it into Ida Pro and I select each processor one by one and see what happens  And they all suck so it's none of the ones that are in Ida Pro  So I'm back to asking Texas Instruments  So I'm like hey does anyone know what kind of you know what kind of processor this is you know thank you and I try to be like really nice  And I'm like you know and I'm not lying I'm saying I'm Charlie Miller you know if they Google they'll see who I am and you know  So I'm just my approach is just to be nice and hopefully Jackie will give me a nice answer  But instead of Jackie this time I get this asshole named Doug Williams and he says proprietary that's it one word answer  I'm like don't you know I'm a prodigy  So anyway I'm like oh thank you for your kind response can you give me a little more information like do you mean this is something you just don't want to tell me  Or do you mean like you made this in your basement and like no one knows what the hell it is  And he says this is the first thing we have customers you know Apple probably who create their own firmware  But we don't disclose this data because we want to protect our intellectual property  Sorry so I'm like well okay I'll just steal your intellectual property  So I just take the binary I stick it into a hex editor and I just start staring at it right  This is the worst job in the world  And actually someone asked me about this part and when I gave this talk in black hat and they were like wow you like some kind of genius for looking at this  I'm like no this is the beauty of like you know the cooking show where the guy throws a bunch of stuff to cook the oven and they're like hey it's done right  So like this was like really hard but in the slide deck it's like oh yeah it was so obvious right but this was actually like took me like a week it was really hard  But you know for your benefit it looks like really cool like I'm awesome  So anyway so what do you notice in this anyone want to beat me to the punch so is there anything special can you read the bytes at all  Anything stick out oh I already said and don't look at that bullet if you want to play my game  Alright so you'll notice there's threes right that's the only thing that you really notice is there's some threes  Okay cool threes and anyone notice like you know something special about the threes  Sorry well they're spread out a certain amount so it turns out the threes are separated by two bytes each so it's like oh well that's sort of interesting  So that's like well maybe then it's aligned somehow on you know three byte widths so you know adjust the hex editor a little bit  And then you start to look hey look sure enough if you look down this columns there's this feature that the high nibble of the third byte is always zero one two or three  Okay so maybe and then you can see this is true for all the columns so maybe this is a 22 bit word so I gotta find some sort of chip that has 22 bit length instructions  Okay I'm in good shape and you know that's probably pretty rare  Okay and then the final clue is I looked at the end of the firmware and so what you see here is the very last so there's lots of these things at the end that say 3FFFFFF  And the very last thing before that is 23FFFFFFFF  So here's the 23FFFFFFFF  Like what would you think would be the very last instruction in something that you disassembled  Ah see I am smart I knew the answer and I don't think I've heard the right answer yet keep going  What's at the end of like big functions little functions?  Return right so I'm like well maybe this is a return because you know that's how I end my functions  And then any guesses what 3FFFFFF is?  What's that?  Ah I heard it so what do you put at the end when you don't want to do anything?  No ops so that's what I'm thinking right so no no no no no no no  So let's see if I'm right so the first thing to see if I have maybe I'm just like totally crazy  So I was like well let's search for this thing that might be a return in the whole file  Okay so there's 410 times it shows up cool and they're all sort of like randomly spread about also cool  And also I'm very happy to say that they didn't like encode or encrypt the firmware that would have really made this sort of analyzing in a hex editor harder  So anyway I'm pretty sure this thing is a return by now and those other things are no ops  I know 23 bit instructions so probably that might be enough to go back to my best friend Google  But my Google foo was weak I could not find it  But my co-worker Deon Losacus he came up with a Google search that did work  So he searched for 23FFFF, 3FFFF22 bit I don't know why I didn't think of that  So anyway I come across this document you know to be fair to show how hard this research was it wasn't the top hit it was the second hit  So you see things like it says blah blah 3FFFF is a no op you know return is 23FFFF so like this is pretty much business  So you look in there and it says something about cool risk and then you Google for that and you come across the document  And you find out that the BQ20Z80 is a cool risk C816 chip or at least something so close that it doesn't matter  So at this point we're in good shape  So I can read all about it and yeah so who saw Hackers last night?  It's a great movie and you know this is the best line I think from it  Angelina and Julie talking about risk architecture is like a total awesome thing  So anyway so this particular chip it's 8 bit it has this Harvard risk architecture  You can see the data flash 64k 64k flash instructions each 22 bits  There's 8 bit registers and of course Ida Pro doesn't support it  So you know this is what the registers look like and luckily since it's risk and risk is going to change everything  There's not too many instructions and so this is what there are there's like you know 40 or something  And so basically all you have to do if you like Ida Pro like me is go in and you can write in Python Ida Pro processor script  And tell it what each of these instructions means and what it looks like and then it can disassemble it  So that's what I did so this is another 2 weeks of my life in one slide  So you just go in this table in the document it explains exactly what the bits are you translate it into Python  And then you've got a new setting on your Ida Pro that says Texas Instruments Cascades BQ20s80  And I'm really this is all released to so you can download it and disassemble and see how these things work now  Oh, I sorry you can't they're proprietary  So so anyway now if you if you pop in Ida Pro you it's risk so you have to make a separate data section for it  And then you're good to go and disassemble  But the problem is that it didn't do that great job  But I just can help it out so I know that it's you know the instructions are fixed link they're all going to be you know 3 by  Aligned so I just want to find that script to go through and disassemble every single instruction because I know that that can be any data  Because of the risk architecture so then now in business and disassembled the whole thing pretty much  And I can start reading it and seeing what how this how this is you know code on the battery what it does  So if you can if you can see that so so basically this is some some function that takes in those SPS commands that you can ask  I for the temperature and stuff it does the operation and it sends it back to the computers this is exactly what we would kind of expect to see  So this one if you if you can see it so on the one on the left it says you know so it's SPS command 21 which is the device name  So it does some calculations and then sends it back this one is 20 which is like the manufacturer name sends it back and so forth  Okay cool so so where are we now I can I can disassemble the firmware I know what's going on the battery but turns out things are going to go wrong for me very soon  So I can disassemble it I can I can dump the flash cool  What else can I do oh so then I started dump the flash right and I noticed that I'm not getting identical results when I flat when I dump the flash each time and you know that's sort of weird but and whatever  And then the bad things are to happen right so then I like well let's let's let's make some changes to the firmware that's really what I want to do I do it  The battery doesn't work anymore so that's bad the other thing wasn't so bad but this is bad if I try it so I like well maybe you have to do exactly what the Texas Instruments like GUI does  So I do exactly what it does right so you know I'm sort of stuck  And so I start to you know I get on the Apple store and I start ordering these batteries because I'm going through them so quick like faster than they can ship them to me I'm bricking them  So and they're like $129 they're pretty expensive so I have a ton of these things and this I have just piles of them in my house that don't work anymore so it's a very expensive hobby  Lucky I have a corporate credit card so I had one idea I was like hey I got an idea they have like on eBay they sell these batteries that you know some dude in China makes in this basement  And you know they let's buy one maybe it's the same thing and then it'll be cheaper so I bought it and yeah I looked at it it's got the Texas Instruments chip and as far as I can tell it's pretty much the same  But he changed the password on it so I couldn't hack it  So anyway so the $40 battery was like super safe I was like oh well  Okay so to start to fix my problems and stop buying batteries I like I wondered if like the Apple store like Traxxas is like this guy has bought like 10 batteries what's he doing  And then like you should have a special like and he's Charlie Miller you know he hates Apple he's up to something so anyway it turns out by experimentation I found out that when I do the reeds from the firmware they're not always reliable and they don't error out or anything  They just return like random crap sometimes and so what I did is now I just have a change the API that I wrote to like it reads it a few times and it makes sure that it always agrees and then it's okay  And so once I do that then everything starts to go much better for me so I can you know consecutive dumps of the firmware like agree so this is good  Alright so then the next problem by the way this is like the worst thing that ever happens to me in my research if you can see it the little X on the battery so when you see that that means you're screwed  So that means you've ripped your battery well if you're doing my stuff otherwise it just means you probably don't have a plug in quite tight enough  Well the other thing so speaking of the Apple store before I so the one thing you can do from this research I didn't blow up batteries but I know how to like manipulate them so if ever anyone has a battery that isn't covered under warranty anymore I can hook you up  So I can you can go in and you can change when it was manufactured you can change how many cycles it's ever seen so you can make it look like it's like five days old you're like dude I just bought this battery like five days ago and it's not working I don't know what the problem is  Well it's good so I help out the community when I can  Alright so then I start so now I'm confident I can read firmware and I can write firmware but still it's not working I'm getting this stupid X that says I've done something wrong  And so I look at the traffic and I figure out what it is and it turns out that it's this particular flag is set the data flash failure flag total failure so it's so you read it blah blah blah and it's like number one after a full reset the instruction flash checks I'm does not verify  Ah crap there's a checksum I've messed it up so I need to figure that out but luckily I have the firmware so I can reverse engineer how they do that  So it turns out that there's these particular functions that I can't change that are in the realm and one of them is in charge of computing the checksum so I just need to figure out who's using that function  In an Ida Pro there's only two references to it one is to some SBS command that Google will tell you has something to do with checksumming and the other one is some function I haven't seen yet so that's my candidate so I check that out  If you look at the Ida Pro double that it does something like it reads in and you know four bytes from data flash and then it compares it and if it's zero these four bytes are zero then it just goes to the end of function  Otherwise it switches the ending this calls the checksum and then compares byte for byte if it's true and if it's not true it goes to the bad code and if it is true if they do agree then it goes to the end of the function  So anyone have an idea what we should set those four bytes in memory to? Zero  So if you set to zero then it doesn't compute the checksum anymore and you can make any changes you want  But then so that was the older version of firmware some of the newer versions of firmware like they got tricky and they encode or they probably would say they encrypt the checksum  And really I think they just did this because you might accidentally get zeros there someday but you wouldn't actually get this weird value  So they do some encoding of the thing but the same basic principle applies that if you set these four bytes instead of the zero to an encoded zero then it won't check the checksum anymore  So you can just do a raw data flash write to the device of zeros for those four bytes and it won't check the checksum anymore  But since I'm totally scared to go into boot run mode with all my dead batteries I figured out a way to do it without going to boot run mode  You can just figure out that there's this undocumented subclass 57 you can read and the checksum is actually in there so you can change those four bytes just with using extended SPS commands  So now I can just freely patch the firmware and do whatever I want so what do I want to do? Well I can make it lie so now when you ask for the temperature I can make a return on anything I want  But what I need to do is understand what things are asked of the battery and what things you know so those are I don't even know what to lie about  So I bought some more stuff plugged it in and I was like okay now I'm going to sniff the traffic the battery has these six little grooves and then maybe a couple other little spots where there's wires  Two of these are going to be S and BUS I don't know which two so I was like well I know I'll buy these little probes I'll hook them up between the things and you know I'll monitor what's going on  This was a disaster so I did this and I must have touched the wrong thing at some time but the computer just didn't work anymore  So apparently I shorted something on the main logic board and so I had to take it into the Apple store which I do this a lot I go to the Apple store and I'm like yeah this isn't working anymore  And they're like what did you do? I was like I don't know I just turned it on and it doesn't work so they replaced the logic board and then everything was good again  So yeah I never tell them what I do  Okay so then I said well the problem was you know all these loose wires and stuff so I'm going to build my own special cable that connects the battery to the computer  And so you know for someone who doesn't do any hardware this is like a big deal it took me like five days soldering and you know all this stuff I plugged in and it didn't work  Piece of crap so that didn't work so finally I figured out the right way to do it is you just move the keyboard a little bit the battery can be sitting where it's supposed to sit  You can still see the wires and you can hook little you know props to it and this is the right way to go  So then I hook up this logic analyzer and I can see exactly which cables have the SM bus traffic  And then you can actually ask it to decode it and you can see the actual SM bus right so it's like a right of eight which is temperature and then it reads the values so B73 so I can see what the temperature response is  And then you know that's cool for just like show but then there's this other thing called a Beagle that will record all this information for a long time  You get like cool gooey like this and you can see still that some bus traffic you can record it for like an hour while you're charging your battery and see all the things that are ever asked of the battery  And so these are the exact things that the with the computer off and the charging that asked for battery status temperature charging current current voltage battery mode relative state of change  Remaining capacity full charge capacity and of those only five ever change so the rest of them are just like yeah everything's cool or whatever so temperature changes but probably that's not so important  Current which is the amount of current it's delivering well okay then voltage remaining charge or main capacity and relative state of charge so these are the things that you could guess the charge is asking for in order to figure out how much charge to deliver it  So I'm gonna lie about those things so I gotta wrap this up quick but basically these are the things I can imagine doing now so where am I at I can make the battery do anything I want  So what could what you know so what right so breaking the battery is super easy I'm an expert at it I'm not even just a project I'm a full blown expert  So here is some code that with my API you can do that all this does is a race is all flash definitely not gonna recover from that  So you can make firmware changes like we've been talking about so you can change all the things that are queried to lie so like no matter how much charge it gets you always like hey I'm 50% full keep it coming  Maybe something bad would happen there so this is basically like a function that deals with SPS commands and I want to change the ones like so like remaining capacity which is SPS command F  Instead of F I'm gonna say you want to know F I'll tell you 1B that's manufacturer date and same thing if you ask me for full charge capacity I'll tell you the serial number  So I'm gonna just make changes to the firmware and the reason is because manufacturer date and serial number are both words which is the same thing as full charge capacity  But they're never queried and you can send them any value you want so then you can control exactly what the battery is reporting and you can do this live while the battery is charging without any wires or anything just remotely with root access  So this is what the code look like that handles that case and then here's the API call to patch the firmware and then it just changes that code to this code which just changes the SPS command and then jumps to the other case  So now when I query remaining capacity you know surprisingly it's exactly the same as the manufacturer date and same thing full charge capacity you know coincidentally is exactly the same as serial number  Okay so now I don't show it here but I go through and I have a white paper that goes in way more detail than I can hear but I go and I change the firmware to live on every single thing that's queried  So now over time it doesn't it doesn't change what I want to I can change it on the fly and I did verify that you know these changes that I make do affect how much current is delivered to the battery but in like some sort of weird non like super obvious way  So anyway this is where I like started to get scared and I tell the story that I was driving you know to a soccer game or something and I saw like fire trucks and stuff going the other direction and I was like oh shit  I was just like messing with this stuff when I left and so I call home and you know my wife answers I'm like well that's good she answered the phone and then I was like you know that computer upstairs in my office can you go unplug that  So she did that anyway so I mentioned that there's there's various layers of defense besides this chip that I'm like owning is the main guy in charge of safety but there's other things that may prevent the big explosion  So there's these like thermal cutoffs and you see so it's like these little cells that they get hot they melt and then you can't get electricity to the cells anymore  But you know the good thing about the batteries I can't mess with it the off-market one the bad thing is I didn't see these little things on the off-market battery  The other thing you can imagine again is attacking the kernel from the battery so this would be like a way to have persistent malware for example I didn't look for a particular bug again like knowing Apple it's hard to imagine there's not a bug there  So you can imagine writing a fuzzer in cool risk assembly and changing the firmware and having the battery literally fuzzing the OS that is crazy but it would be awesome  The easier way is you can just do it in hardware just hook wires in there and emulate the fuzz and Travis gets beat it's talking about he already has something that basically does that  So then if you know I don't really think there's much risk of anything bad happening to you from this without someone else doing something you know a lot more work  But if you're the super paranoid type which probably 90% of people in this room are pretty paranoid I did I release a tool called caulk gun which basically goes in and changes the passwords to values nobody knows  So source code basically looks like this it's just like yeah it gets full access changes the password and that's it  So here's all the people who helped me on this talk you know I'm such a hardware noob that I couldn't get very far so thanks to all those guys  Here's where you can download the paper the slides the tools I have I had a pro scripts I have the firmware IDB files everything you want to like learn and play with and that's it  Thanks a lot.
DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_0_851.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_852_2015.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_2016_3719.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_3720_7283.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_7284_10403.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_10404_12443.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_12444_13151.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_13152_16427.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_16428_17627.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_17628_18371.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_18372_18863.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_18864_20615.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_20616_20807.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_20808_21275.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_21276_23015.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_23016_24203.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_24204_31103.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_31104_33563.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_33564_34019.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_34020_34643.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_34644_45467.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_45468_47291.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_47292_48791.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_48792_50075.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_50076_50771.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_50772_50999.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_51000_51671.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_51672_54311.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_54312_55511.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_55512_60935.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_60936_61571.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_61572_63227.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_63228_64439.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_64440_68087.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_68088_70283.png DEF_CON_19_-_Charlie_Miller_-_Battery_Firmware_Hacking__9ErnoLVxCA_1_70284_73631.png
Alright, so if you can tell if you follow me on Twitter, I have a terrible cold. I've had awful Defcon Been sick the whole time. I didn't get to play soccer. I haven't you know I've been sober for 72 hours and I can barely talk So I'm gonna do my best and you know bear with me, but I do have a backup plan So here's my backup plan. Let me see here If I start to phase out here, you'll be here this Welcome to my battery hacking talk Is that good? Should I do that instead? All right, I'll try it if I get too nasally. I'm gonna go back to the computer. So so hopefully that won't happen Okay, so who am I? And sorry about the screen. I guess they had to move it up. So so people in the front could see So I used to work at the NSA for five years doing secret stuff And then when the iPhone came out I hacked it and when the Android phone came out the G1 phone I hacked that one phone on last four years. I've been sporting the stylist jacket So if you see a guy, you know wearing a white jacket for some reason, that's probably me So everyone who won this year got the jacket for some reason I'm the only one who wears it written a couple books and that sort of stuff and then the most important thing is there's an info So comic that has my name in it. So I feel pretty cool about that. Although. I don't I don't understand this particular comic But maybe that's because it's about me Okay, so why am I why am I here? Why did I decided to do battery firmware hacking? so Last year I came to to black hat and Defconn and I saw Barmy Jack's talk and I thought it was really awesome So I was like, this is cool that you know, it's easy to explain to someone you walk up to an ATM and it gives you money Right, this is this is what it's all about. It's not like hey I can make calculator pop up on your computer like it's just not impressive and You know, that's what I usually do. I usually do less impressive stuff than that It's like I can make this bit change or something right so So then I was like I need to do something cool like Barney me So I came across this YouTube video of this, you know computer like catching on fire and like the flames just going feet Hi, I was like I want to do that. So So I set out like in September last year like right after black hat I was like, I'm gonna see if someone remotely could blow up my computer and And you're gonna find out I actually didn't do that, but I made a lot of progress and so it's sort of interesting anyway and Maybe someone who's a little smarter than me can actually finish it up for me and also I'm kind of a chicken So you'll see that you see about that too Yes, this is the spoilers. You know despite what you might have read in the paper. I actually didn't blow up anything Mostly well, you'll see there's a lot of layers of security. I totally bust one of them and and then I got kind of scared so There's there's like all these parameters you can switch and you got charge it and discharge it and maybe overnight and I work Out of my home and you know, so anyway, it's kind of a chicken shit, but you'll see that it's kind of cool So what's the talk about so mostly I'll start out with a little sort of intro on how Batteries work and that sort of thing and then I'm gonna basically tell you the story of like seven months of my life looking into this and You know, it's it's mostly a story about a guy who's good at software hacking who tried to do hardware hacking So the good thing about that is, you know, probably not you guys who are in hardware hacking village Which I should actually go to and hang out But the guys who do software you'll probably learn some stuff Hopefully about how to do hardware because that's what I had to learn and then I'll talk about like, you know This so what question since I didn't blow up something something. What did I do and what could I do? All right, so so when I started this whole thing, I didn't know anything about batteries or you know chargers or really anything about hardware So I'm giving you a little bit of a spoiler here because just to sort of make sense of what I'm going But I didn't know this when I started so there's this document called the smart battery specification And what it it does is it outlines exactly how batteries and chargers and computers are supposed to communicate with each other And so this is this sort of justifies, you know, what I went after so it says, you know safety is a primary concern It's great the primary concept is that the primary intelligence is supposed to be in the battery itself so You know the computer smart it doesn't have to be the charger doesn't have to be smart What's supposed to be smart in this whole thing is the battery so the brains are supposed to be in the battery and the reason is because When the the battery knows the most about the battery so it knows how much charge it's gotten it knows how much You know how much it's got stored in it and it knows what its temperature is and knows what kind of chemistry it has so the The battery is supposed to have to be the brains of the operation and everything else is supposed to rely on that And so that's why I attack the brains So I'm going to go after the little the little chip on the battery Okay, so What so what are smart batteries right? I just thought these were like duracelles well they're they're duracelles with a chip So are actually more than one chip you'll see so There's just a you know There's chemistry cells and then there's some some chips and this main job is to make sure that it's it gets charged and it doesn't blow up and if you if you have a MacBook like me and you hit You know the the more information about your system you end up with a screen that looks like that if you can read it and on this one On when I started it didn't say this one this one actually if you look it says device name BQ 20z 451 That's the model the chip from from Texas Instruments on this battery But anyway this this information is obtained from the battery by the the operating system asking the battery and the battery telling it All right, so like you know what what could go wrong with a battery Right, so you could you could make their battery not work anymore and this is super easy. I do it all the time never on purpose though and then You could you could reprogram it to you know why and this is what I do Maybe this would allow What what the engineers called thermal runaway and I call like explosions But I didn't do it so I can't say and then the other thing is you can imagine like just talking to you know have the battery talking non-stop to the To the you know OS like you know Dan Kaminsky Defcon talk and then eventually the OS is gonna get bogged down Dang everyone loves Dan Kaminsky at black hat that would have nailed it. Okay, so And then you can imagine it also if there was a bug in the kernel It could actually attack the kernel from the battery and you figure like you know Apple can barely Write secure software when they know someone's gonna attack them But when they're like, you know reading values from a battery They really probably didn't pay attention and then you you're also on the same bus as like a TPM chip or the bio So you could sniff all sorts of other interesting things That you wouldn't normally have access to because you're at this other spot and hardware I see there's things that you might be able to do with this Okay, so so you know the story was again, you know after black hat I sat around you know mopey trying to think of something interesting to do and eventually I was like yeah Let's see if I can blow up a computer So I was like well, you know, I wish I knew something about hardware So I said I know I have a computer and I have a battery. What else? Well, let's just use Google That's what a lot of this talk is I don't know what I'm doing. So I Google stuff So so in this case I Google MacBook battery firmware, right? That seems like a reasonable thing to start and sure enough the very first hit is about a firmware upgrade that they that Apple put out in 2009 and I was like sweet at software I can look at this I understand this so I downloaded it checked it out reverse engineered it and I found out a few things so one thing is that you know the the computer is trying to talk to the battery and it just wraps This function Io connect method structure is structure zero function over and over and over it just keeps calling this and turns out This is a function that you use in OS X to talk to kernel drivers like texts So so this is the way you do it and the particular driver they're talking to is called Apple smart battery manager So the you know that your user land program it has to have root privileges is but it doesn't have to like have you know Wires plug into hardware anything it can it can talk to the battery by using calling this this method So then you know, so I was like okay cool So I noticed that there's this sort of associated source code bundle on the Apple website called Apple smart battery I actually it's called power management package you can download it has like some cool source in it But it doesn't build like they don't they give you enough just to like say that's open source, but not enough to actually use it So but the cool thing is they have a bunch of header files And you can start to read some interesting stuff about like some of the commands that that this this firmware upgrade is doing So what's the firmware upgrade do well based on that header file I can see it does things like It looks at the device name and compares it to a list of things to see whether it this is something it should be upgrading Looks at the firmware version looks at the pack I code again making sure it's something, you know the right one to upgrade And then some things that it's not in the header files I don't know what those things are and then I came across this and I was like that's weird It doesn't look like the rest of the program instead of just calling that function over and over It's got these like weird numbers like 414 and 3672 like these stick out to me Now like I wonder what that is so they're like well, you know, I don't know I'll Google so so you Google that number and And and the very first thing is it's like the default is 36720414 and It's like this is entered by sending the data for 1 4 to address zero and it's like look back at here Look, that's what it's doing and then it's like and then immediately after saying 3617. That's exactly what it's doing So what does it mean and then you can look down a little bit? And it's like the default code is set to unsealed to full and it's like talking about like, you know All sorts of stuff and so you Google a little bit more and I come across this document from Texas instruments That talks about you know, the unsealed key is this magical number. I see I was like, oh, okay It's like, you know unsealing the battery and then I was like, oh look, there's this other thing called full access key that sounds even more important and It's it's ff ff ff. I was like, well, let me try that. Hey, it worked so So this is basically the whole talk is a slide is you know You know they use this chip and you can download the spec and they didn't change the passwords on it So it allows it allows you to and this is on every Apple laptop. I've ever seen I have a bunch of them and Apparently there's a bunch of other computers that use the same chip But I don't know if they've actually changed the password. So I mean like the chips. Okay. It's just Apple is stupid and didn't change So anyway, so now I was like sort of in business because a I know I kind of I know it comes from Texas instruments because that's where I got the doc from I Know Apple is done, but I already kind of knew that and then That's that's where I'm at right now. So I'm feeling pretty good But I still don't know exactly what chip. I just know it came from Texas instruments So I had to figure out what that was and you should know by now that like, you know I could have just like opened the battery looked at the chip, but I'm totally scared to touch stuff So I've still wanted to find a new software way to do it And so I did was there's a way you could query the battery for particular types of data with these different what they call subclass IDs And so I just did that I said, okay Give me your data for subclass ID zero one two three four And it would give me a certain amount of data for each one and all the Texas instruments chips They have documentation on what that's supposed to return and each one returns something a little bit different And so I just you know saw what mine was and I looked at every single doc To see which one had sort of the same kind of it returned the right amount of data and I found out what chip it was by doing that It's a Texas oh right here. I won't give away the story yet. So anyway, this is what my return subclass zero was 22 bytes So you can see like what kind of stuff this is this is the kind of stuff like you're messing with now that you probably shouldn't be So it says like first level safety is subclass ID zero so returns 22 bytes subclass ID one is first level safety current the other one was was voltage and then you keep going then you've got like second level safety and then You know single safety current single safety bolt and so on and it turns out it matches exactly this Texas instruments chip called Ti BQ 20s 80 and so all the MacBook and MacBook Air's and MacBook Pros and all these Have some sort of chip really close to this not not all necessarily the 20s 80, but all this stuff. I'm talking about all works on all them Okay, so that was my like totally weenie way to figure out what it was and then eventually I got it was like well, I should at least get my hands a little bit dirty So I took a battery and I ripped it apart and so if you take a battery out of your computer It looks like this and there's these little screws So you unscrew it and then you just like rip off the plastic part And this is what you see on the inside so you got six with the impalmer cells And then on the end I go over here is like where all the electronic stuff is so then you rip that off and it's like well There's lights on and I'm touching it. It's like for software got really creepy So there's like chips and stuff and you can open it up even more and then you can start to read the labels on the chips So there's one that's called BQ 29312 you can download information about that this job is basically it does safety stuff And then so you know let's this talk, you know there was a newspaper and all that stuff and Travis Goodspeed who's like a total hardware nut Just because he's curious he takes one of his batteries and he you know rips apart the chips He puts an acid bath and x-rays it and this is some pictures he did just because he's a super curious guy So this is what the chip looks like under x-ray And then on the other side of that board you got the BQ 2080 which is the brains of the operation And then this other chip 29412 which is another one of these chips that does safety checks So and then here is the BQ 2080 thanks to Travis for these pictures Pretty cool So anyway so the BQ 2080 is the main brains that's the chip you can talk to through that driver The other two chips you can't talk to directly they're in charge of some safety stuff But what you can do is you can set the parameters in the 2080 and it will push out those parameters to the other chips You can configure those other chips you just can't talk to them directly but that's all you really need So like you can say oh yeah you're in charge of the second level safety suite Yeah yeah don't go off until you're at like you know five million degrees and then everything's bad But until then it's cool so you can still mess with those other chips Oops I skipped this slide So this is the show again like it took me that long to figure out what kind of chip it was Actually if I would have paid attention the actual header file they'd tell me exactly what it was Oh well that's why you know that's why we do it and that's why it took me seven months instead of my normal project which was like two weeks Alright so where am I at now so I know you know I actually took it apart so I know what kind of hardware it is I can download information about it I know sort of I can see how the this firmware updater talks to it so I can you know probably figure out how to talk to it But I don't really know like what kind of things to say to it So back to this document the smart battery specification It describes exactly how this sort of communication works So there's three components there's the battery which is in the middle and that's what we've talked about so far There's the charger which is this thing that you know decides somehow how much charge to deliver to the battery or not to charge it And then there's the host which is like the computer or the thing that needs to get power So basically this is the way they can communicate it's on this bus the SM bus And the SM bus is based on I2C which Stephen Ridley gave this really cool talk about There's lots of things in the electronic role based on I2C Anyway so they talk to each other and it's sort of complicated but I wrote this API that makes it easy to talk to the battery So and I released it you can now load it and play with your battery but just be careful that you don't break it Or at least don't blame me you can break it if you want So anyway so you can do things now like read a word like read the serial number from the thing You can unseal it if you know the password which we do You can write a word so you can change what the manufacturer date was You can change the device name which is what I'm doing here And then you can read data flash which is like all that stuff I was talking about about getting the idea stuff So here I'm reading some data flash whatever 57 was I don't know which one it was Then you can get full access which I'll talk about in a minute which is like the equivalent of like root or whatever on the battery And then you can seal things back up when you're done So what kind of things can you can you know a computer or can we say to the battery So there's this document that says exactly what to do so there's these things called standard commands which is the first column You can do things like ask it what's temperature, what's voltages, how much charge it wants stuff like that And then there's in the next column extended commands so these are things like a little more complicated And then there's other things you need more privileges to do so like you can set passwords You can control the FETs which is the little thing that physically closes to make electricity run into the battery And then you know keys and all that kind of stuff And then there's this data flash this is all just like data like that you can configure the device with So there's lots of like interesting things you can imagine playing with right So you can you know change the chemistry you can change the you know all these thresholds where like bad things are supposed to be happening And so I did that right you know the first thing I do is like you know how about no over voltage protection let's change that But what happens is the battery still just you know it gets its charge and it knows when it's done and it stops So just changing those thresholds those are like safety features when things go wrong If you don't make things go wrong somehow then you don't need those thresholds they're not ever they're not ever hit So you have to do something more so you know we talked about how we can unseal it and then we can get full access But there's other modes that are even more powerful so there's like boot run mode which I'll talk about in a second And that gives you like very low level access like you're sitting near the TI guy sitting in the factory So these are all the different modes that you can put the battery in So sealed that's how it's supposed to ship from the factory let me see if I have slides yeah I'll just go in So I couldn't remember because so the problem with this talk is it was at blackout it was 75 minutes and now it's 50 minutes And I can't remember for sure what I cut so bear with me Okay so sealed I guess I thought this was really important because I didn't cut it So it comes from the factory you're not supposed to mess with it this is how your batteries are supposed to behave You can't change anything you can't configure it you can't do anything So you can only do the standard commands which was that first column and even there you can only read them You can't write to those so you can't set anything if you unseal it which is what the former upgrade did So in a sense it wasn't bad that they use the default unseal command because I could have just reverse engineered anything they chose The bad thing is that they sort of gave me the idea to try the default for the full access and then they didn't change that Anyway so in the unsealed mode you can then access the some of the extended commands you can read the data flash and write the data flash At that point you can start to do some real configuration of the thing And this is again what the battery firmware upgrades go to So full access mode then you have all SPS commands so again this is like root All SPS commands you can write to any of them that allow you to write You can enter these other like super privileged access modes boot ROM and configuration mode And the Apple firmware upgrades don't even need to access that So one of the modes is called configuration mode and this is basically the way that you can configure all the little internal sensors and stuff So the little internal sensor might think it has 2000, I don't know So it measures the amount of current passing into it and it thinks it's 20 milliamps but really it's 22 and so you tell it that And it configures itself to know that oh yeah that was 22 and so you can imagine definitely screwing with it in this mode I didn't mess with that mode So the things you can configure are number of cells so that means like 6 in this case Current, how much current and you're giving it, how much voltage it has and what its temperature is So these are the things that this document I found tells you to do I was like gee I wonder if there's any other calibrations you can do besides those because this sounds like really fun And I don't know and it turns out if you Google what other calibrations for BQ20 Z80 besides temperature and voltage Google actually doesn't help in that case So I just went to the store so I asked the Texas Instruments people So I get on the board and I'm like hi yes can I do anything besides this and this really nice employee named Jackie writes back And she's like no only current voltage and temperature I'm like okay thanks and the only reason I put this up is I think it's really cool because they have these message boards there And you earn points and it's like a video game and like I have 105 points at this point in this game And it says I'm a prodigy and I like that the game realized that you know while I don't know much about batteries I have a lot of potential So I felt really good about that Okay so then the other mode that I deal with a lot is called boot-run mode And this allows low level access and in the regular document that talks about SPS it doesn't talk about this at all Because really you're not supposed to be doing this unless you know what you're doing and obviously I don't So I had to buy some hardware to help me out So and again this is something I bought just to help figure out things you don't actually need this to do all the sort of attacks I talk about So I bought this evaluation module and it's basically the same exact thing that's on the battery except it's like a big board You can look at it and you can play with it and the most important thing is it comes with Windows software that talks over USB to the chip And you know we'll like program it and stuff And so I can just sort of emulate what it does So this is what it looked like at my house sitting on my like dusty floor And then you'll also notice there's this radio shack you know device that I bought there And I was like well I'm a child of the 80s or whatever so I was like those dudes at Radio Shack they know a lot So I went in there and I was like so I was sort of hint around at my project you know I'm like well you know I work for a consulting company And we're evaluating the safety of batteries and tell me about batteries and what do I need to test batteries And the guy was like can I interest you in a mobile phone you know So it was not helpful at all So yeah and it was amazing but there's a lot of people who still shop there surprisingly So anyway this is what the software looks like and this is where I got the little guy for the title screen It gets really cool they have like a you know mascot for battery technology It's like a clippy with a backpack on So this is what the screen looks like and this allows you like the ragui to change all the SPS commands And it shows you all the you know bit fields and what they mean and stuff And you know so here's the data flash and you know again it tells you what each one means What the value is what it changes to but of course the first button that I push when I see it is the pro button Right so it says like this screen is only for advanced users and you know I'm certainly an advanced user So it lets you do things like send raw commands and then the best thing is it lets you flash the firmware Which is something I really want to do so then what I wanted to do is see how it's doing all these things And then write some you know write a driver or something for OSX to let me do it Unfortunately it's a Windows program and I'm trying to do things on max But anyway the evaluation kit comes with this SREC file which has the firmware on it And so I want to reprogram the thing and sniff what it does to know how to talk to it on this little level Because there's no documentation I've seen yet that tells you that So I Google I look around and there's this SREC file it says it's an encrypted or the sync file That's what the ENC is for encrypted So it's an encrypted SREC file and so like I reverse engineered the encryption it's like XOR But you know to give it you know some credit it's not just XOR with a byte It's like XOR with a byte and the previous byte so it's like you know sort of fancy Anyway the firmware file has header stuff it has all the data flash So those are all the configuration parameters it has all the instructions and then a bunch of checksums and stuff So I wrote a PID bug script to intercept the USB traffic and then I could see exactly the sorts And then by comparing it to the things I saw when I would just do a single byte write and all that kind of things in the GUI I could figure out sort of what was going on So for example I figured out like oh to read a word apparently you send the command 8 So write a word you send the command 4 and so on So then I'm back to Google right so I Google SMBus bootrom read 8 write 4 So I was like I don't know how anyone did any research before Google And no Google did not pay me for this talk I should have you know had Microsoft pay me I could say yeah I used Bing Bing did it for me So anyway I came across this document that talks exactly about like how to do talk on the bootrom level And also it talked about how the firmware and the instructions were laid out so that was like really useful too So now when I sniff the reprogramming I see these are the steps it does so it erases everything Instructions and the data and then I can see like how much data it expects Then it reprograms row by row the data and then it reprograms the instructions so there's 300 hex 300 rows of instructions Cool And then I can see the data too so like I could piece together by the data it's sending what the firmware looks like So I pretty much understand now how to write in bootrom to reprogram it just by sniffing I could probably figure out how to read you know how to dump the firmware but at this point in the story I haven't I could get the data flash But the problem is so I could either have the firmware I could either probably dump the firmware Or I could just watch the firmware being uploaded from the SREC file but I don't know like what the hell is in there So that's the next thing I want to do so I want to disassemble it and reverse engineer see how it works But the problem is I don't know what kind of chip it is I know Texas Instruments makes it but I don't know what kind of like you know assembly it takes So you know I do this stupid thing which is I load it into Ida Pro and I select each processor one by one and see what happens And they all suck so it's none of the ones that are in Ida Pro So I'm back to asking Texas Instruments So I'm like hey does anyone know what kind of you know what kind of processor this is you know thank you and I try to be like really nice And I'm like you know and I'm not lying I'm saying I'm Charlie Miller you know if they Google they'll see who I am and you know So I'm just my approach is just to be nice and hopefully Jackie will give me a nice answer But instead of Jackie this time I get this asshole named Doug Williams and he says proprietary that's it one word answer I'm like don't you know I'm a prodigy So anyway I'm like oh thank you for your kind response can you give me a little more information like do you mean this is something you just don't want to tell me Or do you mean like you made this in your basement and like no one knows what the hell it is And he says this is the first thing we have customers you know Apple probably who create their own firmware But we don't disclose this data because we want to protect our intellectual property Sorry so I'm like well okay I'll just steal your intellectual property So I just take the binary I stick it into a hex editor and I just start staring at it right This is the worst job in the world And actually someone asked me about this part and when I gave this talk in black hat and they were like wow you like some kind of genius for looking at this I'm like no this is the beauty of like you know the cooking show where the guy throws a bunch of stuff to cook the oven and they're like hey it's done right So like this was like really hard but in the slide deck it's like oh yeah it was so obvious right but this was actually like took me like a week it was really hard But you know for your benefit it looks like really cool like I'm awesome So anyway so what do you notice in this anyone want to beat me to the punch so is there anything special can you read the bytes at all Anything stick out oh I already said and don't look at that bullet if you want to play my game Alright so you'll notice there's threes right that's the only thing that you really notice is there's some threes Okay cool threes and anyone notice like you know something special about the threes Sorry well they're spread out a certain amount so it turns out the threes are separated by two bytes each so it's like oh well that's sort of interesting So that's like well maybe then it's aligned somehow on you know three byte widths so you know adjust the hex editor a little bit And then you start to look hey look sure enough if you look down this columns there's this feature that the high nibble of the third byte is always zero one two or three Okay so maybe and then you can see this is true for all the columns so maybe this is a 22 bit word so I gotta find some sort of chip that has 22 bit length instructions Okay I'm in good shape and you know that's probably pretty rare Okay and then the final clue is I looked at the end of the firmware and so what you see here is the very last so there's lots of these things at the end that say 3FFFFFF And the very last thing before that is 23FFFFFFFF So here's the 23FFFFFFFF Like what would you think would be the very last instruction in something that you disassembled Ah see I am smart I knew the answer and I don't think I've heard the right answer yet keep going What's at the end of like big functions little functions? Return right so I'm like well maybe this is a return because you know that's how I end my functions And then any guesses what 3FFFFFF is? What's that? Ah I heard it so what do you put at the end when you don't want to do anything? No ops so that's what I'm thinking right so no no no no no no no So let's see if I'm right so the first thing to see if I have maybe I'm just like totally crazy So I was like well let's search for this thing that might be a return in the whole file Okay so there's 410 times it shows up cool and they're all sort of like randomly spread about also cool And also I'm very happy to say that they didn't like encode or encrypt the firmware that would have really made this sort of analyzing in a hex editor harder So anyway I'm pretty sure this thing is a return by now and those other things are no ops I know 23 bit instructions so probably that might be enough to go back to my best friend Google But my Google foo was weak I could not find it But my co-worker Deon Losacus he came up with a Google search that did work So he searched for 23FFFF, 3FFFF22 bit I don't know why I didn't think of that So anyway I come across this document you know to be fair to show how hard this research was it wasn't the top hit it was the second hit So you see things like it says blah blah 3FFFF is a no op you know return is 23FFFF so like this is pretty much business So you look in there and it says something about cool risk and then you Google for that and you come across the document And you find out that the BQ20Z80 is a cool risk C816 chip or at least something so close that it doesn't matter So at this point we're in good shape So I can read all about it and yeah so who saw Hackers last night? It's a great movie and you know this is the best line I think from it Angelina and Julie talking about risk architecture is like a total awesome thing So anyway so this particular chip it's 8 bit it has this Harvard risk architecture You can see the data flash 64k 64k flash instructions each 22 bits There's 8 bit registers and of course Ida Pro doesn't support it So you know this is what the registers look like and luckily since it's risk and risk is going to change everything There's not too many instructions and so this is what there are there's like you know 40 or something And so basically all you have to do if you like Ida Pro like me is go in and you can write in Python Ida Pro processor script And tell it what each of these instructions means and what it looks like and then it can disassemble it So that's what I did so this is another 2 weeks of my life in one slide So you just go in this table in the document it explains exactly what the bits are you translate it into Python And then you've got a new setting on your Ida Pro that says Texas Instruments Cascades BQ20s80 And I'm really this is all released to so you can download it and disassemble and see how these things work now Oh, I sorry you can't they're proprietary So so anyway now if you if you pop in Ida Pro you it's risk so you have to make a separate data section for it And then you're good to go and disassemble But the problem is that it didn't do that great job But I just can help it out so I know that it's you know the instructions are fixed link they're all going to be you know 3 by Aligned so I just want to find that script to go through and disassemble every single instruction because I know that that can be any data Because of the risk architecture so then now in business and disassembled the whole thing pretty much And I can start reading it and seeing what how this how this is you know code on the battery what it does So if you can if you can see that so so basically this is some some function that takes in those SPS commands that you can ask I for the temperature and stuff it does the operation and it sends it back to the computers this is exactly what we would kind of expect to see So this one if you if you can see it so on the one on the left it says you know so it's SPS command 21 which is the device name So it does some calculations and then sends it back this one is 20 which is like the manufacturer name sends it back and so forth Okay cool so so where are we now I can I can disassemble the firmware I know what's going on the battery but turns out things are going to go wrong for me very soon So I can disassemble it I can I can dump the flash cool What else can I do oh so then I started dump the flash right and I noticed that I'm not getting identical results when I flat when I dump the flash each time and you know that's sort of weird but and whatever And then the bad things are to happen right so then I like well let's let's let's make some changes to the firmware that's really what I want to do I do it The battery doesn't work anymore so that's bad the other thing wasn't so bad but this is bad if I try it so I like well maybe you have to do exactly what the Texas Instruments like GUI does So I do exactly what it does right so you know I'm sort of stuck And so I start to you know I get on the Apple store and I start ordering these batteries because I'm going through them so quick like faster than they can ship them to me I'm bricking them So and they're like $129 they're pretty expensive so I have a ton of these things and this I have just piles of them in my house that don't work anymore so it's a very expensive hobby Lucky I have a corporate credit card so I had one idea I was like hey I got an idea they have like on eBay they sell these batteries that you know some dude in China makes in this basement And you know they let's buy one maybe it's the same thing and then it'll be cheaper so I bought it and yeah I looked at it it's got the Texas Instruments chip and as far as I can tell it's pretty much the same But he changed the password on it so I couldn't hack it So anyway so the $40 battery was like super safe I was like oh well Okay so to start to fix my problems and stop buying batteries I like I wondered if like the Apple store like Traxxas is like this guy has bought like 10 batteries what's he doing And then like you should have a special like and he's Charlie Miller you know he hates Apple he's up to something so anyway it turns out by experimentation I found out that when I do the reeds from the firmware they're not always reliable and they don't error out or anything They just return like random crap sometimes and so what I did is now I just have a change the API that I wrote to like it reads it a few times and it makes sure that it always agrees and then it's okay And so once I do that then everything starts to go much better for me so I can you know consecutive dumps of the firmware like agree so this is good Alright so then the next problem by the way this is like the worst thing that ever happens to me in my research if you can see it the little X on the battery so when you see that that means you're screwed So that means you've ripped your battery well if you're doing my stuff otherwise it just means you probably don't have a plug in quite tight enough Well the other thing so speaking of the Apple store before I so the one thing you can do from this research I didn't blow up batteries but I know how to like manipulate them so if ever anyone has a battery that isn't covered under warranty anymore I can hook you up So I can you can go in and you can change when it was manufactured you can change how many cycles it's ever seen so you can make it look like it's like five days old you're like dude I just bought this battery like five days ago and it's not working I don't know what the problem is Well it's good so I help out the community when I can Alright so then I start so now I'm confident I can read firmware and I can write firmware but still it's not working I'm getting this stupid X that says I've done something wrong And so I look at the traffic and I figure out what it is and it turns out that it's this particular flag is set the data flash failure flag total failure so it's so you read it blah blah blah and it's like number one after a full reset the instruction flash checks I'm does not verify Ah crap there's a checksum I've messed it up so I need to figure that out but luckily I have the firmware so I can reverse engineer how they do that So it turns out that there's these particular functions that I can't change that are in the realm and one of them is in charge of computing the checksum so I just need to figure out who's using that function In an Ida Pro there's only two references to it one is to some SBS command that Google will tell you has something to do with checksumming and the other one is some function I haven't seen yet so that's my candidate so I check that out If you look at the Ida Pro double that it does something like it reads in and you know four bytes from data flash and then it compares it and if it's zero these four bytes are zero then it just goes to the end of function Otherwise it switches the ending this calls the checksum and then compares byte for byte if it's true and if it's not true it goes to the bad code and if it is true if they do agree then it goes to the end of the function So anyone have an idea what we should set those four bytes in memory to? Zero So if you set to zero then it doesn't compute the checksum anymore and you can make any changes you want But then so that was the older version of firmware some of the newer versions of firmware like they got tricky and they encode or they probably would say they encrypt the checksum And really I think they just did this because you might accidentally get zeros there someday but you wouldn't actually get this weird value So they do some encoding of the thing but the same basic principle applies that if you set these four bytes instead of the zero to an encoded zero then it won't check the checksum anymore So you can just do a raw data flash write to the device of zeros for those four bytes and it won't check the checksum anymore But since I'm totally scared to go into boot run mode with all my dead batteries I figured out a way to do it without going to boot run mode You can just figure out that there's this undocumented subclass 57 you can read and the checksum is actually in there so you can change those four bytes just with using extended SPS commands So now I can just freely patch the firmware and do whatever I want so what do I want to do? Well I can make it lie so now when you ask for the temperature I can make a return on anything I want But what I need to do is understand what things are asked of the battery and what things you know so those are I don't even know what to lie about So I bought some more stuff plugged it in and I was like okay now I'm going to sniff the traffic the battery has these six little grooves and then maybe a couple other little spots where there's wires Two of these are going to be S and BUS I don't know which two so I was like well I know I'll buy these little probes I'll hook them up between the things and you know I'll monitor what's going on This was a disaster so I did this and I must have touched the wrong thing at some time but the computer just didn't work anymore So apparently I shorted something on the main logic board and so I had to take it into the Apple store which I do this a lot I go to the Apple store and I'm like yeah this isn't working anymore And they're like what did you do? I was like I don't know I just turned it on and it doesn't work so they replaced the logic board and then everything was good again So yeah I never tell them what I do Okay so then I said well the problem was you know all these loose wires and stuff so I'm going to build my own special cable that connects the battery to the computer And so you know for someone who doesn't do any hardware this is like a big deal it took me like five days soldering and you know all this stuff I plugged in and it didn't work Piece of crap so that didn't work so finally I figured out the right way to do it is you just move the keyboard a little bit the battery can be sitting where it's supposed to sit You can still see the wires and you can hook little you know props to it and this is the right way to go So then I hook up this logic analyzer and I can see exactly which cables have the SM bus traffic And then you can actually ask it to decode it and you can see the actual SM bus right so it's like a right of eight which is temperature and then it reads the values so B73 so I can see what the temperature response is And then you know that's cool for just like show but then there's this other thing called a Beagle that will record all this information for a long time You get like cool gooey like this and you can see still that some bus traffic you can record it for like an hour while you're charging your battery and see all the things that are ever asked of the battery And so these are the exact things that the with the computer off and the charging that asked for battery status temperature charging current current voltage battery mode relative state of change Remaining capacity full charge capacity and of those only five ever change so the rest of them are just like yeah everything's cool or whatever so temperature changes but probably that's not so important Current which is the amount of current it's delivering well okay then voltage remaining charge or main capacity and relative state of charge so these are the things that you could guess the charge is asking for in order to figure out how much charge to deliver it So I'm gonna lie about those things so I gotta wrap this up quick but basically these are the things I can imagine doing now so where am I at I can make the battery do anything I want So what could what you know so what right so breaking the battery is super easy I'm an expert at it I'm not even just a project I'm a full blown expert So here is some code that with my API you can do that all this does is a race is all flash definitely not gonna recover from that So you can make firmware changes like we've been talking about so you can change all the things that are queried to lie so like no matter how much charge it gets you always like hey I'm 50% full keep it coming Maybe something bad would happen there so this is basically like a function that deals with SPS commands and I want to change the ones like so like remaining capacity which is SPS command F Instead of F I'm gonna say you want to know F I'll tell you 1B that's manufacturer date and same thing if you ask me for full charge capacity I'll tell you the serial number So I'm gonna just make changes to the firmware and the reason is because manufacturer date and serial number are both words which is the same thing as full charge capacity But they're never queried and you can send them any value you want so then you can control exactly what the battery is reporting and you can do this live while the battery is charging without any wires or anything just remotely with root access So this is what the code look like that handles that case and then here's the API call to patch the firmware and then it just changes that code to this code which just changes the SPS command and then jumps to the other case So now when I query remaining capacity you know surprisingly it's exactly the same as the manufacturer date and same thing full charge capacity you know coincidentally is exactly the same as serial number Okay so now I don't show it here but I go through and I have a white paper that goes in way more detail than I can hear but I go and I change the firmware to live on every single thing that's queried So now over time it doesn't it doesn't change what I want to I can change it on the fly and I did verify that you know these changes that I make do affect how much current is delivered to the battery but in like some sort of weird non like super obvious way So anyway this is where I like started to get scared and I tell the story that I was driving you know to a soccer game or something and I saw like fire trucks and stuff going the other direction and I was like oh shit I was just like messing with this stuff when I left and so I call home and you know my wife answers I'm like well that's good she answered the phone and then I was like you know that computer upstairs in my office can you go unplug that So she did that anyway so I mentioned that there's there's various layers of defense besides this chip that I'm like owning is the main guy in charge of safety but there's other things that may prevent the big explosion So there's these like thermal cutoffs and you see so it's like these little cells that they get hot they melt and then you can't get electricity to the cells anymore But you know the good thing about the batteries I can't mess with it the off-market one the bad thing is I didn't see these little things on the off-market battery The other thing you can imagine again is attacking the kernel from the battery so this would be like a way to have persistent malware for example I didn't look for a particular bug again like knowing Apple it's hard to imagine there's not a bug there So you can imagine writing a fuzzer in cool risk assembly and changing the firmware and having the battery literally fuzzing the OS that is crazy but it would be awesome The easier way is you can just do it in hardware just hook wires in there and emulate the fuzz and Travis gets beat it's talking about he already has something that basically does that So then if you know I don't really think there's much risk of anything bad happening to you from this without someone else doing something you know a lot more work But if you're the super paranoid type which probably 90% of people in this room are pretty paranoid I did I release a tool called caulk gun which basically goes in and changes the passwords to values nobody knows So source code basically looks like this it's just like yeah it gets full access changes the password and that's it So here's all the people who helped me on this talk you know I'm such a hardware noob that I couldn't get very far so thanks to all those guys Here's where you can download the paper the slides the tools I have I had a pro scripts I have the firmware IDB files everything you want to like learn and play with and that's it Thanks a lot.
And then when the iPhone came out I hacked it and when the Android phone came out the G1 phone I hacked that one phone on last four years. I've only one who wears it written a couple books and what you might have read in the paper. I actually document called the smart battery specification batteries and chargers and computers are supposed When the the battery knows the most about the obtained from the battery by the the operating they're like, you know reading values from a things that you might be able to do with this I was like sweet at software I can look at this I computer is trying to talk to the battery and it and so all the MacBook and MacBook Air's and there's the host which is like the computer or this API that makes it easy to talk to the and play with your battery but just be careful passwords You can control the FETs which is the right you know the first thing I do is like you things you can configure are number of cells so has the firmware on it And so I want to reprogram figure out how to read you know how to dump the thing which is I load it into Ida Pro and I lying I'm saying I'm Charlie Miller you know if how many cycles it's ever seen so you can make it have the firmware so I can reverse engineer how computing the checksum so I just need to figure out who's using that function In an Ida Pro BUS I don't know which two so I was like well I that will record all this information for a long things that the with the computer off and the make changes to the firmware and the reason is both words which is the same thing as full charge charge of safety but there's other things that