Spaces:
Running
Running
File size: 8,900 Bytes
62dcfec |
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 6 22:13:12 2022
@author: amaer
"""
import numpy as np
import copy
from ModelTEC import *
import pickle
from threading import Thread
class Graph:
def __init__(self,root,name=None,idtactic=0):
self.Root=root
self.Name=name
self.Idtactic=idtactic
self.Tactics=[]
#........................
def Filter(self):
for i in range(self.Idtactic,len(Scenoariotree.Basetree)):
self.Tactics.append(copy.deepcopy(Scenoariotree.Basetree[i]))
#........................
def Run(self,path="",state=" ",Cinput="",listAttackGod=[],numstop=-1):
self.Filter()
for tactic in self.Tactics:
tactic.Run(self.Root,path=path+"=> "+tactic.Name,state=state,Cinput=Cinput,listAttackGod=listAttackGod,numstop=numstop)
# thread=Thread(target = tactic.Run, args = (self.Root,path+"->"+tactic.Name))
# thread.start()
# thread.join()
#........................
class Technique:
PreprocessingFunc=None
ExtractFeaturesFunc=None
objFile=None
Counter=0
Cinput=' '
listAttackGod=[]
PrintScenoario=print
def __init__(self,name,description,weight=None):
self.Name=name
self.Description=description
self.Weight=weight
self.CleanDescription=None
self.FeaturesDescription=None
self.Graph=None
#........................................
def Preprocessing(self):
if Technique.PreprocessingFunc:
return Technique.PreprocessingFunc([self.Description])
return self.Description
#........................................
def ExtractFeatures(self):
if Technique.ExtractFeaturesFunc:
return Technique.ExtractFeaturesFunc(self.CleanDescription if self.CleanDescription else self.Description)
return np.zeros((1,10))
#........................................
def Fit(self):
self.CleanDescription=self.Preprocessing()
#self.FeaturesDescription=self.ExtractFeatures()
#........................................
def Run(self,id,path="",state=" ",Cinput="",listAttackGod=[],numstop=-1):
if (numstop ==-1 or len(listAttackGod)<numstop):
if id<len(Scenoariotree.Basetree) :
# Technique.objFile.write("=")
# print('=',end='')
path=path+" *-> "+self.Name+" "
row=Technique.PrintScenoario(path,Cinput)
if row is not None:
listAttackGod.append(row)
self.Graph=Graph(root=self,name=self.Name,idtactic=id+1)
self.Graph.Run(path=path,state=state,Cinput=Cinput,listAttackGod=listAttackGod,numstop=numstop)
else :
try:
row=Technique.PrintScenoario(path,Cinput)
if row is not None:
listAttackGod.append(row)
except:
pass
#........................................
def __str__(self):
return f"(Name: {self.Name} , Weights: {self.Weight})"
class Tactic():
dataMS=None
WF="thrshold"
def __init__(self,name,id=0,description=None,weight=None,techniques=None,wfilter="thrshold"):
self.Name=name
self.Id=id
self.Weight=weight
self.Techniques=techniques if techniques else []
#........................................
def Filter1(self,root,path=""):
i=0
while i<len(self.Techniques):
sim=Tactic.dataMS[root.Name][self.Techniques[i].Name]
if sim>=Scenoariotree.ThresholdTechnique:
self.Techniques[i].Weight=sim
else:
# row=Technique.PrintScenoario(path,Technique.Cinput)
# if row is not None:
# Technique.listAttackGod.append(row)
# Technique.Counter+=1
self.Techniques.remove(self.Techniques[i])
i-=1
i+=1
#........................................
def Filter2(self,root,path=""):
listd=[]
max=0
for i in range(len(self.Techniques)):
sim=Tactic.dataMS[root.Name][self.Techniques[i].Name]
if sim>max and sim>=Scenoariotree.ThresholdTechnique:
listd=[self.Techniques[i]]
max=sim
# Scenoariotree.CurrentSoft.predict()
self.Techniques=listd
#........................................
def Filter(self,root,path=""):
if Tactic.WF=="thrshold":
self.Filter1(root,path)
else:
self.Filter2(root,path)
def Run(self,root,path="",state="",Cinput="",listAttackGod=[],numstop=-1):
self.Filter(root,path)
for technique in self.Techniques:
technique.Run(self.Id,path=path,state=state,Cinput=Cinput,listAttackGod=listAttackGod,numstop=numstop)
# thread=Thread(target=technique.Run,args=(self.Id,path))
# thread.start()
# thread.join()
#........................................
def __str__(self):
return f"(Name: {self.Name}, Weight: {self.Weight})"
from BasePath import BASEPATH
class Scenoariotree:
obTEC=None
obTECSodft=None
ThresholdTechnique=0.3
ThresholdTactic=0.3
Basetree=None
CurrentSoft=None
indecisT={}
CurrentInput=''
#....................................................
#....................................................
def __init__(self,file1,file2,Ntactics=None):
self.TacticsOrder=pickle.load(open(BASEPATH+file1,'rb'))
self.indecisT=pickle.load(open(BASEPATH+file2,'rb'))
self.Ntactics=len(self.TacticsOrder)
self.nlp=None#spacy.load('en_core_web_lg')
"""
"""
def setModels(self,ObTEC=None,ObTECSodft=None,obchk=None,isForm=False):
self.obTEC=ObTEC
self.obTECSodft=ObTECSodft
self.obTEC.ChangeModel(ob="svmK")
self.obTECSodft.ChangeModel(ob="svmK")
self.obchk=obchk
Technique.PrintScenoario=obchk.print_table if isForm==False else obchk.process
#....................................................
def InitBasetree(self):
tree=[]
i=0
for key in self.TacticsOrder:
tactic=key
tree.append(Tactic(name=tactic,id=i))
group=self.TacticsOrder[key]
i+=1
for j in range(len(group)):
tree[-1].Techniques.append(Technique(name=group[j],
description=" ",
))
return tree
#..............................................
def getordertactics(self):
return ''
def Fit(self,file="dataSM.pkl",wtype='ALL'):
Tactic.dataMS=pickle.load(open(BASEPATH+file,'rb'))
Scenoariotree.Basetree=self.InitBasetree()
# Scenoariotree.Basetree[6].Techniques.append(Technique(name="Exploitation of Remote Services",description="Exploitation of Remote Services")
#..............................................
def Predict(self,Description,ThresholdTechnique=0.5,ThresholdTactic=0.0,WF="thrshold",istrans=False,numstop=-1):
Technique.Counter=0
Technique.listAttackGod=[]
self.obchk.inintlist()
Tactic.WF=WF
Scenoariotree.ThresholdTechnique=ThresholdTechnique
Scenoariotree.ThresholdTactic=ThresholdTactic
if istrans:
Description=[self.obTEC.to_tran(Description)]
tech=self.obTEC.predict(Description)
if tech!="No":
print('-------------------------------------------')
print('Technique : ', tech)
tact=self.indecisT[tech]
print(' Tactic :',tact)
self.obTECSodft.ChangeModel(ob="svmK")
CurrentSoft=self.obTECSodft.predict(Description+[" "+tech])
print('input same as Software is : ',CurrentSoft)
self.obTECSodft.ChangeModel(ob="svmL")
print('-------------------------------------------')
print('---------------------senarios----------------------')
listofs=[]
self.Root=Technique(name=tech,description=Description)
idt=-1
for key in self.TacticsOrder:
if key==tact:
break
idt+=1
self.Root.Run(id=idt+1,path="=> "+tact,state="",Cinput=Description[0],listAttackGod=listofs,numstop=numstop)
if idt==self.Ntactics-1:
self.obchk.process(tact+" *-> "+ tech,Description[0],isclean=False)
else :
print( "no found any thing........")
return listofs
#..............................................
|