-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DataSetSurrogate to guard against the DataSet attacks.
Improved the API: new BinaryFormatter().Safe().
- Loading branch information
Showing
10 changed files
with
148 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace SafeDeserializationHelpers | ||
{ | ||
using System.Data; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
|
||
/// <summary> | ||
/// Extension methods for the | ||
/// </summary> | ||
public static class BinaryFormatterExtensions | ||
{ | ||
/// <summary> | ||
/// Makes the <see cref="BinaryFormatter"/> safe. | ||
/// </summary> | ||
/// <param name="fmt">The <see cref="BinaryFormatter"/> to guard.</param> | ||
/// <returns>The safe version of the <see cref="BinaryFormatter"/>.</returns> | ||
public static BinaryFormatter Safe(this BinaryFormatter fmt) | ||
{ | ||
// safe type binder prevents delegate deserialization attacks | ||
var binder = new SafeSerializationBinder(fmt.Binder); | ||
fmt.Binder = binder; | ||
|
||
// DataSet surrogate validates binary-serialized datasets | ||
var ss = new SurrogateSelector(); | ||
ss.AddSurrogate(typeof(DataSet), new StreamingContext(StreamingContextStates.All), new DataSetSurrogate()); | ||
fmt.SurrogateSelector = ss; | ||
|
||
// TODO: do we need to chain surrogate selectors? | ||
return fmt; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
namespace SafeDeserializationHelpers | ||
{ | ||
using System.Data; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Security.Permissions; | ||
|
||
/// <summary> | ||
/// Deserialization surrogate for the DataSet class. | ||
/// </summary> | ||
public class DataSetSurrogate : ISerializationSurrogate | ||
{ | ||
private static ConstructorInfo Constructor { get; } = typeof(DataSet).GetConstructor( | ||
BindingFlags.Instance | BindingFlags.NonPublic, | ||
null, | ||
new[] { typeof(SerializationInfo), typeof(StreamingContext) }, | ||
null); | ||
|
||
/// <inheritdoc cref="ISerializationSurrogate" /> | ||
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] | ||
public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) | ||
{ | ||
var ds = obj as DataSet; | ||
ds.GetObjectData(info, context); | ||
} | ||
|
||
/// <inheritdoc cref="ISerializationSurrogate" /> | ||
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] | ||
public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) | ||
{ | ||
Validate(info, context); | ||
|
||
// discard obj | ||
var ds = Constructor.Invoke(new object[] { info, context }); | ||
return ds; | ||
} | ||
|
||
private void Validate(SerializationInfo info, StreamingContext context) | ||
{ | ||
var remotingFormat = SerializationFormat.Xml; | ||
var schemaSerializationMode = SchemaSerializationMode.IncludeSchema; | ||
|
||
var e = info.GetEnumerator(); | ||
while (e.MoveNext()) | ||
{ | ||
switch (e.Name) | ||
{ | ||
case "DataSet.RemotingFormat": // DataSet.RemotingFormat does not exist in V1/V1.1 versions | ||
remotingFormat = (SerializationFormat)e.Value; | ||
break; | ||
|
||
case "SchemaSerializationMode.DataSet": // SchemaSerializationMode.DataSet does not exist in V1/V1.1 versions | ||
schemaSerializationMode = (SchemaSerializationMode)e.Value; | ||
break; | ||
} | ||
} | ||
|
||
// XML dataset serialization isn't known to be vulnerable | ||
if (remotingFormat == SerializationFormat.Xml) | ||
{ | ||
return; | ||
} | ||
|
||
// binary dataset serialization should be double-checked | ||
var tableCount = info.GetInt32("DataSet.Tables.Count"); | ||
for (int i = 0; i < tableCount; i++) | ||
{ | ||
var key = $"DataSet.Tables_{i}"; | ||
var buffer = info.GetValue(key, typeof(byte[])) as byte[]; | ||
|
||
// check the serialized data table using a guarded BinaryFormatter | ||
var fmt = new BinaryFormatter(null, new StreamingContext(context.State, false)).Safe(); | ||
using (var ms = new MemoryStream(buffer)) | ||
{ | ||
var dt = fmt.Deserialize(ms); | ||
if (dt is DataTable) | ||
{ | ||
continue; | ||
} | ||
|
||
// the deserialized data doesn't appear to be a data table | ||
throw new UnsafeDeserializationException("Serialized DataSet probably includes malicious data."); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters