SlideShare a Scribd company logo
Cryptanalysis in the
Time of Ransomware
July 28th 2017
DEF CON 25 Crypto Village
Mark Mager
2
 Senior Malware Researcher at
Endgame
 Reverse engineer and software dev
 Please note: I am NOT a cryptographer
 Washington, DC area
 Previously at
 US-CERT
 CYBERCOM
 Lockheed Martin
 Battelle
 Twitter - @magerbomb
About Me
3
Agenda  Ransomware Execution Flow
 Cryptanalysis Workflow
 Walkthroughs
• Powerware
• Nemucod
• TorrentLocker
• Apocalypse
 Research in the Field
 Conclusion
 Questions
4
Ransomware
Execution
Flow
 Payload written disk and executed
 Key generation / retrieval
• Exchange with C2 (optional)
 Enumeration / directory traversal
• Files are individually encrypted
• Ransom note in each directory (optional)
5
Cryptanalysis
Workflow
 Dynamic analysis
• Observe network communications
• Analyze encrypted files
∙ Magic byte sequences / watermarks
∙ Whole vs. partial encryption
• Forensic artifacts on disk
∙ Reg keys, dropped files, event logs
• Repeat tests multiple times
∙ Adjust environment as needed
∙ Known / chosen plaintext attacks
 Reverse engineering
• Identify crypto algorithm(s) used
∙ Implementation mistakes that may
potentially weaken the crypto
• Key generation, storage, transmission
 Apply lessons learned to decrypter dev
6
Walkthroughs  Walk through ransomware encryption schemes
that can be defeated
 Older variants that are no longer in circulation
 High level reverse engineering
 Crypto implementation details
• Note any differences b/w note and reality
 Focus on devising POC for decrypting
Powerware
Cryptanalysis in the Time of Ransomware
Enumeration
Crypto setup
File
write
File Cleanup
10
Let’s deobfuscate this a little bit…
11
Much better! Now what sticks out?
 Symmetric encryption
• RijndaelManaged class (AES)
 256 bit (32 byte) key
 Initialization vector
 Padding with zero
 Cipher block chaining (CBC) mode
 Only the first 2048 bytes of the file are to be read in and encrypted
 Files less than 2048 bytes are ignored
 No further modifications made to the crypto object before CreateEncryptor()
Deobfuscated file transformation code block
13
Back to the crypto setup…
 Symmetric encryption
• RijndaelManaged class (AES)
 256 bit (32 byte) key
 Initialization vector
 Padding with zero
 Cipher block chaining (CBC) mode
Cryptanalysis in the Time of Ransomware
Let’s build our own decrypter!
16
Nemucod
Cryptanalysis in the Time of Ransomware
2048 bytes max
XOR static key
20
Nemucod  Asymmetric crypto
• RSA-1024
 XOR
 Unique key generation
 255 byte key
• Hard-coded
• Same for every file
 Only the first 2048 bytes
 Simple encrypter binary
How can we decrypt our files?
22
TorrentLocker
Cryptanalysis in the Time of Ransomware
Cryptanalysis in the Time of Ransomware
Decompiled view of ctr_encrypt
libtomcrypt source code
Decompiled view of aes_encrypt
libtomcrypt source code
30
TorrentLocker  From limited reverse engineering we know…
• AES
• Counter (CTR) mode
• libtomcrypt
 Could this potentially be vulnerable?
• Implementation flaws
Source: “Counter Mode Security: Analysis and Recommendations”
31
TorrentLocker  A = large plaintext file of NULL bytes
• Exhaust keystream (if file size limitation)
 B = non-NULL plaintext (arbitrary size < than A)
 ENCRYPT(A) = A’
 A’ XOR A = KEYSTREAM
• A = NULL bytes
∙ XOR A is redundant
・ A’ = KEYSTREAM
 ENCRYPT(B) = B’
 B’ XOR KEYSTREAM = B
How can we test
for a flawed
AES-CTR
implementation?
Let’s see if this holds true…
33
34
TorrentLocker  AES
 Counter (CTR) mode
 Static key
 Static IV / nonce
 2 MB file size limit
 No padding
• Need to consider byte alignment and
determine block size to cover edge cases
Apocalypse
Cryptanalysis in the Time of Ransomware
 After viewing ciphertexts spanning multiple file types, a magic byte sequence
reveals itself
• 77 2A 3C D0
 After encrypting a chosen plaintext
containing solely null bytes
• Appears to be some repetition
39
Apocalypse  Magic byte sequence
 Repetition in ciphertext
• Produced from NULL byte plaintext
 Ransom note doesn’t mention encryption type
 Let’s proceed with reverse engineering…
 Text search in IDA Pro for XOR operations
• Most XORs just clearing out registers, but two stick out in sub_40108
• Good place to start, but it’s not always this easy
 The two XORs of interest are looped over
 Appears that the previously identified magic byte
sequence is written by the first WriteFile call
 Second WriteFile writes out the transformed buffer
containing the presumably encrypted data
Cryptanalysis in the Time of Ransomware
Cryptanalysis in the Time of Ransomware
Let’s test out the script…
45
46
Research in
the Field
 Tracking and reverse engineering new variants
 Developing and releasing decrypters for free
 BleepingComputer forums
• BloodDolly
 @malwarehunterteam
• http://id-ransomware.malwarehunterteam.com
 @malwaretech
• WannaCry killswitch
• https://www.malwaretech.com
 @demonslay335
 … and many others!
Despite
proliferation,
researchers have
kept pace
47
Conclusion  Crypto implementation issues prevalent
• “Crypto is hard”
 Ransom notes are not trustworthy for technical
specs
• Don’t believe the hype
 RE and cryptanalysis / decrypter dev are not
linear processes
• Known / chosen plaintext attacks
• Trial and error
• Focus on sections where modifications
occur, then dig deeper for more clues
• Build out POC, then stress test and harden
as needed to cover all edge cases
Thanks!
Twitter - @magerbomb
E-mail - mager@endgame.com

More Related Content

PDF
CNIT 141: 4. Block Ciphers
PDF
CNIT 141: 1. Encryption
PDF
CNIT 141: 6. Hash Functions
PDF
Block Ciphers Modes of Operation
PDF
CNIT 141 5. Stream Ciphers
PDF
Pyongyang Fortress
PDF
CNIT 1417. Keyed Hashing
PPTX
How to Secure Containers
CNIT 141: 4. Block Ciphers
CNIT 141: 1. Encryption
CNIT 141: 6. Hash Functions
Block Ciphers Modes of Operation
CNIT 141 5. Stream Ciphers
Pyongyang Fortress
CNIT 1417. Keyed Hashing
How to Secure Containers

What's hot (20)

PDF
CNIT 141: 1. Encryption
PPTX
Find the Hacker
PPTX
Introduction to Dynamic Malware Analysis ...Or am I "Cuckoo for Malware?"
PDF
Block Cipher Modes of Operation And Cmac For Authentication
PDF
Sysdig Open Source Intro
PPTX
4055-841_Project_ShailendraSadh
PDF
Authenticated Encryption Gcm Ccm
PDF
CNIT 141 6. Hash Functions
PDF
Software Security
PPTX
Block Cipher
PPT
Block Ciphers Modes of Operation
PDF
Sheila Ayelen Berta - The Art of Persistence: "Mr. Windows… I don’t wanna go ...
PPT
RC4&RC5
PPTX
Malware analysis using volatility
PDF
Unpack your troubles*: .NET packer tricks and countermeasures
PDF
Sysdig Tokyo Meetup 2018 02-27
PPT
13528 l8
PPTX
I mage encryption using rc5
PDF
Bootkits: Past, Present & Future - Virus Bulletin
PDF
Kalyna block cipher presentation in English
CNIT 141: 1. Encryption
Find the Hacker
Introduction to Dynamic Malware Analysis ...Or am I "Cuckoo for Malware?"
Block Cipher Modes of Operation And Cmac For Authentication
Sysdig Open Source Intro
4055-841_Project_ShailendraSadh
Authenticated Encryption Gcm Ccm
CNIT 141 6. Hash Functions
Software Security
Block Cipher
Block Ciphers Modes of Operation
Sheila Ayelen Berta - The Art of Persistence: "Mr. Windows… I don’t wanna go ...
RC4&RC5
Malware analysis using volatility
Unpack your troubles*: .NET packer tricks and countermeasures
Sysdig Tokyo Meetup 2018 02-27
13528 l8
I mage encryption using rc5
Bootkits: Past, Present & Future - Virus Bulletin
Kalyna block cipher presentation in English
Ad

Similar to Cryptanalysis in the Time of Ransomware (20)

DOCX
Discussion Question Contrast file encryption and volume encryptio.docx
PPTX
Advanced malwareanalysis training session2 botnet analysis part1
PDF
CNIT 126: 13: Data Encoding
PDF
CNIT 126 13: Data Encoding
PPTX
Ch02 NetSec5e Network Security Essential Chapter 2.pptx
PDF
Image and text Encryption using RSA algorithm in java
PDF
Practical Malware Analysis Ch13
PDF
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
PDF
Linux Kernel Cryptographic API and Use Cases
PDF
Aes jul-upload
PPT
symet.crypto.hill.cipher.2023.ppt
PDF
When AES(☢) = ☠ - Episode V
PDF
Searchable Encryption Systems
PDF
DEFCON 23 - Eijah - crypto for hackers
PPTX
A study of cryptography for satellite applications
PDF
paper5.pdf
PDF
doc5.pdf
PDF
doc5.pdf
PDF
lecture4.pdf
PDF
sheet5.pdf
Discussion Question Contrast file encryption and volume encryptio.docx
Advanced malwareanalysis training session2 botnet analysis part1
CNIT 126: 13: Data Encoding
CNIT 126 13: Data Encoding
Ch02 NetSec5e Network Security Essential Chapter 2.pptx
Image and text Encryption using RSA algorithm in java
Practical Malware Analysis Ch13
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
Linux Kernel Cryptographic API and Use Cases
Aes jul-upload
symet.crypto.hill.cipher.2023.ppt
When AES(☢) = ☠ - Episode V
Searchable Encryption Systems
DEFCON 23 - Eijah - crypto for hackers
A study of cryptography for satellite applications
paper5.pdf
doc5.pdf
doc5.pdf
lecture4.pdf
sheet5.pdf
Ad

Recently uploaded (20)

PDF
737-MAX_SRG.pdf student reference guides
PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
Sustainable Sites - Green Building Construction
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Artificial Intelligence
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Project quality management in manufacturing
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
737-MAX_SRG.pdf student reference guides
Safety Seminar civil to be ensured for safe working.
Sustainable Sites - Green Building Construction
Internet of Things (IOT) - A guide to understanding
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Model Code of Practice - Construction Work - 21102022 .pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Embodied AI: Ushering in the Next Era of Intelligent Systems
Artificial Intelligence
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
573137875-Attendance-Management-System-original
Current and future trends in Computer Vision.pptx
Fundamentals of safety and accident prevention -final (1).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Project quality management in manufacturing
R24 SURVEYING LAB MANUAL for civil enggi
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx

Cryptanalysis in the Time of Ransomware

  • 1. Cryptanalysis in the Time of Ransomware July 28th 2017 DEF CON 25 Crypto Village Mark Mager
  • 2. 2  Senior Malware Researcher at Endgame  Reverse engineer and software dev  Please note: I am NOT a cryptographer  Washington, DC area  Previously at  US-CERT  CYBERCOM  Lockheed Martin  Battelle  Twitter - @magerbomb About Me
  • 3. 3 Agenda  Ransomware Execution Flow  Cryptanalysis Workflow  Walkthroughs • Powerware • Nemucod • TorrentLocker • Apocalypse  Research in the Field  Conclusion  Questions
  • 4. 4 Ransomware Execution Flow  Payload written disk and executed  Key generation / retrieval • Exchange with C2 (optional)  Enumeration / directory traversal • Files are individually encrypted • Ransom note in each directory (optional)
  • 5. 5 Cryptanalysis Workflow  Dynamic analysis • Observe network communications • Analyze encrypted files ∙ Magic byte sequences / watermarks ∙ Whole vs. partial encryption • Forensic artifacts on disk ∙ Reg keys, dropped files, event logs • Repeat tests multiple times ∙ Adjust environment as needed ∙ Known / chosen plaintext attacks  Reverse engineering • Identify crypto algorithm(s) used ∙ Implementation mistakes that may potentially weaken the crypto • Key generation, storage, transmission  Apply lessons learned to decrypter dev
  • 6. 6 Walkthroughs  Walk through ransomware encryption schemes that can be defeated  Older variants that are no longer in circulation  High level reverse engineering  Crypto implementation details • Note any differences b/w note and reality  Focus on devising POC for decrypting
  • 10. 10 Let’s deobfuscate this a little bit…
  • 11. 11 Much better! Now what sticks out?  Symmetric encryption • RijndaelManaged class (AES)  256 bit (32 byte) key  Initialization vector  Padding with zero  Cipher block chaining (CBC) mode
  • 12.  Only the first 2048 bytes of the file are to be read in and encrypted  Files less than 2048 bytes are ignored  No further modifications made to the crypto object before CreateEncryptor() Deobfuscated file transformation code block
  • 13. 13 Back to the crypto setup…  Symmetric encryption • RijndaelManaged class (AES)  256 bit (32 byte) key  Initialization vector  Padding with zero  Cipher block chaining (CBC) mode
  • 15. Let’s build our own decrypter!
  • 16. 16
  • 19. 2048 bytes max XOR static key
  • 20. 20 Nemucod  Asymmetric crypto • RSA-1024  XOR  Unique key generation  255 byte key • Hard-coded • Same for every file  Only the first 2048 bytes  Simple encrypter binary
  • 21. How can we decrypt our files?
  • 22. 22
  • 26. Decompiled view of ctr_encrypt
  • 28. Decompiled view of aes_encrypt
  • 30. 30 TorrentLocker  From limited reverse engineering we know… • AES • Counter (CTR) mode • libtomcrypt  Could this potentially be vulnerable? • Implementation flaws Source: “Counter Mode Security: Analysis and Recommendations”
  • 31. 31 TorrentLocker  A = large plaintext file of NULL bytes • Exhaust keystream (if file size limitation)  B = non-NULL plaintext (arbitrary size < than A)  ENCRYPT(A) = A’  A’ XOR A = KEYSTREAM • A = NULL bytes ∙ XOR A is redundant ・ A’ = KEYSTREAM  ENCRYPT(B) = B’  B’ XOR KEYSTREAM = B How can we test for a flawed AES-CTR implementation?
  • 32. Let’s see if this holds true…
  • 33. 33
  • 34. 34 TorrentLocker  AES  Counter (CTR) mode  Static key  Static IV / nonce  2 MB file size limit  No padding • Need to consider byte alignment and determine block size to cover edge cases
  • 37.  After viewing ciphertexts spanning multiple file types, a magic byte sequence reveals itself • 77 2A 3C D0
  • 38.  After encrypting a chosen plaintext containing solely null bytes • Appears to be some repetition
  • 39. 39 Apocalypse  Magic byte sequence  Repetition in ciphertext • Produced from NULL byte plaintext  Ransom note doesn’t mention encryption type  Let’s proceed with reverse engineering…
  • 40.  Text search in IDA Pro for XOR operations • Most XORs just clearing out registers, but two stick out in sub_40108 • Good place to start, but it’s not always this easy
  • 41.  The two XORs of interest are looped over  Appears that the previously identified magic byte sequence is written by the first WriteFile call  Second WriteFile writes out the transformed buffer containing the presumably encrypted data
  • 44. Let’s test out the script…
  • 45. 45
  • 46. 46 Research in the Field  Tracking and reverse engineering new variants  Developing and releasing decrypters for free  BleepingComputer forums • BloodDolly  @malwarehunterteam • http://id-ransomware.malwarehunterteam.com  @malwaretech • WannaCry killswitch • https://www.malwaretech.com  @demonslay335  … and many others! Despite proliferation, researchers have kept pace
  • 47. 47 Conclusion  Crypto implementation issues prevalent • “Crypto is hard”  Ransom notes are not trustworthy for technical specs • Don’t believe the hype  RE and cryptanalysis / decrypter dev are not linear processes • Known / chosen plaintext attacks • Trial and error • Focus on sections where modifications occur, then dig deeper for more clues • Build out POC, then stress test and harden as needed to cover all edge cases
  • 48. Thanks! Twitter - @magerbomb E-mail - mager@endgame.com