Andy Grant here to talk to us a little bit about, uh, Mac OS packaging. Thank you. Awesome, thank you, yeah. So I'm gonna talk about the packages used to install software on Mac systems on Apple software laptops. First, I'm gonna start a little bit about myself. I've been doing professional security for 11 years, focusing on application security. I've been doing security as a hobby for 20 plus years. I came to DEF CON 9 as my first DEF CON. Uh, I went up through self-taught ranks when I was in my early teens. Got a computer science degree from Stanford University, focusing on computer security. Went on to join the com- the security consulting group, ISEC Partners. That was acquired by NCC Group and continue to do that to today. I got started just doing the regular pen tests, looking at, uh, applications, finding the vulnerabilities. Moved my rank- way up the ranks to become, um, uh, the leader of the San Francisco team. Joined the management. Did that for 5 years. Ran a team of 30 people. Spent most of my time running that team of 30 people, trying to find an excuse to do the projects my team was supposed to be doing. So I gave up the management stuff and I'm back doing the technical work and here presenting today. I- thank you. Uh, I'm probably best known though by my alias of Dana Vollmer's husband. And, um, and you can find out more about me and my time off to support her career online. So, we're going to cut- talk about what led me to looking at packages and installers, then open up the packages, look inside, uh, and then see what can go wrong in, uh, with these installations. So, first, I've got trust issues. That's what's got me into computer security at the start. Digging into what was going on with my computer security at the start. What was going on in my laptop. I wanted to better understand what was happening. You have these pop-ups sometimes when you're installing software that says, this package is going to run a program. Do you want to continue? And then you don't get to know what that program is or what got ran. Just, it's going to run something. Plus, I'm lucky enough that I get paid to do this, as I was saying. And sometimes the client pays you to look at their application and their application doesn't work. So really all you have to go on is the installer and the process and then you kind of have to give them their money's worth out of just that working environment. So let's look inside what these packages are. So, their technical term, their full name is Mac OS X Installer Flat Packages. They have a .PKG extension. There's not a whole lot of documentation provided by Apple on them. Uh, but you can find some reverse engineered unofficial documentation, but it's kind of out of date and, um, and incomplete. Uh, the flat package application, uh, is, uh, a single file. It's actually an extensible archive or czar. And it, you, in pre 10.5 OSX, these used to be bundles, uh, bundle packages. And so those were more like directories like your .app, um, package, uh, app bundles. There's some helpful tools. One even comes pre-installed on, uh, Mac OS called Package Util. This allows you to inspect, uh, the packages installed on your system, inspect a specific package. And, uh, it's, it's, uh, it's, uh, it's, uh, it's, uh, it's designed to look at the security aspects of these packages. And that's called suspicious package. It'll highlight the code signing signature. It'll, it'll let you look at the, some of the files that gets run. And we'll highlight some, um, nuances of the application that might raise a red flag if it might be a malware installation. So, let's, let's look inside. So, uh, what's inside these? The easiest way to do it is to use your pre-installed Package Util, give it the expand command, and then give it the package and the output directory, and it'll expand everything out there. But we don't want the easy way. We want to do it the hacker way. We got trust issues. We don't trust what Package Util is doing. We want to make our own directory. We understand that command. We go into that directory because the, uh, ways are to extract these files works. It just, um, outputs right into the current working directory. And so now we're going to go through and extract the package files. After you've extracted them, this is sort of the typical structure you find inside. You have a distribution XML file. You have a resources directory. And then you have one or more package directories. These package directories contain a bill of materials, a package info, another plain text XML file, and then two compressed archives, uh, payload and scripts. So the distribution file, this is the customization of the installer window. It says, gives it the title, the welcome text, if there's a license, if there's, um, a read me, the background logo that, that is displayed, and sort of, uh, determines whether you restart or shut down or don't need to do anything after the installation is complete. These can also include JavaScript, specifically a subset called installer JS. And this is supposed to be what triggers that pop-up of some, uh, code is going to run to check to see if your system's compatible. It's in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in updated or removed during the installation process and it includes their file permissions, the owner and group that they will have, the size of the files and creation time and some other uh file metadata. Now we have these um compressed archives. These archives are actually CPIO archives that are have been gzip compressed. The payload includes all those files that are going to be installed that are listed in the um bill of materials and the installation process here is actually just extracting this archive into the install location specified in the package info. You have the scripts um if there's any scripts to be run before or afterwards, same type of archive, CPIO archive. You have pre-installation and post-installation scripts but really this can contain any executable file that has an appropriate shebang at the first line. So it's typically bash, python, gzip. Perl. Uh this gets extracted um during the by the installer into a random directory and gets executed from that location. So to unpackage those packages, those archives within within it you um you can go like I said it's a gzip CPIO file and so you can cat this the pa- the archive into the decompression into the archive extraction or CPIO actually natively detects that expend gzip. Gzip compressed and so you can just put it right into the CPIO and say it's input and um and the away you go it'll dump right into the current directory all the contents of that. If you did it the easy way to begin with the expand command actually goes ahead and extracts this uh archive already and instead of having the scripts um object be a uh archive it is already a directory with the contents inside of it. You do the same thing as scripts. For payload you go through the CPIO command um sometimes in going through that payload and extracting the files it has further packages in it so you just recurse through if you want to find everything that's gonna happen during the course of your install. Unlike with scripts though the package util does not auto expand this and you do have to go through the manual process of extracting the payload files if you wanna see what's gonna be installed on the system. So let's go ahead and see what's going to happen. So let's go through what happens when you actually launch the installer with one of these packages. The you start with the distribution file um then we're working through sort of more the the code execution flow. So it checks to see is there any installer check? If so it looks for the javascript that's gonna be there that javascript is defined within the xml file at between script tags and it goes through and executes that javascript. Then if the package info de um said there was anything that was any pre-installation scripts it goes and runs that pre-installation script. Then you uh install the files you extract the payload into the install location and then you do the post installation scripts if there was any specified in the package info. So now we're gonna get into sort of the security concerns that can happen during the installation that um that that can occur. You have the scripts and the payload which is where most of this is happening. In the scripts you're gonna look at the pre-installation the post installation. These can also include helper scripts and other files so you'll wanna look at what's going on there. They're plain text like I was saying bash, python, pearl pretty easy to audit and understand what's going on and look for common mistakes. Within the payload you also have um you can also have more scripts you've I've seen uninstallation scripts that are easy to audit. Some helper scripts for debugging or logging or error reporting that gets installed. And then you also have your normal native apps that can be installed. So you can install the by the actual application binary the kernel modules the libraries and if you wanna go into it you can go and look at for native application flaws through reverse engineering fire up hopper. So the types of issues I was seeing when looking at these packages largely amount to time of check time of use except they weren't doing any checks. They're y- they're operating within the temp directory which is world writable by anybody. And uh they're doing reads and they're doing writes into it that are untrusted or they're executing files in it without confirming that file is what they believe it should be. I've also seen the um some of these installers go through and set 777 permissions on what gets installed or what's getting written into different locations and that just allows everybody to mess with whatever you wrote there and it's no longer a trusted install. So now we're gonna dig into the actual vulnerabilities I gotta see while looking at this for the past couple months. So I've found a number of privilege escalation issues um did some symbolic link abuse uh and got some arbitrary code execution which was fun. So the first one we have this installation we're gonna go through the payload installs uh um an installer util binary that's just a helper program for the installation to go through and then the post install script actually calls that with sudo with super user powers. And then we're gonna go through the installation and um and executes it. This is written to the temp directory. Temp directory is world writable. This is actually um the payload is installing as the current user um the way this installation is working. And then the post install is calling that user owned uh file with super user powers. So in this instance I'm possibly an unprivileged user asking my IT administrator to install video conferencing software so I can join a meeting later that afternoon. So I would I'm gonna go through the process. I would have audited this program looked at at this seen this vulnerability and then written up a little script to run in the background a while loop say while this file doesn't exist do nothing. And then when it when it does exist remove that file and put in my payload um in in its place and then the post install is gonna come around and execute that with root privileges and I've just escalated from my unprivileged user to my IT administrative's uh root powers over my laptop. Another package I saw um during the pre-installation it tries to do some clean up in case it had previously run the installer. It deletes this um install installation file in the temp directory and then later on it writes to that and wants to make sure further installations unassociated with this install can also write to it. So it sets 777 permissions using sudo and then also makes the owner of it um the current console user. So in this attack any user on the system any process that can write to temp the temp directory which should be all of them can attack the system administrator or whoever's installing this software. So you go ahead and you you touch that file ahead of time so it exists so then your while loop can say while this file exists wait and do nothing. And then once that file has been deleted during the pre-installation go through and create a symbolic link to a directory that you want to gain right access to. So that's what I'm trying to do with this. So right access to such as slash applications and link that to that file's name. Now when they're going through the post installation they'll try to write to this it'll fail things fail silently unless you're doing an explicit checking of failure states during these scripts. And then when you get to this sudo change permissions to 777 that's going to follow that symbolic link and make slash applications world writable. And then it'll also go through and have the the happenstance of changing the the file. So it's going to ownership of applications to the current user. At this point we don't really care about that because it's world writable we can drop whatever applications we want onto the system now. Another package was privilege escalation. This is not um current user to root this is any user to the current installing user's privileges. So they go through they um during the pre-installation in case it had run before it deletes the 7zip file um 7zip rsql file. And then it's going to go through and try to extract this file. And then it goes through and tries to extract this 7zip um file that came with the payload and then execute that 7zip file. This is all happening within slash temp. So pretty straight forward before this install was ever run I just put my payload as that name. You notice none of this is sudo this isn't running with root privileges. So they can't delete my file that's sitting there. So the rm fails. The overwrite from the unzip fails. But the execution happens. Because like I said these fail silently inside the scripts. So I just went from just put putting a a file there on the system. The installer happily goes through and executes it as the current installing user and I've gained the privilege as them. Another uh example we have here is another symbolic link attack uh helper script outside of the payload. So once you're running the application this was for some debug reporting back to the um the developer. It tries to clean up um some of the logging it was doing the temp directory for to capture that debug log. And so it does um an rm dash rf of this temp directory slash star and then it does uh uh remove the directory uh notably with a slash at the end of the path. So the attack here is before the scripts ever run any any user can go and write to that um that temp directory. I have a typo in my slides that say slash var I realize instead of slash temp. But it does a very good job of doing that. In the payload it should be slash temp slash SDU. And the way the symbolic links work that rm dash rf with a slash star will follow the symbolic link and delete everything in the user's home directory in this example. And then the the remove directory command with the slash at the end will follow that symbolic link and blow away their home directory also. So now you're here left with a symbolic link that points to nothing because everything's been deleted. Next we have a vulnerability attack. This is a very common problem that we've had in the past. We've had a vulnerability that um has in the package info it says we're gonna extract a payload into slash temp specifically this razor synapse place using root privileges. We're running as the root user. So you don't even need sudo here everything just runs as uh the super user. During the post installation we're gonna change into that directory and then we're gonna look for star dot pkgs and install them once again with root privileges. So this has been fixed by razor and so we're gonna use the root privileges as our demo today going through everything I've talked about here. We're gonna download the package um extract the files, audit the package, find this um this vulnerability, develop an exploit for it and then drop the payload and watch it take place. So here I go through and I download the package. Once it's on my system I'm gonna go through the hacker way to create the directory, extract the code and then I'm gonna go through the contents of that package into the directory and then I'm gonna go look at the file. So I'm looking at the distribution file first to see if there's any installer text. They don't see any installer text so I'm gonna move on. I'm gonna look at the package info to see what's going on. Here we have that it's extracted into the temp directory. It's installing as root and there's extra little tidbit there that it's gonna ask to restart at the end. It also notes here that there's a post install script. So let's go see what's happening in that post install script. We're gonna create our s- our our collection directory so we don't clutter the current directory. Extract the pa- the scripts into it and then we have all these extra files in here but we're interested in what it named, the post install script. So now this is the whole post install script. We're- we're reading it, we're really fast at finding vulnerabilities. Here's our vulnerability that- that we just looked at. So we're gonna go through, we're gonna and now we're gonna take advantage of this. Now that we know about this vulnerability we can become the malicious server. So we're gonna go through and now we're gonna take advantage of this user. So Mallory's gonna take over and she's gonna go through and develop the- a malicious payload, a package that will be installed, um, as part of this exploit. So we create our scripts directory. We create our pre-installation script. So we put in the shebang. We touch- we just touch a temp file and echo the current running user into it so we know what privilege this exploit runs at. And then every good proof of concept pops calc and so we'll also pop calc as part of our proof of concept. Now we have to make that executable or else it doesn't run. And now we use package build to make up our package. We can give it identifier. This isn't really necessary but it just shows another aspect that you can label and call what these are. Here I specify that there's no payload here. There's no files being extracted and so it doesn't try to look to create that payload archive. I script only package. I create the package. I create that directory build. I create the directory because, um, Razor hasn't been installed yet and I move the payload into that directory. And now I'm done. Now I wait for Bob to come around and run the installer. Bob's gonna do it right now. Bob's not actually an administrator on here so Alice is gonna have to come and type in her admin password. It's gonna continue through. Pop calc. And then we go and look at the temp directory file to see that we just ran as the user here. In my final minute I wanted to, um, make some notes about what we just observed. That there was, um, some unexpected things happen when you do this no payload, um, package that I just created there. When you, when you install anything with a payload, the installer creates a receipt and you can, you can look at your system, all the packages that have been installed by that identifier and then you can give the pa- the package util that identifier and look at all the files that were installed during that process. However, if no files are installed because there's no payload, there's no receipt that that package installer ever ran. So there's no, um, appropriate system record that you can look up and audit to see that this, this ran. It also, if you get super fancy for minimal clicks and user interaction, you can actually have that installer check in the distribution XML file. That JavaScript can call system.run and execute any of the scripts that are inside that script file. So within the system check when it's like do you want to check if your system is compatible you it will actually execute script which can extract an um a zip file and copy those files around your system and that's a one click install instead of all the multiple clicks you normally have to go through. And then for for extra fun you can then call kill all installer and the installer quits itself and the user is immediately um back to its desktop. Some of these uh tips and tricks have been used by the people over at Proterra and they wrote an awesome blog post series two parts um about bypassing application whitelisting specifically Google Santa um to uh using the installer. Um the quick quick version of that is application whitelisting happens at the basically the the process executing level and installer is whitelisted reasonably and all of this installer check javascript all the pre and post script scripts run as part of that installer process and so they're all. whitelisted also. I'll be stepping off to the stage as I think I ran a minute long and so I don't have time to take your questions right now but uh I'll be happy to talk to you here.