start	end	text
0	640	All right.
660	6100	Aloha and welcome to my talk on mastering Apple's endpoint security.
6180	10180	So today we're going to talk about all things related to Apple's endpoint security.
10180	20360	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.
20360	26760	Really shed some light into what I think are the darker corners of endpoint security.
27160	28960	Super stoked you're all here.
28960	41380	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.
41580	50560	But if you are a attacker, a hacker, a malware author and are targeting Mac OS, cool, it's Def Con, all are welcome.
50560	60760	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.
60760	71480	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.
72000	73360	My name is Patrick Wardle .
73360	77060	I am the founder of the Objective-C Foundation.
77380	89340	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.
89900	94180	I'm also the author of the Art of Mac Malware book series.
94180	102200	I mention this here because in volume 2 there are 2 full chapters dedicated solely to endpoint security.
102200	109960	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.
109960	114940	And this is not a sales pitch because good news is this book is 100% free online.
116800	119020	So let's start with some basics.
119020	129780	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.
130300	132320	So what is endpoint security?
132320	134900	Apple has a fairly succinct definition.
134900	141540	They say it's a C API for monitoring system events for potentially malicious activity.
141780	151800	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.
151800	155640	And I cannot stress the importance of this enough.
155700	164880	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.
164880	169800	And you know that sucked because there were no APIs dedicated to security tools.
169800	174300	Something like creating a simple process monitor was very very difficult.
174300	176400	Now though with endpoint security it is a breeze .
176400	178920	So thank you to Apple for releasing this.
178920	182720	Myself and other tool developers we are forever indebted to you.
182720	190180	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 .
190400	194920	Unlike some of their other frameworks, endpoint security is actually fairly well documented.
194920	199900	If you go to their developer site, there's a whole section on that, even sample code.
199900	209500	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.
210220	222840	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.
222840	226820	And so I want to instill that wisdom upon you as well.
226820	231740	So there's 3 main header files, these will be found in the SDK directory under endpoint security.
231740	235740	You have ES client, ES message, and ES types.
236460	240080	As we can see on the slide, these are not normal header files.
240080	247000	They include, for example, ASCII flow charts describing very complicated parts of endpoint security.
247080	253660	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.
254840	262680	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.
262680	269100	Not only is it recommended and supported by Apple, and anytime Apple recommends and supports something, use it.
269100	274240	I've been on the other end of that trying to do something a little different, especially before endpoint security.
274240	281080	That always figuratively bit me in the butt when Apple would change something, for example, when a new version of Mac OS came out.
281560	287320	Also Apple continues to invest and improve endpoint security, so it just gets better and better.
287680	300660	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.
300660	305460	So again, if we use endpoint security, it really, as defenders, gives us a great leg up.
307340	310420	Let's now talk about writing an endpoint security tool.
310440	314340	Conceptually, it's actually pretty straightforward in two basic steps.
314340	319960	Subsequent slides will show code how to do this, but for now let's talk about how to do it conceptually.
320060	325300	So the first thing you're going to do is going to be to create an endpoint security client.
325320	331160	This is important because all subsequent endpoint security API calls need this client.
331160	332400	You basically pass it around.
332400	334340	So step one is to create this client.
334460	344720	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.
344720	346700	We'll get into that a little bit more.
347080	354460	Once you have initialized, created a endpoint security client, you specify and subscribe to events of interest.
354800	359400	So imagine we're interested in tracking process spawns .
359400	363060	We want to get notified any time a process spawns anywhere on the system.
363060	366120	For example, to check if it's malware, something it's trusted.
366120	369320	We can do that with the notify exec event.
369320	371920	We'll talk more about events very shortly.
372140	391400	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.
391400	392920	Super powerful stuff.
396260	400300	Let's now talk about these endpoint security events.
400300	403400	These are the ones that you can subscribe to.
403400	408860	So these are declared in one of the header files, specifically estypes.h.
408860	416680	There's a large structure that currently holds over 150 events that you can tell endpoint security you are interested in subscribing to.
416680	424840	These range from process events that we just mentioned to file events, USB activity, all sorts of really interesting events.
424840	427120	And Apple continues to add more.
427760	430420	Generally these events fall into two categories.
430420	432780	There are notification events.
432780	438000	These are events that are delivered to you after the fact that something has occurred.
438060	442600	So for example, on a previous slide we saw the notify exec event.
442600	446620	This will be delivered to you any time a new process has started.
446620	449560	But it's important to understand that it is reactive.
449560	453380	It's after the fact and you can't really do anything about it.
453380	455380	It's just a notification.
455380	461760	Still, this is very powerful if you want to, for example, build a passive process monitor or file monitor.
461760	463740	Notification events are your friend.
464200	469260	The other category are the more powerful authorization events.
469260	479640	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.
479640	496060	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,
496060	498780	and if you allow it, it will allow the process to continue.
498780	503680	But if you say no, it will actually prevent the process from starting in the first place.
507100	511000	Alright, so let's talk about actually creating an endpoint security client.
511000	516060	Don't want to go through all the code but you can see it's 15 lines, nothing too in depth.
516060	519920	Basically first we import the endpoint security framework.
519920	522620	We declare an endpoint security client.
522840	527600	We define a handler block which we'll talk about more in a subsequent slide.
527600	533960	Most important though at the bottom, line 15, we see we invoke the ES new client API.
533960	547100	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.
547340	554500	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.
554500	561620	Because we'll talk briefly, shortly about some of the prerequisites that Apple leverages on us if we want to use endpoint security.
561680	568340	But for example, this API can fail if we don't have the right entitlements, privileges, or permissions.
570140	583660	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.
583660	587460	So this is done via the ES subscribe API call.
587460	589540	You can see it takes 3 arguments.
589540	592520	The first is that initialized endpoint security client.
592520	595320	Then an array of events.
595320	599720	Here we're only asking to subscribe to 1, the notify exec event.
599720	603220	And then we have to pass the number of events in this array.
603420	606520	Recall that endpoint security is a C API.
606520	607460	It's very low level.
607460	613160	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.
613160	615480	These very low level C isms.
616100	623040	Now once we've subscribed to this event, as I mentioned, any time the matching events occur, we will get notified about them.
625840	637080	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.
637560	639800	Here is the message that is delivered.
639800	643000	It's rather a large structure with a lot of members.
643000	647960	We're not going to go into all of them, but I do want to mention 2 or 3 important ones here.
647960	651620	So first we have what I like to call the responsible process.
651620	655980	And this is the process that is responsible for triggering the event.
655980	666600	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.
666760	670320	Say something accesses the key chain database.
670320	672000	The question is, is that the user?
672000	678760	Is that the trusted key chain app or is that a stealer that's trying to grab your passwords and certificates?
679020	687560	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.
687560	693020	And we can see if it's the user, something trusted, or something malicious like a stealer.
693220	695540	We also get an event type.
695540	699100	You can subscribe to multiple events in the same endpoint security client.
699100	702740	So this will tell you what event this message belongs to.
702740	703560	What type.
703560	707740	And this is important because the event data is event type specific.
707820	716780	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 .
716780	718720	For example, a PID and a path.
718720	725380	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.
727520	731700	Now we talk about the all important endpoint security handler block.
731700	737840	As I mentioned this is invoked automatically by endpoint security anytime a matching event occurs anywhere on the system.
737840	743400	This is where you put all your logic to do whatever you want to do to process that event.
743660	751980	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.
751980	752360	etc.
752360	762740	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.
764200	768320	Now the last thing we need to talk about are those pesky prerequisites.
768320	778320	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.
778320	786760	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.
786760	793020	So they protect it with an entitlement that you have to ask or rather beg Apple give you.
793200	798400	And without this entitlement you can't write endpoint security tools or at least you can't deploy endpoint security tools.
798860	813760	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.
813900	821760	Your users though are going to have to run it with root privileges and either grant the utility or the terminal full disk access.
821760	829440	Again this is just to essentially protect users from inadvertently or something malicious using endpoint security.
829980	834200	Okay so that is a wrap of an introduction to endpoint security.
834200	841760	Hopefully if you've even never heard about endpoint security before you understand at least conceptually how to create an endpoint security tool.
841980	848300	So now I want to talk about some of the more advanced topics and also some of the nuances and pitfalls.
848300	857340	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.
857860	863400	So let's start with caching which is a nuance but a very important optimization.
865280	869020	So caching is related to authorization events.
869020	883680	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.
884240	890620	So for example we have an auth exec event to authorize new processes or deny ones we don't want.
890620	898460	So the way you respond back to the operating system is by invoking the ES respond auth result API.
898460	907700	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.
907700	918100	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.
918100	922980	It basically says yo, I got it, I'm going to cache this, I'm not going to ask you again.
924240	930440	So let's show a specific example of a tool that should have used caching but didn't.
930440	933160	I'm going to pick on this tool because I wrote it.
933380	935920	So one of the tools I've written is called block block.
935920	946080	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.
946080	947520	What is notarization?
947520	955840	It's essentially an Apple security mechanism that requires developers to submit their compiled binaries to Apple.
955920	958200	Apple then scans them for known malware.
958200	963100	If none is found, Apple notarizes it, essentially giving it a stamp of approval.
963100	968600	Then a run time Mac OS will check to see if a process or program is notarized or not.
969020	976440	Now obviously the vast, vast, vast majority of legitimate software is going to be notarized whereas malware often isn't.
976440	981500	Malware authors are not keen on giving their binaries to Apple before they deploy them.
982060	987960	So this is a very powerful heuristic that can block most malware very simply and very generically.
987960	993600	And yes, Mac OS does try to also prevent non-notarized code from running but there's a lot of loopholes.
993600	998620	Mac OS is actually very lenient which is why block block takes a more draconian step.
998920	1003100	So in terms of implementation, block block uses endpoint security.
1003420	1007820	Specifically it registers or subscribes to the off exec event.
1007840	1015980	Then whenever a program is launched by the user, by the system, by whatever, endpoint security pauses that, asks block block.
1015980	1018440	Block block checks if it's notarized or not.
1018440	1020120	And if it is it says cool, you can run.
1020120	1022940	If it's blocked it says no and tells the user about this fact.
1024120	1025720	Here's some of the code from block block.
1025720	1027080	Don't worry about reading it all.
1027080	1029460	It's more for a reference if you're interested.
1029580	1034620	Basically block block uses Apple's code signing APIs to check if something is notarized.
1034620	1042180	And then at the bottom you can see it invokes that ES respond auth result telling endpoint security to allow or deny.
1042180	1043720	I took out the allow code.
1043720	1046240	It's essentially the same as the deny code.
1046240	1049460	But the key is that block block did not cache the result.
1049600	1050140	Why not?
1050140	1052160	Patrick didn't read the header files.
1052260	1055700	Also process launches are not that common.
1055700	1058800	Notarization checks are not that CPU intensive.
1058860	1061480	So in my testing I didn't see a problem.
1061580	1070120	Well of course I deployed block block and shortly thereafter I started getting some bug reports especially from people who were compiling large programs.
1070120	1072380	Large software projects.
1072380	1076200	They said, hey Patrick, block block is causing the CPU to spike.
1076460	1078660	And very quickly I realized what the problem was.
1078660	1088620	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.
1088680	1097720	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.
1097720	1108260	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.
1108560	1109960	But it didn't.
1110300	1114980	Turns out there's some more nuanced problems with caching.
1114980	1119900	And so block block not caching revealed a more subtle problem.
1119980	1132420	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.
1132500	1133720	I was like, that's super weird.
1133720	1136460	Like why is block block impacting Jamf Protect?
1136820	1142040	So it turns out that block block and Jamf Protect both use endpoint security, which is fine.
1142280	1146340	What Jamf does is also subscribe to the auth exec event.
1146340	1147880	Also no problems there.
1147900	1154460	What Jamf did though, it was for every new process launch, they would perform some very CPU intensive actions.
1154460	1158040	Caching the file, running a full Yara scan, all sorts of things.
1158040	1159640	But then Jamf cached that.
1159640	1167880	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.
1168060	1184960	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,
1184960	1195840	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.
1195980	1197360	Who's at fault here?
1197460	1198780	Kind of everybody.
1198780	1200260	I will take full responsibility.
1200260	1202360	Block block should have been doing caching.
1202360	1207340	I would argue the fact that the endpoint security cache is global, I understand why Apple did it.
1207340	1212020	It's kind of a very simple way, but does have some problems.
1212160	1221440	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.
1221440	1225400	And even if a new client comes along, Apple says that the cache would be invalidated.
1225400	1229920	So if you're writing your own security tool, make sure you implement your own cache as well.
1230680	1233380	All right, so you're like, Patrick, I get it, I should use cache.
1233380	1234800	Well, not so fast.
1234800	1236240	Nuances, nuances, nuances.
1236240	1239680	That's really the theme of this talk and Apple's endpoint security.
1239760	1243120	This is because caching may be broader than you think.
1243120	1246700	So let's look at what it means for off exec events.
1246700	1249900	Turns out caching is based on the binary itself.
1249900	1251480	The file inode.
1251480	1252680	So let's look at the slide.
1252680	1260160	Imagine a developer is installing homebrew and he does that via bash and curl, the recommended way.
1260160	1265380	If there's an endpoint security client that is examining new processes, it will see bash and curl being executed.
1265380	1268920	It will approve these because they are trusted, notarized processes.
1268920	1274340	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.
1274740	1283660	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.
1283660	1287700	That is to say, caching does not take into account the arguments.
1287700	1294420	So what you really should do is avoid caching binaries where command line arguments control the behavior.
1294420	1298020	For example, curl, Python or script interpreters.
1298020	1300160	So again, always some nuances.
1301480	1306980	So that's caching and now let's transition to talking about scripts and their interpreters.
1307540	1314400	This is important because a lot of Mac malware even today is distributed to end users as standalone scripts.
1314500	1320600	For example, we have Joker Spy, a malicious Python script, SIGIN, which is a malicious bash script.
1320600	1321920	There's many of these.
1323920	1328580	Now, if you've been following along so far, you've heard me talk a lot about the responsible process.
1328580	1335740	And this is because endpoint security events are tied to the process that essentially triggered them or created them.
1335880	1341620	And this is usually all well and good, but not in the context of script interpreters.
1341780	1346140	So imagine the user inadvertently executes Joker Spy.
1346160	1353500	Well, endpoint security will deliver a process event if someone has subscribed to it, but it won't be for the actual script.
1353500	1355600	It will be for the script interpreter.
1355880	1359140	And if you think about it, we actually don't care about the script interpreter at all.
1359140	1360880	We don't care about Python per se.
1360880	1363320	What we really care about is the script.
1363320	1373400	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.
1376280	1387640	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.
1387640	1390140	These are the structures that describe processes.
1390540	1391560	And this is awesome.
1391560	1400140	So now as we can see on the slide, we can write code that can extract the script from a script interpreter.
1400620	1401380	Note two things, though.
1401380	1405840	First, we check the version to make sure we are executing on a compatible version of endpoint security.
1405840	1413480	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.
1414140	1424980	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.
1427200	1430020	And this appears to be all well and good, right?
1430020	1432560	Now, spoiler, more nuances.
1433100	1441920	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.
1441920	1444280	This generates an endpoint security event.
1444280	1457280	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.
1457960	1472240	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.
1472380	1479400	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.
1479700	1481660	So what's going on?
1482660	1495960	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.
1496000	1506400	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.
1506400	1511740	This is kind of a huge shortcoming and something that we really all need to be aware of.
1511860	1516000	Now, if we are aware of this, we can solve this in various ways.
1516000	1527460	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.
1529060	1531820	So that's scripts and more nuances.
1532380	1537040	The next topic I want to talk about deals with authorization deadlines.
1537040	1542860	And this is arguably the most important topic because if we don't get that right, we're going to get killed.
1542860	1544220	Like, for real.
1546380	1551020	So, let's briefly go back and touch on auth events.
1551280	1555960	So we talked about this a few times, but let's focus on the auth exec event.
1556100	1569560	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.
1570080	1576240	While we are making a decision, that process is going to be essentially suspended or not run.
1577080	1590100	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,
1590100	1595140	and so what macOS does is eventually, if you don't respond quick enough, it will nuke you.
1595140	1596020	It will terminate you.
1596020	1598820	Not like you, you, but your client, right?
1599240	1603040	And it will generate a crash report, and at least it will explain exactly why.
1604380	1607120	So you might be thinking, okay, Patrick, yeah, yeah, I get it.
1607120	1611320	I'm going to make sure I respond fast enough so Apple doesn't kill me.
1611320	1613940	But what if you're waiting on a user?
1614360	1621980	So back to block block, when it detects a non-notarized process, it actually asks the user, yo, what do you want to do?
1622500	1623820	What if the user is out to lunch?
1623820	1625460	What if the user is not paying attention?
1625720	1630000	Ah, if they don't respond, well, is Apple going to kill us?
1630000	1630540	Yeah.
1630540	1633040	So what is block block supposed to do?
1636340	1639620	This is where end point security deadlines come into play.
1639660	1650340	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.
1650700	1655920	If we go to the header file, we can extract some important information about what these deadlines are.
1655960	1658620	First, it's a mock absolute time.
1658620	1660760	Next slide, we'll talk about what that is.
1660760	1663460	It also says failure to respond.
1663460	1664620	You will get nuked.
1664620	1666120	Okay, Apple, we get it.
1666120	1670280	And then thirdly, it says the deadline can and does vary between messages.
1670280	1675800	And we can confirm this if we create a simple program that just spits out the off deadlines.
1675800	1679460	We can see that for system processes, it's 3 or 4 seconds.
1679460	1681600	For application , it's about 15.
1681600	1683460	So that's a decent amount of time.
1684060	1686920	Favor , don't ask Apple to increase this.
1686940	1698760	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.
1698760	1704580	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.
1704580	1709280	So don't ask Apple because we like that it's not super short so far.
1710700	1714280	So let's talk a little bit more about this for two reasons.
1714280	1717000	First, it is a mock absolute time.
1717000	1718860	I have no idea what that was.
1718860	1720260	I read Apple's documentation.
1720360	1723200	It says a value of a clock that increments.
1723320	1725360	Like, okay, that doesn't tell me too much.
1725460	1731480	More importantly, though, most of the APIs we want to use to actually take into account the deadline want nanoseconds.
1731480	1740500	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.
1740940	1742000	So what did Patrick do?
1742000	1743400	Patrick said, okay, cool.
1743400	1746160	Like, I will convert mock time to nanoseconds.
1746160	1749000	And that was all well and good until Apple Silicon came along.
1749260	1755980	With the release of Apple Silicon, Apple published a document called Addressing Architectural Differences.
1756100	1757660	It's just gripping content.
1757860	1767740	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.
1767740	1769100	You cannot do that directly.
1769100	1772160	You're supposed to use a time-based information.
1772240	1781500	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.
1782960	1787400	So once we have that, here is some code to ensure that we respond to the deadline.
1787400	1790180	Again, this is more for research resources.
1790180	1792000	I don't want to stand up here explaining code all day.
1792000	1793500	That's not that exciting.
1793500	1796640	But what we can see is we essentially compute the delta.
1796840	1799960	That is how much more time we have in the deadline.
1799960	1801420	Is it 3.5 seconds?
1801420	1802760	Is it 15 seconds?
1802840	1808540	We then subtract a little bit of time from this because we want to make sure that we're responding before the deadline.
1809080	1822080	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.
1822080	1823520	Now, there's probably other ways to do it.
1823520	1826640	This is just the approach that block block takes.
1826800	1828380	So again, that's off deadlines.
1828380	1833740	Make sure you respond to them quickly enough or Apple will nuke your client and not restart it.
1833740	1836320	Meaning the system is essentially then unprotected.
1837500	1838000	Okay.
1838000	1841520	Let's now talk about muting and mute inversions.
1842460	1848320	So imagine you are using endpoint security and you go to create a file monitor.
1848520	1849200	Awesome.
1849200	1851480	You subscribe to all the file events.
1851480	1852540	You get the entitlement.
1852540	1853360	You compile it.
1853360	1854860	You run your file monitor.
1855240	1856860	And you're immediately overwhelmed.
1856940	1857600	Why?
1857600	1858320	Two reasons.
1858320	1863200	First, there are a ton of legitimate file IO actions happening all over the system.
1863220	1870720	And then secondly, if your tool actually prints or writes to the terminal, that is going to generate file events itself.
1870720	1873760	The endpoint security is going to deliver back to you that then you're going to print out.
1873760	1876000	You're going to kind of go into this recursive loop.
1876620	1883100	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.
1883100	1884300	That we want to ignore.
1884300	1888520	For example, we want to ignore ourself as the responsible process.
1888520	1894600	Or as we will see, you can also tell endpoint security to actually ignore or mute certain events.
1894600	1900520	Perhaps you want to ignore all file events that are destined for the temporary directory.
1900520	1902080	You just don't care about those.
1903560	1906820	So let's start with talking about how to mute a process.
1906820	1912200	Again, this is telling endpoint security you are not interested in receiving any events from the specified process.
1912280	1917180	You do that via the aptly named ES mute process API.
1917300	1921680	This takes an initialized client and then the audit token of the process.
1922020	1928580	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.
1928580	1929900	That's pointless.
1930500	1934180	Question though, how do you get an audit token and what even is an audit token?
1934660	1939760	So an audit token is just a more modern way of identifying a process.
1939760	1943720	PID are reused which introduces a bunch of security issues.
1943720	1946700	So audit tokens are a better way.
1946700	1951300	The problem is a lot of APIs still either want a PID or give you a PID.
1951300	1954200	For example, if you enumerate all the running processes.
1954240	1958760	So you might be wondering how given a PID you can get the process's audit token.
1959160	1962740	Turns out you can use the task name for PID API.
1962920	1964780	Here's some example code that does that.
1964780	1968080	Given a PID it will give you the audit token for that.
1968460	1972980	Make sure though you always call mock port deallocate otherwise you will have a memory leak.
1972980	1975040	Again, found this out the hard way.
1976340	1981100	You can also mute the process via the ES mute path API.
1981100	1984240	Instead of an audit token this takes a path.
1984400	1986620	So again, snippet of code on the slide.
1986820	1991200	We mute ourselves by providing the path to our own process.
1992540	1994760	Okay, so that's how to mute processes.
1994760	1999880	Again, telling end point security, yo, we don't care about any events for this specific process.
1999960	2003240	Well, what if you instead want to mute specific events?
2003240	2009960	Like I mentioned, perhaps in your file monitor you want to ignore everything that is destined for the temporary directory.
2010720	2012640	Well, turns out you can do that as well.
2012900	2017520	And the good news is you actually use the same ES mute path API.
2017640	2025910	As its last parameter, this API takes a type which tells end point security what thing you're actually trying to mute.
2025910	2028250	Either a process or the actual event.
2028550	2032930	And we can see on the slides there's four different types in two categories.
2032930	2035790	The first category tells it to mute a process.
2035910	2039390	The second one tells it to mute a path or an event.
2040490	2042730	In each group there are two types.
2042730	2045570	There's ones that end in prefix and one that ends in literal.
2045570	2049310	Literal just means you're going to give it the full path to the process or the event.
2049310	2051130	Prefix means it matches a prefix.
2051310	2060530	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.
2060950	2064750	One more nuance because end point security is full of nuances.
2064990	2068910	End point security does not support or handle symlinks.
2069030	2075210	So that means we can't actually just give it slash TMP, slash temp because on macOS that is symlinked.
2075210	2084010	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.
2084010	2088670	So you either have to resolve that path yourself or dynamically look it up.
2090230	2092730	Alright, let's now talk about mute inversion.
2092730	2095310	Conceptually this is pretty straight forward.
2095310	2097970	To explain the APIs is a little bit more complicated.
2097970	2100530	It messes with my head but I'm going to try my best.
2101090	2106870	So, so far we've been talking about end point security messages in a very global context.
2106870	2114810	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.
2114810	2121830	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?
2121830	2123770	What if we want to do the invert?
2124010	2134590	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.
2134590	2137710	We want to see how the malware persists, what files it generates.
2138050	2141730	Well, turns out you can now do this via mute inversion.
2142570	2149070	Another example, say you want to protect the user's cookies or document directory.
2149210	2158450	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.
2158710	2165710	So what we could do is write a security tool that monitors this directory and only authorize, for example, the browser.
2165950	2170510	Again, we don't care about any other directory, we only care about this specific one.
2170510	2173830	This is something that mute inversion can help us implement.
2175010	2178590	So how does one actually utilize mute inversion?
2178590	2181730	And this is where it gets a little complicated, at least to me.
2181830	2185930	So what Apple could have done is implement new mute inversion APIs.
2185930	2189670	For example, mute invert process, mute invert path.
2189670	2208270	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.
2208450	2210790	So that's exactly what we can do.
2210790	2213650	And I'll show some code that hopefully illustrates that a little better.
2213830	2218230	So first, we can invoke the ES invert muting API.
2218470	2222870	This will unmute anything that's already muted, but then mute everything else.
2224330	2229410	First, though, we have to talk about some more nuances, specifically the default mute set.
2229930	2242310	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.
2242310	2246670	So a bunch of system processes are actually muted by default.
2246790	2256710	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.
2257050	2263730	Apple says, okay, if you're going to do mute inversion, make sure you call ES unmute all first.
2263730	2277150	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.
2278830	2284830	Let's now look at some code that implements mute inversion that hopefully makes this all a little bit more clear.
2285190	2288810	So here is some code to protect the user's directory.
2289090	2293530	Specifically, we're going to register for the auth open API event.
2293650	2300410	This will be delivered to us any time anybody tries to open any files, in this case, in the user's document directory .
2300890	2302750	Top part of the code, don't worry about it.
2302750	2305690	In the bottom, though, is where we find the mute inversion logic.
2305990	2308610	The first thing we do is unmute all the paths.
2308610	2310610	This is to handle the default mute set.
2310610	2312310	We then invert muting.
2312310	2323110	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.
2323190	2336690	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.
2337250	2348290	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.
2348290	2351710	Just for this, no other files, we don't care about anything else.
2351710	2359910	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.
2361150	2364030	So that's mute and mute inversions.
2364230	2368390	The next thing, one of the last things I want to talk about is, what's new?
2368610	2374870	I mentioned that Apple continues to invest in end point security, which is awesome and something I really appreciate.
2375770	2379870	And one of the great things they do is, they often add new events.
2380450	2383830	This is added in the ES types dot H file.
2383830	2390650	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.
2390990	2400330	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.
2400430	2403490	Which is fine, but just something we need to be aware of.
2404110	2409970	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.
2410210	2418050	So for example, in macOS 10 dot 13, they added the XP malware detected notification event.
2418050	2423010	XP is XProtect, this is Apple's internal macOS antivirus product.
2423010	2426950	I don't know if we can quite determine that, but it's a malware detection capability.
2426950	2435290	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.
2435290	2436330	Super cool.
2436790	2440050	Another one is the BTM launch item ad.
2440050	2441970	This is related to persistence.
2442570	2454890	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.
2454890	2463850	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.
2463850	2464890	Super cool.
2465090	2471170	And then finally in macOS 15 dot 4, Apple added the ability to see TCC events.
2471170	2471970	Which is amazing.
2471970	2474310	I've been begging for this for years.
2474550	2483570	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.
2483570	2490810	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.
2491090	2497330	If you're using objective C, you can just use the available keyword and the compiler will take care of everything else.
2498830	2505490	So let's briefly talk about this new TCC event because it's something I'm super stoked and really appreciated that Apple added.
2505650	2508750	So many of you are probably familiar with TCC.
2508750	2517350	Quick reminder, it is the technology on macOS that protects sensitive user components and sensitive devices on macOS.
2517430	2522910	If we pull out extracted strings from the TCC daemon, we get a comprehensive list.
2522910	2529490	It's things like your address book, your calendar, your phone calls, your face ID, your desktop, your photos.
2529490	2534050	Things that Apple rightfully says, hey, should be protected.
2534290	2541490	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.
2542510	2552890	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.
2552890	2559210	Especially if the malware is in a trojanized application that the user thinks is legitimate.
2559490	2561810	So as an example, we have gravity rat.
2561810	2568830	Gravity rat can detect if an item it's interested in, for example, your photos or documents, are protected by TCC.
2568830	2578990	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.
2579190	2588710	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.
2588710	2591550	Previously security tools could not see this happening.
2591730	2602510	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.
2602910	2608870	Now the structure for this new event is pretty involved and the documentation wasn't that good.
2608870	2622850	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.
2623330	2638350	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.
2638450	2641630	We see the responsible process is TCCD.
2641630	2643550	This is not super useful.
2643670	2645750	It's the same for all these messages.
2645870	2650050	It's just because the TCC daemon is the one actually making the TCC modification.
2650290	2654570	More importantly though, are the values in the actual event.
2654690	2657370	We see the TCC access that was granted.
2657370	2659510	In this case, full disk access.
2659610	2662130	We see the subject of permissions.
2662130	2665210	That is the process that was granted the TCC permissions.
2665210	2666470	In this case, the terminal.
2666470	2667910	And then finally, the instigator.
2667910	2670730	This is kind of like another responsible process.
2670730	2679630	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.
2680670	2682910	We can then implement this in code.
2682910	2688610	Again, this is just for a resource , but this is how to extract the relevant members from that new TCC event.
2688610	2693570	So you can take some action to decide if this is something that you want to block or look into.
2693570	2696550	You know, is it malware granting itself TCC permissions?
2696570	2700310	Is it the user giving TCC permissions to something it should not?
2700310	2701430	Et cetera, et cetera.
2704230	2715530	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.
2716010	2724090	And what this is, is endpoint security will now for processes tell you the code sign validation category.
2724450	2735950	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.
2735950	2738310	And this is all super, super cool.
2738390	2740090	Why is this super, super cool?
2740090	2745690	Well, previously, endpoint security clients had to compute or figure out this information themselves.
2745770	2746550	Why?
2746550	2749050	Well, let's take block block, for example.
2749590	2756550	Block block treats applications from the Mac OS official app store differently than ad hoc signed binaries.
2756550	2765370	This is because apps from the legitimate official trusted Mac app store are signed, validated by Apple, and also run in a sandbox.
2765370	2767790	So they can't really do much bad.
2767790	2771670	So there's very few examples of actually malware coming from the Mac app store.
2771670	2774970	However, there's a ton of malware that is ad hoc signed.
2775370	2782410	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.
2782410	2785730	Either ad hoc, from the Mac app store, et cetera, et cetera.
2785730	2792330	Now, you could do this, but it was a little bit complicated and used a lot of code, and it was somewhat CPU intensive.
2792630	2797130	Well, the great news is in Mac OS 26, endpoint security will now tell you this.
2797130	2803250	So you can just delete all your old code, and your code is both now simplified and more optimal.
2803690	2804730	Hooray.
2805730	2806510	All right.
2806510	2815750	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.
2815750	2821330	So you might be thinking it's a panacea , and it almost is, save for two main limitations.
2821650	2826130	First, events sometimes aren't as comprehensive as we would like.
2826130	2832050	So, for example , the TCC event that we just talked about is only delivered when TCC is modified.
2832070	2836030	There's a lot of malware that actually checks if it has TCC permissions.
2836150	2839150	That does not generate currently any ESS event.
2839150	2844050	I would like that, because legitimate software often doesn't check these things.
2844050	2846050	So this would be a very good thing to have.
2846650	2855490	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.
2855770	2859770	So on the slide, we have malware known as Windtail and JokerSpy.
2859770	2862910	JokerSpy checks to see if it has TCC permissions.
2862910	2865550	This generates no endpoint security notifications.
2865610	2869970	Windtape tries to execute the screen capture utility to take a screenshot.
2869970	2872250	This is now blocked by TCC.
2872330	2875870	Also we don't get an endpoint security message for this.
2875870	2881690	So this is just one example, but sometimes the events are not as comprehensive as you'd like, so be aware of that limitation.
2882650	2887530	The other limitation is endpoint security events only trigger for new activity.
2887530	2892890	This is not a critique of endpoint security, it's just understanding what it is designed to do.
2892930	2904850	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.
2904850	2906890	Endpoint security is not going to help you there.
2906890	2907970	Neither should it.
2907970	2910590	So again, just be aware of that limitation.
2910970	2913930	And also, endpoint security doesn't cover everything.
2913930	2921870	So for example, if you want to do host-based network monitoring, endpoint security does not provide information about network activity.
2921870	2925530	Instead, you're going to want to use Apple's network extension frameworks.
2925550	2935370	Some resources on those additional technologies, but again, the takeaway here is you might need a supplement to create a comprehensive Mac security tool.
2936430	2940150	Alright, finally, some conclusions and takeaways.
2940670	2943510	Really, two main takeaways that are pretty simple.
2943510	2946570	First, endpoint security should be your bestie.
2946570	2953970	I cannot tell you how amazing it is for using or for creating security tools on Mac OS.
2953990	2963770	Because it really gives us the ability to detect or at least observe, essentially, any action that even the most sophisticated malware performs.
2964290	2974930	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.
2975570	2980870	Other takeaway, make sure you use the more advanced capabilities of endpoint security.
2980870	2990430	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.
2991390	2996510	Now, if you are interested in learning more, I already mentioned the Art of the Mac Malware book series.
2996510	3005990	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.
3006130	3009450	So if you want to grab a copy , show up, first come, first serve.
3009570	3010610	Also, I can sign that.
3010610	3012710	We can talk nerdy about endpoint security.
3013070	3025450	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.
3025850	3030350	Also, I'm doing a training there on this exact topic in lots more three days.
3030350	3035110	So if you want to learn more and get a little bit more hands on, check that out as well.
3036110	3039630	Briefly, I also want to mention the Objective Sea Foundation.
3039690	3041570	We do a whole bunch of really cool stuff.
3041570	3049270	So if you're interested in learning more about the conference, our student scholarships, and our diversity programs, check out ObjectiveSea.org.
3049690	3053390	Finally, I first and foremost want to thank you all for attending my talk.
3053390	3056510	Nothing worse than me as a speaker giving a talk when no one shows up.
3056510	3059910	That hasn't happened yet, but very thankful for you being here.
3059950	3062330	Also then, round of applause for DEF CON.
3067530	3075630	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.
3075630	3078370	And then finally, I just want to thank the friends of Objective Sea.
3078370	3092030	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.
3092130	3093270	So, that is a wrap.
3093270	3095170	Here are the resources if you want to learn more.
3095170	3097570	Again, thank you so much for attending my talk.
3097570	3101450	Go out and write some great security tools using endpoint security.
3101470	3102290	Thank you.
