Aloha and welcome to my talk. Uh, today we're gonna be talking about Harnessing Weapons of Mac Destruction. My name is Patrick, I am a security researcher at Jamf. Uh, previously I was a co-founder at Digita Security and I'm also the creator of the Mac security website and tool suite Objective-C. So today we're gonna be talking about repurposing other people's malware for our own surreptitious purposes. We'll start by showing why this is rather an excellent idea and then show exactly how to repurpose various Mac malware samples. Of course we want our repurposed samples to remain undetected both by Apple's built-in malware mitigations and third-party AV products so we'll address that as well. Finally we'll end by covering various generic mitigations and third-party AV products so we'll address that as well. So let's dive in. So here we have a diagrammatic overview that conceptually explains the repurposing process. So imagine that spy A has developed some neat, uh, sophisticated, uh, fully featured malware and has deployed it to various targets, right? In fact, it has infected various systems around the world. Spy B somehow captures this malware, uh, perhaps either in transit or pulls it off an infected system. Spy B then will take this malware sample to her lab and then repurpose it. So what do we mean when we talk about repurposing? Well we'll get into this more shortly but basically we're talking about reconfiguring a piece of existing malware so that you can use it for your own surreptitious purposes. So for example we can see on the slide, uh, a piece of malware that has been used for, uh, repurposing. So we can see on the slide once spy B has repurposed the malware, she can now redeploy that against new targets and instead of beaconing back to spy A's infrastructure for tasking, it'll talk to her new infrastructure instead. Now you might be thinking, okay, this seems like a reasonably neat idea, but why? And we'll talk about two reasons that hopefully show that we should really be asking why not. So first, there are incredibly well-funded, uh, incredibly well-resourced, uh, very multi-functional, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, highly motivated hackers, APT groups, and three-lever, three-letter government agencies that are creating some incredible malware. Uh, this malware is fully featured and has the benefit of being fully tested. A lot of times already even tested in the wild. Now I'm rather lazy, I'd rather be sleeping on the beach or surfing or doing yoga, so I figured why not let these groups create this incredible malware and then we can just repurpose it for our own missions. The idea is to let those with more time and resources be able to use it for their own purposes. So let's say, uh, the first thing we do is we try to get our users to do all of the hard work. Secondly, if our repurposed creations that we redeploy get captured, it's very likely that they will be misattributed back to the original authors, not to us. This is great. Now you might be thinking, okay Patrick, if this is such an amazing idea, why aren't people already doing this? Well, the answer is, they are. Now, I can't comment specifically on this, but leaked slide, uh, slides appear to in to indicate that the, the, the, the, the, the, the, the, the, the, the, the, the, the, the, the, the NSA has had this capability for a while. Also, recent reporting from the New York Times claims that the Chinese are doing this as well. Now, to these well-resourced agencies, the benefit of using repurposed malware is twofold. First, it allows them to deploy other countries' malware into risky environments. For example, perhaps a target where another agency, another country, another APGT group already exists. So imagine you're the NSA, you ta- you hack a high-value target, you find out, ah, someone else is already there. Obviously, you don't want to put your primary implants on that system. So, if you have a repurposed implant that you can use, might as well put that down. Also, we already talked about attribution. These agencies don't like people to know what they're up to, so if they can use repurposed malware, perhaps this muddles the attribution picture. Now, of course, there's gonna be challenges to repurposing malware. If there weren't, we wouldn't be talking about it today. But we'll see these challenges are not insurmountable. So, first, generally speaking, if we capture a piece of malware to repurpose, we're not going to have the source code for the malware. We're just gonna have a binary image. So we'll have to reverse engineer the malware at the binary level. This is kind of the analysis stage, I like to call it. And what we're doing is we're trying to figure out and gain an understanding of what the malware does. Answering questions such as, how does it persist? What does it do? And what we're trying to do is we're trying to figure out what the malware does, how does it install, and what its capabilities are. And, of course, what command and control servers it talks to. Then, with such an understanding, we have to figure out exactly how to patch the malware. Again, usually we don't have source code, so this involves patching at the binary level. Now, if the malware talks to a remote command and control server, which most do, we will have to figure out how the malware determines where to get the address of that server, and then also understand the protocol so we can create a our own command and control server so when we redeploy the repurposed malware, we can communicate with our new infected systems. And then finally, since we are repurposing general known, generally known malware, it's likely that Apple or AV products already have detection signatures for this. So we have to co- we have to talk and we have to figure out how to get around that, because obviously it would be rather unfortunate if you deployed some repurposed malware only to have it detected. Now, I want to reiterate that it's very important to take these challenges seriously and get them right, because if not, relatively bad things can happen. Here, for example, we've attempted to comprehensively repurpose a piece of Mac malware, but it turns out that we forgot to modify or patch out the addresses of the backup command and control servers. This means if we deploy what we believe to be a fully repurposed piece of malware, at some point, it may beacon back to the original hacker's- infrastructure, given him or her access to our infected targets. So obviously this is something we want to be very aware of and make sure when we are repurposing these samples, we're doing it comprehensively. So now let's talk about exactly how to repurpose Mac malware with the goal of making their malware ours. So, the first step is to select a malware specimen. And that is the sample you want to repurpose. This is usually based on two criteria. First, you decide what you need the malware to do. Right? Are you looking for an interactive backdoor? Uh, perhaps a piece of ransomware? Cryptocurrency miner? Take your pick. And then maybe attribution is important to you. Right? Do you want your repurposed sample to originally belong to a three-letter agency, a Russian hacking group? So again, if it gets caught, it'll be likely misattributed to them. So on my website, Objective-C.com, I have a very comprehensive collection of Mac malware. So that's a great place to start. And all the samples we're talking about today that we'll repurpose, the original specimens are available for download. Now, once you've selected your malware sample, it's time to analyze it. In terms of repurposing or reconfiguring, uh, as we mentioned, the main goal is to figure out things like, uh, if it has any remote access. Right? If it has any access to the network. If it has access to the remote command and control server, we want to know what that server is, so that in the malware we can modify it to instead talk to us. If the malware does talk to a remand, uh, remote command and control server, we also need to understand the protocol, so that we can task it when it connects back to us. And of course, you want to fully understand the malware's capability, both locally, how does it persist, how does it interact with the system, and then also remotely. What commands, can we remotely task the malware with? So once you have a comprehensive understanding of the malware, it's time to actually reconfigure or repurpose it. Generally, this means we'll have to patch the malware at the binary level. So here, for example, as we can see on the slide, we've located the addresses of the malware's remote command and control servers directly embedded in the malware's binary. We can simply modify these bytes. For example, we can modify the code of the malware, for example, in a hex editor, to change that address to point to our own command and control server. This means now when we take this malware and redeploy it, the malware will beacon, connect back to our infrastructure, and be ready for tasking. Now, as I mentioned, if the malware is a backdoor or an implant, uh, and expects to talk to a command and control server, well, if we're repurposing it, we're going to have to write our own custom command and control server as well. This will obviously ensure that when the malware checks in and connects back to us, we can give it tasking and fully exploit its remote capabilities. Now, such a custom command and control server really doesn't have to be anything incredibly fancy. We whip up a simple Python script, but if you're planning on deploying these repurposed malware samples to many targets, you might want something more comprehensively. Uh, at a previous talk about creating one such custom command and control server with the goal of analyzing a sample. Now, obviously, each specimen you repurpose is going to be different in the sense that it likely has a different protocol, and thus we'll need a custom command and control server for that specimen, but that paper is kind of a good place to start. Alright, so now let's walk through some actual examples repurposing some Mac malware. And we're going to start with a sample called FruitFly. FruitFly is a rather unique piece of Mac malware. It was originally developed over 15 years ago, uh, remaining in the wild, and only very recently became detected. It's a fully featured backdoor, which supports a myriad of remotely taskable capabilities, which really makes it the perfect candidate for us to repurpose. As we'll see, we can very trivially repurpose this malware, and then that gives us access to a proven, fully featured backdoor that we can utilize for a lot of things. So, uh, let's take a look at some of the first examples and see if we can find an example that we can use for our own subversive purposes. FruitFly's actually written as a Perl script, though it's highly obfuscated. If we take a peek at the backdoor, we can see logic that processes command line arguments. That is to say, the script will check if it's been invoked with certain command line arguments, parameters, or flags. Turns out, it's looking for a port, or a port and IP combo of the command and control server to connect to. So this means if we specify our Poland and Christian service to a command line, own via the command line, we don't actually have to modify the Perl script or the malware binary itself. So what we can do is we can persist this, this malware, FruitFly, uh, as a launch agent, and as we can see on the slide, we can pass in our address of our command and control server. So this means we can trivially repurpose this backdoor to connect to our custom command and control server, right? Because we can specify in the command line, we can now push that out, give the address of our own server, and the malware, when it runs, will connect directly back to us, instead of the hacker's original servers. But to deploy this, we need an installer. Good news, previously I wrote one that's available on Pacebin. As we can see, it's just a few lines of code, which persistently installs the malware in a manner that matches how it was uncovered in the wild. Finally, as I had previously written a custom command and control server to analyze the sample, we can now deploy this in the server, and we can just review this. So, we're gonna do a live demo now. Live demos are always risky. And actually, we're gonna run this on my laptop. This is my own personal developer laptop. Not gonna run this in a VM. Because again, I wanna show you the power of this repurposed malware, and once we've repurposed it, it's essentially trustworthy. So, let's see this in action. So the first thing we're gonna do is we're gonna run the command and control server. This is listening for connections. And then we're going to execute the malware, uh, specifically via the installer, passing in the address of our command and control server. When we run this, we can see that the malware now has connected to our command and control server, on the right. And we see there's a myriad of capabilities that we can now task this malware. Again, this is running on my own personal box. So, for example, if we execute command number 13, this will tell us where the malware is installed. We can see it's running out of users, Patrick, FPS, odd. We can also do things like take a screened screenshot. That is command number two. Execute that. We give it a parameter of zero, saying we want a full screen capture. And this will tell the malware, which has been deployed to my personal system now, to please take a screen shot and send it back to the command and control server. So we can kill the server, and we can open the screen shot, and we can see, yeah, it took an entire screen shot of the desktop. So, uh, we're gonna run this in action. So, uh, let's do this. Okay, so that's FruitFly. Now let's talk about repurposing a cryptocurrency miner, because maybe you want to make a lot of money mining cryptocurrency on other people's computers, but you don't have the skills, time, or resources to write your own. So, our target is called Creative Update. This cryptocurrency miner was spread via Trojan, Trojanized applications, and then spread via the popular third party application website, MacUpdate.com. If we mount the malicious disk drive, we're going to be able to do a lot of different things. So, if we take a look at this disk image that contains one of these Trojanized applications, and reverse engineer the application's main binary, we can see it invoking a method called execute script with privs. And if we look at what this method does in a decompiler or disassembler, we can see it simply executes a file named script out of the application's resource directory. So if we take a peek at this script, we can see it first launches the real pristine copy of the application that it had Trojanized, for example, Firefox. Uh, and this is so the user who has downloaded this infected Trojanized application doesn't think anything is amiss. We can then see in the script, it be- beacons out or connects out to a download server to download a zip archive named mdworker.zip. It then extracts this archive and persistently installs something named mdworker. If we look at the launch agent plist, we can see that the mdworker.plist is a mdworker.plist that persists the malware, we can see the path to the persistent binary, which is that mdworker binary we just discussed. Also, we can see some command line arguments that are passed into this binary. If we execute this mdworker binary, it turns out it's a legitimate cryptocurrency miner belonging to a company named Minergate. The command line arguments therefore specify the miner account, that is where to send the mining results, and the type of cryptocurrency to mine. So, similar to fruit fly, since the relevant parameters are passed via the command line, as say, opposed to directly embedded in the malware's binary, it's trivial to repurpose this sample. So, first we simply modify the property list, the plist, to specify our own miner account, and then zip up the malware's components. Now, instead of having the malware download that zip from a download server, we just add the archive to the mdworker.zip. So, we can see that the server is now live to the application's bundle. This is far more simpler and self-contained. It means we don't actually need to set up a remote download server to host this second piece. Next, we tweak the script, so instead it utilizes the zip archive we just added. Finally, we repackage everything up, and we're ready to go. So, we're gonna do live demo number two. Bring up finder window. First, I want to show you the repurposed malware. It's kind of an application bundle. We can see this is an app, and within the app there is this script file. There's the original Firefox. This is the one that's signed by Mozilla. Uh, if we pop over here though, we'll see that this is what the user downloads and expects to run. So, what we can do is we're gonna run this. This has been reconfigured. Again, I'm running this on my own personal system. I figured while I'm up on stage, I might as well be mining some cryptocurrency. So, we're gonna run this. Firefox pops up. Everything looks great. The user is stoked. They've got this great browser. But now if we pop back to a terminal window, behind the screens, I'll make this a little bigger. We can see that the malware has persistently installed itself, and again, if we look at the program arguments, we can see that it's gonna execute the miner named MDWorker, but we have repurposed it, so it's instead is gonna send all the results of the cryptocurrency mining operations to me. Sweet. Awesome. Alright, well, since we're talking about making money, right, we all love to do that, uh, perhaps you want to deploy some ransomware without spending time writing your own. So, now we'll walk through exactly how to repurpose KeyRanger. KeyRanger was the first fully functional in the wild ransomware targeting macOS. Hackers infected the popular BitTorrent client transmission on the legitimate applications website. This means that if users went to the transmission website to download the BitTorrent client, inadvertently, they would get the malicious, infected, trojanized version and become infected. Looking at the malware's binary code, we can see it executing something named general.rtf out of the application bundle. Though it appears to be a document, if we examine that file a little closer, it turns out it's not a document, it's really a MachO binary. So, what this MachO binary does is first sleeps for a few days, and then it connects out to the attackers command and control server expecting a response consisting of a public RSA encryption key and then decryption instructions. It then uses that encryption key to encrypt the users files, and then displays the decryption instructions that demand a ransom. Pretty standard ransom, ransomware. And it's just sort of like, well, all my functionality. So, let's talk about repurposing this ransomware. First, we don't want to wait for three days. We're kind of in a rush to start encrypting people's files. So what we can do is we can just patch out, knock out the call to the sleep function. Pretty easy to do. And modify the addresses of the servers that the ransomware contacts for the public, uh, RSA encryption key and the decryption instructions. As we can see on the slide, these addresses are directly embedded in the malware's binary, so it's pretty easy for us to hop into a hex editor and change them to the address of our server. Uh, pretty easy, right? And here for testing purposes, we just set them to localhost 127.0.0.1. Now we, we also need a server because the ransomware is gonna connect out when it's run, expecting that encryption key and decryption instructions. Turns out this is really easy to do. We can just take our own public, uh, RSA key and decryption instructions and package that up to, in a text file and then serve that up via netcat. So here's a demo of key ranger that's now been repurposed. We'll see in the terminal window, this is our server, again it's just netcat serving up the encryption key and the decryption instructions. The malware has been repurposed though to talk to this command and control server, not the attacker's original one. And then we'll start seeing the ransomware immediately start encrypting all the files on the desktop. Once the malware has done this, again it's launched when the user launched the transmission application, they're not gonna be privy to anything going on, uh, you know, the encryption happens in the background, although they might see all their files being encrypted. So now when the files are encrypted, the user's gonna be like, crap, this sucks, how do I get my files decrypted? They're gonna open the readme.decrypt file and you can now see we provided our own decryption instructions. So here I've said, hey, you know, sorry about losing all your files, I'm not sending you buckets of money and I will decrypt them for you. It's actually kind of mean, you know, maybe don't do that. Haha. Uh, the last sample I want to talk about repurposing is called Wintail. Now Wintail is a lovely backdoor written by a rather sophisticated APT group. Here we'll repurpose both their exploit and their own backdoor to use for our own missions, our offensive cyber surreptitious processes. Now one of the most interesting piece of this attack was the malware's infection vector. And the way it was able to infect fully patched Mac systems was by leveraging a legitimate functionality of Mac OS by abusing something called custom URL schemes. Now I've written a long detailed blog that really dives into the technical underpinnings of this, but in a nutshell, on Mac OS, as soon as an application is saved to the file system, even before the user has run it, Mac OS parses that application and if that application supports custom URL handlers, Mac OS will automatically register it. Once these custom URL handlers, or the app, has been registered, you can, for example, from a web page, make a request to that custom URL, which Mac OS will be happy to now launch. The exploit is realized in Javascript, uh, basically they have some code to first download a malicious application that contains a custom URL scheme. Mac OS registers that code and then, uh, registers that, and then they have some second piece of Javascript which makes a request to that now registered custom URL scheme to launch the malicious application. In terms of the backdoor, so again, this is the payload of the exploit, they persist a login item that supports capabilities such as file collection and exfiltration. So we fire up a process monitor, as we can see on the slide, we can observe what the malware does once it successfully infects the system. For example, we can see it invoking the built-in zip utility, uh, to compress the files that it has collected for exfiltration. And then via the built-in curl command, it sends them to the attacker's remote command and control server. The backdoor also supports a file download capability. So if we look at the SDF method, a disassembler or decompiler, we can see it decrypts the addresses of some embedded command and control servers. And then we can see that the attacker has downloaded the file. Then it makes a initial request to get a name for a downloaded file. And then makes a second request to get the actual file contents from the attacker's command and control server. If we use a network monitor, we can see this happening live. And as we note on the slide, the address of the command and control server is flux2key.com. And just remember this because we'll get back to this in a second. Now once the malware has downloaded the file from the attacker's, uh, remote command and control server, if we look at more disassembly, we can see that it then executes this. So this is done within the same SDF method. We can see that it unzips the file it gets from the attacker's remote server via the built-built-in ditto command. And then it executes it via the nstask API. So, pretty standard built-in download and execute capabilities. Finally, the backdoor can be remotely uninstalled. So, uh, the backdoor can be remotely uninstalled via self-delete logic. So, every time the malware starts up, it connects to the attacker's command and control infrastructure and basically says, should I delete myself? If the command and control server responds back with a 1, the malware will first delete, uninstall itself, and then terminate. It's kind of a neat capability that allows an attacker to remotely disinfect targets. Alright, so now it's time to coerce this lovely backdoor to do some more backdoor work. So, let's go back to our own bidding. So, first let's talk about repurposing the exploit. It's pretty simple to create a malicious webpage that auto-downloads a zip file containing a malicious application. As we mentioned, Safari by default will auto-expand zip archives. So, if we place a malicious application in that archive that implements some custom URL handlers, MacOS will automatically register that even before the user has run the application. The malicious webpage can then make a request to that now registered custom URL which will cause the application to be launched. Now there will be a pop up or two but the average user probably is gonna click through these. So here's a demo of the repurposed exploit that will drop and persist uh Wintail. We can see the page has downloaded the application and then we made that second request. There was a pop up. Again if the user clicks through which some percentage of users will fall for. We can see that now the malware has been automatically and persistently installed. Again since this is a logic flaw there's nothing really for Apple to patch. So this still works on the latest version of Mac OS. So now we've repurposed the exploit. Let's talk about repurposing the malware because it doesn't really help us if we are spreading their original malware. So let's talk about malware that still beacons back to the attacker's command and control servers. So step one we need to modify the embedded command and control server addresses so that when the malware is deployed via our repurposed exploit it connects to our servers so that we can task it. Unfortunately the embedded addresses of the malware's command and control servers are encrypted. Not to worry what we can do is we can coerce the malware to always load a dynamic library. And then once our library is running in the process address space of the malware we can basically modify uh the malware at run time to intercept the decrypted addresses of the command and control server before the malware uses them to connect out. So let's look a little closer about how we accomplish this. Again the goal is for us to specify our own command and control servers so that the malware connects back to our infrastructure. So let's look a little closer. So our dynamic library has now been loaded into the address space of the malware. What we can do is we can swizzle or swap out the malware's decryption routine. It's a method named upe. This is accomplished via the rather powerful method exchange implementations API. As we see on the slide once the decryption function has been swizzled or swapped out whenever the malware invokes the decryption function, transparently the decryption function is not working. So what we can do is we can call our method in our library. What our method now does is calls into the original decryption routine and I have no idea what the decryption algorithm is or how it works and frankly I don't care. But what I can do is since it's swizzled and passing through us I can see the results of that encryption. So if the original decryption method returns the decrypted addresses of the command and control servers I can just return the my own command and control server back into the malware's logic. So this dynamically gives us the ability to transparently swap out the decrypted command and control servers with our own. And the malware is none the wiser. Now I added some debug messages to the library so if we if we execute this now repurposed malware we can see and watch this at run time and view the addresses of the command and control servers being swapped out. So at the bottom of the screen uh in the terminal out put you can see that our library which is called through to the original decryption method has detected that that response is the command and control server that's now decrypted. We don't want to return that to the logic in the malware that's trying to connect out, so we basically just drop that and return our own. And this ensures now that the backdoor will always communicate with our servers instead of the original APT groups. Now we still have to write a command and control server uh because the malware will now be connecting back to us but we have to be able to talk the protocol so that we can correctly task it. Now the main feature of the Windtail back door is to collect and exfiltrate files. So let's add this first to our custom command and control server. Files are exfiltrated via post requests so we simply write a few lines of code in Python to handle this, saving off each file the malware sends to us from the infected host. So here's a demo now of our custom command and control server receiving exfiltrated files from the attacker. On the left you can see the network requests coming in and on the right all our command and control server does is write these out to the file system. This means we now support the ability to respond to the malware as it exfiltrates all sorts of files off infected hosts. Let's also extend our custom command and control server for this Windtail implant to support the download and execute capability. So we noted the malware makes two requests for this. The first is a request where the expected response is for the name of the downloaded file and then the second request we need to respond with the actual bytes of the file we want to download. The malware will then save that to the file we specify and then unzip that and execute that. Now again we only need a few lines of code in Python. You can leverage a lot of built in Python libraries to do this. Um and once we do that we then have the ability to. Task the implant. So let's watch this in action. We're gonna see the malware connecting out to our command and control servers cause we swapped out those addresses. And what we'll do is we'll have it download and execute the ubiquitous uh calculator dot app. So here comes the connections. We're giving it calculator dot app. This could be any binary and then the malware will unzip that and execute that. Now armed with this remote download and execute capability this means we can install other tools or even second pieces of malware. So let's see what we can do with this. So basically what we want to do with this is try and install another system. So let's go ahead and malware. So it really makes this repurposed implant kind of infinitely uh extensible. Finally let's add the ability uh to our custom command and control server so we can support the self delete command. Uh because at some point you know if the FBI comes knocking on your door it might be really nice to just press a button and have all these infected systems uninstall themselves. It's trivial to support this functionality. Basically when the malware connects out, checks in with our server and asks a should I self-delete? We just respond with one. This will tell the malware to delete and fully uninstall itself. This is another demo showing this in action. We can see the persistently installed implant. It's that orange logo of the application originally tries to mimic a PowerPoint documentation. Kind of sticks out but this is in kind of a hidden system directory. Uh what's gonna happen is the malware is going to connect out to us, make the should I self-delete command, a request. We're gonna respond with one which will cause the implant to uninstall itself and then terminate. So we can see it disappear and then it stops beaconing because obviously it's just self-deleted. Alright so we've showed exactly how to repurpose a myriad of Mac malware. We talked about backdoors, ransomware, cryptocurrency miners. Although we still need to talk about how to remain undetectable. We talked about how to remain protected. As generally speaking we're repurposing known malware. Which means both MacOS and third party AV products likely will block or detect these. Unless we do something about it. So first up let's talk about MacOS's built in malware mitigations such as XProtect, the malware removal tool, and code signing cert- certificate checks. Now at first glance these might seem somewhat problematic as they may generically block our repurposed malware. On any or rather on all Macs. Again we really need to talk about bypassing these because if we went to deploy a nicely repurposed piece of malware and Apple blocked it, ah that would, that would kinda suck. So let's start with XProtect. XProtect is a simple signature AV, simple signature based uh AV scanner that's built into all recent versions of MacOS. Somewhat limited, it only scans files downloaded by the user the first time they're executed. If we look at the signatures they're in a file named XProtect dot Yara we can see various Yara signatures to detect common pieces of Mac malware. Ah for example Key Ranger, the malware we just talked about repurposing. Now at Digito we wrote a simple utility called UXProtect that allows you to explore these signatures via the UI as well as at any time uh basically scan arbitrary files against these built in signatures. Now since XProtect is the most common type of malware in the world, XProtect is purely a signature based detection mechanism. It is incredibly trivial to bypass. So let's take a closer look at the signature for Key Ranger. We can see it maps to a group of instructions in the malware. Knowing this, to bypass the signature we can simply reorder or modify any of these instructions. This will change various bytes which will of course cause the signature to no longer match. So here for example we simply changed the number of bytes that the number of bytes that may be written to a buffer from OX400 to OX300. Now this doesn't impact the malware's functionality at all, but as we've changed the bytes that the signature is looking to match on, we've completely thwarted the XProtect signature. Demo time. Uh here we have two instances of Key Ranger. Again it was distributed in this infected transmission BitTorrent client. On the left we have the original sample. This is the hacker's sample. And on the right we have the repurposed sample that's gonna talk to our own command and control servers to get the encryption key. And then also it has that one byte modification to bypass XProtect. So we're gonna launch both of them. We're gonna see the original one is detected by XProtect and blocked. There's no option to run it. The user has to click cancel. If we execute the second one, it's gonna say hey this is downloaded from the internet. Are you sure you want to run that? User clicks yes. The ransomware is now able to persistently install on the user. So we're able to run the user's system and immediately starts encrypting all their files. Whew. Haha. But wait, there's more. Next up we have code signing checks. So most binaries on MacOS are code signed. Signed binaries are not blocked by Gatekeeper thus a lot of Mac malware is now code signed. The downside from the malware's point of view is once this malware is detected, Apple can simply revoke the code signing code. So it's not gonna be able to run it. So we're gonna have to revoke the code signing certificate and this means the malware will not be able to execute on any other Mac. So here for example we can see we have Wintails, the Wintail binary and the original code signing certificate has been revoked by Apple proper. This means even if we repurpose this and attempt to redeploy this to new systems it won't be allowed to run. Well the problem is really the revoked certificate. It's not the malware per se. So we can simply remove the code signing certificate. And then either we can remove the code signing certificate or we can either distribute the repurposed malware unsigned or re-sign it with another legit developer code signing certificate. Which is pretty easy to get. So to unsign it Apple actually provides a utility called code sign. There's an undocumented flag dash remove signature. So if you execute this and point it at the malware Apple's utility will nicely strip off the uh revoked code signing certificate. And then as I mentioned we can just deploy it as is unsigned or if we want to get a little more fancy we can re-sign it again using the uh code signing certificate. So the code signing utility passing in the dash S flag. This means the re-signed windtail will now no longer be blocked by Mac OS. Finally we have MRT. This is Apple's malware re- removal tool. It's another built in AV like tool that ships with all versions of Mac OS. It's similar to XProtect but scans malware that may be already on the system and if it detects it it'll automatically removes it. Now unlike XProtect it's signatures are directly embedded in the code sign certificate. So it's not embedded in its binary. Kind of interesting. And as an interesting aside Apple also uses MRT to remove legitimate software it deems unsafe. Such as the recent uh zoom application that had some vulnerable components. So giving a lot of power to Apple here for them to arbitrary remove uh files from your system. Now since MRT like XProtect uses signatures it's equally trivial to bypass. If we dump the MRT binary because again the signatures are embedded directly in that binary, we can see all the signatures including one for fruit fly which makes us wonder is this going to detect our repurposed fruit fly malware? Well if we examine the embedded MRT fruit fly signature we can see it's detecting fruit fly based on both the path of the malware and its launch agent plist. This means as long as we change either the path or the name of the malware it's not going to detect it. Like I said, trivial. Now I also want to briefly mention bypassing third party antivirus products because you might run into a system where the user you're targeting has an antivirus product installed. Now there's been a lot of talk about this so I'm not going to dive into it a ton but suffice to say uh traditional AV products that largely rely on signatures which is pretty much the norm especially on Mac OS are equally trivial to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- to- bypass. For example I took fruit fly, recall it's a Perl script, I ran it through a free online Perl obfuscator which caused detections on virus total to drop all the way to zero. Now that works but you might want to use more sophisticated capabilities especially for the binary based malware so you can do things like hack them, use a custom encryptor or even in memory execution. I gave a talk at Black Hat a few years ago about these techniques uh recommend checking that out because that can ensure that even if the user has a third party AV product installed on their system it will be unlikely to detect your repurposed malware creation. Now for Mac users this kind of sucks right? We showed that advanced adversaries are already using repurposed malware samples and that is actually not that difficult to do. Often times we just have to switch a few bytes or change the command line arguments and now you know power to the people we have the ability to take these very sophisticated data and use them in a very sophisticated way. So if you would like a foreshadowing of whatира can do then I've got the three actors here. Don't to stop it so long as you're okay with it. Its weirdly kindl for a calculator to be able to really, really prototype is really interesting. So you can do it in a scenario where your voice is very clean. You can actually program that to take the threads which you can do in a room white hats now and talk about how to generically detect such threats. So first and this is a well known fact that I hopefully just illustrated uh detecting malware based on signatures bound to fail. So instead what we need to do is look for unusual or malicious behaviors. By definition malware does unusual or abnormal things. So let's leverage this fact to discuss generically detecting even repurposed threats. Because again as we just stated if we or an attacker repurposed a known piece of malware, the underlying functionalities, its behaviors and capabilities aren't going to change. That is to say even a repurposed piece of ransomware is still going to encrypt all your files. Uh a repurposed backdoor is going to persist and then start exfiltrating your files to a remote server. So here on the slide I've kind of listed some capabilities or some actions that are going to be used in this particular case. So let's start by talking about persistence. What I mean by persistence is the way that malware installs itself so that it's automatically restarted every time the system is rebooted. This is something the majority of malware, both on Windows and Mac, strives to achieve. So what we can do is we can simply monitor the file system, specifically observing or monitoring the persistence locations. And then if anything modifies those we can alert the user or assist admin and say hey look something just persisted maybe you want to take a closer look at it. So here for example, we can now alert and generically detect when Wintail, either the original or the repurposed item, persist. Next up is mic and webcam access. A lot of Mac malware accesses the webcam or the mic to in spot to spy on the users of the system. So here we're going to use our an infected system. So to detect this subversive activity we can register with MacOS. It turns out there is a callback you can register that anytime anybody accesses either the mic or the webcam you will programmatically get notification of that fact. So this will allow us to detect malware even something like fruit fly which we've repurposed or an exploit like zoom anytime it accesses the mic or the camera. Next up we have key logger detection. Malware often times tries to capture your keystrokes to steal things like passwords and credit card information. On MacOS the easiest way to capture keystrokes is by registering something called an event tap. What we can do is we can generically detect these taps by again registering for a dispatch notification that will be kindly invoked by the operating system anytime a new event tap is installed. This allows us to very trivially detect key loggers even if they have been repurposed. We talked a lot about fruit fly this is one of the more sophisticated feature complete malware. One of the neat features it has is the ability to remotely interact with the UI via synthetic or programmatic mouse and keyboard events. So the attacker can move the mouse, click on buttons or even send program matic keyboard events to an infected system. So this is something we want to detect because this isn't something that should normally be happening on a Mac system. Luckily it's pretty easy to detect. We can install our own event tap and monitor for mouse activity. When a mouse click is generated we can then examine a variable, a state variable, which MacOS sets to one if the mouse event the click or the move has been program matically generated. So this allows awesome allows us to now generically detect synthetic clicks, even in repurposed malware. Now at the start of the talk I mentioned I was the creator of Objective-C, this is my kind of personal Mac security webpage, but the main goal of Objective-C is to provide free and largely open source security tools for end users who are using Macs. Uh, many of the detections we talked about are realized in tools on this site, so if you're interested in that, hop over to Objective-C dot com. Now, a downside of these tools is they're all separate, right? There's an individual key logger detector, there's an individual firewall, there's not like an all in one solution. So when I started working at Digita we decided to combine these into a single all in one tool. Uh, and the idea was to monitor the system and then create rules to detect unusual behavior. We then took both those pieces and fed them into Apple's built in game logic engine, which would do all the hard work for us and would evaluate our rules on top of our data and spit out a result for us. So now let's talk about some of these behavior based detection rules. And it's important to note you can use these in other security tools as well, so this is not specific to our product per se. So using this system we can generically and rather trivially detect all the repurposed malware we talked about today. Uh, so for example, Wintel, we can detect it's exploitation vector by observing the fact that Safari downloads and then auto extracts an application. That same application then triggers the registration of a custom URL handler, and finally then that same application is started automatically via custom URL request from a website. So detecting the combination of the two events gives us a generic capability, both against Wintel or any other malware that utilizes this same infection vector. Similarly, we can detect FruitFly at install time by observing various events that cumulatively indicate something is suspicious or at least amiss. So here for example, we observe the fact that a hidden non-Apple binary has persisted as a launch item. There's no legitimate reason that this should ever happen. So we can use this as a way to see in pure consistency what could happen. So that's really shady. Now imagine that FruitFly is already running on the system. Uh, we can also detect this by observing run-time behaviors. For example, FruitFly does things that are really shady and suspicious. For example as we mentioned persisting and running a hidden process. This hidden process in some scenarios will drop an im- embedded unsigned binary to the temp directory, execute that and then start capturing or generating synthetic events. Clearly this is ineffective by default. faculty related questions, incredibly shady. Now of course we don't have to trigger when all these events occur. Uh we can look at them either cumulatively, we see one or two we might want to take a closer look at the system because something might be amiss. So hopefully I've illustrated that by using these behavioral heuristics we can generically detect even sophisticated MAC uh threats even those that have been repurposed by advanced adversaries or by Patrick. Now before I end this talk I'm really excited to announce Objective by the Sea version 3.0. Some of you are aware uh Objective by the Sea is a MAC security conference uh that I organize each year. Uh this year it'll be back in Hawaii so if you want an excuse for your company to send you to Hawaii in February go to Objective Sea dot com. Well that is a wrap. Thank you so much for watching and I'll see you next time. Bye!