在 ReadAllBytes 中搜索特定值

发布于 2025-01-03 01:32:16 字数 719 浏览 7 评论 0原文

我正在编写一个程序,该程序读取“.exe”文件并将其十六进制值存储在字节数组中,以便与包含一系列值的数组进行比较。 (就像一个非常简单的病毒扫描程序)

byte[] buffer = File.ReadAllBytes(currentDirectoryContents[j]);

然后我使用 BitConverter 创建这些值的单个字符串

string hex = BitConverter.ToString(buffer);

下一步是在该字符串中搜索一系列值(定义)并返回正值匹配。这就是我遇到问题的地方。我的定义是十六进制值,但在记事本中创建并保存为defintions.xyz

string[] definitions = File.ReadAllLines(@"C:\definitions.xyz");

我一直在尝试将它们读入字符串数组并将数组的定义元素与字符串十六进制进行比较

bool[] test = new bool[currentDirectoryContents.Length];

test[j] = hex.Contains(definitions[i]);

这是作业中的一部分,这就是为什么我不会发布该程序的完整代码。上周五之前我没有使用过 C#,所以此时我很可能会犯一些愚蠢的错误。

非常感谢任何建议:)

I am writing a program that reads '.exe' files and stores their hex values in an array of bytes for comparison with an array containing a series of values. (like a very simple virus scanner)

byte[] buffer = File.ReadAllBytes(currentDirectoryContents[j]);

I have then used BitConverter to create a single string of these values

string hex = BitConverter.ToString(buffer);

The next step is to search this string for a series of values(definitions) and return positive for a match. This is where I am running into problems. My definitions are hex values but created and saved in notepad as defintions.xyz

string[] definitions = File.ReadAllLines(@"C:\definitions.xyz");

I had been trying to read them into a string array and compare the definition elements of the array with string hex

bool[] test = new bool[currentDirectoryContents.Length];

test[j] = hex.Contains(definitions[i]);

This IS a section from a piece of homework, which is why I am not posting my entire code for the program. I had not used C# before last Friday so am most likely making silly mistakes at this point.

Any advice much appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2025-01-10 01:32:16

目前还不清楚您到底使用哪种定义格式。 Base64 是一种很好的 byte[] 编码,您可以使用 Convert.ToBase64String 和 Convert.FromBase64String() 快速来回转换。但你的问题表明字节是以十六进制编码的。我们假设新的 byte[] { 1, 2, 3, 4} 看起来像“01020304”。然后这个辅助函数将这样的字符串转换回 byte[]:

    static byte[] Hex2Bytes(string hex) {
        if (hex.Length % 2 != 0) throw new ArgumentException();
        var retval = new byte[hex.Length / 2];
        for (int ix = 0; ix < hex.Length; ix += 2) {
            retval[ix / 2] = byte.Parse(hex.Substring(ix, 2), System.Globalization.NumberStyles.HexNumber);                
        }
        return retval;
    }

您现在可以使用 Boyer-Moore 等算法进行快速模式搜索。

It is pretty unclear exactly what kind of format you use of the definitions. Base64 is a good encoding for a byte[], you can rapidly convert back and forth with Convert.ToBase64String and Convert.FromBase64String(). But your question suggests the bytes are encoded in hex. Let's assume it looks like "01020304" for a new byte[] { 1, 2, 3, 4}. Then this helper function converts such a string back to a byte[]:

    static byte[] Hex2Bytes(string hex) {
        if (hex.Length % 2 != 0) throw new ArgumentException();
        var retval = new byte[hex.Length / 2];
        for (int ix = 0; ix < hex.Length; ix += 2) {
            retval[ix / 2] = byte.Parse(hex.Substring(ix, 2), System.Globalization.NumberStyles.HexNumber);                
        }
        return retval;
    }

You can now do a fast pattern search with an algorithm like Boyer-Moore.

圈圈圆圆圈圈 2025-01-10 01:32:16

我希望您明白这是一种非常低效的方法。但除此之外,您应该这样做:

bool[] test = new bool[currentDirectoryContents.Length];
for(int i=0;i<test.Length;i++){
  byte[] buffer = File.ReadAllBytes(currentDirectoryContents[j]);
  string hex = BitConverter.ToString(buffer);
  test[i] = ContainsAny(hex, definitions);
}

bool ContainsAny(string s, string[] values){
  foreach(string value in values){
    if(s.Contains(value){
      return true;
    }
  }
  return false;
}

如果您可以使用 LINQ,您可以这样做:

var test = currentDirectoryContents.Select(
             file=>definitions.Any(
               definition => 
                 BitConverter.ToString(
                   File.ReadAllBytes(file)
                 ).Contains(definition)
             )
           ).ToArray();

另外,请确保您的定义文件的格式与 BitConverter 的输出相匹配。 ToString():大写,用破折号分隔每个编码字节:

12-AB-F0-34
54-AC-FF-01-02 

I expect you understand that this is a very inefficient way to do it. But except for that, you should just do something like this:

bool[] test = new bool[currentDirectoryContents.Length];
for(int i=0;i<test.Length;i++){
  byte[] buffer = File.ReadAllBytes(currentDirectoryContents[j]);
  string hex = BitConverter.ToString(buffer);
  test[i] = ContainsAny(hex, definitions);
}

bool ContainsAny(string s, string[] values){
  foreach(string value in values){
    if(s.Contains(value){
      return true;
    }
  }
  return false;
}

If you can use LINQ, you can do it like this:

var test = currentDirectoryContents.Select(
             file=>definitions.Any(
               definition => 
                 BitConverter.ToString(
                   File.ReadAllBytes(file)
                 ).Contains(definition)
             )
           ).ToArray();

Also, make sure that your definitions-file is formatted in a way that matches the output of BitConverter.ToString(): upper-case with dashes separating each encoded byte:

12-AB-F0-34
54-AC-FF-01-02 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文