secana / PeNet

Portable Executable (PE) library written in .Net
Apache License 2.0
592 stars 113 forks source link

Unable to add a Import using the AddImport method #234

Open skul377 opened 2 years ago

skul377 commented 2 years ago

I tried using the stock templat as given below to add an import to a file. However, I am not able to see the import added to the binary. Please let me know where I am going wrong.

            PeNet.PeFile peFile = new PeNet.PeFile(@"D:\Sample\7z.exe");
            peFile.AddImport("gdi32.dll", "StartPage");

I imported the package via NuGet and compiled the code. The code exits as expected but doesn't provide the expected results.

superewald commented 2 years ago

Had the same issue and worked around it by saving the rawFile buffer to file (it seems like AddImport doesn't apply changes to the binary).

So in your case do this:

// add import
PeNet.PeFile peFile = new PeNet.PeFile(@"D:\Sample\7z.exe");
peFile.AddImport("gdi32.dll", "StartPage");
// save binary to disk
File.WriteAllBytes(@"D:\Sample\7z.exe", peFile.rawFile.ToArray());