Jump to content
Tuts 4 You

[Help] Patching Tool In VB.Net


Guest forum

Recommended Posts

Guest forum

Hi all. i am new to the patching. i want to make a patch in vb.net , which will replace multiple bytes of a program.

like when i load the original & patched file in DUP , its comparision :

patchh.png

there are many patterns

how to make its patch in vb.net

Link to comment

I found a 2011 project made on VB.NET

here is an simple example of how to patch data 

                '>>>>>>>>>>>>patch OFFSETS
                Dim PatchOffsets As List(Of GenericArray) = //here create a new list of patch offsets //AddFilePatch.Item(i).Offsets
                Dim destStream As New FileStream(filePatch, FileMode.Open)
                ' destStream.Lock(0, destStream.Length)

                For k = 0 To PatchOffsets.Count - 1
                    destStream.Position = PatchOffsets.Item(k).var1
                    destStream.WriteByte("&H" & PatchOffsets.Item(k).var2)
                Next

                If PatchOffsets.Count > 0 Then
                    txtInfo.Text = txtInfo.Text & "  " & PatchOffsets.Count & " byte(s) patched!" & vbCrLf
                End If
                '>>>>>>>>>>>>patch OFFSETS

the GenericArray class is like :
Public Class GenericArray

    Dim _var1 As String //file position
    Dim _var2 As String //new value

 

to compare two files and find the diffs C# example :

            byte[] flOrigin = System.IO.File.ReadAllBytes(textBox1.Text);
            byte[] flPatched = System.IO.File.ReadAllBytes(textBox2.Text);


            int startOFFSET = 0;
            int endOFFSET = flOrigin.Length;

            for (i = startOFFSET; i < endOFFSET; i++)
            {
                if (flOrigin[i] != flPatched[i])
                {
                    lstvItem = new ListViewItem();
                    lstvItem.Text = flOrigin[i].ToString("X2");
                    lstvItem.SubItems.Add(flPatched[i].ToString("X2"));
                    lstvItem.SubItems.Add(i.ToString());

                    lstv.Items.Add(lstvItem);
                }
            }

 

 

dont get confused :

-an application with two textboxes contain the files

-a button than find the diffs (using the 2nd example)

-a button apply the changes (using the 1st example)

 

 

 

Edited by whoknows
  • Thanks 1
Link to comment
  • 2 weeks later...
On 4/12/2020 at 2:48 AM, forum said:

thank you for the code. but i am unable to use these codes , any tutorial how to use it

yeah mee too can't able to use the code. it gives me lot of errors
But hopefully he can have a time to share to us the correct code and project file also

  • Like 1
Link to comment
On 4/11/2020 at 10:07 PM, whoknows said:

I found a 2011 project made on VB.NET

here is an simple example of how to patch data 


                '>>>>>>>>>>>>patch OFFSETS
                Dim PatchOffsets As List(Of GenericArray) = //here create a new list of patch offsets //AddFilePatch.Item(i).Offsets
                Dim destStream As New FileStream(filePatch, FileMode.Open)
                ' destStream.Lock(0, destStream.Length)

                For k = 0 To PatchOffsets.Count - 1
                    destStream.Position = PatchOffsets.Item(k).var1
                    destStream.WriteByte("&H" & PatchOffsets.Item(k).var2)
                Next

                If PatchOffsets.Count > 0 Then
                    txtInfo.Text = txtInfo.Text & "  " & PatchOffsets.Count & " byte(s) patched!" & vbCrLf
                End If
                '>>>>>>>>>>>>patch OFFSETS

the GenericArray class is like :
Public Class GenericArray

    Dim _var1 As String //file position
    Dim _var2 As String //new value

 

to compare two files and find the diffs C# example :


            byte[] flOrigin = System.IO.File.ReadAllBytes(textBox1.Text);
            byte[] flPatched = System.IO.File.ReadAllBytes(textBox2.Text);


            int startOFFSET = 0;
            int endOFFSET = flOrigin.Length;

            for (i = startOFFSET; i < endOFFSET; i++)
            {
                if (flOrigin[i] != flPatched[i])
                {
                    lstvItem = new ListViewItem();
                    lstvItem.Text = flOrigin[i].ToString("X2");
                    lstvItem.SubItems.Add(flPatched[i].ToString("X2"));
                    lstvItem.SubItems.Add(i.ToString());

                    lstv.Items.Add(lstvItem);
                }
            }

 

 

dont get confused :

-an application with two textboxes contain the files

-a button than find the diffs (using the 2nd example)

-a button apply the changes (using the 1st example)

 

 

 


This help me a lot to complete my personal tool/collection :)

I was able to  recode C# code to vb.net

Here's the screenshot of final build


This will help me a lot for my learning about patching using find and replace hex

spacer.png

spacer.png

  • Like 2
Link to comment

nice shit mate!

--

now, create 'the patcher app'

this will read the embedded resource from itself  (ex ikriv.com/blog/?p=1530),

call it offsets.txt or whatever, this function to read the resource :

        public static string ReadASCIIfromResources(string filename)
        {
            byte[] Buffer;
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename))
            {
                Buffer = new byte[stream.Length];
                stream.Read(Buffer, 0, Buffer.Length);
            }

            return Encoding.ASCII.GetString(Buffer);
        }

when user hit the PATCH button, read the offsets.txt, load the target file and patch it,  will be good to store the original file SHA1 to make a checksum comparison (aka is the equal target and not a diff version)

--

once the 'the patcher app' working as you expected, remove the offsets.txt resource &compile it.

add the 'the patcher app' exe, to your app  u posted^ as embedded resource.

create a new button 'create patch', this will read the 'the patcher app' exe from resources 

            byte[] byteTMP;
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("URnamespace.P.exe"))
            {
                byteTMP = new byte[stream.Length];
                stream.Read(byteTMP, 0, byteTMP.Length);
            }

 

 

use Mono.Cecil to add the embedded resource w/ the needed offsets

            AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(mStream);

            EmbeddedResource erTemp = null;
			
            //PATCHDATA    
            string tmp = ";" + txtAppName.Text + "|" + txtAuthor.Text + "|" + txtURL.Text + "|" + txtDate.Text + "\r\n";
            for (int i = 0; i < General.Patch.Count; i++)
            {
                tmp += (General.Patch[i]) + "\r\n";
            }
			
            erTemp = new EmbeddedResource("URnamespace.offsets.txt", ManifestResourceAttributes.Public, Encoding.ASCII.GetBytes(tmp));
            assembly.MainModule.Resources.Add(erTemp);

save the assembly with Mono.Cecil

 assembly.Write(stream);

 

this code parts taken from different ancient projects, just to give you an idea. ofc u can use dnlib to do the resources work, the result will be the same.

Edited by whoknows
Link to comment
1 hour ago, whoknows said:

nice shit mate!

--

now, create 'the patcher app'

this will read the embedded resource from itself  (ex ikriv.com/blog/?p=1530),

call it offsets.txt or whatever, this function to read the resource :


        public static string ReadASCIIfromResources(string filename)
        {
            byte[] Buffer;
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename))
            {
                Buffer = new byte[stream.Length];
                stream.Read(Buffer, 0, Buffer.Length);
            }

            return Encoding.ASCII.GetString(Buffer);
        }

when user hit the PATCH button, read the offsets.txt, load the target file and patch it,  will be good to store the original file SHA1 to make a checksum comparison (aka is the equal target and not a diff version)

--

once the 'the patcher app' working as you expected, remove the offsets.txt resource &compile it.

add the 'the patcher app' exe, to your app  u posted^ as embedded resource.

create a new button 'create patch', this will read the 'the patcher app' exe from resources 


            byte[] byteTMP;
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("URnamespace.P.exe"))
            {
                byteTMP = new byte[stream.Length];
                stream.Read(byteTMP, 0, byteTMP.Length);
            }

 

 

use Mono.Cecil to add the embedded resource w/ the needed offsets


            AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(mStream);

            EmbeddedResource erTemp = null;
			
            //PATCHDATA    
            string tmp = ";" + txtAppName.Text + "|" + txtAuthor.Text + "|" + txtURL.Text + "|" + txtDate.Text + "\r\n";
            for (int i = 0; i < General.Patch.Count; i++)
            {
                tmp += (General.Patch[i]) + "\r\n";
            }
			
            erTemp = new EmbeddedResource("URnamespace.offsets.txt", ManifestResourceAttributes.Public, Encoding.ASCII.GetBytes(tmp));
            assembly.MainModule.Resources.Add(erTemp);

save the assembly with Mono.Cecil


 assembly.Write(stream);

 

this code parts taken from different ancient projects, just to give you an idea. ofc u can use dnlib to do the resources work, the result will be the same.

Wow I'm gonna explore this code too :)
This is big help for me as a beginner 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...