|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import pefile |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import pprint |
| 8 | +import logging |
| 9 | +import networkx |
| 10 | +import collections |
| 11 | +import tempfile |
| 12 | +from networkx.drawing.nx_agraph import write_dot |
| 13 | +from networkx.algorithms import bipartite |
| 14 | + |
| 15 | +# Use argparse to parse any command line arguments |
| 16 | + |
| 17 | +args = argparse.ArgumentParser("Visualize shared image relationships between a directory of malware samples") |
| 18 | +args.add_argument("target_path",help="directory with malware samples") |
| 19 | +args.add_argument("output_file",help="file to write DOT file to") |
| 20 | +args.add_argument("malware_projection",help="file to write DOT file to") |
| 21 | +args.add_argument("resource_projection",help="file to write DOT file to") |
| 22 | +args = args.parse_args() |
| 23 | +network = networkx.Graph() |
| 24 | + |
| 25 | +class ExtractImages(): |
| 26 | + def __init__(self,target_binary): |
| 27 | + self.target_binary = target_binary |
| 28 | + self.image_basedir = None |
| 29 | + self.images = [] |
| 30 | + |
| 31 | + def work(self): |
| 32 | + self.image_basedir = tempfile.mkdtemp() |
| 33 | + icondir = os.path.join(self.image_basedir,"icons") |
| 34 | + bitmapdir = os.path.join(self.image_basedir,"bitmaps") |
| 35 | + raw_resources = os.path.join(self.image_basedir,"raw") |
| 36 | + for directory in [icondir,bitmapdir,raw_resources]: |
| 37 | + os.mkdir(directory) |
| 38 | + rawcmd = "wrestool -x {0} -o {1} 2> /dev/null".format(self.target_binary,raw_resources) |
| 39 | + bmpcmd = "mv {0}/*.bmp {1} 2> /dev/null".format(raw_resources,bitmapdir) |
| 40 | + icocmd = "icotool -x {0}/*.ico -o {1} 2> /dev/null".format(raw_resources,icondir) |
| 41 | + for cmd in [rawcmd,bmpcmd,icocmd]: |
| 42 | + try: |
| 43 | + os.system(cmd) |
| 44 | + except Exception,msg: |
| 45 | + pass |
| 46 | + for dirname in [icondir,bitmapdir]: |
| 47 | + for path in os.listdir(dirname): |
| 48 | + logging.info(path) |
| 49 | + path = os.path.join(dirname,path) |
| 50 | + imagehash = hash(open(path).read()) |
| 51 | + if path.endswith(".png"): |
| 52 | + self.images.append((path,imagehash)) |
| 53 | + if path.endswith(".bmp"): |
| 54 | + self.images.append((path,imagehash)) |
| 55 | + |
| 56 | + def cleanup(self): |
| 57 | + os.system("rm -rf {0}".format(self.image_basedir)) |
| 58 | + |
| 59 | +# search the target directory for PE files to extract images from |
| 60 | +image_objects = [] |
| 61 | +for root,dirs,files in os.walk(args.target_path): |
| 62 | + for path in files: |
| 63 | + # try to parse the path to see if it's a valid PE file |
| 64 | + |
| 65 | + try: |
| 66 | + pe = pefile.PE(os.path.join(root,path)) |
| 67 | + except pefile.PEFormatError: |
| 68 | + continue |
| 69 | + fullpath = os.path.join(root,path) |
| 70 | + images = ExtractImages(fullpath) |
| 71 | + images.work() |
| 72 | + image_objects.append(images) |
| 73 | + |
| 74 | + # create the network by linking malware samples to their images |
| 75 | + for path, image_hash in images.images: |
| 76 | + # set the image attribute on the image nodes to tell GraphViz to render images within these nodes |
| 77 | + if not image_hash in network: |
| 78 | + network.add_node(image_hash,image=path,label='',type='image') |
| 79 | + node_name = path.split("/")[-1] |
| 80 | + network.add_node(node_name,type="malware") |
| 81 | + network.add_edge(node_name,image_hash) |
| 82 | + |
| 83 | +# write the bipartite network, then do the two projections and write them |
| 84 | +write_dot(network, args.output_file) |
| 85 | +malware = set(n for n,d in network.nodes(data=True) if d['type']=='malware') |
| 86 | +resource = set(network) - malware |
| 87 | +malware_network = bipartite.projected_graph(network, malware) |
| 88 | +resource_network = bipartite.projected_graph(network, resource) |
| 89 | + |
| 90 | +write_dot(malware_network,args.malware_projection) |
| 91 | +write_dot(resource_network,args.resource_projection) |
0 commit comments