00:00:00.200,00:00:05.939 >>Andy Grant’s here to to talk to us a little bit about uh Mac OS packaging. Thank you. 00:00:05.939,00:00:09.409 >>Awesome. Thank you. Yeah. So I’m going to talk about the packages used to install 00:00:09.409,00:00:16.416 software on Mac systems, on Apple software laptops. First I’m going to start a little bit 00:00:16.416,00:00:21.889 about myself, I’ve been doing professional security for 11 years focusing on application 00:00:21.889,00:00:28.495 security. Been doing security as a hobby for 20 plus years. I came to DefCon 9 as my first 00:00:28.495,00:00:35.369 DefCon. Uh I went up through self-taught ranks when I was in my early teens. Got a computer 00:00:35.369,00:00:40.407 science degree from Stanford University focusing on computer security. Went on to join the 00:00:40.407,00:00:45.479 comp the security consulting group, ISEC Partners. I was acquired by NCC Group and 00:00:45.479,00:00:51.518 continued to do that to today. I got started just doing the regular pen tests, looking at 00:00:51.518,00:00:57.457 applications, finding the vulnerabilities. Moved my rank way up the ranks to become um uh 00:00:57.457,00:01:03.130 the leader of the San Francisco team. Joined the management. Did that for 5 years. Ran a team of 00:01:03.130,00:01:08.802 30 people. Spent most of my time running that team of 30 people trying to find an excuse to do 00:01:08.802,00:01:12.773 the projects my team was supposed to be doing so I gave up the management stuff and I’m 00:01:12.773,00:01:17.778 back doing the technical work and here presenting today. [applause] Uh thank you. Uh I’m 00:01:20.147,00:01:26.820 probably best known though by my alias of Dana Vollmer’s husband and um and you can find out more 00:01:26.820,00:01:31.825 about me and my time off to support her career online. So we’re going to cut talk about 00:01:34.628,00:01:39.132 what lead me to looking at packages and installers. Then to open up the packages, look 00:01:39.132,00:01:44.137 inside ah and then see what can go wrong in uh with these installations. So first, I’ve 00:01:47.341,00:01:53.013 got trust issues. That’s whats got me into computer security at the start. Digging into what was 00:01:53.013,00:01:57.751 going on in my laptop. I wanted to better understand what was happening. You have these pop 00:01:57.751,00:02:01.755 ups sometimes when you’re installing software that says “This package is going to run a 00:02:01.755,00:02:06.293 program. Do you want to continue?” Then you don’t get to know what that program is or 00:02:06.293,00:02:11.798 what got ran. Just it’s going to run something. Plus I’m lucky enough that I get paid to do 00:02:11.798,00:02:17.204 this as I was saying and sometimes the client pays you to look at their application and 00:02:17.204,00:02:21.875 their application doesn’t work so really all you have to go on is the installer and the process 00:02:21.875,00:02:28.081 and then you kinda have to give them their money’s worth out of just that working environment. 00:02:28.081,00:02:34.654 So let’s look inside what these packages are. So their technical term, their full name is “Mac OS 00:02:34.654,00:02:40.494 X installer flat packages” that have a dot PKG extension. There’s not a whole lot of 00:02:40.494,00:02:45.832 documentation provided by Apple on them. Uh but you can find some reverse engineered 00:02:45.832,00:02:52.072 unofficial documentation but it’s kind of out of date and um and incomplete. Uh the flat 00:02:52.072,00:02:56.743 package aspect of this is that it’s a single file. It’s actually an eXtensible ARchive 00:02:56.743,00:03:01.681 or XAR and it in in pre 10 dot 5 OS X these used to be bundles uh bundle packages and so those 00:03:05.886,00:03:11.691 were more like directories like your dot app um package uh app bundles. There’s some helpful 00:03:11.691,00:03:17.931 tools. One even comes preinstalled on uh MAc OS called package util. This allows you to 00:03:17.931,00:03:23.103 inspect uh the packages installed on your system. Inspect a specific package and 00:03:23.103,00:03:28.375 go through and open open them up which is what we’re going to get into. There’s uh nifty uh third 00:03:28.375,00:03:30.377 party uh package out there that’s specifically designed to look at the security aspects of 00:03:30.377,00:03:32.913 these packages and that’s called “Suspicious Package”. It will highlight the code signing 00:03:32.913,00:03:34.915 signature. It’ll it’ll let you look at some the files that get run and will highlight some um 00:03:34.915,00:03:36.917 nuances of that application that might raise a red flag. If it might be a malware installation. 00:03:36.917,00:03:39.386 So let’s let’s look inside. What’s inside these? The easiest way to do it is to use your 00:03:39.386,00:03:43.590 preinstalled package util. Give it the expand command and then give it the package in the 00:03:43.590,00:03:45.592 output directory and it will expand everything out there. But we don’t want the easy way, we 00:03:45.592,00:03:50.597 want to do it the hacker way. We got trust issues. We don’t trust what package util is doing, we 00:03:54.101,00:03:59.106 want to make our own directory. We understand that command. We go into that directory because 00:04:05.879,00:04:11.518 the uh ways are to extract these files works it just um outputs right into the current working 00:04:11.518,00:04:16.590 directory and so now we’re going to go through and extract the package files. After you’ve 00:04:16.590,00:04:21.595 extracted them, this is sort of the typical structure you find inside. You have a distribution 00:04:27.601,00:04:29.603 XML file. You have a resources directory. And then you have one or more package directories. 00:04:29.603,00:04:31.605 These package directories contain a bill of materials, a package info, another plain text 00:04:31.605,00:04:33.607 XML file, and then two compressed archives, ah payload and scripts. So the distribution 00:04:33.607,00:04:38.612 file. This is a customization of the installer window. It says gives it the title, the welcome 00:04:52.959,00:04:54.961 text, if there’s a license, if there’s um ah readme, the background logo that that is 00:04:54.961,00:04:59.432 displayed, and sort of uh determines whether you restart or shutdown or don’t need to do 00:04:59.432,00:05:04.371 anything after the installation is complete. These can also include javascript. Specifically 00:05:10.443,00:05:17.083 a subset called installer JS and this is supposed to be what triggers that pop up of some uh 00:05:17.083,00:05:23.890 code is gonna run to check to see if your system’s compatible. It’s an compatibility check. You 00:05:23.890,00:05:28.328 have the package info. This is more information about the individual packages are just 00:05:28.328,00:05:32.632 installed with that distribution. It has the specific requirements of that 00:05:32.632,00:05:37.571 package. The install location where that payload’s uh that package payload will be 00:05:37.571,00:05:42.576 extracted to. And then it’ll also also include any paths to the scripts that will be run. 00:05:44.878,00:05:49.816 Then you have the bill of materials . This is um a special file that lists out all the 00:05:49.816,00:05:54.821 files that will be installed, updated, or removed during the installation process and it 00:05:54.821,00:06:00.260 includes the file permissions, the owner and group that they will have. The size of the files 00:06:00.260,00:06:05.265 and creation time and some other uh file meta data. Now we have these um compressed archives. 00:06:07.901,00:06:14.107 These archives are actually CPIO archives that have been gzip compressed. The payload includes 00:06:14.107,00:06:20.614 all those files that are gonna be installed, that are listed in the um bill of materials and the 00:06:20.614,00:06:24.985 installation process here is actually just extracting this archive into the 00:06:24.985,00:06:31.891 install-location specified in the package info. You have the scripts um if there’s any 00:06:31.891,00:06:37.897 scripts to be run before or afterwards. Same type of archives, CPIO archive. You have 00:06:37.897,00:06:42.869 pre-installation and post-installation scripts but really this can contain any 00:06:42.869,00:06:48.208 executable file that has an appropriate shebang at the first line so it’s typically bash, 00:06:48.208,00:06:53.613 python, perl. Uh this gets extracted um during by the installer into a random 00:06:53.613,00:06:58.618 directory and gets executed from that location. So to unpackage those packages, those archives 00:07:01.554,00:07:06.559 within within it you um you can go like I said, it’s a gzip CPIO file and so you can cap this. 00:07:09.062,00:07:14.067 The ah uh archive into the decompression, into the archive extraction or CPIO actually 00:07:16.569,00:07:22.609 natively detects that it’s been gzip compressed and so you can just put it right into the CPIO 00:07:22.609,00:07:27.947 and say its input and um and then away you go. It’ll dump right into the current directory 00:07:27.947,00:07:33.353 all the contents of that. If you did it the easy way to begin with, the expand command 00:07:33.353,00:07:38.358 actually goes ahead and extracts this archive already and instead of having the scripts um object 00:07:40.760,00:07:45.765 be a uh archive it is already a directory with the contents inside of it. You do the same 00:07:47.934,00:07:52.939 thing as scripts for payload. You go through the CPIO command. Um sometimes in going that 00:07:55.208,00:08:00.280 payload and extracting the files it has further packages in it so you just recurse through if you 00:08:00.280,00:08:05.685 want to find everything that’s going to happen during the course of your install. Unlike 00:08:05.685,00:08:11.725 with scripts though, the package util does not auto expand this and you do have to go through 00:08:11.725,00:08:15.862 the manual process of extracting the payload files if you want to see what’s going to be installed 00:08:15.862,00:08:22.068 on the system. So this walks through what happens when you actually launch the installer 00:08:22.068,00:08:28.608 with one of these packages. The you start with the distribution file. Um then we’re working 00:08:28.608,00:08:33.380 through sort of more than the code execution flow. So you it checks to see is there any 00:08:33.380,00:08:37.751 installer check, if so it looks for the java script that it’s going to be there that 00:08:37.751,00:08:43.390 javascript is defined within the XML file at between script tags and it goes through and executes 00:08:43.390,00:08:48.395 that javascript. Then if the package info de um said there was any pre-installation scripts 00:08:50.463,00:08:56.069 it goes and runs that pre-installation script. Then you install the files. You 00:08:56.069,00:09:01.408 extract the payload into the install location and then you do the post-installation scripts if 00:09:01.408,00:09:07.647 there’s any specified in the package info. So now we’re going to get into sort of the security 00:09:07.647,00:09:13.319 concerns that can happen during the installation that um that can occur. You have the scripts 00:09:13.319,00:09:17.490 in the payload which is where most of this is happening and the scripts you’re going to look 00:09:17.490,00:09:21.828 at the pre-installation, the post-installation. These can also include helper scripts and 00:09:21.828,00:09:25.899 other files so you’ll want to look at what’s going on there. The plain text like I was 00:09:25.899,00:09:30.270 saying, bash, python, perl, pretty easy to audit and understand what’s going on and 00:09:30.270,00:09:35.275 look for common mistakes. Within the payload you also have um you can also have more scripts. 00:09:38.111,00:09:42.282 You’ve I’ve seen an uninstallation scripts are easy to audit. Some helper scripts 00:09:42.282,00:09:47.187 for debugging or logging or error reporting that gets installed. And then you also 00:09:47.187,00:09:52.025 have your normal native apps that can solve the by the actual application binary, the kernel 00:09:52.025,00:09:56.396 modules, the libraries and if you want to go into it you can go and look at for native 00:09:56.396,00:10:03.002 applications files through reverse engineering, fire a popper. So the types of issues I 00:10:03.002,00:10:08.475 was seeing when looking at these packages largely amount to time of check, time of use, except 00:10:08.475,00:10:14.581 they weren’t doing any checks. There they’re operating within the temp directory which is 00:10:14.581,00:10:20.920 world writable by anybody and uh their doing reads out of it that are untrusted. They’re doing 00:10:20.920,00:10:24.891 writes into it that are untrusted, or they’re executing files in it without confirming 00:10:24.891,00:10:30.363 that file is what they believe it should be. I’ve also seen the um some of these installers go 00:10:30.363,00:10:35.535 through and set 777 permissions on what gets installed or what gets written into different 00:10:35.535,00:10:40.340 locations and that just allows everybody to mess with whatever you wrote there and it’s no 00:10:40.340,00:10:47.313 longer a trusted install. So now we’re going to dig into the actual vulnerabilities I gotta 00:10:47.313,00:10:53.186 see while looking at this for the past couple months. So I even found a number of privilege 00:10:53.186,00:10:59.759 escalation issues um did some symbolic link abuse uh and got some arbitrary code execution 00:10:59.759,00:11:05.832 which was fun. So the first one we have this installation we’re going to go through the payload 00:11:05.832,00:11:12.171 installs ah um and the installer util binary. That’s just a helper program for the 00:11:12.171,00:11:17.110 installation to go through and then the post install script actually calls that with sudo 00:11:17.110,00:11:22.582 with superuser powers and um and executes it. This is written to the temp directory. Temp 00:11:22.582,00:11:29.122 directory is world writable. This is actually um the payload is installing as a current user 00:11:29.122,00:11:35.061 um the way this installation is working and then the postinstall is calling that user owned uh 00:11:35.061,00:11:40.066 file with superuser powers. So in this instance I’m possibly an unprivileged user asking my IT 00:11:42.235,00:11:46.673 administrator to install video conferencing software so I can join a meeting later that 00:11:46.673,00:11:53.513 afternoon. So I would have audited this program, looked at at this, seen this vulnerability 00:11:53.513,00:11:58.418 and then written up a little script to run in the background. A while loop say while this file 00:11:58.418,00:12:04.324 doesn’t exist, do nothing and then when it when it does exist remove that file and put it in 00:12:04.324,00:12:10.096 my payload um in it’s place and then the post install is going to come around and execute that 00:12:10.096,00:12:15.134 with root privileges and I’ve just escalated from my unprivileged user to my IT 00:12:15.134,00:12:20.139 administrative’s group powers over my laptop. Another package I saw um during the 00:12:23.509,00:12:27.981 pre-installation tries to do some cleanup in case it previously ru run the installer. 00:12:27.981,00:12:33.252 It deletes this um install installation file in the temp directory and then later on it 00:12:33.252,00:12:38.057 writes to that and wants to make sure further installations unassociated with this install 00:12:38.057,00:12:43.062 can also write to it so it sets 777 permissions uh using sudo and then also makes the owner of 00:12:45.531,00:12:52.438 it um the current console user. So in this attack, any user on the system, any process that can 00:12:52.438,00:12:57.610 write to temp the temp directory should be all of them can attack the system administrator or 00:12:57.610,00:13:02.982 whoever’s inst installing the software. So you go ahead and you you touch that file ahead of 00:13:02.982,00:13:07.987 time so it exists so then you’re while loop can say while this file exists wait and do nothing 00:13:09.989,00:13:14.761 and then once that file has been deleted during the pre-installation go through and 00:13:14.761,00:13:20.266 create a symbolic link to a directory that you want to gain write access to such as slash 00:13:20.266,00:13:25.271 applications and link that to that file’s name. Now when they’re going through the 00:13:25.271,00:13:29.342 post-instillation it will try to write to this, it’ll fail. Things fail silently unless 00:13:29.342,00:13:33.946 you’re doing an explicit checking of fail receipts during these scripts. And then when you 00:13:33.946,00:13:38.951 to get to this sudo change permissions to 777 that’s going to follow that symbolic link and 00:13:41.320,00:13:47.460 make slash applications world writable. And then it’ll also go through and have the the 00:13:47.460,00:13:51.731 happenstance of change in the ownership of applications to the current user. At this point we 00:13:51.731,00:13:55.802 don’t really care about that because it’s world writable. We can drop whatever applications 00:13:55.802,00:14:00.740 we want onto this system now. Another package was privilege escalation. This is not to um 00:14:04.544,00:14:10.083 current user to root, this is any user to the current installing user’s privileges. So 00:14:10.083,00:14:16.789 they go through, they um during the preinstallation, in case it had run before, it deletes the 7 00:14:16.789,00:14:23.196 zip file um 7 zip arc uh binary in the temp directory and then it goes through and tries to 00:14:23.196,00:14:28.201 extract the 7 zip um file that came with the pu payload and then executes that 7 zip file. 00:14:30.470,00:14:35.241 This is all happening within slash temp. So pretty straightforward. Before this 00:14:35.241,00:14:40.780 installer is ever run I just put my payload as that name. You notice none of this is sudo. 00:14:40.780,00:14:46.285 This isn’t running with root privileges so they can’t delete my file that’s sitting there so 00:14:46.285,00:14:53.192 the RM fails. The overwrite from the unzip fails. But the execution happens because like I 00:14:53.192,00:14:59.499 said, these fail silently inside the scripts. So I just went from just put putting a a file there 00:14:59.499,00:15:03.002 on the system. The installer happily goes through and executes it as a current 00:15:03.002,00:15:08.007 installing user and I’ve gained privilege as them. Another uh example we have here is another 00:15:10.676,00:15:14.947 symbolic link attack uh helper script outside of the payload so once you’re running the 00:15:14.947,00:15:21.788 application this was for some debug reporting back to the um the developer. It tries to clean 00:15:21.788,00:15:26.926 up on some of the logging it was doing in the temp directory for to capture that debug log. And 00:15:26.926,00:15:33.766 so it does um an RM dash RF of this temp directory slash star and then it does a a remove the 00:15:33.766,00:15:39.238 deck directory and notably with a slash at the end of the path. So the attack here is before the 00:15:39.238,00:15:45.144 script’s ever run and any user can go and write to that um that temp directory. I have a typo in 00:15:45.144,00:15:49.682 my slide. It does say slash var I realize instead of slash temp but in the payload it should be 00:15:49.682,00:15:54.687 slash temp slash SDU. And the way the symbolic links work that RM dash RF with the slash star 00:15:56.722,00:16:00.560 will follow the symbolic link and delete everything in the user’s home directory in this 00:16:00.560,00:16:06.632 example and then the the remove directory command with the slash at the end will follow that 00:16:06.632,00:16:10.736 symbolic link and blow away their home directory also. So now here you’re here left with 00:16:10.736,00:16:15.741 the symbolic link that points to nothing because everything’s been deleted. Next we have a 00:16:18.110,00:16:24.483 vulnerability that um has in the package info it says we’re going extract a payload into slash 00:16:24.483,00:16:29.789 temp, specifically this razer synapse place using root privileges for running as the 00:16:29.789,00:16:34.927 root user. So you don’t even need sudo here, everything just runs as uh the super user. 00:16:34.927,00:16:38.898 During the post-installation we’re going to change into that directory and then we’re going 00:16:38.898,00:16:43.903 to look for star dot PKGs and install them once again with root privileges. So this has 00:16:45.938,00:16:51.577 been fixed by razer and so we’re going to use this as our demo today. Going through everything 00:16:51.577,00:16:55.514 I’ve talked about here. We’re going to download the package, um extract the files, audit the 00:16:55.514,00:17:01.020 package, find this um this vulnerability, develop an exploit for it and then drop the 00:17:01.020,00:17:06.025 payload and watch it take place. So here I go through and I download the package. I once 00:17:13.566,00:17:18.037 it’s on my system I’m going to go through the hacker way to create the directory, extract 00:17:18.037,00:17:23.843 the contents of that package into the directory, and then I’m going to go look at the files. 00:17:23.843,00:17:28.781 So I’m looking at the distribution file first to see if there’s any installer texts. 00:17:28.781,00:17:31.884 I don’t see any installer texts so I’m going to move on. I’m going to look at the package 00:17:31.884,00:17:36.923 info to see what’s going on. Here we have that it’s extracted into the temp directory, it’s 00:17:36.923,00:17:40.993 installing as root and there’s an extra little tidbit there that’s going to ask to restart 00:17:40.993,00:17:46.032 at the end. It also notes here that there’s a post-install script so let’s go see what’s 00:17:46.032,00:17:52.271 happening in that post-install script. We’re going to create our our collection dir directory 00:17:52.271,00:17:57.944 so we don’t clutter the current directory, extract the pay uh the scripts into it and then we 00:17:57.944,00:18:01.580 have all these extra files in here but we’re interested in what it named the post install 00:18:01.580,00:18:07.386 script. So now this is a whole post-install script. We’re reading it, we’re really fast at 00:18:07.386,00:18:13.025 finding vulnerabilities. Here’s a vulnerability that that we just looked at. So we’re going 00:18:13.025,00:18:16.162 to go through, we’re going to and and now we’re going to take advantage of this. Now that we 00:18:16.162,00:18:20.032 know about this vulnerability, we can become the malicious user. So mallory is going to 00:18:20.032,00:18:25.071 take over and she’s going to go through and develop the an a malicious payload, a package 00:18:25.071,00:18:30.776 that will be installed um as part of this exploit. So we create a scripts directory. We 00:18:30.776,00:18:36.816 create our pre-installation script. So we put in the shebang. We tack we just attach 00:18:36.816,00:18:42.254 a temp file and echo the current running user into it so we know what privilege this exploit runs 00:18:42.254,00:18:47.460 at and then every good proof of concept pocs calc and so we’re also poc calc as part of our 00:18:47.460,00:18:52.465 proof of concept. Now we have to make that executable or else it doesn’t run. And now we use 00:18:56.202,00:19:00.740 package build to make up our package. We can give it an identifier that isn’t really 00:19:00.740,00:19:05.511 necessary but it just shows another aspect that you can label and call what these are. 00:19:05.511,00:19:10.816 Here I specify that there’s no payload here. There’s no files being extracted and so it it 00:19:10.816,00:19:16.055 doesn’t try to look to create that payload archive. It’s a script only package. We’ll 00:19:16.055,00:19:21.293 create the package. I create that directory because it it um razer hasn’t been installed yet 00:19:21.293,00:19:27.533 and I move the payload into that directory. And now I’m done. Now I wait for Bob to come around 00:19:27.533,00:19:33.873 and run the installer. Bob’s going to do it right now. Bob’s not actually an administrator on 00:19:33.873,00:19:38.878 here so Alice is going to have to come and type in her admin password. It’s going to continue 00:19:41.280,00:19:47.753 through. Poc calc. And then we go and look at the temp directory file to see that we 00:19:47.753,00:19:52.758 just ran the root user here. [applause] In my final minute I want to um make some notes about 00:19:59.165,00:20:04.003 what we just observed, that there was um some unexpected things happen when you do this 00:20:04.003,00:20:09.008 no payload um package that I just created there. When you when you install anything with a 00:20:09.008,00:20:14.046 payload the installer creates a receipt and you can look in your system and all the packages that 00:20:14.046,00:20:18.417 have been installed by that identifier and then you can give the uh give the package util 00:20:18.417,00:20:23.389 that identifier and look at all the files that were installed during that process. However if 00:20:23.389,00:20:28.427 no files are installed because there’s no payload, there’s no receipt that that package 00:20:28.427,00:20:33.766 installer ever ran. So there’s no um appropriate system record that you could look up and audit 00:20:33.766,00:20:39.472 to see that this this ran. It also if you get super fancy, for minimal clicks and user 00:20:39.472,00:20:43.909 interaction, and you can actually have that installer check in the distribution XML 00:20:43.909,00:20:50.316 file that javascript can call system dot run and execute any scripts that are inside that 00:20:50.316,00:20:54.386 script file. So within the system check when it’s like “Do you want to check if your 00:20:54.386,00:20:59.992 system’s compatible?” you it will actually execute script which can extract an um a zip 00:20:59.992,00:21:04.263 file and copy those files around your system. And that’s a one click install instead of all the 00:21:04.263,00:21:09.268 multiple clicks you normally have to go through. And then for for extra fun you can then call 00:21:09.268,00:21:14.373 kill all installer and the installer quits itself and the user is immediately um back to 00:21:14.373,00:21:19.478 its desktop. Some of these uh tips and tricks have been used by the people over at 00:21:19.478,00:21:24.416 Praetorian. They wrote an awesome blog post series, 2 parts, um about bypassing 00:21:24.416,00:21:31.023 application whitelisting, specifically Google santa umm to uh using the installer um the 00:21:31.023,00:21:36.128 quick quick version of that is application whitelisting happens at the basically the process ex 00:21:36.128,00:21:41.901 executing level and installer is whitelisted reasonably and all of this installer check 00:21:41.901,00:21:47.873 javascript, all of the pre and post script scripts run as part of that installer process and so 00:21:47.873,00:21:54.880 they’re all whitelisted also. I’ll be stepping off to the stage as I think I ran a minute 00:21:54.880,00:21:59.084 long and so I don’t have time to take your questions right now but uh I’d be happy to talk to 00:21:59.084,00:22:03.856 you here. [applause]