flawopen.com/असुरक्षित deserialization/.NET BinaryFormatter सुरक्षा
.NET का BinaryFormatter सुरक्षित है?
Microsoft कहता है कि BinaryFormatter असुरक्षित है और इसे सुरक्षित नहीं बनाया जा सकता। डेटा विश्वसनीय लगने पर भी इसका उपयोग बंद करने की सलाह है। .NET 9 में API प्रकार मौजूद रहते हैं, लेकिन अंतर्निहित implementation हमेशा PlatformNotSupportedException देता है। पुराने runtimes और unsupported compatibility package में वही जोखिम बना रहता है।
// Trust boundary पार करने वाले डेटा को deserialize न करें। using System.IO; using System.Runtime.Serialization.Formatters.Binary; public static class LegacyReader { public static object Deserialize(Stream input) => new BinaryFormatter().Deserialize(input); } // इन unrestricted polymorphic serializers को भी हटाएँ: SoapFormatter NetDataContractSerializer LosFormatter ObjectStateFormatter // ASP.NET ViewState के अंदर उपयोग किया जाता है // SerializationBinder इन्हें सुरक्षित नहीं बनाता।
// डेटा का आकार application code तय करता है। // Nullable reference types make the DTO contract explicit. #nullable enable using System; using System.IO; using System.Text.Json; public sealed record MyDto(int Id, string Name); public static class DtoReader { public static MyDto Parse(ReadOnlySpan<byte> json) { // Input size सीमित करें और application नियम validate करें। if (json.Length > 65_536) throw new InvalidDataException(); MyDto? value = JsonSerializer.Deserialize<MyDto>(json); if (value is null || value.Id <= 0 || string.IsNullOrWhiteSpace(value.Name) || value.Name.Length > 100) throw new InvalidDataException(); return value; } } // पुराने NRBF files के records NrbfDecoder से देखें // और केवल अनुमत values को DTO में map करें। // Arbitrary runtime types दोबारा न बनाएँ।
BinaryFormatter खतरनाक क्यों है
BinaryFormatter.Deserialize stream से object graph बनाता है और हमलावर-नियंत्रित डेटा को execution flow प्रभावित करने दे सकता है। Microsoft सेवा-अस्वीकृति, जानकारी उजागर होने और दूरस्थ code execution के जोखिम बताता है। मूल समस्या unrestricted polymorphic deserialization है: serialized डेटा यह प्रभावित कर सकता है कि कौन-से प्रकार और object state संसाधित हों। कोई formatter switch इसे अविश्वसनीय bytes के सुरक्षित parser में नहीं बदलता।
जोखिम केवल सार्वजनिक HTTP endpoints तक सीमित नहीं है। फ़ाइलें, cache, queues, database records, desktop-client संदेश और cloud-sync state बाद में हमलावर के नियंत्रण में आ सकते हैं। Bytes के सिस्टम में आने और आगे जाने का रास्ता खोजें; “हमने लिखा” या “यह internal है” स्थायी trust label नहीं है।
SerializationBinder पर्याप्त क्यों नहीं
Custom SerializationBinder resolve होने वाले types सीमित कर सकता है, लेकिन Microsoft स्पष्ट कहता है कि binder या अन्य configuration switch DoS नहीं रोक सकते और अन्य हमलों के लिए भी अपर्याप्त हो सकते हैं। Binder migration का विकल्प नहीं है; हटाने तक अविश्वसनीय input स्वीकार न करें।
पुराने उपयोग और संबंधित रास्ते खोजें
Source code, project references और runtime configuration में BinaryFormatter, IFormatter, SoapFormatter, NetDataContractSerializer, LosFormatter और ObjectStateFormatter खोजें। Session providers, cache adapters, queue consumers, file import/export और पुराने ASP.NET Web Forms paths की जाँच करें। ViewState आम तौर पर message authentication code (MAC) से सुरक्षित होता है; इसे चालू रखें और machine key सुरक्षित रखें। MAC सामान्य BinaryFormatter deserialization को सुरक्षित नहीं बनाता।
डेटा प्रारूप और runtime के अनुसार माइग्रेट करें
नए डेटा के लिए आवश्यकता के अनुसार System.Text.Json, DataContractSerializer, MessagePack या protobuf-net जैसा स्पष्ट contract चुनें; कोई सीधा replacement नहीं है। पुराने NRBF files रखने हों तो .NET 9 का NrbfDecoder encoded application types बनाए बिना records decode कर सकता है। संरचना validate करें और केवल अपेक्षित values को DTO में map करें। WinForms/WPF resources, clipboard और drag-and-drop के migration निर्देश अलग से देखें।
अक्सर पूछे जाने वाले प्रश्न
Microsoft विश्वसनीय माने गए डेटा के लिए भी migration की सलाह देता है, क्योंकि storage, cache, files, sync और downstream सेवाएँ trust boundary पार कर सकती हैं। हर स्रोत में कौन लिख सकता है, जाँचें और धारणा पर निर्भर रहने के बजाय BinaryFormatter हटाएँ।
.NET 9 में API types रहते हैं, लेकिन पुराना compatibility switch हो या नहीं, in-box implementation हमेशा PlatformNotSupportedException देता है। Upgrade से पहले उपयोग खोजकर migrate करें। Unsupported compatibility package vulnerable implementation लौटाता है; यह सुरक्षा सुधार नहीं है।
Json.NET का default TypeNameHandling मान None है, इसलिए payload metadata मनमाने CLR types नहीं चुनता। Untrusted input के लिए अन्य मान सक्षम न करें। Polymorphism जरूरी हो तो application-controlled सख्त allowlist और library guidance अपनाएँ। क्या TypeNameHandling सुरक्षित है? देखें।
स्रोत और संदर्भ
- Microsoft Learn — BinaryFormatter security guide
- Microsoft Learn — BinaryFormatter migration guide
- Microsoft Learn — Choose a BinaryFormatter replacement
- Microsoft Learn — Safely read NRBF payloads with NrbfDecoder
- Microsoft Learn — SYSLIB0011
- Microsoft Learn — ASP.NET ViewState MAC
- CWE-502 — Deserialization of Untrusted Data
- Is Json.NET TypeNameHandling safe?