Cryptography Session 2020 v1
Cryptography Session 2020 v1
1
About Me
3
Challenge: Decrypt ‘Crypto’ 10
15
210 mins
� Prev. editions are also free!
https://www.cl.cam.ac.uk/~rja1
4/book.html
4
‘Crypto’
5
Crypto Building Blocks
6
Encryption
� Encryption – an encoding which can be reversed (given a key)
� A plaintext (M) message is encrypted by a cipher ({}) to a ciphertext (E) using a key (K)
E = {M}K
� Decryption is possible with the cipher, the ciphertext, and the key
� e.g. AES, RSA, ECC, 3DES, …
This might seem obvious to some – but it is not uncommon to encounter base64
‘encryption’ in the wild.
8
Hashes
� (Cryptographic) Hashes – not an encoding & not reversible
� Different than the larger, general class of hash functions
� For a cryptographic hash function f:
given f(x) -- you can’t find / guess / calculate x
� Modern Crypto
� Deals with numbers: input & output
� Text is treated as numbers via encodings – ASCII or UTF-8 is the most likely encoding
e.g. CE6B02C8CDD27C50C8C9459517DF1E5EBD1FBB14DF554D5FC1FE647DBDCAE881 10
Stream Ciphers
� One-Time Pad (OTP) – the only proven secure
encryption scheme
� Uses random key-stream, of length equal to or
greater than the message
� Then combine key-stream with message (assume
XOR)
� Stream Ciphers – approximate the OTP
� Expand short key into pseudo-random keystream
� Then XOR
Di Kyle Siehl - Self-made, based on raster w:Image:Wep-crypt.png, which was taken with permission from The Final
� e.g. RC4, Salsa20, FISH Nail in WEPs Coffin, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=1806804
By WhiteTimberwolf (SVG version) - PNG version, Public Domain, By WhiteTimberwolf (SVG version) - PNG version, Public Domain,
https://commons.wikimedia.org/w/index.php?curid=26434116 https://commons.wikimedia.org/w/index.php?curid=26434096
12
Symmetric / Asymmetric Crypto
� Symmetric Crypto – can be encrypted + decrypted by any party with the SAME key
� e.g. any of the crypto we’ve discussed so far
� Asymmetric Crypto – can be encrypted by any party for a specific recipient
� aka public-key cryptography
� Leverages certain problems that are hard in one way & easy in the other: prime factorization and
discrete logarithms
� Keys exist as pairs of public & private halves -- key-pairs
� The party with the private key can decrypt & sign (more on signatures later)
� Any parties with the public key can encrypt & verify
� e.g. RSA, ECC
� e.g.
-----BEGIN RSA PRIVATE KEY-----
izfrNTmQLnfsLzi2Wb9xPz2Qj9fQYGgeug3N2MkDuVHwpPcgkhHkJgCQuuvT+qZI
… 13
Crypto Building Blocks
Section Summary
� Encryption… it hides information, binds it – protects confidentiality, but not integrity (without additional
effort)
� E = {M}K
� (Crypto) Hashes – one-way functions. With f(x) you cannot get x
� ’Classic’ Crypto – involves substituting alphabets: rotation or re-mapping (we’re over-simplifying –
there’s only 50 minutes here)
� Stream Cipher – combine a sequence of key bits with a sequence of cleartext bits with XOR (or
effectively XOR)
� Block Ciphers – have a limited key sequence, but extend to larger cleartext sequences
� Not all block cipher modes are created equal (e.g. Electronic Coloring Book (ECB))
� Symmetric Crypto – all parties share the same key
� Asymmetric Crypto – only one party has the decryption key (private key)
Attacks on Building Blocks
15
Attacking Hashes
� Google.
� Seriously... google this 21232f297a57a5a743894a0e4a801fc3 (from before) now
� Identifying what type of hash you have in-hand will be useful – the length gives it away
� If you don’t know lengths yet, use hash detector tools; e.g. cothan/hashdetector
� Hash Crack sites
� hashcat tool
� (ab)uses your GPU for rapid hash cracking
� Rainbow Tables
� ’halves’ / parts-of hashes pre-built and ready to go
� For things like MD5 these are trivial
� For things like SHA-256 these are huge (multi-TB)
� You can pick-up pre-generated tables at DEFCON Data Duplication Village. Bring a 6 TiB HDD.
� Salts
� Because it’s pretty easy to lookup or build a table of known inputs for hashes; designers
tend to follow the best practice of ’salting’ their inputs
� D033e22ae348aeb5660fc2140aec35850c4da997 = SHA1(‘admin’)
� 3947cdf52a551de4983746545a1affdb2b04f4a2 = SHA1(‘saltadmin’)
� Salts are usually pre-prepended onto the input; sometimes with a separator like ‘.’ or ’+’
� hashcat can find a salt for a given hash and input pair.
� hashcat can also find inputs for hashes with a given salt as a parameter.
� Find the salt with one known hash first.
� OR find the salt with research (some systems’ password salts are well-known)
18
Still More on Attacking Hashes
� Password lists
� Brute-forcing (all possible character combinations) for
inputs to hashes is possible
� ‘password lists’ are more useful. There are hundreds of
these to choose from, most from data breaches over the
past years.
� In CTFs the rockyou list is the most common – but for
applied hash cracking: YMMV.
� This is more generally known as a dictionary attack 19
Hands On: 15 Minute Challenge
Reverse these hashes:
� 5f4dcc3b5aa765d61d8327deb882cf99
� 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
� ecadec2924e86bf88d622ceb0855382d
� ff4827739b75d73e08490b3380163658
� 6ce3bb6eb450df7d6345151ec00e4a4e
� Today (challenges/puzzles/RE):
� Try shift ciphers (start with ROT13)
� Then try a substitution cipher
� Then have ‘fun’ : http://rumkin.com/tools/cipher/
Hands On: 10 min Classic Crypto Attack Example
Ploregehpx 2018 sbe gur jva!
N uhtr gunax lbh arrqf gb tb bhg gb bhe fcbafbef. Guvf ceb-vaqhfgel rirag qrcraqf ba npgvir
fcbafbe vaibyirzrag naq fhccbeg.
26
Cryptanalysis and More
� Linear Cryptanalysis
� solving for linear relationships between cleartext (input) and ciphertext (outputs)
� at fractional likelihoods
� using the likelihoods to sometimes predict ciphertext from cleartext
� 'correct' crypto is designed to resist these attacks
� Differential Cryptanalysis
� solving for sensitivity relationships of changes to cleartext bits (input) onto ciphertext bits (outputs)
� at fractional likelihoods
� then use any high likelihoods to guide attacks with chosen inputs
� Modern ‘correct’ crypto is design to resist these attacks too 27
Reality Check
� We talked about attack models & attack goals; some families of attacks
� No simple attacks after ‘Classic’ crypto
� Few practical attacks
� Attacking Crypto these ways is hard, for ’correct’ crypto:
� e.g. SHA-256, AES-128, RSA-2048, ECC w/ curve 25519
Lqydolg#Sdvvzrug$ Sdvvzrug#RN$$$#=,
Hints::
• from the IOLI crackme challenges:
pof.eslack.org/tmp/IOLI-crackme.tar.gz
• ‘Sdvvzrug’ shows up in both strings, this tells you something
• ‘#’ is 0x35 and ‘$’ is 0x36
• ‘ ’ is 0x32 and ‘!’ is 0x33
• rumkin.com/tools/cipher/caesar.php
Done? Already? Do a ‘beginner’ challenge at potatopla.net/crypto/ 29
Other Attacks on Block Ciphers
AES_ECB( )=
https://github.com/pakesson/diy-ecb-penguin
Other Attacking Building Blocks
� Software Exploitation can yield both control of the software and also information leaks
� Access to process memory can be fruitful key extraction attacks
� Multiple tools are available to scour memory for keys: aeskeyfind, radare2,
volatility
� Reverse engineering of the program code in memory can yield pointers to the memory
locations of keys
32
Aside: Entropy Visualization (cont’d)
� The entropy estimates can be broken-up over a large input and visualized
� You can identify and distinguish between
� encrypted (correct) content
� Other encrypted (incorrect) content
� Compressed content
� Rules of thumb:
� Compression looks like pretty high entropy
� Encryption looks like really high entropy
33
Aside: Entropy Visualization (cont’d)
Image
binvis.io
entropy
34
Hands-On: 15 Minute Challenge
Use Entropy Visualization (and anything else) to Identify:
1) A compressed file
2) An ECB-mode encrypted file
3) A ‘correct’ encrypted file
In the set: https://goo.gl/LbzMbE
37
Protocols
► The target looks-up encryption key KT for ID T; decrypts the {…} KT and checks the nonce N hasn’t been seen before.
► Nonce : Number used ONCE
► E.g. older keyfobs / garage door openers – source is the fob, target is the car or garage door.
39
Protocol: Message Authentication Codes (MAC)
� Message Authentication Codes: for a message: create a value that can enable the message to be verified by any party with the
shared key -- the same shared key is used to create the value.
� e.g. CBC-MAC – build a MAC with CBC chaining mode of a block cipher
� e.g. CMAC – also uses a block cipher
� e.g. HMAC – build a mac with a hash function
� E.g. CBC-MAC-AES128, HMAC-SHA1, etc.
� Parties receiving messages that don’t verify against the key (shared in this case) shall discard messages
� How the shared keys are distributed and how messages are discarded is additional protocol details (for the next layer of
the protocol specification)
� In both MAC and Signatures, parties receiving messages that don’t verify against the key (public in this case) shall discard
messages
� How the public keys are distributed and how messages are discarded is additional protocol details (for the next layer of the
protocol specification)
� e.g. what if they sent: K|M|{H(M)}k where K is the public key?
Protocol: Challenge-Response (in General)
� Source wants to be authenticated by the target
� Source receives a nonce as challenge
� Transforms it and replies as response
� An ideal C-R would make it impractical for an attacker to guess the secret by observing traffic of multiple C-R
exchanges.
� If attacker sees both challenge and response: known plaintext attack
source target
Ready
rand()
Challenge: N
Crypto Crypto
f f
(・) (・)
Response: f(N) =
42
PASS / FAIL
Protocols
Section Summary
44
Attacks on Protocols
� Simple Authentication assumes that the key KT is associated with the ID T and
� Are there other T that could associate with KT? Try sending to other target (Key collision attack)
Attacks on MAC
� For digests
� Recall: these aren’t actually MACs – but they get used that way occasionally
� Recall: you will know the input, i.e. you will have at least one digest:message pair
� You need to identify digest algorithm – length usually gives it away; also see tools like
cothan/hashdetector
� You may need to identify the salt also – hashcat can do this
M|{H(M)}k
� Agreeing on the K public key for the k private key is a critical part.
K|M|{H(M)}K
� Then an attack is to use your own private/public key pair a/A and send:
A|M|{H(M)}a
� Watch out for this broken protocol (sending the pubkey). It happens sometimes…
� More generally: try to find ways to substitute the expected public key K for your key, A 48
� Stored in flash somewhere?
Attack on Challenge-Response
Middleperson Attack (in General)
� Interposing an actor in-between the source and target
� Enables tampering with the contents, ordering, timing etc.
� Good concept for attacks on specific Challenge-Response protocols
� Definitely applicable in TLS/SSL attacks when you can interpose
� Can even be effectively achieved without physical interposition if messages can be selectively denied (e.g. CANT-style
CAN bus attacks)
source target
rand()
f f
(・) (・)
=
49
Attacks on Protocols
Section Summary
� Attacks on protocols are more fruitful than attacks on building blocks
Supplicant Access
(client) point
� 4-way handshake
Nonce � A nonce
� Then another nonce with MAC
Nonce (& MAC) � Then a global key with MAC
� Then an ACK
Client starts using
Key (& MAC) � Grossly over-simplified on the left
(installs) session
key here.
Ack
Attacks on WPA2
� There is a MAC, implemented as a HMAC which is sent by supplicants and derived from the pre-shared
key
� Hash attacks to reverse this
� There are advantages to having observed multiple nonce & MAC -- so the attack starts with causing
the target to deauthorize from the Wi-Fi (repeatedly)
� hashcat can do the cracking, but not the de-auth
� airocrack-ng can do both
54
Attacks on WPA2 (cont’d)
Nonce
� There is a key reuse vulnerability in some client software, Nonce (& MAC)
dubbed KRACK
Key (& MAC)
� When the key is ‘installed’, the client resets its
communication counters Ack
� The spec of the formal proof did not include ”keys must be Key (& MAC)
‘installed’ once and only once”
Ack
55
https://www.krackattacks.com/ 0x00…00(& MAC)
Hands-On: 1 Minute Challenge
� Capture as many users of the Cybertruck Wi-Fi as you can in 1 minute.
I’m kidding – please don’t attack the Wi-Fi. I’m using it.
• KRACK is unnecessary – your systems all know the WPA2 password already (it is
a pre-shared key)
• How this would work :
• ’de-auth’ other clients so you could witness their handshake with the Access
Point.
• At which point you would have their session key and could decrypt all their
traffic.
56
Protocol: WPA2
Section Summary
� WPA2 Passwords can be cracked, indirectly, via the hashes exposed in the handshake
� The process is accelerated by capturing multiple 4-way handshakes, so the attack usually
also includes a flood of de-authenticating the clients
� WPA2 keys can be reinstalled (KRACK)
� Re-installing a key resets counters – this gives a key reuse attack
� Sometimes WPA2 keys can be nulled (KRACK)
� Then follow up with known-key attack (v. simple in this case)
58
Protocol: TLS / SSL
� Transport Layer Security (TLS). Was SSL, now that name is deprecated
� Used in HTTPS – but can be found without HTTP
connect
…
…
certificate
?
Certificates?
� Chains of Digital Signatures (asymmetric crypto)
� Recall: only the owner of the private part of a public key-pair can:
� decrypt traffic encrypted to the public key
� create a signature verifiable by anyone with the public key
?
?
Trust Store
in?
By Yanpas - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=46369922
61
Client Implementations of Server Authentication
“Type” Trust
62
� The proxies will work out-of-the-box on Type 1 and Type 2
Middleperson (aka MiTM) attacks
• Some require that you setup the proxy as a gateway -- some can work as a sibling (leveraging
ARP poisoning)
63
Certificate Substitution Attacks (on Type 2)
� Proxy creates two TLS connections
� Upstream, client connection to server – normal, valid, nothing to see here
� Downstream, served to client – supplies some other certificate
� Type 2 client sees ‘a cert’ and is happy
connect connect
… …
… …
‘certificate’ certificate
? 64
Trust Store Attacks (on Type 3)
� Can you add a root certificate authority to the trust store?
� If you have UI access to an Android device the answer is probably yes
� Can you use a compromised root certificate that is already in the trust store?
� There have been several compromised root certificates over the years (Komodo,
Symantec)
� If the devices is old enough, the compromised root certificate might be in its trust store
� Forge a server certificate signed with the secret from the compromise root; install that in
the proxy (e.g. mitmproxy, Burp, etc.)
� Getting the compromised secret is… the tricky part
Types 4-5 Attacks
� Recall: types 4-5 use certificate pining – they will only accept a connection from a server with a particular
expected public key
� If a different public key is supplied they abort connections
� If you have superuser privileges on the systems executing the type 4-5 code then there are simple ways to
replace the expected pub key or bypass the abort connection response:
� Patch the pubkey from the software
� Runtime hooking: e.g. Universal Android SSL Pinning Bypass with Frida
https://codeshare.frida.re/@pcipolloni/universal-android-ssl-pinning-bypass-with-frida/
� For Type 6: the runtime integrity checks will prevent most patches, hooks and exploits.
66
Other Attacks (on all types)
� SWEET32 – monitor long-lived Triple-DES and recover cookies
� DROWN – break confidentiality of some TLS (downgrade)
� Logjam – break confidentiality and integrity of some TLS (downgrade)
� POODLE – break confidentiality and integrity of some TLS (downgrade)
� There are even passive differential cryptanalysis attacks – working only at large-scale and long time periods
� Recover a RSA private key from a TLS Session with Perfect Forward Secrecy – Marco Ortisi
� Tools:
� mitmproxy, Burp, ZAP, MITMf
� poodle-PoC , Tim---/drown , drownAttackDemo
Heavy Vehicle Networks
Crash Course
69
1/3 Rapid Review: CAN
� Applications commonly pack signals into bitfield locations of a frame ‘type’ (Arbitration ID)
� Encoding time-varying signals by changing those bitfield contents over time
time
10-1000 Hz
0x7263 0x00ff0002000000
0x7263 0x00ff00fedc0000
Arb ID Data
0x7263 0x00ff00dcdc0000
� Simplify Wiring
� Enables multiple systems on one
bus
� Data sharing between ECUs
� External interface with 6 or 9-
pin connector
73
From CyberTruck Challenge June 2019 material by Dr. Jeremy Daily
Pinouts
74
From CyberTruck Challenge June 2019 material by Dr. Jeremy Daily
Message
Structure
time
10-1000 Hz
0x7263 0x00ff0002000000
0x7263 0x00ff00fedc0000
PGN Data
0x7263 0x00ff00dcdc0000
77
Protocol: UDS Seed-Key Exchange
78
UDS
� Unified Diagnostic Services – ISO 14229 ; on CAN: ISO 15765
� Used for nearly ALL vehicle Diagnostic Protocols
� You will learn a lot about it in other sessions today and tomorrow
� There are actions in UDS that are protected. To execute the action requires authorization: e.g.
� Read memory
� Reflash ECUs
� Perform potentially dangerous maintenance operations
� Aka ‘the fun stuff’
79
UDS Authorization
� Sometimes UDS is helpful; it will tell you that you need to authorize
� Negative Response Code : SecurityAccessDenied
� You’ll learn about these
Diag-
nostic
CAN ECU
SW
-- -- 02 27 05
rand()
-- -- 04 67 05 5E ED
f f
(・) (・)
-- -- 04 27 06 FF FF
=
81
-- -- 07 27 06 FF FF FF FF FF
Diag- CAN
nostic ECU
SW
-- -- 02 27 05
rand()
-- -- 04 67 05 5E ED
f f
(・ (・
) )
-- -- 04 27 06 FF FF
=
-- -- 07 27 06 FF FF FF FF FF
� J1939 IDs 0x18DA00F1 and 0x18DAF100 are used for UDS over J1939
82
15 Minute Hands-On: Derive the Seed-Key Routines
1 2 3
83
Aside: Attack Trees
84
Aside: Attack Tree Notation
� $$:
� Word Smart-Art
� Visio
� OmniGraffle
� Also purpose-built commercial products
� Free:
� Graphviz DOT
� Any indented text
� Any mind-mapping SW; e.g. Mindmup (at right)
mindmup-as-attack-trees:
(Free & MIT-licensed)
86
Recall: Attacking Seed-Key Exchange
� Attack Goals:
If you don’t:
• Then it is equivalent to pirating a session ; which we’ll expand on…
87
b) Pirating it…
Obtain Diag SW (3.0)
AND
4.0 LUT extraction
OR
4.1 RE algorithm and params from
Diag SW
AND
AND
4.2 Solve for unknowns in a know
formula
OR
4.3 Retry seeds until repeated from
capture
2.3 Brute-force
89
John Maag, NMFTA HVCS Nov 2017
Reverse Engineering the
Algorithm & Key from
Maintenance SW (4.1)
90
’Brute-Force’ (2.3)
� If you have no diagnostic SW, you can’t lift the correct
algorithm through LUT or RE.
� You can try every possible 16-bit value for each seed received.
91
‘Crypto’
92
Solve for Unknown Constants in a Known Algorithm (4.2)
� If you have diagnostic software for a related ECU
� and access to ~10 captures of seed-key exchange
93
https://www.enigmatos.com/2018/03/14/hacking-cars-with-z3-2/
Glitch Past (2.4)
� If you have no diagnostic SW…
� If you can setup low-jitter triggering based on key reply CAN frame
� You can try glitching the module at delays from that trigger.
� Maybe use simple power analysis to refine the delay
� Large search space; tricky setup (removing caps etc.)
Observable / 94
Trigger
Protocol: Seed-Key Exchange
Section Summary
� J1939 IDs 0x18DA00F1 and 0x18DAF100 are used for UDS over J1939
� SecurityAccess service is 0x27 / sub requestSeed: 0x05 sendKey: 0x06
96
Summary
� Crypto building blocks don’t get broken very often (given only the capture of comms)
� Crypto protocols get broken
� Crypto gets broken via side-channels
� Crypto gets broken by compromise of execution environment
98