使用 PeNet 库创建/删除部分

creating / deleting sections using the PeNet library

我正在使用 PeNet nuget package to work with a pe file, there was a problem when creating or deleting a section, I take the code from the author's example,但没有任何效果。 (没有出现错误,但没有文件 written/overwritten。)

使用 C# 控制台应用程序,代码如下:

var bin = File.ReadAllBytes(@"C:\Users\PC\Desktop\test.dll");
var peFile = new PeNet.PeFile(bin);

peFile.AddSection(".newSec", 100, (ScnCharacteristicsType)0x40000040);
peFile.RemoveSection(".rsrc", true);

关于类似主题的GitHub issue

PeNet works internally by loading the whole PE file into a buffer. All changes you make are done to this buffer in memory. If you want to save your changes, you just have to save the buffer.

很遗憾,那里提供的代码不正确。 PEFile 没有 Buff 属性。所以使用:

System.IO.File.WriteAllBytes(
    "C:\Users\PC\Desktop\newDLL.dll", 
    peFile.RawFile.ToArray()
);

可悲的是,作者似乎也没有实现 Save 方法...

进一步阅读文档: