1 00:00:00,000 --> 00:00:00,640 All right. 2 00:00:00,660 --> 00:00:06,100 Aloha and welcome to my talk on mastering Apple's endpoint security. 3 00:00:06,180 --> 00:00:10,180 So today we're going to talk about all things related to Apple's endpoint security. 4 00:00:10,180 --> 00:00:20,360 We are going to start with some basics but very quickly we will then move into some more advanced topics as well as cover some nuances and pitfalls. 5 00:00:20,360 --> 00:00:26,760 Really shed some light into what I think are the darker corners of endpoint security. 6 00:00:27,160 --> 00:00:28,960 Super stoked you're all here. 7 00:00:28,960 --> 00:00:41,380 I imagine those of you who are defenders, malware analysts, perhaps those writing security software, the content of this talk, it's relevance should be readily apparent. 8 00:00:41,580 --> 00:00:50,560 But if you are a attacker, a hacker, a malware author and are targeting Mac OS, cool, it's Def Con, all are welcome. 9 00:00:50,560 --> 00:01:00,760 I think this talk will be very relevant to you as well because as we'll see, endpoint security really provides defenders the tools to detect even the most advanced malware. 10 00:01:00,760 --> 00:01:11,480 So again, if you're a hacker , this might cause you some consternation so having a in -depth understanding of what the defenders have in their arsenal I think is very relevant. 11 00:01:12,000 --> 00:01:13,360 My name is Patrick Wardle . 12 00:01:13,360 --> 00:01:17,060 I am the founder of the Objective-C Foundation. 13 00:01:17,380 --> 00:01:29,340 More recently in parallel, I am also the co-founder of W where we are building core Mac OS detection components to integrate into larger enterprise security products. 14 00:01:29,900 --> 00:01:34,180 I'm also the author of the Art of Mac Malware book series. 15 00:01:34,180 --> 00:01:42,200 I mention this here because in volume 2 there are 2 full chapters dedicated solely to endpoint security. 16 00:01:42,200 --> 00:01:49,960 So if you find this talk interesting and you want to dig more into the content of what we're covering today, I recommend reading that. 17 00:01:49,960 --> 00:01:54,940 And this is not a sales pitch because good news is this book is 100% free online. 18 00:01:56,800 --> 00:01:59,020 So let's start with some basics. 19 00:01:59,020 --> 00:02:09,780 Those of you who perhaps have dabbled in endpoint security, this might be a little bit of review but it's important for us to all be on the same page before we delve into more advanced topics. 20 00:02:10,300 --> 00:02:12,320 So what is endpoint security? 21 00:02:12,320 --> 00:02:14,900 Apple has a fairly succinct definition. 22 00:02:14,900 --> 00:02:21,540 They say it's a C API for monitoring system events for potentially malicious activity. 23 00:02:21,780 --> 00:02:31,800 For me, what I love about endpoint security is really for the first time it's a framework that Apple has released specifically for third party security tools. 24 00:02:31,800 --> 00:02:35,640 And I cannot stress the importance of this enough. 25 00:02:35,700 --> 00:02:44,880 Some of you I look around, you look to be perhaps my age or a little bit older so some of you might have been writing Mac security tools before endpoint security was released. 26 00:02:44,880 --> 00:02:49,800 And you know that sucked because there were no APIs dedicated to security tools. 27 00:02:49,800 --> 00:02:54,300 Something like creating a simple process monitor was very very difficult. 28 00:02:54,300 --> 00:02:56,400 Now though with endpoint security it is a breeze . 29 00:02:56,400 --> 00:02:58,920 So thank you to Apple for releasing this. 30 00:02:58,920 --> 00:03:02,720 Myself and other tool developers we are forever indebted to you. 31 00:03:02,720 --> 00:03:10,180 While I am heaping some praise on Apple, which is maybe a little bit rare for me, I also want to give a shout out to them for their documentation . 32 00:03:10,400 --> 00:03:14,920 Unlike some of their other frameworks, endpoint security is actually fairly well documented. 33 00:03:14,920 --> 00:03:19,900 If you go to their developer site, there's a whole section on that, even sample code. 34 00:03:19,900 --> 00:03:29,500 They also ship a neat utility, ES logger, that's built into Mac OS that allows you to explore endpoint security directly from the terminal. 35 00:03:30,220 --> 00:03:42,840 Now though, if you talk to anyone that really does any in-depth tool development using endpoint security, they will say, okay, ignore all the online documentation, go to the header files. 36 00:03:42,840 --> 00:03:46,820 And so I want to instill that wisdom upon you as well. 37 00:03:46,820 --> 00:03:51,740 So there's 3 main header files, these will be found in the SDK directory under endpoint security. 38 00:03:51,740 --> 00:03:55,740 You have ES client, ES message, and ES types. 39 00:03:56,460 --> 00:04:00,080 As we can see on the slide, these are not normal header files. 40 00:04:00,080 --> 00:04:07,000 They include, for example, ASCII flow charts describing very complicated parts of endpoint security. 41 00:04:07,080 --> 00:04:13,660 So again, thank you to whoever the Apple developers who poured their heart and soul into these header files, super indebted to them as well. 42 00:04:14,840 --> 00:04:22,680 Another takeaway or point I want to make early on is if you are developing security tools for Mac OS, you should be using endpoint security. 43 00:04:22,680 --> 00:04:29,100 Not only is it recommended and supported by Apple, and anytime Apple recommends and supports something, use it. 44 00:04:29,100 --> 00:04:34,240 I've been on the other end of that trying to do something a little different, especially before endpoint security. 45 00:04:34,240 --> 00:04:41,080 That always figuratively bit me in the butt when Apple would change something, for example, when a new version of Mac OS came out. 46 00:04:41,560 --> 00:04:47,320 Also Apple continues to invest and improve endpoint security, so it just gets better and better. 47 00:04:47,680 --> 00:05:00,660 And then finally, as we will see, endpoint security is insanely powerful, giving us unparalleled insight into activity on the operating system, which means even the most sophisticated malware will be observable. 48 00:05:00,660 --> 00:05:05,460 So again, if we use endpoint security, it really, as defenders, gives us a great leg up. 49 00:05:07,340 --> 00:05:10,420 Let's now talk about writing an endpoint security tool. 50 00:05:10,440 --> 00:05:14,340 Conceptually, it's actually pretty straightforward in two basic steps. 51 00:05:14,340 --> 00:05:19,960 Subsequent slides will show code how to do this, but for now let's talk about how to do it conceptually. 52 00:05:20,060 --> 00:05:25,300 So the first thing you're going to do is going to be to create an endpoint security client. 53 00:05:25,320 --> 00:05:31,160 This is important because all subsequent endpoint security API calls need this client. 54 00:05:31,160 --> 00:05:32,400 You basically pass it around. 55 00:05:32,400 --> 00:05:34,340 So step one is to create this client. 56 00:05:34,460 --> 00:05:44,720 We'll see that you create this client with a handler block, and I will repeat this several times, that this handler block receives the events of interest that you subscribe for. 57 00:05:44,720 --> 00:05:46,700 We'll get into that a little bit more. 58 00:05:47,080 --> 00:05:54,460 Once you have initialized, created a endpoint security client, you specify and subscribe to events of interest. 59 00:05:54,800 --> 00:05:59,400 So imagine we're interested in tracking process spawns . 60 00:05:59,400 --> 00:06:03,060 We want to get notified any time a process spawns anywhere on the system. 61 00:06:03,060 --> 00:06:06,120 For example, to check if it's malware, something it's trusted. 62 00:06:06,120 --> 00:06:09,320 We can do that with the notify exec event. 63 00:06:09,320 --> 00:06:11,920 We'll talk more about events very shortly. 64 00:06:12,140 --> 00:06:31,400 But the idea is once we've created a client and subscribed to this event, any time a new process is started, either by the system, by the user, by malware, by an exploit, doesn't matter, new process, it means we, our endpoint security client, will get a notification and an endpoint security message telling us that just happened. 65 00:06:31,400 --> 00:06:32,920 Super powerful stuff. 66 00:06:36,260 --> 00:06:40,300 Let's now talk about these endpoint security events. 67 00:06:40,300 --> 00:06:43,400 These are the ones that you can subscribe to. 68 00:06:43,400 --> 00:06:48,860 So these are declared in one of the header files, specifically estypes.h. 69 00:06:48,860 --> 00:06:56,680 There's a large structure that currently holds over 150 events that you can tell endpoint security you are interested in subscribing to. 70 00:06:56,680 --> 00:07:04,840 These range from process events that we just mentioned to file events, USB activity, all sorts of really interesting events. 71 00:07:04,840 --> 00:07:07,120 And Apple continues to add more. 72 00:07:07,760 --> 00:07:10,420 Generally these events fall into two categories. 73 00:07:10,420 --> 00:07:12,780 There are notification events. 74 00:07:12,780 --> 00:07:18,000 These are events that are delivered to you after the fact that something has occurred. 75 00:07:18,060 --> 00:07:22,600 So for example, on a previous slide we saw the notify exec event. 76 00:07:22,600 --> 00:07:26,620 This will be delivered to you any time a new process has started. 77 00:07:26,620 --> 00:07:29,560 But it's important to understand that it is reactive. 78 00:07:29,560 --> 00:07:33,380 It's after the fact and you can't really do anything about it. 79 00:07:33,380 --> 00:07:35,380 It's just a notification. 80 00:07:35,380 --> 00:07:41,760 Still, this is very powerful if you want to, for example, build a passive process monitor or file monitor. 81 00:07:41,760 --> 00:07:43,740 Notification events are your friend. 82 00:07:44,200 --> 00:07:49,260 The other category are the more powerful authorization events. 83 00:07:49,260 --> 00:07:59,640 These actually are delivered to you before the event occurs and the operating system actually pauses or holds the event until you explicitly approve or deny it. 84 00:07:59,640 --> 00:08:16,060 So we'll see for example there's another process event off exec that you can register for and if you register and subscribe to that whenever a new process is about to be started, the operating system and point security will pause that, ask you if you want to allow or deny the process, 85 00:08:16,060 --> 00:08:18,780 and if you allow it, it will allow the process to continue. 86 00:08:18,780 --> 00:08:23,680 But if you say no, it will actually prevent the process from starting in the first place. 87 00:08:27,100 --> 00:08:31,000 Alright, so let's talk about actually creating an endpoint security client. 88 00:08:31,000 --> 00:08:36,060 Don't want to go through all the code but you can see it's 15 lines, nothing too in depth. 89 00:08:36,060 --> 00:08:39,920 Basically first we import the endpoint security framework. 90 00:08:39,920 --> 00:08:42,620 We declare an endpoint security client. 91 00:08:42,840 --> 00:08:47,600 We define a handler block which we'll talk about more in a subsequent slide. 92 00:08:47,600 --> 00:08:53,960 Most important though at the bottom, line 15, we see we invoke the ES new client API. 93 00:08:53,960 --> 00:09:07,100 This takes a pointer to the endpoint security client that will be initialized and then the handler block that will be invoked automatically by endpoint security any time matching events occur anywhere on the system. 94 00:09:07,340 --> 00:09:14,500 Now, this is the first endpoint security API you're going to call so it's very important to check the return result from this. 95 00:09:14,500 --> 00:09:21,620 Because we'll talk briefly, shortly about some of the prerequisites that Apple leverages on us if we want to use endpoint security. 96 00:09:21,680 --> 00:09:28,340 But for example, this API can fail if we don't have the right entitlements, privileges, or permissions. 97 00:09:30,140 --> 00:09:43,660 Once we have created a endpoint security client, next we need to tell endpoint security what of those 150 or so events we are actually interested in and then subscribe to them. 98 00:09:43,660 --> 00:09:47,460 So this is done via the ES subscribe API call. 99 00:09:47,460 --> 00:09:49,540 You can see it takes 3 arguments. 100 00:09:49,540 --> 00:09:52,520 The first is that initialized endpoint security client. 101 00:09:52,520 --> 00:09:55,320 Then an array of events. 102 00:09:55,320 --> 00:09:59,720 Here we're only asking to subscribe to 1, the notify exec event. 103 00:09:59,720 --> 00:10:03,220 And then we have to pass the number of events in this array. 104 00:10:03,420 --> 00:10:06,520 Recall that endpoint security is a C API. 105 00:10:06,520 --> 00:10:07,460 It's very low level. 106 00:10:07,460 --> 00:10:13,160 So a lot of times you're going to see APIs that take an array, an account, a string, and a length, et cetera, et cetera. 107 00:10:13,160 --> 00:10:15,480 These very low level C isms. 108 00:10:16,100 --> 00:10:23,040 Now once we've subscribed to this event, as I mentioned, any time the matching events occur, we will get notified about them. 109 00:10:25,840 --> 00:10:37,080 So when an event occurs that matches one of the ones we've subscribed to, endpoint security will deliver us a lovely message indicating that a matching event has just occurred. 110 00:10:37,560 --> 00:10:39,800 Here is the message that is delivered. 111 00:10:39,800 --> 00:10:43,000 It's rather a large structure with a lot of members. 112 00:10:43,000 --> 00:10:47,960 We're not going to go into all of them, but I do want to mention 2 or 3 important ones here. 113 00:10:47,960 --> 00:10:51,620 So first we have what I like to call the responsible process. 114 00:10:51,620 --> 00:10:55,980 And this is the process that is responsible for triggering the event. 115 00:10:55,980 --> 00:11:06,600 So imagine we've registered for file IO endpoint security events and imagine we are now monitoring the file system to detect anomalous or unusual events. 116 00:11:06,760 --> 00:11:10,320 Say something accesses the key chain database. 117 00:11:10,320 --> 00:11:12,000 The question is, is that the user? 118 00:11:12,000 --> 00:11:18,760 Is that the trusted key chain app or is that a stealer that's trying to grab your passwords and certificates? 119 00:11:19,020 --> 00:11:27,560 Well the good thing is, when we get the file IO event that endpoint security delivers to us, if we examine the responsible process, it will tell us whodunit. 120 00:11:27,560 --> 00:11:33,020 And we can see if it's the user, something trusted, or something malicious like a stealer. 121 00:11:33,220 --> 00:11:35,540 We also get an event type. 122 00:11:35,540 --> 00:11:39,100 You can subscribe to multiple events in the same endpoint security client. 123 00:11:39,100 --> 00:11:42,740 So this will tell you what event this message belongs to. 124 00:11:42,740 --> 00:11:43,560 What type. 125 00:11:43,560 --> 00:11:47,740 And this is important because the event data is event type specific. 126 00:11:47,820 --> 00:11:56,780 If we've registered for say process and file events and this is a process event, the event structure will be related to the process . 127 00:11:56,780 --> 00:11:58,720 For example, a PID and a path. 128 00:11:58,720 --> 00:12:05,380 If it's related to a file event, it won't have a PID but it will have things like the file source and destination path. 129 00:12:07,520 --> 00:12:11,700 Now we talk about the all important endpoint security handler block. 130 00:12:11,700 --> 00:12:17,840 As I mentioned this is invoked automatically by endpoint security anytime a matching event occurs anywhere on the system. 131 00:12:17,840 --> 00:12:23,400 This is where you put all your logic to do whatever you want to do to process that event. 132 00:12:23,660 --> 00:12:31,980 If you're writing a simple process monitor or file monitor, in this case we just print out some of the information, the responsible process, etc. 133 00:12:31,980 --> 00:12:32,360 etc. 134 00:12:32,360 --> 00:12:42,740 If you're trying to detect malware, this is where you would have some heuristics for example to examine what the event is, who did it, and if it's malware or not. 135 00:12:44,200 --> 00:12:48,320 Now the last thing we need to talk about are those pesky prerequisites. 136 00:12:48,320 --> 00:12:58,320 So, endpoint security is an insanely powerful framework that gives you essentially full access, full observability of everything that's happening on the operating system. 137 00:12:58,320 --> 00:13:06,760 Apple understandably says hey this is super powerful and so from a privacy point of view we just don't want any process using endpoint security. 138 00:13:06,760 --> 00:13:13,020 So they protect it with an entitlement that you have to ask or rather beg Apple give you. 139 00:13:13,200 --> 00:13:18,400 And without this entitlement you can't write endpoint security tools or at least you can't deploy endpoint security tools. 140 00:13:18,860 --> 00:13:33,760 So once you beg for this entitlement and make a sacrifice to Cupertino if they grant it to you, you are super stoked, you can then create a program that uses endpoint security, notarize it, entitle it , and then deploy it to your users. 141 00:13:33,900 --> 00:13:41,760 Your users though are going to have to run it with root privileges and either grant the utility or the terminal full disk access. 142 00:13:41,760 --> 00:13:49,440 Again this is just to essentially protect users from inadvertently or something malicious using endpoint security. 143 00:13:49,980 --> 00:13:54,200 Okay so that is a wrap of an introduction to endpoint security. 144 00:13:54,200 --> 00:14:01,760 Hopefully if you've even never heard about endpoint security before you understand at least conceptually how to create an endpoint security tool. 145 00:14:01,980 --> 00:14:08,300 So now I want to talk about some of the more advanced topics and also some of the nuances and pitfalls. 146 00:14:08,300 --> 00:14:17,340 Many of these have bit me, figuratively speaking, in the bot and so I want to share them with you so you can avoid my blood sweat and tears. 147 00:14:17,860 --> 00:14:23,400 So let's start with caching which is a nuance but a very important optimization. 148 00:14:25,280 --> 00:14:29,020 So caching is related to authorization events. 149 00:14:29,020 --> 00:14:43,680 So remember we had the authorization events which are events that the operating system will essentially disallow them until your endpoint security client tells the operating system to either allow or deny it. 150 00:14:44,240 --> 00:14:50,620 So for example we have an auth exec event to authorize new processes or deny ones we don't want. 151 00:14:50,620 --> 00:14:58,460 So the way you respond back to the operating system is by invoking the ES respond auth result API. 152 00:14:58,460 --> 00:15:07,700 This takes your decision, a yes or no, and then a very important flag which tells endpoint security whether to cache the response or not. 153 00:15:07,700 --> 00:15:18,100 If you tell endpoint security to cache the response, subsequent process executions of that same process will not result in endpoint security sending you another message. 154 00:15:18,100 --> 00:15:22,980 It basically says yo, I got it, I'm going to cache this, I'm not going to ask you again. 155 00:15:24,240 --> 00:15:30,440 So let's show a specific example of a tool that should have used caching but didn't. 156 00:15:30,440 --> 00:15:33,160 I'm going to pick on this tool because I wrote it. 157 00:15:33,380 --> 00:15:35,920 So one of the tools I've written is called block block. 158 00:15:35,920 --> 00:15:46,080 It monitors for persistent malware but it also has a neat feature that if you turn on, notarization mode, will block any process that is not notarized. 159 00:15:46,080 --> 00:15:47,520 What is notarization? 160 00:15:47,520 --> 00:15:55,840 It's essentially an Apple security mechanism that requires developers to submit their compiled binaries to Apple. 161 00:15:55,920 --> 00:15:58,200 Apple then scans them for known malware. 162 00:15:58,200 --> 00:16:03,100 If none is found, Apple notarizes it, essentially giving it a stamp of approval. 163 00:16:03,100 --> 00:16:08,600 Then a run time Mac OS will check to see if a process or program is notarized or not. 164 00:16:09,020 --> 00:16:16,440 Now obviously the vast, vast, vast majority of legitimate software is going to be notarized whereas malware often isn't. 165 00:16:16,440 --> 00:16:21,500 Malware authors are not keen on giving their binaries to Apple before they deploy them. 166 00:16:22,060 --> 00:16:27,960 So this is a very powerful heuristic that can block most malware very simply and very generically. 167 00:16:27,960 --> 00:16:33,600 And yes, Mac OS does try to also prevent non-notarized code from running but there's a lot of loopholes. 168 00:16:33,600 --> 00:16:38,620 Mac OS is actually very lenient which is why block block takes a more draconian step. 169 00:16:38,920 --> 00:16:43,100 So in terms of implementation, block block uses endpoint security. 170 00:16:43,420 --> 00:16:47,820 Specifically it registers or subscribes to the off exec event. 171 00:16:47,840 --> 00:16:55,980 Then whenever a program is launched by the user, by the system, by whatever, endpoint security pauses that, asks block block. 172 00:16:55,980 --> 00:16:58,440 Block block checks if it's notarized or not. 173 00:16:58,440 --> 00:17:00,120 And if it is it says cool, you can run. 174 00:17:00,120 --> 00:17:02,940 If it's blocked it says no and tells the user about this fact. 175 00:17:04,120 --> 00:17:05,720 Here's some of the code from block block. 176 00:17:05,720 --> 00:17:07,080 Don't worry about reading it all. 177 00:17:07,080 --> 00:17:09,460 It's more for a reference if you're interested. 178 00:17:09,580 --> 00:17:14,620 Basically block block uses Apple's code signing APIs to check if something is notarized. 179 00:17:14,620 --> 00:17:22,180 And then at the bottom you can see it invokes that ES respond auth result telling endpoint security to allow or deny. 180 00:17:22,180 --> 00:17:23,720 I took out the allow code. 181 00:17:23,720 --> 00:17:26,240 It's essentially the same as the deny code. 182 00:17:26,240 --> 00:17:29,460 But the key is that block block did not cache the result. 183 00:17:29,600 --> 00:17:30,140 Why not? 184 00:17:30,140 --> 00:17:32,160 Patrick didn't read the header files. 185 00:17:32,260 --> 00:17:35,700 Also process launches are not that common. 186 00:17:35,700 --> 00:17:38,800 Notarization checks are not that CPU intensive. 187 00:17:38,860 --> 00:17:41,480 So in my testing I didn't see a problem. 188 00:17:41,580 --> 00:17:50,120 Well of course I deployed block block and shortly thereafter I started getting some bug reports especially from people who were compiling large programs. 189 00:17:50,120 --> 00:17:52,380 Large software projects. 190 00:17:52,380 --> 00:17:56,200 They said, hey Patrick, block block is causing the CPU to spike. 191 00:17:56,460 --> 00:17:58,660 And very quickly I realized what the problem was. 192 00:17:58,660 --> 00:18:08,620 When you compile a large software project, especially if it's like Objective C or Swift code, the compiler is invoked again and again and again and again to compile each file. 193 00:18:08,680 --> 00:18:17,720 Since block block wasn't caching the result, this means endpoint security would ask block block again and again and again and again if it wanted to allow the compiler. 194 00:18:17,720 --> 00:18:28,260 Obviously block block should have checked the compiler once, said, yeah this is a signed notarized trusted Apple process, cached that response and then endpoint security would have never asked block block again. 195 00:18:28,560 --> 00:18:29,960 But it didn't. 196 00:18:30,300 --> 00:18:34,980 Turns out there's some more nuanced problems with caching. 197 00:18:34,980 --> 00:18:39,900 And so block block not caching revealed a more subtle problem. 198 00:18:39,980 --> 00:18:52,420 So I got another bug report saying, hey Patrick, I have block block and Jamf Protect installed on my computer and I noticed that when block block is running, Jamf Protect CPU utilization goes through the roof. 199 00:18:52,500 --> 00:18:53,720 I was like, that's super weird. 200 00:18:53,720 --> 00:18:56,460 Like why is block block impacting Jamf Protect? 201 00:18:56,820 --> 00:19:02,040 So it turns out that block block and Jamf Protect both use endpoint security, which is fine. 202 00:19:02,280 --> 00:19:06,340 What Jamf does is also subscribe to the auth exec event. 203 00:19:06,340 --> 00:19:07,880 Also no problems there. 204 00:19:07,900 --> 00:19:14,460 What Jamf did though, it was for every new process launch, they would perform some very CPU intensive actions. 205 00:19:14,460 --> 00:19:18,040 Caching the file, running a full Yara scan, all sorts of things. 206 00:19:18,040 --> 00:19:19,640 But then Jamf cached that. 207 00:19:19,640 --> 00:19:27,880 They basically said, okay, we're okay doing a one time very CPU intensive thing, but since we cached it, we will only do it once. 208 00:19:28,060 --> 00:19:44,960 Well unfortunately the endpoint security cache is global and there is a nuance that if there is another endpoint security client on the same system that is not caching the results, this actually undoes the caching even for the other client, which meant that because block block was not caching, 209 00:19:44,960 --> 00:19:55,840 this meant that Jamf was receiving all the same endpoint security messages again and again and therefore were performing the same CPU intensive actions over and over. 210 00:19:55,980 --> 00:19:57,360 Who's at fault here? 211 00:19:57,460 --> 00:19:58,780 Kind of everybody. 212 00:19:58,780 --> 00:20:00,260 I will take full responsibility. 213 00:20:00,260 --> 00:20:02,360 Block block should have been doing caching. 214 00:20:02,360 --> 00:20:07,340 I would argue the fact that the endpoint security cache is global, I understand why Apple did it. 215 00:20:07,340 --> 00:20:12,020 It's kind of a very simple way, but does have some problems. 216 00:20:12,160 --> 00:20:21,440 And then I think Jamf should have taken the step of implementing an internal cache because if some other client doesn't implement caching, ultimately it would impact them. 217 00:20:21,440 --> 00:20:25,400 And even if a new client comes along, Apple says that the cache would be invalidated. 218 00:20:25,400 --> 00:20:29,920 So if you're writing your own security tool, make sure you implement your own cache as well. 219 00:20:30,680 --> 00:20:33,380 All right, so you're like, Patrick, I get it, I should use cache. 220 00:20:33,380 --> 00:20:34,800 Well, not so fast. 221 00:20:34,800 --> 00:20:36,240 Nuances, nuances, nuances. 222 00:20:36,240 --> 00:20:39,680 That's really the theme of this talk and Apple's endpoint security. 223 00:20:39,760 --> 00:20:43,120 This is because caching may be broader than you think. 224 00:20:43,120 --> 00:20:46,700 So let's look at what it means for off exec events. 225 00:20:46,700 --> 00:20:49,900 Turns out caching is based on the binary itself. 226 00:20:49,900 --> 00:20:51,480 The file inode. 227 00:20:51,480 --> 00:20:52,680 So let's look at the slide. 228 00:20:52,680 --> 00:21:00,160 Imagine a developer is installing homebrew and he does that via bash and curl, the recommended way. 229 00:21:00,160 --> 00:21:05,380 If there's an endpoint security client that is examining new processes, it will see bash and curl being executed. 230 00:21:05,380 --> 00:21:08,920 It will approve these because they are trusted, notarized processes. 231 00:21:08,920 --> 00:21:14,340 And if it was a person who came to Patrick's talk, it was like, okay, I'm going to cache these because caching is very important. 232 00:21:14,740 --> 00:21:23,660 Well, the problem is if later malware uses bash or curl, endpoint security will not deliver a new event because bash and curl have not changed. 233 00:21:23,660 --> 00:21:27,700 That is to say, caching does not take into account the arguments. 234 00:21:27,700 --> 00:21:34,420 So what you really should do is avoid caching binaries where command line arguments control the behavior. 235 00:21:34,420 --> 00:21:38,020 For example, curl, Python or script interpreters. 236 00:21:38,020 --> 00:21:40,160 So again, always some nuances. 237 00:21:41,480 --> 00:21:46,980 So that's caching and now let's transition to talking about scripts and their interpreters. 238 00:21:47,540 --> 00:21:54,400 This is important because a lot of Mac malware even today is distributed to end users as standalone scripts. 239 00:21:54,500 --> 00:22:00,600 For example, we have Joker Spy, a malicious Python script, SIGIN, which is a malicious bash script. 240 00:22:00,600 --> 00:22:01,920 There's many of these. 241 00:22:03,920 --> 00:22:08,580 Now, if you've been following along so far, you've heard me talk a lot about the responsible process. 242 00:22:08,580 --> 00:22:15,740 And this is because endpoint security events are tied to the process that essentially triggered them or created them. 243 00:22:15,880 --> 00:22:21,620 And this is usually all well and good, but not in the context of script interpreters. 244 00:22:21,780 --> 00:22:26,140 So imagine the user inadvertently executes Joker Spy. 245 00:22:26,160 --> 00:22:33,500 Well, endpoint security will deliver a process event if someone has subscribed to it, but it won't be for the actual script. 246 00:22:33,500 --> 00:22:35,600 It will be for the script interpreter. 247 00:22:35,880 --> 00:22:39,140 And if you think about it, we actually don't care about the script interpreter at all. 248 00:22:39,140 --> 00:22:40,880 We don't care about Python per se. 249 00:22:40,880 --> 00:22:43,320 What we really care about is the script. 250 00:22:43,320 --> 00:22:53,400 We want to perhaps scan that to see if it's malware, run some heuristics against it to see if it's something malicious, and if so, block, yes, Python, but because of the script. 251 00:22:56,280 --> 00:23:07,640 So Apple realized this after endpoint security was released, and in version 2.0 of endpoint security, they added a script member to the exec key structures. 252 00:23:07,640 --> 00:23:10,140 These are the structures that describe processes. 253 00:23:10,540 --> 00:23:11,560 And this is awesome. 254 00:23:11,560 --> 00:23:20,140 So now as we can see on the slide, we can write code that can extract the script from a script interpreter. 255 00:23:20,620 --> 00:23:21,380 Note two things, though. 256 00:23:21,380 --> 00:23:25,840 First, we check the version to make sure we are executing on a compatible version of endpoint security. 257 00:23:25,840 --> 00:23:33,480 This is very important to do any time you use these newer methods because you don't want to crash if the user runs your program on an older version of macOS. 258 00:23:34,140 --> 00:23:44,980 Also then we check to make sure it's null because for most processes that don't have scripts, it's going to be blank, but for script interpreters, it should be set, meaning we get the script, which then we can scan to make sure it's not malware. 259 00:23:47,200 --> 00:23:50,020 And this appears to be all well and good, right? 260 00:23:50,020 --> 00:23:52,560 Now, spoiler, more nuances. 261 00:23:53,100 --> 00:24:01,920 So if we execute joker spy, it's a malicious Python script named sh.py, we can see the first invocation where we execute it directly. 262 00:24:01,920 --> 00:24:04,280 This generates an endpoint security event. 263 00:24:04,280 --> 00:24:17,280 We look at the script member that Apple now sets, and indeed, it is the path to the malicious script, which we can now detect as malware and block it terminating the Python process, protecting the user from infection. 264 00:24:17,960 --> 00:24:32,240 Well, if we execute it again, this time via the script interpreter, so Python and then script.py, we can see that as expected, because we're not caching script interpreters, we get another process event. 265 00:24:32,380 --> 00:24:39,400 However, in this case, the script member is null, it's blank, and so we can't detect that and the malware is free to run. 266 00:24:39,700 --> 00:24:41,660 So what's going on? 267 00:24:42,660 --> 00:24:55,960 Well, if we go back to the header files, which recall are the source of all answers to everything related to endpoint security, we see a very short comment that says, this field is only valid if a script was executed directly and not as an argument. 268 00:24:56,000 --> 00:25:06,400 Which means, as we saw in the previous slide, if the script is executed directly, the script member will be set, but if it's executed via a script interpreter, it will remain blank. 269 00:25:06,400 --> 00:25:11,740 This is kind of a huge shortcoming and something that we really all need to be aware of. 270 00:25:11,860 --> 00:25:16,000 Now, if we are aware of this, we can solve this in various ways. 271 00:25:16,000 --> 00:25:27,460 For example, if we see a script interpreter, we can ourselves manually enumerate all of the arguments and for those that are file paths, we can treat those as scripts. 272 00:25:29,060 --> 00:25:31,820 So that's scripts and more nuances. 273 00:25:32,380 --> 00:25:37,040 The next topic I want to talk about deals with authorization deadlines. 274 00:25:37,040 --> 00:25:42,860 And this is arguably the most important topic because if we don't get that right, we're going to get killed. 275 00:25:42,860 --> 00:25:44,220 Like, for real. 276 00:25:46,380 --> 00:25:51,020 So, let's briefly go back and touch on auth events. 277 00:25:51,280 --> 00:25:55,960 So we talked about this a few times, but let's focus on the auth exec event. 278 00:25:56,100 --> 00:26:09,560 Recall that if we subscribe to this event, any time a new process is about to be spawned, endpoint security will first come to us and ask us if we should or we want to allow or deny the process. 279 00:26:10,080 --> 00:26:16,240 While we are making a decision, that process is going to be essentially suspended or not run. 280 00:26:17,080 --> 00:26:30,100 And so Apple says, okay, third-party developers, we're going to give you a little bit of time to make your decision, but if you take all day, like, this is going to impact the user experience, and you know Apple is not stoked about the user experience being impacted, 281 00:26:30,100 --> 00:26:35,140 and so what macOS does is eventually, if you don't respond quick enough, it will nuke you. 282 00:26:35,140 --> 00:26:36,020 It will terminate you. 283 00:26:36,020 --> 00:26:38,820 Not like you, you, but your client, right? 284 00:26:39,240 --> 00:26:43,040 And it will generate a crash report, and at least it will explain exactly why. 285 00:26:44,380 --> 00:26:47,120 So you might be thinking, okay, Patrick, yeah, yeah, I get it. 286 00:26:47,120 --> 00:26:51,320 I'm going to make sure I respond fast enough so Apple doesn't kill me. 287 00:26:51,320 --> 00:26:53,940 But what if you're waiting on a user? 288 00:26:54,360 --> 00:27:01,980 So back to block block, when it detects a non-notarized process, it actually asks the user, yo, what do you want to do? 289 00:27:02,500 --> 00:27:03,820 What if the user is out to lunch? 290 00:27:03,820 --> 00:27:05,460 What if the user is not paying attention? 291 00:27:05,720 --> 00:27:10,000 Ah, if they don't respond, well, is Apple going to kill us? 292 00:27:10,000 --> 00:27:10,540 Yeah. 293 00:27:10,540 --> 00:27:13,040 So what is block block supposed to do? 294 00:27:16,340 --> 00:27:19,620 This is where end point security deadlines come into play. 295 00:27:19,660 --> 00:27:30,340 So recall that for every end point security event, we get this big end point security message that has a lot of metadata, but one of the things it has for these off events is a deadline. 296 00:27:30,700 --> 00:27:35,920 If we go to the header file, we can extract some important information about what these deadlines are. 297 00:27:35,960 --> 00:27:38,620 First, it's a mock absolute time. 298 00:27:38,620 --> 00:27:40,760 Next slide, we'll talk about what that is. 299 00:27:40,760 --> 00:27:43,460 It also says failure to respond. 300 00:27:43,460 --> 00:27:44,620 You will get nuked. 301 00:27:44,620 --> 00:27:46,120 Okay, Apple, we get it. 302 00:27:46,120 --> 00:27:50,280 And then thirdly, it says the deadline can and does vary between messages. 303 00:27:50,280 --> 00:27:55,800 And we can confirm this if we create a simple program that just spits out the off deadlines. 304 00:27:55,800 --> 00:27:59,460 We can see that for system processes, it's 3 or 4 seconds. 305 00:27:59,460 --> 00:28:01,600 For application , it's about 15. 306 00:28:01,600 --> 00:28:03,460 So that's a decent amount of time. 307 00:28:04,060 --> 00:28:06,920 Favor , don't ask Apple to increase this. 308 00:28:06,940 --> 00:28:18,760 There's a funny post where someone does and Quinn Eskimo, who is my favorite Apple employee, he is an OG that responds to everyone's technical questions on Apple forums. 309 00:28:18,760 --> 00:28:24,580 And he, I don't know if he's joking or not, but he's like, every time someone asks to make the ES off deadline longer, Apple shortens it. 310 00:28:24,580 --> 00:28:29,280 So don't ask Apple because we like that it's not super short so far. 311 00:28:30,700 --> 00:28:34,280 So let's talk a little bit more about this for two reasons. 312 00:28:34,280 --> 00:28:37,000 First, it is a mock absolute time. 313 00:28:37,000 --> 00:28:38,860 I have no idea what that was. 314 00:28:38,860 --> 00:28:40,260 I read Apple's documentation. 315 00:28:40,360 --> 00:28:43,200 It says a value of a clock that increments. 316 00:28:43,320 --> 00:28:45,360 Like, okay, that doesn't tell me too much. 317 00:28:45,460 --> 00:28:51,480 More importantly, though, most of the APIs we want to use to actually take into account the deadline want nanoseconds. 318 00:28:51,480 --> 00:29:00,500 So we need to convert this from a mock time clock increment value to something that we actually understand that the APIs want, which are nanoseconds. 319 00:29:00,940 --> 00:29:02,000 So what did Patrick do? 320 00:29:02,000 --> 00:29:03,400 Patrick said, okay, cool. 321 00:29:03,400 --> 00:29:06,160 Like, I will convert mock time to nanoseconds. 322 00:29:06,160 --> 00:29:09,000 And that was all well and good until Apple Silicon came along. 323 00:29:09,260 --> 00:29:15,980 With the release of Apple Silicon, Apple published a document called Addressing Architectural Differences. 324 00:29:16,100 --> 00:29:17,660 It's just gripping content. 325 00:29:17,860 --> 00:29:27,740 Of course, Patrick didn't read this, but this was problematic because one of the few things Apple says you must do very carefully on ARM systems is convert mock times to nanoseconds. 326 00:29:27,740 --> 00:29:29,100 You cannot do that directly. 327 00:29:29,100 --> 00:29:32,160 You're supposed to use a time-based information. 328 00:29:32,240 --> 00:29:41,500 I don't know exactly what that means, but Apple does provide a nice helper function that we can invoke to convert mock times to nanoseconds. 329 00:29:42,960 --> 00:29:47,400 So once we have that, here is some code to ensure that we respond to the deadline. 330 00:29:47,400 --> 00:29:50,180 Again, this is more for research resources. 331 00:29:50,180 --> 00:29:52,000 I don't want to stand up here explaining code all day. 332 00:29:52,000 --> 00:29:53,500 That's not that exciting. 333 00:29:53,500 --> 00:29:56,640 But what we can see is we essentially compute the delta. 334 00:29:56,840 --> 00:29:59,960 That is how much more time we have in the deadline. 335 00:29:59,960 --> 00:30:01,420 Is it 3.5 seconds? 336 00:30:01,420 --> 00:30:02,760 Is it 15 seconds? 337 00:30:02,840 --> 00:30:08,540 We then subtract a little bit of time from this because we want to make sure that we're responding before the deadline. 338 00:30:09,080 --> 00:30:22,080 And then at the bottom of the slide, we create a dispatch semaphore that will time out right before the deadline to make sure if the user, for example , hasn't responded, at least we programmatically respond so that macOS does not kill us. 339 00:30:22,080 --> 00:30:23,520 Now, there's probably other ways to do it. 340 00:30:23,520 --> 00:30:26,640 This is just the approach that block block takes. 341 00:30:26,800 --> 00:30:28,380 So again, that's off deadlines. 342 00:30:28,380 --> 00:30:33,740 Make sure you respond to them quickly enough or Apple will nuke your client and not restart it. 343 00:30:33,740 --> 00:30:36,320 Meaning the system is essentially then unprotected. 344 00:30:37,500 --> 00:30:38,000 Okay. 345 00:30:38,000 --> 00:30:41,520 Let's now talk about muting and mute inversions. 346 00:30:42,460 --> 00:30:48,320 So imagine you are using endpoint security and you go to create a file monitor. 347 00:30:48,520 --> 00:30:49,200 Awesome. 348 00:30:49,200 --> 00:30:51,480 You subscribe to all the file events. 349 00:30:51,480 --> 00:30:52,540 You get the entitlement. 350 00:30:52,540 --> 00:30:53,360 You compile it. 351 00:30:53,360 --> 00:30:54,860 You run your file monitor. 352 00:30:55,240 --> 00:30:56,860 And you're immediately overwhelmed. 353 00:30:56,940 --> 00:30:57,600 Why? 354 00:30:57,600 --> 00:30:58,320 Two reasons. 355 00:30:58,320 --> 00:31:03,200 First, there are a ton of legitimate file IO actions happening all over the system. 356 00:31:03,220 --> 00:31:10,720 And then secondly, if your tool actually prints or writes to the terminal, that is going to generate file events itself. 357 00:31:10,720 --> 00:31:13,760 The endpoint security is going to deliver back to you that then you're going to print out. 358 00:31:13,760 --> 00:31:16,000 You're going to kind of go into this recursive loop. 359 00:31:16,620 --> 00:31:23,100 So what we really want to do is tell endpoint security, okay, thank you for all the file events, but there's certain things that we want to avoid. 360 00:31:23,100 --> 00:31:24,300 That we want to ignore. 361 00:31:24,300 --> 00:31:28,520 For example, we want to ignore ourself as the responsible process. 362 00:31:28,520 --> 00:31:34,600 Or as we will see, you can also tell endpoint security to actually ignore or mute certain events. 363 00:31:34,600 --> 00:31:40,520 Perhaps you want to ignore all file events that are destined for the temporary directory. 364 00:31:40,520 --> 00:31:42,080 You just don't care about those. 365 00:31:43,560 --> 00:31:46,820 So let's start with talking about how to mute a process. 366 00:31:46,820 --> 00:31:52,200 Again, this is telling endpoint security you are not interested in receiving any events from the specified process. 367 00:31:52,280 --> 00:31:57,180 You do that via the aptly named ES mute process API. 368 00:31:57,300 --> 00:32:01,680 This takes an initialized client and then the audit token of the process. 369 00:32:02,020 --> 00:32:08,580 So there's a snippet of code on the slide where we're going to mute ourselves because we don't want file events from our own file monitor. 370 00:32:08,580 --> 00:32:09,900 That's pointless. 371 00:32:10,500 --> 00:32:14,180 Question though, how do you get an audit token and what even is an audit token? 372 00:32:14,660 --> 00:32:19,760 So an audit token is just a more modern way of identifying a process. 373 00:32:19,760 --> 00:32:23,720 PID are reused which introduces a bunch of security issues. 374 00:32:23,720 --> 00:32:26,700 So audit tokens are a better way. 375 00:32:26,700 --> 00:32:31,300 The problem is a lot of APIs still either want a PID or give you a PID. 376 00:32:31,300 --> 00:32:34,200 For example, if you enumerate all the running processes. 377 00:32:34,240 --> 00:32:38,760 So you might be wondering how given a PID you can get the process's audit token. 378 00:32:39,160 --> 00:32:42,740 Turns out you can use the task name for PID API. 379 00:32:42,920 --> 00:32:44,780 Here's some example code that does that. 380 00:32:44,780 --> 00:32:48,080 Given a PID it will give you the audit token for that. 381 00:32:48,460 --> 00:32:52,980 Make sure though you always call mock port deallocate otherwise you will have a memory leak. 382 00:32:52,980 --> 00:32:55,040 Again, found this out the hard way. 383 00:32:56,340 --> 00:33:01,100 You can also mute the process via the ES mute path API. 384 00:33:01,100 --> 00:33:04,240 Instead of an audit token this takes a path. 385 00:33:04,400 --> 00:33:06,620 So again, snippet of code on the slide. 386 00:33:06,820 --> 00:33:11,200 We mute ourselves by providing the path to our own process. 387 00:33:12,540 --> 00:33:14,760 Okay, so that's how to mute processes. 388 00:33:14,760 --> 00:33:19,880 Again, telling end point security, yo, we don't care about any events for this specific process. 389 00:33:19,960 --> 00:33:23,240 Well, what if you instead want to mute specific events? 390 00:33:23,240 --> 00:33:29,960 Like I mentioned, perhaps in your file monitor you want to ignore everything that is destined for the temporary directory. 391 00:33:30,720 --> 00:33:32,640 Well, turns out you can do that as well. 392 00:33:32,900 --> 00:33:37,520 And the good news is you actually use the same ES mute path API. 393 00:33:37,640 --> 00:33:45,910 As its last parameter, this API takes a type which tells end point security what thing you're actually trying to mute. 394 00:33:45,910 --> 00:33:48,250 Either a process or the actual event. 395 00:33:48,550 --> 00:33:52,930 And we can see on the slides there's four different types in two categories. 396 00:33:52,930 --> 00:33:55,790 The first category tells it to mute a process. 397 00:33:55,910 --> 00:33:59,390 The second one tells it to mute a path or an event. 398 00:34:00,490 --> 00:34:02,730 In each group there are two types. 399 00:34:02,730 --> 00:34:05,570 There's ones that end in prefix and one that ends in literal. 400 00:34:05,570 --> 00:34:09,310 Literal just means you're going to give it the full path to the process or the event. 401 00:34:09,310 --> 00:34:11,130 Prefix means it matches a prefix. 402 00:34:11,310 --> 00:34:20,530 So if we want to mute all file events going into the temporary directory or any of its subdirectories, we use the target prefix type. 403 00:34:20,950 --> 00:34:24,750 One more nuance because end point security is full of nuances. 404 00:34:24,990 --> 00:34:28,910 End point security does not support or handle symlinks. 405 00:34:29,030 --> 00:34:35,210 So that means we can't actually just give it slash TMP, slash temp because on macOS that is symlinked. 406 00:34:35,210 --> 00:34:44,010 The mute API will succeed but you'll actually still see events from slash temp because again it's symlinked and end point security doesn't support that. 407 00:34:44,010 --> 00:34:48,670 So you either have to resolve that path yourself or dynamically look it up. 408 00:34:50,230 --> 00:34:52,730 Alright, let's now talk about mute inversion. 409 00:34:52,730 --> 00:34:55,310 Conceptually this is pretty straight forward. 410 00:34:55,310 --> 00:34:57,970 To explain the APIs is a little bit more complicated. 411 00:34:57,970 --> 00:35:00,530 It messes with my head but I'm going to try my best. 412 00:35:01,090 --> 00:35:06,870 So, so far we've been talking about end point security messages in a very global context. 413 00:35:06,870 --> 00:35:14,810 Right, we've talked about process monitors and file monitors that once we subscribe to these events, end point security says here are all the events. 414 00:35:14,810 --> 00:35:21,830 Here's all the processes, here's all the files and yes we showed how to mute a few of them but what if we want to do the opposite? 415 00:35:21,830 --> 00:35:23,770 What if we want to do the invert? 416 00:35:24,010 --> 00:35:34,590 Specifically, what if we want to write a file monitor to analyze a piece of malware and ignore every process except the one specific one we are running, the malware. 417 00:35:34,590 --> 00:35:37,710 We want to see how the malware persists, what files it generates. 418 00:35:38,050 --> 00:35:41,730 Well, turns out you can now do this via mute inversion. 419 00:35:42,570 --> 00:35:49,070 Another example, say you want to protect the user's cookies or document directory. 420 00:35:49,210 --> 00:35:58,450 We have some source code from Mac Stealer, it's a malicious Python script and we can see it's accessing Google Chrome's cookie directory to grab all the cookies. 421 00:35:58,710 --> 00:36:05,710 So what we could do is write a security tool that monitors this directory and only authorize, for example, the browser. 422 00:36:05,950 --> 00:36:10,510 Again, we don't care about any other directory, we only care about this specific one. 423 00:36:10,510 --> 00:36:13,830 This is something that mute inversion can help us implement. 424 00:36:15,010 --> 00:36:18,590 So how does one actually utilize mute inversion? 425 00:36:18,590 --> 00:36:21,730 And this is where it gets a little complicated, at least to me. 426 00:36:21,830 --> 00:36:25,930 So what Apple could have done is implement new mute inversion APIs. 427 00:36:25,930 --> 00:36:29,670 For example, mute invert process, mute invert path. 428 00:36:29,670 --> 00:36:48,270 But what Apple did, which was very slick, is they said, okay, you already have mute path and mute process APIs, so what if we provide a new API that just toggles mute inversion and then you can still use those older APIs or the original APIs, but now they will be inverted. 429 00:36:48,450 --> 00:36:50,790 So that's exactly what we can do. 430 00:36:50,790 --> 00:36:53,650 And I'll show some code that hopefully illustrates that a little better. 431 00:36:53,830 --> 00:36:58,230 So first, we can invoke the ES invert muting API. 432 00:36:58,470 --> 00:37:02,870 This will unmute anything that's already muted, but then mute everything else. 433 00:37:04,330 --> 00:37:09,410 First, though, we have to talk about some more nuances, specifically the default mute set. 434 00:37:09,930 --> 00:37:22,310 Turns out when endpoint security starts, Apple, without really telling anybody too much about this, mutes a bunch of quote system critical binaries that protect clients from panics and timeouts. 435 00:37:22,310 --> 00:37:26,670 So a bunch of system processes are actually muted by default. 436 00:37:26,790 --> 00:37:36,710 Well, this is problematic if we invert muting because remember when I said we invert muting, that first unmutes everything, which means it's going to unmute the default set, which is not what we want to do. 437 00:37:37,050 --> 00:37:43,730 Apple says, okay, if you're going to do mute inversion, make sure you call ES unmute all first. 438 00:37:43,730 --> 00:37:57,150 This will temporarily unmute the default set and then when you invert muting, which mutes everything, it will mute the default set back, essentially ensuring that the default mute set is always muted, which is what we want. 439 00:37:58,830 --> 00:38:04,830 Let's now look at some code that implements mute inversion that hopefully makes this all a little bit more clear. 440 00:38:05,190 --> 00:38:08,810 So here is some code to protect the user's directory. 441 00:38:09,090 --> 00:38:13,530 Specifically, we're going to register for the auth open API event. 442 00:38:13,650 --> 00:38:20,410 This will be delivered to us any time anybody tries to open any files, in this case, in the user's document directory . 443 00:38:20,890 --> 00:38:22,750 Top part of the code, don't worry about it. 444 00:38:22,750 --> 00:38:25,690 In the bottom, though, is where we find the mute inversion logic. 445 00:38:25,990 --> 00:38:28,610 The first thing we do is unmute all the paths. 446 00:38:28,610 --> 00:38:30,610 This is to handle the default mute set. 447 00:38:30,610 --> 00:38:32,310 We then invert muting. 448 00:38:32,310 --> 00:38:43,110 This remutes the default mute set and then also tells Apple when subsequently we call the original mute APIs, instead of muting things, it should unmute something. 449 00:38:43,190 --> 00:38:56,690 So on line 19, we invoke ES mute path, but since we've inverted muting, this will actually tell end point security that we are just interested in events for the user's document directory. 450 00:38:57,250 --> 00:39:08,290 You can read more about that in the book, but if we compile this code and execute this, we can see now any time a process attempts to access the user's documents directory, we get a notification. 451 00:39:08,290 --> 00:39:11,710 Just for this, no other files, we don't care about anything else. 452 00:39:11,710 --> 00:39:19,910 And if it's something like finder, we can trust and allow that, whereas if it's something like max stealer, we can block that and protect the user. 453 00:39:21,150 --> 00:39:24,030 So that's mute and mute inversions. 454 00:39:24,230 --> 00:39:28,390 The next thing, one of the last things I want to talk about is, what's new? 455 00:39:28,610 --> 00:39:34,870 I mentioned that Apple continues to invest in end point security, which is awesome and something I really appreciate. 456 00:39:35,770 --> 00:39:39,870 And one of the great things they do is, they often add new events. 457 00:39:40,450 --> 00:39:43,830 This is added in the ES types dot H file. 458 00:39:43,830 --> 00:39:50,650 You can open that and you can see towards the end of the big structure that contains all the events, the new events that are added. 459 00:39:50,990 --> 00:40:00,330 Apple also adds comments explaining what versions of macOS these new events are supported on because, here's another nuance, they don't back port them. 460 00:40:00,430 --> 00:40:03,490 Which is fine, but just something we need to be aware of. 461 00:40:04,110 --> 00:40:09,970 So if we look at this enum, we can see some of the latest events they've added and they're actually super cool events. 462 00:40:10,210 --> 00:40:18,050 So for example, in macOS 10 dot 13, they added the XP malware detected notification event. 463 00:40:18,050 --> 00:40:23,010 XP is XProtect, this is Apple's internal macOS antivirus product. 464 00:40:23,010 --> 00:40:26,950 I don't know if we can quite determine that, but it's a malware detection capability. 465 00:40:26,950 --> 00:40:35,290 They've now added an end point security message that your tool can subscribe to that when Apple detects some malware, it will let you know as well. 466 00:40:35,290 --> 00:40:36,330 Super cool. 467 00:40:36,790 --> 00:40:40,050 Another one is the BTM launch item ad. 468 00:40:40,050 --> 00:40:41,970 This is related to persistence. 469 00:40:42,570 --> 00:40:54,890 If you register for this method, or this event, now anytime anything persists on the system as a login item, launch daemon or launch agent, macOS end point security will let you know. 470 00:40:54,890 --> 00:41:03,850 This is super useful as well because the vast majority of malware persists in this manner and now we have a generic way to detect that something was just persisted. 471 00:41:03,850 --> 00:41:04,890 Super cool. 472 00:41:05,090 --> 00:41:11,170 And then finally in macOS 15 dot 4, Apple added the ability to see TCC events. 473 00:41:11,170 --> 00:41:11,970 Which is amazing. 474 00:41:11,970 --> 00:41:14,310 I've been begging for this for years. 475 00:41:14,550 --> 00:41:23,570 So we'll look at this on the next slide, but before we jump to that, note that if you want to use the new events, you should wrap them in some run time safety checks. 476 00:41:23,570 --> 00:41:30,810 Again to ensure that if your tool is run on an older version of macOS where these new events are not available, bad things don't happen. 477 00:41:31,090 --> 00:41:37,330 If you're using objective C, you can just use the available keyword and the compiler will take care of everything else. 478 00:41:38,830 --> 00:41:45,490 So let's briefly talk about this new TCC event because it's something I'm super stoked and really appreciated that Apple added. 479 00:41:45,650 --> 00:41:48,750 So many of you are probably familiar with TCC. 480 00:41:48,750 --> 00:41:57,350 Quick reminder, it is the technology on macOS that protects sensitive user components and sensitive devices on macOS. 481 00:41:57,430 --> 00:42:02,910 If we pull out extracted strings from the TCC daemon, we get a comprehensive list. 482 00:42:02,910 --> 00:42:09,490 It's things like your address book, your calendar, your phone calls, your face ID, your desktop, your photos. 483 00:42:09,490 --> 00:42:14,050 Things that Apple rightfully says, hey, should be protected. 484 00:42:14,290 --> 00:42:21,490 This is great because it means if malware even gets on your system, if it tries to access your photos or your location, TCC will block that. 485 00:42:22,510 --> 00:42:32,890 Malware though is starting to catch on and some of it has, some samples have exploits to get around this, but the run of the mill malware just asks the users. 486 00:42:32,890 --> 00:42:39,210 Especially if the malware is in a trojanized application that the user thinks is legitimate. 487 00:42:39,490 --> 00:42:41,810 So as an example, we have gravity rat. 488 00:42:41,810 --> 00:42:48,830 Gravity rat can detect if an item it's interested in, for example, your photos or documents, are protected by TCC. 489 00:42:48,830 --> 00:42:58,990 In that case, it just shows the user an alert saying, hey, I can't run until you grant me full disk access, essentially sidestepping TCC. 490 00:42:59,190 --> 00:43:08,710 And again, since this is a trojanized application, naive users might not think anything is amiss and then go ahead and grant gravity rat or the terminal full disk access. 491 00:43:08,710 --> 00:43:11,550 Previously security tools could not see this happening. 492 00:43:11,730 --> 00:43:22,510 However, because of this new TCC modification event, we now can register for that and get alerted if the user or malware modifies the TCC permissions. 493 00:43:22,910 --> 00:43:28,870 Now the structure for this new event is pretty involved and the documentation wasn't that good. 494 00:43:28,870 --> 00:43:42,850 So what I do in this case is just execute Apple's ESS, ES logger tool, specify the new event and then perform some action to trigger that event and then look at what the values are reported. 495 00:43:43,330 --> 00:43:58,350 So if I follow the instructions for gravity rat and go into the system's settings preference pane and give terminal full disk access, this is what ES logger will report to us for this new TCC modification event. 496 00:43:58,450 --> 00:44:01,630 We see the responsible process is TCCD. 497 00:44:01,630 --> 00:44:03,550 This is not super useful. 498 00:44:03,670 --> 00:44:05,750 It's the same for all these messages. 499 00:44:05,870 --> 00:44:10,050 It's just because the TCC daemon is the one actually making the TCC modification. 500 00:44:10,290 --> 00:44:14,570 More importantly though, are the values in the actual event. 501 00:44:14,690 --> 00:44:17,370 We see the TCC access that was granted. 502 00:44:17,370 --> 00:44:19,510 In this case, full disk access. 503 00:44:19,610 --> 00:44:22,130 We see the subject of permissions. 504 00:44:22,130 --> 00:44:25,210 That is the process that was granted the TCC permissions. 505 00:44:25,210 --> 00:44:26,470 In this case, the terminal. 506 00:44:26,470 --> 00:44:27,910 And then finally, the instigator. 507 00:44:27,910 --> 00:44:30,730 This is kind of like another responsible process. 508 00:44:30,730 --> 00:44:39,630 And in this case, because we are following gravity rat instructions and doing it via the system preferences application, that is the value of this instigator application. 509 00:44:40,670 --> 00:44:42,910 We can then implement this in code. 510 00:44:42,910 --> 00:44:48,610 Again, this is just for a resource , but this is how to extract the relevant members from that new TCC event. 511 00:44:48,610 --> 00:44:53,570 So you can take some action to decide if this is something that you want to block or look into. 512 00:44:53,570 --> 00:44:56,550 You know, is it malware granting itself TCC permissions? 513 00:44:56,570 --> 00:45:00,310 Is it the user giving TCC permissions to something it should not? 514 00:45:00,310 --> 00:45:01,430 Et cetera, et cetera. 515 00:45:04,230 --> 00:45:15,530 Now the last new thing I want to talk about that I'm really excited about, yes, I get excited about these things, is new in Mac OS 26 and it is ESCS validation category. 516 00:45:16,010 --> 00:45:24,090 And what this is, is endpoint security will now for processes tell you the code sign validation category. 517 00:45:24,450 --> 00:45:35,950 As we can see in the enum, the values for this range from platform, if it's a platform binary, part of the operating system, to development, app store, locally, if it's ad hoc. 518 00:45:35,950 --> 00:45:38,310 And this is all super, super cool. 519 00:45:38,390 --> 00:45:40,090 Why is this super, super cool? 520 00:45:40,090 --> 00:45:45,690 Well, previously, endpoint security clients had to compute or figure out this information themselves. 521 00:45:45,770 --> 00:45:46,550 Why? 522 00:45:46,550 --> 00:45:49,050 Well, let's take block block, for example. 523 00:45:49,590 --> 00:45:56,550 Block block treats applications from the Mac OS official app store differently than ad hoc signed binaries. 524 00:45:56,550 --> 00:46:05,370 This is because apps from the legitimate official trusted Mac app store are signed, validated by Apple, and also run in a sandbox. 525 00:46:05,370 --> 00:46:07,790 So they can't really do much bad. 526 00:46:07,790 --> 00:46:11,670 So there's very few examples of actually malware coming from the Mac app store. 527 00:46:11,670 --> 00:46:14,970 However, there's a ton of malware that is ad hoc signed. 528 00:46:15,370 --> 00:46:22,410 Previously, endpoint security would just tell you, hey, this process is signed, and it would be up to you to determine how it was signed. 529 00:46:22,410 --> 00:46:25,730 Either ad hoc, from the Mac app store, et cetera, et cetera. 530 00:46:25,730 --> 00:46:32,330 Now, you could do this, but it was a little bit complicated and used a lot of code, and it was somewhat CPU intensive. 531 00:46:32,630 --> 00:46:37,130 Well, the great news is in Mac OS 26, endpoint security will now tell you this. 532 00:46:37,130 --> 00:46:43,250 So you can just delete all your old code, and your code is both now simplified and more optimal. 533 00:46:43,690 --> 00:46:44,730 Hooray. 534 00:46:45,730 --> 00:46:46,510 All right. 535 00:46:46,510 --> 00:46:55,750 The last thing I want to talk about are some limitations, because at this point, we've touched on a lot of advanced topics, and hopefully illustrated the power of endpoint security. 536 00:46:55,750 --> 00:47:01,330 So you might be thinking it's a panacea , and it almost is, save for two main limitations. 537 00:47:01,650 --> 00:47:06,130 First, events sometimes aren't as comprehensive as we would like. 538 00:47:06,130 --> 00:47:12,050 So, for example , the TCC event that we just talked about is only delivered when TCC is modified. 539 00:47:12,070 --> 00:47:16,030 There's a lot of malware that actually checks if it has TCC permissions. 540 00:47:16,150 --> 00:47:19,150 That does not generate currently any ESS event. 541 00:47:19,150 --> 00:47:24,050 I would like that, because legitimate software often doesn't check these things. 542 00:47:24,050 --> 00:47:26,050 So this would be a very good thing to have. 543 00:47:26,650 --> 00:47:35,490 Also, if malware tries to perform some TCC action that's blocked, that also doesn't generate an event, because the TCC event is just for modifications. 544 00:47:35,770 --> 00:47:39,770 So on the slide, we have malware known as Windtail and JokerSpy. 545 00:47:39,770 --> 00:47:42,910 JokerSpy checks to see if it has TCC permissions. 546 00:47:42,910 --> 00:47:45,550 This generates no endpoint security notifications. 547 00:47:45,610 --> 00:47:49,970 Windtape tries to execute the screen capture utility to take a screenshot. 548 00:47:49,970 --> 00:47:52,250 This is now blocked by TCC. 549 00:47:52,330 --> 00:47:55,870 Also we don't get an endpoint security message for this. 550 00:47:55,870 --> 00:48:01,690 So this is just one example, but sometimes the events are not as comprehensive as you'd like, so be aware of that limitation. 551 00:48:02,650 --> 00:48:07,530 The other limitation is endpoint security events only trigger for new activity. 552 00:48:07,530 --> 00:48:12,890 This is not a critique of endpoint security, it's just understanding what it is designed to do. 553 00:48:12,930 --> 00:48:24,850 So if you're writing a security tool and you get installed on a new system, you might want to, for example, enumerate all existing persistent items, or look at everything that has already been granted TCC access. 554 00:48:24,850 --> 00:48:26,890 Endpoint security is not going to help you there. 555 00:48:26,890 --> 00:48:27,970 Neither should it. 556 00:48:27,970 --> 00:48:30,590 So again, just be aware of that limitation. 557 00:48:30,970 --> 00:48:33,930 And also, endpoint security doesn't cover everything. 558 00:48:33,930 --> 00:48:41,870 So for example, if you want to do host-based network monitoring, endpoint security does not provide information about network activity. 559 00:48:41,870 --> 00:48:45,530 Instead, you're going to want to use Apple's network extension frameworks. 560 00:48:45,550 --> 00:48:55,370 Some resources on those additional technologies, but again, the takeaway here is you might need a supplement to create a comprehensive Mac security tool. 561 00:48:56,430 --> 00:49:00,150 Alright, finally, some conclusions and takeaways. 562 00:49:00,670 --> 00:49:03,510 Really, two main takeaways that are pretty simple. 563 00:49:03,510 --> 00:49:06,570 First, endpoint security should be your bestie. 564 00:49:06,570 --> 00:49:13,970 I cannot tell you how amazing it is for using or for creating security tools on Mac OS. 565 00:49:13,990 --> 00:49:23,770 Because it really gives us the ability to detect or at least observe, essentially, any action that even the most sophisticated malware performs. 566 00:49:24,290 --> 00:49:34,930 Now, it's up to us to determine if that action is malicious or not, and that could be a whole separate talk, but at least provides us the ability to observe the events of even the most sophisticated threats. 567 00:49:35,570 --> 00:49:40,870 Other takeaway, make sure you use the more advanced capabilities of endpoint security. 568 00:49:40,870 --> 00:49:50,430 Caching, authorization events, deadlines, et cetera, et cetera, but be aware of the nuances and limitations so you don't get bit figuratively in the butt. 569 00:49:51,390 --> 00:49:56,510 Now, if you are interested in learning more, I already mentioned the Art of the Mac Malware book series. 570 00:49:56,510 --> 00:50:05,990 I just want to reiterate that again because tomorrow I'm going to be doing a book signing at No Starch Presses, and we're actually going to be giving away I believe 20 or 30 copies for free. 571 00:50:06,130 --> 00:50:09,450 So if you want to grab a copy , show up, first come, first serve. 572 00:50:09,570 --> 00:50:10,610 Also, I can sign that. 573 00:50:10,610 --> 00:50:12,710 We can talk nerdy about endpoint security. 574 00:50:13,070 --> 00:50:25,450 Also, if you're interested in Mac security topics, Apple security topics, I want to invite you all to Objective by the Sea, which is three days of dedicated Mac security talks from the world's top Mac security researchers. 575 00:50:25,850 --> 00:50:30,350 Also, I'm doing a training there on this exact topic in lots more three days. 576 00:50:30,350 --> 00:50:35,110 So if you want to learn more and get a little bit more hands on, check that out as well. 577 00:50:36,110 --> 00:50:39,630 Briefly, I also want to mention the Objective Sea Foundation. 578 00:50:39,690 --> 00:50:41,570 We do a whole bunch of really cool stuff. 579 00:50:41,570 --> 00:50:49,270 So if you're interested in learning more about the conference, our student scholarships, and our diversity programs, check out ObjectiveSea.org. 580 00:50:49,690 --> 00:50:53,390 Finally, I first and foremost want to thank you all for attending my talk. 581 00:50:53,390 --> 00:50:56,510 Nothing worse than me as a speaker giving a talk when no one shows up. 582 00:50:56,510 --> 00:50:59,910 That hasn't happened yet, but very thankful for you being here. 583 00:50:59,950 --> 00:51:02,330 Also then, round of applause for DEF CON. 584 00:51:07,530 --> 00:51:15,630 I run OBTS, so it would be kind of blasphemous for me to say DEF CON is the best conference, but kind of is, so shout out to that. 585 00:51:15,630 --> 00:51:18,370 And then finally, I just want to thank the friends of Objective Sea. 586 00:51:18,370 --> 00:51:32,030 These are the companies and products that support the foundation, support my research, make us be able to give the books for free, etc., so if you know anybody who works at these companies, give them a high five and thank them. 587 00:51:32,130 --> 00:51:33,270 So, that is a wrap. 588 00:51:33,270 --> 00:51:35,170 Here are the resources if you want to learn more. 589 00:51:35,170 --> 00:51:37,570 Again, thank you so much for attending my talk. 590 00:51:37,570 --> 00:51:41,450 Go out and write some great security tools using endpoint security. 591 00:51:41,470 --> 00:51:42,290 Thank you.