逆向.net程序,碰到了BinaryFormatter ,这个类对对象序列化是C#自身特有的逻辑的,python很难还原BinaryFormatter的序列化逻辑,除非对BinaryFormatter底层逻辑非常了解。于是需要在python中通过pythonnet调用C# DLL对数据对象进行序列化,但是BinaryFormatter不仅对语言不友好,同时在.net版本间的兼容性极差,以下代码序列化代码为例:
var dicList = new Dictionary<string, IList<Dictionary<string, object>>>();
dicList.Add("1", new List<Dictionary<string, object>>() { new Dictionary<string, object>() { { "11", "22" } } });
using (MemoryStream memoryStream = new MemoryStream())
{
using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
using (BufferedStream bufferedStream = new BufferedStream(gzipStream))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(bufferedStream, dicList);
}
}
returnData = memoryStream.ToArray();
}
其中NetCore3.1及以上版本对其序列化的程序集信息为:
System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Collections.Generic.IList`1[[System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
System.Collections.Generic.GenericEqualityComparer`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Collections.Generic.IList`1[[System.Collec