We have a great speaker this morning. Patrick Bortles is going to talk to us about Mac malware. Good morning, aloha, and welcome to my talk. Thank you for being here, especially so early. Really appreciate it. Wow. Okay, we might have to try that again at the end. But yeah, super stoked you're here. Today we're going to be talking about how to identify what I refer to as script-wrapped binaries, and then how to extract their embedded malicious payloads. My name is Patrick Bortles. I am the founder of the Objective-C Foundation, more recently also the co-founder of W, where we're building core macOS detection components to integrate into larger security tools. I'm also the author of the Art of Mac Malware book series that's relevant today, because what we're talking about, the specific topic, is covered in even more detail in the book. So I want to mention the book, because first it's free, so this is not a sales pitch. But if you're interested in this topic and you want to learn or read more about it, check out the book, specifically chapter five. All right, so we're going to start with a brief introduction and talk about some concepts before diving into some more technical details. So imagine you are a malware analyst. And if you are a malware analyst, you don't have to imagine. But if you are, imagine you encounter an unknown file. Generally your goal is to quickly classify it in one of three categories. Is it good? Is it benign? And if it is, you're done. There's no point in the context of malware analysis to reverse engineer known good files. Somewhat similarly, if it's malicious but it is, for example, well known to the AV community, people have already reverse engineered, really again doesn't make sense to continue on. However, if it is malicious and appears to be brand new, that's where I think the fun begins. This is where you're going to do the full analysis, write a report, create IOCs. But again, the goal is we want to do this fairly quickly and efficiently. So one of the first things you want to do when you receive a unknown file that perhaps is malware, perhaps is not, is to figure out its file type. This is a very basic step but it's important because most static and even a lot of dynamic analysis tools are file type specific. So if you encounter a script, you're not going to necessarily use a disassembler or a debugger because as we'll see, scripts are pretty straightforward to analyze. They're human readable. On the other hand, if you encounter a binary, this is a little bit more hardcore reverse engineering and you're going to defer to binary analysis tools, for example, a disassembler or a debugger. Which is great but it's time consuming. You need a certain amount of technical knowledge. And so what if it turns out that that binary is really just an execution host for a script? So the question is can then we skip all these time consuming, in depth, low level binary analysis steps? And well, in many cases the answer is yes. So what are these script based or non binary binaries? Well, they're basically binaries that originally were scripts that are now packaged up into binaries. And shortly we'll explain why this is the case. So on the slide we have a simple first stage component of some malware. The malware author has packaged it up into an executable application binary. Sends it to the user. The user executes it. The binary component will run but really the more interesting piece is the malicious script component that in this case is directly embedded in the application bundle. So you might be wondering why are these non binary binaries even a thing? Well, it all comes down to simplicity and what I refer to as runability. So for one, scripts are easier to write than compiled code. So a lot of script kitties start writing malware by creating malicious scripts. Number two, scripts are often a little bit more cross platform. So if you're a malware author and you want to target Windows and Mac OS users, a script is probably a better way than perhaps writing native executable code. And then finally three, and I think this is the main reason, Mac OS users, the targets, the victims, don't really know how to run scripts easily. But many of them will unfortunately double click on, for example, an application binary. So it makes sense to distribute your malicious code as something like an app that users can easily interface with and that the operating system will run. Versus saying, hey user, go to the terminal and run python malicious file dot py. That's not really ideal. Now then, the question is, well why should you care? And there's two main reasons because as we will see, a lot of these binaries that have these malicious script components are made up of two main pieces. An executable loader and then the actual malicious script. Now, take it from me, you don't want to spend days reverse engineering this malicious or rather non-malicious binary loader. Uh, and as we'll see a lot of times these loaders are benign, although they can be and are abused by malware. Uh, instead you want to be able to recognize that this binary contains something like a script and then go analyze that. Because that is actually where the malicious component lies and it's far easier to analyze a script versus a compiled executable. We'll also see some cases where the script is directly bedded in the binary which means extracting it is actually very important for our continued analysis because if you run it in a virtual machine that script is never going to touch the file system so you can't extract it in that, in that means to perform your continued analysis. Okay, so now let's talk about how we can identify these non-binary binaries and then how to extract scripts. So it turns out there's many ways to take a script and package it up into something that's more easily runnable. For example, a native Mac OS application. I've listed some of the tools that we will look at. I really need to stress that these tools by themselves are not malicious. They are totally benign. Many of you are probably familiar with pi installer, electron, et cetera, et cetera. But why we are talking about them today is first they all take as input scripts and can create native executables for application binaries. But as we will see they have all been abused by malware authors to make essentially malicious applications. So the mission or our goal is today is to A, learn how to identify these non-binary binaries and also identify the tool that created them. Then once we know that we can talk about how to extract or at least reconstruct the script that facilitates our binary analysis. Well see this is actually pretty straight forward but this is still something very practical to uh, to understand. And again worth noting each tool packages up the script in a different way. So it's important for us to be able to recognize each tool because the way we extract the script to analyze it is going to be different for each one. So let's now go through some of the common script based packaging utilities. Uh, we'll talk about how to identify each and talk about malware samples that have abused that. We're going to start with platypus. Platypus is open source and as its developers say it allows you to create Mac apps from command line scripts. Since it's open source we can actually look at the loader code. This is the binary component that gets packaged up and runs for example when the user double clicks it. And we can see for example the name and location of the script is directly hard coded. Also when this binary component is run all it does is go and executes a script. So this makes it actually pretty easy to identify platypus applications. If you do throw it in a disassembler you'll see a lot of symbol names that start with script exec. You'll also see that the application delegate, this is like the main function for apps, basically just invokes a method named execute script. But again we're trying to avoid disassemblers and kind of hardcore reverse engineering here because we're trying to do this quickly and easily. So it turns out that if you look at an application bundle and it has a file in the resources directory aptly named script, this is very likely an application that was created with platypus. So now let's look at some malicious code that abused platypus to distribute scripts in a application bundle to macOS users. Starting with creative update. This was used in a pretty nasty supply chain attack. Adversaries hacked the popular macupdate.com website and distributed trojanized versions of firefox. If we take a closer look at the application bundle we can see in the resources directory there is a file named script. And if we open this, this is actually the malicious first stage component of the malware. And as you can see it's a script. It's very easy to analyze. It's very easy to see. It's human readable. We didn't have to open a disassembler. We didn't have to do any debugger. And this is awesome. If we run a process monitor and execute the malware we can see indeed the application bundle does both drop and execute this script. Another example of another platypus application that was malware was Eleanor. Again, same approach. If we look at the resource directory we see a script called script. And if we open it, again it's a human readable script. We can see it contains the installer and persistent logic for the malware. Awesome. Again, pretty trivial, right? Let's now talk about PyInstaller. Many of you are probably familiar with PyInstaller. Again, there is nothing bad about PyInstaller. It's just a utility that allows you to, given a Python script, build a native executable, for example, for Mac OS. And so malware authors can and do abuse it. One point, PyInstaller will take the Python script it's packaging up and compile it into compiled byte, uh, Python byte code. So we'll talk about that on the next slide. So, given an application or binary you're analyzing it, how do you determine if it's been created via PyInstaller? Oh, pretty quick. If you look at the main function, you can see it calls py underscore main. But again, we're trying to stay away from reverse engineering. So easier, just look at the embedded strings that you can extract with the strings utility. And you will see there are many PyInstaller related strings in the application. So, once you've identified this application as, hey, this was created with PyInstaller, how do you extract the Python script? How do you reconstruct it? Because that's going to contain the malicious logic that you want to analyze if it is malware. Well, since PyInstaller actually embeds the Python script directly into the binary, it's not in an external resource like it was with platypus. We have to use a dedicated tool. Luckily there's a great PyInstaller extractor that's available on GitHub. So we can see on the slide, we run that and we give it the path to the compiled binary that contains the, uh, Python code. And it extracts it for us. This is all well and good, except as I mentioned, PyInstaller will first compile this Python script into Python compiled byte code. So, even though we've extracted the script, it's in this intermediary non-human readable format. Not a problem. There's a lot of decompilers for Python compiled byte code. Uh, so you can just upload it to one of those or use the uncompile command line utility. And if we do that with gravity rat, the sample that we're looking at here, we can see this reconstructs or recreates the original Python source code or representation thereof. As we can see, Python source code is very easy to analyze. And even if you don't understand Python, just give it to chat GPT and it'll tell you exactly what the malware is doing. Next up we have Electron. I'm sure many of you are familiar with Electron applications. It's a well known framework or tool that allows developers and malware authors to create cross platform applications built on web tech. If we look at a application that has been created with Electron, again if we know what to look for, it's pretty easy to say, hey, this is an Electron app. You can either look for a dependency on the Electron framework. You can use something like o-tool to dump the dependencies . Far easier, just look at the application bundle and you'll see the application has a dependency on the Electron framework. And thus that says, okay, this is indeed a Electron application. Again, we don't want to focus on reversing the main executable component. Instead we're interested in the scripts that if this is malware, this is where the malicious logic will lie. So again, because Electron, somewhat similar to pi installer, packages up and compresses the scripts, we need a intermediary tool to pull those scripts out. Luckily we can do that with NPX. And if we execute NPX with the ASAR command line flag, point it at a Electron application, it will extract and decompress any of the scripts that are embedded in that, which then we can go and analyze. In this case, we look at gravity rat again. There was another instance that was distributed as a Electron app. Once we run it through NPX, we get the original JavaScript files. JavaScript is trivial to analyze, especially compared to compiled binary code, right? And so in this case we can see the malware doing things like checking if it's running in a VM and or persisting as a cron job. The first part is actually really important because if we had taken this binary and run it in an isolated virtual machine to try to dynamically analyze it, it would have detected it was running in a VM and would have just exit, exited, thwarting our analysis efforts. So this is why, again, it's actually very important to do this as a static analysis and know how to identify these apps, pull out the malicious logic and figure out how it executes. Once you have an understanding of this, your continued, more comprehensive analysis can continue. Next up we have Tori. This one's a little bit off the radar, but there are some examples, as we will see, of pretty sophisticated malware abusing it to target macOS users. Again, the first question is, how do we identify if an application or binary was built with the Tori framework? Well, unfortunately, what Tori does is embeds everything directly in the main binary, so there's no external dependencies, there's no script sitting in the resource directory, but it's still pretty easy to identify once you know what to look for. So either you can load it in a disassembler and look at the symbols, you can also use a tool like NM to extract the symbols and you'll see a lot of symbols referring to Tori. Again, though, far simpler, just use the strings utility, extract embedded strings, and you will see lots of strings referencing Tori, which identifies this application as being built with that. Now, I wasn't super familiar with Tori, so I hopped on chat GPT, I'm like, tell me how this works, and more importantly, how can I extract any scripts that have been embedded into it? Again, this is what I want to extract and analyze because that's likely where the malicious logic will be. Chat GPT was super helpful and told me that Tori uses a Brotli compression algorithm, I wasn't very familiar with that, but chat GPT also then created a nice Python, uh, decompressor script for me, turns out there's a library that can handle the decompression. So what we can do is we can use this very simple decompression Python script, if we point it to the binary that contains the embedded compressed scripts, it can now extract and dump those out. So if we execute this, on the slide you can see I'm running this custom Brotli decompiler that chat GPT nicely wrote for me. Uh, we can see that it is able to find and extract the embedded JavaScript. If we look closely at the JavaScript, we can see that what it does is gets a test application property, and then whatever the value of that is, it executes. Interesting. Well, we can use the X adder command then to dump that test attribute, and we can see it contains the download and execute payload of the malware. So then, this is great, this can, uh, support our continued analysis of this malware, and again if we didn't understand or know anything about Tori and how to identify this, we would have likely totally missed this, or spent all our time reverse engineering the main application executable, which doesn't contain any malicious logic per se. Finally, I want to briefly touch on Applescript. It's just another way where malware authors can take a script, an Applescript, compile it into an application, and then distribute it to their victims. Super easy to identify if, if an application has been, uh, Applescript or originally was an Applescript. You can look at the resources directory, there'll be a file named main or something similar with the SCPT extension. Also normally the main application binary is called Applet. Again, and sounding like a broken record, you don't want to spend any time reverse engineering the Applet binary, instead you want to recover and extract the Applescript code. So, let's look at an example, uh, the malware here we're looking at is called osaminer. It was actually a standalone, standalone component, but if we run the file command against it, we can see that macOS identifies it as compiled Applescript. Unfortunately for compiled Applescript, osadcompile cannot decompile it. So, we have to work a little harder. Luckily there is a great Applescript disassembler written by a researcher named Jinmo on GitHub. So what we can do is we can feed that Applescript component into that and we can see that we get some decent output. We can see a function named E appears to be doing some encoding, a function named D some decoding, but still this is not like super readable, right? Well it turns out, uh, researcher Phil Stokes built upon this Applescript disassembler and actually created a Applescript decompiler. So you can also grab this via GitHub. It takes as input the output from the Applescript disassembled and then as output gives you somewhat of a reconstruction of the original Applescript code. So for example, here we can see that osaminer is persisting and executing other components. This is super useful information for our continued analysis. Alright, so let's end with some conclusions and takeaways. So again, I'm all about like working smart, not hard. So I asked Chachi B, I'm like, what's the main takeaway here? And he gave me this nice little tagline which I was like, cool, let's roll with it. Basically said, why reverse engineer when you can simply unwrap? So that's really the main takeaway from this talk. And I guess the more specific three takeaways are, is really once you're looking at a binary, perhaps you're trying to figure out if it's malicious, if it's known, if it's good, what is it? First see if it's built with one of these script based, uh, packaging tools that we talked about today. If it has been and we talked about how to identify those cases, what you can then do is you can extract the embedded script using the package specific tools. And then end result, you will now have a script that A, is not only trivial to understand, it's human readable, but B, is going to be actually the component you want to analyze because that's where the malicious, malicious logic actually is stored. And also again, we're pointing out, you don't want to spend any time on the binary because not only is it irrelevant, but it's not going to contain any malicious code as well. So if you're interested in learning more, I did mention the Art of Mac Malware book series. I'm actually doing a book signing at No Starch Press at 11 a.m. right after this talk, giving out a bunch of free copies. So if you want to come chat more, get a signed book, maybe get a free copy , first come first serve, stop by. Also if you're interested in Mac security topics, I would invite you all to Objective by the Sea. It's a conference that's dedicated to Apple security topics. Three days, great talks. I'm also giving a training there on topics like this. So if this has piqued your interest and you want to learn more, check that out as well. Finally, I want to, first and foremost, thank you all for attending my talk, especially so early on a morning, talking about reverse engineering topics, first thing in the morning, definitely like take some grit, right? Also I just want to thank DEF CON. I mean, amazing conference, so round of applause for DEF CON, which we all love. And then finally, I briefly want to thank the companies and products that support the Objective Sea Foundation. They are the ones that support my research, allow the books to be free, support the community. So I just want to take a moment to thank them. But again, thank you all for attending. I know this was kind of a short talk and a lot of information to jam in, but hopefully you learned a lot and I will be near the stage and outside if there's any more questions. But again, thank you so much and enjoy the rest of the conference.