Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving a modified PE File #34

Closed
BigJk opened this issue Nov 11, 2017 · 3 comments
Closed

Saving a modified PE File #34

BigJk opened this issue Nov 11, 2017 · 3 comments
Projects

Comments

@BigJk
Copy link

BigJk commented Nov 11, 2017

First of all, PeNet is awesome. I'm just missing the ability to write modified headers / file back to disk. Is this feature really not implemented or did I miss something? In case its really missing is there a chance to see it implemented in the future?

@secana
Copy link
Owner

secana commented Nov 19, 2017

Hi @BigJk,
you are right, the feature is missing. I've always wanted to add it but currently I do not have the time. You can try the following, maybe it's enough for you use case:

using System;

namespace PeSave
{
    class Program
    {
        static void Main(string[] args)
        {
            var peFile = new PeNet.PeFile(@"c:\windows\system32\kernel32.dll");

            // do your changes here on the peFile object
            peFile.ImageNtHeaders.FileHeader.Machine = 0x111;
            peFile.ImageNtHeaders.OptionalHeader.BaseOfCode = 0x2222;

            // save the changed PE file to disk
            System.IO.File.WriteAllBytes("myChangedPeFile", peFile.Buff);
        }
    }
}

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.

I hope that helps.

Regards,
Secana

@BigJk
Copy link
Author

BigJk commented Nov 21, 2017

@secana Thanks for the answer! I expected the Buff variable to just hold the original version so I didn't think about that. I will check if it works.

@secana
Copy link
Owner

secana commented Nov 26, 2017

Note to self: Add a "save" function in PeNet

@secana secana added this to New in PeNet Dec 13, 2017
@secana secana closed this as completed in 8f84436 May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

2 participants