郓城微信网站建设nginx 代理 wordpress

张小明 2026/1/15 21:45:53
郓城微信网站建设,nginx 代理 wordpress,江门市建设工程备案网站,怎么评价一个网站做的好否多态反序列化是处理继承结构对象序列化的常见需求#xff0c;但不同 JSON 序列化库的实现机制差异会带来显著的安全风险。微软 CA2326 规则明确警示#xff1a;避免使用非安全的 JsonSerializerSettings 配置#xff08;如 Newtonsoft.Json 的 TypeNameHandling 非 None 值但不同 JSON 序列化库的实现机制差异会带来显著的安全风险。微软 CA2326 规则明确警示避免使用非安全的 JsonSerializerSettings 配置如 Newtonsoft.Json 的 TypeNameHandling 非 None 值否则可能引发类型注入攻击。本文将对比 Newtonsoft.Json 与 System.Text.Json 在多态反序列化中的实现差异重点分析安全性问题并通过代码实例验证两者的安全表现。多态反序列化的实现机制差异Newtonsoft.Json基于TypeNameHandling 的灵活设计Newtonsoft.Json 通过 TypeNameHandling 配置项控制是否在 JSON 中嵌入类型元数据。当设置 TypeNameHandling 支持多态时JSON 会携带 $type 字段包含类型的完全限定名和程序集信息反序列化时直接根据该字段实例化对应类型。这种设计虽然灵活支持多态但缺乏默认的类型校验机制攻击者可构造包含恶意类型的 JSON触发敏感类型实例化。System.Text.Json多态配置的安全设计System.Text.Json 默认不支持多态反序列化需通过 [JsonDerivedType] 特性或 DerivedTypes 显式声明允许的派生类型。反序列化时仅处理配置过的类型拒绝未授权的类型注入从机制上规避了安全风险。CA2326 规则的警示CA2326 规则的核心是禁止使用 TypeNameHandling 非 None 值的配置 —— 攻击者可利用 $type 字段构造恶意 JSON实例化如 ProcessStartInfo执行系统命令、FileStream读写文件等敏感类型引发远程代码执行或数据泄露。using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Diagnostics; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; namespace NewtonsoftSecurityDemo { [JsonPolymorphic(TypeDiscriminatorPropertyName CustomerType)] [JsonDerivedType(typeof(PaymentCompletedEvent), PaymentCompletedEvent)] [JsonDerivedType(typeof(OrderCreatedEvent), OrderCreatedEvent)] public class TransactionEvent { public string EventId { get; set; } Guid.NewGuid().ToString(); public DateTime EventTime { get; set; } DateTime.Now; public string OrderId { get; set; } // 业务扩展字段攻击者利用的入口 public object ExtData { get; set; } } public class PaymentCompletedEvent : TransactionEvent { public decimal Amount { get; set; } public string PaymentMethod { get; set; } } public class OrderCreatedEvent : TransactionEvent { public string UserId { get; set; } public int ItemCount { get; set; } } // Newtonsoft.Json 安全绑定器演示白名单校验 public class EventSerializationBinder : ISerializationBinder { // 仅允许的安全类型白名单 private readonly HashSetstring _allowedTypes new() { NewtonsoftSecurityDemo.PaymentCompletedEvent, NewtonsoftSecurityDemo.OrderCreatedEvent, //System.Diagnostics.ProcessStartInfo }; public Type BindToType(string assemblyName, string typeName) { // 仅允许白名单内的类型 if (!_allowedTypes.Contains(typeName)) { throw new NotSupportedException($禁止反序列化未授权类型{typeName}); } return Type.GetType(${typeName}, {assemblyName}) ?? typeof(TransactionEvent); } public void BindToName(Type serializedType, out string? assemblyName, out string? typeName) { assemblyName serializedType.Assembly.FullName; typeName serializedType.FullName; } } class Program { static void Main(string[] args) { Console.WriteLine( Newtonsoft.Json 命令执行攻击演示 ); Newtonsoft_Attack_ProcessStartInfo(); Console.WriteLine(\n Newtonsoft.Json 文件读取攻击演示 ); Newtonsoft_Attack_FileStream(); Console.WriteLine(\n Newtonsoft.Json 启用 SerializationBinder安全防护演示 ); Newtonsoft_Secure_WithBinder(); Console.WriteLine(\n System.Text.Json 安全防护演示 ); SystemTextJson_Defense(); Console.ReadKey(); } /// summary /// 模拟注入ProcessStartInfo执行系统命令 /// /summary static void Newtonsoft_Attack_ProcessStartInfo() { string maliciousCallbackJson $ {{ $type: NewtonsoftSecurityDemo.PaymentCompletedEvent, NewtonsoftSecurityDemo, EventId: {Guid.NewGuid()}, OrderId: ORD_{new Random().Next(1000, 9999)}, Amount: 999.00, PaymentMethod: Alipay, ExtData: {{ $type: System.Diagnostics.ProcessStartInfo,System.Diagnostics.Process, FileName: cmd.exe, Arguments: /c echo some scripts C:\temp\attack_log.txt echo doing C:\temp\attack_log.txt, UseShellExecute: true }} }}; var settings new JsonSerializerSettings { TypeNameHandling TypeNameHandling.Auto, }; var eventData Newtonsoft.Json.JsonConvert.DeserializeObjectTransactionEvent(maliciousCallbackJson, settings); Console.WriteLine($处理订单事件{eventData.OrderId}); if (eventData.ExtData is ProcessStartInfo psi) { Directory.CreateDirectory(C:\temp); Process.Start(psi); Console.WriteLine($ [攻击成功] 执行命令{psi.Arguments}); Console.WriteLine($ [攻击结果] 生成文件C:\temp\attack_log.txt 文件内容); if (File.Exists(C:\temp\attack_log.txt)) { string content File.ReadAllText(C:\temp\attack_log.txt); Console.WriteLine(${content}); } } } /// summary /// 模拟注入FileInfo读取敏感文件 /// /summary static void Newtonsoft_Attack_FileStream() { string targetFile Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), appsettings.json); if (!File.Exists(targetFile)) { File.WriteAllText(targetFile, ConnectionString: 123456); } string maliciousExportJson $ {{ $type: NewtonsoftSecurityDemo.OrderCreatedEvent, NewtonsoftSecurityDemo, OrderId: ORD_{new Random().Next(1000, 9999)}, UserId: user_{new Random().Next(100, 999)}, ExtData: {{ $type: System.IO.FileInfo, FileName: {targetFile.Replace(\, \\)} }} }}; var settings new JsonSerializerSettings { TypeNameHandling TypeNameHandling.Auto }; var eventData Newtonsoft.Json.JsonConvert.DeserializeObjectTransactionEvent(maliciousExportJson, settings); Console.WriteLine($处理订单导出{eventData.OrderId}); // 通过FileInfo读取文件内容模拟攻击逻辑 if (eventData.ExtData is FileInfo fileInfo) { using (var sr new StreamReader(fileInfo.OpenRead())) { string sensitiveContent sr.ReadToEnd(); Console.WriteLine($ [攻击成功] 读取敏感文件内容\n{sensitiveContent}); } } } /// summary /// Newtonsoft.Json 启用SerializationBinder拦截恶意类型 /// /summary static void Newtonsoft_Secure_WithBinder() { string maliciousCallbackJson $ {{ $type: NewtonsoftSecurityDemo.PaymentCompletedEvent, NewtonsoftSecurityDemo, EventId: {Guid.NewGuid()}, OrderId: ORD_{new Random().Next(1000, 9999)}, Amount: 999.00, PaymentMethod: Alipay, ExtData: {{ $type: System.Diagnostics.ProcessStartInfo,System.Diagnostics.Process, FileName: cmd.exe, Arguments: /c echo some scripts C:\temp\attack_log.txt echo doing C:\temp\attack_log.txt, UseShellExecute: true }} }}; var settings new JsonSerializerSettings { TypeNameHandling TypeNameHandling.Auto, SerializationBinder new EventSerializationBinder() // 启用白名单校验 }; try { var eventData Newtonsoft.Json.JsonConvert.DeserializeObjectTransactionEvent(maliciousCallbackJson, settings); if (eventData.ExtData is ProcessStartInfo) { Console.WriteLine( [防护失效] 恶意类型未被拦截异常); } } catch (Exception ex) { Console.WriteLine($ [防护成功] 拦截未授权类型{ex.Message}); } } /// summary /// System.Text.Json 安全防护验证 /// /summary static void SystemTextJson_Defense() { string maliciousCallbackJson $ {{ CustomerType: PaymentCompletedEvent, EventId: {Guid.NewGuid()}, OrderId: ORD_{new Random().Next(1000, 9999)}, Amount: 999.00, PaymentMethod: Alipay, ExtData: {{ $type: System.Diagnostics.ProcessStartInfo,System.Diagnostics.Process, FileName: cmd.exe, Arguments: /c echo some scripts C:\temp\attack_log.txt echo doing C:\temp\attack_log.txt, UseShellExecute: true }} }}; var eventData System.Text.Json.JsonSerializer.DeserializeTransactionEvent(maliciousCallbackJson); Console.WriteLine($ 主对象类型{eventData.GetType().FullName}); Console.WriteLine($ ExtData 实际类型{eventData.ExtData.GetType().FullName}); if (eventData.ExtData is JsonElement) { Console.WriteLine( [防护成功] 恶意类型ProcessStartInfo被拦截ExtData仅保留原始JSON结构未反序列化为恶意对象); } else if (eventData.ExtData is ProcessStartInfo) { Console.WriteLine( [防护失效] 恶意类型解析成功); } else { Console.WriteLine($ [正常业务] 解析到合法类型{eventData.ExtData.GetType().FullName}); } Console.WriteLine(\n尝试转换ExtData为ProcessStartInfo); try { var psi (ProcessStartInfo)eventData.ExtData; Console.WriteLine( [防护失效] 恶意类型解析成功); } catch (InvalidCastException ex) { Console.WriteLine($ [防护成功] 强制转换失败原因{ex.Message}); } } } }运行结果为通过 Demo 可以发现- Newtonsoft 无防护时攻击成功- Newtonsoft 启用 SerializationBinder 后拦截了恶意类型- System.Text.Json 始终拦截恶意类型ExtData 为 JsonElement无法转换为 ProcessStartInfo。为什么 Newtonsoft.Json 启用 SerializationBinder 可降低风险先看代码public class EventSerializationBinder : ISerializationBinder { // 仅允许的安全类型白名单 private readonly HashSetstring _allowedTypes new() { NewtonsoftSecurityDemo.PaymentCompletedEvent, NewtonsoftSecurityDemo.OrderCreatedEvent, //System.Diagnostics.ProcessStartInfo }; public Type BindToType(string assemblyName, string typeName) { // 仅允许白名单内的类型 if (!_allowedTypes.Contains(typeName)) { throw new NotSupportedException($禁止反序列化未授权类型{typeName}); } return Type.GetType(${typeName}, {assemblyName}) ?? typeof(TransactionEvent); } public void BindToName(Type serializedType, out string? assemblyName, out string? typeName) { assemblyName serializedType.Assembly.FullName; typeName serializedType.FullName; } }SerializationBinder 的核心作用是接管从 JSON 中的 $type 字符串 到实际 Type 类型的映射过程强制校验类型合法性。简单说- 无 SerializationBinder反序列化器会无条件反射创建 $type 指定的任意类型包括危险类型- 有 SerializationBinder反序列化器必须经过你的自定义校验逻辑仅允许白名单内的类型被实例化直接阻断恶意类型的创建。小结Newtonsoft.Json 的 TypeNameHandling 机制虽灵活但易被利用触发安全漏洞System.Text.Json 通过显式多态配置白名单的设计规避了类型注入风险。在实际开发中针对多态场景建议优先使用 System.Text.Json。若必须使用 Newtonsoft.Json需遵循以下安全实践- 避免使用 TypeNameHandling 非 None 值。- 若必须启用需严格校验 $type 字段类型的合法性仅允许安全类型。我希望您喜欢这篇文章并一如既往地感谢您阅读并与朋友和同事分享我的文章。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

手机网站网站开发流程做一个网站难不难

reStructuredText适配:满足Python Sphinx文档系统的图像需求 在技术文档日益成为软件工程核心资产的今天,仅仅写出清晰的文字已远远不够。越来越多的项目开始重视视觉内容的质量——尤其是那些承载历史记忆的老照片、系统架构图或模型输出结果。当这些图…

张小明 2026/1/8 7:13:39 网站建设

口碑最好的旅游网站阻止wordpress更新

以下是2025年热门AI论文工具的快速对比,帮助您在写作论文时选择合适工具。这六款工具均支持LaTeX模板和论文格式规范,适配性强。总结基于核心功能和独特优势:工具名称核心功能主要优势aibiye深度文本改写与内容优化智能保持原逻辑&#xff0c…

张小明 2026/1/8 20:54:15 网站建设

青岛做网站哪家做的好织梦可以做英文网站吗

三十二. 委托调用delegatecall 1.delegatecall作用: ​ delegatecall在B合约调用A合约的函数,A合约的函数逻辑产生结果,赋值到B合约状态变量上 2.delegatecall使用案例: pragma solidity ^0.8.7;contract A {uint256 public num;uint256 public str;address public adr…

张小明 2026/1/8 8:10:56 网站建设

广东品牌网站建设报价表网站怎么推广运营

基于数模融合的复合多阶段退化模型3D威亚剩余寿命预测研究 本文针对3D威亚系统中伺服电机退化引发的安全性问题,提出一种基于数据与模型融合的剩余寿命预测方法。通过核主成分分析对多源退化数据降维,构建复合退化量以解决变量间高相关性;采用非线性Wiener过程建立多阶段退…

张小明 2026/1/8 20:54:10 网站建设

企业网站制作找什么人建好网站后如何向里面加东西

目录 一、冒泡排序 1.介绍 2.使用模板 3.示例 4.注意事项 二、选择排序 1.介绍 2.使用模板 3.示例 4.注意事项 三、快速排序 1.介绍 2.使用模板 3.示例 4.注意事项 四、C#内置的排序方法 1.介绍 2.使用模板 3.注意事项 五、简单总结 一、冒泡排序 1.介绍 …

张小明 2026/1/8 20:54:08 网站建设