使用BinaryWriter进行十六进制编辑(VB.NET)

时间:2013-07-26 11:44:39

标签: vb.net

我在Hex Workshop中打开一个.exe文件,我想编辑这个行:

hex

VB.NET如何使用BinaryWriter实现这一点?该行代表exe的IP。我希望有一个程序,其中包含一个文本框,您可以在其中放置新的IP。所以,我到目前为止:

Using writer As BinaryWriter = New BinaryWriter(File.Open("C:\localhost.exe", FileMode.Open))
            writer.BaseStream.Position = ???
            writer.write???
            End Using

那么,我将它设置为什么位置,我写的是什么类型?字节?感谢。

1 个答案:

答案 0 :(得分:0)

找到字符串中第一个字符的偏移量并使用此方法:

Private Sub Patch(ByVal TargetFile As String, ByVal FileOffset As Long, ByVal NewValue() As Byte)
    Try
        Dim br As BinaryReader = New BinaryReader(File.Open(TargetFile, FileMode.Open))
        br.BaseStream.Position = FileOffset
        Dim byteB As Byte
        For Each byteB In NewValue
            If (byteB.ToString <> String.Empty) Then
                br.BaseStream.WriteByte(byteB)
            Else
                Exit For
            End If
        Next byteB
        br.Close()
    Catch
    End Try
End Sub

像这样:

Patch("C:\localhost.exe", Val("OFFSET OF FIRST CHARACTER IN STRING"), New Byte() {&H31, &H32, &H37, &H2e, &H30, &H2e, &H30, &H2e, &H38})

或者代替你的字节数组,使用:

System.Text.Encoding.ASCII.GetBytes("127.0.0.8") 'It'll change 127.0.0.1 to 127.0.0.8

确保你的字符串的第一个字符有正确的偏移量