143 PHỤ LỤC MÃ NGUỒN CƠ BẢN SỬ DỤNG TRONG LUẬN ÁNThuật toán CFD from collections import defaultdict from copy import copy, deepcopy def capNhapXuoiself, u, v, temp: self.tsDN[v] = self.t
Trang 160143
PHỤ LỤC
MÃ NGUỒN CƠ BẢN SỬ DỤNG TRONG LUẬN ÁNThuật toán CFD
from collections import defaultdict
from copy import copy, deepcopy
def capNhapXuoi(self, u, v, temp):
self.tsDN[v] = self.tsDN[v] + self.tsC[v][u] * temp
self.tsC[u][v] = self.tsC[v][u] * temp
Trang 161self.opcodeNumber = 100 # so opcode trong do thi
self.trongso = None # mang trong so khi dem duong bang qhdself.goc = 0 # dinh goc do thi
self.gocChuTrinh = 0 # goc chu trinh
self.truoc = [] # dinh lien truoc cua 1 dinh trong qua trinhdanh dau
self.V = vertices
self.paths = []
self.isleaf = None
self.coLienThong = [False] * self.V
self.graph = defaultdict(list) # do thi chinh
self.rgraph = defaultdict(list) # do thi dao
self.cgraph = defaultdict(list) # do thi danh dau bo quy trinhself.lgraph = defaultdict(list) # do thi danh dau cac duonglien thong co ban
# function to add an edge to graph
Trang 162chuaxet = [[0] * self.V for _ in range(self.V)]
allVertexBefore = [list()] * self.V
Trang 163146
if (chuaxet[u][v] == 0):
if (not (v in allVertexBefore[u])):
que.append(v)chuaxet[u][v] = 1allVertexBefore[v] = allVertexBefore[v] | allVertexBefore[u]else:
canhxoa.append(v)for v in canhxoa:
Trang 164def loangTu(self, u):
# Mark the current node as visited and store in path
Trang 165# Mark all the vertices as not visited
visited = [False] * (self.V)
path = []
# Call the recursive helper function to print all paths
self.tryPathToLeafs(self.goc, visited, path)
Trang 166if (time.time() - self.starttime < self.nguong):
# If current vertex is not destination
# Recur for all the vertices adjacent to this vertex
for i in self.graph[u]:
if visited[i] == False:
self.tryPathToLeafs(i, visited, path)
# Remove current vertex from path[] and mark it as unvisitedpath.pop()
# Mark all the vertices as not visited
visited = [False] * (self.V)
# Create an array to store paths
Trang 167150
path = []
# Call the recursive helper function to print all paths
self.tryAllPathToLeafs(self.goc, visited, path)
# Đếm số đường đi theo phương pháp CFD
# Số lần xuất hiện của đỉnh u trên đường tổng là sefl.trongso.tsDN[u]
# Số lần xuất hiện của cạnh (u,v) trên đường tổng là
Trang 168#Tính phần giao nhau giữa 2 block (2 đỉnh) khi lấy 3-gram
def opcode_giao_nhau_2dinh_3gram(elf,idV1, idV2):
'''
tính phần 3-gram giao nhau giữa 2 basic block
:param elf: tệp tin elf
if len(opcodeV1) >=2 and len(opcodeV2) >= 2:
giaogiao = [opcodeV1[-2], opcodeV1[-1], opcodeV2[0], opcodeV2[1]]return n_gram_in_block(giaogiao,3)
elif len(opcodeV1) ==1 and len(opcodeV2) >= 2:
return Counter([(opcodeV1[0], opcodeV2[0], opcodeV2[1])])
elif len(opcodeV1) >=2 and len(opcodeV2) == 1:
Trang 169152
return Counter([(opcodeV1[-1], opcodeV1[-2], opcodeV2[0])])else:
return Counter()
#Tính phần giao nhau giữa 2 block (2 đỉnh) khi lấy 2-gram
def opcode_giao_nhau_2dinh_2gram(elf,idV1, idV2):
'''
lay phan 2-gram giao nhau giua 2 basic block
:param elf: file mẫu elf
if len(opcodeV1) != 0 and len(opcodeV2) != 0:
return Counter([(opcodeV1[-1], opcodeV2[0])])
else:
return Counter()
Trang 170153
Thuật toán CFDVex
from lib.analysis.graph import Graph
from lib.analysis.elf_analysis import Elf
Tính n-gram trong một basic block
:param basicBlock: mã Vex của các basic block được lưu trong mảngbasicBlock
:param nGram: 2 hoặc 3
:return: ngram cua basic block
# nhan tat ca cac key trong fdist voi trong so
for key in fdist.keys():
Trang 171return Counter()
def vexStatements_2gram(self):
'''
TODO tinh 2gram VEX IR statements:
:param binPath: duong dan elf
:param cfgPath: duong dan cfg
:return: counter chua 2-gram opcode cua file elf'''
elf = Elf(self.elfFile)
elf.loadCFG(self.cfgFile)
elf.makeGraph()
Trang 172self.graphInfo = [numVertices, numPaths[0]]
# tinh freqdist cua 2-gram opcode
opcode_2gram_freqdist = Counter() # counter chua 2-gram cua fileelf
Trang 173156
opcode_2gram_freqdist.update(block_freqdist)
# cap nhat 2-gram list_file = []
#cua block vao 2gram cua elf filefor v in graph.graph[u]:
pass
self.vex2gramStatements = opcode_2gram_freqdist.items()
return self.vex2gramStatements