start	end	text
0	2900	We have a great speaker this morning.
3000	6880	Patrick Bortles is going to talk to us about Mac malware.
7880	11680	Good morning, aloha, and welcome to my talk.
17560	20320	Thank you for being here, especially so early.
20320	21680	Really appreciate it.
22060	23020	Wow.
23180	25120	Okay, we might have to try that again at the end.
25240	26880	But yeah, super stoked you're here.
26880	36960	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.
38280	39780	My name is Patrick Bortles.
39780	51320	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.
53300	62760	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.
62760	67120	So I want to mention the book, because first it's free, so this is not a sales pitch.
67260	73600	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.
74960	81900	All right, so we're going to start with a brief introduction and talk about some concepts before diving into some more technical details.
82240	85180	So imagine you are a malware analyst.
85180	87780	And if you are a malware analyst, you don't have to imagine.
87860	91200	But if you are, imagine you encounter an unknown file.
91320	95900	Generally your goal is to quickly classify it in one of three categories.
96180	97140	Is it good?
97140	97940	Is it benign?
97940	99100	And if it is, you're done.
99100	103980	There's no point in the context of malware analysis to reverse engineer known good files.
104340	113480	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.
113480	118640	However, if it is malicious and appears to be brand new, that's where I think the fun begins.
118640	122600	This is where you're going to do the full analysis, write a report, create IOCs.
122600	126000	But again, the goal is we want to do this fairly quickly and efficiently.
127160	136140	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.
136140	143760	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.
143880	152140	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.
152140	153640	They're human readable.
154040	164540	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.
164540	166160	Which is great but it's time consuming.
166160	168400	You need a certain amount of technical knowledge.
168400	173860	And so what if it turns out that that binary is really just an execution host for a script?
173980	180020	So the question is can then we skip all these time consuming, in depth, low level binary analysis steps?
180020	183080	And well, in many cases the answer is yes.
183800	187700	So what are these script based or non binary binaries?
187700	194420	Well, they're basically binaries that originally were scripts that are now packaged up into binaries.
194420	197380	And shortly we'll explain why this is the case.
197380	201680	So on the slide we have a simple first stage component of some malware.
201740	206820	The malware author has packaged it up into an executable application binary.
207000	208320	Sends it to the user.
208440	209740	The user executes it.
209740	218280	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.
219780	223880	So you might be wondering why are these non binary binaries even a thing?
223880	228060	Well, it all comes down to simplicity and what I refer to as runability.
228060	231840	So for one, scripts are easier to write than compiled code.
231840	236920	So a lot of script kitties start writing malware by creating malicious scripts.
237680	240240	Number two, scripts are often a little bit more cross platform.
240240	247940	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.
248100	256620	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.
256620	261980	But many of them will unfortunately double click on, for example, an application binary.
261980	269860	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.
269860	274340	Versus saying, hey user, go to the terminal and run python malicious file dot py.
274340	276680	That's not really ideal.
278080	280940	Now then, the question is, well why should you care?
280940	289400	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.
289420	293300	An executable loader and then the actual malicious script.
293380	300100	Now, take it from me, you don't want to spend days reverse engineering this malicious or rather non-malicious binary loader.
300120	306440	Uh, and as we'll see a lot of times these loaders are benign, although they can be and are abused by malware.
306560	312900	Uh, instead you want to be able to recognize that this binary contains something like a script and then go analyze that.
312900	319800	Because that is actually where the malicious component lies and it's far easier to analyze a script versus a compiled executable.
320080	333920	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,
333920	336840	in that means to perform your continued analysis.
337600	344900	Okay, so now let's talk about how we can identify these non-binary binaries and then how to extract scripts.
346440	352820	So it turns out there's many ways to take a script and package it up into something that's more easily runnable.
352820	355960	For example, a native Mac OS application.
355980	357900	I've listed some of the tools that we will look at.
357900	361420	I really need to stress that these tools by themselves are not malicious.
361420	362620	They are totally benign.
362620	367180	Many of you are probably familiar with pi installer, electron, et cetera, et cetera.
367180	376800	But why we are talking about them today is first they all take as input scripts and can create native executables for application binaries.
376860	383740	But as we will see they have all been abused by malware authors to make essentially malicious applications.
384340	395180	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.
395180	403720	Then once we know that we can talk about how to extract or at least reconstruct the script that facilitates our binary analysis.
403720	410220	Well see this is actually pretty straight forward but this is still something very practical to uh, to understand.
410260	414100	And again worth noting each tool packages up the script in a different way.
414100	421560	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.
421820	426480	So let's now go through some of the common script based packaging utilities.
426520	432400	Uh, we'll talk about how to identify each and talk about malware samples that have abused that.
432560	434260	We're going to start with platypus.
434260	441640	Platypus is open source and as its developers say it allows you to create Mac apps from command line scripts.
442020	445480	Since it's open source we can actually look at the loader code.
445480	449940	This is the binary component that gets packaged up and runs for example when the user double clicks it.
449940	455520	And we can see for example the name and location of the script is directly hard coded.
455700	462080	Also when this binary component is run all it does is go and executes a script.
462320	467220	So this makes it actually pretty easy to identify platypus applications.
467320	473540	If you do throw it in a disassembler you'll see a lot of symbol names that start with script exec.
473800	482220	You'll also see that the application delegate, this is like the main function for apps, basically just invokes a method named execute script.
482580	489500	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.
489500	502020	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.
502500	512160	So now let's look at some malicious code that abused platypus to distribute scripts in a application bundle to macOS users.
512160	514260	Starting with creative update.
514260	517000	This was used in a pretty nasty supply chain attack.
517000	525180	Adversaries hacked the popular macupdate.com website and distributed trojanized versions of firefox.
525200	532300	If we take a closer look at the application bundle we can see in the resources directory there is a file named script.
532900	537680	And if we open this, this is actually the malicious first stage component of the malware.
537780	539020	And as you can see it's a script.
539020	540300	It's very easy to analyze.
540300	541240	It's very easy to see.
541240	542300	It's human readable.
542340	543940	We didn't have to open a disassembler.
543940	545500	We didn't have to do any debugger.
545500	546620	And this is awesome.
547160	554200	If we run a process monitor and execute the malware we can see indeed the application bundle does both drop and execute this script.
555600	560680	Another example of another platypus application that was malware was Eleanor.
560700	562260	Again, same approach.
562260	566500	If we look at the resource directory we see a script called script.
566500	569520	And if we open it, again it's a human readable script.
569520	574220	We can see it contains the installer and persistent logic for the malware.
574220	574880	Awesome.
574880	576740	Again, pretty trivial, right?
577580	579920	Let's now talk about PyInstaller.
579920	582020	Many of you are probably familiar with PyInstaller.
582020	585060	Again, there is nothing bad about PyInstaller.
585060	593040	It's just a utility that allows you to, given a Python script, build a native executable, for example, for Mac OS.
593140	596480	And so malware authors can and do abuse it.
596880	605460	One point, PyInstaller will take the Python script it's packaging up and compile it into compiled byte, uh, Python byte code.
605460	607480	So we'll talk about that on the next slide.
607540	614040	So, given an application or binary you're analyzing it, how do you determine if it's been created via PyInstaller?
614040	615360	Oh, pretty quick.
615360	619700	If you look at the main function, you can see it calls py underscore main.
619700	621960	But again, we're trying to stay away from reverse engineering.
621980	627360	So easier, just look at the embedded strings that you can extract with the strings utility.
627360	631980	And you will see there are many PyInstaller related strings in the application.
632840	640420	So, once you've identified this application as, hey, this was created with PyInstaller, how do you extract the Python script?
640420	641500	How do you reconstruct it?
641500	646140	Because that's going to contain the malicious logic that you want to analyze if it is malware.
646540	654480	Well, since PyInstaller actually embeds the Python script directly into the binary, it's not in an external resource like it was with platypus.
654480	656120	We have to use a dedicated tool.
656420	660920	Luckily there's a great PyInstaller extractor that's available on GitHub.
660920	667700	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.
667700	669760	And it extracts it for us.
669880	678180	This is all well and good, except as I mentioned, PyInstaller will first compile this Python script into Python compiled byte code.
678180	684380	So, even though we've extracted the script, it's in this intermediary non-human readable format.
685700	686600	Not a problem.
686600	690580	There's a lot of decompilers for Python compiled byte code.
690580	695420	Uh, so you can just upload it to one of those or use the uncompile command line utility.
695420	705740	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.
705840	709320	As we can see, Python source code is very easy to analyze.
709320	716500	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.
717700	719600	Next up we have Electron.
719600	723600	I'm sure many of you are familiar with Electron applications.
723600	733560	It's a well known framework or tool that allows developers and malware authors to create cross platform applications built on web tech.
734400	743960	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.
744220	748480	You can either look for a dependency on the Electron framework.
748500	751580	You can use something like o-tool to dump the dependencies .
751840	758080	Far easier, just look at the application bundle and you'll see the application has a dependency on the Electron framework.
758600	762900	And thus that says, okay, this is indeed a Electron application.
763460	767000	Again, we don't want to focus on reversing the main executable component.
767000	773280	Instead we're interested in the scripts that if this is malware, this is where the malicious logic will lie.
774580	783560	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.
783560	786100	Luckily we can do that with NPX.
786100	800340	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.
800620	802620	In this case, we look at gravity rat again.
802620	806760	There was another instance that was distributed as a Electron app.
806760	811400	Once we run it through NPX, we get the original JavaScript files.
811400	816560	JavaScript is trivial to analyze, especially compared to compiled binary code, right?
816580	824020	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.
824020	838060	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.
838120	847780	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.
847960	852720	Once you have an understanding of this, your continued, more comprehensive analysis can continue.
854000	855400	Next up we have Tori.
855400	865300	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.
865720	872280	Again, the first question is, how do we identify if an application or binary was built with the Tori framework?
873000	887260	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.
887780	897320	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.
897620	908980	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.
909660	920960	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?
920960	926320	Again, this is what I want to extract and analyze because that's likely where the malicious logic will be.
926580	941600	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.
941860	956540	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.
957000	964980	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.
965000	970000	Uh, we can see that it is able to find and extract the embedded JavaScript.
970020	980260	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.
980520	981300	Interesting.
981740	989520	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.
989680	1003940	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,
1003940	1006720	which doesn't contain any malicious logic per se.
1007460	1010580	Finally, I want to briefly touch on Applescript.
1010580	1019960	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.
1020140	1026980	Super easy to identify if, if an application has been, uh, Applescript or originally was an Applescript.
1026980	1034400	You can look at the resources directory, there'll be a file named main or something similar with the SCPT extension.
1034400	1037420	Also normally the main application binary is called Applet.
1037520	1047160	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.
1049120	1053260	So, let's look at an example, uh, the malware here we're looking at is called osaminer.
1053260	1063340	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.
1063560	1070540	Unfortunately for compiled Applescript, osadcompile cannot decompile it.
1070540	1072820	So, we have to work a little harder.
1072980	1078560	Luckily there is a great Applescript disassembler written by a researcher named Jinmo on GitHub.
1078640	1085020	So what we can do is we can feed that Applescript component into that and we can see that we get some decent output.
1085020	1094100	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?
1094780	1102540	Well it turns out, uh, researcher Phil Stokes built upon this Applescript disassembler and actually created a Applescript decompiler.
1102720	1104960	So you can also grab this via GitHub.
1104960	1114040	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.
1114040	1119440	So for example, here we can see that osaminer is persisting and executing other components.
1119520	1123440	This is super useful information for our continued analysis.
1124720	1129280	Alright, so let's end with some conclusions and takeaways.
1129560	1131640	So again, I'm all about like working smart, not hard.
1131640	1134700	So I asked Chachi B, I'm like, what's the main takeaway here?
1134700	1137740	And he gave me this nice little tagline which I was like, cool, let's roll with it.
1137740	1141080	Basically said, why reverse engineer when you can simply unwrap?
1141080	1144480	So that's really the main takeaway from this talk.
1144480	1156380	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?
1156380	1162420	First see if it's built with one of these script based, uh, packaging tools that we talked about today.
1162900	1172460	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.
1172540	1188360	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.
1189360	1197160	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.
1197740	1202300	So if you're interested in learning more, I did mention the Art of Mac Malware book series.
1202300	1206020	I'm actually doing a book signing at No Starch Press at 11 a.m.
1206020	1208740	right after this talk, giving out a bunch of free copies.
1208740	1214100	So if you want to come chat more, get a signed book, maybe get a free copy , first come first serve, stop by.
1214280	1219200	Also if you're interested in Mac security topics, I would invite you all to Objective by the Sea.
1219200	1222500	It's a conference that's dedicated to Apple security topics.
1222500	1223720	Three days, great talks.
1223720	1226380	I'm also giving a training there on topics like this.
1226380	1231080	So if this has piqued your interest and you want to learn more, check that out as well.
1231860	1242620	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?
1242860	1244880	Also I just want to thank DEF CON.
1244880	1248660	I mean, amazing conference, so round of applause for DEF CON, which we all love.
1253340	1258860	And then finally, I briefly want to thank the companies and products that support the Objective Sea Foundation.
1258860	1263640	They are the ones that support my research, allow the books to be free, support the community.
1263640	1265400	So I just want to take a moment to thank them.
1265400	1267560	But again, thank you all for attending.
1267560	1275920	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.
1275920	1278500	But again, thank you so much and enjoy the rest of the conference.
