tags:

views:

310

answers:

2

So. Let's say I were to make a hex editor to edit... oh... let's say a .DLL file. How can I edit a .DLL file's hex by using C# or C++? And for the "fixed part", I want to make it so that I can browse from the program for a specific .DLL, have some pre-coded buttons on the programmed file, and when the button is pressed, it will automatically execute the requested action, meaning the button has been pre-coded to know what to look for in the .DLL and what to change it to. Can anyone help me get started on this?

Also, preferably C#. Thank you!

A: 

If the idea is to load a hex edit box you can use the following: Be.HexEditor Editing a file's "hex" is nothing more than changing bytes in it. The part of having pre-programmed changes is going to be that more general type. But for actually viewing, finding and then having the option of changing anything you want, Be.HexEditor is a good option. I used it over a year ago, I would hope that it has some new features that will make your life easier.

The program seems nice, but I do not see how I will be able to use this for my idea. :\
Kevin
Its like a plugin that provides you with a widget which is a hex editor box. Like I said, Editing a file's "hex" is nothing more than changing the bytes it. If you want to just do that and not have an actual editor (look up "hex editor" in google and see what you get) you will need things like the C# classes, FileStream, BinaryReader and BinaryWriter.
That's what I want. Calling functions from the program to change the bytes of a file.
Kevin
Do you know how to use OpenFileDialog in C#?
Yes, I know how to use it, but how would that help?
Kevin
You can start out getting that ready. since you'll want that in your program. Next things would be to open the file selected with OpenFileDialog with FileStream.
With FileStream? I've got no idea what that's supposed to mean. :x
Kevin
+2  A: 

The basics are very simple.

A DLL, or any file, is a stream of bytes.

Basic file operations allow you to read and write arbitrary portions of a file. The term of art is basically "Random Access Files Operations".

In C, the fundamental operations are read(), write(), and lseek().

read allows you to read a stream of bytes in to a buffer, write allows you to write a buffers of bytes to a file, lseek allows you to position anywhere you want in the file.

Example:

int fd = open("test.dat", O_RDWR);
off_t offset = lseek(fd, 200, SEEK_SET);
if (off_t == -1) {
    printf("Boom!\n");
    exit(1);
}    
char buf[1024];
ssize_t bytes_read = read(fd, buf, 1024);
offset = lseek(fd, 100, SEEK_SET);
ssize_t bytes_written = write(fd, buf, 1024);
flush(fd);
close(fd);

This reads 1024 bytes from a file, starting at the 200th byte of the file, then writes it back to the file at 100 bytes.

Once you can change random bytes in a file, it's a matter of choosing what bytes to change, how to change them, and doing the appropriate reads/lseeks/writes to make the changes.

Note, those are the most primitive I/O operations, there are likely much better ones you can use depending on your language etc. But they're all based on those primitives.

Interpreting the bytes of a file, displaying them, etc. That's an exercise for the reader. But those basic I/O capabilities give you the fundamentals of changing files.

Will Hartung