1 00:00:03,440 --> 00:00:10,780 Good morning. Aloha and welcome to my talk on Apple's background task management. My name is 2 00:00:10,780 --> 00:00:15,620 Patrick Wardle. I am the founder of the Objective C Foundation. There I work on free open 3 00:00:15,620 --> 00:00:22,520 source Mac security tools. I write the Art of Mac Malware book series and also co-found and 4 00:00:22,520 --> 00:00:27,560 organized the Objective by the Sea Mac security conference. So basically all things Apple, 5 00:00:27,560 --> 00:00:34,080 all things Mac. So today, what will you learn? What will we dive into? What am I going to be 6 00:00:34,080 --> 00:00:39,300 talking about? Well, the cool thing is there's kind of many facets to this talk. I mean, really 7 00:00:39,300 --> 00:00:47,500 the goal is to focus and understand on Apple's, Mac OS's background task management, BTM, which of 8 00:00:47,500 --> 00:00:52,140 course will explain exactly what that is. But we're also going to cover topics of reversing 9 00:00:52,140 --> 00:00:57,000 malware detection, Mac OS internals and more. So hopefully there's something in this talk for 10 00:00:57,000 --> 00:00:57,540 everyone. 11 00:00:58,460 --> 00:01:03,280 We're going to start by diving into the internals of Apple's new background task 12 00:01:03,280 --> 00:01:09,360 management. I will caveat this part. We're going to get into the weeds pretty deep, some reverse 13 00:01:09,360 --> 00:01:14,800 engineering, disassembly. But this is really important for the remainder of the talk. And with 14 00:01:14,800 --> 00:01:20,360 an understanding then of the internals of this background task management, we'll then show how we 15 00:01:20,360 --> 00:01:25,380 can build some neat tools to leverage this. Then, because, you know, DEF CON is a hacker 16 00:01:25,380 --> 00:01:27,540 conference, we'll talk about, of course, how to fully buy it. So let's get started. 17 00:01:27,540 --> 00:01:30,040 So we're going to start by talking about how to bypass this new system. And then we'll end by talking 18 00:01:30,040 --> 00:01:36,660 about how to detect our zero-day bypasses. So first let's start with an overview, starting with 19 00:01:36,660 --> 00:01:42,580 defining and talking about what is background task management. So this is something that was 20 00:01:42,580 --> 00:01:48,780 largely introduced in Mac OS 13, so the most recent version of Mac OS. And what it is, it's a 21 00:01:48,780 --> 00:01:57,220 system designed by Apple on Mac OS to govern and consolidate persistent items or in Apple's parlance, 22 00:01:57,220 --> 00:02:03,040 background tasks. So what are persistent items? Persistent items, in the context of this talk, are 23 00:02:03,040 --> 00:02:10,240 simply processes that have installed themselves in a manner that every time the system is rebooted or 24 00:02:10,240 --> 00:02:16,660 the user re-logs in, are automatically re-executed by the operating system. So things like an auto 25 00:02:16,660 --> 00:02:22,820 updater that needs to run every day is going to persistently install itself. Of course, malware is 26 00:02:22,820 --> 00:02:27,160 going to do the same thing, because if malware does not persist and you reboot your system, you've 27 00:02:27,220 --> 00:02:33,380 just disinfected yourself. Now what happens when an item is installed that persistently 28 00:02:33,380 --> 00:02:40,580 installs itself is three things, three visual things related to BTM. First, and again this happens 29 00:02:40,580 --> 00:02:45,620 on the most recent version of Mac OS, there's going to be a system alert informing the user that 30 00:02:45,620 --> 00:02:50,680 whatever they just installed is persisting. So an alert to the user saying, hey, something just 31 00:02:50,680 --> 00:02:55,920 added itself to the operating system, it's going to be automatically restarted. Second, there is a 32 00:02:55,920 --> 00:02:57,200 security message that is going to be sent to the user, and that security message is going to be 33 00:02:57,200 --> 00:03:03,440 a message that is broadcast that security tools can register for, which is a notification, a 34 00:03:03,440 --> 00:03:08,180 programmatic notification saying, hey, something just persisted. You can see from a security point 35 00:03:08,180 --> 00:03:12,540 of view, from a security tool, this is something that's very useful. And then finally that item is 36 00:03:12,540 --> 00:03:19,560 going to be registered in a database, the BTM database. So why do we even care about this? Who 37 00:03:19,560 --> 00:03:25,700 cares about this? Well, we do. First and foremost, as defenders, we now have a nicely organized 38 00:03:26,180 --> 00:03:30,860 subsystem that manages most persistent items. If we can understand and tap into that, 39 00:03:31,000 --> 00:03:35,680 we can build, and I'll show you exactly how, powerful security tools that can allow us to 40 00:03:35,680 --> 00:03:42,200 generically detect persistent malware. That is awesome. As hackers, though, BTM poses problem. 41 00:03:43,600 --> 00:03:49,080 Two reasons. There's going to be an alert telling the user, hey, something just persisted, so if 42 00:03:49,080 --> 00:03:54,380 you get on a system and try to persist your back door, the user will be alerted. Also as I just 43 00:03:54,380 --> 00:03:55,680 mentioned, there is a security message said, I am not expecting this to work. It seems like they're 44 00:03:55,700 --> 00:04:00,100 that's broadcasted so any security tools that are installed on the system will get a 45 00:04:00,100 --> 00:04:04,720 notification. These are both brand new events on the latest version of Mac OS. So you can see 46 00:04:04,720 --> 00:04:09,400 as hackers this is a problem for us. So we need to figure out exactly how to get around this. 47 00:04:10,400 --> 00:04:14,780 Now BTM is largely undocumented. Apple really doesn't describe a lot. So the first part of 48 00:04:14,780 --> 00:04:21,040 this talk is really going to be diving into the internals of this subsystem. Now when I want 49 00:04:21,040 --> 00:04:26,460 to understand something on Mac OS I always turn to the logs and this is because a lot of 50 00:04:26,460 --> 00:04:30,680 components in the operating system, even undocumented ones, give us a lot of very 51 00:04:30,680 --> 00:04:37,900 informative log messages. Unfortunately on Mac OS a lot of log messages are redacted so what we 52 00:04:37,900 --> 00:04:43,360 first have to do is install a logging profile which basically tells the operating system, hey, 53 00:04:43,460 --> 00:04:47,720 please unredact and log everything. And there's a link on the slide how to do that. It's 54 00:04:47,720 --> 00:04:51,020 pretty straightforward but just know if you're going to be splunking around in the log 55 00:04:51,040 --> 00:04:56,800 make sure you've kind of turned on this full logging capability. So now that we have logging 56 00:04:56,800 --> 00:05:05,080 off and running, we just install a launch daemon which is a persistent component. And then look 57 00:05:05,080 --> 00:05:11,180 in the logs and see what is shown. So we can see on the slide there's a few very interesting log 58 00:05:11,180 --> 00:05:16,380 messages that are directly related to the BTM subsystem. And what we're going to do is we're 59 00:05:16,380 --> 00:05:20,420 going to take these log messages and find the binaries that are responsible for generating 60 00:05:20,420 --> 00:05:21,020 them. And then we're going to take these log messages and find the binaries that are 61 00:05:21,040 --> 00:05:24,980 responsible for generating them and look in their disassembly at the functions that are 62 00:05:25,500 --> 00:05:29,580 broadcasting or logging these messages to figure out exactly what's going on. So they're great 63 00:05:29,580 --> 00:05:36,480 signposts. So we can see two things are happening. First, there are two binaries, two 64 00:05:36,480 --> 00:05:41,820 processes that appear to be responsible for BTM. One is called SMD and the other one is called 65 00:05:41,820 --> 00:05:47,740 background task management D. We'll dive into both of these. And based on the method names, we 66 00:05:47,740 --> 00:05:51,020 first see one that's called launch D will scan your path. And the other one is called 67 00:05:51,780 --> 00:05:57,800 register launch item. So it appears that the operating system has noticed that a new item was 68 00:05:57,800 --> 00:06:04,340 persisted. It's going to scan that directory and then register that item. This makes sense. So 69 00:06:04,340 --> 00:06:10,120 let's first talk about SMD. This is the system management demon. And the first thing it does is we 70 00:06:10,120 --> 00:06:18,180 see that it generates a log message saying got a FS event. FS stands for file system. And if we 71 00:06:18,180 --> 00:06:21,000 debug this, we can see that when this log message is generated, we can see that the system is 72 00:06:21,000 --> 00:06:25,240 called. If we poke around and look at some of the registers, we can see that one of the 73 00:06:25,240 --> 00:06:33,200 registers contains an XPC dictionary with an event that is related to watch paths. Now, if we 74 00:06:33,200 --> 00:06:38,180 look at SMD's property list, which contains information about how it should be launched and 75 00:06:38,180 --> 00:06:43,440 some other activities, we can see it contains key value pairs that tell the operating system, 76 00:06:44,080 --> 00:06:49,720 please invoke me any time something is written, for example, to the launch daemon directory. If 77 00:06:49,720 --> 00:06:50,980 you're not familiar with Mac, you're not familiar with Mac, you're not familiar with the 78 00:06:51,000 --> 00:06:55,900 OS, writing something to the launch daemon directory is how you persist an item as a daemon. 79 00:06:56,420 --> 00:07:01,680 So what this is telling the operating system is, hey, I am interested any time something is 80 00:07:01,680 --> 00:07:07,620 added to this directory. If we dig in a little more, looking at some of the method names that we 81 00:07:07,620 --> 00:07:14,800 saw in the log messages, we see that SMD will generate a XPC connection to a service called 82 00:07:14,800 --> 00:07:20,980 com.apple.backgroundtaskmanagement. Now, XPC is a Mac-specific program. It's not a Mac-specific 83 00:07:21,000 --> 00:07:25,480 program. It's an IPC inter-process communication mechanism that allows one process to talk to 84 00:07:25,480 --> 00:07:31,960 another, to both send messages but also to invoke methods and code in the remote process. And 85 00:07:31,960 --> 00:07:37,680 we'll see BTM utilizes this heavily. Again, if we poke around in the debugger, we can see that 86 00:07:37,680 --> 00:07:43,740 the scan path that SMD is looking at and is going to talk to the other component is the launch 87 00:07:43,740 --> 00:07:49,920 daemon directory, which is where our persistent item was just placed. Let's look a little bit more 88 00:07:51,000 --> 00:07:53,960 closely at this. We can see that it's sending messages to confirm who SMD is sending them to. 89 00:07:54,200 --> 00:08:01,840 We can use Mac OS's LSMP command. This is LS list MP mock ports. This will show you who is sending 90 00:08:01,840 --> 00:08:08,020 mock messages and to whom. So we get the PID of background task management, run LSMP, and we 91 00:08:08,020 --> 00:08:14,280 can see, yes, there's a bunch of mock messages, XPC messages coming from SMD. And if we set a 92 00:08:14,280 --> 00:08:20,560 break point on the BTM daemon, we can see that the way its methods are invoked are via this XPC 93 00:08:20,560 --> 00:08:26,660 message. So really, SMD is just talking to the BTM daemon. So now let's look at this BTM daemon. 94 00:08:26,820 --> 00:08:31,380 This is like the second piece of the puzzle, kind of the core component of the background task 95 00:08:31,380 --> 00:08:37,400 management subsystem. If we look at its property list, we can see it has various mock interfaces. 96 00:08:37,880 --> 00:08:44,800 These are the XPC interfaces that processes such as SMD can connect and talk to. And then also the 97 00:08:44,800 --> 00:08:50,540 path to its binary. Let's take a closer look at its binary. We can see that it has a lot of 98 00:08:50,560 --> 00:08:54,140 features. We can see first that it's going to be running with root privileges. This makes sense 99 00:08:54,140 --> 00:08:59,820 because it needs to monitor system activity, manage these persistent items. If we look at its 100 00:08:59,820 --> 00:09:05,440 entitlements, which are capabilities, additional features that the operating system will give it, 101 00:09:05,580 --> 00:09:12,080 we can see it has some related to managing BTM. Also, there's an interesting one, and we'll dive 102 00:09:12,080 --> 00:09:17,460 into this more, showing it has an entitlement allowing it to submit endpoint security events. 103 00:09:17,700 --> 00:09:20,460 We'll see this is how that security message is generated. 104 00:09:21,640 --> 00:09:28,820 If we look at its launch item registration function, we can see that what it's going to do is once it 105 00:09:28,820 --> 00:09:34,600 gets a message from SMD, it's going to look at, for example, the launch daemon directory and find 106 00:09:34,600 --> 00:09:39,260 the new item that was added. So, for example, when I install Zoom on my system, Zoom 107 00:09:39,260 --> 00:09:44,500 legitimately installs a launch daemon, a persistent component. We can see in the debugger that 108 00:09:44,500 --> 00:09:49,160 the BTM daemon has now the property list of that new item that was registered. 109 00:09:50,860 --> 00:09:58,860 We now have another player that enters the scene, and this is the BTM agent. So if we go back to the logs, 110 00:09:58,960 --> 00:10:04,720 we can see that there's a log message from the BTM daemon that says, should post advisory, and in this 111 00:10:04,720 --> 00:10:11,100 case, it is set equal to true. Next, we see a log message from a new process called the background 112 00:10:11,100 --> 00:10:18,020 task management agent, and we can say, we can see a log message saying posting new advisory notification. 113 00:10:18,620 --> 00:10:20,540 So at the bottom of the slide, we have kind of a little bit more information about what the new 114 00:10:20,540 --> 00:10:23,860 process is going to look like. So this is kind of now a full diagrammatic overview where SMD is 115 00:10:23,860 --> 00:10:30,020 going to get the initial notification, send an XPC message to the BTM daemon. The BTM daemon is then 116 00:10:30,020 --> 00:10:36,240 going to talk to this new process, the BTM agent, which is then going to show the alert to the user. 117 00:10:36,700 --> 00:10:42,160 So let's briefly look at this agent. It's the last piece of the BTM puzzle. If we look at its 118 00:10:42,160 --> 00:10:48,000 property list, we can see it has some mock and XPC interfaces. Again, these are exposed so that 119 00:10:48,300 --> 00:10:50,520 processes, such as the BTM daemon, will be able to access those processes. So we can see, 120 00:10:50,540 --> 00:10:56,200 we can see that the BTM agent can send a message to it. And then also the path to its binary. If we 121 00:10:56,200 --> 00:11:02,020 look at its binary, we notice it's running as not root but as the user. This makes sense because 122 00:11:02,020 --> 00:11:07,900 the agent is responsible for displaying the notification on the user's desktop, which you have 123 00:11:07,900 --> 00:11:13,380 to be running in the user session in order to do that. In terms of entitlement, we can see that it 124 00:11:13,380 --> 00:11:20,100 has various BTM specific entitlements and also then a private Apple entitlement that allows it to 125 00:11:20,100 --> 00:11:24,380 generate system notifications. On Mac OS, you need these entitlements, for example, to show a 126 00:11:24,380 --> 00:11:30,760 system prompt. Okay. We're kind of wrapping up the internals of this. Kind of one of the last pieces. 127 00:11:31,060 --> 00:11:36,520 We need to look a little closer at how this message is actually displayed. So we know that the BTM 128 00:11:36,520 --> 00:11:42,660 daemon is going to send a message to the BTM agent. And if we look at the agent's disassembly, we can 129 00:11:42,660 --> 00:11:49,080 see it has a method aptly named post advisory for content. And in the bottom, I've debugged this 130 00:11:49,080 --> 00:11:53,940 and set a break point on this method. And we can see that the argument that it's passed is the 131 00:11:53,940 --> 00:12:00,720 message that should be displayed in the notification. So this is just the internals of how the 132 00:12:00,720 --> 00:12:07,900 agent then will display the message to the user alerting them as to the fact that something was 133 00:12:07,900 --> 00:12:15,140 just persistently installed. Once that has been displayed, we see one other log message which 134 00:12:15,140 --> 00:12:19,060 shows that the background task management daemon, it's kind of the core piece of the 135 00:12:19,080 --> 00:12:25,360 database, is actually updating some undocumented proprietary database with what appears to be the 136 00:12:25,360 --> 00:12:30,220 item that was registered. And we'll dig into this more when we dive into writing a tool. But 137 00:12:30,220 --> 00:12:35,820 basically what the BTM daemon is doing is now storing information about the item that was just 138 00:12:35,820 --> 00:12:42,940 persisted into a new database. So that was kind of an overview. We kind of got into the weeds. But 139 00:12:42,940 --> 00:12:47,980 it's important again to understand these different components because as we now talk about writing 140 00:12:47,980 --> 00:12:53,820 tools to interact with them, we have to understand those. And also when we want to bypass these, 141 00:12:54,120 --> 00:12:59,880 we're going to have to have this understanding as well. So that's out of the way. Let's kind of pull 142 00:12:59,880 --> 00:13:05,880 it back a little bit and talk first about how we can write some neat tools to leverage BTM to 143 00:13:05,880 --> 00:13:13,000 detect persistent malware. Now why this is such a great idea, in my opinion, is because the vast 144 00:13:13,000 --> 00:13:17,960 majority of malware is going to persist. Again, persistence is a way to ensure that you're 145 00:13:17,980 --> 00:13:24,080 automatically re-executed by the operating system each time the system is rebooted. So this is 146 00:13:24,080 --> 00:13:31,860 something that the vast, vast majority of malware strives to achieve. I study the new Mac malware of 147 00:13:31,860 --> 00:13:37,540 each year. It's kind of one of my end-of-the-year highlights, sadly. And I've noticed some 148 00:13:37,540 --> 00:13:42,120 interesting trends, and this is not surprising to me, but again, this just reiterates that most 149 00:13:42,660 --> 00:13:47,960 malware is going to persist. So in 2015, which is now almost ten years ago, I was able to 150 00:13:47,980 --> 00:13:52,540 write a tool where I was like, hey, since all Mac malware is persisting, I should write a tool 151 00:13:52,540 --> 00:13:58,680 that monitors for these persistent events, and then when malware persists, regardless of how it 152 00:13:58,680 --> 00:14:04,280 got onto your system via zero day, a trojanized installer, a supply chain attack, like, I might 153 00:14:04,280 --> 00:14:10,400 not be able to detect all that, but as soon as it persists, I can alert the user. So I released 154 00:14:10,400 --> 00:14:15,580 this free open source tool called BlockBlock, and BlockBlock does exactly kind of as the name 155 00:14:15,580 --> 00:14:17,960 implies. It will monitor for persistent malware, and it will monitor for persistent 156 00:14:17,980 --> 00:14:23,900 activity, and then alert the user. So this is what Apple is doing now. It took them ten years or so 157 00:14:23,900 --> 00:14:29,400 to realize this is a good idea. I like to think they were inspired by my tool. But, you know, from a 158 00:14:29,400 --> 00:14:33,420 security point of view, this is great that now this is built into the operating system. And to me, 159 00:14:33,440 --> 00:14:39,320 again, this just iterates the fact that Apple now has also realized that because most Mac malware is 160 00:14:39,320 --> 00:14:45,520 going to persist, this is a great way to now alert the user that perhaps something has slipped 161 00:14:45,520 --> 00:14:46,640 into their system. 162 00:14:47,980 --> 00:14:54,020 Let's look at a brief example, Dazzle Spy, one of my great examples for this because it was 163 00:14:54,020 --> 00:15:00,880 deployed via zero day Safari bugs. So a very powerful deployment mechanism, right? Users in this 164 00:15:00,880 --> 00:15:06,400 case were almost SOL. If they inadvertently browsed to the site, clicked on a link, the zero 165 00:15:06,400 --> 00:15:13,100 days would infect their operating systems, their Mac OS systems, even if they were fully patched. 166 00:15:13,660 --> 00:15:17,960 If we look at the embedded strings for Dazzle Spy, we can see some of the things that are 167 00:15:17,980 --> 00:15:21,060 happening. We can see something related to a launch agent, which is another way you can persist. 168 00:15:21,340 --> 00:15:27,060 And if we run a file monitor and execute the malware, for example, in a virtual machine, we can 169 00:15:27,060 --> 00:15:34,700 passively observe it persisting itself as a launch agent. So, again, even if we can't detect the 170 00:15:34,700 --> 00:15:40,780 zero day exploitation or prevent that, as soon as the malware goes to persist, if we are able to 171 00:15:40,780 --> 00:15:45,540 detect that, we can then alert the user that something is amiss and maybe stop the infection 172 00:15:45,540 --> 00:15:46,420 before it's gone too far. 173 00:15:47,980 --> 00:15:54,880 So the first idea I had to leverage this new BTM is I want to dump the database because the 174 00:15:54,880 --> 00:16:01,180 BTM database has a nice comprehensive list of all the persistent items. So the idea is, like, 175 00:16:01,180 --> 00:16:05,920 let's write a tool that you can run on a Mac system that says, show me everything that's 176 00:16:05,920 --> 00:16:12,180 persisted. Now, this will include legitimate software. For example, Zoom we saw. But if there is 177 00:16:12,180 --> 00:16:17,400 any persistent malware on your system, it will likely also be in this database. The problem is, 178 00:16:17,420 --> 00:16:17,960 this database is not going to be in the system. It's going to be in the system. It's going to be in the 179 00:16:17,980 --> 00:16:22,300 database. This database is proprietary and undocumented. Luckily, though, Apple ships a tool 180 00:16:22,300 --> 00:16:28,520 called the SFL tool that allows you to interact with this database. And one of the commands it has 181 00:16:28,520 --> 00:16:33,860 is dump BTM. And if you execute this, it will nicely dump the database. So here, for example, we 182 00:16:33,860 --> 00:16:41,020 can see the information about Zoom's persistent component in the BTM database. Now, it's great that 183 00:16:41,020 --> 00:16:45,200 there's a command line tool, but I want to be able to programmatically do this so I can add this 184 00:16:45,200 --> 00:16:52,200 to my own security tools. So I said, okay, well, SFL tool obviously knows how to grab this 185 00:16:52,200 --> 00:16:57,400 database, parse it. Let me reverse engineer and figure out how it does this. So if we reverse 186 00:16:57,400 --> 00:17:02,280 engineer it, we can see in a nutshell that it sends an XPC message to the background task 187 00:17:02,280 --> 00:17:07,980 management daemon and the daemon will then send the database, its contents back to the SFL tool. 188 00:17:08,120 --> 00:17:13,060 We can see that also in the log messages with the BTM daemon saying, yes, I received an XPC 189 00:17:13,060 --> 00:17:18,660 message to get the contents of this database. So I said, awesome, let me implement this same 190 00:17:18,660 --> 00:17:25,580 code. Let me talk to the BTM daemon, kindly ask and say, hey, I also want the database. So I 191 00:17:25,580 --> 00:17:30,720 mimicked the code of SFL tool, compiled and executed this, and it completely failed. 192 00:17:30,900 --> 00:17:35,620 Basically we can see a log message saying, Patrick, you don't have Apple's private entitlement. 193 00:17:36,040 --> 00:17:41,640 Rude. You can't talk to me. Please go away. And we can confirm this if we reverse engineer the 194 00:17:43,140 --> 00:17:49,420 database. We can see it has code to check who is talking to it. And it will only talk to 195 00:17:49,420 --> 00:17:56,640 processes that are privately entitled, meaning things like SFL tool. I can't get that private 196 00:17:56,640 --> 00:18:02,240 Apple entitlement. I can't spoof it. So BTM daemon is basically going to tell me to take a 197 00:18:02,240 --> 00:18:06,900 hike, whereas FSL tool is going to be allowed to make that request and get the contents of the 198 00:18:06,900 --> 00:18:13,040 database. So fail. But can we just access the database directly? Yeah, as long as we have the 199 00:18:13,040 --> 00:18:19,580 full disk access, we can. So the first thing is that let me dig into this database to figure out 200 00:18:19,580 --> 00:18:25,480 what exactly it is. I ran the file tool on it. It told me it was an Apple binary property list. 201 00:18:25,760 --> 00:18:32,240 We can use the PLU command to convert it to a normal property list. But unfortunately that 202 00:18:32,240 --> 00:18:39,460 doesn't really help because it shows us it's just a NS key archived, which is a object that has a 203 00:18:39,460 --> 00:18:43,020 bunch of other serialized objects. So really the database has a bunch of other serialized objects. 204 00:18:45,440 --> 00:18:54,800 So not the best format. But SFL tool can somehow deserialize those objects and display them to 205 00:18:54,800 --> 00:19:00,260 the user. So if we reverse engineer SFL tool some more and figure out how those objects are 206 00:19:00,860 --> 00:19:06,480 deserialized, in theory we should be able to write our own deserializer and then read the database 207 00:19:06,480 --> 00:19:12,680 directly ourselves and display this information to the user. So if we look at SFL's tool, we find it's 208 00:19:13,040 --> 00:19:19,420 a serialization code which describes the objects that are in this database. So what we can do is we 209 00:19:19,420 --> 00:19:25,240 can essentially reimplement that deserialization ourselves. I'm not going to go through all of 210 00:19:25,240 --> 00:19:30,460 this. But we can see there's things in these serialized objects such as an executable path, 211 00:19:30,800 --> 00:19:37,240 bundle identifier, et cetera, et cetera. We can then, with this deserialization logic 212 00:19:37,240 --> 00:19:43,020 complete, write some code that opens the database, loads it into our own keyed archiver, and then we can 213 00:19:43,040 --> 00:19:50,540 invoke the decode object of class method which will invoke our custom deserializer and 214 00:19:50,540 --> 00:19:56,660 deserialize all of the objects for us. So I push this logic into an open source library called 215 00:19:56,660 --> 00:20:05,960 dump BTM. And with this library, now you can write a five or ten line program that simply can 216 00:20:05,960 --> 00:20:11,220 interact with the database. The library exposes two main methods. The first one is dump BTM. 217 00:20:11,320 --> 00:20:13,020 You can either specify the best way to do that or you can specify the best way to do that. 218 00:20:13,040 --> 00:20:16,280 You can specify the path or it will look it up for you. And this will print it out to standard 219 00:20:16,280 --> 00:20:21,280 out. Or it will load the contents of this database into a dictionary that then you can 220 00:20:21,280 --> 00:20:28,000 programmatically dig into more. So here's an example, the output of our dump BTM tool. This 221 00:20:28,000 --> 00:20:34,320 should look exactly like SFL tool, which was the goal. Another cool thing about it is this 222 00:20:34,320 --> 00:20:39,980 doesn't need root access to run where Apple's SFL tool does. So if we execute this, we can see 223 00:20:39,980 --> 00:20:43,020 we're opening the background task management database. And we can see that this is the 224 00:20:43,040 --> 00:20:47,160 database which contains all the persistent items on the system that we're interested in and 225 00:20:47,160 --> 00:20:51,320 starts printing them out. Again, we can see, for example, the zoom daemon that has installed 226 00:20:51,320 --> 00:20:57,880 itself. So what I then did was I took this library and I implemented it into one of my well-known 227 00:20:57,880 --> 00:21:03,000 security tools called knock knock. Knock knock's goal is to simply tell you who's there, pretty 228 00:21:03,000 --> 00:21:08,320 much saying these are all the persistent items. And with the addition of this new library code, it 229 00:21:08,320 --> 00:21:13,020 can now also query the undocumented BTM database, get all the persistent items from the database 230 00:21:13,040 --> 00:21:19,960 and display that. So on a system where Dazzle Spy has infected via the Safari zero days, we can 231 00:21:19,960 --> 00:21:25,860 now enumerate its persistence and alert the user that their system is infected. Now, there's one 232 00:21:25,860 --> 00:21:31,340 kind of downside to that approach, and that is it's somewhat reactive, right? You're going to get 233 00:21:31,340 --> 00:21:35,900 on a system if you're doing some forensic analysis or seeing if it's infected, and you're going to 234 00:21:35,900 --> 00:21:42,240 now query, and yes, you can get a list of everything that's persisted, but it would be nice to get a 235 00:21:42,240 --> 00:21:47,280 notification in real time when something is persisted. That's a little more proactive. So let's 236 00:21:47,280 --> 00:21:52,600 talk about how to do that now. Now, you might remember that I mentioned that the background task 237 00:21:52,600 --> 00:21:58,740 management daemon has an entitlement related to endpoint security. What is endpoint security? 238 00:21:59,040 --> 00:22:05,380 It's another subsystem in Mac OS that's designed to be designed to interact with third-party 239 00:22:05,380 --> 00:22:10,720 security tools. The idea is the endpoint security system, and we'll dive into this more, basically 240 00:22:10,720 --> 00:22:12,220 monitors all sorts of activities that are going on in the system. So the endpoint security system 241 00:22:12,240 --> 00:22:16,140 is a system where you can monitor activity on the system, and then third-party security clients can 242 00:22:16,140 --> 00:22:22,640 tell it what they are interested in, and when that event occurs, the endpoint security subsystem will 243 00:22:22,640 --> 00:22:27,500 programmatically deliver it to them. So, for example, we will see, we can tell the endpoint 244 00:22:27,500 --> 00:22:32,000 security subsystem, hey, we are interested whenever something persists, and then it will 245 00:22:32,000 --> 00:22:37,460 programmatically call us. So this is really great for a security tool to monitor, for example, 246 00:22:37,560 --> 00:22:42,220 whenever a persistent item is installed, whether that is Zoom or Dazzle Spotlight or whatever. 247 00:22:44,640 --> 00:22:50,940 So what we can do is we can look at the header files for the endpoint security system, and we note 248 00:22:50,940 --> 00:22:56,820 that in Mac OS 13, the most recent version, when BTM was introduced, there are some new 249 00:22:56,820 --> 00:23:01,900 endpoint security events that we can now listen for. And the one we're going to focus on is the 250 00:23:01,900 --> 00:23:09,000 BTM launch item ad. As the name implies, this is a message that will be delivered any time a new 251 00:23:09,000 --> 00:23:12,200 item is persisted. So the idea is, we can look at the header files for the endpoint security system, 252 00:23:12,220 --> 00:23:17,060 and if an installer or a piece of malware comes along and persists itself, if our ES client has 253 00:23:17,060 --> 00:23:21,240 registered for these notifications, Apple will deliver that to us. So you can see if you're 254 00:23:21,240 --> 00:23:25,980 writing a security tool, this is great. You can then perhaps examine that item, alert the user, 255 00:23:26,200 --> 00:23:30,620 generate a log message, et cetera, et cetera. So let's look how to do this programmatically. 256 00:23:31,440 --> 00:23:36,260 This is some standard endpoint security code. This is how you write a client, basically three 257 00:23:36,260 --> 00:23:41,860 steps. You tell it what events are of interest to you. Here, we want to detect when a new 258 00:23:41,860 --> 00:23:47,900 persistent item is added. So we tell it we're interested in the BTM launch item ad event. We 259 00:23:47,900 --> 00:23:52,920 create a callback that will be invoked when this event is delivered to us. And then we 260 00:23:52,920 --> 00:23:59,040 subscribe, which kicks off basically our listener. Now, whenever something persists, the 261 00:23:59,040 --> 00:24:05,380 operating system will call us because we said, hey, please let us know. And here are some 262 00:24:05,380 --> 00:24:11,840 additional information about the contents of that message. So we can see things like, you know, 263 00:24:11,860 --> 00:24:18,500 the item, the executable, the path to its property list. Again, these are the components of the 264 00:24:18,500 --> 00:24:25,700 message that are going to be delivered to us. So now, if we add this code and compile and 265 00:24:25,700 --> 00:24:31,620 execute, we are now listening essentially for persistence events. If Dazzle Spy comes along and 266 00:24:31,620 --> 00:24:38,140 infects this system, we can now see that we get a notification from the operating system, from 267 00:24:38,140 --> 00:24:41,840 the endpoint security subsystem, telling us, hey, just to let you know, we're going to get a notification 268 00:24:43,100 --> 00:24:48,120 from Dazzle Spy. So here is the path to its executable and also the path to its property 269 00:24:48,120 --> 00:24:52,620 list. This is great because if we look at that, first we can see it's an executable software 270 00:24:52,620 --> 00:24:59,660 updater that's running from a hidden directory. Huge red flag. Its property list is also called 271 00:24:59,660 --> 00:25:06,000 com.apple.software, but if we look at the binary software update, it's not signed by Apple. So 272 00:25:06,000 --> 00:25:11,840 another huge red flag. So this is an example of now how we can generically detect even the 273 00:25:11,860 --> 00:25:17,200 brand-new malware that's being deployed via zero days simply by monitoring for persistence 274 00:25:17,200 --> 00:25:23,340 activities. Now, one problematic issue, the other endpoint security event related to 275 00:25:24,220 --> 00:25:30,120 persistence events, the remove event is broken. You can register for it but when a persistent 276 00:25:30,120 --> 00:25:35,560 item is removed, it's just never delivered. So Apple, please fix. And speaking of broken, the 277 00:25:35,560 --> 00:25:41,540 BTM launch ad event was actually horribly broken to begin with in two ways. First, whenever an 278 00:25:41,540 --> 00:25:45,940 an item was persisted, the operating system would just call you with all the items that were 279 00:25:45,940 --> 00:25:51,800 persisted, which is totally unhelpful. I'm only interested in the new items, not the 25 other 280 00:25:51,800 --> 00:25:57,720 items that are already on the system. And also if you persisted as a login item, which is 281 00:25:57,720 --> 00:26:03,120 another persistence mechanism, you wouldn't get a notification at all. So I sent a bug report to 282 00:26:03,120 --> 00:26:07,540 Apple and I said, hey, yo, this is like all broken. They took their happy time, but eventually 283 00:26:07,540 --> 00:26:16,120 in Mac OS 10, 13.3, they fixed this. Okay. So now we've talked about the internals of the BTS 284 00:26:16,120 --> 00:26:21,680 subsystem. We've showed how we can write some pretty powerful detection tools that allow us to 285 00:26:21,680 --> 00:26:26,240 detect malware when it persists. So this is great for defenders. But as I mentioned, DEF CON, 286 00:26:26,440 --> 00:26:30,860 like, it's a hacker conference, so we need to, like, put on our black hats and talk about now 287 00:26:30,860 --> 00:26:36,540 how to break this subsystem. And the reason we want to break this subsystem as hackers are 288 00:26:36,540 --> 00:26:37,520 twofold. We want to break this subsystem as hackers are twofold. We want to break this subsystem as 289 00:26:37,540 --> 00:26:42,200 hackers are twofold. First, let's put ourselves in the shoes of a hacker. We come along, we 290 00:26:42,200 --> 00:26:48,200 infect a Mac system with our zero days, all well and good. We go to persist. Two things now happen 291 00:26:48,200 --> 00:26:54,000 because of BTM. First, a big system alert is generated and displayed to the user saying, hey, 292 00:26:54,160 --> 00:26:58,220 something just persisted. And, like, if you're browsing Safari and all of a sudden this pops up, 293 00:26:58,320 --> 00:27:03,380 like, yeah, you're probably owned. And then also, as we showed, this will generate an endpoint 294 00:27:03,380 --> 00:27:07,520 security message, which means that if any security system, you know, if you're using a security 295 00:27:07,520 --> 00:27:12,940 tool on your Mac, any security tool, it will pick up this event and also be like, yo, strange, 296 00:27:13,260 --> 00:27:19,660 you've essentially been hacked. So as hackers, we want to bypass both the security alert and the 297 00:27:19,660 --> 00:27:25,700 endpoint security event. Two things to point out. These are both separate, so we have to find 298 00:27:25,700 --> 00:27:32,200 attacks and bypasses for both of them. But the good news is, both of them are independent on the 299 00:27:32,200 --> 00:27:37,500 functionality of the persistent item. Meaning, regardless of what happens to the alert, the 300 00:27:38,380 --> 00:27:43,480 security event, they're both notifications. So either way, the item will be successfully 301 00:27:43,480 --> 00:27:47,920 persisted. It's not like the operating system is waiting for the user to click on the event and say 302 00:27:47,920 --> 00:27:52,140 allow. So this is good. This means if we can suppress these messages or somehow prevent them 303 00:27:52,140 --> 00:27:57,180 from being delivered, we're great. Our malware will be able to persist invisibly and still 304 00:27:57,180 --> 00:28:02,600 function. So one thing you can do, and this is not really a bypass per se, it's more just a 305 00:28:02,600 --> 00:28:07,500 limitation of BTM, is simply persist in a way that is outside of the system. So if you have a 306 00:28:07,520 --> 00:28:13,300 launch daemon, launch agent, or login items, which are the three persistent mechanisms that 307 00:28:13,300 --> 00:28:18,100 BTM currently monitors. These are the most popular ones, but there are a myriad of other ways. 308 00:28:18,300 --> 00:28:23,200 For example, you can persist as a cron job, and currently BTM does not monitor cron jobs. 309 00:28:23,560 --> 00:28:29,900 There's also a great blog post by Chaba who basically shows that there's 20 or so more 310 00:28:29,900 --> 00:28:34,900 esoteric and stealthy ways to persist. So if you persist your malware in any of those ways, again, 311 00:28:34,940 --> 00:28:37,500 as long as you're not a launch daemon, a launch agent, or a launch agent, you're going to be able to 312 00:28:37,520 --> 00:28:45,120 log an item. BTM doesn't even come into play. But I wanted to directly bypass that system alert. 313 00:28:45,300 --> 00:28:52,080 So method one is hilariously simple. If we go back to that SFL tool and look at what it supports, 314 00:28:52,300 --> 00:29:00,340 one of its commands is the reset BTM database. And the idea is if your database gets into a funky 315 00:29:00,340 --> 00:29:07,140 state, you can reset it. The operating system will then just rescan all the launch daemon agent and 316 00:29:07,140 --> 00:29:12,900 log in items directory and rebuild the database. So that's great. The problem is when you execute 317 00:29:12,900 --> 00:29:19,040 that, it puts the background task management daemon in like a funky confused state. So if we look at 318 00:29:19,040 --> 00:29:24,660 the log messages, what we can first do is reset the database and then persist an item. And if you 319 00:29:24,660 --> 00:29:31,320 look at the log messages, you can see that should post advisory is now set to false. I don't know 320 00:29:31,320 --> 00:29:37,040 exactly why, but I know that if you reset the database, this will always be the case. So what 321 00:29:37,140 --> 00:29:42,780 a hacker can now do, granted with root privileges, is just reset the database, persist their item. 322 00:29:42,940 --> 00:29:48,480 Their item will be successfully persisted and restart every time the system is rebooted. But the 323 00:29:48,480 --> 00:29:53,320 alert will never be delivered. And again, they're simply using Apple's own tool. So this isn't 324 00:29:53,320 --> 00:29:57,900 some crazy like keep overflow. You're basically just saying, hey, please reset the database. And 325 00:29:57,900 --> 00:30:03,120 the system is like, cool. And then you can persist silently. So that's awesome. But the problem 326 00:30:03,120 --> 00:30:07,120 with that is you need root access. So I said, okay, let's do one thing. Let's do root access. 327 00:30:07,140 --> 00:30:13,920 One better. Remember, the BTM daemon will send an XPC notification to the BTM agent and say, 328 00:30:14,020 --> 00:30:19,780 please display a notification to the user. What privilege level is the agent running at? User. 329 00:30:19,980 --> 00:30:25,920 And this is because it needs to display a message on the user's desktop session. So I said, can 330 00:30:25,920 --> 00:30:32,840 we just send the BTM agent a stop signal? Like, yo, go to sleep for a minute? What happens? 331 00:30:33,140 --> 00:30:37,120 Well, it turns out you can do this. The BTM daemon will still send a notification to the user. 332 00:30:37,120 --> 00:30:42,700 It will deliver the message to the BTM agent and believe that was successfully delivered. But 333 00:30:42,700 --> 00:30:47,520 since the agent is sleeping, it's not really going to get that message. And so it will never be 334 00:30:47,520 --> 00:30:52,560 displayed. So what a hacker can do is get the process ID of the BTM agent, which, again, is 335 00:30:52,560 --> 00:30:58,240 running as user, so lowest privilege level. Send it a stop signal to put it to sleep. Go to 336 00:30:58,240 --> 00:31:03,260 persist. BTM daemon will try to deliver the message and think it succeeded. But the agent is 337 00:31:03,260 --> 00:31:07,100 sleeping. And then once they have persisted, kill the agent. And then once they have persisted, kill the 338 00:31:07,100 --> 00:31:10,880 agent, which, again, you can do because it's running with your same privilege level. You need to 339 00:31:10,880 --> 00:31:15,300 kill the agent because otherwise when you wake it up, the alert will have been queued and will be 340 00:31:15,300 --> 00:31:21,100 displayed. But if you do this, you will now be able to persist and the alert to the user will 341 00:31:21,100 --> 00:31:26,880 never be displayed. Now, this is well and good. I'm like, cool. Like, these techniques work on 342 00:31:26,880 --> 00:31:33,420 the latest version of Mac OS, so hackers take note. I guess Apple take note, too. But, you know, 343 00:31:33,460 --> 00:31:37,080 I'm like, cool. Users might ignore that alert anyways. But, you know, it's not going to work. 344 00:31:37,100 --> 00:31:42,580 What I'm really more interested in is preventing the endpoint security message because security 345 00:31:42,580 --> 00:31:48,700 tools, including my own, listen for this method. You know, this is how Apple says register for 346 00:31:48,700 --> 00:31:52,360 persistence notification. And you can imagine security tools, you know, are not going to 347 00:31:52,360 --> 00:31:55,700 necessarily ignore this. A user might ignore an alert. A security tool, if it gets a 348 00:31:55,700 --> 00:32:00,740 persistence event, is going to examine that very closely. So, again, as adversaries, hackers, we 349 00:32:00,740 --> 00:32:04,700 want to figure out how to prevent that message from being delivered. And it's a little harder 350 00:32:04,700 --> 00:32:09,800 because this is all being delivered in the root session. So, I went back to the BTM daemon and 351 00:32:09,800 --> 00:32:15,760 started looking around at how it submits events to the endpoint security system when it detects 352 00:32:15,760 --> 00:32:20,940 something has persisted. And unfortunately, if we follow the call chain, we can see it calls into 353 00:32:20,940 --> 00:32:25,740 the kernel, which means we're going to have to pop into kernel. However, right below that kernel 354 00:32:25,740 --> 00:32:32,140 call, we can see a log message that says failed to submit event. So, I'm like, okay, well, it 355 00:32:32,140 --> 00:32:34,680 looks like there's a failure scenario. I have no idea what that is. So, I'm going to go ahead and 356 00:32:34,700 --> 00:32:38,440 figure out what that is. But if we can figure out what that failure scenario is and trigger 357 00:32:38,440 --> 00:32:42,760 that, we might be able to prevent the endpoint security message from being delivered to any 358 00:32:42,760 --> 00:32:48,280 security tool on the system, which is the goal. So, we pop into the kernel and there's kind of a 359 00:32:48,280 --> 00:32:53,320 lot going on in the slide, kind of follow the black arrows. We can basically see that in the 360 00:32:53,320 --> 00:32:58,820 endpoint security kernel extension, so this is running in kernel mode, we find a bunch of 361 00:32:58,820 --> 00:33:04,560 methods. One is called PDB read audit token. This reads a process ID from a user 362 00:33:04,560 --> 00:33:10,900 mode buffer and then calls another method, create from PID version. And this invokes the 363 00:33:10,900 --> 00:33:17,440 proc find API given a PID. This is going to be important. Then it calls another method to get 364 00:33:17,440 --> 00:33:23,280 the executable V node. This is kind of like the file system V node of the process it's just 365 00:33:23,280 --> 00:33:28,840 looked up. So, basically, it's trying to get a path to a process. What process? Well, if we set 366 00:33:28,840 --> 00:33:33,620 a break point in kernel mode and install something, for example, oversight, which is one of my 367 00:33:33,620 --> 00:33:40,520 utilities that installs a launch, a log in item, we can see that the process that it's looking up 368 00:33:40,520 --> 00:33:46,500 is the responsible process that has persisted the item. And we can confirm that if we look at the 369 00:33:46,500 --> 00:33:51,080 endpoint security message in user mode, we can see that the PID and the process match. So, 370 00:33:51,140 --> 00:33:56,640 again, what the endpoint security kernel extension is trying to do is look up the process that is 371 00:33:56,640 --> 00:34:01,680 responsible for generating the persistent event. The installer, the malware, et cetera, et 372 00:34:01,680 --> 00:34:03,600 cetera. This makes sense because they want to do the same thing over and over again. They want to 373 00:34:03,620 --> 00:34:09,480 deliver that to the security tool in user mode. So, I looked at that a little more and I 374 00:34:09,480 --> 00:34:14,460 thought, okay, what happens if the responsible process is, like, gone? What if it executes really 375 00:34:14,460 --> 00:34:21,380 quickly, right? This is kind of happening separately. So, maybe it will get confused. And so 376 00:34:21,380 --> 00:34:26,020 what happens, we can see in this case, they call proc find with null. They don't have a PID 377 00:34:26,020 --> 00:34:33,060 because the process that persisted, at least I thought, was gone. Now, if you call proc find with 378 00:34:33,620 --> 00:34:39,500 null, which is also zero, you get back a process object, but it's the one belonging to the kernel 379 00:34:39,500 --> 00:34:44,780 task. Because the kernel task is assigned PID zero. It's not really a process, per se, but this is 380 00:34:44,780 --> 00:34:50,000 just how it works on Mac OS. And that's okay. I mean, they kind of have the wrong process at this 381 00:34:50,000 --> 00:34:55,300 point. But it's problematic then when they call proc get executable V node because the kernel 382 00:34:55,300 --> 00:35:00,520 doesn't have an executable V node. It's not a running process backed by a binary, per se. So, 383 00:35:00,560 --> 00:35:03,600 that is going to return null, which causes them to return null, which causes them to return null. 384 00:35:05,080 --> 00:35:11,880 And if we go back to the log messages in user mode, we can see a log message that says, fail to 385 00:35:11,880 --> 00:35:18,480 submit ES sys call, error three, no such process. So, I was like, okay, I think I can trigger this, 386 00:35:18,600 --> 00:35:23,900 but how? Like, how do I develop a proof of concept? And I thought about it, and I said, oh, 387 00:35:23,900 --> 00:35:30,900 wait a minute, my own tool block block uses the CP command to install a property list. It copies 388 00:35:30,900 --> 00:35:33,600 that. And that's a very quick command that exits. So, I'm going to go back and I'm going to 389 00:35:33,620 --> 00:35:37,900 look at this. So, I was like, is this going to trigger this kernel bug and bypass that? So, let's 390 00:35:37,900 --> 00:35:42,920 do a demo. I haven't prayed to the demo god. So, fingers crossed this all worked. But go big or go 391 00:35:42,920 --> 00:35:45,880 home. The first thing we're going to do, I'm going to make this a little bigger, is we're 392 00:35:45,880 --> 00:35:52,380 going to execute Apple's ES logger command. And this is going to ‑‑ this is a utility, an 393 00:35:52,380 --> 00:35:56,120 endpoint security client that allows you to listen for endpoint security events. And we're 394 00:35:56,120 --> 00:36:03,400 saying, please listen for the BTM launch add item. So, in theory, if an item is persisted, we 395 00:36:03,400 --> 00:36:09,520 should get notification. We're then going to have some log messages running. This is just 396 00:36:09,520 --> 00:36:13,700 going to show us the error message. And then we're going to install block block. Fingers 397 00:36:13,700 --> 00:36:21,760 crossed. Put in my password. This is definitely going to work. Now, we do get an alert, and 398 00:36:21,760 --> 00:36:25,780 we'll address that in a second. These alerts are, again, separate from the endpoint security 399 00:36:26,800 --> 00:36:31,620 subsystem. If we go over here, though, we see there's no message. There's supposed to be a 400 00:36:31,620 --> 00:36:33,380 message. Why isn't there a message? It's because there's no message. There's no message. 401 00:36:33,400 --> 00:36:38,440 Well, if we go back to here and look for lib endpoint, we can see that we have successfully 402 00:36:38,440 --> 00:36:46,100 triggered this kernel bug, failed to submit event, sys call failed, error, no such process. And 403 00:36:46,100 --> 00:36:51,160 this is because block block, again, is a script that executes shell commands, which start and 404 00:36:51,160 --> 00:36:56,200 stop very quickly. So the kernel doesn't have time to look that up or isn't tracking those 405 00:36:56,200 --> 00:37:00,960 separately. When it goes to look that up, that process is already gone. So it doesn't have a 406 00:37:00,960 --> 00:37:03,380 PID, and so it errors out. So, basically, that's the way we're going to be doing this. So, 407 00:37:03,400 --> 00:37:10,580 basically, all you have to do to bypass the security events is to do that. Okay. I guess we 408 00:37:10,580 --> 00:37:16,060 have some time. Let's show how we can block these messages as well. Yeah. Awesome. Thank 409 00:37:16,060 --> 00:37:25,660 you. So I'm going to uninstall block block, click through this, just to kind of reset everything, 410 00:37:26,000 --> 00:37:29,900 uninstall, and we're going to run it again, and we're going to this time try to hide those 411 00:37:29,900 --> 00:37:33,380 alerts. So I remember I told you the, you know, the, you know, the, you know, the, you know, the 412 00:37:33,400 --> 00:37:39,440 launch agent, the background, the BTM agent is responsible for displaying those alerts. And so 413 00:37:39,440 --> 00:37:44,760 what we can do is just tell it to go to sleep. So let's do that exactly. So what we're going to do 414 00:37:44,760 --> 00:37:48,180 first over here, I'll make this a little bigger, is we're just going to get the process ID of the 415 00:37:48,180 --> 00:37:54,840 background task management agent. We can see it has a PID of 86276. It's also running as me as 416 00:37:54,840 --> 00:37:59,360 the normal user. So what we can do is we can do this programmatically, or we can just do it via 417 00:37:59,360 --> 00:38:03,380 the kill. We're going to send it the kill stop command, which is going to say, hey, I'm going to 418 00:38:03,380 --> 00:38:09,600 go to sleep. And we can confirm that if we do the process listing again, it now has a T here. T 419 00:38:09,600 --> 00:38:16,780 means that item is now suspended. If we, and I'm going to reset this because the uninstaller for 420 00:38:16,780 --> 00:38:20,660 block block installs something persistently, but does it programmatically, so this generates an 421 00:38:20,660 --> 00:38:25,420 event, this just proves that normally we should get an event. So I'm going to reset this to show 422 00:38:25,420 --> 00:38:30,700 that the installer doesn't. We're now going to run block block again, and the hope is we, A, 423 00:38:30,740 --> 00:38:33,360 don't get a security event, and, B, not get a security event. So we're going to run block block 424 00:38:33,380 --> 00:38:41,060 again. No, no alert is shown. So fingers crossed. Live demo number two. Block block is going to 425 00:38:41,060 --> 00:38:47,320 install. Hooray. Happy. We're going to get an alert from block block saying I'm up and running. 426 00:38:47,640 --> 00:38:58,000 But no alerts from the endpoint security system. And no alerts from the subsystem. So now 427 00:38:58,000 --> 00:39:03,100 what we would do is we would kill the agent to make sure that those messages are not delivered. 428 00:39:03,380 --> 00:39:07,460 What I'm going to do instead is resume it to show you that if we didn't do those, those would 429 00:39:07,460 --> 00:39:12,160 actually be there. So, again, normally a hacker wouldn't, like, continue the agent. They would 430 00:39:12,160 --> 00:39:16,440 kill it at this point, which is, again, because it's running with the same privileges. But if we 431 00:39:16,440 --> 00:39:21,200 continue it, we will see those are the messages. So they were delivered, but because the agent was 432 00:39:21,200 --> 00:39:26,060 sleeping, they weren't actually shown, and if we kill it, they will never be displayed. So what 433 00:39:26,060 --> 00:39:32,600 we have done is fully bypassed background task management, which is, I think, kind of problematic. 434 00:39:33,380 --> 00:39:38,820 And why is this problematic? Let me just clear these annoying alerts. It's really problematic 435 00:39:38,820 --> 00:39:43,580 because this is a security mechanism that Apple has basically told us will protect our Macs, but in 436 00:39:43,580 --> 00:39:49,160 reality, it's implemented incredibly poorly and is trivial to bypass. And this is problematic 437 00:39:49,160 --> 00:39:53,560 because users might now be overconfident in the security of their system because Apple has 438 00:39:53,560 --> 00:39:58,580 promised them this security feature. And security tool vendors might also be leveraging this to 439 00:39:58,580 --> 00:40:03,360 detect persistence, believing it works, when in reality, it's broken. It's not working. So, 440 00:40:04,120 --> 00:40:10,760 that, in my opinion, is problematic. So let's end kind of on a more positive note and show how we 441 00:40:10,760 --> 00:40:15,560 can detect these bypasses until Apple gets their act together and actually starts shipping, well, 442 00:40:15,640 --> 00:40:20,640 secure implementations. So the first thing we want to do is, how do we detect if someone 443 00:40:20,640 --> 00:40:27,380 resets the SFL or the BTM database? This is not something that should ever normally happen, 444 00:40:27,520 --> 00:40:32,240 right? It's kind of an undocumented database. No one really knows about the SFL tool. So if you 445 00:40:33,380 --> 00:40:37,820 are setting the database, it's like probably a hacker trying to leverage one of these bypasses. So 446 00:40:37,820 --> 00:40:43,580 what we can do is we can write a process monitor that can detect when this process is spawned and 447 00:40:43,580 --> 00:40:49,660 also look at the arguments. We can write a process monitor that's either passive, that can say, 448 00:40:49,740 --> 00:40:54,480 hey, this happened, or using other endpoint security events, you can actually receive a 449 00:40:54,480 --> 00:41:02,460 notification when a process is about to be spawned. And if it's the SFL tool with the dump BTM 450 00:41:02,460 --> 00:41:08,660 command, you can actually prevent, sorry, with the reset database command, you can actually 451 00:41:08,660 --> 00:41:13,240 block that all together. So this is a way to essentially prevent that command from being run so 452 00:41:13,240 --> 00:41:20,260 the database cannot be reset. What about telling the BTM agent to go to sleep? Well, it turns 453 00:41:20,260 --> 00:41:27,600 out we can also use the endpoint security system to register for signals. Specifically, we can 454 00:41:27,600 --> 00:41:32,440 use the event type off signal, which will be delivered to us any time we need it. So we can 455 00:41:33,340 --> 00:41:41,480 send a signal to another process. What we can then do is we can examine both the signal and the 456 00:41:41,480 --> 00:41:47,180 source and destination process. And if the destination process is the background task 457 00:41:47,180 --> 00:41:53,200 management agent and the signal that is about to be sent to it is a stop event to put it to 458 00:41:53,200 --> 00:41:59,420 sleep, we can tell the operating system, do not allow that to happen. So if we implement this 459 00:41:59,420 --> 00:42:02,440 code and are running this in our security tool, you can tell the operating system, do not allow 460 00:42:02,460 --> 00:42:06,880 that to happen. You can now see that when the attacker tries to send that sig stop message to 461 00:42:06,880 --> 00:42:12,760 tell the BTM agent to go to sleep, our security tool will deny that and that will actually fail. 462 00:42:12,940 --> 00:42:18,100 So now we've prevented the agent from going to sleep. Again, these are things like Apple should 463 00:42:18,100 --> 00:42:26,400 be doing. It's like lame that we have to do this, but it is what it is, right? In terms of getting 464 00:42:26,400 --> 00:42:31,980 the endpoint security notification for persistence, this one is a little tougher because it's a kernel 465 00:42:31,980 --> 00:42:38,380 flaw. So kind of hard for us to detect. Well, good news is there are other endpoint security 466 00:42:38,380 --> 00:42:46,180 alerts or messages that will also be delivered that are not susceptible to this bug. For example, 467 00:42:46,320 --> 00:42:53,420 when a piece of malware writes a property list to the launch daemon directory to persist, yes, 468 00:42:53,440 --> 00:42:57,880 in theory, this is supposed to generate a BTM launch item ad and will in some cases, but we 469 00:42:57,880 --> 00:43:01,960 showed how to trivially bypass this, this will always also generate a B T M launch item ad and will in some cases, but we showed how to trivially bypass this. This will always also 470 00:43:01,980 --> 00:43:31,960 generate file notifications. For example, notify create, notify write, that will be delivered to us and are not susceptible to the kernel flaw, which allows us then to say, hey, something was written to those directories. So what we can do, and this is how block block detects persistence is, it doesn't use the BTM launch item ad because it's broken currently. I wish it worked. It would be really nice. But instead, it uses the file events to monitor the locations where items can persist. As I mentioned, those events are not necessarily 471 00:43:31,980 --> 00:43:53,920 delivered accurately and correctly. So it listens for those events. If it turns out it's in one of those directories, it then alerts the user. The other thing is, unlike Apple's approach, it has the ability to block that so you can actually take some action against that versus just clicking on an alert that's like, yeah, cool. Okay. So let's wrap this all up with some 472 00:43:53,920 --> 00:44:01,960 takeaways. So we talked about understanding Mac OS's BTM subsystem. I think it was important to understand that because we want to write tools and 473 00:44:01,960 --> 00:44:07,020 and also understand limitations. Once we had an understanding of the internals of the background 474 00:44:07,020 --> 00:44:13,340 task management, we showed how to write some very powerful malware detection tools that are built 475 00:44:13,340 --> 00:44:18,760 on the fact that Mac malware persists and BTM now manages and governs persistence. 476 00:44:19,540 --> 00:44:25,880 Unfortunately we showed that it's trivial to bypass the BTM subsystem entirely. Again, all 477 00:44:25,880 --> 00:44:31,440 these approaches worked on all the latest versions of Mac OS. So go out and cause chaos I 478 00:44:31,440 --> 00:44:37,780 guess. No doubt. But one of the other takeaways is, like, if we're not doing this research, who 479 00:44:37,780 --> 00:44:43,500 is? What is Apple doing? Counting their trillions in Cupertino? I don't know. But it's really 480 00:44:43,500 --> 00:44:48,760 problematic when they say, hey, there's this great new security system that's built into Mac OS. 481 00:44:49,060 --> 00:44:56,640 You now are protected against persistent malware. In reality, that's really not true. And again, as 482 00:44:56,640 --> 00:45:00,240 I mentioned, that's problematic because users are going to have a false sense of security, which 483 00:45:00,240 --> 00:45:01,420 is bad. Makes sense. But we're not doing it. We're doing it because we're going to have to 484 00:45:01,420 --> 00:45:05,100 make the system easier to victimize. And then also security tools might be thinking they're 485 00:45:05,100 --> 00:45:11,300 doing their jobs when in reality they're not at no fault of their own. So again, maybe anything 486 00:45:11,300 --> 00:45:16,320 Apple says, take with a huge grain of salt and continue to keep poking on Mac OS because even 487 00:45:16,320 --> 00:45:22,600 new code that Apple releases is, well, I would say subpar. Now if you're interested in learning 488 00:45:22,600 --> 00:45:26,160 more about Mac malware, reverse engineering, writing security tools, I've written some free 489 00:45:26,160 --> 00:45:31,400 books on the topic. Volume 1 is out. Go to TAOMM.org. 100% free online. You can download it 490 00:45:31,420 --> 00:45:35,260 online. I'm also going to be doing a book signing today right after this talk at the No Starch Press. 491 00:45:35,400 --> 00:45:40,340 So if you want to pick up Volume 1, get my signature, check it out. I'm also writing Volume 2, 492 00:45:40,440 --> 00:45:45,760 which covers basically the topics we talked about today, which are the programmatic approaches to 493 00:45:45,760 --> 00:45:52,920 detecting malware. I'll end by just mentioning briefly again the nonprofit I'm part of because I 494 00:45:52,920 --> 00:45:56,980 think we're doing a lot of really cool things that I think many of you will be interested in. We 495 00:45:56,980 --> 00:46:01,280 organized the OBTS Security Conference. I see a lot of friends here from that conference. So thank 496 00:46:01,280 --> 00:46:06,480 you all. We also raised money for college scholarships for students who are going into 497 00:46:06,480 --> 00:46:12,500 technology fields, and we have various diversity programs as well. Our current effort, though, is 498 00:46:12,500 --> 00:46:17,360 raising money for the wildfires in Maui. Many of you know I live there. A lot of my friends had 499 00:46:18,380 --> 00:46:25,260 everything lost, so we are working to raise money for that. So something very personal. If you feel 500 00:46:25,720 --> 00:46:29,800 like you want to support that, it's awesome, or even just share this. It's something that would be 501 00:46:29,800 --> 00:46:31,260 very meaningful to you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank 502 00:46:31,260 --> 00:46:36,980 you. to those who lost their houses and their loved ones. So speaking of thank you, I also want to 503 00:46:36,980 --> 00:46:43,300 briefly just end by thanking the companies who support the nonprofit. We're able to organize this 504 00:46:43,300 --> 00:46:47,420 great conference. I'm able to release these tools, these free books, give these talks, thanks 505 00:46:47,420 --> 00:46:53,260 to them. Really couldn't do that without them. So I briefly want to thank them all. Also thank you 506 00:46:53,260 --> 00:46:58,640 all for spending this rather early Saturday morning with me talking nerdy about MacOS stuff. 507 00:46:58,800 --> 00:46:59,960 Thank you again to you all. 508 00:47:01,260 --> 00:47:01,860 Thank you.