2016-09-19 97 views
5

this链接,在备注部分提到“TypeNameHandling should be used with caution when your application deserializes JSON from an external source. Incoming types should be validated with a custom SerializationBinder when deserializing with a value other than TypeNameHandling.None.”。如果使用TypeNameHandling.All进行序列化/反序列化,那么外部源的JSON会有什么危害?一个工作示例将不胜感激。TypeNameHandling在Newtonsoft Json中谨慎

回答

10

当使用TypeNameHandling.All反序列化并且没有Seri​​alizationBinder检查时,json.net将尝试创建一个类型的实例,该类型作为JSON中的元数据。

public class Car 
{ 
    public string Maker { get; set; } 
    public string Model { get; set; } 
} 

{ 
    "$type": "Car", 
    "Maker": "Ford", 
    "Model": "Explorer" 
} //create a Car and set property values 

但攻击者可以发送您危险的类型存在于你的代码或框架。

即从hereSystem.CodeDom.Compiler.TempFileCollection是一个可序列化的类,其目的是维护由编译过程产生的临时文件列表,并在不再需要时删除它们。为了确保文件被删除,类实现了一个终结器,当垃圾收集器正在清理对象时将会调用这个终结器。攻击者可以构建这个类的序列化版本,将其内部文件集合指向受害者系统上的任何文件。这将在反序列化之后的某个点被删除,而没有任何来自反序列化应用程序的交互。

[Serializable] 
    public class TempFileCollection 
    { 
     private Hashtable files; 
     // Other stuff... 

     ~TempFileCollection() 
     { 
     if (KeepFiles) {return} 
     foreach (string file in files.Keys) 
     { 
      File.Delete(file); 
     } 
     } 
    } 

    { 
     "$type": "System.CodeDom.Compiler.TempFileCollection", 
     "BasePath": "%SYSTEMDRIVE", 
     "KeepFiles": "False", 
     "TempDir": "%SYSTEMROOT%" 
    } // or something like this, I just guessing but you got the idea 
+0

如需更多信息,请参见[如何配置Json.NET创建一个漏洞的Web API(https://www.alphabot.com/security/blog/2017/net/How-to-configure-Json .NET到创造 - 一个脆弱的web-API.html)。 – dbc

+0

还有AlvaroMuñoz&Oleksandr Mirosh的黑帽纸https://www.blackhat.com/docs/us-17/thursday/us-17-Munoz-Friday-The-13th-JSON-Attacks-wp.pdf – dbc

0

一些额外的攻击工具已经在阿尔瓦罗·穆尼奥斯&阿列克Mirosh的黑帽纸https://www.blackhat.com/docs/us-17/thursday/us-17-Munoz-Friday-The-13th-JSON-Attacks-wp.pdf被确定。它们是:

  • System.Configuration.Install.AssemblyInstaller - 攻击矢量:在装配负载上执行有效负载。

  • System.Activities.Presentation.WorkflowDesigner - 攻击向量:在解析Xaml有效载荷时执行静态方法。攻击向量:攻击者向受控服务器发送带有URL的有效载荷,该服务器响应Xaml有效载荷和目标服务器在解析Xaml有效载荷期间执行期望的静态方法。 1)调用任何解组对象的方法; 2)调用任何解组对象的方法; 2)调用任何解组对象的方法; 2)调用任何解组对象的方法; 2)调用任何解组对象的方法; 2)我们可以用受控参数调用所需类型的参数化构造函数; 3)调用任何公共方法,包括具有受控参数的静态方法。

  • System.Windows.Forms.BindingSource - 攻击矢量:任意的getter调用。

  • Microsoft.Exchange.Management.SystemManager.WinForms.ExchangeSettingsProvider - 攻击矢量:它允许从setter跳转到嵌套的BinaryFormatter反序列化。