-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpgws.py
337 lines (285 loc) · 12.2 KB
/
fpgws.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
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
337
'''
Created on 2017年5月12日
@author: heguofeng
'''
import pickle
import json
from webshell.freqitems import getKeywordsByApriori, FPGrowth
from webshell.basetool import ObservesState, getDataFromFile, saveDatatoFile
import time
import getopt
import sys
class FPGrowthWebShell(object):
'''
classdocs
'''
def __init__(self,fpgkeywordsDict={},obsmode = 0,minSupport = 4):
'''
Constructor
fpgkeywordsDict= [{kw1_1,...kw1_n}:[prob,used],... {kw2_1,...kw2_n}:[prob,used]]
'''
self.keywordsDict = fpgkeywordsDict
self.obs = ObservesState(mode = obsmode)
self.minSupport = minSupport
def loadModel(self, filename="keywords.pkl", code="pickle"):
if code == "json":
fr = open(filename, 'r')
txt = fr.read()
kwsl = json.loads(txt)["keywordsDict"]
self.keywordsDict = dict(map(lambda kw:(frozenset(kw[0]),kw[1]),kwsl))
elif code == "pickle":
fr = open(filename, 'rb')
data = pickle.load(fr)
self.keywordsDict = data["keywordsDict"]
# data = pickle.load(fr)
# self.keywordsList = data[keywordsList]
fr.close()
def saveModel(self, filename="keywords.pkl", code="pickle"):
'''
json format
{"keywordsDict": [
[['HOME', 'C'],[16, 0]]
...
[['HOME', 'C'],[16, 0]]
]
}
pickle format:
{"keywordsDict": [
[frozenset('HOME', 'C'):[16, 0]]
...
[frozenset('HOME', 'C'):[16, 0]]
]
}
'''
data = {
"keywordsDict": self.keywordsDict,
}
if code == "json":
kwsl = list(map(lambda kw:(list(kw[0]),kw[1]),self.keywordsDict.items()))
txt = json.dumps({"keywordsDict": kwsl})
txt = txt.encode('utf-8').decode('unicode-escape')
fw = open(filename, 'w')
fw.write(txt)
elif code == "pickle":
fw = open(filename, 'wb')
pickle.dump(data, fw)
fw.close()
def loadDataFromFile(self,filename,filterfunction=lambda r:r[1]=='w'):
records=getDataFromFile(filename,filterfunction)
dataSet = []
for record in records:
dataSet.append(self.obs.getObservesFromRecord(record))
print("record count",len(dataSet))
return dataSet
def moreTrain(self,inputfilename="",statesfilename=""):
self.needReCalculate = True
def train(self,inputfilename="",statesfilename="",modelfilename = ""):
'''
if has statesfilename will use states
if have modelfilename will add train
'''
print(time.time())
if len(statesfilename) != 0:
listrecords = getDataFromFile(statesfilename)
ids = list(map(lambda r:r[0],listrecords))
filterfunction = lambda r:r[0] in ids
else:
filterfunction = lambda r:r[1]=='w'
myFPGrowth = FPGrowth(minSupport=self.minSupport)
records = self.loadDataFromFile(inputfilename,filterfunction=filterfunction)
if len(modelfilename) == 0:
myFPGrowth.loadData(records)
else:
self.loadModel(modelfilename, code = "pickle")
dataset = dict(map(lambda kws: (kws[0],kws[1][0]),self.keywordsDict.items()))
dataset.update(myFPGrowth.createInitSet(list(map(lambda r : self.obs.getObservesFromRecord(r),records))))
myFPGrowth.loadDataSet(dataset)
print("itemcount:",len(myFPGrowth.getItems(myFPGrowth.dataSet)))
myFPGrowth.createTree()
#myFPGrowth.rootTree.disp(1)
#HeadNode.disp(myFPGrowth.headerTable)
print("headlen",len(myFPGrowth.headerTable))
freqItems = myFPGrowth.getFreqItems()
print("getFreqItems:",len(freqItems))
self.keywordsDict = dict(sorted(map(lambda fi:(frozenset(fi[0]),[fi[1],0]),freqItems),key=lambda wk:len(wk[0]),reverse = True))
#print(self.keywordsDict)
print(len(self.keywordsDict))
self.verify(inputfilename, statesfilename)
print("after verify",len(self.keywordsDict))
self.saveModel("keywords.txt", code = "json")
return self.keywordsDict
def addKeywords(self,kwsfilename):
''' just like model file
{"keywordsDict": [
[['HOME', 'C'],[16, 0]]
...
[['HOME', 'C'],[16, 0]]
]
'''
fr = open(kwsfilename, 'r')
txt = fr.read()
kwsl = json.loads(txt)["keywordsDict"]
self.keywordsDict.update(dict(map(lambda kw:(frozenset(kw[0]),kw[1]),kwsl)))
# kwsfile = open(kwsfilename,"rt")
# kwslines = kwsfile.readlines()
# kwsfile.close()
# for line in kwslines:
# keywords = re.findall(r'\{(.*?)\}',line)
# probs = re.findall(r'\[(.*?)\]',line)
pass
def verify(self,inputfilename,statesfilename=""):
if len(statesfilename) != 0:
listrecords = getDataFromFile(statesfilename)
ids = list(map(lambda r:r[0],listrecords))
filterfunction = lambda r:r[0] in ids
states = listrecords
else:
filterfunction = lambda r:True
states = getDataFromFile(inputfilename,filterfunction=filterfunction)
records = getDataFromFile(inputfilename,filterfunction=filterfunction)
keywordsList = sorted(self.keywordsDict.keys(),key=lambda k:len(k),reverse=True)
#print(keywordsList)
for i in range(len(records)):
observes = self.obs.getObservesFromRecord(records[i])
state = 'n'
for kws in keywordsList:
if kws.issubset(observes):
if states[i][1]=='n':
#print("%s can't correct tagto n! obs %s,remove %s"%(records[i][0],str(observes),str(kws)))
self.keywordsDict[kws][1] = -1
else:
state = 'w'
self.keywordsDict[kws][1] += 1
break
for kws in list(self.keywordsDict.keys()):
if self.keywordsDict[kws][1] == -1:
del self.keywordsDict[kws]
keywordsList.remove(kws)
if states[i][1]!= state:
print("%s can't correct tagto %s ! obs %s"%(records[i][0],states[i][1],str(observes)))
for kws in list(self.keywordsDict.keys()):
if self.keywordsDict[kws][1] == 0:
del self.keywordsDict[kws]
keywordsList.remove(kws)
def predict(self,observes,verbose = False):
try:
obsSet=set(observes)
keywordsList = sorted(self.keywordsDict.keys(),key=lambda k:len(k),reverse=True)
for kws in keywordsList:
if kws.issubset(obsSet):
if verbose:
print("obs %s match keywords:%s"%(observes,kws))
return 'w',1,kws
return 'n',1,set([])
except:
return 'n',0.5,set([])
def trainWebShellByFPGrowth(inputfilename,modelfilename="keywords.pkl",statefilename="result.txt"):
#minsup=[25,20,10,6,4]
minsup=[10]
for i in range(len(minsup)):
print("minsup",minsup[i])
starttime=time.time()
fpgws = FPGrowthWebShell(obsmode= 0,minSupport= minsup[i])
fpgws.train(inputfilename, statefilename)
fpgws.saveModel(modelfilename, code="pickle")
print("end of trainWebShellByFPGrowth use %f second. "%(time.time()-starttime))
def trainWebShellByFPGrowth2(inputfilename,modelfilename="keywords.pkl",statefilename="result.txt"):
starttime=time.time()
fpgws = FPGrowthWebShell(obsmode = 0)
fpgws.loadModel(modelfilename, code="pickle")
print("end of trainWebShellByFPGrowth use %f second. "%(time.time()-starttime))
def predictUrlIsWebShellByFPGrowth(modelfilename = "fpgws.pkl",code="pickle"):
# this is for hive transform url
fpgws = FPGrowthWebShell(obsmode = 0)
fpgws.loadModel(modelfilename, code=code)
obs1 = ObservesState(mode = 0)
for url in sys.stdin:
record = ["1","",url]
observes = obs1.getObservesFromRecord(record)
#print(record,observes)
state,prob,kws = fpgws.predict(observes,verbose=False)
print("%s\t%d\t%s"%(state,prob,str(kws)))
def predictWebShellByFPGrowth(inputfilename,modelfilename="keywords.pkl",statefilename="result.txt",idlist=[]):
starttime=time.time()
fpgws = FPGrowthWebShell(obsmode = 0)
fpgws.loadModel(modelfilename, code="pickle")
#fpgws.addKeywords("mykeywords.txt")
statelist = []
stateproblist = []
if len(idlist) == 0 :
records=getDataFromFile(inputfilename)
verbose = False
else:
records=getDataFromFile(inputfilename,lambda r:r[0] in idlist)
verbose = True
print("start find webshell in %s total %d records"%(inputfilename,len(records)))
obs1 = ObservesState(mode = 0)
for record in records:
observes = obs1.getObservesFromRecord(record)
if verbose:
print(record[0],observes)
state,prob,kws = fpgws.predict(observes,verbose=verbose)
statelist.append([record[0],state])
stateproblist.append([record[0],state,prob])
count = saveDatatoFile(statefilename, statelist,lambda r:r[1]=='w')
countprob = saveDatatoFile(statefilename+".prob", stateproblist,lambda r:r[1]=='w')
print("find %d webshells in %s .result saved in %s."%(count,inputfilename,statefilename))
print("end of FPG judge use %f second. "%(time.time()-starttime))
if __name__ == '__main__':
opts,args = getopt.getopt(sys.argv[1:],"hui:o:s:m:l:tpa",
["help","inputfile","outputfile","statefile",
"modelfile","list","train","predict","url"])
if len(opts) == 0:
opts.append(("-h",""))
inputfilename = ""
outputfilename = ""
statefilename = ""
modelfilename = ""
idlist=[]
for op,value in opts:
if op == "-h" or op == "--help" :
print("LinearEquation WebShell version 1.0.by Heguofeng")
print('''
-h --version :help this information.
-i inputfilename
-o outputfilename
-s statefilename
-m modelfilename
-l id1[,id2]
-t :train
-p :predict
-a : apriori find keywords
-f : fp_growth find keywords
-u : url from stdin
''')
if op == "-i" or op == "--input":
inputfilename = value
if op == "-o" or op == "--output":
outputfilename = value
if op == "-s" or op == "--output":
statefilename = value
if op == "-m" or op == "--output":
modelfilename = value
if op == "-l" or op == "--list":
idlist = list(value.strip().split(","))
if op == "-a" or op == "--apriori":
getKeywordsByApriori(
modelfilename = modelfilename,
idlist = idlist )
if op == "-t" or op == "--fpgrowth":
trainWebShellByFPGrowth(
inputfilename = inputfilename,
modelfilename = modelfilename,
statefilename = statefilename,
)
if op == "-p" or op == "--predict":
predictWebShellByFPGrowth(
inputfilename = inputfilename,
modelfilename = modelfilename,
statefilename = statefilename,
idlist = idlist
)
if op == "-u" or op == "--url":
predictUrlIsWebShellByFPGrowth(
modelfilename = modelfilename,
)