简体   繁体   English

C#如何将序列化的.NET对象编译为.NET程序集

[英]C# How to compile a serialized .NET object to a .NET Assembly

I have a binary file, with serialized .NET object (stream) in it. 我有一个二进制文件,其中带有序列化的.NET对象(流)。 I need to compile it back to a .NET Assembly (Maybe using CodeDomProvider Class or anything else). 我需要将其编译回.NET程序集(也许使用CodeDomProvider类或其他方法)。

Any pointer will be highly appreciated. 任何指针将不胜感激。

Thanks in Advance. 提前致谢。

There is no guarantee that it is possible to deserialize a BinaryFormatter serialized object ( BinaryFormatter is the .NET-included binary serializer... and it is considered to be quite "evil") to the source code that generated it. 不能保证可以反序列化BinaryFormatter序列化的对象( BinaryFormatter是包含.NET的二进制序列化器,并且被认为是相当“邪恶”)到生成它的源代码。 Simple example: 简单的例子:

[Serializable]
public class MyClass
{
    public DateTime Foo { get; private set; }

    public MyClass()
    {
        Foo = DateTime.Now;
    }
}

There is no way in C# to write a MyClass object with a specific Foo value unless you are using reflection. 除非您使用反射,否则C#中无法用特定的Foo值编写MyClass对象。 You can't write: 你不能写:

var bar = new MyClass { Foo = new DateTime(2018, 1, 1 }

because there is no setter for Foo . 因为Foo没有二传手。

Specific cases (where there a no private fields and if there are setters they are all setters that only set the value of the backing field without doing extra elaboration) can be converted to C# assignments. 可以将特定情况(其中没有私有字段并且如果有设置程序的情况下,它们都是只设置后备字段的值而不进行额外说明的所有设置程序)可以转换为C#分配。

What it is possible to do (but in general it is a bad idea with BinaryFormatter , because it doesn't handle very well changes to the underlying types) is include the binary file as an embedded resource (see this ) and then read the file: 可以怎么做(但通常是一个坏主意用BinaryFormatter ,因为它没有很好地处理变化的基本类型)是包括二进制文件作为嵌入资源(见 ),然后读取文件:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "ConsoleApp2.Folder1.File1.bin";

MyClass mc;

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
    var bf = new BinaryFormatter();
    mc = (MyClass)bf.Deserialize(stream);
}

Note that this is a very very bad idea, because if anything changes in the underlying types (even some private fields), everything will break badly. 请注意,这是一个非常非常糟糕的主意,因为如果基础类型(甚至某些私有字段)发生任何更改,则所有内容都会严重中断。

I've done it using a tool called ClrGuard. 我已经使用名为ClrGuard的工具完成了此操作。 https://github.com/endgameinc/ClrGuard . https://github.com/endgameinc/ClrGuard It will capture the .NET assembly as it tries to execute and dump it in disk. 它将在尝试执行.NET程序集并将其转储到磁盘时捕获该程序集。 Then we can load with ilspy or any other .NET de-compiler. 然后,我们可以使用ilspy或任何其他.NET反编译器进行加载。

ClrGuard will hook into all .NET processes on the system. ClrGuard将挂钩到系统上的所有.NET进程。 From there, it performs an in-line hook of the native LoadImage() function. 从那里开始,它执行本机LoadImage()函数的内联挂钩。 This is what Assembly.Load() calls under the CLR hood. 这就是Assembly.Load()在CLR幕后所调用的。

Reference : https://www.endgame.com/blog/technical-blog/hunting-memory-net-attacks 参考: https : //www.endgame.com/blog/technical-blog/hunting-memory-net-attacks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM