All right. Aloha and welcome to my talk on mastering Apple's endpoint security. So today we're going to talk about all things related to Apple's endpoint security. 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. Really shed some light into what I think are the darker corners of endpoint security. Super stoked you're all here. 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. But if you are a attacker, a hacker, a malware author and are targeting Mac OS, cool, it's Def Con, all are welcome. 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. 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. My name is Patrick Wardle . I am the founder of the Objective-C Foundation. 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. I'm also the author of the Art of Mac Malware book series. I mention this here because in volume 2 there are 2 full chapters dedicated solely to endpoint security. 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. And this is not a sales pitch because good news is this book is 100% free online. So let's start with some basics. 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. So what is endpoint security? Apple has a fairly succinct definition. They say it's a C API for monitoring system events for potentially malicious activity. 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. And I cannot stress the importance of this enough. 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. And you know that sucked because there were no APIs dedicated to security tools. Something like creating a simple process monitor was very very difficult. Now though with endpoint security it is a breeze . So thank you to Apple for releasing this. Myself and other tool developers we are forever indebted to you. 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 . Unlike some of their other frameworks, endpoint security is actually fairly well documented. If you go to their developer site, there's a whole section on that, even sample code. 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. 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. And so I want to instill that wisdom upon you as well. So there's 3 main header files, these will be found in the SDK directory under endpoint security. You have ES client, ES message, and ES types. As we can see on the slide, these are not normal header files. They include, for example, ASCII flow charts describing very complicated parts of endpoint security. 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. 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. Not only is it recommended and supported by Apple, and anytime Apple recommends and supports something, use it. I've been on the other end of that trying to do something a little different, especially before endpoint security. That always figuratively bit me in the butt when Apple would change something, for example, when a new version of Mac OS came out. Also Apple continues to invest and improve endpoint security, so it just gets better and better. 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. So again, if we use endpoint security, it really, as defenders, gives us a great leg up. Let's now talk about writing an endpoint security tool. Conceptually, it's actually pretty straightforward in two basic steps. Subsequent slides will show code how to do this, but for now let's talk about how to do it conceptually. So the first thing you're going to do is going to be to create an endpoint security client. This is important because all subsequent endpoint security API calls need this client. You basically pass it around. So step one is to create this client. 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. We'll get into that a little bit more. Once you have initialized, created a endpoint security client, you specify and subscribe to events of interest. So imagine we're interested in tracking process spawns . We want to get notified any time a process spawns anywhere on the system. For example, to check if it's malware, something it's trusted. We can do that with the notify exec event. We'll talk more about events very shortly. 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. Super powerful stuff. Let's now talk about these endpoint security events. These are the ones that you can subscribe to. So these are declared in one of the header files, specifically estypes.h. There's a large structure that currently holds over 150 events that you can tell endpoint security you are interested in subscribing to. These range from process events that we just mentioned to file events, USB activity, all sorts of really interesting events. And Apple continues to add more. Generally these events fall into two categories. There are notification events. These are events that are delivered to you after the fact that something has occurred. So for example, on a previous slide we saw the notify exec event. This will be delivered to you any time a new process has started. But it's important to understand that it is reactive. It's after the fact and you can't really do anything about it. It's just a notification. Still, this is very powerful if you want to, for example, build a passive process monitor or file monitor. Notification events are your friend. The other category are the more powerful authorization events. 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. 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, and if you allow it, it will allow the process to continue. But if you say no, it will actually prevent the process from starting in the first place. Alright, so let's talk about actually creating an endpoint security client. Don't want to go through all the code but you can see it's 15 lines, nothing too in depth. Basically first we import the endpoint security framework. We declare an endpoint security client. We define a handler block which we'll talk about more in a subsequent slide. Most important though at the bottom, line 15, we see we invoke the ES new client API. 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. 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. Because we'll talk briefly, shortly about some of the prerequisites that Apple leverages on us if we want to use endpoint security. But for example, this API can fail if we don't have the right entitlements, privileges, or permissions. 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. So this is done via the ES subscribe API call. You can see it takes 3 arguments. The first is that initialized endpoint security client. Then an array of events. Here we're only asking to subscribe to 1, the notify exec event. And then we have to pass the number of events in this array. Recall that endpoint security is a C API. It's very low level. 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. These very low level C isms. Now once we've subscribed to this event, as I mentioned, any time the matching events occur, we will get notified about them. 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. Here is the message that is delivered. It's rather a large structure with a lot of members. We're not going to go into all of them, but I do want to mention 2 or 3 important ones here. So first we have what I like to call the responsible process. And this is the process that is responsible for triggering the event. 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. Say something accesses the key chain database. The question is, is that the user? Is that the trusted key chain app or is that a stealer that's trying to grab your passwords and certificates? 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. And we can see if it's the user, something trusted, or something malicious like a stealer. We also get an event type. You can subscribe to multiple events in the same endpoint security client. So this will tell you what event this message belongs to. What type. And this is important because the event data is event type specific. 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 . For example, a PID and a path. 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. Now we talk about the all important endpoint security handler block. As I mentioned this is invoked automatically by endpoint security anytime a matching event occurs anywhere on the system. This is where you put all your logic to do whatever you want to do to process that event. 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. etc. 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. Now the last thing we need to talk about are those pesky prerequisites. 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. 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. So they protect it with an entitlement that you have to ask or rather beg Apple give you. And without this entitlement you can't write endpoint security tools or at least you can't deploy endpoint security tools. 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. Your users though are going to have to run it with root privileges and either grant the utility or the terminal full disk access. Again this is just to essentially protect users from inadvertently or something malicious using endpoint security. Okay so that is a wrap of an introduction to endpoint security. Hopefully if you've even never heard about endpoint security before you understand at least conceptually how to create an endpoint security tool. So now I want to talk about some of the more advanced topics and also some of the nuances and pitfalls. 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. So let's start with caching which is a nuance but a very important optimization. So caching is related to authorization events. 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. So for example we have an auth exec event to authorize new processes or deny ones we don't want. So the way you respond back to the operating system is by invoking the ES respond auth result API. 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. 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. It basically says yo, I got it, I'm going to cache this, I'm not going to ask you again. So let's show a specific example of a tool that should have used caching but didn't. I'm going to pick on this tool because I wrote it. So one of the tools I've written is called block block. 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. What is notarization? It's essentially an Apple security mechanism that requires developers to submit their compiled binaries to Apple. Apple then scans them for known malware. If none is found, Apple notarizes it, essentially giving it a stamp of approval. Then a run time Mac OS will check to see if a process or program is notarized or not. Now obviously the vast, vast, vast majority of legitimate software is going to be notarized whereas malware often isn't. Malware authors are not keen on giving their binaries to Apple before they deploy them. So this is a very powerful heuristic that can block most malware very simply and very generically. And yes, Mac OS does try to also prevent non-notarized code from running but there's a lot of loopholes. Mac OS is actually very lenient which is why block block takes a more draconian step. So in terms of implementation, block block uses endpoint security. Specifically it registers or subscribes to the off exec event. Then whenever a program is launched by the user, by the system, by whatever, endpoint security pauses that, asks block block. Block block checks if it's notarized or not. And if it is it says cool, you can run. If it's blocked it says no and tells the user about this fact. Here's some of the code from block block. Don't worry about reading it all. It's more for a reference if you're interested. Basically block block uses Apple's code signing APIs to check if something is notarized. And then at the bottom you can see it invokes that ES respond auth result telling endpoint security to allow or deny. I took out the allow code. It's essentially the same as the deny code. But the key is that block block did not cache the result. Why not? Patrick didn't read the header files. Also process launches are not that common. Notarization checks are not that CPU intensive. So in my testing I didn't see a problem. Well of course I deployed block block and shortly thereafter I started getting some bug reports especially from people who were compiling large programs. Large software projects. They said, hey Patrick, block block is causing the CPU to spike. And very quickly I realized what the problem was. 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. 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. 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. But it didn't. Turns out there's some more nuanced problems with caching. And so block block not caching revealed a more subtle problem. 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. I was like, that's super weird. Like why is block block impacting Jamf Protect? So it turns out that block block and Jamf Protect both use endpoint security, which is fine. What Jamf does is also subscribe to the auth exec event. Also no problems there. What Jamf did though, it was for every new process launch, they would perform some very CPU intensive actions. Caching the file, running a full Yara scan, all sorts of things. But then Jamf cached that. 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. 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, 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. Who's at fault here? Kind of everybody. I will take full responsibility. Block block should have been doing caching. I would argue the fact that the endpoint security cache is global, I understand why Apple did it. It's kind of a very simple way, but does have some problems. 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. And even if a new client comes along, Apple says that the cache would be invalidated. So if you're writing your own security tool, make sure you implement your own cache as well. All right, so you're like, Patrick, I get it, I should use cache. Well, not so fast. Nuances, nuances, nuances. That's really the theme of this talk and Apple's endpoint security. This is because caching may be broader than you think. So let's look at what it means for off exec events. Turns out caching is based on the binary itself. The file inode. So let's look at the slide. Imagine a developer is installing homebrew and he does that via bash and curl, the recommended way. If there's an endpoint security client that is examining new processes, it will see bash and curl being executed. It will approve these because they are trusted, notarized processes. 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. 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. That is to say, caching does not take into account the arguments. So what you really should do is avoid caching binaries where command line arguments control the behavior. For example, curl, Python or script interpreters. So again, always some nuances. So that's caching and now let's transition to talking about scripts and their interpreters. This is important because a lot of Mac malware even today is distributed to end users as standalone scripts. For example, we have Joker Spy, a malicious Python script, SIGIN, which is a malicious bash script. There's many of these. Now, if you've been following along so far, you've heard me talk a lot about the responsible process. And this is because endpoint security events are tied to the process that essentially triggered them or created them. And this is usually all well and good, but not in the context of script interpreters. So imagine the user inadvertently executes Joker Spy. Well, endpoint security will deliver a process event if someone has subscribed to it, but it won't be for the actual script. It will be for the script interpreter. And if you think about it, we actually don't care about the script interpreter at all. We don't care about Python per se. What we really care about is the script. 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. 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. These are the structures that describe processes. And this is awesome. So now as we can see on the slide, we can write code that can extract the script from a script interpreter. Note two things, though. First, we check the version to make sure we are executing on a compatible version of endpoint security. 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. 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. And this appears to be all well and good, right? Now, spoiler, more nuances. 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. This generates an endpoint security event. 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. 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. 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. So what's going on? 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. 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. This is kind of a huge shortcoming and something that we really all need to be aware of. Now, if we are aware of this, we can solve this in various ways. 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. So that's scripts and more nuances. The next topic I want to talk about deals with authorization deadlines. And this is arguably the most important topic because if we don't get that right, we're going to get killed. Like, for real. So, let's briefly go back and touch on auth events. So we talked about this a few times, but let's focus on the auth exec event. 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. While we are making a decision, that process is going to be essentially suspended or not run. 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, and so what macOS does is eventually, if you don't respond quick enough, it will nuke you. It will terminate you. Not like you, you, but your client, right? And it will generate a crash report, and at least it will explain exactly why. So you might be thinking, okay, Patrick, yeah, yeah, I get it. I'm going to make sure I respond fast enough so Apple doesn't kill me. But what if you're waiting on a user? So back to block block, when it detects a non-notarized process, it actually asks the user, yo, what do you want to do? What if the user is out to lunch? What if the user is not paying attention? Ah, if they don't respond, well, is Apple going to kill us? Yeah. So what is block block supposed to do? This is where end point security deadlines come into play. 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. If we go to the header file, we can extract some important information about what these deadlines are. First, it's a mock absolute time. Next slide, we'll talk about what that is. It also says failure to respond. You will get nuked. Okay, Apple, we get it. And then thirdly, it says the deadline can and does vary between messages. And we can confirm this if we create a simple program that just spits out the off deadlines. We can see that for system processes, it's 3 or 4 seconds. For application , it's about 15. So that's a decent amount of time. Favor , don't ask Apple to increase this. 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. 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. So don't ask Apple because we like that it's not super short so far. So let's talk a little bit more about this for two reasons. First, it is a mock absolute time. I have no idea what that was. I read Apple's documentation. It says a value of a clock that increments. Like, okay, that doesn't tell me too much. More importantly, though, most of the APIs we want to use to actually take into account the deadline want nanoseconds. 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. So what did Patrick do? Patrick said, okay, cool. Like, I will convert mock time to nanoseconds. And that was all well and good until Apple Silicon came along. With the release of Apple Silicon, Apple published a document called Addressing Architectural Differences. It's just gripping content. 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. You cannot do that directly. You're supposed to use a time-based information. 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. So once we have that, here is some code to ensure that we respond to the deadline. Again, this is more for research resources. I don't want to stand up here explaining code all day. That's not that exciting. But what we can see is we essentially compute the delta. That is how much more time we have in the deadline. Is it 3.5 seconds? Is it 15 seconds? We then subtract a little bit of time from this because we want to make sure that we're responding before the deadline. 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. Now, there's probably other ways to do it. This is just the approach that block block takes. So again, that's off deadlines. Make sure you respond to them quickly enough or Apple will nuke your client and not restart it. Meaning the system is essentially then unprotected. Okay. Let's now talk about muting and mute inversions. So imagine you are using endpoint security and you go to create a file monitor. Awesome. You subscribe to all the file events. You get the entitlement. You compile it. You run your file monitor. And you're immediately overwhelmed. Why? Two reasons. First, there are a ton of legitimate file IO actions happening all over the system. And then secondly, if your tool actually prints or writes to the terminal, that is going to generate file events itself. The endpoint security is going to deliver back to you that then you're going to print out. You're going to kind of go into this recursive loop. 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. That we want to ignore. For example, we want to ignore ourself as the responsible process. Or as we will see, you can also tell endpoint security to actually ignore or mute certain events. Perhaps you want to ignore all file events that are destined for the temporary directory. You just don't care about those. So let's start with talking about how to mute a process. Again, this is telling endpoint security you are not interested in receiving any events from the specified process. You do that via the aptly named ES mute process API. This takes an initialized client and then the audit token of the process. 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. That's pointless. Question though, how do you get an audit token and what even is an audit token? So an audit token is just a more modern way of identifying a process. PID are reused which introduces a bunch of security issues. So audit tokens are a better way. The problem is a lot of APIs still either want a PID or give you a PID. For example, if you enumerate all the running processes. So you might be wondering how given a PID you can get the process's audit token. Turns out you can use the task name for PID API. Here's some example code that does that. Given a PID it will give you the audit token for that. Make sure though you always call mock port deallocate otherwise you will have a memory leak. Again, found this out the hard way. You can also mute the process via the ES mute path API. Instead of an audit token this takes a path. So again, snippet of code on the slide. We mute ourselves by providing the path to our own process. Okay, so that's how to mute processes. Again, telling end point security, yo, we don't care about any events for this specific process. Well, what if you instead want to mute specific events? Like I mentioned, perhaps in your file monitor you want to ignore everything that is destined for the temporary directory. Well, turns out you can do that as well. And the good news is you actually use the same ES mute path API. As its last parameter, this API takes a type which tells end point security what thing you're actually trying to mute. Either a process or the actual event. And we can see on the slides there's four different types in two categories. The first category tells it to mute a process. The second one tells it to mute a path or an event. In each group there are two types. There's ones that end in prefix and one that ends in literal. Literal just means you're going to give it the full path to the process or the event. Prefix means it matches a prefix. 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. One more nuance because end point security is full of nuances. End point security does not support or handle symlinks. So that means we can't actually just give it slash TMP, slash temp because on macOS that is symlinked. 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. So you either have to resolve that path yourself or dynamically look it up. Alright, let's now talk about mute inversion. Conceptually this is pretty straight forward. To explain the APIs is a little bit more complicated. It messes with my head but I'm going to try my best. So, so far we've been talking about end point security messages in a very global context. 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. 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? What if we want to do the invert? 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. We want to see how the malware persists, what files it generates. Well, turns out you can now do this via mute inversion. Another example, say you want to protect the user's cookies or document directory. 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. So what we could do is write a security tool that monitors this directory and only authorize, for example, the browser. Again, we don't care about any other directory, we only care about this specific one. This is something that mute inversion can help us implement. So how does one actually utilize mute inversion? And this is where it gets a little complicated, at least to me. So what Apple could have done is implement new mute inversion APIs. For example, mute invert process, mute invert path. 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. So that's exactly what we can do. And I'll show some code that hopefully illustrates that a little better. So first, we can invoke the ES invert muting API. This will unmute anything that's already muted, but then mute everything else. First, though, we have to talk about some more nuances, specifically the default mute set. 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. So a bunch of system processes are actually muted by default. 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. Apple says, okay, if you're going to do mute inversion, make sure you call ES unmute all first. 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. Let's now look at some code that implements mute inversion that hopefully makes this all a little bit more clear. So here is some code to protect the user's directory. Specifically, we're going to register for the auth open API event. This will be delivered to us any time anybody tries to open any files, in this case, in the user's document directory . Top part of the code, don't worry about it. In the bottom, though, is where we find the mute inversion logic. The first thing we do is unmute all the paths. This is to handle the default mute set. We then invert muting. 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. 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. 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. Just for this, no other files, we don't care about anything else. 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. So that's mute and mute inversions. The next thing, one of the last things I want to talk about is, what's new? I mentioned that Apple continues to invest in end point security, which is awesome and something I really appreciate. And one of the great things they do is, they often add new events. This is added in the ES types dot H file. 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. 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. Which is fine, but just something we need to be aware of. 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. So for example, in macOS 10 dot 13, they added the XP malware detected notification event. XP is XProtect, this is Apple's internal macOS antivirus product. I don't know if we can quite determine that, but it's a malware detection capability. 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. Super cool. Another one is the BTM launch item ad. This is related to persistence. 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. 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. Super cool. And then finally in macOS 15 dot 4, Apple added the ability to see TCC events. Which is amazing. I've been begging for this for years. 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. 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. If you're using objective C, you can just use the available keyword and the compiler will take care of everything else. So let's briefly talk about this new TCC event because it's something I'm super stoked and really appreciated that Apple added. So many of you are probably familiar with TCC. Quick reminder, it is the technology on macOS that protects sensitive user components and sensitive devices on macOS. If we pull out extracted strings from the TCC daemon, we get a comprehensive list. It's things like your address book, your calendar, your phone calls, your face ID, your desktop, your photos. Things that Apple rightfully says, hey, should be protected. 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. 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. Especially if the malware is in a trojanized application that the user thinks is legitimate. So as an example, we have gravity rat. Gravity rat can detect if an item it's interested in, for example, your photos or documents, are protected by TCC. 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. 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. Previously security tools could not see this happening. 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. Now the structure for this new event is pretty involved and the documentation wasn't that good. 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. 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. We see the responsible process is TCCD. This is not super useful. It's the same for all these messages. It's just because the TCC daemon is the one actually making the TCC modification. More importantly though, are the values in the actual event. We see the TCC access that was granted. In this case, full disk access. We see the subject of permissions. That is the process that was granted the TCC permissions. In this case, the terminal. And then finally, the instigator. This is kind of like another responsible process. 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. We can then implement this in code. Again, this is just for a resource , but this is how to extract the relevant members from that new TCC event. So you can take some action to decide if this is something that you want to block or look into. You know, is it malware granting itself TCC permissions? Is it the user giving TCC permissions to something it should not? Et cetera, et cetera. 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. And what this is, is endpoint security will now for processes tell you the code sign validation category. 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. And this is all super, super cool. Why is this super, super cool? Well, previously, endpoint security clients had to compute or figure out this information themselves. Why? Well, let's take block block, for example. Block block treats applications from the Mac OS official app store differently than ad hoc signed binaries. This is because apps from the legitimate official trusted Mac app store are signed, validated by Apple, and also run in a sandbox. So they can't really do much bad. So there's very few examples of actually malware coming from the Mac app store. However, there's a ton of malware that is ad hoc signed. 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. Either ad hoc, from the Mac app store, et cetera, et cetera. Now, you could do this, but it was a little bit complicated and used a lot of code, and it was somewhat CPU intensive. Well, the great news is in Mac OS 26, endpoint security will now tell you this. So you can just delete all your old code, and your code is both now simplified and more optimal. Hooray. All right. 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. So you might be thinking it's a panacea , and it almost is, save for two main limitations. First, events sometimes aren't as comprehensive as we would like. So, for example , the TCC event that we just talked about is only delivered when TCC is modified. There's a lot of malware that actually checks if it has TCC permissions. That does not generate currently any ESS event. I would like that, because legitimate software often doesn't check these things. So this would be a very good thing to have. 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. So on the slide, we have malware known as Windtail and JokerSpy. JokerSpy checks to see if it has TCC permissions. This generates no endpoint security notifications. Windtape tries to execute the screen capture utility to take a screenshot. This is now blocked by TCC. Also we don't get an endpoint security message for this. So this is just one example, but sometimes the events are not as comprehensive as you'd like, so be aware of that limitation. The other limitation is endpoint security events only trigger for new activity. This is not a critique of endpoint security, it's just understanding what it is designed to do. 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. Endpoint security is not going to help you there. Neither should it. So again, just be aware of that limitation. And also, endpoint security doesn't cover everything. So for example, if you want to do host-based network monitoring, endpoint security does not provide information about network activity. Instead, you're going to want to use Apple's network extension frameworks. Some resources on those additional technologies, but again, the takeaway here is you might need a supplement to create a comprehensive Mac security tool. Alright, finally, some conclusions and takeaways. Really, two main takeaways that are pretty simple. First, endpoint security should be your bestie. I cannot tell you how amazing it is for using or for creating security tools on Mac OS. Because it really gives us the ability to detect or at least observe, essentially, any action that even the most sophisticated malware performs. 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. Other takeaway, make sure you use the more advanced capabilities of endpoint security. 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. Now, if you are interested in learning more, I already mentioned the Art of the Mac Malware book series. 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. So if you want to grab a copy , show up, first come, first serve. Also, I can sign that. We can talk nerdy about endpoint security. 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. Also, I'm doing a training there on this exact topic in lots more three days. So if you want to learn more and get a little bit more hands on, check that out as well. Briefly, I also want to mention the Objective Sea Foundation. We do a whole bunch of really cool stuff. So if you're interested in learning more about the conference, our student scholarships, and our diversity programs, check out ObjectiveSea.org. Finally, I first and foremost want to thank you all for attending my talk. Nothing worse than me as a speaker giving a talk when no one shows up. That hasn't happened yet, but very thankful for you being here. Also then, round of applause for DEF CON. 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. And then finally, I just want to thank the friends of Objective Sea. 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. So, that is a wrap. Here are the resources if you want to learn more. Again, thank you so much for attending my talk. Go out and write some great security tools using endpoint security. Thank you.