Tuesday, July 9, 2019

getting byte image (byte code from malware detection

from math import log
import numpy as np

def byte_make_image(byte_code):
    img_array=[]
    for row in byte_code:
        xx=row.split()
        if len(xx)!=17:
            continue
        img_array.append([int(i,16) if i!='??' else 0 for i in xx[1:] ])
    img_array = np.array(img_array)
    if img_array.shape[1]!=16:
        assert(False)
    b=int((img_array.shape[0]*16)**(0.5))
    b=2**(int(log(b)/log(2))+1)
    a=int(img_array.shape[0]*16/b)
    img_array=img_array[:a*b/16,:]
    img_array=np.reshape(img_array,(a,b))
    #img_array = np.uint8(img_array)
    #im = Image.fromarray(img_array)
    return img_array


img = byte_make_image(byte_code)

No comments:

Post a Comment