• 对指定目录下的数据生成样本和section名之间的二分图
• 程序的命令行参数和listing-4-8.py保持一致,输入为指定目录名,输出为二分图的dot文件,样本投影的dot文件和section名的dot文件
• 程序功能可以在listing-4-8.py基础上进行修改
老师提供的代码样例
#!/usr/bin/python3
import pefile
import sys
import argparse
import os
import pprint
import networkx
import re
from networkx.drawing.nx_agraph import write_dot
import collections
from networkx.algorithms import bipartite
args = argparse.ArgumentParser("Visualize shared hostnames between a directory of malware samples")
args.add_argument("target_path",help="directory with malware samples")
args.add_argument("output_file",help="file to write DOT file to")
args.add_argument("malware_projection",help="file to write DOT file to")
args.add_argument("hostname_projection",help="file to write DOT file to")
args = args.parse_args()
network = networkx.Graph()
valid_hostname_suffixes = [string.strip() for string in open("domain_suffixes.txt")]
valid_hostname_suffixes = set(valid_hostname_suffixes)
def find_hostnames(string):
possible_hostnames = re.findall(r'(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{,61}[a-zA-Z0-9])?.)+[a-zA-Z]{2,6}', string)
valid_hostnames = [hostname for hostname in possible_hostnames if hostname.split(".")[-1].lower() in valid_hostname_suffixes]
return valid_hostnames
# search the target directory for valid Windows PE executable files
for root,dirs,files in os.walk(args.target_path):
for path in files:
# try opening the file with pefile to see if it's really a PE file
try:
pe = pefile.PE(os.path.join(root,path))
except pefile.PEFormatError:
continue
fullpath = os.path.join(root,path)
# extract printable strings from the target sample
strings = os.popen("strings '{0}'".format(fullpath)).read()
# use the search_doc function in the included reg module to find hostnames
hostnames = find_hostnames(strings)
if len(hostnames):
# add the nodes and edges for the bipartite network
network.add_node(path,label=path[:32],color='black',penwidth=5,bipartite=0)
for hostname in hostnames:
network.add_node(hostname,label=hostname,color='blue',
penwidth=10,bipartite=1)
network.add_edge(hostname,path,penwidth=2)
if hostnames:
print("Extracted hostnames from:",path)
pprint.pprint(hostnames)
# write the dot file to disk
write_dot(network, args.output_file)
malware = set(n for n,d in network.nodes(data=True) if d['bipartite']==0)
hostname = set(network)-malware
# use NetworkX's bipartite network projection function to produce the malware
# and hostname projections
malware_network = bipartite.projected_graph(network, malware)
hostname_network = bipartite.projected_graph(network, hostname)
# write the projected networks to disk as specified by the user
write_dot(malware_network,args.malware_projection)
write_dot(hostname_network,args.hostname_projection)
代码分析:
完成作业之前我们先分析一下代码的各个模块,以便更好的运用和修改
(1)argparse基本用法#创建对象
args = argparse.ArgumentParser("Visualize shared hostnames between a directory of malware samples")
#添加参数
args.add_argument("target_path",help="directory with malware samples")
#参数第一个是选项,后一个是help命令时的说明
args.add_argument("output_file",help="file to write DOT file to")
args.add_argument("malware_projection",help="file to write DOT file to")
args.add_argument("hostname_projection",help="file to write DOT file to")
#解析参数
args = args.parse_args()
argparse 是python自带的命令行参数解析包,可以用来方便地读取命令行参数。它的使用也比较简单。
参考 argparse用法 ,我们知道在这里是用来读取命令行参数,并输出文件output_file、malware_projection、hostname_projection的。
作用:返回一个去掉特殊字符的字符串副本。
参数:需要去掉的特殊字符。从字符串起始至末尾去掉chars。默认值:字符串中的空格。
#!/usr/bin/python str = "0000000this is string example....wow!!!0000000"; print str.strip( '0' ); 输出:this is string example....wow!!!
因此样例中的此行代码为——从文件domain_suffixes.txt中读取去除了空格的字符串(域名)到数组valid_hostname_suffixes中。接着使用set()函数删除数组中重复的元素。
valid_hostname_suffixes = [string.strip() for string in open("domain_suffixes.txt")]
(3)正则表达式
python 正则表达式
#对字符串进行处理——对域名匹配
def find_hostnames(string):
possible_hostnames = re.findall(r'(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{,61}[a-zA-Z0-9])?.)+[a-zA-Z]{2,6}', string)
##如果找到的域名后缀在后缀库中,则为真正的域名
valid_hostnames = [hostname for hostname in possible_hostnames if hostname.split(".")[-1].lower() in valid_hostname_suffixes]
return valid_hostnames
(4)os.path.join()函数
作用:连接两个或更多的路径名组件。
1.如果各组件名首字母不包含’/’,则函数会自动加上;
2.第一个以”/”开头的参数开始拼接,之前的参数全部丢弃,当有多个时,从最后一个开始;
3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾;
代码修改:
分析完大部分陌生的代码后,我们就知道,其实只要将对应的hostname更换成section名字——sectionname即可。
完整代码如下:
#!/usr/bin/python3
import pefile
import sys
import argparse
import os
import pprint
import networkx
import re
from networkx.drawing.nx_agraph import write_dot
import collections
from networkx.algorithms import bipartite
#从命令行读取参数target_path、output_file、malware_projection、section_projection
args = argparse.ArgumentParser("Visualize shared hostnames between a directory of malware samples")
args.add_argument("target_path", help="directory with malware samples")
args.add_argument("output_file", help="file to write DOT file to")
args.add_argument("malware_projection", help="file to write DOT file to")
args.add_argument("section_projection", help="file to write DOT file to")
args = args.parse_args()
network = networkx.Graph()
# search the target directory for valid Windows PE executable files
for root, dirs, files in os.walk(args.target_path):
for path in files:
# try opening the file with pefile to see if it's really a PE file
try:
pe = pefile.PE(os.path.join(root, path))
except pefile.PEFormatError:
continue
#寻找文件路径
fullpath = os.path.join(root, path)
#向二分图里添加节点、边
if len(pe.sections):
#将文件名作为节点添加二分图
network.add_node(path, label=path[:32], color='black', penwidth=5, bipartite=0)
for section in pe.sections:
#对sectionname进行处理
sectionname = str(section.Name.strip(b'x00'))
#将sectionname作为节点添加进二分图
network.add_node(sectionname,label=sectionname,color='blue',penwidth=10,bipartite=1)
#构造边
network.add_edge(sectionname,path,penwidth=2)
if pe.sections:
print("Extracted hostnames from:", path)
pprint.pprint(sectionname)
# write the dot file to disk
write_dot(network, args.output_file)
malware = set(n for n, d in network.nodes(data=True) if d['bipartite'] == 0)
section = set(network) - malware
# use NetworkX's bipartite network projection function to produce the malwareand section projections
malware_network = bipartite.projected_graph(network, malware)
section_network = bipartite.projected_graph(network, section)
# write the projected networks to disk as specified by the user
write_dot(malware_network, args.malware_projection)
write_dot(section_network, args.section_projection)
最后提供参数并执行文件test.py
python3 test.py ~/Desktop/hw2/APT1_MALWARE_FAMILIES/ network.dot malware.dot sectinname.dot



