All right. So, I am very excited to hear this talk because it's going to be given by a kid that is very young, very intelligent. In fact, he's competing in the CTF this weekend, which makes him way smarter than the lawyer talking to you right now, that's for sure. But I'm also excited because this talk in the abstract, you can see it actually has some magic words in there. How to get started. Yeah? Because a lot of these talks you're going to see this weekend kind of drop you right in the middle and you're picking up just as little bit as you see somebody show off some kind of cool exploit. This is going to be much more of a kind of 45 minutes survey course, I understand. So, sit back and relax and let's give Seamus a big round of applause. Have a good time. Thank you. All right. So, I hope you're all here because you're interested in learning something about how the baseband on your phone works. works. So I guess we'll just get started then. So this is kind of the brief agenda for the talk. A little bit about me, why I think these are an interesting research topic and why I've been working on them for the past several months. A little bit about the history and evolution of the platform because I think knowing certain design decisions and why they were made in the past can really help you when you're looking at disassembly and code and like did they really do that silly thing? A little bit about how a modern system on a chip is laid out. It's changed a lot since the first cell phones were introduced. A little bit about the actual custom CPU architecture that the baseband chips and Qualcomm phones actually run on. A brief intro to some cell stuff because not everybody has a huge radio background. I know I didn't. And then how you actually start analyzing these things. So I'm Seamus. I've been working in information security for about four years now. I'm still an undergraduate student actually. I go to UMBC in Maryland. I was a member of their competition team when we went to CCDC a couple years back. I'm participating in the CTF here with Spaceballs this weekend. And I'm really interested in like embedded systems and vulnerability research and exploit development and all those kinds of things. And kernels. I like playing with kernels. All right. So my goals for my research and hopefully for what I do is what is a baseband and how does it really like interact with Android. I wanted to find bugs in it. That was my main goal. And I also wanted to like learn something and have fun. So there's been a little bit of prior work in the area. The first few real baseband talks came out 2010, 2011. And this was back when the Qualcomm basebands ran on ARM processors. So and they used a completely different real time operating system than they run now. So it's a little bit outdated. But it's still working. So I'm excited about it. I'm excited about it. I'm excited about the fact that it's still worth looking through all these old talks and digging up the videos. They give a great background introduction to some of this stuff. The first talk which really tackled their new architecture was baseband exploitation in 2013 which was given at PacSec. It was the first talk to really cover Hexagon and any details about it whatsoever. And that's around when those chips started being introduced into phones 2012, 2013. Every Qualcomm phone since then sold has one of these chips in it. And then more recently there was a talk just this past year at CanSecWest which was after I started doing my research. And that was a talk which kind of talked about a bug they found and the debugger they built around it and some of the cool things they did. But it was a lot less of like how do you get started looking for bugs. Which those have always historically been some of the most useful talks to me when I'm trying to learn something. Not a walkthrough of exploiting like this kernel zero day. Those are cool and I'm excited about them. So I'm excited about the rest of the talk. So for those of you who aren't entirely sure what a baseband is. It's the main chip in your cell phone which actually communicates with a cellular network. On some level it handles the transformation of radio waves into actual ones and zeros. And then processes the information inside of those. There's a ridiculous number of cellular standards in use around the world. And most baseband chips support most if not all of the hardware and the hardware and the hardware is all of them and fall over in between them. Because you don't know if you're leaving the country or if you're roaming in between cells, different carriers. If you're roaming on a different carrier, they have things set up differently. So there's a lot of complexity within these, just inherent to the nature of them. It's also your phone's main external interface to the rest of the world. You know, your data comes over this, your internet, all your text messages. And it's one of the last few attack surfaces where you can actually get truly, like, remote code execution, unauthenticated, no user interaction whatsoever. Along with some of the other firmware on your phone, like the Bluetooth and the Wi-Fi. You know, a lot of exploits have been going the way of, you need a browser bug and phishing and somebody clicks on a link. This, you really don't really need much in the way of interaction. You just need a phone number. So that's why it was interesting to me. So I kind of picked Qualcomm to focus on, as they had by far the largest market share of the whole baseband market. Um, and at least up until recently, most of your high-end phones carried a Qualcomm chip of some kind in them. You know, all your Snapdragon processors and most of your high-end Androids these days. Uh, the market's spread up a little bit more. Um, Huawei makes some of their own basebands. Uh, Samsung makes some with their Exynos chips. But the main reason Qualcomm owns a lot of the market is they were the big proponent of a cellular standard called CDMA, which is one of the two big ones. They're kind of, you know, the group into two groups. Um, so you have GSM and you have CDMA. And if, this is why phones can be carrier locked. Because AT&T is GSM and Verizon is CDMA. So if you, and they have a death grip on all the patents in the United States for CDMA basically. So if you want to sell a phone which does that, you kind of got to stick a Qualcomm chip in it. Um, for phones in other markets around the world, CDMA is more prominently a United States thing. So you'll see a little more variety in the chips. But they still have more than half the market share. Um, and so, um, it's a little bit about the history of the platform. Um, these chips in your phones, the basebands, they run a completely separate operating system, separate from Android or iOS. Um, it's its own self-contained operating system with its own bugs and its own rules. So these are, uh, something called an RTOS, a real-time embedded operating system. Uh, they're typically found in lots of, uh, like industrial applications, power, gas, electric, um, things which have strict time constraints. And you can't have a process like a browser hanging up and freezing your entire system. Things need to happen on a schedule. Um, and there can't be any kind of interruption to those. And you can kind of split RTOSes into two separate categories, where they either have some form of memory protection or they just don't. Um, so there's either some form of virtual memory, even if it's not meant for protection purposes, or that everything just runs in like a flat address space, straight in memory. Um, the RTOSes are used pretty widely. Um, Cisco routers use one of them. ThreadX is Intel's. That runs Intel's modems. Um, and some newer Broadcom Wi-Fi chips I just recently learned, things like that. Um, so going back, uh, to the start of the mobile phones, uh, Rex was the Qualcomm's real-time executive. It's the original RTOS which ran the modem. And this is before you had smartphones and Androids. So this ran your entire phone. Um, it was actually designed for the Intel 186. And then, uh, as they started growing as a company and the 186 was getting a little outdated, they actually tried to license a 386 quad. Um, so it was actually designed for the Intel 186. Um, so this is what they poured from Intel. And Intel kind of blew them off at the time because they were busy with other things. But if they hadn't, the cellular market would look pretty different today. Um, because this is what drove the switch to ARM on all your chips and phones. So Intel might very well have started producing like lower power consumption chips and the mobile market might not be all ARM if they hadn't, uh, rejected Qualcomm's offer way back when. Um, but this chip, uh, so they poured it to ARM, uh, this RTOS, sorry. They poured it to ARM and everything ran in supervisor mode. There was no privilege separation. If you had any kind of code execution, you were king. Um, it kind of ran as like one single process and your tasks were just threads in that process. Um, so it was very much a product of its times in the early 90s. Uh, so you'll actually still run into this, which is the reason I'm kind of going through some of the quirks. If you buy any kind of like feature phone, so like not a smartphone, burner phone type of deal, a lot of those run something called, um, Qualcomm Brew, which is like their binary runtime for, uh, for wireless, I think. Uh, and that wrecks is the core heart of that. Actually the rest of it's piled on top. Um, so tasks are your threads. Basically, um, every task has a task control block, which kind of stores the information about it, where your stack pointer currently is, what's the priority of the task, because this is an RTOS. So if your task has a really high priority, it's not going to interrupt it. Um, the size of your stack, every task has its own stack. And then when they are, uh, suspended to let room for operation, they're going to let room for something else happen. This is all just saved. So if you have any kind of like overflow or code execution, you can actually overwrite a different task's, um, current state from the stack. So when it's restored, you just took control of something else. Um, there's, uh, the task control blocks are just stored in a doubly linked list, similar process in other operating systems. It's very trivial to dork with things on this system. Um, the only problems is it's poorly documented. Um, so kind of moving out of the end of the 90s and into the mid 2000s, uh, Qualcomm decided to switch away from Rex. Um, and they didn't actually do it for any security reasons. It's just really tricky to debug code when you have no memory protections. You don't know whether something's being caused by some of your code overwriting some other code accidentally. It's really tricky to debug things. Um, so they started looking for solutions. Uh, if you dig through some of their ancient documentation, you can find, uh, Rex didn't even do any kind of parameter validation, and they basically told you, whatever you passed our API better be right, because we're not gonna check it. Um, it's great for when you're trying to find bugs. Sadly, they kind of moved away from this to something a little bit more secure. Um, so in the middle, like 2005, 2006-ish, they switched to a microkernel called L4, which is kind of an academic microkernel. Came out of academia in the 90s, I think. Uh, the way most people have heard of it is something called SEL4, which was the formally verified version of it, which was mathematically proven to be free of certain bug classes. Um, but the microkernel itself was freely available, different kinds of licenses. Um, so this is actually a much more mature operating system. You actually have real processes and real threads. Um, there's actually a privileged separation barrier. The kernel runs in supervisor mode, and the actual programs run in user space. Um, they were actually going to port L4 to their new newest DSP, the one they designed in-house, and they kind of had a falling out with the company they were working with on this with L4, so they decided to just rewrite their own real-time operating system, not really necessarily learning from the mistakes from the last time they did that. So they took L4, which is much more secure than what they had before, and they kind of ruined it by writing an emulation layer for their old RTOS because they didn't want to actually change any of their code base for the modem services. This is something you'll see a lot when you're dealing with this kind of firmware, is it has to be approved by different licensing bodies and re-evaluated when you make changes, and that can be quite a process to go through, so they're trying to avoid changing things as much as possible, which is why bugs can be long-lived sometimes, or there's terrible turnaround times actually getting updates for your baseband or your Wi-Fi firmware, things like that. So they wrote this emulation layer so they could take all of their actual modem software, and it will keep them up-to-date with what they're doing, and it will keep the same API it thinks it's talking to, and it transparently converts it on the back-end. So that doesn't really help you eliminate that many bugs. But they at least do run the modem software in user mode now instead of in kernel space. And then moving on from that, they kind of started developing their own in-house digital signal processor. This is the thing which makes Qualcomm chips pretty unique, is most other basebands, Huawei's HiSilicon, Samsung's Shannon baseband, they all keep the same API, run on some form of ARM processor, and they might have some digital signal processing logic to handle the lower-level physical layer conversion, but everything actually runs on ARM. A lot of the actual processing does. Qualcomm actually runs everything just on the DSP, as opposed to having the DSP handle some of the conversion and then passing it over to a different processor. So the current one is Kurt, the Qualcomm real-time OS. It used to have a different name. I don't know actually what that stood for. It used to be called Blast. And there are actually two hexagon cores in most earphones now. They have one which handles the baseband, the broadband radio, and they have one which handles audio processing. So they actually exposed this to application developers. You can, if you think your algorithm will run better on digital signal processor for some audio tricks you're trying to do, you actually have access to this in a limited capacity. Most of the APIs are, again, backwards compatible, so they don't have to change as much software. So some of the threading stuff changed a little bit, but a lot of it is pretty much the same. It's a pretty mature real-time OS as far as real-time OSs go. You have mutexes and semaphores, same kind of priority-based scheduling you see across a lot of these. But it does actually have memory protections, and the operating system itself handles the virtual-to-physical memory mapping. So the mitigations are kind of hit and miss on this. It is far ahead of its peers. Samsung, for instance, is a little bit ahead of its peers. Samsung, for instance, has NX and has it turned on in some places, but not necessarily covering what you would expect it to. There is a complete lack of ASLR, and this is common in all base bands. It's really tricky to port some of this software to actually move around in an address space. And like the real-time OS loads in the same spot every time. The kernel base is the same place. And then these things have hard code addresses calling each other around. So there's no ASLR. There's plenty of gadgets once you actually have code execution. They do have a non-executable stack. So you can't really write to code. You're not supposed to be able to execute data. And it does its best to enforce that. It does have stack cookies. It actually has a special register, the frame key register, which actually holds some of this stack cookie information and XORs it. And then its heat protection is more for accidental corruption. It kind of just marks whether your blocks are in use or not with a magic value. It's not anything like Windows or some of the more common heap allocators for browsers these days. So it's kind of a mixed bag as far as exploitation is concerned. You can't directly jump to your shell code, but as soon as you actually get program counter or something like that, there's no ASLR. So it's pretty easy to know where things are in memory and form your own gadgets or use a gadget to write your shell code into a place you can execute it. So AMSS is their name for the software stack which runs on the modem. You'll see a lot of confusion about this if you ever start doing any research. People say this covers more than it actually does. But it's the actual modem software, different drivers and protocols and everything you really use to talk to a cell tower. The lower part of it is pretty chipset specific, you know, which error interface protocols you actually support. You know, there's a dozen different cell standards. The hardware is all different. So you have low-level different hardware drivers. And on the feature phones, I mentioned earlier, there's a lot of different hardware drivers. And on the feature phones, I mentioned earlier, you have like QChat and QCamera and QMessage and all these applications which actually run are part of AMSS on those and they run on the same baseband. So there's more than 60 different processes or tasks which this is comprised of. You have a watchdog to make sure things don't really time out. You have a sleep timer to save power. You have different things for call control and message processing and diagnostics. Of those, one of the most important is the actual diagnostics task. It provides client server infrastructure where you can request information from the modem stack. And this is what a lot of Qualcomm's internal tools will talk to. So it's a holdover from their old Rex days. It will handle requests over serial or serial exposed over USB, which is more common now. It's a packet-based protocol. You send information and send stuff back. So if you ever get your hands on any of the Qualcomm tools which have been floating around, this is how you do it. You send it off. Any kind of over-the-air logs, failures, debugging information if you manage to turn it on, current status if you're trying to, like, test things over the air, different statistics on calls or drop packets and things like that. So modern systems on a chip are a lot more complicated than they used to be. And one of the things you kind of run into is something called a QFuse, at least on Qualcomm devices. And a lot of the other chipsets that you get on Qualcomm, like, Samsung's have their own similar depending on the chipset. But it's a bank of one-time programmable fuses. There is very little public documentation about what controls what. But these are one time, once you blow the bit, you're not really supposed to be able to change it back. And hardware debugging is disabled in production devices by blowing one of these bits. Supposedly the factory has a way to re-enable it, but I haven't been able to figure it out and I don't know anyone else who's mentioned being able to figure it out either. This is how, for example, the Samsung phone, they have something called the Knox bit, which is blown if you flash an unsigned kernel. And they kind of use that to void warranty claims sometimes. So if you unlock your bootloader and start dorking with your Android device, that's how they tell and that's how they'll deny you if you break it. Secure boot is heavily based off this. The cryptographic keys are burned in and can't be changed. The trust zone uses an undefined number of these bits as well. So in the good old days, before more modern Android and iOS devices, you kind of had your baseband processor and your actual application processor. And the baseband was actually the first one which booted up and brought up the application processor. And it had direct DMA. If you popped the baseband, you could kind of just control everything and write to the application processor and the user would never know. Chips are a lot more complicated now and they have a lot more interconnected subsystems. So, on a current Qualcomm SOC, for example, you have your modem processor subsystem, which is composed of this hexagon DSP and the actual accompanying hardware ASIC, which handles the over-the-air stuff. You have your actual application processor set, which on more recent devices is like its own set of eight ARM cores. You have something called RPM, which handles power management for the device. And this has its own bootloader and boot ROM and it runs its own separate ARM core. You don't really know about it. There's an interesting piece of research. I'm trying to remember the company name. I think it was Aleph Security. And they used a bug and were able to dump some of these other bootloaders so they could dump like the RPM bootloader and you could play with it and see what it does. These are all cryptographically verified, too, before they're actually uploaded to the device. Your wireless subsystem is its own separate little subsystem. On most devices, it's a Broadcom chip, although Qualcomm has their own Wi-Fi. I don't know which one. But it's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It's a good one. It isn't the size for which phones they put it on. Then you have your audio subsystem, which is another digital signal processor, which again, is exposed to the user. So this is a really simplified bootloader. It's actually a lot more complicated than that. But usually you power on the phone. The burned in bootloader powers up. And it loads a secondary stage bootloader. And that loads like the RPM firmware to handle a lot of the power on the device. And then it loads a second stage, which actually loads your Trust Zone. Your QSE, your is Qualcomm's trust zone environment. So it loads that, and then the trust zone handles loading everything else. So all the cryptographic signatures are verified by the trust zone after this point. So that loads the application bootloader. So that's like Aboot or Sboot, which actually boots your Android kernel, for example. So that handles loading the kernel. And then the kernel itself pushes over something called a modem boot authenticator, which handles validating the actual modem image, which is then pushed over. And the modem has its own, say, bootloader, and then the AMSS software is validated, and the modem kind of is brought up. Then after that, your wireless and your other DSPs are brought online. So the baseband and the application processor have to have some way to be able to talk to each other, to be able to say, hey, I want to make a phone call, hey, you got an incoming text, things like that. So on most devices, there's a form of shared memory interface where one side writes some data, and the other side checks periodically and picks it up and processes it. And there's actually a common set of APIs, if we're talking about Android here. iOS does a little different. But there's a common set of APIs on the Linux and on the modem side, which take care of initializing this, allocating this, searching for things in it. So it is part of the actual main RAM. The baseband chip does not, it has its own separate RAM, and neither do any of these other subsystems. They are all actually talking to the same eight or however many gigs of RAM you have on your phone. And it's just portioned up for what each little device wants its own little piece. So the SMD is the shared memory driver, which is a more high-level wrapper around some of these low-level APIs. So you can kind of just have a pipe and shove data into it. And then the other side just reads from it. It's a little less complicated for driver developers. Each of your... Your AMSS tasks has its own separate two-way channel over this shared memory interface. So your diagnostics has a set of channels, and that's where it passes information. Remote procedure calls have their own set of channels. Your GPS has your own set of channels. So this is one of your main attack surfaces if you're looking to elevate your privileges from the baseband chip itself to the actual application processor. They talk back and forth, and this is one of the main ways they communicate. So if you can find bugs and how messages are being parsed or sent, that's how you can kind of jump and try and get code execution on the other side. One of the other things they also have is the QMI protocol Qualcomm designed. Originally, it was kind of designed to supplement the old Haze AT command set you used to program modems with. And it itself, like Qualcomm, everything Qualcomm does, is a wraparound of another older protocol they used to have. But it's used to talk to your modem. You can configure it, send it different commands, but you don't actually make any hardware decisions based off of this. All you can do is kind of influence how different pieces act. And again, just like the diagnostics task, it's a client-server model. It's a variable length packet structure, which is, interestingly, this is where a lot of your bugs are gonna be. There's a set header, and then a different number of payloads you can send, and these are all type-length value-based. So the first portion specifies what this is. You know, if you're trying to set up a phone call, or what you're trying to accomplish. And then the next piece is how long is the message, and then followed by the actual data. So, because this was originally coded quite a while ago, it's quite possible to find bugs, and it just blindly trusts the values you're feeding it, for instance, like your length. And if it passes that into some sort of copy, or it makes parsing decisions based on that, you can try and get code execution in the kernel on the Android or iOS side. So, the real kind of new thing about what Qualcomm's been doing is they designed their own custom in-house DSP. It's not really based on anything else. So, you know, if you were here and you heard about the CTF last year, where they wrote their own custom architecture with 9-bit bytes, it's not quite as bad as that, but it's not really related to any type of Intel or ARM assembly code you usually see. It's a general-purpose digital signal processor, which is really interesting. It's not specifically designed to handle cellular stuff, which is why you can handle, like, audio processing on it as well. So there are two on the SOC, like I mentioned earlier. One is dedicated just for your broadband radio, and the second one is just for audio processing, and if you write an app, you can actually access this. And that's its own separate attack service for, like, breaking out of sandboxes if you're trying to play audio and things like that, because that other audio DSP also has its own RTOS on it, and if you can pop that, you kind of get, like, media server privileges depending on how your Android device is enabled, and you have a lot more privileges from there to try and escalate the system. So the way the Hexagon chip is laid out, you have a variable number of hardware threads. It depends on the specific iteration of the chip, but you can say four for a general example. So kind of like cores on an Intel device. And each of those threads shares the L1 and the L2 caches between them. The threads don't have separate caches. But there are separate caches for instructions and for data in operands, just like you have on an ARM or a PowerPC chip. So you can't... Even if there was no NX or DEP, you couldn't just smash the stack and then jump to your shell code because that's data, and it won't be in the instruction cache, and you'll cause a fault. So you actually do have to, like, dork with cache lines and stuff if you're trying to build an exploit like that. And the other really interesting thing is these four hardware threads are designed for parallel execution. So when you compile a program down to Hexagon, it actually... The compiler figures out different operations which don't need... They don't depend on each other. So then it will schedule those just to run at the same time simultaneously across all four threads. So this is kind of on a high level how it's laid out. Each thread has its own execution unit. It pulls instructions. Those are packet-based, so your packet doesn't have to be four instructions. It can be three. And the execution unit will just not fetch anything for that. So you don't have the packets running out of order. You can't have a packet with three running on three of the threads and the other threads actually like a packet ahead. It'll just kind of spin its wheels for a cycle and then pull in the next packet. And then they have shared instruction and data caches and then a shared more high level L2 cache. So the architecture is kind of standard as far as embedded devices go. You have a bunch of general purpose registers. They're 32 bits wide. Your first six are used to pass arguments to functions when you're looking at the assembly. The return value is stored in register zero. And then you have a split between caller saved and callee saved just kind of like you do on most other architectures. The stack is again pretty standard. You have a stack pointer and a frame pointer and you have a link register just like ARM for holding return addresses in. The stack grows downwards from high to low memory. It does need to be eight byte aligned which is a one gotcha when you're writing an exploit. And because Qualcomm designed this architecture from scratch they kind of had the freedom to put in a lot of different things they wanted to. So they have instructions which will just take care of setting up and tearing down your stack frames as opposed to the compiler wasting a few instructions like it kind of does with Intel and ARM. So it's pretty standard. The threads in their RTOS are actually saved on the stack. They're pretty far apart but if you have a large enough overwrite you can corrupt the state of another thread or another process just like you were able to back in Rex. That hasn't really changed. Besides your kind of standard yeah we have registers we pass arguments in them it works like a computer. They add in a couple really useful things for debugging. So you have a system status register which is the first place you should be checking when you get like a RAM dump or any kind of crash. So this holds the ACID which is the address space ID the crash happened in. It'll hold an eight bit value for the cause of the crash whether it was a like a page fault or you know divide by zero or your other standard operations. And it will tell you which of the virtual address registers to look in for more information. So there are actually two different bad virtual address registers which hold exception addresses generated so the address of the exception handler when an exception occurs. And there's like two separate execution slots for each of those threads which is why there's two registers. And then you have an exception link register which holds the program counter value from when an exception was triggered. So it saves it off for you. So they actually have a lot of useful debugging information. So this is a little more recently introduced. They've been constantly making iterations of this processor. So on the most recent ones probably shipped into phones in the last year or so. They've introduced something called protection domains which implement hardware hardware enforced separate address spaces. And they did this because they have the capability to run the WiFi subsystem on the same DSP as the broadband radio. And they want to keep them very very separate so they can't touch each other. And this sounds like a really cool idea in theory except that means your WiFi and your broadband are going to be sharing the same layer 1 and layer 2 caches. I haven't really dug into that but I'm pretty sure you can either get interleaks or really influence the way the other one is seeing the cache. So it's kind of divided. There's a kernel mode then they have kind of like a second split after that in between standard users and guests. So guest is more like ring 1 or ring 2 on an Intel device as opposed to ring 3 which is the user. So your broadband radio stuff kind of runs in the guest OS mode. The RTOS kernel itself runs in the kernel mode. And then your WiFi would run in user mode. And interestingly enough sometimes the stack checks for like the stack canaries and stuff aren't actually done in kernel mode only in the user and the guest mode. Alright. So cellular stuff is very very complicated. I spent way too much time reading documentation trying to figure this stuff out. But after the first generation telephone lines it kind of split into two branches as they were going into wireless. So you have GSM which is time division multiple access based and then you have the standard Qualcomm came up with which is why they really own a lot of patents around it which is CDMA which is code division multiple access I think. And those kind of split onto separate branches for like the next 20 years. Each adding things independently of each other. And then LTE is kind of a convergence of the two. So over on my screen is on the left. But where you see GSM and CDMA that's really like second generation what you think of as a 2G cellular network. Those did not originally have any ability for data. You had no internet. You couldn't get your emails. So that was kind of tacked onto the standards with a form of edge and GPRS in the case of GSM networks. So that's really what they call 2.5G once you start digging into like the cellular forums. And then it kind of kept on proceeding through 3G and adding more higher speeds trying to make things more efficient, less power hungry. And then LTE is kind of where we're at now. And they're already discussing the fifth generation network standards. So just like when you're looking at networking on a computer you have your OSI or your TCPIP model and things are kind of split up into different layers. Cellular stack has that as well. Things are broken up into several different layers. And just like with TCPIP the higher up you go the more complicated things are and there are more bugs. Think of it kind of like layer 2 you have your ethernet and you can have ARP spoofing and stuff. And it's really confusing computers about who you are. And then you go up to IP you have more nonsense you can play with the headers like IP fragmentation and reassembly. It's more trying to dork with the actual other end of the connection as opposed to fooling them about who you are. So cellular is pretty much the same way. You have your physical layer which in this case it's not over a wire it's over the air. And then as you go up it gets more complicated and there's more things to parse. So up at this top level with like the NAS layer and your internet protocol stuff is a really good place to hunt for bugs. Like all your call control signaling happens up there. There's loads of messages with like length value and type length value associated with them. Going down a little bit to like radio resource control there's lots of messages in there but typically they're a little more fixed length when you're digging through the standards. So there is a load of different ways you can actually make or receive a call. 2G, 3G, 2.5G over LTE, is it a real voice call or is it just voice over data call? You know, voice over LTE is more common now. But any kind of VoIP call you can make over the internet you can also make it over the internet on your phone. And the cellular phones typically support all of these because you don't know where in the country or what network you're really going to be on and your call type can drop as you're switching in between cells. If you've ever been driving and your call quality gets bad and it dropped to 2G because you have no cell coverage. So all these different ways to do things adds a lot of complexity. And these aren't really that simple to begin with. There's a lot more involved in the set up and the tear down than there is in just like a TCPIP handshake. To be fair, only from BTS over to MS is the actual handset part of this. The rest of that is actually more in the back end of the cellular network. But it's still pretty complicated. Trying to go through these. The best place to start looking for bugs is the standards. They don't specify any kind of implementation. They just say what it should look like. And it's totally up to the vendor and the carrier and the OEM to work this out. 3GPP is the organization which kind of says these are the standards we're going to deal with. And that's made up of several different organizations and they all argue and eventually agree on something. Unlike Wi-Fi or Bluetooth, these are freely available. You can just go download them, which is fantastic. The reason nobody does is because they're like 80,000 pages long. And they're terrible. But they're actually really useful when you are looking for bugs. So you can kind of see it specifies the packet itself. What each octet is in the packet. And then it breaks down to each bit in that octet. So that's interesting after you think you've found a bug and you're trying to trigger it. The more interesting part to look at to start is where it specifies what options you can really add. So there's a bunch of mandatory and optional ones as you can see. And you're interested in all the ones which have variable length options basically. LV or TLV. Because there's a good chance one of these they screwed up somewhere along the way. So how do you actually want to get started dealing with this? It's their own custom architecture. There is no like IDA Pro does not support this natively. So there are a few plug-ins for IDA Pro. Out of the three of those, I found the GSMK one off of GitHub to be the most intuitive to start with at first. It's actually been updated somewhat recently for IDA 7. The second one down is actually a Python wrapper around the objdump patches Qualcomm themselves has provided. So that's a good place to start for if you want to write your own disassembler because they kind of did some of the hard work of parsing the documentation for you. I started trying to port that into Binary Ninja because I wanted the lifting to an intermediate language. But that's still really gross and in no shape to be released to anybody unfortunately. So what do you actually look at on a device? The radio firmware is usually on its own separate partition. It varies from device to device. You'll have to find a way to pull that off. You might have to root your phone in order to get it. It really varies because the Android ecosystem is so fragmented. It's a massive binary. This one I just pulled off of my current phone is 300 megabytes. And when you load it into IDA, it will find 80,000 plus functions. And there's no debugging information or anything. There is a lot of strings which is useful. Especially a lot of debugging information left in those strings. But you still have 80,000 functions to dig through with no really good place to start. So I was trying to cut down on the effort. So one of my ideas I had was if I'm looking for exploits, I'm going to be looking for memory copies and string copies. And I want to find the memory allocations and the deallocations and all that kind of stuff. All your standard library functions essentially. So I was like those should be called fairly frequently I think. And how many different pieces need to parse things. So you could use IDA Python to kind of count the number of cross-references to a function. That doesn't necessarily give you the best metric because one part of that graph could be called a whole bunch of different times. But it's an easy place to start. And these things will be called two, three, four thousand times. So you can kind of sort that and start at the top of the list and work down. And I found memory copies and things like that pretty quickly. It's pretty obvious when you're staring at the assembly code. A couple of tight copy loops and a stub at either end. So how you actually debug this thing is the problem. Even debugging an Android kernel is really irritating to do if you've ever tried it when you're trying to do it on a device. You have to do a serial over UART option or over USB and you've got to patch it and KGDB is not fun to deal with. This is even worse. So Qualcomm has some internal tools which use that Diag protocol and talk to it. We aren't really supposed to have those. A few of them have been leaked from different carriers and things like that. It's pretty hit or miss. Some of them don't work on newer phones. These phones do support JTAG although on some phones it's disabled in production devices. But to get that JTAG connection you need to buy it from a company called Lagerbach and they're the only ones who make it. And it's something like north of $10,000. It was way out of my budget. Although I'm sure it works very well. The approach some of the other presentations on the subject have taken is they get memory read-write through an exploit and then they build a debugger around that. That implies you already have an exploit to start with rather than using a debugger to help you poke around. Either one is a good option. But the way I took was since the modem firmware is right here I can just patch that and try to make it do what I want. The only problem with that is all of the other modems are signed. So if you make a change you'll fail the signature verification and nothing happens. Unlike Samsung modems which are encrypted on disk Qualcomm doesn't encrypt them so it's easy to disassemble and patch them but you have to get past the signature verification. Which like I mentioned earlier with the boot order happens in the trust zone. So you need a trust zone bug. The great thing about basebands is they don't change that often because this firmware has to be recertified. At least it's a place to start. So there's been trust zone bugs going back years. Any number of them will work depends on which phone and which trust zone version you have. But it's only a two byte change in the trust zone you patch which disables the signature checking. So you can use that as an arbitrary write you patch out those bytes and then you can load whatever the hell you want. So it does require you to have an unlocked boot loader so that you can compile your own kernel and slash that. And the modem has it's own set of internal hashes for different segments within it. And those still do need to be consistent but those aren't cryptographic in nature they're just more of an integrity error kind of check. And you can fix those up. So there's plenty of different CVEs they're all over the place some of them have like full pox you don't even need to do that much work to turn it into a trust zone write to disable things with. So then you actually need to build a debugger around this and I'm running a little short on time. So to build a debugger you basically need to be able to read and write memory to set breakpoints. If you're trying to break on access to a piece of data you might need to change permissions since you're really not supposed to be able to write to that. And setting register values is kind of useful too. So there's a few different ways the baseband takes input. Obviously over the air is the main way. Trying to implement a debugger over the radio is not something I wanted to try. You have that shared memory interface again and you could write a kernel drive at a time and talk to that but you need something to respond on the other end. So one of the things I did was they implemented a debugging stub on the other end and they kind of talked back and forth and built a full feature debugger around it. I didn't really want to take the effort to do that and one of the other ways you used to originally talk to a modem was over serial. For these Hayes AT commands is how you originally used to configure something. You could pass it dialing and connection parameters and say here's the number and make a call and it did it. So we've kind of come a long way and vendors add their own command extensions to them. Some of them do really ridiculous things. Samsung has had issues in the past where there are commands to unlock the device so even if it's passcode locked if you plug it into your computer and talk to the modem you can unlock it. So some of those really aren't that great. So since I can patch the modem firmware and I can patch out the signature checking so I can upload my own I figured this is an already easy way to talk to the device I don't need to write my own stub on the other end and these already have a way to all the input output to serial is already handled for you because there's 100 different AT commands all that logic is already pre-declared for you so I can just hook one of those handlers patch out what it does and replace it with my functionality. So I picked a couple different Qualcomm commands which aren't really used frequently I'm not even sure what some of them do but now they do what I want them to do so you can just you make one a read, you pass it naturally you pass an address, you pass a size it spits stuff back out same thing with writes and trying to change permissions you can turn these primitives into a more featured debugger without actually having to dork with a ton of code on the modem side. And I found about this after I put in all this hard work is that company Lauterbach actually makes a simulator for Hexagon so you can upload a RAM dump to this simulator and it will show you the whole processor state and you can debug it and this is actually really cool and it is actually free there's like a free trial you can get and sure like your scripting abilities are kind of limited but you can kind of work with that for not having to pay $10,000 if you actually want to test stuff you need a license to broadcast on cellular frequencies most places in the world otherwise like in America the FCC gets really mad at you so you're supposed to have some kind of faraday cage or do it in your concrete basement and pray they never come knocking on your door and I've been really lucky in that software defined rate and the videos have gotten a lot cheaper recently and there's a lot of different ones to choose from and as long as it's full duplex it can transmit and receive at the same time it can support GSM and Edge and GPRS and the different things I want to play with I just bought the BladeRF recently because that was the cheapest and I'm a student and I'm poor but the options get way more expensive after that but the BladeRF works great for my purposes I've had zero complaints all it needs to do is be a full duplex I think the HackerRF is cheaper but it is not full duplex that won't work a bunch of different open source projects have come up YateBTS OpenBTS the list keeps on growing every year YateBTS is the one I ended up going with as it's actually really cleanly documented and in the code comments it'll say which part of the standards this came from and which table these options came from it's really easy to cross reference when you're trying to root cause a bug and trigger it and yeah I think we're pretty much out of time but I will be hanging around if anybody has questions for me or you can hit me up on Twitter or something I usually respond pretty quickly so yeah