简体   繁体   中英

creating / deleting sections using the PeNet library

I am using the 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 , but nothing works. (No errors appear, but no files are written/overwritten.)

Using a C # console application, here's the code:

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);

From this GitHub issue about a similar topic:

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.

Unfortunately, the code offered there is incorrect. PEFile has no Buff property. So use:

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

Sadly, it seems the author never implemented the Save method either...

Further Reading from the docs:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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