 All right, 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 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 gene one fun I hacked that one ponon 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 Def Con and and I saw Barmy Jack's talk and I would 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 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 can see about that too Okay, so 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 anybody's kind of a chicken shit, but you'll see that it's kind of cool So what 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 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 were in hardware hacking village Which I should actually go to and hang out But the guys you 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 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 con 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. 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 Dura cells well they they're Dura cells 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 on this one Actually, if you look it says device named BQ 20z 451 That's the the model the chip from from Texas Instruments on this battery But anyway this if 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 call 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 Oh, that's like, you know a 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 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. It's like 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. It's 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 computer is trying to talk to the battery and it just wraps this function I connect method structure is structure zero function over and over and over just keeps calling this and it turns out This is a function that you use an OS X to talk to kernel drivers like kexts So so this is the way you do it and the particular driver. They're talking to it's 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 use 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 Actually, it's called power management package. You can download it. 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 It 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 four one four and three six seven two like these stick out to me Yeah, 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 the very first thing is it's like the default is three six seven two oh four one four and It's like this is entered by sending the data for one four to add or 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 three six seventeen 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, I'm stealing 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 f f f f 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 was stupid and didn't change the passwords 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 dock from I Know Apple is dumb, 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 dock 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 right here. I won't give away the store 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 it 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 voltage and so on and it turns out it matches exactly this Texas Instruments chip called Ti BQ 20 z80 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 they're not all necessarily the 20s 80 But all this stuff. I'm talking about all works on all of them Okay, so that was my like totally weenie way to figure out what it was and then eventually Like I 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 It's like where all the electronics stuff is So then you rip that off and it's like whoa, there's lights on and I'm touching it It's like for a 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 he's he's curious He takes one of his batteries and he you know rips apart the the chips He puts an acid bath and x-rays it and this is this is some pictures He did just because he's he's super curious guys. This is what the chip looks like under x-ray and Then on the other side of that that board you've got the BQ 20s 80 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 20s 80. Thanks to Travis for these pictures Pretty cool So anyway So the the BQ 20s 80 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 20s 80 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 I have million degrees and then then everything's bad, but until that it's cool So so you can still mess with those those other chips Ups, I skipped this slide So so this is so again like it took me that long to figure out what kind of chip was actually if I would have paid attention The actual header file they 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 is like two weeks All right, so so where am I 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 I can see how that this farmer 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 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 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 that you know decides Somehow how much charge to deliver to the battery or not to charge it and 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 they can communicate it's on this bus the SM bus and That's some buses based on I to see which Stephen Ridley gave this like really cool talk about There's lots of things in the Electrax world based on I to see anyway So they talked to each other and it's sort of complicated, but but I 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 that stuff. I was talking about about getting the idea stuff So here I'm reading some data flash 50 whatever 57 was I don't know which one those time ahead then you can get full access which I'll talk about in a Minute which is like the it's the equivalent of like, you know root or whatever on the battery And then you can seal things back up when you're done Okay, so so what kind of things can you can you know? Computer or can we say to the battery so that 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 temperatures was 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 These are things you need more privileges to do so like you can set passwords You can control the FETs, which is the little thing that that decide, you know that that physically closes to make electricity run instead of battery And then you know keys and all that kind of stuff 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 could 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 How about no overvoltage 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 But if you don't make things go wrong somehow then then you don't need those thresholds. They're not ever they're not ever hit So you have to do something more so So you know we talked about how we can unseal it and then we can get full access But there's there's other modes are even more powerful So they're 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 You're the ti guy sitting in the factory So these are all the different modes that you can put the battery in so seal That's how it's supposed to ship from them 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 black I 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 unsealed command because I could have just reverse engineered anything they chose The bad thing is that they 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 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 the 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, you know the little internal sensor might think yours sent it has, you know 2000 I don't know so it measures, you know the amount of current passing into it and it thinks it's 20 mil amps But really it's 22 and so you tell it that and then it reconfigures 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 our number of cells So that means like six 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 yeah, I wonder if there's any other Calibrations you can do besides those because this sounds like really fun and you know, I don't know and you know It turns out if you Google like what other calibrations for bq-20 z80 besides temperature voltage, you know like Google actually doesn't help in that case So I just went to the store. So I asked the Texas Instruments people so I said So I get on the board and I'm like, hi. Yes You know can I do anything besides this and this like really nice employee named Jackie writes back She's like no only current voltage and temperature Like okay, thanks and the other reason I put this up is I think it's really cool because they have these these message boards there And you earn points and it's like a video game and like I have a hundred five 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 batters. I have a lot of potential. So I felt really good about that okay, so So then the other mode that I deal with a lot is called boot round 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 talked about So I bought this evaluation module and it's 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 that talks over USB to the chip and let's 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, you know, I've worked 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 You can I interest you in a mobile phone, you know So it's not helpful at all So, yeah, and then yeah, it was amazing, but there's a lot of people who are 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 I think it's really cool. They have them. They have like a you know mascot for battery technology It's like a it's like 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 they what the value is what to change it to But of course the first button that I push when I when I see it is the pro button, right? So it says like this 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 unless you flash the firmware, which is something I really want to do so So then what I what I would wanted to do is see how it's doing all these things and then write some You know write a driver or something for OS X to let me do it Unfortunately, it's a Windows program and I'm trying to do things on Macs But anyway, the the evaluation kit comes with this SREC file Which is has the firmware on it And so I want to I want to reprogram the thing and sniff what it does to know how to 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 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 some some credit. It's not just XOR with a bite It's like XOR with a bite and the previous bite. So it's like, you know, sort of fancy Anyway, the the firmware file has header header stuff It has all the data flash. So those are all the configuration parameters has all the instructions and then a bunch of check sums and stuff So I wrote a pie debug 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 I could figure out sort of what was going on It's like for example, I figured out like oh to read a word apparently you send the command 8 to write a word You send the command 4 and so on So So then I'm back to Google, right? So I saw I Google SM bus 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, just had Microsoft pay me I could say yeah, I used being being did it for me So anyway, I came across this document that talks exactly about like how to do Talk in the bootrom level and also it talked about how the firmware the instructions were laid out So that was like really useful to So now when I sniff the reprogram and I see this these are the steps It does what erases everything Instructions in the data and then I can see like how much data it expects and 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 at 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 I at this point in this story I have it I could get the data flash and But the problem is so I could I either have the firm I could either probably dump the firmware or I could just watch the firmware being uploaded from the src 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 wanted this assemble it and like reverse engineers see how it works But the problem is I don't know what kind of chip it is right? I know text instruments makes it I don't know what kind of like, you know assembly it takes So, you know, I do the 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 They Google they'll see who I am and you know, so I'm just my approach is 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's he says Proprietary, that's it one word answer I'm like, don't you know I'm a prodigy So so anyway, I'm like, 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 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 so I just take the binary I stick it into 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're like you're 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 like throws much stuff to cook six in the oven They're like, hey, it's done right so like this was like really hard Well in the slide deck it's like oh, yeah, it was this is so obvious, right? But this was actually like to me like a week is really hard But you know for your benefit it looks like really cool like I'm awesome So anyway, so so what do you notice in this anyone want to want to beat me to the punch So is there anything special? Can you read the the bites at all anything stick out? Oh I already said and the don't look at that bullet if you want to play my game All right, so you'll notice There's there's threes, right? That's the only thing that you really notice is there's some threes Okay, cool threes and anyone notice the really like you know something special about the threes They're sorry. Well, they're spread out a certain amount. So it turns out that the threes are separated by two bites each So it's like oh, well that's sort of interesting So so that's like well, maybe then it's aligned somehow on you know three bite widths So you know adjusted the hex editor a little bit and then you start to look hey look sure enough If you look down those columns There's there's this this feature that the the high nibble of the third bite 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 there's so I got to find some sort of chip that has 22-bit Length instructions and maybe 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 3f ff ff And the very last thing before that is 23 ff ff ff so here's the 23 ff ff Can anyone like what would you think would be the very last instruction in something that you disassembled? See I am smart. I knew the answer. I don't think I've heard the right answer yet. Keep 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 3f fff is What's that? Oh, I heard it So so like 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 not not not not not not not so let's see if I'm right So so the first thing to see if I have if I'm maybe I'm just like totally crazy So I was like well, let's search for this thing that might be a return and the whole file Okay, so there's 410 times it shows up cool, and they're all sort of like randomly spread about we're also cool and And 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, you know, I'm pretty sure this thing's a return by now and those other things are no ops And 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 Dian Los Acos he came up with a Google search that did work So he searched for 23 fff 3 fff 22 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 3 fff is a no op, you know return is 23 ffff So like this is 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 BQ 20s 80 is a cool risk See 816 chip or at least something so close that it doesn't matter So at this point we're in we're in good 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 Jolie talking about risk architecture is like a total awesome thing So anyway, so this particular chip it's 8 bits It has 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 I don't pro doesn't support it So, you know, this is what the registers look like And luckily since it's risk and risk has got to change everything that There's not too many instructions and so there's this is what they are There's like, you know 40 or something and so basically all you have to do if you like I had a pro like me Is go in and you can you can write in Python I don't pro processor script and Tell it what each of these instructions means and and what it looks like and then it can disassemble it That's what I did. So this is, you know another two 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 I to pro that says Texas Instruments Cascades BQ 20s 80 And I'm really this is all released to so you can download it and disassemble and see how these things work now. Oh Sorry, you can't they're proprietary So so anyway now if you if you pop in I to pro you you you it's risk So you have to make a separate data section for it and then You're good to go to 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 length They're all going to be you know, three by a line So I just want to pie that script to go through and disassemble every single instruction because I know that that can mean a data because of the risk architecture So then now I'm in business and this assembled the whole thing pretty much And 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 so on the one on the left it says You know, so it's SPS command 21. So 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 the flash cool What else can I do oh so then I start to dump the flash right and I notice 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 man, whatever And then the bad things start to happen, right? So then I like well, 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 gooey does so I do exactly what it does Bricked 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 a hundred twenty nine dollars are 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 have they sell these batteries that you know some dude in China makes in his 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. That's got the Texas Instruments ship And as far as I can tell it's pretty much the same, but he changed the password on it. So I Couldn't I couldn't hack it Yeah, so anyway, so the $40 battery was was like super safe. I was like, well, okay So so 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's 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 So anyway, it turns out by experimentation I found out that when I do the reads 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 I changed 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 I can you know consecutive dumps of the firmware like agree. So this is this is good All right, so then the next problem. Well, 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 so that means you've ripped your your your battery Well, if you're doing my stuff, it otherwise it just means you probably don't have a plug didn't quite time No, 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 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 It's gonna it's good. So I help out the community when I can All right, so 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, and it's like number one after a full reset the instruction flash checks I'm does not verify it. 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 And an Ida Pro there's only two references to it One is to some SPS 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 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 end in 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 the that those four bytes in memory to? Zero right 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 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 right to the device to Of zeros for those four bytes, and it won't check the checksum anymore And 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 the boot run mode You can just figure out that there's 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 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 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 need to know what to lie about So I bought some more stuff plugged it in and I was like, okay now. I'm gonna 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 Gotta be S and bus. I don't know which to So I just 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 The computers didn't work anymore. So apparently I short 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, they're like, what'd 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 had I said well the problem was you know all these loose wires and stuff So I'm gonna 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 Sodering and you know all this stuff. I plugged in it didn't work. It's a 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 a little you know approach to it. This is this is the right way to go So then I hook up this this logic analyzer and I can see exactly which cables have the SM bus traffic and then you can actually have 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 responses and Then you know that's cool for just like show But then there's something called a beagle that will 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 a computer off and a 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 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. I mean so where am I at? I can make the battery do anything I want So 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 Why so like no matter how much charge it gets you always like hey, I'm 50% full keep it coming And 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 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 you 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 the remaining capacity full charge capacity But they're never queried and you can send 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, you know without any wires or anything just remotely with the root access So this is what the code look like that handles that case And then here's the the API call to pass the firmware and then it just all it 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 that goes in way more detail than I can't hear But I go and I changed the firmware to lie about every single thing that's queried so now over time it Doesn't it doesn't change I don't 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 you know, 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 I was like, oh shit Because I was just like messing with this stuff when I left It's like 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 that 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 the you know the good thing about that 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 the other thing you could 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 Goodspeed is talking about he already has something it 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 released a tool called caulk gun which basically goes in and changes the passwords to values. Nobody knows The 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