【问题标题】:How to find an address by a hex value in string format?如何通过字符串格式的十六进制值查找地址?
【发布时间】:2019-06-29 16:46:58
【问题描述】:

我有一个 int key1 = -1466731422;,当我在十六进制编辑器中搜索它时,它会从 .exe 中返回十六进制值 62 74 93 A8
我要做的是覆盖newKey1,用户可以选择它,覆盖key1
假设我们想用int newKey1 = -1566731422 覆盖key1,到目前为止我所做的是:

private void btnGravar_Click(object sender, EventArgs e)
{
    FixHex(key1, newKey1); //transform the int key in hex string

    br = new BinaryReader(File.OpenRead(element.FileName));            
    try
    {                
        for (long i = 0; i <= br.BaseStream.Length; i++)
        {
            if (br.BaseStream.ReadByte() == (byte)Convert.ToInt32(key1, 16))
            {
                progressBar.Value = progressBar.Maximum;
                br.Close();
                bw = new BinaryWriter(File.OpenWrite(element.FileName));
                bw.BaseStream.Position = i;
                bw.Write(newKey1);
                bw.Close();

                MessageBox.Show("Key updated", "Success");
                break;
            }
            else
            {
                progressBar.Value += 1;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}        

它没有工作,但是。我的for 循环没有找到匹配项,所以我认为它会解决它是一种通过int key(因为在十六进制编辑器中我可以做到)或字符串格式的十六进制来查找地址的方法。

【问题讨论】:

  • 您只比较一个字节而不是整个密钥。这会导致很多误报。
  • @itsme86:比较的两边只是一个字节,一个通过读取一个字节,另一个通过丢弃高位。
  • @BenVoigt 我的FixHex 方法返回给我:string key1 = "0x627493A8"string newKey1 = "0x62939DA2" 还有另一种方法可以将它与ReadByte 进行比较?
  • 我不知道是否有帮助,但我也有一个包含每个十六进制对的列表...例如:627493A8 :: pairHex[0] = 62;对十六进制 [1] = 74;对十六进制 [2] = 93;等等。

标签: c# winforms binary hex byte


【解决方案1】:

正如其他人所说,您没有以正确的方式比较正确的事物。 您想要比较正在比较的内容的方式取决于您在 BinaryReader 中读取的数据的格式以及“key1”的类型。

在您的描述中,您说“key1”是一个 int,但随后尝试将其转换为字符串。我只是假设它是一个int。如有不妥可以留言。

从描述中也不清楚您文件中数据的格式。我假设它是整数。如果它不是整数,请添加评论。读取整数看起来像:

int testVal = br.ReadInt32();

这给了你一个 int,因为两者都是 int,你可以只比较 ints:

if(testVal == key1)
{/*do your other stuff*/}

最好在不是文件末尾时循环而不是循环每个字节,例如:

while(br.BaseStream.Position != br.Basestream.Length)

把它放在一起可能看起来像:

while(br.BaseStream.Position != br.Basestream.Length)
{        
    int testVal = br.ReadInt32();

    if(testVal == key1)
    {/*do your other stuff*/}
}

另一个注意事项:因为每次调用此方法时您都在“新建”BinaryReader 并且 BinaryReader 在“if”中关闭,所以您可以考虑在 BinaryReader 周围使用using 并在那里声明。这将确保 BinaryReader 在异常情况下被正确处理。下面的变体将允许对 BinaryReader 进行适当的处​​理,但仍会捕获任何异常(此外,捕获一般异常也令人不悦):

try
{
    using(var br = new BinaryReader(File.OpenRead(element.FileName)))
    {
        while(br.BaseStream.Position != br.Basestream.Length)
        {        
            if(br.ReadInt32() == key1)
            {/*do your other stuff*/}
        }
    }
}
catch(Exception ex)
{
}

希望对您有所帮助。

【讨论】:

    【解决方案2】:
    1. 由于您正在寻找一个整数值(占用四个字节),因此无需读取单个字节。使用带有Int32 值的BinaryReader 重载直接读取一个整数,并查看它是否与您寻找的匹配。
    2. 您正在尝试编写一个字符串。它不是一个字符串,它是一个小端的四个字节序列。您需要编写这四个字节来替换现有的那些。

    BinaryReader.ReadInt32() 将流位置提前 4 个字节,因此您需要跟踪读取位置,每次读取将其递增 1 并设置BaseStream 位置手动

    BinaryReaderBinaryWriter使用FileAccess.ReadWriteFileShare.ReadWrite,你可以找到值并一次性覆盖它:

    注意:这里没有办法验证可能的误报,因为缺少有关文件结构的信息。这取决于你。

    int valOriginal = -1466731422;
    int valSubstitute = -1566731422;
    int valLength = BitConverter.GetBytes(valOriginal).Length;
    
    using (var reader = new BinaryReader(
        File.Open("[File Path]", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))) {
        int position = 0;
        while (position < (reader.BaseStream.Length - valLength))
        {
            reader.BaseStream.Position = position;
            if (reader.ReadInt32() == valOriginal)
            {
                using (var writer = new BinaryWriter(
                    File.Open("[File Path]", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))) {
                    writer.BaseStream.Position = position;
                    writer.Write(valSubstitute);
                };
                break;
            }
            position += 1;
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-08
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 2019-07-27
      • 2011-05-04
      • 1970-01-01
      相关资源
      最近更新 更多