1 00:00:03,840 --> 00:00:11,100 Thank you, uh, aloha and welcome to my talk, Nothing But Nets. Yes, I am Patrick Wardle. I am 2 00:00:11,100 --> 00:00:16,120 the founder of the Objective-C Foundation. Uh, you might know me from some of the, uh, 3 00:00:16,240 --> 00:00:20,980 Objective-C free open source Mac security tools. I also write the Art of Mac Malware book 4 00:00:20,980 --> 00:00:28,060 series. I finally organized Objective by the Sea security conference. So, what will you learn 5 00:00:28,060 --> 00:00:32,080 today? What are we gonna talk about? What are we gonna dive into? Well, pretty much four 6 00:00:32,080 --> 00:00:37,740 things that really, in a nutshell, will show us how to leverage, uh, Mac OS's, Apple's 7 00:00:37,740 --> 00:00:44,040 networking frameworks to heuristically detect malware directly on, uh, a host. So, we're 8 00:00:44,040 --> 00:00:47,760 gonna start by looking at some examples of Mac malware that utilize the network because, 9 00:00:47,860 --> 00:00:52,340 well, before we write tools to detect such activity, we should have at least a baseline 10 00:00:52,340 --> 00:00:57,620 understanding of, well, what we're looking for. We're then gonna talk about how to take a 11 00:00:57,620 --> 00:01:02,300 snapshot, enumerate the network state on a Mac to see things like active connections, open 12 00:01:02,300 --> 00:01:07,860 sockets, etc. We're then gonna kinda graduate up to more comprehensive tools that will allow us to 13 00:01:07,860 --> 00:01:13,480 continually monitor the network. Both DNS traffic and all traffic, including filtering and 14 00:01:13,480 --> 00:01:19,080 blocking certain connections. And then finally, we're gonna end with how to then tie that 15 00:01:19,080 --> 00:01:24,060 activity back to processes and determine if those processes are suspicious or malicious 16 00:01:24,060 --> 00:01:27,600 because, you know, obviously legitimate software is also gonna be used to detect malware. So, 17 00:01:27,620 --> 00:01:35,860 we need a way to identify what is unauthorized. Now, this is the main idea, and this is a very 18 00:01:35,860 --> 00:01:40,140 obvious idea, so I'm not claiming this is new, and it's just let's detect malware via 19 00:01:40,140 --> 00:01:45,140 unauthorized network activity. This is based on two, again, very obvious, uh, observations, 20 00:01:45,460 --> 00:01:51,040 which is first, malware uses the network. How? Good question. In a variety of ways. For 21 00:01:51,040 --> 00:01:57,300 example, it pops reverse shells. Uh, it might use the network to propagate to other machines. A lot 22 00:01:57,300 --> 00:02:01,480 of malware, especially backdoors, will interact with a command and control server and take 23 00:02:01,480 --> 00:02:06,140 tasking from a remote server. And then a lot of malware will also, for example, download 24 00:02:06,140 --> 00:02:11,380 additional payloads or files, or exfiltrate up, uh, files to, uh, a remote server. So, 25 00:02:11,380 --> 00:02:16,760 again, a lot of opportunities for detecting the malware based on its network traffic. So, the 26 00:02:16,760 --> 00:02:23,120 idea is, if we can observe this network activity and classify it as suspicious or unauthorized, we 27 00:02:23,120 --> 00:02:25,420 should be able to generically uncover malware. 28 00:02:26,500 --> 00:02:33,160 Now, the way, uh, network monitoring is normally done is via placing an application ne- uh, 29 00:02:33,300 --> 00:02:37,720 network application in the network, especially in the context of the enterprise. So, this is a 30 00:02:37,720 --> 00:02:42,020 separate hardware device that all traffic from the network may be routed through, and it can 31 00:02:42,020 --> 00:02:46,320 then examine and look for anomalies. And this definitely works, there's some pros to this 32 00:02:46,320 --> 00:02:50,960 approach, but there's definitely some cons. So, this talk, instead, we're gonna focus on 33 00:02:50,960 --> 00:02:55,120 host-based network monitoring, meaning our code is going to run 34 00:02:55,420 --> 00:03:01,080 directly on the host. The benefits of this are threefold. First, and most importantly, is the 35 00:03:01,080 --> 00:03:05,920 fact that, if we are running on the host, where we are observing network traffic, we will be able to 36 00:03:05,920 --> 00:03:11,640 identify the responsible process. Imagine you are a network appliance, you see a DNS request, you 37 00:03:11,640 --> 00:03:16,060 might not be able to say what application generated this, right? Is this the user's browser or a 38 00:03:16,060 --> 00:03:20,620 malicious backdoor? Well, if our code is running on the host and we observe that same DNS 39 00:03:20,620 --> 00:03:24,980 request, we will able, we will be able to pinpoint or tie that back to the 40 00:03:24,980 --> 00:03:29,760 process, and then analyze that process to answer the question, is it malicious or not? So, 41 00:03:29,800 --> 00:03:35,380 that's really kind of the main benefit of host-based network monitoring. It also happens 42 00:03:35,380 --> 00:03:38,880 pre-encryption. So, if you want to do full packet capturing, well, you don't have to worry 43 00:03:38,880 --> 00:03:44,340 about removing SSL certificates, manning the middle, it's just there before it gets encrypted to 44 00:03:44,340 --> 00:03:49,040 go out on the wire. And then thirdly, it's more simple and maybe more cost effective. You don't 45 00:03:49,040 --> 00:03:54,320 need to buy any additional hardware. Uh, and especially recently, a lot of these hardware network 46 00:03:54,320 --> 00:03:58,540 appliances have security vulnerabilities that hackers are actually using to get into the 47 00:03:58,540 --> 00:04:03,760 network. So, not ideal. Now, you might be thinking, okay, cool, host-based network 48 00:04:03,760 --> 00:04:07,940 monitoring, it's pretty well understood, some good pros. Why are we giving a talk about it? 49 00:04:07,980 --> 00:04:13,400 Well, a few main reasons. One of them is, on Mac OS, there's just really not been a lot of 50 00:04:13,400 --> 00:04:18,080 research or examples of how to do this. And there's a variety of reasons for this. One of the 51 00:04:18,080 --> 00:04:24,060 main reasons is a lot of the frameworks and, uh, capabilities we're gonna use are only, uh, 52 00:04:24,060 --> 00:04:29,480 pretty new. They've only been recently introduced by Apple. There's also a lot of challenges, 53 00:04:29,520 --> 00:04:34,320 blood, sweat, and tears that I've been through the last few years developing host-based 54 00:04:34,320 --> 00:04:39,060 networking tools on Mac OS. So I want to kind of share some of the pain points so y'all don't 55 00:04:39,060 --> 00:04:45,580 have to. So, one example is Apple's networking frameworks used to be incredibly buggy. So, you 56 00:04:45,580 --> 00:04:52,100 write this lovely user mode tool that leverages Apple's documented frameworks and then the user 57 00:04:52,100 --> 00:04:54,040 runs it and it panics the box. One of the reasons for that is, you know, if you have a 58 00:04:54,060 --> 00:04:57,380 bug, what do they do? They file a bug report saying your tool is crap. And you're like, is it? 59 00:04:57,500 --> 00:05:01,700 It runs in user mode. How can it panic the box? And you look into it and it turns out that Apple 60 00:05:01,700 --> 00:05:07,000 has a kernel bug in their networking logic. And you're like, thanks, Cupertino. Now, Apple is 61 00:05:07,000 --> 00:05:11,960 pretty receptive about fixing this, but again, an example of, you know, kind of, uh, uh, uh, 62 00:05:11,960 --> 00:05:16,640 trials and tribulations, let's just say. Other challenges, uh, a lot of the frameworks are 63 00:05:16,640 --> 00:05:20,800 private. So, you have a very powerful networking framework you want to use, but there's no 64 00:05:20,800 --> 00:05:24,040 documentation. There's no source code. So you have to go through and reverse engineer it. You 65 00:05:24,060 --> 00:05:27,360 have to go through and reverse engineer it. Also a pain. And then finally, the documentation, 66 00:05:27,720 --> 00:05:33,400 super lacking. The other thing that Apple originally did, which was really not cool, was that 67 00:05:33,400 --> 00:05:38,740 they actually neutered capabilities themselves. So, story time, you used to run network 68 00:05:38,740 --> 00:05:42,640 monitoring tools that ran directly in the kernel. Uh, this makes sense. It's where the 69 00:05:42,640 --> 00:05:47,600 networking stack is. Apple said, mmm, we don't want third party kernel extensions anymore in 70 00:05:47,600 --> 00:05:51,980 Mac OS, so we're gonna kick you all out of the kernel. But don't worry, we've introduced some 71 00:05:51,980 --> 00:05:57,160 new frameworks that your code can link against and utilize to give you the same 72 00:05:57,160 --> 00:06:00,940 capabilities. And I was like, okay, this is actually kind of neat. Writing kernel code is a 73 00:06:00,940 --> 00:06:05,140 pain in the butt. If there's a bug in it, it's gonna panic the box. So, yes, I would rather write 74 00:06:05,140 --> 00:06:10,400 a tool that runs in user mode if it gives me the same capabilities as before. What Apple didn't 75 00:06:10,400 --> 00:06:17,160 tell anybody is they exempted a large number of processes that would not be routed through these 76 00:06:17,160 --> 00:06:21,880 user mode networking frameworks. So imagine you wanted to write a firewall that maybe 77 00:06:21,980 --> 00:06:27,440 blocked all traffic. Yeah, good luck, because Apple's system processes would be exempted from 78 00:06:27,440 --> 00:06:33,100 that. And again, they did this without telling anybody, which in my opinion is, well, wrong. So we 79 00:06:33,100 --> 00:06:37,000 reversed engineer and figured this out and called Apple out. Turned out it was trivial to 80 00:06:37,000 --> 00:06:43,720 leverage these trusted processes to generate network activity on your own behalf. So once we 81 00:06:43,720 --> 00:06:48,540 created a proof of concept that was able to bypass this and reported it to Apple, they said, 82 00:06:48,540 --> 00:06:51,780 no, this is functionality by design. Went to the 83 00:06:51,780 --> 00:06:56,860 press, made some noise, made Apple look bad. They immediately fixed it. So a takeaway, if you 84 00:06:56,860 --> 00:07:03,840 want to enact change at Apple, make them look bad. The unfortunate reality. So now as I mentioned, 85 00:07:03,920 --> 00:07:09,100 let's look at some Mac malware, ones that access the network to get a good understanding of the 86 00:07:09,100 --> 00:07:14,260 kind of activity we're gonna look for. The first malware sample I want to talk about is called 87 00:07:14,260 --> 00:07:19,600 Dummy. I named it Dummy because it's pretty dumb, uh, but it's simple and effective in a way. So 88 00:07:19,600 --> 00:07:21,760 maybe that's a kind of a harsh name. We can 89 00:07:21,780 --> 00:07:27,280 see on the slide, it's basically a bash script that executes various Python commands that do 90 00:07:27,280 --> 00:07:34,280 three things. Connect out to the attacker's remote server, redirect standard in out error, and 91 00:07:34,280 --> 00:07:39,140 then spawn an interactive shell that's tied to those sockets. And again, because the file 92 00:07:39,140 --> 00:07:43,840 descriptors for standard in and error have been redirected to the socket, this gives the remote 93 00:07:43,840 --> 00:07:48,100 attacker a reverse shell on the infected system. So again, it's kind of a standard vanilla 94 00:07:48,700 --> 00:07:49,340 reverse shell. 95 00:07:51,780 --> 00:07:56,320 Another Mac malware that uses the network is IPStorm. IPStorm is interesting for a variety of 96 00:07:56,320 --> 00:08:02,160 reasons. One of the reasons is it was originally written on Linux. As Macs become more popular, more 97 00:08:02,160 --> 00:08:07,600 prevalent, especially in the enterprise, we're seeing a trend of malware authors and adversaries 98 00:08:08,360 --> 00:08:13,540 porting or recompiling their malicious creations to run natively on Mac OS. IPStorm is a great 99 00:08:13,540 --> 00:08:20,900 example of that. So hackers are definitely taking notice of Mac OS's increased popularity. Now as 100 00:08:20,900 --> 00:08:26,220 we can see on the slide, the quote from the company that did the initial analysis, the malware 101 00:08:26,600 --> 00:08:31,960 would propagate from one system to the next by SSH brute force attacks. Uh, so the idea is if it 102 00:08:31,960 --> 00:08:36,160 could guess the password or brute force in, it would then infect the system. So it had some worm 103 00:08:36,160 --> 00:08:41,340 like capabilities. Again, this network activity, if we can uh, observe that, maybe that's an 104 00:08:41,340 --> 00:08:47,820 opportunity to detect this malware. Once IPStorm successfully infected a system, it would then 105 00:08:47,820 --> 00:08:50,880 open a reverse shell. So again, network activity. 106 00:08:50,900 --> 00:08:57,060 that we could perhaps use to detect uh, the infection. Another recent Mac malware specimen 107 00:08:57,060 --> 00:09:02,400 that leverages the network is GoRat. GoRat is a pretty standard but fairly feature complete 108 00:09:02,400 --> 00:09:07,520 backdoor for Mac OS. I've listed its capabilities. You can see it things, it does things 109 00:09:07,520 --> 00:09:12,120 like upload files, download files, pretty standard. However, it does have some additional 110 00:09:12,120 --> 00:09:17,920 capabilities, which we don't commonly see in Mac malware, that allow it to interact with other 111 00:09:17,920 --> 00:09:20,720 systems via the network. For example, it has the ability 112 00:09:20,900 --> 00:09:24,640 to generate a port scan. So again, if we're interested in detecting malware via network 113 00:09:24,640 --> 00:09:29,200 activity, if the malware is generating port scans of remote systems, maybe something, uh, that's 114 00:09:29,200 --> 00:09:33,840 something we can pick up on. It also has the ability to set up tunnels and proxies, more 115 00:09:33,840 --> 00:09:39,580 network activity to, for us to detect. It's also worth talking about supply chain attacks. 116 00:09:39,840 --> 00:09:44,640 These are becoming unfortunately ever more prolific. And again, the adversaries are now 117 00:09:44,640 --> 00:09:50,640 starting to target Mac OS systems. Normally, supply chain attacks mostly impacted, uh, 118 00:09:50,900 --> 00:09:56,240 Windows systems. A great example of this, of a supply chain attack attacking and targeting Mac 119 00:09:56,240 --> 00:10:03,560 systems is 3CX. Uh, what happened is an APT group associated with the North Koreans broke into 120 00:10:03,560 --> 00:10:10,420 3CX, which is a popular PBX software for, uh, the enterprise. They installed a Mac OS backdoor 121 00:10:10,420 --> 00:10:18,440 on 3CX's build server and Trojanized 3CX's desktop application. This meant when users around the 122 00:10:18,440 --> 00:10:23,720 world downloaded the update, they would inadvertently get infected. One thing to note is 123 00:10:23,720 --> 00:10:28,520 before the adversaries did this, uh, after they infected the installer, but before they 124 00:10:28,520 --> 00:10:33,240 distributed it, they actually sent it to Apple. And this is because on recent versions of Mac OS, 125 00:10:33,400 --> 00:10:37,780 software has to be notarized, which means Apple has scanned it and given it their stamp of 126 00:10:37,780 --> 00:10:42,480 approval before it will run. So the attackers took a little bit of a gamble here, essentially 127 00:10:42,480 --> 00:10:48,320 sending the infected installer to Cupertino. Apple happily notarized it. Less than ideal. 128 00:10:48,440 --> 00:10:55,340 So how was 3CX, how was the supply chain found? Because again, super stealthy, uh, and the 129 00:10:55,340 --> 00:11:00,020 payload is, you know, signed and notarized. Well it turns out, via network traffic. So as we can 130 00:11:00,020 --> 00:11:05,360 see on the slide from the 3CX forums, the user basically said, hey, one of my EDR products 131 00:11:05,360 --> 00:11:11,800 noticed anomalous DNS requests gen- generated from the 3CX application. So again, this kind of 132 00:11:11,800 --> 00:11:15,960 drives home the point of the power of network detection and especially in the context of supply 133 00:11:15,960 --> 00:11:18,420 chain attacks, this is probably the best approach you can give to a 3CX user. Uh, the 134 00:11:18,440 --> 00:11:24,120 best approach for us to detect intrusions. Speaking of best approaches, also worth 135 00:11:24,120 --> 00:11:29,860 mentioning iOS. Now this talk is predominantly about Mac OS, but a lot of the techniques we 136 00:11:29,860 --> 00:11:35,220 talk about, the frameworks also exist on iOS. So it would be not that difficult to write similar 137 00:11:35,220 --> 00:11:42,800 capabilities on iOS. Now to detect malware on iOS, you're basically SOL, because iOS is so 138 00:11:42,800 --> 00:11:48,100 locked down. On a non-GL broken iPhone, you can't even get a list of running processes. So like 139 00:11:48,440 --> 00:11:52,940 how are you going to detect malware? Unfortunately, nation states have zero-click, zero-day 140 00:11:52,940 --> 00:11:57,360 vulnerabilities that allow them to infect iOS devices by merely knowing someone's phone 141 00:11:57,360 --> 00:12:02,820 number. Uh, so anywhere in the world they can hit you. And once they're in, yes that's a hard 142 00:12:02,820 --> 00:12:07,700 target to get into, but they can actually leverage the security of that system as a protection 143 00:12:07,700 --> 00:12:12,900 mechanism. For example, I just mentioned you can't run a list- a process listing, so the 144 00:12:12,900 --> 00:12:17,980 adversaries, once they're in, they're like happy. An example of this is Kaspersky and the Tri- 145 00:12:17,980 --> 00:12:23,760 angulation, uh, malware. This recently came out, where an unknown adversary hacked into 146 00:12:23,760 --> 00:12:29,340 corporate and employee devices, uh, of Kaspersky, the Russian antivirus company, uh, and 147 00:12:29,340 --> 00:12:34,620 remained undetected for four years, existing on their iOS devices. That is insane. Kaspersky is 148 00:12:34,620 --> 00:12:40,680 one of the top security EDR AV companies in the world. They did not detect, uh, an infection on 149 00:12:40,680 --> 00:12:44,900 their own systems for four years, and that's not their fault per se. How did they eventually 150 00:12:44,900 --> 00:12:47,860 detect it? Network traffic, as we can see on the slide. 151 00:12:47,980 --> 00:12:53,640 So, again, if you're doing malware analysis on iOS, or malware detection rather, network is 152 00:12:53,640 --> 00:12:57,820 really the place you want to be, and perhaps the only way you're going to detect, especially these 153 00:12:57,820 --> 00:13:03,940 more stealthy and advanced, uh, attacks. Okay, so that gives us an overview of macOS 154 00:13:03,940 --> 00:13:09,060 malware, hopefully illustrated that we really want to be looking at network activity, uh, 155 00:13:09,120 --> 00:13:13,560 because again, most of the malware is going to generate something that's observable. So now 156 00:13:13,560 --> 00:13:17,780 let's talk about how to, uh, enumerate this network activity, starting by 157 00:13:17,980 --> 00:13:24,840 using file descriptors. The idea is pretty basic, and that is on macOS, very similar to BSD and 158 00:13:24,840 --> 00:13:29,540 some other Linux-y like operating systems. File descriptors are kind of everything, meaning 159 00:13:29,540 --> 00:13:34,560 sockets themselves are also file descriptors. So what we're going to do is we simply query the 160 00:13:34,560 --> 00:13:40,100 operating system, we specify the process ID of a process we're interested in, and the operating 161 00:13:40,100 --> 00:13:46,540 system will give us a list of file descriptors that that process has opened. What we can then do 162 00:13:46,540 --> 00:13:52,420 is iterate through that, focusing only on sockets, right, we don't really care about normal file 163 00:13:52,420 --> 00:13:56,460 descriptors, and for those sockets then we can query the operating system again to get 164 00:13:56,460 --> 00:14:02,200 information about those. So here's the example code, I'm not going to walk through the code 165 00:14:02,200 --> 00:14:07,000 because I don't think you want me up here like going line by line, but, you know, I wanted to put 166 00:14:07,000 --> 00:14:12,080 this up here for, uh, you know, like as a resource, so later you can go back and, and check it 167 00:14:12,080 --> 00:14:16,400 out. But I'll briefly describe some of it. So on the bottom what we're doing is we're invoking the 168 00:14:16,540 --> 00:14:23,060 proc pid info API, we invoke this first with a pid, uh, the process we're interested in file 169 00:14:23,060 --> 00:14:27,860 descriptors, uh, and then a bunch of nulls, the operating system will return to us the size of the 170 00:14:27,860 --> 00:14:33,100 buffer we need to allocate to store the file descriptors in memory. Uh, we allocate that 171 00:14:33,100 --> 00:14:38,200 buffer, call that API again with the size and our newly allocated buffer, and voila, we get a 172 00:14:38,200 --> 00:14:43,320 list of the file descriptors that the process has opened. Again, this is going to include normal 173 00:14:43,320 --> 00:14:45,000 files, but also sockets. 174 00:14:46,540 --> 00:14:50,080 Now what we're going to do is we're going to iterate through all of those file descriptors and 175 00:14:50,080 --> 00:14:54,340 we're going to focus on those that are just sockets. So you can see again some example code 176 00:14:54,340 --> 00:15:01,740 that implements this. For each file descriptor that is a socket, we then call the proc pid fd info 177 00:15:02,180 --> 00:15:09,720 with that socket file descriptor to get socket specific information. From that, we can now examine 178 00:15:09,720 --> 00:15:15,100 the socket. The first thing we do is we make sure it's a networking socket. On Mac OS you can also 179 00:15:15,100 --> 00:15:20,460 have Unix sockets, which are local sockets used for IPC mechanisms. We don't care about those in 180 00:15:20,460 --> 00:15:26,880 the context of detecting Mac malware. So you can see on the slide we focus on AF-Inet or AF-Inet 6, 181 00:15:27,020 --> 00:15:33,620 IPv4, or 6 sockets only. We compile this code and run it. I run it against, uh, the GitHub 182 00:15:33,620 --> 00:15:38,880 desktop application that's running on my system. We can see it found 70 file descriptors and has 183 00:15:38,880 --> 00:15:45,080 filtered out those that are sockets, which is a very small subset. Why am I using GitHub desktop? Great question. 184 00:15:45,100 --> 00:15:50,840 Uh, it's obviously a legitimate application. This is not malware. It's easy to test with. It has a lot of 185 00:15:50,840 --> 00:15:55,620 legitimate connections. I always like to test my code on legitimate software before trying it on 186 00:15:55,620 --> 00:16:02,500 malware. And also, re-illustrates the point that legitimate applications obviously are gonna use 187 00:16:02,500 --> 00:16:09,340 the network, uh, as well. So now that we have a network socket, we can start extracting relevant 188 00:16:09,340 --> 00:16:14,960 information from that. If it's a UDP socket, there's not a ton of information because it's 189 00:16:15,100 --> 00:16:21,700 stateless. But we can at least, for example, extract the local port. For TCP sockets, there's tons 190 00:16:21,700 --> 00:16:26,360 more information available to us. Again, not gonna walk through all this, kind of some boring code, 191 00:16:26,620 --> 00:16:31,620 but you can see we're able to extract, for example, the information about the remote endpoint. 192 00:16:32,040 --> 00:16:36,440 The remote, uh, remote IP address and remote port. Clearly you can see if we're trying to detect 193 00:16:36,440 --> 00:16:40,680 malware, this is useful, right? Where is this application talking to? Is it a command and control 194 00:16:40,680 --> 00:16:44,780 server in North Korea? Okay, that's a big red flag. Or is it just some well-known 195 00:16:45,100 --> 00:16:51,220 trusted endpoint? So if we compile and run this against, uh, GitHub desktop, now since we're 196 00:16:51,220 --> 00:16:56,200 extracting all the socket information, we can see all the information about GitHub desktop's 197 00:16:56,200 --> 00:17:02,360 network activity. Uh, so socket details such as the family, the port, the protocol, remote, uh, 198 00:17:02,420 --> 00:17:08,980 local information, et cetera, et cetera. So now if we run this code against, uh, or on a system 199 00:17:08,980 --> 00:17:14,780 that is infected with OS dummy, OXX dummy, we can see that this code picks up the fact that 200 00:17:14,780 --> 00:17:22,240 there is a established connection on port 1337, yes, this is the port the malware uses, uh, to 201 00:17:22,240 --> 00:17:26,660 some remote, uh, command and control server. And you're like, okay, that's, that's interesting, 202 00:17:26,780 --> 00:17:32,620 now we can determine that, but, you know, dummy as we know uses Python to set up the reverse shell. 203 00:17:32,800 --> 00:17:38,920 So, the question is, how can I tell from solely this network activity that this instance of 204 00:17:38,920 --> 00:17:44,740 Python is being used maliciously? Great question, we'll get to that shortly. Now let's 205 00:17:44,780 --> 00:17:49,880 talk about enumerating network state using the private network statistics framework. And the 206 00:17:49,880 --> 00:17:55,040 reason I wanna talk about this other way is that it has some additional pros over the method we 207 00:17:55,040 --> 00:17:59,940 just talked, the file descriptor method. So via the procpid API method that we just talked 208 00:17:59,940 --> 00:18:05,500 about, a few kind of perhaps downsides to it, it's process specific, so you only get the file 209 00:18:05,500 --> 00:18:10,300 handles for the process you specify. Of course you can just enumerate all the processes and 210 00:18:10,300 --> 00:18:14,760 make the requests for each one, but that's some additional code. Also, we get all the 211 00:18:14,780 --> 00:18:19,380 file descriptors, so we had to add additional code to filter out the sockets. Again, totally 212 00:18:19,380 --> 00:18:25,840 doable, but extra code. Most notably though, it doesn't provide us any statistics. Like, I want to 213 00:18:25,840 --> 00:18:31,560 know how much data, how many bytes, uh, a network connection is responsible for. Uh, for 214 00:18:31,560 --> 00:18:36,380 example, is an application exfiltrating gigs of data? That's super interesting. We cannot 215 00:18:36,380 --> 00:18:42,860 determine that from file handles, file descriptors alone. So, what do we do? Well, it turns 216 00:18:42,860 --> 00:18:44,760 out there's a very interesting utility that's built into Python. It's called the file descriptor 217 00:18:44,780 --> 00:18:50,460 built into Mac OS. It's an Apple utility, uh, called NetTop. And if you execute it, it gives us 218 00:18:50,460 --> 00:18:56,300 all the information we're looking for. So it's global, so it's for every process. Uh, it's just 219 00:18:56,300 --> 00:19:00,960 sockets, so we don't have to worry about other files. And also it provides statistics, how many 220 00:19:00,960 --> 00:19:06,540 bytes up and down, uh, each connection is responsible for. Unfortunately, this utility is 221 00:19:06,540 --> 00:19:13,220 closed source, uh, so we can't utilize it pro- programmatically very easily. But if we look at 222 00:19:13,220 --> 00:19:14,760 its dependencies, the life cycle, uh, we can't use it. Uh, we can't use it. Uh, we can't use it. Uh, 223 00:19:14,760 --> 00:19:15,180 we can't use it. So we can't use it. So we can't use it. But if we look at libraries, it's 224 00:19:15,180 --> 00:19:20,640 linked against, and we can do that with the O tool utility, we can see that it's linked against a 225 00:19:20,640 --> 00:19:26,940 private framework called network statistics dot framework. Now, the well regarded noted 226 00:19:26,940 --> 00:19:32,200 security researcher Jonathan Levin actually fully reverse engineered this framework and wrote a 227 00:19:32,200 --> 00:19:38,820 nice open-source command line utility leveraging this, uh, network private framework. So you can 228 00:19:38,820 --> 00:19:44,740 check that out. I love that but I wanted a UI version. I wanted a tool that a user would be, you 229 00:19:44,740 --> 00:19:49,440 very easily able to double click and see all network activity on their system. So the first 230 00:19:49,440 --> 00:19:53,960 tool I want to talk about today is called Netiquette. It's built on top of the private 231 00:19:54,320 --> 00:19:59,060 network statistics framework and it's a nice UI. It's a pretty simple tool but it will show 232 00:19:59,060 --> 00:20:04,300 you all the network activity on your system, a snapshot of that. Uh kind of organized by 233 00:20:04,300 --> 00:20:10,120 process. You can also see information as you would expect about the interface it's bound to, 234 00:20:10,120 --> 00:20:15,420 the state of the connection, but yes also bytes up and down. If you're interested in learning 235 00:20:15,420 --> 00:20:20,500 more about this tool, pop over to the Objective-C website, it's free, fully open source. But 236 00:20:20,500 --> 00:20:27,740 let's look briefly at some of the code uh behind it. The code that interacts with the private 237 00:20:27,740 --> 00:20:32,840 network statistics framework because it's actually pretty straight forward. So the first thing 238 00:20:32,840 --> 00:20:38,400 you have to do is create a nstat manager, a network statistics manager. You allocate that. 239 00:20:38,560 --> 00:20:40,100 Once that has been created, you have to create a network statistics manager. Once that has been 240 00:20:40,100 --> 00:20:46,280 created, you set a callback block via the nstat sort set description block function. And we'll 241 00:20:46,280 --> 00:20:50,540 get to that in a second. Then you tell the operating system that framework that you're 242 00:20:50,540 --> 00:20:56,700 interested in all TCP and all UDP connection. There are uh APIs for that. And then what you do 243 00:20:56,700 --> 00:21:02,300 is you start a query. This tells the operating system to call the API in the framework and 244 00:21:02,300 --> 00:21:08,800 enumerate all current network activity. And for each connection it will invoke the callback block 245 00:21:08,800 --> 00:21:15,320 that you've set. So here we just call NS log which will print out information about that. We then 246 00:21:15,320 --> 00:21:20,420 link in the network statistics framework and run it. What information do we get for each 247 00:21:20,420 --> 00:21:25,120 connection? A ton which is a great. Detecting malware? The more information we have, the 248 00:21:25,120 --> 00:21:31,740 better. So here are the details from one single connection from GitHub desktop. Little hard to 249 00:21:31,740 --> 00:21:36,280 read that's kinda by design just to show you how much information this private framework gives 250 00:21:36,280 --> 00:21:38,780 you again for every connection on the network. So if I want to run a network, I need to run an 251 00:21:38,800 --> 00:21:43,280 host. You can see the responsible process. Again this is something we really are interested in. 252 00:21:43,340 --> 00:21:47,980 Is it malware or is it you know GitHub desktop? We also get the transmitted bytes up and 253 00:21:47,980 --> 00:21:53,400 down. And then if it's a TCP connection a ton of information uh about the connection as well. 254 00:21:53,500 --> 00:21:59,660 So a really powerful framework to use. Now there's always pros and cons to everything so I 255 00:21:59,660 --> 00:22:03,980 always like to point out some of the downsides. And the biggest downsides to the two 256 00:22:03,980 --> 00:22:10,540 approaches we've talked about so far are that they only provide you a snapshot of the current 257 00:22:10,540 --> 00:22:17,400 state of the network on the host. Why is this problematic? Well if malware is sleeping or only 258 00:22:17,400 --> 00:22:22,720 beacons out once an hour and you perform your snapshot of network activity between that you 259 00:22:22,720 --> 00:22:26,920 will not see the network activity associated with the malware. And this is actually very 260 00:22:26,920 --> 00:22:33,660 common. For example both dummy and some of the 3CX payloads utilize sleep or beacon or 261 00:22:33,660 --> 00:22:39,340 connect out on certain intervals. So again perhaps our tools might miss them. Now of course you 262 00:22:39,340 --> 00:22:44,500 could take a snapshot every 5 seconds but that might be somewhat resource intensive and also you 263 00:22:44,500 --> 00:22:50,620 still might miss the malware. So what we need to do now is kind of graduate up and talk about 264 00:22:50,620 --> 00:22:57,080 more comprehensive tools that allow us to continually monitor the network. So at any time if 265 00:22:57,080 --> 00:23:03,220 the network or a malicious piece of software tries to access the network in an unauthorized manner 266 00:23:03,660 --> 00:23:09,100 we will be there to observe it. So let's start by talking about how to build a DNS monitor. 267 00:23:09,460 --> 00:23:15,580 That's gonna run natively on macOS fully in user mode. The idea is pretty straight forward. The 268 00:23:15,580 --> 00:23:20,540 majority of malware regardless of how it gets onto your system is going to connect out to a remote 269 00:23:20,540 --> 00:23:26,520 server and most malware does that via domain which means it's first going to have to resolve the 270 00:23:26,520 --> 00:23:32,880 name. Sure we all know how DNS works but a quick recap. The malware tries to connect out. This 271 00:23:32,880 --> 00:23:37,940 generates a DNS request to the DNS server. The DNS server returns the IP address and then the 272 00:23:37,940 --> 00:23:44,540 malware connects out to the IP address. So the question is, is this a good approach to detect 273 00:23:44,540 --> 00:23:49,460 malware? And I think the answer is yes it's very efficient because again most malware is gonna be 274 00:23:49,460 --> 00:23:54,460 generating DNS requests. We don't have to monitor all network traffic and in the case of some very 275 00:23:54,460 --> 00:24:01,140 stealthy malware attacks like the 3CX one this was actually how the malware was detected in the 276 00:24:01,140 --> 00:24:02,700 first place. Anomalous DNS. 277 00:24:02,880 --> 00:24:09,820 So how do we build a DNS monitor for macOS? More generically how do we build these monitoring 278 00:24:09,820 --> 00:24:16,080 tools that allow us to continually monitor the network? The answer is to use Apple's network 279 00:24:16,080 --> 00:24:22,940 extensions framework. This is a pretty new framework that Apple uh now affords us to use. Uh I 280 00:24:22,940 --> 00:24:28,020 believe originally it was available on iOS but now you can utilize it on macOS. And like some of the 281 00:24:28,020 --> 00:24:32,860 other frameworks and APIs I've talked about this is actually decently documented. So kudos to Apple 282 00:24:32,880 --> 00:24:37,620 here. You can read on Apple's site all the cool things you can do with network extensions. 283 00:24:37,820 --> 00:24:43,460 They're actually incredibly powerful. We're gonna focus on the last two uh specifically how to 284 00:24:43,460 --> 00:24:49,180 intercept DNS traffic and then we're gonna talk at the very end about how to filter all network 285 00:24:49,180 --> 00:24:54,700 traffic. So again we can use network extensions to do both of these. There's a great WWDC video 286 00:24:54,700 --> 00:25:02,800 where Apple explains exactly how to use these. Have a watch. So in order to build a DNS uh monitor 287 00:25:02,880 --> 00:25:10,120 we actually are going to use a DNS proxy class. Uh and that is the NEDNS proxy manager class. 288 00:25:10,340 --> 00:25:15,400 So I saw this in the documentation and it says hey this will allow your application to intercept 289 00:25:15,400 --> 00:25:23,740 all DNS traffic. Perfect. How do I do this? So I write code by googling things. Uh and so I just 290 00:25:23,740 --> 00:25:28,540 googled it and there was like two hits. The first one was Apple's developer documentation that 291 00:25:28,540 --> 00:25:32,580 kind of described the APIs but not much more. And then the second hit was 292 00:25:32,880 --> 00:25:38,540 like hey y'all how do we do this? So I was like ah well other people are in the same boat as me. 293 00:25:39,020 --> 00:25:43,720 There's no example code. There's no great documentation. So this could also be a learning 294 00:25:43,720 --> 00:25:48,940 experience for me. This is a tool I want to write anyways. Uh so I saw that there would be some 295 00:25:48,940 --> 00:25:53,500 uh other interest as well. You know I'm not the only person who wants a host based DNS monitor 296 00:25:53,500 --> 00:25:59,900 for Mac OS. There's at least one other person. So worth it. So the tool I wrote is called DNS monitor. 297 00:25:59,900 --> 00:26:05,480 Uh again it's fully open source free on Objective-C dot org. And you see when we execute it from 298 00:26:05,480 --> 00:26:11,380 the terminal if we then resolve a DNS name for example Objective-C dot org via NS lookup this 299 00:26:11,380 --> 00:26:19,060 will generate a DNS request that we can intercept. Now unfortunately there are several 300 00:26:19,060 --> 00:26:24,400 prerequisites. So if you're interested in writing your own network monitoring tools which you 301 00:26:24,400 --> 00:26:29,880 should. It's great fun. Uh there are some hoops you have to jump through. Uh the first one is the 302 00:26:29,900 --> 00:26:34,500 build. Unfortunately. So first when building it you have to have certain entitlements that 303 00:26:34,500 --> 00:26:40,240 Apple may or may not give you. So be prepared to beg. Also then when you build it there's a lot of 304 00:26:40,240 --> 00:26:44,920 constraints. Uh specifically you have to put your network extension in a certain place of the 305 00:26:44,920 --> 00:26:49,960 application. Uh so building the project can get a little hairy. And then of course you have to sign 306 00:26:49,960 --> 00:26:53,580 and notarize this. So this means you have to at least pay Apple a few hundred bucks for the 307 00:26:53,580 --> 00:26:59,880 developer ID and get them to uh validate and notarize it. Okay now you want your running system. 308 00:26:59,900 --> 00:27:06,460 You want your user to run it. What do they have to do? Click a lot. So if you go to Objective-C dot 309 00:27:06,460 --> 00:27:11,920 org and download DNS monitor which is entitled which means Apple gave me permission to do this. 310 00:27:11,980 --> 00:27:17,240 It's signed and notarized which means Apple has scanned it and verified it. You still have to 311 00:27:17,240 --> 00:27:22,140 click allow on this Safari prompt. When you open it gatekeeper's gonna bug you. So you're gonna 312 00:27:22,140 --> 00:27:26,740 have to click allow there. When you go to launch the app it's gonna say hey wait a minute this is a 313 00:27:26,740 --> 00:27:29,800 network extension instead of system extension. You need to uh. 314 00:27:29,900 --> 00:27:35,040 improve that. So you have to click open security systems. Uh you then once in there have to 315 00:27:35,040 --> 00:27:38,820 improve that. Which is gonna require you to authorize. So you're gonna have to put in your 316 00:27:38,820 --> 00:27:43,960 password. Uh and then finally before it's able to access the network to monitor for traffic you're 317 00:27:43,960 --> 00:27:48,220 gonna have to click allow one more time. So this is slightly annoying and if you're writing code 318 00:27:48,220 --> 00:27:52,260 you have to handle all these situations. Like what if the user clicks no? You have to handle code 319 00:27:52,260 --> 00:27:57,340 to maybe do this again or fail gracefully. So this is a little annoying and at this point I'm 320 00:27:57,340 --> 00:27:59,880 like man Mac OS has really become like Windows 10. So this is a little annoying and at this point I'm 321 00:27:59,900 --> 00:28:03,800 like Mac OS has really become like Windows Vista. You might not be as old as I am so you might not 322 00:28:03,800 --> 00:28:08,740 remember Vista but Vista was like this Windows operating system where you had to click allow on 323 00:28:08,740 --> 00:28:13,400 everything. And everyone like made fun of it. Guess who made the most fun of it? Apple. They 324 00:28:13,400 --> 00:28:19,360 made these hilarious commercials basically ragging on Vista for having a user click allow. And 325 00:28:19,360 --> 00:28:25,900 since you might not remember that I have the commercial here that we are now gonna watch. So I 326 00:28:25,900 --> 00:28:29,800 hope the audio works if not there's subtitles. Hello I'm a Mac. Mac has issued a solute 327 00:28:29,900 --> 00:28:33,800 salutation. Cancel or allow? This is an Apple commercial. Allow and I'm a PC. You're returning Mac 328 00:28:33,800 --> 00:28:40,300 salutation. Cancel or allow? Allow. Mac is asking a question. Cancel or allow? Allow. He's part of 329 00:28:40,300 --> 00:28:45,280 Vista my new operating system. PCs have a lot of security problems so he asked me to authorize 330 00:28:45,280 --> 00:28:49,380 pretty much anything I do. You're pointing out Vista's flaws. Cancel or allow? Allow. I could 331 00:28:49,380 --> 00:28:52,520 turn him off but then he wouldn't give me any warnings at all and that would defeat the purpose. 332 00:28:52,940 --> 00:28:56,740 You are coming to a sad realization. Cancel or allow? Allow. 333 00:28:59,480 --> 00:28:59,880 Hilarious. 334 00:28:59,900 --> 00:29:05,260 So I don't know if you were counting but there was five allows. How many allows did we need to 335 00:29:05,260 --> 00:29:09,940 install a network extension? Six. So you know come on Apple if you're gonna like rag on Windows 336 00:29:09,940 --> 00:29:15,140 like don't become them. Now we're not here to pick on Apple too much so let's get back to some 337 00:29:15,140 --> 00:29:21,300 code but that was some good comic relief. So let's talk about how to activate a system extension. 338 00:29:21,720 --> 00:29:26,780 And network extensions are a subset of system extensions so any network extension is also gonna 339 00:29:26,780 --> 00:29:29,880 be a system extension. So again I'm not gonna walk through the code too much but let's talk about 340 00:29:29,900 --> 00:29:37,800 this a little bit. So a network extension is a system extension that you generate a request to 341 00:29:37,800 --> 00:29:42,640 activate a system extension. You then set your delegate. This is a class that's gonna handle 342 00:29:42,640 --> 00:29:47,540 various methods that the operating system will invoke. And then finally you submit the request. 343 00:29:47,900 --> 00:29:51,900 This is what's gonna generate one of the pros pop-ups and if the user approves it your system 344 00:29:51,900 --> 00:29:57,540 extension is gonna be off and running. Now though we also wanna monitor network traffic. Again 345 00:29:57,540 --> 00:29:59,880 we're having a network extension. So what we're gonna do is we're gonna create a network extension. 346 00:29:59,900 --> 00:30:05,480 we need to do is we need to activate the network extension. This requires five steps. We load the 347 00:30:05,480 --> 00:30:11,780 current preferences, we set a description, we initialize and configure a provider protocol, in 348 00:30:11,780 --> 00:30:17,760 this case we're using the NDNS proxy provider protocol because we want to proxy DNS traffic. We 349 00:30:17,760 --> 00:30:23,740 set the uh a flag to enable and then we save the preferences which triggers another pop up that 350 00:30:23,740 --> 00:30:28,920 the user has to approve which then if they approve will activate the network extension. At 351 00:30:28,920 --> 00:30:34,020 this point our network extension will be off and running. You can confirm this using Apple's 352 00:30:34,020 --> 00:30:39,440 system extension control utility with the list command. If we do that we can see that our DNS 353 00:30:39,440 --> 00:30:45,040 networking monitor has been loaded. Behind the scenes what Apple has done is copied it from your 354 00:30:45,040 --> 00:30:49,780 application bundle and moved it into a root owned directory and loaded it from there. So this is 355 00:30:49,780 --> 00:30:53,640 one of the reasons you have to be very careful about packaging up your application that contains 356 00:30:53,640 --> 00:30:57,720 a network extension because the operating system needs it to be in that specific location. 357 00:30:58,640 --> 00:31:04,640 Okay so now we're off off and running. That code so far was executed in our application. Now 358 00:31:04,640 --> 00:31:09,220 since our network extension is loaded it's gonna be running as a separate process in the root 359 00:31:09,220 --> 00:31:15,300 session we need to start monitoring DNS traffic. So the first thing is we do is we call the start 360 00:31:15,300 --> 00:31:19,920 system extension mode function. Apple says do this right away. I don't really know what it does. 361 00:31:20,000 --> 00:31:26,700 Kicks things off. Behind the scenes Mac OS will consult the info dot plist file of the network 362 00:31:26,700 --> 00:31:33,120 extension which you will have to set up in a way to specify what class in your program is 363 00:31:33,120 --> 00:31:40,040 going to implement the delegates for example to handle new DNS requests. So on the slide we can 364 00:31:40,040 --> 00:31:44,800 see how to do that. So what are these delegate methods that this class has to implement? Well 365 00:31:44,800 --> 00:31:49,460 there's three. There's a start one and a stop one. This just gives you the opportunity to 366 00:31:49,460 --> 00:31:56,340 perform any initializations or clean up. The most important one is the handle new flow API method. 367 00:31:56,700 --> 00:32:02,380 Apple's documentation says this will be invoked any time a new DNS flow which is essentially a 368 00:32:02,380 --> 00:32:10,320 connection is generated. Perfect. Now remember we are building a DNS monitor but we're using this 369 00:32:10,320 --> 00:32:17,240 proxy class which means as a proxy we are actually responsible for well proxying on the data. So 370 00:32:17,240 --> 00:32:22,260 what we have to do is six or so different steps. And I'll walk you through that. So an 371 00:32:22,260 --> 00:32:26,680 application or some malicious software needs to resolve a domain. So what we're gonna do is we're 372 00:32:26,700 --> 00:32:30,960 gonna create a DNS server. So they do that. That generates a DNS request. Before that DNS 373 00:32:30,960 --> 00:32:35,880 request goes out to the DNS server because we've installed a network extension the operating 374 00:32:35,880 --> 00:32:42,180 system is going to deliver it to us. It's gonna call our handle new flow method. What we have to 375 00:32:42,180 --> 00:32:49,080 do is open the local flow, read the datagrams which is gonna be a DNS question, a DNS query and 376 00:32:49,080 --> 00:32:56,360 then connect out to the DNS server ourselves, build a new connection and then write the datagrams, 377 00:32:56,360 --> 00:33:01,460 the DNS request out to that remote endpoint. So we're basically responsible for taking it and 378 00:33:01,460 --> 00:33:07,600 now delivering it because again we're proxying the request. This also means we're responsible for 379 00:33:07,600 --> 00:33:14,100 reading and delivering the response from the DNS server. So the next two steps are we re- we read 380 00:33:14,100 --> 00:33:19,140 the response from the DNS server and then write it to the local flow which means it will then be 381 00:33:19,140 --> 00:33:26,280 delivered to the local application or the malware, whoever generated the DNS request. Now you might 382 00:33:26,360 --> 00:33:30,480 be wondering okay that's cool but how do we parse the DNS packets? And this is a question I had 383 00:33:30,480 --> 00:33:35,180 and I thought do I have to do this myself and I tried and then I was like wait this is a horrible 384 00:33:35,180 --> 00:33:39,560 idea. There's probably gotta be a better way. Turns out there's a relatively unknown library 385 00:33:39,560 --> 00:33:45,080 called libresolve, it was at least unknown to me, that uh ships with macOS that you can link 386 00:33:45,080 --> 00:33:49,900 into your application or your network extension and you can parse DNS packets. So on the slide we 387 00:33:49,900 --> 00:33:55,660 have an example of that. We give it a packet, a DNS request or response. We call the DNS parse packet 388 00:33:56,360 --> 00:34:01,580 returns to us a structure with all the members of the DNS nicely par- parsed out. This is super 389 00:34:01,580 --> 00:34:08,860 helpful, definitely use this library. What we haven't talked about yet is how do we identify the 390 00:34:08,860 --> 00:34:13,460 process that was responsible for generating the DNS request. At the beginning of this talk I 391 00:34:13,460 --> 00:34:18,520 said this is the best thing about host based network monitoring and well it is so how do we do 392 00:34:18,520 --> 00:34:24,740 that? Turns out it's pretty easy. The flow object that gets passed to the handle new flow method 393 00:34:24,740 --> 00:34:30,120 has what is known as an audit token that uniquely identifies the responsible process. The 394 00:34:30,120 --> 00:34:35,400 application, the malware, whomever is trying to make the DNS request. Audit tokens are kind of 395 00:34:35,400 --> 00:34:41,380 modern ways to uniquely identify processes. They're more secure than PIDs. Uh but once we have 396 00:34:41,380 --> 00:34:47,120 this audit token we can retrieve the process's PID, it's path, etc, etc. Anything about that. So 397 00:34:47,120 --> 00:34:51,420 that's really helpful because that can allow us to identify who is responsible for that DNS 398 00:34:51,420 --> 00:34:54,720 request. So we compile and execute this process. It's a very simple process. It's very easy to use. It's a 399 00:34:54,720 --> 00:35:00,820 code on a system that's infected with 3CX. We can now see that when again this very stealthy 400 00:35:00,820 --> 00:35:06,360 supply chain attack is executed when the malware tries to connect out to the trojanized installer 401 00:35:06,360 --> 00:35:11,820 connects out to the attacker's command and control server we can observe this and say hey 402 00:35:11,820 --> 00:35:19,080 wait a minute this isn't you know 3CX it's talking to some like msstorageboxes.com. Big red flag. 403 00:35:20,540 --> 00:35:24,700 Now via the DNS monitor we can also do things like block blocks. We can also do things like block 404 00:35:24,720 --> 00:35:29,740 DNS requests or responses because again we're proxying all the traffic. So this is great if we 405 00:35:29,740 --> 00:35:35,760 want to build perhaps a more uh proactive security tool. So let's look at how to do that. Super 406 00:35:35,760 --> 00:35:41,660 simple. What we do is we get the DNS request via the handle new flow API that again is gonna be 407 00:35:41,660 --> 00:35:48,160 automatically executed for each new DNS request. We parse that packet using lib resolve and then 408 00:35:48,160 --> 00:35:53,780 what we can do is we can extract the question that is the domain that the application or the malware is 409 00:35:53,780 --> 00:36:00,080 trying to resolve and if it's on a block list that we specify we simply close out the flow. This 410 00:36:00,080 --> 00:36:04,380 means that the connection is actually never gonna go out and from the application's point of view 411 00:36:04,380 --> 00:36:10,140 the DNS resolution has just failed. So if we implement this in the DNS monitor we can now see 412 00:36:10,140 --> 00:36:16,680 that if for example I've told it to block google.com if we try to resolve google.com via you 413 00:36:16,680 --> 00:36:22,640 know nslookup or any other way it's just gonna time out and the system is gonna be like uh I can't 414 00:36:22,640 --> 00:36:23,760 help you. So this is great. 415 00:36:23,780 --> 00:36:27,840 This means now if we know the command and control server we can block where the malware is trying to 416 00:36:27,840 --> 00:36:33,040 connect out to. It won't be able to resolve it and then won't be able to connect out. We can also 417 00:36:33,040 --> 00:36:39,600 block the answer. The answer is gonna be the IP address that the domain resolves to. So perhaps we 418 00:36:39,600 --> 00:36:44,980 might not know what all domains uh malicious IP address is mapped to. As long as we know that the 419 00:36:44,980 --> 00:36:52,620 IP itself is suspicious or bad uh when the answer comes back the answer recall is proxied through us 420 00:36:52,620 --> 00:36:57,400 before it gets back to the application. So again we can simply say is this something we want to 421 00:36:57,400 --> 00:37:01,800 block and if so close the flow which will have the same effect the application will never get the 422 00:37:01,800 --> 00:37:07,500 response. Last feature of DNS monitor you can dump the cache. This was actually one of the 423 00:37:07,500 --> 00:37:12,520 features I wanted the operating system to give me and when I was one of the reasons why I wrote 424 00:37:12,520 --> 00:37:17,900 this tool. So what we can do is we can send a signal to the DNS monitor and it will basically 425 00:37:17,900 --> 00:37:22,520 dump uh all it's records. All the domains and the IP addresses for all the query 426 00:37:22,620 --> 00:37:27,980 on the system. This is great perhaps as a troubleshooting DNS troubleshooting tool. Now 427 00:37:27,980 --> 00:37:32,900 there's one shortcoming again pros and cons everything is a package deal and that is that we 428 00:37:32,900 --> 00:37:38,540 are only gonna see DNS traffic. So let's turn our eyes back to dummy and if we look at line 429 00:37:38,540 --> 00:37:43,900 number six we can see it connects out to the attacker's command and control server. It uses a 430 00:37:43,900 --> 00:37:49,700 hard coded IP address which means what? No DNS traffic. Which means we will not see any of 431 00:37:49,700 --> 00:37:55,680 dummy's network traffic because it doesn't need to use DNS. So the next question is can we 432 00:37:56,200 --> 00:38:02,080 enhance our DNS monitor or take a more comprehensive approach to see all network traffic? The 433 00:38:02,080 --> 00:38:06,620 answer is of course we can do this and also throw in some rules to build a utility such as a 434 00:38:06,620 --> 00:38:12,500 firewall. So Lulu you might be familiar it's been around for a while recently updated it to use 435 00:38:12,500 --> 00:38:17,900 Apple's new network extensions framework. Previously it used a kernel extension and well it's a 436 00:38:17,900 --> 00:38:19,680 firewall right? I don't need to explain. So I'm gonna go ahead and show you how to do this. 437 00:38:19,700 --> 00:38:25,280 But the idea is it sees all network traffic and if it's from an authorized or trusted process it 438 00:38:25,280 --> 00:38:29,920 allows it. If it's something that it doesn't recognize it prompts the user to be like yo 439 00:38:29,920 --> 00:38:36,000 something's happening confirm or deny. Oops maybe I do it too. Alright so how do we do this? 440 00:38:36,300 --> 00:38:42,540 We use another class that the network extension APIs and library affords to us which is the NE 441 00:38:42,540 --> 00:38:49,680 filter data provider. This allows you to monitor and govern all network flows. As you can see 442 00:38:49,700 --> 00:38:55,320 from the header file description. The idea is all network traffic comes to us and then we can 443 00:38:55,320 --> 00:39:01,900 classify it as benign, authorized or perhaps suspicious and malicious. The way we do that and 444 00:39:01,900 --> 00:39:06,100 we'll dive into that shortly is by either looking at the responsible process. We can do some 445 00:39:06,100 --> 00:39:14,620 analysis on the traffic itself or we can ask the user. So very similar to the DNS monitor we need 446 00:39:14,620 --> 00:39:19,680 to enable the network extension uh by again these four steps. The first step is to enable the network 447 00:39:19,700 --> 00:39:23,660 filter. There's a few differences here since we're monitoring all network traffic we have some 448 00:39:23,660 --> 00:39:29,700 more customization capabilities including the fact that we could in theory monitor or rather examine 449 00:39:29,700 --> 00:39:35,360 all packets. For firewall we don't want to we just care more about the connections so we say no. 450 00:39:35,520 --> 00:39:40,960 However we do say we want to filter all sockets so we set that to yes. Once we've saved these 451 00:39:40,960 --> 00:39:47,000 configuration changes this will activate the network filter and all network traffic will now be 452 00:39:47,000 --> 00:39:49,680 routed through us. Again the network filter is set to yes. So we can see all network traffic is now 453 00:39:49,680 --> 00:39:49,800 routed through us. So we can see our network traffic is now out there. We can see that the 454 00:39:49,800 --> 00:39:50,100 network traffic is still in the middle. So we can see it from the web. Now the next step is to 455 00:39:50,100 --> 00:39:51,280 allow the network to be controlled and out of the network. Teraz the handle new flow method 456 00:39:51,280 --> 00:39:57,140 will be invoked. Apple describes the handle new flow method it's pretty similar to the DNS 457 00:39:57,140 --> 00:40:04,240 monitor with one separate or new thing and that is it expects a response from us whether to allow or 458 00:40:04,240 --> 00:40:08,700 deny the connection so there's three answers you can give and you have to give an answer either 459 00:40:08,700 --> 00:40:15,080 block which will cause the operating system to drop the connection uh allow it which will allow it or 460 00:40:19,680 --> 00:40:26,020 Now if you want a passive monitor, you just click or return allow for all connections. Okay, 461 00:40:26,080 --> 00:40:30,060 so now maybe the most important part of the com- uh the talk, the conversation is, okay, now 462 00:40:30,060 --> 00:40:34,540 we have the ability to take snapshots of network activity, traffic, uh look at DNS traffic, 463 00:40:34,920 --> 00:40:39,280 uh or even, you know, monitor all system traffic. How do we, and this is an incredibly 464 00:40:39,280 --> 00:40:45,020 important question, determine if that traffic is unauthorized, malicious, belongs to 465 00:40:45,020 --> 00:40:49,200 something that should not be on the system talking to the network. So let's talk about some 466 00:40:50,480 --> 00:40:54,840 heuristics. I mentioned there's two main ways, three if you ask the user, but let's just focus 467 00:40:54,840 --> 00:40:59,720 on two here. The most common way, and this is well established, is via the network information 468 00:40:59,720 --> 00:41:05,080 that is provided in the flow. Now there's been talks about this, books written on it, this is 469 00:41:05,080 --> 00:41:11,240 how most network appliances classify network traffic as being unauthorized or not. So things 470 00:41:11,240 --> 00:41:17,660 like state, right, like a listening socket bound to the external interface on port 1337 or 471 00:41:17,660 --> 00:41:19,660 666, like, big red flag. 472 00:41:19,680 --> 00:41:26,040 Right? Also maybe statistics, is something beaconing every hour or perhaps once a day at 473 00:41:26,040 --> 00:41:32,480 midnight and uploading a gig of data to some unknown endpoint. That's suspicious. But again, 474 00:41:32,620 --> 00:41:37,480 since we're doing host-based traffic analysis and running on the host, we can also bring in the 475 00:41:37,480 --> 00:41:43,840 responsible process. Perhaps for example that process that's beaconing at midnight and sending a 476 00:41:43,840 --> 00:41:47,720 gig of data somewhere is the backup solution just backing up the user's files at midnight. And 477 00:41:47,720 --> 00:41:49,660 that's fine. You will not be able to tell 478 00:41:49,680 --> 00:41:54,460 that from a network application, uh, or network appliance. But if we're on the host, we can say, 479 00:41:54,540 --> 00:41:59,920 hey, who is doing this? And answer that question conclusively. So let's talk about now how to 480 00:42:00,520 --> 00:42:07,520 examine that n- that responsible process to perhaps determine if it's malicious or not. Now I 481 00:42:07,520 --> 00:42:11,940 will caveat there are many ways to do that. That could be a talk unto itself, but let's look at 482 00:42:11,940 --> 00:42:17,180 some recent examples that would at least be able to, uh, identify the malware samples we talked 483 00:42:17,180 --> 00:42:19,240 about earlier in the talk as, well, 484 00:42:19,680 --> 00:42:26,240 malware. So some examples, you know, is the item that's running a non-platform binary, is it 485 00:42:26,240 --> 00:42:31,940 something that's non-notarized? Uh, is it persistently unsolved? Perhaps a red flag. And is 486 00:42:31,940 --> 00:42:37,420 it doing something unusual, for example, with the process hierarchy? So let's look at that. The 487 00:42:37,420 --> 00:42:43,840 first one is how to determine if something belongs to macOS proper. In Apple parlance they say 488 00:42:43,840 --> 00:42:49,640 that's a platform binary. Uh, and also if it's notarized or not. So notarization that I talked about is a 489 00:42:49,680 --> 00:42:54,900 method where applications, programs are submitted to Apple and they are approved. Yes, Apple 490 00:42:54,900 --> 00:43:00,940 sometimes inadvertently approves malware, but generally speaking, all legitimate third-party 491 00:43:00,940 --> 00:43:07,300 software is mostly notarized, whereas most malware is not. So this is kind of a good, pretty solid 492 00:43:07,300 --> 00:43:12,460 red flag if you see something that's not notarized. Not gonna go through all the code, but once we 493 00:43:12,460 --> 00:43:17,920 have an audit token for the responsible process or even a PID, what we can do is we can invoke 494 00:43:17,920 --> 00:43:24,560 various code signing APIs in macOS to ask questions like, is this an Apple binary? Is this 495 00:43:24,560 --> 00:43:28,700 notarized? And then the operating system will figure that out and give us an answer that will allow 496 00:43:28,700 --> 00:43:33,880 us to, well, answer this question. If you're interested in the full implementation, I wrote a 497 00:43:33,880 --> 00:43:38,700 utility called block block, which does some other stuff, but it also has a full implementation of 498 00:43:38,700 --> 00:43:44,820 this code. Next question, is it persistently installed? The majority of mac malware is going to 499 00:43:44,820 --> 00:43:47,900 persist. Uh, so if you see a network connection that's tied to a network connection, you can 500 00:43:47,920 --> 00:43:48,220 see that it's persistently installed. If you see a network connection that's tied to a 501 00:43:48,220 --> 00:43:53,380 persistent process, maybe cause for concern, or at least something you're gonna wanna look at a 502 00:43:53,380 --> 00:43:59,420 little closer. On recent versions of macOS, persistence is governed or managed by this 503 00:43:59,420 --> 00:44:04,800 undocumented backgrounds item database. I wrote an open source utility that will 504 00:44:04,800 --> 00:44:10,940 deserialize the items in this database and print them out for you. So this, for example, is what 505 00:44:10,940 --> 00:44:17,500 dummy looks like in the database. So OSX dummy persists as a launch daemon. Its component, as we 506 00:44:17,500 --> 00:44:22,580 saw, is a script that will run as root, and since it's a script, it's not signed. So we have a 507 00:44:22,580 --> 00:44:29,920 persistent script running as root. That's not common. That's, that's cause for concern. And 508 00:44:29,920 --> 00:44:34,160 then finally, and we'll stick with dummy here, you should also look at the process hierarchy. So at 509 00:44:34,160 --> 00:44:38,800 the very beginning, I posed the question, how do we know that this instance of Python is 510 00:44:39,680 --> 00:44:45,660 malicious, or being abused, rather? Well, if you look at the process hierarchy for it, you will 511 00:44:45,660 --> 00:44:51,580 see that Python is a child of bash, which is a child of launchd, meaning that it has been 512 00:44:51,580 --> 00:44:58,280 persisted and then executed via bash. We can also see its running as root. So now what we have, just to 513 00:44:58,280 --> 00:45:05,040 put everything together, we have a established connection to some unknown IP address on port one 514 00:45:05,040 --> 00:45:12,620 three three seven, strange, uh, its tied back to a script that's running as root, uh, via Python, 515 00:45:12,760 --> 00:45:15,640 that spawned via bash, that's persistently installed. So we have a selfish connection between the two. And one of the things that we're 516 00:45:15,660 --> 00:45:20,520 like all those things are kind of individually strange but then if we combine them all 517 00:45:20,520 --> 00:45:24,580 together that's a huge red flag. 99% of the time that's probably gonna be malware at least 518 00:45:24,580 --> 00:45:31,060 something you want to take a closer look at. Alright so let's conclude this and wrap this all 519 00:45:31,060 --> 00:45:37,580 up. So hopefully I've illustrated to you that by leveraging macOS's networking frameworks we 520 00:45:37,580 --> 00:45:43,200 can heuristically detect malware directly from the host. Again this is based on the uh very keen 521 00:45:43,200 --> 00:45:48,800 observation that malware uses the network and so if we can detect this unauthorized activity we 522 00:45:48,800 --> 00:45:54,700 can uncover malware. If you're interested in learning more I've written a book on analyzing 523 00:45:54,700 --> 00:46:01,560 Mac malware. It's 100% free uh on taomm.org for the art of Mac malware. I'm also writing a 524 00:46:01,560 --> 00:46:07,200 another uh volume which will be about the programmatic detection. So think this topic plus 525 00:46:07,660 --> 00:46:12,560 many more. This will also be free as well. I'm gonna be doing a book signing tomorrow on 526 00:46:12,560 --> 00:46:18,300 volume one at the no starch press table at 11am. So if you wanna pick up a copy of the book or 527 00:46:18,300 --> 00:46:23,340 have me sign it stop by and say hi. I wanna end by briefly talking about our organization 528 00:46:23,340 --> 00:46:29,140 cause I think it's amazing and I'm super stoked. So the Objective-C foundation is a non-profit 529 00:46:29,140 --> 00:46:32,740 and we do some really cool things that I just wanna share with you cause I'm excited about it. 530 00:46:32,920 --> 00:46:38,560 So we organize the OBTS security conference. I look around and I see many familiar faces from 531 00:46:38,560 --> 00:46:42,080 the conference. Speakers, attendees. If you haven't heard about Objective by the C. 532 00:46:42,560 --> 00:46:47,500 It's awesome. We also generate uh uh raise money for college scholarships for high school 533 00:46:47,500 --> 00:46:53,160 students and have some diversity programs uh which we're really stoked on as well. So again pop 534 00:46:53,160 --> 00:46:59,000 over to Objective-C dot org. Now I wanna end maybe not on the happiest note uh many of you 535 00:46:59,000 --> 00:47:05,380 know I live in Maui. The Objective-C foundation is born in Maui. The OBTS conference started on 536 00:47:05,380 --> 00:47:10,200 Maui. Uh I made all these slides on Maui. Uh and earlier this week there was some really uh 537 00:47:12,560 --> 00:47:17,800 funny people died. Lost everything. So what we're doing is we are uh launching a fundraiser 538 00:47:17,800 --> 00:47:24,080 through our non-profit. Our 501 CC 501 C3 to raise money for that. Uh so if you're called to 539 00:47:24,080 --> 00:47:27,980 support a great cause uh check that out or just even pass share on the link. Hundred percent of 540 00:47:27,980 --> 00:47:31,960 the money is gonna go to you know the residents of Maui that have literally lost uh everything 541 00:47:31,960 --> 00:47:37,580 including uh loved ones. Uh so again pop over to Objective-C dot org to uh get some more 542 00:47:37,580 --> 00:47:42,140 information about that. Finally I just wanna thank the companies who support the Objective-C 543 00:47:42,560 --> 00:47:47,520 foundation. They're the ones that allow us to organize a conference. Uh support my research. 544 00:47:47,740 --> 00:47:52,880 Allow you know uh me to be able to write a book and make it free. The goal is really for us as a 545 00:47:52,880 --> 00:47:58,980 foundation and me as a researcher to uh empower the community, share my knowledge and that is I'm 546 00:47:58,980 --> 00:48:03,680 able to do that because of these companies. So uh just again I wanna end by thanking them. I 547 00:48:03,680 --> 00:48:09,200 also wanna thank you for sitting through my talk. Really appreciate your attendance. I think 548 00:48:09,200 --> 00:48:12,540 I'm out of time so maybe no public Q and A's but I will pop over to Objective-C dot org. 549 00:48:12,540 --> 00:48:17,340 We're here and since this is the last talk of the day we can hang out here as long as uh y'all 550 00:48:17,340 --> 00:48:21,660 want and so if there's any questions uh come ask away. Again thank you so much I really 551 00:48:21,660 --> 00:48:23,040 appreciate y'all being here.