2. • Senior Consultant @ Protiviti
• Execute penetration testing, red teams, etc.
• Enjoy custom implant and tool development
• Casually blog at adapt-and-attack.com
• Not a crypto expert
About Me
3. • Current Landscape
• Keying Review and Advantages
• Keying With Remote Resources
• Tie It Together
Agenda
4. Defense is getting better…
Endpoint
Security (EDR)
Sandboxing
Innovative
Tooling
VirusTotal,
Clouds, ML
Threat
Hunting
Improved
Methodologies
Detection
Capabilities
Researchers
Eventually, you WILL get caught and your remote access code WILL be analyzed…
5. • Command And Control
• Method (HTTP, DNS)
• Server Addresses
• Initial Execution Behavior
• Launching another process?
• Using injection? Process hollowing?
• Endpoint telemetry may catch this, may not…
• Custom Implant
Attacker Crown Jewels
7. Ever think about *what* key data points you lose
control of the moment you send that phishing email?
Ever think about *who* will see those key data
points?
13. • Advantages
• Great technique to defeat automated solutions
• Can slow down defender analysis
• Disadvantages
• Can eventually be reversed once obfuscation is
understood
• Behavior analysis, sandboxing, etc. may reveal behaviors
• can be limited if anti-analysis is included before obfuscation
‘Obfus’ + ‘cation’
@danielhbohannon and @cobbr_io good people to follow for obfuscation-fu!
15. • Keying is the idea of encrypting your payloads and using
local and remote resources to build the decryption key.
• These "resources" could be environmental variables on
the victim's workstation, HTML source of a web page, or
a specific computer name in Active Directory.
When an attacker is using this strategy, they may need to
discover information in order to encrypt the payload
properly.
What Is Keying?
16. • Back in 2016, Josh Pitts (@midnite_runr) and Travis
Morrow (@wired33) presented about this technique
• They released a tool: https://github.com/Genetic-
Malware/Ebowla
• Discussed previous academic studies on the topic
dating back to 1998
• Discussed Gauss – a real world example of malware
using keying in 2012
This is not new
17. • It still works GREAT
• Encrypts your DLLs, EXEs, shellcode, Python,
PowerShell
• Gives you binaries created from Go/Python, or
PowerShell
• Thanks to Josh and Travis on pushing these
techniques forward to the industry
Ebowla Is Awesome
18. • Keying is needed now more than ever
• Share my experiences and use cases
• Common Responses:
• “Oh yeah, that technique sounds useful for bypassing
sandboxes”
• “It’s a cool idea but it seems like a lot of work”
• “I don’t get it”
• “Yeah we use it, we have [X] technique that works really
well for us”
So Why am I Here?
19. Check out the Genetic Malware talks by Josh and
Travis for more in-depth understanding...
Slides:
https://github.com/Genetic-Malware/Ebowla
Video of ekoparty12 presentation:
https://www.youtube.com/watch?v=WI8Y24jTTlw
Keying Review
20. • Attacker is targeting a specific VP of E-Corp
and through OSINT has discovered
• He uses Microsoft Project
C:Program FilesMicrosoft Officeoffice15winproj.exe
• His username is JDoe
• Attacker wants to encrypt his payload so it only
executes on target’s workstation
Keying Review
21. Create Encrypted Data -
1. Hash the key data
“C:Program FilesMicrosoft Officeoffice15winproj.exeJDoe”
2. Use first 32 characters as encryption key
3. Encrypt payload with key
Keying Review
22. Create Keyed Payload -
1. Create functions to retrieve key data
- Get environment variable for “USERNAME”
- Get all paths under “C:Program Files”
2. Loop through all key data and hash each possible key
3. Attempt to decrypt with each key hash
Keying Review
24. • The following data carries over to the payload:
• Method(s) to retrieve key values
• The following data does not carry over to the
payload:
• The actual values (“JDoe”, MS Project file path)
• Original payload
Information Disclosed In Final Payload
25. • This is payload is now target specific and should have
difficulty running on sandboxes, VirusTotal, and
incident responder’s analysis systems
• Difficult to determine the target if sent to all VPs at
E-Corp
Keying Review - Takeaways
26. • Ebowla outputs to a compiled binary generated from
Python or Go
• This is ideal: would require serious analysis and reversing
to determine that the payload is using keying techniques
• Keying use in text-based payloads are subject to
quicker analysis with direct access to source code
Compiled vs Scripting
27. • What does a responder do when they see the
following being used as a key:
[Environment]::GetEnvironmentVariable("USERNAME")
[Environment]::GetEnvironmentVariable(“COMPUTERNAME")
[Environment]::GetEnvironmentVariable(“USERDNSDOMAIN")
• Sent through phishing? Find all recipients of the
email, compile the list, and try each one until it
executes.
• Found as persistence? Well, we know where to get
the environment variables…
Scripting Downsides
28. • Get all possible combinations of those three
environment variables
• Upside of combinations: possible blank values!
• If USERNAME=leo, COMPUTERNAME=pc, and
USERDNSDOMAIN=ecorp
Combinations
leopcecorp
leopc
leoecorp
leo
pcecorp
pc
ecorp
29. • What if E-Corp saw this..?
combos = new array()
combos += GetEnvionVar(“COMPUTERNAME”)
combos += GetEnvironVar(“USERNAME”)
combos += GetTime()
combos += GetSIDOfUser()
combos += GetIPAddressOf(“intranet.tesla.com”)
combos = GenerateCombinations(combos)
Combinations
30. • What if E-Corp saw this..?
combos = new array()
combos += GetEnvionVar(“COMPUTERNAME”)
combos += GetEnvironVar(“USERNAME”)
combos += GetTime()
combos += GetSIDOfUser()
combos += GetIPAddressOf(“intranet.tesla.com”)
combos = GenerateCombinations(combos)
Combinations
31. • Builds on the Combinations idea
• Custom key derivation functions
• Allows attackers to come up with new and creative ways
to specifically target remote access code to endpoints
• Allows attackers to still use keying if they haven’t found
data that fits Ebowla
• Currently supports C#, JScript and PowerShell
• JScript does not use DotNetToJScript
Introducing KeyRing
32. • Combos (string)
• Keying functions that would return one string value
• Ex) environment variables, current time (HH:MM)
• Chains ([]string)
• Keying functions returning one or more strings
• Ex) directory listings, running processes, computer
names in active directory
• Currently supports one chain and unlimited combos
KeyRing Vocabulary
33. KeyRing Decryption Process
combos = new array()
chains = new array ()
combos += GetEnvionVar(“COMPUTERNAME”)
combos += GetEnvironVar(“USERNAME”)
combos += GetTime()
chains = GetRunningProcesses()
combos = GenerateCombinations(combos)
for chain in chains {
for combo in combos {
tryDecryption(path, user, payload, payloadHash)
}
}
34. • More combinations, larger chains…
Keying Considerations
Longer execution
CPU may spike
Fan noise
Hard to determine
final key
38. • If the payload also went to retrieve remote
resources, we could control those resources
(ex: HTTP)
• If we can control the data on those remote
endpoints, we can effectively “turn on” and “turn
off” those resources
• This returns control to the attacker: YOU choose
when your payload executes and when it doesn’t
Adding Remote Resources to Your Keys
39. • Back in 2015, Alex Rymdeko-Harvey (@Killswitch_GUI)
and Chris Truncer (@christruncer) put together an HTTP
Key module for Veil
https://www.veil-framework.com/framework/veil-
evasion/
• This is no longer a part of the Veil project
• Alex’s original blog post is no longer up
https://web.archive.org/web/20161001104512/http://cybersyndicates.com
:80/2015/06/veil-evasion-aes-encrypted-httpkey-request-module/
This also is not new
40. HTTP Key
Key Server C2 Server
www.site1.com www.site2.com
GET /content/dashboard.html
41. HTTP Key
Key Server C2 Server
www.site1.com www.site2.com
Expected HTML returned…
42. HTTP Key
Key Server C2 Server
www.site1.com www.site2.com
C2 Session Established
43. HTTP Key
Key Server C2 Server
www.site1.com www.site2.com
Benign HTML returned…
46. • Use benign response, send phish, wait an hour,
replace benign response
• Replace expected page with benign page
immediately after execution
• May never be decrypted again
• Monitor logs for access attempts to the URL/DNS
record to detect incident response
Use Cases
47. • KeyServer is DNS and HTTP keying made easy
• Easily turn the keys “on” or “off”
• Greppable logs
• Alerts for Slack and Email
• Built to be behind Apache mod_rewrite
Introducing KeyServer
48. • Use Constraints to automate switching the key status
Turn key on between 09:59am and 10:01am
or
Turn key off after it has been accessed once
or
Turn key on once access has been attempted 20 times
or…
KeyServer Constraints
49. • KeyRing includes code to generate HTTP keying for
C#, PowerShell, and JScript
• KeyRing includes code to generate DNS keying for C#
• Uses Pinvoke for DNS queries
• Thanks to @Arno0x0x for
https://github.com/Arno0x/DNSExfiltrator
• DNS and HTTP keying are “Combos” in KeyRing
terms
KeyRing+KeyServer
51. Phishin’ With HTTP Key
Payload + Key Server C2 Server (empire)
www.fakederbycon.com webmail.stealthyc2.com
GET /freetix
GET /dave/kennedy.html
1
2
52. IR Detection with DNS Key
Key Server C2 Server (empire)
keyserver.site webmail.stealthyc2.com
In-Memory w/High Delay
Persistence on
user workstation
AB
55. • I still lost my KeyServer domain
• How could it be better?
Original Payload
Encrypt with HTTP/DNS key
Encrypt with other keying
Could’ve Done Better…
58. If Blue Team blocks KeyServer IP/domain/URL, payload
won’t execute and you will lose access if you don’t
have other methods of persistence
Important Note
60. • Several detection techniques that continue to scare me…
• Improvements to in-memory detection capabilities
(CreateRemoteThread, etc.)
• Abnormal binaries initiating network connections – Sysmon
EID 3
• PowerShell and command line logging
And why are these finally getting the attention they
deserve?
Odds Are In Defenders Favor
62. • If I don’t feel 100% comfortable attempting to get
my payload directly into memory…
Let’s drop things to disk! With keying techniques, I feel
comfortable doing this again
Droppin’ Files To Disk
64. • Prepare a C# dropper
• Implant will use InternetExplorer.Application COM object
for web requests to C2 server
• Get an HTA file onto disk
• Use DotNetToJScript to get C# over to JScript
• Use keying to encrypt DotNetToJScript output
• Embed inside HTA file
• Find a way to execute “mshta.exe
C:fullpathtofile.hta”
Here’s A Reliable Go-To…
65. • No network connections initiated from anywhere but
Internet Explorer or Edge
• Mshta.exe is a signed binary
• All interesting behaviors still occur in memory
• Everyone’s looking for “mshta.exe https://...”
• https://attack.mitre.org/wiki/Technique/T1170
• Bonus: Not a console application, fits nicely with scheduled
tasks
• Downside: Mshta.exe execution itself may set of alarms
The Advantages
67. • Understand the whole file’s behaviors
• Look for encryption/decryption functions
• Be careful interacting with attacker infrastructure
• Be careful running a file to analyze
• Avoid VirusTotal uploads if possible
Defense
68. • Highly recommend implementing forms of Keying
throughout your offensive operations
• Please Key Responsibly…
Hide your remote access code and behaviors as much
as possible during the exercise, but share your
behaviors and IoCs after the exercise.
Conclusion
Warning: I’m not a crypto expert so you don’t have to expect any crazy math in this talk, but what is on the docket is...
I think we can all agree that defense is getting better...
As attackers, we have crown jewels too.
Command and control – this one is the most obvious and applies to everyone. Defenders finding your C2 method and C2 servers is worst case scenario. Yes, red team infrastructure deployments with long haul and different C2s can help, but still not ideal.
Execution behavior – In more advanced techniques, you may be using a more complex execution behavior rather than just executing an empire stager with powershell.exe. This information could help the targeted organization find other infections or this could burn your latest evasion method. Endpoint telemetry may already reveal all of this, with Carbon Black and others. It may not, or it may not reveal the “how”
Custom Implant – More and more places are moving towards custom implants and c2 frameworks. This was actually my own personal motivator. Losing our custom remote access code, even if its just a dropper, is a concern. Real attackers agree.
Phishing is the most common method to get remote access code executed on a remote victim, so most of this presentation will come from that point of view.
Two questions that I frankly didn’t really think about my first few years as a pentester.
Key data points lost here….
C2 server
Using PowerShell Empire (the payload structure is obviously from Empire)
The macro wrote out a file to the user’s temp folder called
And this isn’t just PowerShell.
Lets talk about obfuscation…
This idea I’m about to present about is not new, its actually quite “old”. Its been around for a few years, and actually the presenters showed some academic research from back in 1998.
Ebowla is pretty awesome, and still works great. If you’ve used it at all in the past few years, you’ll notice AVs still have a really hard time putting a signature on what they’re doing.
With heightened defense awareness, improving tools, and the number of prying eyes these days, keying is needed now more than ever. Especially with a constant push and discussion around custom implants and new techniques to bypass these improving defensive tools.
I think a necessary discussion with keying is around compiled vs scripting payloads....
Combinations can help us here mask what exactly are being used as the key
Using ordered combinations, not permutations, we can generate more key values to test with
A big upside to combinations, is when generating it will try each with a blank value as well
The code here we start with creating a chain and combo array
Then we add to the combos and chain arrays by calling various functions
Getting environment variables and time for combos
And getting all running processes on the system into chains
We then generate all combinations of all the combo values
Finally, we loop through each chain, and for each chain we tack on each combo
Now that we have all these chains and combos to choose from, we need to talk a bit about keying considerations
The HTTP key module essentially allowed veil to encrypt a payload based on specific HTML, then you host the HTML on a website. The payload would then reach out to the website, take the HTML, hash it, and try and decrypt.
REMOVE THE A RECORD STUFF
REPLACE SERVER WITH ‘A’
Add dotted line from ‘2’ to C2 server
Obfuscation definitely still needed.. Especially if KeyRing code gets used eventually it will get signatured and obfuscation will be required.
AMSI and scriptblock logging some other reasons why its still needed, for example, if using PowerShell the decrypted code will hit the
So, here’s an example of how I’ve applied this new line of thinking and using keying techniques, by burning one of my go to techniques I’ve used here and there for the past 18 months or so.
Even on MITRE ATT&CK, it only shows example detections that include mshta execution using an HTA on an HTTP server. This was the case with regsvr32 executions.
There’s no specific appliance or technique that will detect keyed payloads, obviously, but here’s some ideas for the defense nonetheless…