【问题标题】:Counter of a hexdump binary filehexdump 二进制文件的计数器
【发布时间】:2019-02-17 01:30:40
【问题描述】:

我正在尝试生成恶意软件文件的二元组 hexdump,这将帮助我与基于二元组的不同恶意软件文件相关联,我尝试使用计数器、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

【问题讨论】:

  • 这是什么语言的?请编辑您的标签以添加语言。
  • 做到了。谢谢。
  • Collections() 接受一个参数。我试过这个:collections.Counter(foo=zip(s), bar=slice(s, 1, None)),但我不知道这是否有帮助。
  • 顺便说一句:看起来这些块没有像我想的那样工作。这是我正在查看的内容:pastebin.com/iP8PAscd
  • 这里是使用来自 stackoverflow 上另一个问题的 ngrams sn-p 的替代方法:pastebin.com/GzG5FxD3 我不知道这是否能解决您的问题。

标签: python counter binaryfiles hexdump malware-detection


【解决方案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)......

【讨论】:

  • 做了很多修改。我想这就是你需要的。
  • 请告诉我您将如何比较多个文件的这些计数。我很感兴趣。
  • 非常感谢您的帮助,但我认为这不是我想要的。从二进制文件创建的 hexdump(例如 - 4d5a90000300000004000000ff ....)应该以这样的方式切片和压缩,以便我可以计算二进制文件的二元组,例如 {4d5a}:100,{5a90}:300 ,{9000}:100。通过这种方式,当我将生成不同二进制文件的二元组时,我可以检查一个二进制文件与其他二进制文件的关系,这将有助于我区分二进制文件或恶意二进制文件。如果我能找到解决方案,我会告诉你的。
  • 我觉得你只要把2改成4就行了,两个是字符,但是你要4d5a,所以你要4个字符,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多