00:00:00.033-->00:00:05.939 >>Alright, Aloha and welcome to my talk on making and breaking Mac Firewalls. My name is 00:00:05.939-->00:00:12.112 Patrick um, I work at Digitas security where we are creating cyber security tools for the Mac 00:00:12.112-->00:00:17.117 enterprise. I’m also the creator of the Mac security website Objective See. [Audience member 00:00:21.521-->00:00:23.524 hoots] [Speaker hoots] [applause] [laughter] So, today [applause] thank you, thank you. 00:00:23.524-->00:00:26.727 We’re going to be talking about creating or making a Mac firewall and then we’re gonna 00:00:26.727-->00:00:33.667 kind of shift gears and talk about breaking and bypassing such products. [Pause] So about 00:00:33.667-->00:00:38.839 a year ago, ah I decided I wanted to write a firewall for Mac OS because there were no 00:00:38.839-->00:00:45.212 free open source options. So in this section of the talk we’ll describe this process creating 00:00:45.212-->00:00:51.451 LuLu, which is my free open source Mac Firewall. Now, there are many reasons you might want 00:00:51.451-->00:00:56.089 to create or install a firewall, they’re actually pretty good security tools. Ah, probably the 00:00:56.089-->00:01:02.029 two main reasons are, one to protect your privacy or two to thwart cyber attacks. Because 00:01:02.029-->00:01:07.434 most firewalls are able to generically detect malware when the malware connects out perhaps 00:01:07.434-->00:01:13.540 to exfiltrate data or connect to a command and control server for tasking. So what our firewall is 00:01:13.540-->00:01:18.712 going to do is monitor all network traffic, allowing traffic out that’s trusted and 00:01:18.712-->00:01:24.484 ideally blocking or preventing unauthorised or malicious traffic. [Pause] Now, since 00:01:24.484-->00:01:28.989 we’re going to need to monitor all traffic globally, we're going to have to write a kernel 00:01:28.989-->00:01:33.994 extension. In Mac OS, Apple provides network kernel extensions or NKE’s as a way to 00:01:36.363-->00:01:41.368 extend or modify the network infrastructure. And one type of NKE, is a socket filter, which 00:01:43.437-->00:01:49.609 as its name suggests, allows code to filter network traffic at the socket level, which for a 00:01:49.609-->00:01:55.649 firewall is perfect. That’s exactly what we need. Now there are two steps to register a 00:01:55.649-->00:02:01.989 socket filter. First, we ah, populate a socket filter structure and the structure 00:02:01.989-->00:02:07.561 contains various callbacks, that once registered, will be automatically invoked by the 00:02:07.561-->00:02:12.633 operating system on certain socket operations. Which then gives our firewall the ability 00:02:12.633-->00:02:19.606 to examine these socket actions and determine whether they should be allowed or blocked. 00:02:19.606-->00:02:24.945 [Pause] So the second step, is then to invoke the socket filter register function to install 00:02:24.945-->00:02:31.618 your socket filter. Now, besides the populated structure this also takes a socket domain type 00:02:31.618-->00:02:36.523 and protocol, which means if you want to filter all socket domains, types and protocols, 00:02:36.523-->00:02:42.696 you should invoke this method or function several times. Pause] Okay, so now let’s talk about 00:02:42.696-->00:02:47.367 these callbacks, which, as I mentioned once your socket filter is registered, will be 00:02:47.367-->00:02:52.839 automatically invoked by the operating system on socket events. The first callback, is 00:02:52.839-->00:02:58.912 the attach callback and as we can see on the slide it will be invoked anytime a new socket is 00:02:58.912-->00:03:05.519 created. [Pause] So it’s created with a cookie parametre that’s designed to hold any socket 00:03:05.519-->00:03:09.489 specific information. You can really put whatever you want there. So what we do is, we 00:03:09.489-->00:03:15.462 allocate a chunk of memory and based on the PID of the process that’s creating the socket, we 00:03:15.462-->00:03:21.201 either set it to allow, for example if it’s a trusted system damin or block if it’s a process 00:03:21.201-->00:03:26.540 that, that the user has chosen to block. Now if it’s a process we don’t recognise, we set this 00:03:26.540-->00:03:30.811 action to ask and then we're gonna have to do some extra logic to determine what action 00:03:30.811-->00:03:37.184 to take. [Pause] So next is the connect out callback, this is called before initiating an 00:03:37.184-->00:03:42.656 outgoing connection. Again, it takes the same cookie, which we've set to either allow, block 00:03:42.656-->00:03:47.828 or ask and the socket and the remote address that the socket is trying to connect to. So this 00:03:47.828-->00:03:53.834 obviously allows us to examine the end point. Now if the action has been set to allow, we just 00:03:53.834-->00:03:57.971 return okay, this tells the operating system we’re okay with allowing this connection to 00:03:57.971-->00:04:03.110 continue. If it’s set to block we return an error code from this function, which tells the 00:04:03.110-->00:04:08.281 operating system we want to block or not allow the connection. And if it’s set to 00:04:08.281-->00:04:13.286 ask, we have to a-ah- ex-ah execute some extra logic to determine what action to take. 00:04:16.123-->00:04:20.827 [Pause] So in other words we have to figure out whether to allow or block the connection. 00:04:20.827-->00:04:25.165 So what we do, is we first put the thread to sleep that’s trying to perform the socket 00:04:25.165-->00:04:31.505 operation, this will pause the action. We then ask our user mode firewall component for 00:04:31.505-->00:04:37.177 assistance and we pass the information from the kernel, specifically the PID and the 00:04:37.177-->00:04:42.182 socket, via a shared que. [Pause] Once the damin gets this request from the kernel socket 00:04:44.251-->00:04:50.490 filter, it maps the PID to a path and first checks a rules database, to see if that path is 00:04:50.490-->00:04:55.729 in the database. If it’s not found perhaps it’s a brand new application, or a piece of 00:04:55.729-->00:05:00.734 malware that has somehow gotten onto your system. What it does, is it sends a message to another 00:05:00.734-->00:05:05.238 firewall component that’s running in the users session. And this is what actually 00:05:05.238-->00:05:11.211 displays the firewall alert to the user. [Pause] Now the users response, they’ll either have to 00:05:11.211-->00:05:17.450 click allow or block, will then be passed back to the damin. The damin will save this response to 00:05:17.450-->00:05:22.255 the rules database, so moving forward, it knows what to do and then also sends this response 00:05:22.255-->00:05:28.728 back to the kernel component via an IO kit interface. The kernel extension will then wake up the 00:05:28.728-->00:05:33.667 thread that was put to sleep and then apply the action, either allowing the connection or 00:05:33.667-->00:05:39.506 blocking it. [Pause] So that’s basically all that’s needed to create a comprehensive firewall 00:05:39.506-->00:05:44.678 for Mac OS. And putting this all together, we have LuLu. As I mentioned LuLu is a free 00:05:44.678-->00:05:48.949 firewall, I don’t think any user should have to pay for security products and also the full 00:05:48.949-->00:05:53.420 source for the firewall is online on GitHub. And as of today you can download and 00:05:53.420-->00:05:58.425 install version 1.0. [Pause] [Applause] Awesome. Thank You. Very friendly audience today, I 00:06:02.095-->00:06:06.833 really like this. [Speaker laughs]. Alright, so DefCon, in my opinion is predominantly, you 00:06:06.833-->00:06:10.070 know, a hacker conference, it’s about breaking things and exploits and vulnerabilities. 00:06:10.070-->00:06:14.608 So, let’s kind of switch gears and talk about now breaking and bypassing such firewall 00:06:14.608-->00:06:21.014 products. So imagine you're an attacker or a piece of malicious code that has somehow made it on 00:06:21.014-->00:06:24.985 to and end system. Unfortunately, there’s a firewall that’s installed, so if 00:06:24.985-->00:06:29.289 you were going to connect out, perhaps to exfiltrate data or connect to a command and control 00:06:29.289-->00:06:34.527 server, the firewall is going to detect and block this. So your goal is simple, how can you 00:06:34.527-->00:06:39.065 connect out without the the firewall blocking this data? So in the this section, we will 00:06:39.065-->00:06:43.336 first look at some firewall aware malware, we’ll then look at some security vulnerabilities 00:06:43.336-->00:06:49.743 in firewall products and then end with ways to completely bypass the firewalls. [Pause] So 00:06:49.743-->00:06:55.215 first, it’s definately important for malware or an implant to detect if a firewall is 00:06:55.215-->00:06:59.953 installed on the box. Otherwise it really might be their undoing, as the firewall may 00:06:59.953-->00:07:04.190 detect a previously undetected malware when it tries to connect out. And as we can see on the 00:07:04.190-->00:07:10.363 slide, this has happened. Now I’ve yet to see any public Mac malware that tries to 00:07:10.363-->00:07:14.701 specifically bypass Mac firewalls, but there are some specimens that are firewall 00:07:14.701-->00:07:20.240 aware. And what I mean by that is they will enumerate installed processes or look specifically 00:07:20.240-->00:07:26.346 for firewalls and if they see an installed firewall they will not persistently infect that system. 00:07:26.346-->00:07:31.551 One example of this is double robber and what double robber did is it look for a little 00:07:31.551-->00:07:36.690 snitch, which is a popular Mac firewall. If that was installed it would not infect the system, 00:07:36.690-->00:07:41.394 it would just execute a benign instance of the application it had infected and then simply 00:07:41.394-->00:07:48.001 exit. [Pause] Okay, now on to some security issues. Ah, software such as firewalls often 00:07:48.001-->00:07:53.506 very complex and firewalls often run with elevated privileges or even in the kernel. Now 00:07:53.506-->00:07:57.844 unfortunately they are not as well written as the operating system nor have been audited as 00:07:57.844-->00:08:01.715 well,which makes them excellent,excellent targets for local privilege,ah 00:08:01.715-->00:08:06.386 vulnerabilities. [Pause] So I talked about this bug ah at Defcon awhile ago, but just 00:08:06.386-->00:08:11.024 briefly to go over, it’s kind of unique kernel bug that affected the little snitch firewall 00:08:11.024-->00:08:16.930 kernel extension. Basically the firewall extension took a 64-bit value from user mode and used 00:08:16.930-->00:08:22.936 that in an allocation and a copy routine. Unfortunately, the allocation routine expected a 00:08:22.936-->00:08:27.941 32-bit value, so it truncate that. However, the copy routine expected a full 64-bit value, so 00:08:30.243-->00:08:34.681 it’ll allow us to have a controllable mismatch between the allocation and then the copy 00:08:34.681-->00:08:38.918 routine. Which led to an exploitable heat overflow in the kernel allowing us to execute 00:08:38.918-->00:08:45.525 arbitrary code in Ring zero. [Pause] Another issue I found while briefly looking at little 00:08:45.525-->00:08:51.598 snitch affected its installer and updater component. Ah, in short the firewall installer did 00:08:51.598-->00:08:57.303 not validate the components it was going to install. So a local unprivileged attacker could 00:08:57.303-->00:09:03.076 modify these components and then the firewall installer or updater would naively install 00:09:03.076-->00:09:08.448 and execute them as root, again giving a local unprivileged attacker a very reliable way to 00:09:08.448-->00:09:14.521 elevate their privileges. [Pause] Alright, now onto bypassing firewalls, ah we’ll 00:09:14.521-->00:09:19.159 first look at some product specific bypasses, but then we’ll also look at some more 00:09:19.159-->00:09:23.930 powerful generic ones that can bypass all third party firewalls. Again, the goal here 00:09:23.930-->00:09:29.869 is, network access without being blocked. Now I do want to reiterate that in my opinion 00:09:29.869-->00:09:35.542 these bypasses are not security issues per say, like they don’t deserve a CVE, um, but however, 00:09:35.542-->00:09:39.946 they’re still very valuable especially for an attacker or piece of malware, which may 00:09:39.946-->00:09:45.552 otherwise be thwarted by the firewall. So first off we have Radio Silence, is a popular 00:09:45.552-->00:09:50.023 firewall for Mac OS. It kind of takes an interesting approach, in that it allows any new 00:09:50.023-->00:09:55.795 process, but the user can explicitly blacklist certain applications. However, if we 00:09:55.795-->00:10:01.034 look at the blacklisting logic in the kernel extension, we can see it looking for a name of a 00:10:01.034-->00:10:06.673 process and it appears if the process is named Launch D, it cannot be blacklisted nor 00:10:06.673-->00:10:12.045 blocked. [Pause] So let’s test this, we take some malware, we name it Launch D, again the path 00:10:12.045-->00:10:18.017 doesn’t matter, just the name and then we manually create a rule to blacklist this process. 00:10:18.017-->00:10:23.490 We then run it and as we can see on the slide, it still allowed out, because again, it is named 00:10:23.490-->00:10:29.996 Launch D. So kinda lame, but again fully bypasses this firewall. Another popular Mac 00:10:29.996-->00:10:35.869 firewall is named Hands Off, and it turns out that we can pretty easily bypass this via a 00:10:35.869-->00:10:41.908 synthetic click. So for example, if we execute curl, which is something that Mac malware often 00:10:41.908-->00:10:48.181 does, for example, to download additional components. As expected the firewall will 00:10:48.181-->00:10:53.987 detect this unauthorized activity and display an alert. What the attacker or malware can 00:10:53.987-->00:11:00.193 then do, is send a problematic synthetic click to that allow button, which will click the 00:11:00.193-->00:11:05.932 allow button, basically, ah, hiding the alert and allowing the connection. And it also 00:11:05.932-->00:11:10.637 turns out there are ways you can do this without the user noticing. So, there are ways 00:11:10.637-->00:11:14.507 that you can this invisibly, so that the user is not gonna see the alert in this synthetic 00:11:14.507-->00:11:19.512 mouse click. Next up we have LuLu [Pause] by default LuLu trusts [Audience member laughs] 00:11:21.881-->00:11:24.684 Apple binaries, yeah, you know I’m picking on everyone including my own tools, ah you 00:11:24.684-->00:11:29.689 know, I think it’s only fair. [Speaker laughs] [Audience applause] So, LuLu by default 00:11:31.791-->00:11:37.730 trusts Apple sign processes, however it grey lists certain Apple binaries, which could 00:11:37.730-->00:11:43.036 potentially be abused for malicious activity. So, for example, netcat in curl, even 00:11:43.036-->00:11:48.908 though those are signed by Apple it will still alert anytime anybody executes them. So the 00:11:48.908-->00:11:54.047 question is , can we find another signed Apple utility that is not on the grey list? 00:11:54.047-->00:11:59.686 And the answer is yes, turns out you can exfiltrate data via the Who Is utility. That, this was 00:11:59.686-->00:12:06.025 news to me. So as we can see on the slide in the LuLu debug log, if we execute this, the firewall 00:12:06.025-->00:12:10.263 will see the outgoing connection, because again it’s global, it sees all network 00:12:10.263-->00:12:16.069 traffic. However, because who is assigned by Apple proper and is not on the grey list, it will be 00:12:16.069-->00:12:21.641 allowed. Now note, this has been fixed, I basically added Who Is, to the grey list. [Pause] And 00:12:21.641-->00:12:27.046 finally we a little snitch, little snitch is the defacto most well-known firewall for Mac 00:12:27.046-->00:12:32.852 OS. Turns out, it has an undeletable rule that says any process can talk to iCloud dot 00:12:32.852-->00:12:37.924 com, domains or url’s. This means any process, even malware, is allowed to talk to Apple 00:12:37.924-->00:12:42.929 servers. To test this, we can manually create a deny rule for curl and then we can execute 00:12:45.131-->00:12:51.337 curl with an iCloud url and it is still allowed. [Pause] So awhile back I reversed 00:12:51.337-->00:12:56.342 engineered the iCloud protocol and built a CNC server on IDrive, for testing purposes. 00:12:59.445-->00:13:01.547 [Audience laughs][Speaker laughs] Don’t, don’t get mad at me Apple. [Applause] So [Speaker 00:13:01.547-->00:13:06.552 laughs] It actually is a great like dropbox like, um CNC server, because you can get 00:13:08.588-->00:13:12.792 alerts when files are uploaded. It’s, it’s, it’s really great. So once we understand the 00:13:12.792-->00:13:17.563 protocol, what we can do is, is we can write some custom code that we can use to exfiltrate 00:13:17.563-->00:13:22.702 data. Now even if little snitch is installed, and sees the connection, it will be allowed 00:13:22.702-->00:13:29.042 because the endpoint we are talking to matches or maps ICloud domain. [Pause] Okay, so 00:13:29.042-->00:13:33.746 those were some product specific bypasses, kinda neat, kinda funny, but in my opinion they’re 00:13:33.746-->00:13:38.384 still a little bit lame. And they’re lame because, first an attacker would have to enumerate 00:13:38.384-->00:13:43.022 and determine the specific firewall product that was installed and then have one of 00:13:43.022-->00:13:49.228 these product specific bypasses. Way more powerful, are generic bypasses, which can just bypass 00:13:49.228-->00:13:54.767 any installed firewall. And these are all possible because the firewall’s essentially 00:13:54.767-->00:14:00.039 disadvantaged, it has to allow some network traffic off the box. For example, trusted system 00:14:00.039-->00:14:07.046 damin’s or the users probably going to fully allow certain things like browsers. [Pause] So 00:14:07.046-->00:14:12.018 the first thing we do to find a generic bypass, is once we’re on a system, is a piece of malware 00:14:12.018-->00:14:18.257 or an attacker is passively sniff to see what traffic is allowed off the box and we can 00:14:18.257-->00:14:23.362 do this via the LSOF utility. So if execute this on, this on a box, even though I have my 00:14:23.362-->00:14:27.200 firewall installed, obviously there’s gonna be some traffic that is allowed out. So, for 00:14:27.200-->00:14:32.205 example, we can see browsers, chat applications, some Adobe licensing crap etc-etc. [Pause] 00:14:34.574-->00:14:40.279 So now we know what traffic or what processes are allowed through the firewall. So, for 00:14:40.279-->00:14:46.119 the first generic bypass, we’re going to indirectly exploit the trust of a process via a trusted 00:14:46.119-->00:14:51.124 protocol. So on Mac OS, any time a DNS request is made, this is handled by Apple’s MDNS 00:14:53.126-->00:14:57.597 responder. This means if a random application or piece of malware tries to resolve a 00:14:57.597-->00:15:02.235 domain, the malware or the application actually does not generate the network request. 00:15:02.235-->00:15:07.240 It’s sent locally to the system damin, which then, on the applications behalf, will 00:15:09.809-->00:15:13.713 resolve the domain. [Pause] So, what malware can do, is basically abuse this fact, 00:15:13.713-->00:15:18.785 because, yes again, the firewall is going to see it as DNS resolution. But since it’s just 00:15:18.785-->00:15:24.657 a DNS request coming from the Apple signed trusted DNS damin, which is handling all DNS 00:15:24.657-->00:15:29.128 requests for the entire system, the firewall is going to allow it. So, very easily, we can 00:15:29.128-->00:15:35.201 build a by-directional command control protocol, purely based on DNS. [Pause] Next up, let’s 00:15:35.201-->00:15:40.106 talk about abusing browsers. Ah, the simple fact is, if you have a browser installed, it’s going 00:15:40.106-->00:15:44.610 to be able to access the network. Ah, you know, even if you maybe say only port 80 and 00:15:44.610-->00:15:48.781 443, you know, you're probably going to get an indiscriminate access to talk to any web 00:15:48.781-->00:15:54.153 server. And again, we can passively determine this via LSOF. [Pause] So, the first way 00:15:54.153-->00:16:00.259 we can abuse browsers, is via Applescript. So as we can see on the slide, we have kindly asked 00:16:00.259-->00:16:07.099 Safari to invisibly browse to an attacker controlled url,and any data we want to exfiltrate, we 00:16:07.099-->00:16:12.405 can put in as a git parametre. Again, the firewall will see this connection, but as it’s 00:16:12.405-->00:16:18.411 Safari, simply browsing to a url, it will not be locked. [Pause] Now, an even better way 00:16:18.411-->00:16:23.449 to bypass any installed firewall, is to abuse browsers command line interfaces. Really 00:16:23.449-->00:16:27.453 doesn’t get any easier than this. Are all major browsers now support a command light 00:16:27.453-->00:16:32.158 interface, so you can very easily programmatically execute them from a script, piece of 00:16:32.158-->00:16:37.063 malware, from the command line. And again, the firewall will see this connection, but it’s just 00:16:37.063-->00:16:42.969 the browser browsing to some url, so it will not lock it. [Pause] Yet another way to 00:16:42.969-->00:16:48.507 generically bypass all third party firewall products, is to simply inject a library or code 00:16:48.507-->00:16:54.313 into an application or a binary that the firewall trusts. And again via LSOF, we can determine 00:16:54.313-->00:17:00.786 passively what those are. Now once this code, this library is running in that trusted process, 00:17:00.786-->00:17:05.858 from the firewall’s point of view, it will also be given that same level of trust and thus can 00:17:05.858-->00:17:10.329 access the network. For example, if the browser’s trusted and you inject code into the browser, 00:17:10.329-->00:17:14.467 and that malicious code connects out, the firewall will allow it, because it just sees it’s the 00:17:14.467-->00:17:20.039 browser. [Pause] Now, there is many ways on Mac OS to inject into,especially third party 00:17:20.039-->00:17:24.710 applications, we don’t have time to go into all of them, but I have listed them here on the 00:17:24.710-->00:17:31.584 slide. [Pause] The final way to bypass kernels involves, or firewalls rather, involves 00:17:31.584-->00:17:36.289 getting code into the kernel. And the simple fact, is if an attacker is able to get code 00:17:36.289-->00:17:40.426 running in the kernel, it’s completely game over for the firewalls. Uh, first a lot of 00:17:40.426-->00:17:45.431 firewalls will generically allow traffic that is originated from the kernel. And secondarily, if 00:17:47.567-->00:17:52.772 malicious code is running in the kernel, it can actually unregister or unhook any 00:17:52.772-->00:17:58.010 installed socket filter driver. This will then transparently disable the firewall and then 00:17:58.010-->00:18:02.248 the attackers code, even from user mode, can’t connect out without having to worry about 00:18:02.248-->00:18:08.921 the firewall. Okay, so let’s start wrapping this all up. [Pause] So, today we talked 00:18:08.921-->00:18:14.327 about building a firewall for Mac OS, ah, we saw that using a socket filter kernel extension, 00:18:14.327-->00:18:19.532 really not that complicated. As I mentioned the source code for LuLu is on GitHub, so if you're 00:18:19.532-->00:18:24.737 interested in more ,of the details, and specifics, I would recommend checking that out. 00:18:24.737-->00:18:28.574 [Pause] We then talked about breaking firewalls, we showed that a lot of them actually have 00:18:28.574-->00:18:33.546 significant security flaws, which are very problematic, because the firewalls often run 00:18:33.546-->00:18:38.150 with root privileges or even in the kernel. And then we also showed that the unfortunate 00:18:38.150-->00:18:43.289 reality is, these products are all trivial to generically bypass. Now, this doesn’t mean 00:18:43.289-->00:18:48.260 we should uninstall our firewall products, but I think it makes a good case for a defence in depth 00:18:48.260-->00:18:54.200 strategy. For example, maybe some other host based security products that can perhaps block 00:18:54.200-->00:18:58.604 some of the prerequisites for these attacks. For example, per-ah preventing dial in 00:18:58.604-->00:19:04.243 injection or perhaps can detect that the browser is running from the command line when the user 00:19:04.243-->00:19:08.948 is inactive. Like, that’s not something a firewall should detect per say, but perhaps 00:19:08.948-->00:19:14.487 another security tool should. [Pause] And finally, today I’m really excited to announce a 00:19:14.487-->00:19:18.958 brand new Mac security conference, ah, called Objective by the Sea. We have a really 00:19:18.958-->00:19:23.295 cool line up of Mac security researchers, it’s also going to be at this awesome resort in 00:19:23.295-->00:19:26.966 Hawaii. And if you're a supporter of Objective See, which you can be for one dollar, 00:19:26.966-->00:19:32.138 the conference to attend, is completely free. [Pause] I just want to reiterate that it’s in 00:19:32.138-->00:19:37.576 Hawaii, this is the actual resort of where it’s gonna be. November third and fourth, ah, 00:19:37.576-->00:19:42.381 so I would love if you could all attend and, ah, for more information checkout, objective 00:19:42.381-->00:19:46.619 by the sea dot com. [Pause] Okay, that’s a wrap I really want to thank you all for 00:19:46.619-->00:19:50.556 attending, also want to tha-thank the friends, the partners of Objective See, which 00:19:50.556-->00:19:55.127 are Digitas security and Malwarebytes and we have 13 seconds for questions. But I 00:19:55.127-->00:20:00.132 will be around here after the talk, to obviously answer any other questions. [Applause].