1 00:00:00,000 --> 00:00:13,680 Okay. I'd like to introduce our next speaker. JBO is from DC9723 in Tel Aviv, Israel. Jonathan 2 00:00:13,680 --> 00:00:19,680 Barr or JBO is presenting the sandcastle, the fatal Immaculate Sandbox through the lens 3 00:00:19,680 --> 00:00:24,840 of office macros. Now, for those who don't know, JBO is a regular speaker here and his 4 00:00:24,840 --> 00:00:31,560 presentations are usually very, very good. So I'd like to welcome you here, JBO. The macOS 5 00:00:31,560 --> 00:00:38,040 Sandbox is a powerful tool for application security and hardened macOS office to a point 6 00:00:38,040 --> 00:00:46,440 where they're not wildly used as an entry vector. Or are they? In this talk, we will 7 00:00:46,440 --> 00:00:53,640 give dive into sandbox escape mechanisms on macOS, as well as present a few techniques 8 00:00:53,640 --> 00:01:00,360 for potential generic sandbox escapes. John Barr or JBO is a principal security researcher at 9 00:01:00,360 --> 00:01:07,240 Microsoft working as the Microsoft defender research archival for cross-platform. Jonathan has 10 00:01:07,240 --> 00:01:13,240 rich experience in vulnerability research, exploitation, cryptanalysis, and offensive 11 00:01:13,240 --> 00:01:20,200 security in general. So we welcome you JBO and take it away. Thank you and thank you for having me. 12 00:01:20,200 --> 00:01:28,120 It's a real pleasure. I couldn't make it to DEF CON physically this year. So I am grateful for the 13 00:01:28,120 --> 00:01:36,680 opportunity. With that, let's get started, I guess. I got the introduction already. So not a lot of 14 00:01:36,680 --> 00:01:44,200 other stuff. I would say that I work at Microsoft. I think it was mentioned. I'm a security research 15 00:01:44,200 --> 00:01:52,440 architect at Microsoft. I focus on Linux, macOS, iOS, Android, routers, cameras, Chrome OS. So 16 00:01:52,440 --> 00:01:58,200 interestingly things that normally do not run Windows. One other thing that I can say is that 17 00:02:00,200 --> 00:02:04,440 I'm in charge of internal offensive security for Defender. And that includes simulating attacks, 18 00:02:04,440 --> 00:02:10,200 tampering attempts, and continuous red teaming. And you can feel free to follow me on Twitter or 19 00:02:11,080 --> 00:02:19,560 however it's called. I post once in a while. So a bit of motivation about Office macros. In Windows, 20 00:02:19,560 --> 00:02:27,080 Office macros are basically a very common method of gaining initial entry vector. On Windows, you 21 00:02:27,080 --> 00:02:34,440 have something called Defender application guard or up guard for short. And it's actually getting 22 00:02:34,440 --> 00:02:41,960 deprecated. But the idea, which was a very good idea, is to use, basically, spin up another virtual 23 00:02:41,960 --> 00:02:50,040 machine right next to your host that would present the document there. So any attacks that 24 00:02:50,040 --> 00:02:56,200 happen, happen on that virtual machine. But that's no longer relevant. Because A, it's getting 25 00:02:56,200 --> 00:03:02,280 deprecated. And B, it's only in Windows. In macOS, however, you don't have that kind of technology. 26 00:03:02,280 --> 00:03:06,120 And what happens really is that apps, generally in Office in particular, 27 00:03:07,560 --> 00:03:13,080 is heavily sandboxed by something called the macOS sandbox. And there is this question on how much 28 00:03:13,080 --> 00:03:19,720 are Office macros used as an entry vector these days, right? So that's the question that we'd 29 00:03:19,720 --> 00:03:26,040 like to answer today. A bit about the macOS sandbox, if you guys are not familiar, it's a set of 30 00:03:26,040 --> 00:03:32,920 customizable rules that are enforced on macOS apps, basically things like, oh, can my app access the 31 00:03:32,920 --> 00:03:39,160 network? Or what kind of files does an app have access to? Or can an app print the document? 32 00:03:39,160 --> 00:03:46,520 And so on and so forth. And it's well documented by Apple. And interestingly, to publish in the 33 00:03:46,520 --> 00:03:52,760 App Store, in the Apple App Store, your app must have sandboxing rules. So that's something that 34 00:03:52,760 --> 00:04:00,680 is very Apple-esque to do, to force developers into security. And obviously, if you have a process 35 00:04:00,680 --> 00:04:07,080 that runs inside an app, the child processes of that process are also sandboxed, because otherwise, 36 00:04:07,080 --> 00:04:11,480 what's the point, right? To create the child process. And if it's not sandboxed, then game over. 37 00:04:13,720 --> 00:04:20,360 And you can view the macOS sandbox with the code sign utility. I hope that you guys are able to 38 00:04:20,360 --> 00:04:26,360 see the screen well. The idea is that you don't have to remember anything here, but this is basically 39 00:04:26,360 --> 00:04:33,000 the Microsoft Word app. And in the Word app, you can start seeing all the entitlements that the app 40 00:04:33,000 --> 00:04:41,800 has. And some of them are basically the sandbox profiles that the app has. And this is where 41 00:04:41,800 --> 00:04:46,520 journey began. We'll start with some historical data, and then I'll show my original research. So 42 00:04:47,400 --> 00:04:54,920 the Office sandbox rules in the past basically have this kind of sandbox rule. And you can see, 43 00:04:54,920 --> 00:05:00,440 you don't have to understand the syntax or the dialect here. But it basically says, you can allow 44 00:05:00,440 --> 00:05:06,440 all file reads and all file writes on everything that is a regular file that starts with, that 45 00:05:06,440 --> 00:05:13,720 adheres to this regular expression, which basically starts with a tilde and a dollar sign. That's 46 00:05:14,440 --> 00:05:19,080 basically what it says. And if you remember from Windows, whenever you double-clicked a document, 47 00:05:19,080 --> 00:05:25,800 it would show you some sort of like a weird tilde dollar, whatever dot doc x, for instance. And 48 00:05:25,800 --> 00:05:32,600 apparently, Microsoft Office uses that to cache some data about the document. So basically, 49 00:05:32,600 --> 00:05:38,760 when Microsoft decided to start Office for Mac, they decided to move that functionality. But it 50 00:05:38,840 --> 00:05:47,240 basically means that you have to allow writing to those kind of files. So that's kind of in 2018, 51 00:05:47,800 --> 00:05:57,880 basically. It was found by Adam Chester, I believe, from MBSEC, that you can drop this poc.pillist 52 00:05:57,880 --> 00:06:04,440 that starts with a tilde and then a dollar sign to a library, a lunch agent. And this, if you're 53 00:06:04,440 --> 00:06:09,160 unfamiliar with Mac, this basically registers a lunch agent, which is kind of like a service, 54 00:06:09,720 --> 00:06:16,120 but not really. And basically, the idea is that those lunch agents are unsandboxed, right? So 55 00:06:16,120 --> 00:06:20,680 it's like writing to the registry, if you will, if you're coming from a Windows background. 56 00:06:22,120 --> 00:06:28,120 So that's like a very logical sandbox escape. It's the only sandbox escape that is very 57 00:06:28,120 --> 00:06:37,240 office specific. And it's still worth mentioning. So basically, that was the idea. 58 00:06:38,840 --> 00:06:45,320 Lunch agents, as I said, is a persistence mechanism on macOS as well. And it's launched by 59 00:06:45,320 --> 00:06:50,840 LaunchD, which is the number one process, literally, PID equals one, which is not sandboxed. 60 00:06:50,840 --> 00:06:56,520 And this is how the attack looks like. So you have word dumping, let's say with a word macro, 61 00:06:57,480 --> 00:07:04,440 tilde dollar poc.pillist, and then LaunchD, which is the lunch demon kind of reads that 62 00:07:04,440 --> 00:07:09,320 pillars from the lunch agents directory, and then launches it, right? Then you can do whatever you 63 00:07:09,320 --> 00:07:16,120 want here. And the conclusion here, and that's the main takeaway, is that making unsandbox 64 00:07:16,120 --> 00:07:21,000 processes, execute child processes is a good idea for attackers, if you want to escape the sandbox. 65 00:07:21,720 --> 00:07:29,880 So that was the attack in 2018. The fix was a Microsoft fix. It's not an Apple fix because 66 00:07:29,880 --> 00:07:36,840 this was specifically in the sandboxing rules. It's basically very easy. It says something like, 67 00:07:37,400 --> 00:07:44,680 you can read or write files that have these regex just as before, but you will deny all of the 68 00:07:45,640 --> 00:07:52,040 all of the file reads and file writes, file writes in particular, sorry, that either start in the 69 00:07:52,040 --> 00:07:57,640 home directory, library, lunch agents, that's number one. And number two is library application 70 00:07:57,640 --> 00:08:04,840 scripts, which is another directory that that could be abused. We will talk about that later, 71 00:08:04,840 --> 00:08:12,040 so keep that in mind. But basically, the library, lunch agents drop is now is now denied by the 72 00:08:12,120 --> 00:08:19,480 sandbox. So that's like, you know, one off kind of fix. It's not a deep fix. It basically says, 73 00:08:19,480 --> 00:08:27,720 oh, no more lunch agents for you. So the next couple of sandbox escapes that I will mention, 74 00:08:27,720 --> 00:08:34,760 again, our history shows us, are going to bypass with IPC with interprocess communications. 75 00:08:35,320 --> 00:08:43,240 And in this case, we're going to use archives. The lunch D process that I mentioned before, 76 00:08:43,880 --> 00:08:50,760 this guy, the one that runs the pillars file, controls lunch agents and lunch demons, 77 00:08:50,760 --> 00:08:56,680 which is another type of kind of like a service in macOS. It basically controls many other things 78 00:08:56,680 --> 00:09:03,080 in macOS. And when I say many other things, I mean, mostly everything. And one other thing 79 00:09:03,080 --> 00:09:07,960 that launched the controls is is launching apps when you double click something, right? 80 00:09:07,960 --> 00:09:13,640 And this feature is known as lunch services. And you can double click or just run the user 81 00:09:13,640 --> 00:09:21,080 being open command, which is which will basically open the default thing. And it also opens like, 82 00:09:21,080 --> 00:09:27,080 for instance, if you do open on an HTTP, it will open your default browser. Or if you open on a 83 00:09:27,080 --> 00:09:32,040 text file, it knows the association between text file and the default app. And it will show you 84 00:09:32,280 --> 00:09:38,040 it will open the right app for you. So that's kind of like the functionality of lunch services. 85 00:09:39,000 --> 00:09:43,960 It's able to launch apps. And lunch D will only launch apps that are already installed, 86 00:09:43,960 --> 00:09:51,880 obviously. You can't just say, hey, run this process. It has to be an app. And there is a 87 00:09:51,880 --> 00:09:58,680 difference between apps and processes in macOS. Basically, an app is a directory structure that 88 00:09:58,680 --> 00:10:04,440 will also have an executable in it. So it has to be a specific directory structure. 89 00:10:05,080 --> 00:10:09,320 And there is this question of what apps are already installed and can run code. 90 00:10:11,880 --> 00:10:19,320 And we have an app called terminal. The terminal app basically is the one that runs ZSH or bash, 91 00:10:19,320 --> 00:10:27,720 if you will, but it's ZSH is the default one in macOS. And it will run a file called .zshn. 92 00:10:27,720 --> 00:10:34,120 If you drop .zshn to the home directory, it will just run whatever commands that are in there. 93 00:10:34,120 --> 00:10:40,760 The issue is that you can't write .zshn file because you have file name restrictions. You 94 00:10:40,760 --> 00:10:47,000 have to start with a tilde and then a dollar sign. And then it was found by mad have bat, 95 00:10:47,000 --> 00:10:53,080 and I'm sorry that I butcher his name, to use something called the archive utility. 96 00:10:53,080 --> 00:10:57,640 The archive utility is another app. And interestingly enough, it's the default app 97 00:10:57,640 --> 00:11:01,960 that opens archives. Interestingly, it automatically extracts files in the same 98 00:11:01,960 --> 00:11:08,680 directory as the archive. No questions asked. No user interaction by default. So that's awesome. 99 00:11:09,320 --> 00:11:16,040 And that's the exploit. You basically drop it tilde dollar poc.zip in the user's home folder, 100 00:11:16,040 --> 00:11:21,800 and that contains the malicious .zshn file. If you remember, we can drop any file that starts 101 00:11:21,800 --> 00:11:26,760 with a tilde dollar sign. And then basically, you trigger the open command to extract, 102 00:11:27,480 --> 00:11:33,400 to basically kind of simulate double click on this zip file. Because of that, the archive 103 00:11:33,400 --> 00:11:39,560 utility, which is not sandboxed because it's run by launch d, will extract the zip file and create 104 00:11:39,560 --> 00:11:46,520 the .zshn file. And then you can just trigger terminal to automatically run the commands from 105 00:11:46,520 --> 00:11:53,880 .zshn. So this is how it looks like. You have Word. It will dump the zip file. You can do that 106 00:11:53,880 --> 00:12:00,600 because of the file name is okay. The file name itself, the file itself, the contents are attacker 107 00:12:00,600 --> 00:12:06,920 controlled completely. So the attacker can prepare a zip file with this name that contains .zshn file 108 00:12:06,920 --> 00:12:13,400 with malicious commands in them. Word will use the interprocess communication to launch d. I 109 00:12:13,400 --> 00:12:21,160 didn't mention that, but the open command that is used by the exploit here basically uses xpc, 110 00:12:21,160 --> 00:12:27,400 which is kind of like a weird IPC and macOS, to tell launch d, hey, please open this zip file. 111 00:12:27,960 --> 00:12:33,640 Launch d says, okay, zip files are associated with the archive utility. Archive utility is now on 112 00:12:33,640 --> 00:12:41,000 sandboxed and will extract the poc.zip file. It will extract the .zshn file. Again, because it's 113 00:12:41,000 --> 00:12:46,920 not sandboxed, it can do so and write arbitrary files. And then Word uses one more time to talk 114 00:12:46,920 --> 00:12:52,920 to launch d to open terminal. Terminal, when it starts, will read the .zshn file and run arbitrary 115 00:12:52,920 --> 00:13:01,240 commands. So that's the exploit from 2020. This, unlike the first exploit that we've seen, this 116 00:13:01,240 --> 00:13:08,520 is an Apple issue, right? And what Apple has done, has done basically is to fix the archive utility. 117 00:13:08,520 --> 00:13:14,120 The archive utility will extract to the user's downloads folder instead of the home folder if 118 00:13:14,120 --> 00:13:20,520 it's coming from a sandboxed app. You can still probably use that for some side loading or something, 119 00:13:20,520 --> 00:13:26,920 but it's given how hardened macOS has become. I don't know if it's realistic, but you can still 120 00:13:26,920 --> 00:13:33,640 use this functionality. So that's the first thing. The other thing that we let's keep in our minds is 121 00:13:33,640 --> 00:13:39,400 that dumped files from sandbox apps from Word specifically will have the com Apple quarantine 122 00:13:39,400 --> 00:13:47,080 extended attributes attached for identification purposes. This is not an extended attribute 123 00:13:48,120 --> 00:13:53,560 talk, but basically imagine it as if it's a metadata that is kept on the file system 124 00:13:54,520 --> 00:13:59,800 associated with the file that is dropped. And if you are familiar with this extended 125 00:13:59,880 --> 00:14:04,040 attribute, so yes, this is the same extended attribute that is used by Apple's gatekeeper to 126 00:14:04,040 --> 00:14:11,000 enforce certain things. It makes a lot of sense because you don't want a sandbox app to drop another 127 00:14:11,000 --> 00:14:18,520 app, for instance. The other thing that I will mention is that you can't exploit terminal-based 128 00:14:18,520 --> 00:14:24,040 sandbox escapes because terminal now refuses to run anything that has com Apple quarantine. 129 00:14:24,760 --> 00:14:32,600 So that's just another thing. In 2021, PerceptionPoint found the following idea. 130 00:14:33,240 --> 00:14:39,160 Sandbox apps can basically write to their own special container directory. For every sandbox 131 00:14:39,160 --> 00:14:45,560 app, it has a special container directory that they can write arbitrary contents to. It's a 132 00:14:45,560 --> 00:14:51,640 special directory. You can't write outside of that directory unless it's allowed by the sandboxing 133 00:14:51,640 --> 00:14:56,200 rules, but the sandboxing rules do not apply to this container directory. It's completely 134 00:14:56,200 --> 00:15:03,080 unrestricted for all means and purposes. So the idea is that you can use the open command, 135 00:15:03,720 --> 00:15:10,920 and the open command has specific features. One of them is a dash, dash, and it controls 136 00:15:10,920 --> 00:15:18,600 environment variables. You can basically say, tell launch D, hey, start this app, but with this 137 00:15:18,600 --> 00:15:22,520 environment variable in mind. You can set arbitrary environment variables, and that 138 00:15:22,520 --> 00:15:30,440 gives a lot of power to an attacker. The idea is to basically dump a .csh end file, just like 139 00:15:30,440 --> 00:15:36,440 before. You dump it in your own container directory, which you can write to because it's 140 00:15:36,440 --> 00:15:41,480 your own container directory. You can write whatever you want there, and then basically tell 141 00:15:41,480 --> 00:15:48,840 launch D, hey, start the terminal app, but the home directory for the terminal app 142 00:15:48,840 --> 00:15:54,840 is now my own container directory. Because of that, terminal will start, and we'll look for 143 00:15:54,840 --> 00:16:02,600 zsh end file in the home directory. Because the home directory is spoofed, basically it will run 144 00:16:02,600 --> 00:16:11,320 the .csh end file, and that's game over. So this is how it looks like. Word dumps the .csh end file 145 00:16:11,480 --> 00:16:19,000 to the container directory, which it can do. It will use xpc to talk to launch D to start 146 00:16:19,000 --> 00:16:25,320 terminal with a spoofed home directory, which is the container directory. Terminal starts, 147 00:16:25,320 --> 00:16:32,680 looks for a .csh end file in the home directory, which again is spoofed, and will basically run 148 00:16:32,680 --> 00:16:38,200 this file. So that was a very cool sandbox escape. Everything is very logical, very reliable. 149 00:16:39,080 --> 00:16:47,080 Apple fixed that in the terminal app now. The terminal app locks certain environment 150 00:16:47,080 --> 00:16:53,080 variables now. It basically does not allow home to be overridden even by launch D, and there is 151 00:16:53,080 --> 00:16:58,840 one more honorable mention from 2022 by Vojcek Regula that abused something very similar, 152 00:16:59,720 --> 00:17:05,160 another environment variable called OS install environment to do something very similar. 153 00:17:06,120 --> 00:17:10,120 So that's the first thing. The second thing is that terminal now refuses to run 154 00:17:10,120 --> 00:17:18,280 quarantine script files as well. So that's the fix. Now, this is my own discovery from 2022. 155 00:17:19,160 --> 00:17:25,320 Back in the day, Python came pre-installed as an app, and terminal refuses to run files with 156 00:17:25,320 --> 00:17:32,920 com-apple-quarantine extended attribute that includes .csh-banf-sh, and Python also refuses 157 00:17:32,920 --> 00:17:38,760 to run that. So if you had a Python file, you could drop it from Word, but it won't help you 158 00:17:38,760 --> 00:17:45,240 that much because you can't have Python run it because of this extended attribute. Also, terminal, 159 00:17:45,240 --> 00:17:53,880 if you remember, part of this fix, they locked the home directory, the home environment variable, 160 00:17:53,880 --> 00:17:59,560 the OS install environment variable, and also the Python startup environment variable, 161 00:17:59,560 --> 00:18:07,800 which you could have abused, perhaps. And then I was looking at other interesting flags to the 162 00:18:07,800 --> 00:18:17,480 open command, and there I found the stdn flag to the open utility, and what it will do, 163 00:18:17,480 --> 00:18:22,520 it will launch the application with the standard input connected to a path. So it's kind of like 164 00:18:22,520 --> 00:18:27,640 a neat way to override the standard input instead of being your keyboard. You can just say, 165 00:18:27,640 --> 00:18:33,080 hey, the standard input is this file. And then the sandbox escape is kind of trivial. You can write 166 00:18:33,080 --> 00:18:40,120 a tilde dollar poc.py, and instead of having Python executed directly, you can supply it as 167 00:18:40,120 --> 00:18:47,480 the standard input to Python. It doesn't matter that it has the quarantine extended attribute 168 00:18:47,480 --> 00:18:54,200 because you can still read the file from Python and treat it as the standard input. So this is 169 00:18:54,280 --> 00:19:00,040 how the exploit looks like. Very simple. You dump poc.py, just like that, you use 170 00:19:01,160 --> 00:19:07,640 xpc to talk to launch the, which will run Python, which will read the poc.py as the standard input 171 00:19:07,640 --> 00:19:14,680 and basically run it. So the most simple exploit ever. And that was like two years ago. This was 172 00:19:14,680 --> 00:19:20,600 the history lesson that we had. The current state in 2024. The current state is that 173 00:19:21,240 --> 00:19:26,760 launch services, everything is super duper hardened now. Launch services checks the xpc 174 00:19:26,760 --> 00:19:33,160 client for sandboxing rules. And this means that the entire idea of using open, just like we've 175 00:19:33,160 --> 00:19:39,240 done three or four times already in this talk, all of those tricks are essentially dead. You won't 176 00:19:39,240 --> 00:19:46,120 be able to use that as far as I've tried. And look, you can't really abuse that anymore. 177 00:19:46,920 --> 00:19:53,320 That's issue number one. That's the biggest issue. Other issues are that Python isn't 178 00:19:53,320 --> 00:19:59,400 pre-installed anymore. So that kind of sucks for attackers. I like Python. Red timers can't rely 179 00:19:59,400 --> 00:20:09,640 only on inter architecture between 2022 and 2024. ARM is, I guess, the main focus of Apple, 180 00:20:09,640 --> 00:20:16,360 even though they still support Intel quite a lot. So you need to know kind of where you land, 181 00:20:16,360 --> 00:20:21,560 but there is a thing called universal binaries if you want to attack a box. And there is another 182 00:20:21,560 --> 00:20:27,480 thing called hardened runtime. Hardened runtime, I won't go into lots of details, but it basically 183 00:20:27,480 --> 00:20:34,920 means that injections are very, very, very, very challenging. Most of the apps that you'll find 184 00:20:35,880 --> 00:20:42,600 have hardened runtime and launch constraints and other things that will really not allow you to 185 00:20:42,600 --> 00:20:49,480 do a lot as an attacker. So again, there is this question, are VBA macros, are malicious office 186 00:20:49,480 --> 00:20:56,680 documents still a thing in macOS? So I asked around, and this is February 9th, I asked around 187 00:20:56,680 --> 00:21:02,920 February this year, and it seems like not so much. The last thing I've seen was Mac Stiller abusing 188 00:21:02,920 --> 00:21:09,480 my own vulnerability, the one from 2022, which kind of like, I guess, an honor, but also sucks 189 00:21:09,480 --> 00:21:16,360 to realize that you have kind of helped with a vulnerability disclosure. By the way, every 190 00:21:16,360 --> 00:21:22,200 vulnerability that I disclose is a responsible disclosure and it will drop zero days. So what 191 00:21:22,200 --> 00:21:28,280 I'm going to talk about today is also disclosed to Apple. So don't run and code your sandbox 192 00:21:28,280 --> 00:21:39,560 escapes immediately after this talk. So that kind of sucked to know that Mac Stiller stole my exploit. 193 00:21:39,560 --> 00:21:47,480 And then Greg here said, pointed out to the exploit itself, it's basically the POC that I had from 194 00:21:47,480 --> 00:21:55,480 2022. So they didn't even change the POC, that kind of sucks. Anyway, so when you start something 195 00:21:55,480 --> 00:22:02,120 fresh, you're like, okay, let's look at office because people still need to run their macros, 196 00:22:02,120 --> 00:22:07,480 right? Macros are not created on office for attackers. They're created for legitimate business 197 00:22:07,480 --> 00:22:14,520 purposes. I know that it's hard to believe, but that's the case. And it does say in the Microsoft 198 00:22:14,520 --> 00:22:18,840 website, interestingly, I know that it sounds funny, I am a Microsoft employee, but sometimes it's 199 00:22:18,840 --> 00:22:24,840 easier to read the public documents rather than reading the code or, you know, scouring for some 200 00:22:24,840 --> 00:22:31,640 internal documentation. And it's very, very interesting to see that the public documentation 201 00:22:31,640 --> 00:22:39,000 says that Office 2016 and above for Mac is sandboxed. And it says that unlike, I'll read it here, 202 00:22:39,000 --> 00:22:48,920 unlike previous Office apps that support VBA, VBA and the Mac Office Mac app is totally sandboxed. 203 00:22:49,000 --> 00:22:55,320 The sandboxing restricts apps from accessing resources outside of the app container. The app 204 00:22:55,320 --> 00:23:00,440 container is the fancy directory that I was talking about three slides ago. And this affects 205 00:23:00,440 --> 00:23:08,200 add-ins as well as the VBA itself. And the idea is that they've added a new VBA command specifically 206 00:23:08,200 --> 00:23:13,720 for Mac for Office 2016 and above. And these are the commands. I'll call them two and a half commands 207 00:23:13,720 --> 00:23:19,800 because one of them is basically just basically gives you the Office version, doesn't do much for 208 00:23:19,800 --> 00:23:25,880 attackers. It's helpful for developers, obviously. And those two commands are grant access to 209 00:23:25,880 --> 00:23:30,440 multiple files, which sounds very interesting. And Apple script tasks, which sounds even more 210 00:23:30,440 --> 00:23:37,000 interesting. So we will assess both of them and try to understand what could be done by attackers. 211 00:23:38,760 --> 00:23:43,320 First of all, code execution is what we want. And the most direct approach is usually the 212 00:23:43,320 --> 00:23:49,320 right thing. Apple script tasks basically calls an external Apple script script. Now, 213 00:23:49,320 --> 00:23:54,520 Apple script is kind of like, in my opinion, so don't quote, well, do quote me. It's kind of like 214 00:23:54,520 --> 00:24:01,560 an abomination of script languages, in my opinion. It's very annoying. I dislike the syntax, but it 215 00:24:01,560 --> 00:24:09,240 is a thing on Mac OS and iOS. You can use Apple scripts to basically as a scripting language. 216 00:24:10,200 --> 00:24:19,240 It is still there in Mac. It's there by default. So Office has allowed to run an arbitrary Apple 217 00:24:19,240 --> 00:24:28,200 script from VBA, from VB Macros. And the idea is that there is an, you call this, 218 00:24:29,800 --> 00:24:34,920 call this Apple script task command or function, and you give an Apple script 219 00:24:35,640 --> 00:24:40,600 file name and the handler and stuff like that. It will basically run it outside the sandbox. 220 00:24:42,440 --> 00:24:47,560 And interestingly, that script file must be under the home directory, launch application scripts, 221 00:24:47,560 --> 00:24:55,320 bundle ID. Bundle ID is like com.microsoft.word, for word, for instance. And if you remember from 222 00:24:55,320 --> 00:25:03,640 slide number five or six, I don't remember by heart, where I mentioned that when Microsoft fixed the 223 00:25:04,360 --> 00:25:11,960 Office sandboxing rules, they didn't just fix the launch agents to be prohibited. 224 00:25:11,960 --> 00:25:17,960 They also added this directory right here. And now you can see why. If you have a malicious VBA 225 00:25:17,960 --> 00:25:23,720 macro, the obvious thing that the macro could have done is to drop something here and then 226 00:25:23,720 --> 00:25:29,960 basically execute it with this feature. And now attackers can't do it. The only thing that I did 227 00:25:29,960 --> 00:25:36,840 mention that I did want to look for is directory traversal, like creating Simlinks or 228 00:25:38,440 --> 00:25:44,120 doing ..slash or something that's in the file name. None of them work. So that's very good. 229 00:25:44,120 --> 00:25:49,320 Well, it's bad for attackers. It's good for Microsoft. So yay and nay at the same time. 230 00:25:50,680 --> 00:25:56,200 The basic idea is that I did not find a very obvious way to abuse this for a sandbox escape. 231 00:25:56,840 --> 00:26:03,800 And as you remember from before, this is the fix that Microsoft have done. 232 00:26:03,800 --> 00:26:07,880 And you can see here the application scripts directory. So there you go. 233 00:26:08,760 --> 00:26:14,520 Let's move on to grant access to multiple files. You can use grant access to multiple files to 234 00:26:14,520 --> 00:26:22,200 write a .dshn file even, right? Because again, this allows you to write arbitrary files, 235 00:26:22,200 --> 00:26:27,640 you can call this grant access to multiple files with an array. The array is basically, 236 00:26:27,640 --> 00:26:41,000 in this case, it's an array of one element, which is the user's home directory and then 237 00:26:41,000 --> 00:26:47,320 .dshn with arbitrary contents. You call grant access to multiple files and basically it will 238 00:26:47,320 --> 00:26:53,160 grant you access there. So this looks pretty promising. You can basically use that to escape 239 00:26:53,160 --> 00:27:01,080 the sandbox and have a malicious office macro. But of course, because this is a secure API, 240 00:27:01,080 --> 00:27:06,360 it will pop up this thing. So this is how it looks like the first time that you try, 241 00:27:06,360 --> 00:27:12,200 you basically have to click select and then select the directory. You click grant access 242 00:27:12,760 --> 00:27:19,000 and then the file could be written. So that's kind of like a very convoluted way 243 00:27:19,000 --> 00:27:25,480 of getting a malicious office macro to do something malicious. But it could be done 244 00:27:25,480 --> 00:27:30,440 for a very unsuspecting user. But it has to be a very unsuspecting user. Interestingly, 245 00:27:30,440 --> 00:27:37,400 if you deny, if you do cancel and you call this thing one more time, it will just pop up again. 246 00:27:37,480 --> 00:27:44,120 So one thing that an attacker could do is basically do it in a loop. Until it's approved, 247 00:27:44,120 --> 00:27:50,280 you can just do it forever until the user says, you know what, fuck it and then click select and 248 00:27:50,280 --> 00:27:57,240 then approves. So that's something that an attacker could do even today. But interestingly enough, 249 00:27:57,240 --> 00:28:02,520 after you grant access for the first time, second time just works. Even after restarting the app, 250 00:28:02,520 --> 00:28:09,640 you can restart the computer. And that tells me a lot. It tells me that there is a persistent 251 00:28:09,640 --> 00:28:15,800 user choice. The user choice, the idea that the user granted access is saved persistently. 252 00:28:16,360 --> 00:28:21,080 And I was like, okay, where is it saved? And I found where it saved. And interestingly enough, 253 00:28:21,080 --> 00:28:27,800 it's saved under the container directory forward. If you remember from 10 slides ago, 254 00:28:28,680 --> 00:28:33,880 you can write and read arbitrary files there. Like this directory is free for all even from 255 00:28:33,880 --> 00:28:39,000 the Office Macro. So it's very interesting that this file is com microsoft word secure 256 00:28:39,000 --> 00:28:44,520 bookmarks.plist actually contains the user choice that says, hey, you know what, 257 00:28:44,520 --> 00:28:52,120 you can write arbitrary files to this path right here. Plist files are just configuration 258 00:28:52,200 --> 00:28:58,520 files for Mac if you're unfamiliar with them. And you use plutil to print out the data there. 259 00:28:59,880 --> 00:29:05,080 So this is very interesting. And there is this question on whether I could actually abuse the 260 00:29:05,080 --> 00:29:12,040 fact that I have free access from within the VBA macro to this Plist file. This is what we're going 261 00:29:12,040 --> 00:29:19,880 to find out in the next 20 something minutes. So the first thing that we can do always is read the 262 00:29:19,880 --> 00:29:26,920 file. Because the file is in the container directory, we can arbitrarily read and write to it. 263 00:29:27,640 --> 00:29:32,920 It's a binary Plist file. Plist files, as I said, are configuration files, but 264 00:29:34,520 --> 00:29:39,000 and they come in like two and a half shapes, I guess, so three shapes. And one of them is a 265 00:29:39,000 --> 00:29:45,640 binary format. So parsing it kind of sucks, even though you can do it from within VBA. 266 00:29:46,280 --> 00:29:53,480 And I didn't go to like a lot of lengths. I just wanted to POC that allows me to read and 267 00:29:53,480 --> 00:30:00,440 understand whether a specific path has been approved or not. So what I've done is to just 268 00:30:00,440 --> 00:30:07,000 do some forensics. I understood that strings seem to be ANSI Pascal strings. So first byte is the 269 00:30:07,000 --> 00:30:14,440 length and then there is the string length itself. And VBA script can check the directory 270 00:30:14,920 --> 00:30:19,880 if the directory is approved and by simply reading the file. So if you don't want to spam the user 271 00:30:19,880 --> 00:30:24,280 with this kind of like, hey, grant me access right now and being all suspicious, you can be very 272 00:30:24,280 --> 00:30:32,120 stealthy and say something like, okay, can I just create this .zsh end file without popping up this 273 00:30:32,120 --> 00:30:39,720 thing? So this is how you do it. You basically read the Plist file. This is where it is. And you can 274 00:30:39,720 --> 00:30:49,640 basically read the this Plist file in binary format and then basically look for the right 275 00:30:49,640 --> 00:30:56,040 string in it. And this is the implementation. I know that it's ugly. It's not my fault that VBA 276 00:30:56,040 --> 00:31:02,040 is not a very friendly language. But this is how it could be done by an attacker. So an attacker 277 00:31:02,040 --> 00:31:07,000 could basically, if this is a document that is being opened millions of times or by many people, 278 00:31:07,000 --> 00:31:13,240 can basically run the script forever and ever and ever on every machine. And in every opportunity, 279 00:31:13,240 --> 00:31:19,160 you can just ask if it just so happens that someone allowed access to this directory. And if so, 280 00:31:19,160 --> 00:31:24,360 escape the sandbox. So this is how a script could be stealthy. But you know, you have to wait for 281 00:31:24,360 --> 00:31:29,080 someone to do something. So it's not a real vulnerability. It's an abuse of a feature, I would 282 00:31:29,080 --> 00:31:36,760 say. The writing to files is the most interesting part. If I could write to this Plist file 283 00:31:36,840 --> 00:31:43,480 and basically add arbitrary values to it, I wouldn't because that means that I can approve 284 00:31:44,360 --> 00:31:50,920 any path that I like without any user interaction, without this interaction right here. So that's 285 00:31:50,920 --> 00:31:58,520 kind of where I'm aiming. Interestingly, it looks like it's signed. You see this, the bookmark data 286 00:31:58,520 --> 00:32:05,560 key actually contains some signature information in it. There is a date and there is a UUID, 287 00:32:06,280 --> 00:32:13,000 like a unique ID there. But I did try and naively modifying the keys or values there, 288 00:32:13,000 --> 00:32:19,000 end up being ignored by Office and still creating this thing rather than silently just approving, 289 00:32:19,000 --> 00:32:23,800 just like we've done before. So there is the question, how is it signed? Can we calculate 290 00:32:23,800 --> 00:32:33,240 the key ourselves? So before we start, we have to talk about how apps work in macOS and how you 291 00:32:33,240 --> 00:32:38,520 really select files. If you ever opened Word or anything like that, you can just click file open 292 00:32:39,160 --> 00:32:45,560 and magically you can just get access to arbitrary files. But how do you get that access if Word is 293 00:32:45,560 --> 00:32:54,280 sandboxed? No one, no one, you know, when I first learned about the macOS sandbox, I had this very 294 00:32:54,280 --> 00:33:02,520 same question. How can apps actually get sandboxed apps, get access to arbitrary files that normally 295 00:33:02,520 --> 00:33:08,440 they do not have access to? And what really happens is that unlike Windows or Linux where 296 00:33:08,440 --> 00:33:14,760 basically opening in Windows, it's more clear because you have a dialog box. Usually if you have 297 00:33:14,760 --> 00:33:21,640 an app in Windows and you click file open, the dialog box will run in the same process context. 298 00:33:21,640 --> 00:33:27,480 In macOS, it's not like that at all. What happens really is that the sandbox app typically gains 299 00:33:27,480 --> 00:33:33,480 access to the specific files. If a user selects those files using a different process called macOS 300 00:33:33,480 --> 00:33:40,040 open dialog. So whenever you want to open a file, it actually does an IPC, an inter-process 301 00:33:40,040 --> 00:33:47,240 communication, to another service called the open and save panel service. And this service is on 302 00:33:47,240 --> 00:33:54,680 sandboxes, has full access to the files, and it basically grants the access to the files with IPC. 303 00:33:55,640 --> 00:34:03,640 So it's really interesting, the different design between traditional OSs that you guys might be 304 00:34:03,640 --> 00:34:11,000 familiar with Windows and Linux, where basically you open a file because you open a file, but in 305 00:34:11,000 --> 00:34:18,040 mac it's a bit different. You have to basically pass the ability to open a file to a different app. 306 00:34:18,760 --> 00:34:24,920 In Linux, there is something quite similar in which you can pass file descriptors over 307 00:34:24,920 --> 00:34:31,720 a Linux domain socket, but this is not a Linux doc. So that's what really happens when you select 308 00:34:31,720 --> 00:34:40,600 the file. And there is something that we have to have the right terminology. A sandbox extension, 309 00:34:40,600 --> 00:34:45,960 that's the terminology, is a token that is created by the kernel and grants the process 310 00:34:45,960 --> 00:34:51,160 possessing it the ability to access certain resources, such as files. So in our case, what 311 00:34:51,160 --> 00:34:56,840 happens is that the open dialog, the service, asks the kernel, hey, please create a token for this 312 00:34:56,840 --> 00:35:05,080 specific file, and then transfer that token to Office. And then there is this API that is called 313 00:35:05,080 --> 00:35:10,360 sandbox extension issue file. You can just call it directly, even though there are high-level 314 00:35:10,360 --> 00:35:15,960 APIs to do that kind of functionality. This is how it looks like when you select the file. 315 00:35:15,960 --> 00:35:21,240 So you basically say, hey, I want to open the file. This is, by the way, unrelated to what we've 316 00:35:21,240 --> 00:35:26,600 seen so far. This is just how you open the file word. So when you open the filing word on mac, 317 00:35:26,600 --> 00:35:34,600 it will create an IPC to the open dialog surface, which is unsanboxed. This thing will access the 318 00:35:34,600 --> 00:35:40,120 file and ask the kernel, hey, please create a read-write token to this file right here, 319 00:35:40,120 --> 00:35:46,360 let's say temp quark. Kernel basically creates this token. The token is then transferred to 320 00:35:46,360 --> 00:35:53,800 the open dialog service and then over IPC to the word app. And then the word uses the token to 321 00:35:53,800 --> 00:36:03,720 access the file. The token itself is composed of several fields. And most interestingly, it has an 322 00:36:03,720 --> 00:36:12,040 hmax 256 authentication. The key is generated by each boot and each boot by sandbox.kxt, 323 00:36:12,040 --> 00:36:18,920 which is a kernel extension. So the kernel itself generates a key to sign this token right here. 324 00:36:19,560 --> 00:36:24,040 There are other interesting fields there, including volume, inode, and other file system 325 00:36:24,040 --> 00:36:29,880 related to find information. The file path is obviously there in the token. And the capability, 326 00:36:30,440 --> 00:36:35,080 for instance, is capability to read and write, for instance. This is what we care about. 327 00:36:35,800 --> 00:36:41,720 So the conclusions are the following. A process can't fake a token because of the signature, 328 00:36:41,720 --> 00:36:48,680 because of the authentication. And the other conclusion is that a token can't persist between 329 00:36:48,680 --> 00:36:55,160 reboots because the key is generated in each boot by sandbox.kxt. If you save, let's say, 330 00:36:55,800 --> 00:37:02,600 this token for later use, after a reboot, it will be invalidated. The key is no longer the same key. 331 00:37:03,720 --> 00:37:10,280 So because of that, what happens, well, before that, there is another thing called security 332 00:37:10,280 --> 00:37:19,240 scope URLs. And this is simply like a token serialization in an NS URL. It's not very interesting. 333 00:37:19,240 --> 00:37:28,360 What I'm trying to say here is that there is an upper-level API to use this thing, 334 00:37:28,360 --> 00:37:34,200 to use the sandbox extension issue file. So you can use the entire next step 335 00:37:35,320 --> 00:37:41,880 higher-level API. That's the entire thing. The idea is that once you have a token, 336 00:37:41,880 --> 00:37:45,560 you can just tell the kernel, hey, you know what, I have a token, please let me pass. 337 00:37:45,560 --> 00:37:52,920 That's the entire idea here. And then this is where things get interesting. This is what word 338 00:37:52,920 --> 00:37:58,680 and other apps use. It's called security scope bookmarks. And the idea is that sometimes we want 339 00:37:58,680 --> 00:38:05,000 to persistently save the access token, even between reboots. You can't naively use the token as is 340 00:38:05,000 --> 00:38:13,400 because the HMAC-256 will be invalidated after the reboot. And what you do is to talk to another 341 00:38:13,400 --> 00:38:19,240 service, another sandbox service called bookmark agent. And this guy will create a normal token, 342 00:38:19,240 --> 00:38:25,320 but also calculate something called a security cookie. And that cookie is saved with a path 343 00:38:25,320 --> 00:38:32,200 persistently. So this is how you basically save the access token. And whenever you want to use 344 00:38:32,200 --> 00:38:39,240 the token, what happens when you have a request, you check in the persistent storage for the security 345 00:38:39,240 --> 00:38:46,040 cookie. If the cookie exists and valid, you create a token and provide it to the app, basically use 346 00:38:48,200 --> 00:38:54,520 the API that does not survive reboots. If the cookie doesn't exist, you use the open dialogue. 347 00:38:54,520 --> 00:39:00,360 Now, I know this sounds very convoluted, so I prepared, again, this graph. What happens is 348 00:39:01,000 --> 00:39:08,040 word says, hey, I want read-write access to TempQuack and talks to the scope bookmark agent. 349 00:39:08,600 --> 00:39:15,160 This agent basically will get the PILIST. It's not done exactly that way. The PILIST is actually 350 00:39:15,160 --> 00:39:21,640 maintained by Word, but whatever, and asks, hey, is TempQuack approved for read-write with the valid 351 00:39:21,640 --> 00:39:27,080 cookie? If the answer is yes, it will basically talk to the kernel. It will get read-write access 352 00:39:27,080 --> 00:39:33,720 just the way that we've done before and pass this token to Word. Now, Word has access to TempQuack. 353 00:39:34,680 --> 00:39:41,480 If we don't have access to the cookie, if we don't have a valid cookie or if we don't have an 354 00:39:41,480 --> 00:39:47,240 entry for TempQuack, I mean, what happens is that you have to talk to the open dialogue service, 355 00:39:47,240 --> 00:39:53,960 which will present the open dialogue and asks you to grant access, which is something that 356 00:39:53,960 --> 00:40:01,080 the red teamer doesn't want to have. Then the token is passed to Word. You can also save data 357 00:40:01,080 --> 00:40:09,000 later. Scope bookmark agent can save the data for later in the PILIST file for persistence. 358 00:40:11,800 --> 00:40:17,560 How do you sign a bookmark? How is this bookmark distinct in the PILIST? How is this signed? 359 00:40:18,920 --> 00:40:23,560 There are two types of bookmarks, actually, but we'll only talk about app-scoped bookmarks, 360 00:40:23,560 --> 00:40:28,760 because I didn't find any documents called bookmarks. Perhaps it's a feature that is not used a lot. 361 00:40:29,400 --> 00:40:36,360 So app-scoped bookmarks basically calculate the crypto key. The crypto key is HMAC 256 362 00:40:36,920 --> 00:40:43,000 of a secret key, signing the up bundle. In our case, it's called Microsoft Word. 363 00:40:43,640 --> 00:40:48,920 Now, the crypto key is used later to sign the bookmark data. That's the bookmark cookie. 364 00:40:49,800 --> 00:40:56,280 So what do we know from inside of the sandbox app? That's as an attacker, 365 00:40:56,280 --> 00:41:02,280 switching hats from an engineer that understands this thing to a hacker or to an attacker. 366 00:41:03,800 --> 00:41:07,240 We know the bundle identifiers. We know com.microsoft.word. 367 00:41:07,800 --> 00:41:13,000 And all we need is the secret key. If I know the secret key, I can calculate the crypto key. 368 00:41:13,000 --> 00:41:18,360 And if I know the crypto key, I can create arbitrary bookmark data and add it to the PILIST 369 00:41:18,360 --> 00:41:25,160 in order to gain arbitrary access to arbitrary files. That's the idea. The secret data, 370 00:41:25,160 --> 00:41:29,400 the secret key, however, this guy right here comes from the key chain. 371 00:41:30,680 --> 00:41:35,560 Now, if you're unfamiliar with the key chain, key chain is basically like a password manager 372 00:41:35,560 --> 00:41:43,480 that is there by default on macOS. It's being used for various things to basically store secrets. 373 00:41:44,520 --> 00:41:51,480 Now, this is the name of the secret key. The secret key is called comapplescodebookmarksagent.xpc. 374 00:41:51,480 --> 00:41:57,960 And this is not a key chain talk, but basically every entry in the key chain 375 00:41:57,960 --> 00:42:05,560 has an ACL, an access control list. And in this case, the access control list for this secret 376 00:42:05,560 --> 00:42:14,840 key specifically mentions that the only apps that have this kind of have the access to the key, 377 00:42:14,840 --> 00:42:18,760 to the secret key, is this code bookmark agent, which is not surprising. 378 00:42:19,480 --> 00:42:25,480 It means that if I'm an attacker and running inside Word macro, I don't have access to this 379 00:42:26,840 --> 00:42:34,440 item. So it looks like we're kind of lost. This is how things look like. Really, Word, 380 00:42:37,320 --> 00:42:42,920 like the complete thing is that in order to sign this token, like everything is done just 381 00:42:43,000 --> 00:42:49,480 like we've done before. But whenever the token is created, the code bookmark agent reads the secret 382 00:42:49,480 --> 00:42:54,680 data from the key chain and then calculates the crypto key, calculates the cookie, and this is 383 00:42:54,680 --> 00:43:01,400 what's really being saved inside the playlist file. So just to mention how the signing looks like, 384 00:43:02,200 --> 00:43:08,200 and there is this question of what can we do, right? And I thought about it for like a few days, 385 00:43:08,200 --> 00:43:13,720 and then I realized that the ACL, the access control list, the thing that we saw, sorry, 386 00:43:14,680 --> 00:43:20,280 the thing that we saw here, actually dictates who can read the key. But I don't really need to 387 00:43:20,280 --> 00:43:26,120 read the key. I just need to decide on the key. And this is the vulnerability that I disclosed 388 00:43:26,120 --> 00:43:31,800 to Apple. The idea is that the ACL dictates you can't read the password, but I can just add one. 389 00:43:31,800 --> 00:43:39,720 So you can create from within even a sandboxed, even from within a sandboxed app, you can 390 00:43:39,720 --> 00:43:45,240 basically delete the previous key and decide on a new one. So I delete the previous one, 391 00:43:45,240 --> 00:43:51,000 decide on a new one. This is the password. You can see AAA, BC, so on. And more interestingly, 392 00:43:51,000 --> 00:43:59,560 I added the big A here, big dash big A. And this flag actually says that the access control list is 393 00:43:59,560 --> 00:44:05,960 allowing everyone to access the key. So that's kind of like a win. It basically means that I 394 00:44:05,960 --> 00:44:13,720 invalidated all of the previous, all of the previous persistent user choices. So there is some impact 395 00:44:13,720 --> 00:44:20,040 on the user experience in the future. But basically now that I know, I control the secret key, so 396 00:44:20,040 --> 00:44:26,040 I know what that is, and I allow everyone to access it, including this code bookmark agent, 397 00:44:26,040 --> 00:44:32,680 it basically means that it could be used. So how does my generic sandbox escape look like? 398 00:44:32,680 --> 00:44:36,840 You delete the old key from the key chain. You create a new key from the key chain with the 399 00:44:36,840 --> 00:44:42,440 password that we control, right? You can basically do that because it's not enforced by the key chain 400 00:44:42,440 --> 00:44:47,960 ACL. Now that you control the password, you calculate the crypto key for an app, let's say 401 00:44:47,960 --> 00:44:53,640 com.microsoft.org. Now that you know the crypto key, you artificially sign new items in the security 402 00:44:53,640 --> 00:45:00,920 bookmark playlist. Now basically you call the API before the grant access to multiple files, 403 00:45:00,920 --> 00:45:06,840 which will use this code bookmark agent, which will read the same thing that we added before the 404 00:45:06,840 --> 00:45:13,400 same playlist. We'll use the same key that we controlled to validate that the entry is correct 405 00:45:13,400 --> 00:45:21,400 and get us access to write 2.zshn. And then I dump that zshn file, and now I wait for terminal 406 00:45:21,400 --> 00:45:27,080 or something to trigger. Or I can do whatever I want. I can even create a launch agent that way, 407 00:45:27,080 --> 00:45:34,120 same idea. So that's the generic sandbox escape that I had. Interestingly, exploitation, by the 408 00:45:34,120 --> 00:45:40,840 way, took me a long time. There are some very fine details that you have to come up with when you 409 00:45:40,840 --> 00:45:48,200 artificially sign new items. But interestingly, I got stuck here. I still got the scope bookmark 410 00:45:48,200 --> 00:45:55,480 agent to not respect my new password. And the thing that I discovered is that it doesn't 411 00:45:55,480 --> 00:46:03,320 ask for the secret key every time from the key chain. It actually just asks one time and then 412 00:46:03,320 --> 00:46:09,800 keeps it in memory with the understanding that it won't change. It's a long-term key. So what I 413 00:46:09,800 --> 00:46:15,480 ended up doing is that I have to either kill it or wait for a reboot. I did not find an easy way 414 00:46:15,480 --> 00:46:25,080 to kill it from within the sandbox app. So the exploit becomes even much, much harder. You have 415 00:46:25,080 --> 00:46:31,320 to maintain state between reboots. This is the actual sandbox escape that does work. You basically 416 00:46:31,320 --> 00:46:38,760 check for a state file. Oops, sorry. If the state file doesn't exist or the process ID of this 417 00:46:38,760 --> 00:46:43,640 code bookmark agent is the same thing, it basically means that we didn't have a reboot. 418 00:46:43,640 --> 00:46:47,240 So what you're doing in the macro is to delete the old key from the key chain, 419 00:46:47,240 --> 00:46:51,160 create a new key with the password that we control, just like we've done before. 420 00:46:51,160 --> 00:46:56,280 We calculate the crypto key and create a new state file and wait for a reboot. 421 00:46:57,560 --> 00:47:03,560 If this thing does not apply, it basically means that we've experienced the reboot. And what we 422 00:47:03,560 --> 00:47:10,200 can do is artificially sign new items. We know that this code bookmark agent fetched the correct 423 00:47:10,200 --> 00:47:17,720 key. And what we do now is to do what we've done before. So this is the generic sandbox escape 424 00:47:17,720 --> 00:47:24,440 that I have, which is very convoluted but does work. One thing to keep in mind is that the issue 425 00:47:24,440 --> 00:47:30,600 with the ACL that I mentioned, the fact that the ACL does not prevent you from deleting and creating 426 00:47:30,600 --> 00:47:35,880 a new password, opens up other techniques that are not vulnerabilities per se. For example, 427 00:47:36,840 --> 00:47:44,600 if you're using any Chromium-based browser that's a Chrome for fun, the cookies and everything that 428 00:47:44,600 --> 00:47:49,640 you use are basically encrypted. And they're encrypted with a key that is saved in the key 429 00:47:49,640 --> 00:47:55,960 chain. And the only Chrome in my example can open them. So one of the things that you can do as an 430 00:47:55,960 --> 00:48:02,680 attacker is delete that key from the key chain, define your own key. It will invalidate all 431 00:48:02,680 --> 00:48:07,800 the cookies from Chrome. So there is a user experience. But from that point on, as an attacker, 432 00:48:07,800 --> 00:48:13,400 you can just wait for cookies to be written to disk. And you can decrypt them because you 433 00:48:13,400 --> 00:48:20,360 control the password. So that's like a bigger issue. And Red Teamers can use that ability to 434 00:48:20,920 --> 00:48:30,360 wreak havoc or gain any secret information you could not get. Some conclusions, I don't know 435 00:48:30,360 --> 00:48:36,280 if we have time for questions, but some conclusions. The attacker strategies that I found for malicious 436 00:48:36,280 --> 00:48:40,520 Office Macros are the following. You can either nag the user until the permissions are approved. 437 00:48:41,480 --> 00:48:49,400 My favorite, I guess. You can be stealthy and check for permissions and then drop the .cshn file. 438 00:48:49,400 --> 00:48:55,320 Or you can rely on a very complex exploit that relies on a reboot, which is what I described 439 00:48:55,320 --> 00:49:01,800 in the previous 10 slides or so. Apple makes sandbox escapes really challenging. Even though 440 00:49:01,800 --> 00:49:07,800 I know that there has been a Black Hat talk just a few days ago about some newfound sandbox 441 00:49:07,800 --> 00:49:15,080 escapes, so you should check it out. I did not follow up completely on that. But it is getting 442 00:49:15,080 --> 00:49:21,000 harder and harder to escape the sandbox. And Office VBA Macros are kind of safe if you think 443 00:49:21,000 --> 00:49:25,960 about it, because these are very convoluted things. So they're kind of safe, but kind of not, 444 00:49:25,960 --> 00:49:31,960 but kind of are. And you get to choose whether you feel safe or not. With that, thank you so, 445 00:49:31,960 --> 00:49:39,720 so, so much for listening and for tuning in. Thank you. And I don't know if we have time for 446 00:49:39,720 --> 00:49:45,320 questions. If there are, I'll try to answer them. I guess we do have time for questions. 447 00:49:46,200 --> 00:49:52,840 Yeah. If there are any questions, you can write them in the chat. Or I see that there are some... 448 00:49:55,480 --> 00:50:00,040 I don't see anything that's written in the chat that looks like a question. So, hooray. 449 00:50:03,960 --> 00:50:10,680 Okay. You can feel free to reach out after. I will be answering questions. I will not be giving 450 00:50:10,680 --> 00:50:23,800 exploitation code. But besides that, you're free to ask me whatever you want. Thank you so, so much.