WEBVTT

00:00.000 --> 00:02.900
We have a great speaker this morning.

00:03.000 --> 00:06.880
Patrick Bortles is going to talk to us about Mac malware.

00:07.880 --> 00:11.680
Good morning, aloha, and welcome to my talk.

00:17.560 --> 00:20.320
Thank you for being here, especially so early.

00:20.320 --> 00:21.680
Really appreciate it.

00:22.060 --> 00:23.020
Wow.

00:23.180 --> 00:25.120
Okay, we might have to try that again at the end.

00:25.240 --> 00:26.880
But yeah, super stoked you're here.

00:26.880 --> 00:36.960
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.

00:38.280 --> 00:39.780
My name is Patrick Bortles.

00:39.780 --> 00:51.320
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.

00:53.300 --> 01:02.760
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.

01:02.760 --> 01:07.120
So I want to mention the book, because first it's free, so this is not a sales pitch.

01:07.260 --> 01:13.600
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.

01:14.960 --> 01:21.900
All right, so we're going to start with a brief introduction and talk about some concepts before diving into some more technical details.

01:22.240 --> 01:25.180
So imagine you are a malware analyst.

01:25.180 --> 01:27.780
And if you are a malware analyst, you don't have to imagine.

01:27.860 --> 01:31.200
But if you are, imagine you encounter an unknown file.

01:31.320 --> 01:35.900
Generally your goal is to quickly classify it in one of three categories.

01:36.180 --> 01:37.140
Is it good?

01:37.140 --> 01:37.940
Is it benign?

01:37.940 --> 01:39.100
And if it is, you're done.

01:39.100 --> 01:43.980
There's no point in the context of malware analysis to reverse engineer known good files.

01:44.340 --> 01:53.480
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.

01:53.480 --> 01:58.640
However, if it is malicious and appears to be brand new, that's where I think the fun begins.

01:58.640 --> 02:02.600
This is where you're going to do the full analysis, write a report, create IOCs.

02:02.600 --> 02:06.000
But again, the goal is we want to do this fairly quickly and efficiently.

02:07.160 --> 02:16.140
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.

02:16.140 --> 02:23.760
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.

02:23.880 --> 02:32.140
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.

02:32.140 --> 02:33.640
They're human readable.

02:34.040 --> 02:44.540
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.

02:44.540 --> 02:46.160
Which is great but it's time consuming.

02:46.160 --> 02:48.400
You need a certain amount of technical knowledge.

02:48.400 --> 02:53.860
And so what if it turns out that that binary is really just an execution host for a script?

02:53.980 --> 03:00.020
So the question is can then we skip all these time consuming, in depth, low level binary analysis steps?

03:00.020 --> 03:03.080
And well, in many cases the answer is yes.

03:03.800 --> 03:07.700
So what are these script based or non binary binaries?

03:07.700 --> 03:14.420
Well, they're basically binaries that originally were scripts that are now packaged up into binaries.

03:14.420 --> 03:17.380
And shortly we'll explain why this is the case.

03:17.380 --> 03:21.680
So on the slide we have a simple first stage component of some malware.

03:21.740 --> 03:26.820
The malware author has packaged it up into an executable application binary.

03:27.000 --> 03:28.320
Sends it to the user.

03:28.440 --> 03:29.740
The user executes it.

03:29.740 --> 03:38.280
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.

03:39.780 --> 03:43.880
So you might be wondering why are these non binary binaries even a thing?

03:43.880 --> 03:48.060
Well, it all comes down to simplicity and what I refer to as runability.

03:48.060 --> 03:51.840
So for one, scripts are easier to write than compiled code.

03:51.840 --> 03:56.920
So a lot of script kitties start writing malware by creating malicious scripts.

03:57.680 --> 04:00.240
Number two, scripts are often a little bit more cross platform.

04:00.240 --> 04:07.940
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.

04:08.100 --> 04:16.620
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.

04:16.620 --> 04:21.980
But many of them will unfortunately double click on, for example, an application binary.

04:21.980 --> 04:29.860
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.

04:29.860 --> 04:34.340
Versus saying, hey user, go to the terminal and run python malicious file dot py.

04:34.340 --> 04:36.680
That's not really ideal.

04:38.080 --> 04:40.940
Now then, the question is, well why should you care?

04:40.940 --> 04:49.400
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.

04:49.420 --> 04:53.300
An executable loader and then the actual malicious script.

04:53.380 --> 05:00.100
Now, take it from me, you don't want to spend days reverse engineering this malicious or rather non-malicious binary loader.

05:00.120 --> 05:06.440
Uh, and as we'll see a lot of times these loaders are benign, although they can be and are abused by malware.

05:06.560 --> 05:12.900
Uh, instead you want to be able to recognize that this binary contains something like a script and then go analyze that.

05:12.900 --> 05:19.800
Because that is actually where the malicious component lies and it's far easier to analyze a script versus a compiled executable.

05:20.080 --> 05:33.920
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,

05:33.920 --> 05:36.840
in that means to perform your continued analysis.

05:37.600 --> 05:44.900
Okay, so now let's talk about how we can identify these non-binary binaries and then how to extract scripts.

05:46.440 --> 05:52.820
So it turns out there's many ways to take a script and package it up into something that's more easily runnable.

05:52.820 --> 05:55.960
For example, a native Mac OS application.

05:55.980 --> 05:57.900
I've listed some of the tools that we will look at.

05:57.900 --> 06:01.420
I really need to stress that these tools by themselves are not malicious.

06:01.420 --> 06:02.620
They are totally benign.

06:02.620 --> 06:07.180
Many of you are probably familiar with pi installer, electron, et cetera, et cetera.

06:07.180 --> 06:16.800
But why we are talking about them today is first they all take as input scripts and can create native executables for application binaries.

06:16.860 --> 06:23.740
But as we will see they have all been abused by malware authors to make essentially malicious applications.

06:24.340 --> 06:35.180
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.

06:35.180 --> 06:43.720
Then once we know that we can talk about how to extract or at least reconstruct the script that facilitates our binary analysis.

06:43.720 --> 06:50.220
Well see this is actually pretty straight forward but this is still something very practical to uh, to understand.

06:50.260 --> 06:54.100
And again worth noting each tool packages up the script in a different way.

06:54.100 --> 07:01.560
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.

07:01.820 --> 07:06.480
So let's now go through some of the common script based packaging utilities.

07:06.520 --> 07:12.400
Uh, we'll talk about how to identify each and talk about malware samples that have abused that.

07:12.560 --> 07:14.260
We're going to start with platypus.

07:14.260 --> 07:21.640
Platypus is open source and as its developers say it allows you to create Mac apps from command line scripts.

07:22.020 --> 07:25.480
Since it's open source we can actually look at the loader code.

07:25.480 --> 07:29.940
This is the binary component that gets packaged up and runs for example when the user double clicks it.

07:29.940 --> 07:35.520
And we can see for example the name and location of the script is directly hard coded.

07:35.700 --> 07:42.080
Also when this binary component is run all it does is go and executes a script.

07:42.320 --> 07:47.220
So this makes it actually pretty easy to identify platypus applications.

07:47.320 --> 07:53.540
If you do throw it in a disassembler you'll see a lot of symbol names that start with script exec.

07:53.800 --> 08:02.220
You'll also see that the application delegate, this is like the main function for apps, basically just invokes a method named execute script.

08:02.580 --> 08:09.500
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.

08:09.500 --> 08:22.020
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.

08:22.500 --> 08:32.160
So now let's look at some malicious code that abused platypus to distribute scripts in a application bundle to macOS users.

08:32.160 --> 08:34.260
Starting with creative update.

08:34.260 --> 08:37.000
This was used in a pretty nasty supply chain attack.

08:37.000 --> 08:45.180
Adversaries hacked the popular macupdate.com website and distributed trojanized versions of firefox.

08:45.200 --> 08:52.300
If we take a closer look at the application bundle we can see in the resources directory there is a file named script.

08:52.900 --> 08:57.680
And if we open this, this is actually the malicious first stage component of the malware.

08:57.780 --> 08:59.020
And as you can see it's a script.

08:59.020 --> 09:00.300
It's very easy to analyze.

09:00.300 --> 09:01.240
It's very easy to see.

09:01.240 --> 09:02.300
It's human readable.

09:02.340 --> 09:03.940
We didn't have to open a disassembler.

09:03.940 --> 09:05.500
We didn't have to do any debugger.

09:05.500 --> 09:06.620
And this is awesome.

09:07.160 --> 09:14.200
If we run a process monitor and execute the malware we can see indeed the application bundle does both drop and execute this script.

09:15.600 --> 09:20.680
Another example of another platypus application that was malware was Eleanor.

09:20.700 --> 09:22.260
Again, same approach.

09:22.260 --> 09:26.500
If we look at the resource directory we see a script called script.

09:26.500 --> 09:29.520
And if we open it, again it's a human readable script.

09:29.520 --> 09:34.220
We can see it contains the installer and persistent logic for the malware.

09:34.220 --> 09:34.880
Awesome.

09:34.880 --> 09:36.740
Again, pretty trivial, right?

09:37.580 --> 09:39.920
Let's now talk about PyInstaller.

09:39.920 --> 09:42.020
Many of you are probably familiar with PyInstaller.

09:42.020 --> 09:45.060
Again, there is nothing bad about PyInstaller.

09:45.060 --> 09:53.040
It's just a utility that allows you to, given a Python script, build a native executable, for example, for Mac OS.

09:53.140 --> 09:56.480
And so malware authors can and do abuse it.

09:56.880 --> 10:05.460
One point, PyInstaller will take the Python script it's packaging up and compile it into compiled byte, uh, Python byte code.

10:05.460 --> 10:07.480
So we'll talk about that on the next slide.

10:07.540 --> 10:14.040
So, given an application or binary you're analyzing it, how do you determine if it's been created via PyInstaller?

10:14.040 --> 10:15.360
Oh, pretty quick.

10:15.360 --> 10:19.700
If you look at the main function, you can see it calls py underscore main.

10:19.700 --> 10:21.960
But again, we're trying to stay away from reverse engineering.

10:21.980 --> 10:27.360
So easier, just look at the embedded strings that you can extract with the strings utility.

10:27.360 --> 10:31.980
And you will see there are many PyInstaller related strings in the application.

10:32.840 --> 10:40.420
So, once you've identified this application as, hey, this was created with PyInstaller, how do you extract the Python script?

10:40.420 --> 10:41.500
How do you reconstruct it?

10:41.500 --> 10:46.140
Because that's going to contain the malicious logic that you want to analyze if it is malware.

10:46.540 --> 10:54.480
Well, since PyInstaller actually embeds the Python script directly into the binary, it's not in an external resource like it was with platypus.

10:54.480 --> 10:56.120
We have to use a dedicated tool.

10:56.420 --> 11:00.920
Luckily there's a great PyInstaller extractor that's available on GitHub.

11:00.920 --> 11:07.700
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.

11:07.700 --> 11:09.760
And it extracts it for us.

11:09.880 --> 11:18.180
This is all well and good, except as I mentioned, PyInstaller will first compile this Python script into Python compiled byte code.

11:18.180 --> 11:24.380
So, even though we've extracted the script, it's in this intermediary non-human readable format.

11:25.700 --> 11:26.600
Not a problem.

11:26.600 --> 11:30.580
There's a lot of decompilers for Python compiled byte code.

11:30.580 --> 11:35.420
Uh, so you can just upload it to one of those or use the uncompile command line utility.

11:35.420 --> 11:45.740
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.

11:45.840 --> 11:49.320
As we can see, Python source code is very easy to analyze.

11:49.320 --> 11:56.500
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.

11:57.700 --> 11:59.600
Next up we have Electron.

11:59.600 --> 12:03.600
I'm sure many of you are familiar with Electron applications.

12:03.600 --> 12:13.560
It's a well known framework or tool that allows developers and malware authors to create cross platform applications built on web tech.

12:14.400 --> 12:23.960
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.

12:24.220 --> 12:28.480
You can either look for a dependency on the Electron framework.

12:28.500 --> 12:31.580
You can use something like o-tool to dump the dependencies .

12:31.840 --> 12:38.080
Far easier, just look at the application bundle and you'll see the application has a dependency on the Electron framework.

12:38.600 --> 12:42.900
And thus that says, okay, this is indeed a Electron application.

12:43.460 --> 12:47.000
Again, we don't want to focus on reversing the main executable component.

12:47.000 --> 12:53.280
Instead we're interested in the scripts that if this is malware, this is where the malicious logic will lie.

12:54.580 --> 13:03.560
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.

13:03.560 --> 13:06.100
Luckily we can do that with NPX.

13:06.100 --> 13:20.340
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.

13:20.620 --> 13:22.620
In this case, we look at gravity rat again.

13:22.620 --> 13:26.760
There was another instance that was distributed as a Electron app.

13:26.760 --> 13:31.400
Once we run it through NPX, we get the original JavaScript files.

13:31.400 --> 13:36.560
JavaScript is trivial to analyze, especially compared to compiled binary code, right?

13:36.580 --> 13:44.020
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.

13:44.020 --> 13:58.060
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.

13:58.120 --> 14:07.780
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.

14:07.960 --> 14:12.720
Once you have an understanding of this, your continued, more comprehensive analysis can continue.

14:14.000 --> 14:15.400
Next up we have Tori.

14:15.400 --> 14:25.300
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.

14:25.720 --> 14:32.280
Again, the first question is, how do we identify if an application or binary was built with the Tori framework?

14:33.000 --> 14:47.260
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.

14:47.780 --> 14:57.320
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.

14:57.620 --> 15:08.980
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.

15:09.660 --> 15:20.960
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?

15:20.960 --> 15:26.320
Again, this is what I want to extract and analyze because that's likely where the malicious logic will be.

15:26.580 --> 15:41.600
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.

15:41.860 --> 15:56.540
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.

15:57.000 --> 16:04.980
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.

16:05.000 --> 16:10.000
Uh, we can see that it is able to find and extract the embedded JavaScript.

16:10.020 --> 16:20.260
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.

16:20.520 --> 16:21.300
Interesting.

16:21.740 --> 16:29.520
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.

16:29.680 --> 16:43.940
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,

16:43.940 --> 16:46.720
which doesn't contain any malicious logic per se.

16:47.460 --> 16:50.580
Finally, I want to briefly touch on Applescript.

16:50.580 --> 16:59.960
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.

17:00.140 --> 17:06.980
Super easy to identify if, if an application has been, uh, Applescript or originally was an Applescript.

17:06.980 --> 17:14.400
You can look at the resources directory, there'll be a file named main or something similar with the SCPT extension.

17:14.400 --> 17:17.420
Also normally the main application binary is called Applet.

17:17.520 --> 17:27.160
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.

17:29.120 --> 17:33.260
So, let's look at an example, uh, the malware here we're looking at is called osaminer.

17:33.260 --> 17:43.340
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.

17:43.560 --> 17:50.540
Unfortunately for compiled Applescript, osadcompile cannot decompile it.

17:50.540 --> 17:52.820
So, we have to work a little harder.

17:52.980 --> 17:58.560
Luckily there is a great Applescript disassembler written by a researcher named Jinmo on GitHub.

17:58.640 --> 18:05.020
So what we can do is we can feed that Applescript component into that and we can see that we get some decent output.

18:05.020 --> 18:14.100
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?

18:14.780 --> 18:22.540
Well it turns out, uh, researcher Phil Stokes built upon this Applescript disassembler and actually created a Applescript decompiler.

18:22.720 --> 18:24.960
So you can also grab this via GitHub.

18:24.960 --> 18:34.040
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.

18:34.040 --> 18:39.440
So for example, here we can see that osaminer is persisting and executing other components.

18:39.520 --> 18:43.440
This is super useful information for our continued analysis.

18:44.720 --> 18:49.280
Alright, so let's end with some conclusions and takeaways.

18:49.560 --> 18:51.640
So again, I'm all about like working smart, not hard.

18:51.640 --> 18:54.700
So I asked Chachi B, I'm like, what's the main takeaway here?

18:54.700 --> 18:57.740
And he gave me this nice little tagline which I was like, cool, let's roll with it.

18:57.740 --> 19:01.080
Basically said, why reverse engineer when you can simply unwrap?

19:01.080 --> 19:04.480
So that's really the main takeaway from this talk.

19:04.480 --> 19:16.380
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?

19:16.380 --> 19:22.420
First see if it's built with one of these script based, uh, packaging tools that we talked about today.

19:22.900 --> 19:32.460
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.

19:32.540 --> 19:48.360
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.

19:49.360 --> 19:57.160
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.

19:57.740 --> 20:02.300
So if you're interested in learning more, I did mention the Art of Mac Malware book series.

20:02.300 --> 20:06.020
I'm actually doing a book signing at No Starch Press at 11 a.m.

20:06.020 --> 20:08.740
right after this talk, giving out a bunch of free copies.

20:08.740 --> 20:14.100
So if you want to come chat more, get a signed book, maybe get a free copy , first come first serve, stop by.

20:14.280 --> 20:19.200
Also if you're interested in Mac security topics, I would invite you all to Objective by the Sea.

20:19.200 --> 20:22.500
It's a conference that's dedicated to Apple security topics.

20:22.500 --> 20:23.720
Three days, great talks.

20:23.720 --> 20:26.380
I'm also giving a training there on topics like this.

20:26.380 --> 20:31.080
So if this has piqued your interest and you want to learn more, check that out as well.

20:31.860 --> 20:42.620
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?

20:42.860 --> 20:44.880
Also I just want to thank DEF CON.

20:44.880 --> 20:48.660
I mean, amazing conference, so round of applause for DEF CON, which we all love.

20:53.340 --> 20:58.860
And then finally, I briefly want to thank the companies and products that support the Objective Sea Foundation.

20:58.860 --> 21:03.640
They are the ones that support my research, allow the books to be free, support the community.

21:03.640 --> 21:05.400
So I just want to take a moment to thank them.

21:05.400 --> 21:07.560
But again, thank you all for attending.

21:07.560 --> 21:15.920
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.

21:15.920 --> 21:18.500
But again, thank you so much and enjoy the rest of the conference.

