-
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 protection against the new WindowsIdentity gadget:
- Loading branch information
Showing
13 changed files
with
203 additions
and
27 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
Large diffs are not rendered by default.
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
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
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,58 @@ | ||
namespace SafeDeserializationHelpers | ||
{ | ||
using System; | ||
using System.Data; | ||
using System.Runtime.Serialization; | ||
using System.Security.Permissions; | ||
using System.Security.Principal; | ||
|
||
/// <summary> | ||
/// Safe surrogate selector provides surrogates for DataSet and WindowsIdentity classes. | ||
/// </summary> | ||
internal sealed class SafeSurrogateSelector : ISurrogateSelector | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SafeSurrogateSelector"/> class. | ||
/// </summary> | ||
/// <param name="nextSelector">Next <see cref="ISurrogateSelector"/>, optional.</param> | ||
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] | ||
public SafeSurrogateSelector(ISurrogateSelector nextSelector = null) | ||
{ | ||
if (nextSelector != null) | ||
{ | ||
SurrogateSelector.ChainSelector(nextSelector); | ||
} | ||
|
||
// register known surrogates for all streaming contexts | ||
var ctx = new StreamingContext(StreamingContextStates.All); | ||
SurrogateSelector.AddSurrogate(typeof(DataSet), ctx, new DataSetSurrogate()); | ||
SurrogateSelector.AddSurrogate(typeof(WindowsIdentity), ctx, new WindowsIdentitySurrogate()); | ||
} | ||
|
||
private SurrogateSelector SurrogateSelector { get; } = new SurrogateSelector(); | ||
|
||
/// <inheritdoc cref="ISurrogateSelector" /> | ||
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] | ||
public void ChainSelector(ISurrogateSelector selector) | ||
{ | ||
if (selector != null) | ||
{ | ||
SurrogateSelector.ChainSelector(selector); | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="ISurrogateSelector" /> | ||
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] | ||
public ISurrogateSelector GetNextSelector() | ||
{ | ||
return SurrogateSelector.GetNextSelector(); | ||
} | ||
|
||
/// <inheritdoc cref="ISurrogateSelector" /> | ||
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] | ||
public ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector) | ||
{ | ||
return SurrogateSelector.GetSurrogate(type, context, out selector); | ||
} | ||
} | ||
} |
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,72 @@ | ||
namespace SafeDeserializationHelpers | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Security.Permissions; | ||
using System.Security.Principal; | ||
|
||
/// <summary> | ||
/// Deserialization surrogate for the WindowsIdentity class. | ||
/// </summary> | ||
internal class WindowsIdentitySurrogate : ISerializationSurrogate | ||
{ | ||
private static ConstructorInfo Constructor { get; } = typeof(WindowsIdentity).GetConstructor( | ||
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, | ||
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 ISerializable; | ||
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 result = Constructor.Invoke(new object[] { info, context }); | ||
return result; | ||
} | ||
|
||
private void Validate(SerializationInfo info, StreamingContext context) | ||
{ | ||
// check the serialized data using a guarded BinaryFormatter | ||
var fmt = new BinaryFormatter().Safe(); | ||
|
||
var e = info.GetEnumerator(); | ||
while (e.MoveNext()) | ||
{ | ||
switch (e.Name) | ||
{ | ||
case "System.Security.ClaimsIdentity.actor": | ||
case "System.Security.ClaimsIdentity.claims": | ||
case "System.Security.ClaimsIdentity.bootstrapContext": | ||
var base64 = info.GetString(e.Name); | ||
if (string.IsNullOrEmpty(base64)) | ||
{ | ||
continue; | ||
} | ||
|
||
// safe BinaryFormatter will throw on malicious payload | ||
var buffer = Convert.FromBase64String(base64); | ||
using (var ms = new MemoryStream(buffer)) | ||
{ | ||
fmt.Deserialize(ms); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |