十六进制转储二进制文件的计数器

3

我正在尝试生成一个恶意软件文件的十六进制转储的二元组,这将帮助我基于二元组与不同的恶意软件文件建立关联。我试图使用计数器、zip和切片来获得结果,但却遇到了错误。如果有人能帮我解决问题,我会很高兴。

import binascii
import re
import collections
try:
    from itertools import izip as zip
except ImportError: # will be 3.x series
    pass
try:
    from itertools import islice as slice
except ImportError: # will be 3.x series
    pass
with open('path', 'rb') as f:
    for chunk in iter(lambda: f.read(), b''):
        s=binascii.hexlify(chunk)
        print(collections.Counter(zip(s),slice(s,1,None)))

The result should be like:Counter({(4d5a):200,(5a76):120,(7635):1000...}) but instead i am getting this error:


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-110-d99ed11a1260> in <module>
      3     for chunk in iter(lambda: f.read(), b''):
      4         s=binascii.hexlify(chunk)
----> 5         print(collections.Counter(zip(s),slice(s,1,None)))
      6 

~\Anaconda3\lib\collections\__init__.py in __init__(*args, **kwds)
    562         self, *args = args
    563         if len(args) > 1:
--> 564             raise TypeError('expected at most 1 arguments, got %d' % len(args))
    565         super(Counter, self).__init__()
    566         self.update(*args, **kwds)

TypeError: expected at most 1 arguments, got 2

这是用什么语言编写的?请编辑您的标签以添加语言信息。 - absoluteAquarian
完成了。谢谢。 - Shubham Kalsi
Collections() 接受一个参数。我尝试了这个:collections.Counter(foo=zip(s), bar=slice(s, 1, None)),但我不知道它是否有帮助。 - Utkonos
顺便说一句:看起来块不像你期望的那样工作。这是我正在查看的内容:https://pastebin.com/iP8PAscd - Utkonos
这里有一个替代方案,使用了stackoverflow上另一个问题中的ngrams代码片段:https://pastebin.com/GzG5FxD3 我不知道这是否解决了你的问题。 - Utkonos
1个回答

1
import binascii
import collections
import pathlib

malware = pathlib.Path().home().joinpath('Desktop').joinpath('Malware').joinpath('HWID_4_0_6YMBWX.exe')
malware.exists()

with open(malware, 'rb') as fh:
    data = fh.read()

def find_ngrams(data, n):
    s = binascii.hexlify(data).decode()
    return zip(*[s[i:] for i in range(n)])

x = find_ngrams(data, 2)

output = dict()
for ngram, count in collections.Counter(x).items():
    output[''.join(ngram)] = count
i = sorted(output.items(), key=lambda x: x[1], reverse=True)

print(i)

输出(截断):
[('00', 31198), ('ff', 14938), ('40', 11669), ('8b', 11537), ('06', 11360), ('20', 11340), ('08', 11144)......

请告诉我您将如何比较许多文件中的这些计数。我非常感兴趣。 - Utkonos
非常感谢您的帮助,但我认为这不是我想要的。从二进制文件创建的十六进制转储(例如- 4d5a90000300000004000000ff....)应该被切片并压缩,以便我可以计算二进制文件的二元组,例如{4d5a}:100,{5a90}:300,{9000}:100。通过这种方式,当我生成不同二进制文件的二元组时,我可以检查一个二进制文件与其他二进制文件的关系,这将有助于我区分二进制文件或恶意二进制文件。如果我找到解决方案,我会让您知道。 - Shubham Kalsi
我认为你只需要将2改成4。两个是字符,但你想要的是4d5a,所以你需要4个字符,对吧? - Utkonos
我使用上述代码构建了一堆代码,并正在开发各种相似性算法:[SimilarityResult(near = 0.5267333984375,far = 57.467529296875,grams = 4,simtype = <SimilarityAlgorithms.CENTER_DISTANCE:1>), SimilarityResult(near = 404496,far = 3766192,grams = 4,simtype = <SimilarityAlgorithms.SUM_OF_DIFFERENCES:2>), SimilarityResult(near = 0.1981453128933405,far = 0.11489986201073397,grams = 4,simtype = <SimilarityAlgorithms.COSINE_THETA:3>)] - Utkonos
你想要的二元组在文本中实际上是四元组,这是由.decode()函数引起的。 - Utkonos
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接