Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Trying to simply relocate...

Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
Code:
ofd.ShowDialog();
            BinaryReader br = new BinaryReader(File.Open(ofd.FileName, FileMode.Open, FileAccess.ReadWrite));
            //FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None
            string passwordValues = null;
            for (int i = 0x00000034; i <= 0x000000054; i++)
            {
                br.BaseStream.Position = i;
                passwordValues += br.ReadByte().ToString("X2");
               //Beginning hex 0x00000034 Ending hex 0x000000054
            }
            MessageBox.Show(passwordValues);
            br.Close();
            if (passwordValues.StartsWith("00"))
            {
                MessageBox.Show("This account is blocked still.");
                passwordValues = passwordValues.Remove(0, 2);
            }
            else
            {
                //Console.WriteLine("This account is not blocked.");
                //MessageBox.Show(passwordValues);
                MessageBox.Show("This account is not blocked.");
            }

All I simply want to do is to strip the first 00 from the beginning of the binary and relocate it to the end of the binary and write it back to the file. This should not be too hard to figure out but, I am having difficulty with it. This is being done in C#. Any help would be appreciated. Thank you.
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
There are several misconceptions in your code snippet:

  • Reading and writing files are two separate processes. If you want to alter the file you have to necessarily read its full content, modify it and then overwrite the file.
  • The BinaryReader increases the stream position by itself, you may need to seek at the start but you don't need to move it every time you've read something.
  • Binary 00 is not the same as two times the character '0'.
  • When using disposable objects like Stream / File readers and writers I highly recommend using "using" for necessary cleanups.

I also think you don't want to relocate but make sure the file stays the same size and offsets don't change by padding the end of the string with two 0 bytes, right?

According to your problem description I'd write something like this (if the file is very large you might have to read and write in segments):

Code:
// Temp vars holding the file content
byte[] pre, sub;

// The password which you want to modify
string pw;

// Offsets for readability
const int begin = 0x34, end = 0x54;

// Read file
using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open))
using (BinaryReader br = new BinaryReader(fs))
{
	pre = br.ReadBytes(begin);
	pw = System.Text.Encoding.UTF8.GetString(br.ReadBytes(end - begin));
	sub = br.ReadBytes((int)fs.Length - end);
}

// Do your stuff with the password string
MessageBox.Show(pw);
if (pw.StartsWith("\0\0"))
{
	MessageBox.Show("This account is blocked still.");

	// Relocation of the two 0 bytes
	pw = pw.Substring(2);
	pw += "\0\0";
}
else
{
	//Console.WriteLine("This account is not blocked.");
	//MessageBox.Show(pw);
	MessageBox.Show("This account is not blocked.");
}

// Write the file
using (FileStream fs = new FileStream(ofd.FileName, FileMode.Create))
using (BinaryWriter bw = new BinaryWriter(fs))
{
	bw.Write(pre);
	bw.Write(System.Text.Encoding.UTF8.GetBytes(pw));
	bw.Write(sub);
}

You should refrain from using BinaryReader.ReadString() as it expects the string to be prefixed with its length, which it most probably isn't. If the string is incorrectly parsed it might be encoded differently (change the encoder). If that still doesn't help you could keep the binary pw as byte array and only parse out the parts that you need to modify.

If you purely want to relocate the bytes you can use this approach that doesn't parse the string:

Code:
// Temp vars holding the file content
byte[] pre, pw, sub;

// Offsets for readability
const int begin = 0x34, end = 0x54;

// Read file
using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open))
using (BinaryReader br = new BinaryReader(fs))
{
	pre = br.ReadBytes(begin);
	br.ReadInt16(); // Easiest way to skip two bytes really
	pw = br.ReadBytes(end - begin - 2);
	sub = br.ReadBytes((int)fs.Length - end);
}

// Write the file
using (FileStream fs = new FileStream(ofd.FileName, FileMode.Create))
using (BinaryWriter bw = new BinaryWriter(fs))
{
	bw.Write(pre);
	bw.Write(pw);
	bw.Write((short)0); // write two 0 bytes (there is more ways to do it but this is simple)
	bw.Write(sub);
}

I feel like there might be a more elegant way to do it, though it's a strange problem and usually binary parsing code looks kinda messy.
 
Last edited:
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
The tools for this file just simply move the md5 hash password over one slot to disable login and the file never changes size so it is always 7kb. I will test your ideas and let you know what my final results are. Thank you for your help.
 
Back
Top