-
Notifications
You must be signed in to change notification settings - Fork 1
/
mtdsim.m
236 lines (195 loc) · 7.2 KB
/
mtdsim.m
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
clc;clear all;close all;
format bank
global nodenum
global nodeos
global nodeippool
global nodevulnum
global vullevel
global edge
global Gsr
global attview
global defview
global attiniip
global attendip
global attgoal
global crafttime
global attweaponresult
global ipbase
global usrview
global filesizeleave
global usrchange
global usrhold
global usrcost
global nodes
global nodeservice
global maxhostchangedowntime
nodenum=1;
nodeippool=20;
nodeos=['L','W','U'];
nodeservice=['F','H','M'];
nodevulnum=3;
vullevel=['C','S','V'];
crafttime=[10,30,50];%攻击工具制作时间,根据漏洞威胁程度分为10,30,50个逻辑时间T
%Gsr=[node;edge];
attview = [];
defview = [];
attgoal=[];
ipchangfrequency=10;
hostchangfrequency=50;
maxhostchangedowntime=5;
attphase=7;%攻击阶段分为7个,侦查、武器化、运输、漏洞利用开发、安装、C2C和AoO
Maxsimtime=100;
Simatttimes=zeros(attphase+1,Maxsimtime);
Simusrchange=zeros(1,Maxsimtime);
Simusrhold=zeros(1,Maxsimtime);
Simusrcost=zeros(1,Maxsimtime);
Simattwinstate=cell(1,Maxsimtime);
Maxipfrequency=300;
Attipreconn=zeros(1,Maxipfrequency);
% for ipfsimtimes=1:Maxipfrequency
% ipchangfrequency=ipfsimtimes;
Simtime=Maxsimtime;
filesize=2048;%传输文件大小
while Simtime>0
T=1;
nodes=nodeini('sample');
usrview = nodes;
defview=Gsr;
attiniip=0;
attendip=255;
attview.ip=attiniip;
filesizeleave=filesize;
wholewin=false;
atttimes=zeros(1,attphase+1);
attstarttimes=zeros(1,attphase);
attweaponresult=-1;
usrchange=0;
usrhold=0;
usrcost=0;
attwinstate=zeros(1,attphase);
attwinstate(1,1)=false;
usrfileend=false;
sumattwinstate=[];
while wholewin ~= true
% if mod(T,ipchangfrequency)==0
if mod(T,ipchangfrequency)==0
nodes=defmove('ip');%防御者动作
% usrview = nodes;
end
if mod(T,hostchangfrequency)==0
nodes=defmove('host-random');
end
sysmove('hostchangeup',T);
if filesizeleave<=0
usrfileend=true;
else
filesizeleave = usrmove('ftp',filesizeleave,1);%系统用户动作
end
if ~attwinstate(1,1)
attreconnresult = attmove('reconn');%攻击者侦查动作
if attreconnresult
attwinstate(1,1)=true;
atttimes(1,1)=atttimes(1,1)+1;
else
atttimes(1,1)=atttimes(1,1)+1;
end
end
if attwinstate(1,1) && ~attwinstate(1,2)
attweaponresult = attmove('weapon',attweaponresult);%攻击者攻击工具制作
if attweaponresult==0
attwinstate(1,2)=true;
atttimes(1,2)= atttimes(1,2);
else
atttimes(1,2)= atttimes(1,2)+1;
end
end
if attwinstate(1,1) && attwinstate(1,2) && ~attwinstate(1,3)
attdeliveryresult = attmove('delivery',0,1);%攻击者发送攻击工具
if attdeliveryresult
attwinstate(1,3)=true;
atttimes(1,3)=atttimes(1,3)+1;
else
attwinstate(1,1)=false;
%attweaponresult=-1;
atttimes(1,3)=atttimes(1,3)+1;
end
end
if attwinstate(1,1) && attwinstate(1,2) && attwinstate(1,3) && ~attwinstate(1,4)
attexploitresult = attmove('exploit',0,1);%攻击者开发利用系统漏洞
if attexploitresult
attwinstate(1,4)=true;
atttimes(1,4)=atttimes(1,4)+1;
else
attwinstate(1,1)=false;
attwinstate(1,2)=false;
attweaponresult=-1;
attwinstate(1,3)=false;
atttimes(1,4)=atttimes(1,4)+1;
end
end
if attwinstate(1,1) && attwinstate(1,2) && attwinstate(1,3) && attwinstate(1,4) && ~attwinstate(1,5)
attinstallresult = attmove('install',0,1);%攻击者安装相应攻击软件
if attinstallresult
attwinstate(1,5)=true;
atttimes(1,5)=atttimes(1,5)+1;
else
attwinstate(1,1)=false;
attwinstate(1,2)=false;
attweaponresult=-1;
attwinstate(1,3)=false;
attwinstate(1,4)=false;
atttimes(1,5)=atttimes(1,5)+1;
end
end
if attwinstate(1,1) && attwinstate(1,2) && attwinstate(1,3) && attwinstate(1,4) && attwinstate(1,5) && ~attwinstate(1,6)
attc2cresult = attmove('c2c',0,1);%攻击者实施命令控制
if attc2cresult
attwinstate(1,6)=true;
atttimes(1,6)=atttimes(1,6)+1;
else
attwinstate(1,1)=false;
attwinstate(1,2)=false;
attweaponresult=-1;
attwinstate(1,3)=false;
attwinstate(1,4)=false;
attwinstate(1,5)=false;
atttimes(1,6)=atttimes(1,6)+1;
end
end
%usrfileend &&
if attwinstate(1,1) && attwinstate(1,2) && attwinstate(1,3) && attwinstate(1,4) && attwinstate(1,5) && attwinstate(1,6)
wholewin=true;
atttimes(1,attphase+1)=T;
end
sumattwinstate=[sumattwinstate;attwinstate];
% astrans('ias',T);
% astrans('eas',T);
T=T+1;
end
Simusrchange(1,Simtime)=usrchange;
Simusrcost(1,Simtime)=usrcost;
Simusrhold(1,Simtime)=usrhold;
for phase=1:(attphase+1)
Simatttimes(phase,Simtime)=atttimes(1,phase);
end
Simattwinstate{1,Simtime}=sumattwinstate;
Simtime=Simtime-1;
end
% attipreconneffort=(size(find(Simatttimes(1,:)>256),2))/Maxsimtime;
% Attipreconn(1,ipchangfrequency)=attipreconneffort;
% end
% xdata=1:Maxipfrequency;
% f=fit(xdata',Attipreconn(1,:)','exp2');
% plot(f,'fit');
% hold on
% plot(Attipreconn,'.');
attipreconneffort=(size(find(Simatttimes(1,:)>256),2))/Maxsimtime;
usreffcost=(Simusrchange+Simusrhold)./(Simusrcost+Simusrchange+Simusrhold);
figure(2);
histogram(Simatttimes(1,:));
ylabel('Frequency');
xlabel('Number of Attacks')
minatt=min(Simatttimes(1,:))
meanatt=mean(Simatttimes(1,:))
maxatt=max(Simatttimes(1,:))
meanusr=mean(usreffcost)