-
Notifications
You must be signed in to change notification settings - Fork 0
/
phishing_detection.py
165 lines (148 loc) · 7.22 KB
/
phishing_detection.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
import numpy as np
import pandas as pd
import feature_extraction
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from flask import jsonify
from flask import Flask, request, render_template
import numpy as np
import pandas as pd
from sklearn import metrics
import warnings
import pickle
from decimal import Decimal
warnings.filterwarnings('ignore')
from feature import FeatureExtraction
def getResult(url,method):
print(url,"-----------------------------------------")
#Importing dataset
data = pd.read_csv('Dataset\dataset.csv',delimiter=",")
#Seperating features and labels
X = np.array(data.iloc[: , :-1])
y = np.array(data.iloc[: , -1])
#print(type(X))
#Seperating training features, testing features, training labels & testing labels
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
# classifier = RandomForestClassifier()
if method=="logisticregression":
classifier = LogisticRegression()
classifier.fit(X_train, y_train)
score = classifier.score(X_test, y_test)
score = score*100
print(score,"::::::::::::score")
X_new = []
X_input = url
X_new=feature_extraction.generate_data_set(X_input)
X_new = np.array(X_new).reshape(1,-1)
analysis_result = ""
try:
prediction = classifier.predict(X_new)
print(prediction)
print("------------------------------")
if prediction == -1:
analysis_result = "Phishing URL"
elif prediction == 0:
analysis_result = "This website has been detected as Suspecious"
else:
analysis_result = "This website has been detected as Legitimate URL"
except Exception as a:
print(a)
analysis_result = "This website has been detected as Phishing URL"
result_of_analysis = """<section class="iq-about overview-block-pt iq-hide">
<div class="container">
<div class="row align-items-end">
<div class="col-lg-8 col-md-12">
<div class="about-content">
<h1 class="text-about iq-tw-6">Result of Your URL : <span class="iq-font-green iq-fw-8">"""+url+"""</span></h1>
<ul class="listing-mark iq-mtb-20 iq-tw-6 iq-font-black">
<li class="good">"""+analysis_result+"""</li>
</ul>
<h5 class="iq-mt-20 iq-mb-20" style="color: #65d972;font-size: 16px;">Accuracy : """+str(score)+"""div>
</div>
</div></h5>
</
</div>
</section>
"""
dictt={"prediction":analysis_result,
"res":prediction,
"score":score,
"url":url
}
return dictt
else:
classifier = RandomForestClassifier()
classifier.fit(X_train, y_train)
score = classifier.score(X_test, y_test)
score = score*100
print(score,"::::::::::::score")
X_new = []
X_input = url
X_new=feature_extraction.generate_data_set(X_input)
X_new = np.array(X_new).reshape(1,-1)
analysis_result = ""
try:
prediction = classifier.predict(X_new)
print(prediction)
if prediction == -1:
analysis_result = "Phishing URL"
elif prediction == 0:
analysis_result = "This website has been detected as Suspecious"
else:
analysis_result = "This website has been detected as Legitimate URL"
except Exception as a:
analysis_result = "This website has been detected as Phishing URL"
result_of_analysis = """<section class="iq-about overview-block-pt iq-hide">
<div class="container">
<div class="row align-items-end">
<div class="col-lg-8 col-md-12">
<div class="about-content">
<h1 class="text-about iq-tw-6">Result of Your URL : <span class="iq-font-green iq-fw-8">"""+url+"""</span></h1>
<ul class="listing-mark iq-mtb-20 iq-tw-6 iq-font-black">
<li class="good">"""+analysis_result+"""</li>
</ul>
<h5 class="iq-mt-20 iq-mb-20" style="color: #65d972;font-size: 16px;">Accuracy : """+str(score)+"""div>
</div>
</div></h5>
</
</div>
</section>
"""
# print(result_of_analysis,"resssssssssssssssssssssult")
dictt={"prediction":analysis_result,
"res":prediction,
"score":score,
"url":url
}
print("-----------------------------------------------")
print(dictt)
# print(result_of_analysis,"resssssssssssssssssssssult")
return dictt
def getResult2(url,modelname):
if modelname=="Decision tree":
file = open("pickle/model.pkl","rb")
tree = pickle.load(file)
file.close()
obj = FeatureExtraction(url)
x = np.array(obj.getFeaturesList()).reshape(1,30)
y_predc =tree.predict(x)[0]
print(y_predc)
# a=tree.predict_proba(x)[0,0]
# print(a)
if modelname=="Gradient":
obj = FeatureExtraction(url)
x = np.array(obj.getFeaturesList()).reshape(1,30)
file = open("picle/best.pkl","rb")
gbc = pickle.load(file)
file.close()
y_pred =gbc.predict(x)[0]
# y_pro_malicious = gbc.predict_proba(x)[0,0]
y_pro_non_malicious = gbc.predict_proba(x)[0,1]
y_pro_malicious= Decimal('1') - Decimal(str(y_pro_non_malicious))
y_pro_malicious=float(y_pro_malicious)
print(y_pro_malicious)
if y_pro_malicious>0.50:
print("it is a malicious url")
else:
print("it is a legitimate url")