-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinding_path_ip_to_target.py
105 lines (75 loc) · 3.07 KB
/
finding_path_ip_to_target.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# this function finds attack paths from all edge state(s) to target state in the graph
# the function takes the graph, target state and source IP as input and find paths between them
# first we import all necessary module exploit
import json
from graph_tool.all import *
import time
import numpy as np
import re
from collections import deque
import csv
def attack_paths_ip_to_target(g, start, target_IP, max=10000):
if start >= g.num_vertices():
print ("TODO: Raise error")
return []
# print "==== Searching for path to node index ", start, "in the graph ===="
paths =[]
q = deque()
threshold=0
label=g.vp["label"][start]
src_IP=re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", label)[0]
if src_IP==target_IP:
print('attack graph source IP and target IP is same')
else:
IP_track=False
for oe in g.vertex(start).out_edges():
q.append(([int(oe.target())], [(start, int(oe.target()))], 0))
while len(q):
u = q.popleft()
key = u[0].pop(0)
if g.vp["shape"][key] == "AND":
src_IP=0
conds = map(lambda x:x.target(), g.vertex(key).out_edges())
for cond in conds:
if g.vp["shape"][cond] == "LEAF":
label=g.vp["label"][cond]
src_IP=re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", label)
if 'nfsExportInfo' in label:
src_IP=re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", label)[1]
elif len(src_IP)!=0:
src_IP=re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", label)[0]
if src_IP==target_IP:
IP_track=True
break
# attacker = False
newkeys = False
loop = False
w = 0
for oe in g.vertex(key).out_edges():
new = int(oe.target())
edge = (key, new)
if edge in u[1]:
loop = True
else:
u[1].append(edge)
if g.vp["shape"][new] == "OR":
u[0].append(new)
newkeys = True
threshold=threshold+1
if threshold==200000:
break
if loop:
print ("Discard loop path")
elif IP_track:
paths.append([u[1], u[2]])
#print(paths)
IP_track=False
elif len(u[0]) > 0:
q.append((u[0], u[1], w + u[2]))
elif g.vp["shape"][key] == "OR":
for oe in g.vertex(key).out_edges():
new = int(oe.target())
edge = (key, new)
if edge not in u[1]:
q.append((u[0]+[new], u[1]+[(key, new)], 0))
return paths