-
Notifications
You must be signed in to change notification settings - Fork 29
/
testmodelmultinb.py
149 lines (128 loc) · 4.33 KB
/
testmodelmultinb.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
import numpy as np
from sklearn import preprocessing
from sklearn import metrics
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.svm import SVC
from sklearn.externals import joblib
import json
import csv
def test(stri):
bdic= json.load( open( "bdic.json", "rb" ) )
mdic= json.load( open( "mdic.json", "rb" ) )
if stri=='malicious':
flist=[]
data = json.load(open("phish50.json", 'r'))
process=json.load(open("all.json",'r'))
for each in data:
url_length = 0
out_of_place = 0
ip_check = 0
nameservers = 0
location = 0
special_char = 0
tags_count = 0
term=0
for process_data in process:
if process[process_data]["url_length"] == data[each]["url_length"]:
url_length = url_length + 1
if process[process_data]["no_of_out_of_place_features"] == data[each]["no_of_out_of_place_features"]:
out_of_place = out_of_place + 1
if process[process_data]["ip_check"] == data[each]["ip_check"]:
ip_check = ip_check + 1
if process[process_data]["nameservers"] == data[each]["nameservers"]:
nameservers = nameservers + 1
if process[process_data]["location"] == data[each]["location"]:
location = location + 1
if process[process_data]["special_char_count"] == data[each]["special_char_count"]:
special_char = special_char + 1
if process[process_data]["url_length"] == data[each]["url_length"]:
url_length = url_length + 1
if process[process_data]["tags_count"] == data[each]["tags_count"]:
tags_count+=1
lsum=0
liss=data[each]["terms"]
if liss:
for li in liss:
if not li in bdic:
term=-1
elif not li in mdic:
term=1
elif bdic[li]>mdic[li]:
term=1
elif bdic[li]<mdic[li]:
term=-1
else:
term=0
lsum+=term
lis = [url_length,out_of_place,ip_check,nameservers,location,special_char,tags_count,lsum]
lis2= [url_length,out_of_place,ip_check,nameservers,location,special_char,tags_count,lsum,-1]
flist.append(lis2)
print lis
model=joblib.load("trainmodelmultinb.pkl")
print model.predict(lis)
print len(data)
with open("confusion2.csv", "a") as f:
writer = csv.writer(f)
writer.writerows(flist)
if stri=='benign':
flist=[]
data = json.load(open("benign(testing60).json", 'r'))
process =json.load(open("all.json",'r'))
#print data
final_list = []
for each in data:
url_length = 0
out_of_place = 0
ip_check = 0
nameservers = 0
location = 0
special_char = 0
tags_count = 0
term=0
for process_data in process:
if process[process_data]["url_length"] == data[each]["url_length"]:
url_length = url_length + 1
if process[process_data]["no_of_out_of_place_features"] == data[each]["no_of_out_of_place_features"]:
out_of_place = out_of_place + 1
if process[process_data]["ip_check"] == data[each]["ip_check"]:
ip_check = ip_check + 1
if process[process_data]["nameservers"] == data[each]["nameservers"]:
nameservers = nameservers + 1
if process[process_data]["location"] == data[each]["location"]:
location = location + 1
if process[process_data]["special_char_count"] == data[each]["special_char_count"]:
special_char = special_char + 1
if process[process_data]["url_length"] == data[each]["url_length"]:
url_length = url_length + 1
if process[process_data]["tags_count"] == data[each]["tags_count"]:
tags_count+=1
lsum=0
liss=data[each]["terms"]
if liss:
for li in liss:
if not li in bdic:
term=-1
elif not li in mdic:
term=1
elif bdic[li]>mdic[li]:
term=1
elif bdic[li]<mdic[li]:
term=-1
else:
term=0
lsum+=term
lis = [url_length,out_of_place,ip_check,nameservers,location,special_char,tags_count,lsum]
lis2=[url_length,out_of_place,ip_check,nameservers,location,special_char,tags_count,lsum,1]
flist.append(lis2)
print each
print lis
model=joblib.load("trainmodelmultinb.pkl")
print model.predict(lis)
print len(data)
with open("confusion.csv", "a") as f:
writer = csv.writer(f)
writer.writerows(flist)
test("benign")