我正在尝试将文件(任何文件,如 exe、apk)转换为灰度图像。我已经使用下面的代码准备了文件的二进制位。然而,我坚持将 8 位分组来表示图像中的一个像素,因此每个像素的范围是 0-255。文献表明,恶意软件可以通过将其转换为灰度图像并应用CNN模型进行分类来进行分类
import cv2
import numpy
import os
import binascii
filePath = "240387329dee4f03f98a89a2feff9bf30dcba61fcf614cdac24129da54442762"
file = open(filePath, "rb")
with file:
byte = file.read()
hexadecimal = binascii.hexlify(byte)
decimal = int(hexadecimal, 16)
binary = bin(decimal)[2:].zfill(8)
print("hex: %s, decimal: %s, binary: %s" % (hexadecimal, decimal, binary))
Run Code Online (Sandbox Code Playgroud)
编辑:
我已经写了下面的内容,其中固定了图像的宽度。任何反馈?
import cv2
import numpy
import os
import binascii
import array
import scipy.misc
#print (format(5,"b"))
filename='240387329dee4f03f98a89a2feff9bf30dcba61fcf614cdac24129da54442762';
f=open(filename,'rb');
ln = os.path.getsize(filename);
width = 500;
rem = ln%width;
a=array.array("B");
a.fromfile(f,ln-rem);
f.close;
g=numpy.reshape(a,(len(a)/width,width));
g= numpy.uint8(g);
scipy.misc.imsave('Malware.png',g);
Run Code Online (Sandbox Code Playgroud)