Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2020-092
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2020 - 092
2020-092
Commits
9b9c81dd
Commit
9b9c81dd
authored
Oct 18, 2020
by
Haritha Chanuka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload application.py File
parent
e1dfbb33
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
295 additions
and
0 deletions
+295
-0
WANHEDA Server/application.py
WANHEDA Server/application.py
+295
-0
No files found.
WANHEDA Server/application.py
0 → 100644
View file @
9b9c81dd
from
datetime
import
datetime
from
flask
import
Flask
,
jsonify
,
request
,
make_response
from
flask_restful
import
Resource
,
Api
from
flask_cors
import
CORS
,
cross_origin
from
flask_pymongo
import
PyMongo
import
pickle
import
pandas
as
pd
import
numpy
as
np
app
=
Flask
(
__name__
)
app
.
config
[
'JSON_AS_ASCII'
]
=
False
cors
=
CORS
(
app
)
app
.
config
[
'CORS_HEADERS'
]
=
'Content-Type'
app
.
config
[
'SECRET_KEY'
]
=
'thisisthesecretkey'
app
.
config
[
'MONGO_URI'
]
=
'mongodb://haritha:haritha123@cluster0-shard-00-00.wb9fh.mongodb.net:27017,cluster0-shard-00-01.wb9fh.mongodb.net:27017,cluster0-shard-00-02.wb9fh.mongodb.net:27017/WANHEDA?ssl=true&replicaSet=atlas-80gv2b-shard-0&authSource=admin&retryWrites=true&w=majority'
mongo
=
PyMongo
(
app
)
# Feature Scaling
from
sklearn.preprocessing
import
LabelEncoder
,
OneHotEncoder
from
sklearn.compose
import
ColumnTransformer
mobileBotNet
=
pickle
.
load
(
open
(
'models/mobileBotNet.sav'
,
'rb'
))
slowloris
=
pickle
.
load
(
open
(
'models/slowloris.sav'
,
'rb'
))
ntpamplification
=
pickle
.
load
(
open
(
'models/ntpamplification.sav'
,
'rb'
))
@
app
.
route
(
'/'
,
methods
=
[
'GET'
])
def
call_api
():
return
jsonify
(
"Welcome to WANHEADA"
)
@
app
.
route
(
'/mobilebotnet/predict'
,
methods
=
[
'POST'
])
def
mobilebotnet_predict
():
source
=
request
.
json
[
'source'
]
protocol
=
request
.
json
[
'protocol'
]
X
=
pd
.
DataFrame
({
'Source'
:
source
,
'Protocol'
:
protocol
},
index
=
[
4
])
X
=
X
.
to_numpy
()
labelencoder_X_T
=
LabelEncoder
()
X
[:,
0
]
=
labelencoder_X_T
.
fit_transform
(
X
[:,
0
])
labelencoder_X_S
=
LabelEncoder
()
X
[:,
1
]
=
labelencoder_X_S
.
fit_transform
(
X
[:,
1
])
result
=
mobileBotNet
.
predict
(
X
)
if
(
int
(
result
[
0
])
==
0
):
notification
=
mongo
.
db
.
notification
notification
.
insert
({
'ip'
:
source
,
'message'
:
'Mobile botnet attack. Blocked by WANHEDA'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
(
int
(
result
[
0
]))
@
app
.
route
(
'/volumetrice/predict'
,
methods
=
[
'POST'
])
def
volumetrice_predict
():
data
=
request
.
json
X
=
pd
.
DataFrame
({
'Time'
:
400.393403
,
'Source'
:
"192.162.78.90"
,
'Destination'
:
"122.142.8.10"
,
'Protocol'
:
"ARP"
,
'Length'
:
412
},
index
=
[
4
])
source
=
request
.
json
[
'source'
]
protocol
=
request
.
json
[
'protocol'
]
X
=
X
.
to_numpy
()
labelencoder_X_T
=
LabelEncoder
()
X
[:,
0
]
=
labelencoder_X_T
.
fit_transform
(
X
[:,
0
])
labelencoder_X_S
=
LabelEncoder
()
X
[:,
1
]
=
labelencoder_X_S
.
fit_transform
(
X
[:,
1
])
labelencoder_X_D
=
LabelEncoder
()
X
[:,
2
]
=
labelencoder_X_D
.
fit_transform
(
X
[:,
2
])
labelencoder_X_P
=
LabelEncoder
()
X
[:,
3
]
=
labelencoder_X_P
.
fit_transform
(
X
[:,
3
])
result
=
mobileBotNet
.
predict
(
data
)
if
(
int
(
result
[
0
])
==
0
):
notification
=
mongo
.
db
.
notification
notification
.
insert
(
{
'ip'
:
source
,
'message'
:
'Volumetrice attack. Blocked by WANHEDA'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
(
result
)
@
app
.
route
(
'/slowloris/predict'
,
methods
=
[
'POST'
])
def
slowloris_predict
():
source
=
request
.
json
[
'source'
]
flow_duration
=
request
.
json
[
'flow_duration'
]
bwd_packets
=
request
.
json
[
'bwd_packets'
]
X
=
pd
.
DataFrame
({
'Flow Duration'
:
flow_duration
,
'Total Length of Bwd Packets'
:
bwd_packets
},
index
=
[
4
])
result
=
slowloris
.
predict
(
X
)
if
(
int
(
result
[
0
])
<
0.5
):
notification
=
mongo
.
db
.
notification
notification
.
insert
({
'ip'
:
source
,
'message'
:
'Slowloris attack. Blocked by WANHEDA'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
(
int
(
result
[
0
]))
@
app
.
route
(
'/ntpAmplification/predict'
,
methods
=
[
'POST'
])
def
ntpAmplification_predict
():
Source
=
request
.
json
[
'source'
]
Source_Port
=
request
.
json
[
'source_port'
]
Destination_Port
=
request
.
json
[
'destination_port'
]
X
=
pd
.
DataFrame
({
'Source Port'
:
Source_Port
,
'Destination Port'
:
Destination_Port
},
index
=
[
4
])
result
=
ntpamplification
.
predict
(
X
)
if
(
int
(
result
[
0
])
==
0
):
notification
=
mongo
.
db
.
notification
notification
.
insert
({
'ip'
:
Source
,
'message'
:
'NtpAmplification attack. Blocked by WANHEDA'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
(
int
(
result
[
0
]))
@
app
.
route
(
'/whitelist/check'
,
methods
=
[
'POST'
])
def
whitelist_check
():
ip
=
request
.
json
[
'ip'
]
whitelist
=
mongo
.
db
.
whitelist
s
=
whitelist
.
find_one
({
'ip'
:
ip
})
if
s
:
output
=
{
'ip'
:
s
[
'ip'
]}
else
:
output
=
"false"
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/whitelist/getall'
,
methods
=
[
'GET'
])
def
get_all_whitelist
():
whitelist
=
mongo
.
db
.
whitelist
output
=
[]
for
s
in
whitelist
.
find
():
output
.
append
({
'ip'
:
s
[
'ip'
]})
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/whitelist/add'
,
methods
=
[
'POST'
])
def
add_to_whitelist
():
whitelist
=
mongo
.
db
.
whitelist
ip
=
request
.
json
[
'ip'
]
ip_id
=
whitelist
.
insert
({
'ip'
:
ip
})
new_ip
=
whitelist
.
find_one
({
'_id'
:
ip_id
})
output
=
{
'ip'
:
new_ip
[
'ip'
]}
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/whitelist/delete'
,
methods
=
[
'POST'
])
def
delete_from_whitelist
():
ip
=
request
.
json
[
'ip'
]
whitelist
=
mongo
.
db
.
whitelist
s
=
whitelist
.
find_one
({
'ip'
:
ip
})
if
s
:
ip_id
=
whitelist
.
delete_one
({
'ip'
:
ip
})
output
=
{
'ip'
:
ip
}
else
:
output
=
"false"
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/blacklist/check'
,
methods
=
[
'POST'
])
def
blacklist_check
():
ip
=
request
.
json
[
'ip'
]
blacklist
=
mongo
.
db
.
blacklist
s
=
blacklist
.
find_one
({
'ip'
:
ip
})
if
s
:
notification
=
mongo
.
db
.
notification
notification
.
insert
({
'ip'
:
ip
,
'message'
:
'Black listed ip address tried to access the protected server.'
,
'time'
:
datetime
.
datetime
.
now
()})
output
=
{
'ip'
:
s
[
'ip'
]}
else
:
output
=
"false"
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/blacklist/getall'
,
methods
=
[
'GET'
])
def
get_all_blacklist
():
blacklist
=
mongo
.
db
.
blacklist
output
=
[]
for
s
in
blacklist
.
find
():
output
.
append
({
'ip'
:
s
[
'ip'
]})
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/blacklist/add'
,
methods
=
[
'POST'
])
def
add_to_blacklist
():
blacklist
=
mongo
.
db
.
blacklist
ip
=
request
.
json
[
'ip'
]
ip_id
=
blacklist
.
insert
({
'ip'
:
ip
})
new_ip
=
blacklist
.
find_one
({
'_id'
:
ip_id
})
output
=
{
'ip'
:
new_ip
[
'ip'
]}
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/blacklist/delete'
,
methods
=
[
'POST'
])
def
delete_from_blacklist
():
ip
=
request
.
json
[
'ip'
]
blacklist
=
mongo
.
db
.
whitelist
s
=
blacklist
.
find_one
({
'ip'
:
ip
})
if
s
:
ip_id
=
blacklist
.
delete_one
({
'ip'
:
ip
})
output
=
{
'ip'
:
ip
}
else
:
output
=
"false"
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/user/register'
,
methods
=
[
'POST'
])
def
add_user
():
user
=
mongo
.
db
.
user
username
=
request
.
json
[
'username'
]
email
=
request
.
json
[
'email'
]
password
=
request
.
json
[
'password'
]
s
=
user
.
find_one
({
'email'
:
email
})
if
s
:
return
jsonify
({
'result'
:
"false"
})
user_id
=
user
.
insert
({
'username'
:
username
,
'email'
:
email
,
'password'
:
password
})
new_user
=
user
.
find_one
({
'_id'
:
user_id
})
output
=
{
'username'
:
new_user
[
'username'
],
'email'
:
new_user
[
'email'
],
'password'
:
new_user
[
'password'
]}
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/user/login'
,
methods
=
[
'POST'
])
def
user_login
():
print
(
request
.
json
)
email
=
request
.
json
[
'email'
]
password
=
request
.
json
[
'password'
]
user
=
mongo
.
db
.
user
s
=
user
.
find_one
({
'email'
:
email
})
if
s
:
if
(
password
==
s
[
'password'
]):
output
=
{
'username'
:
s
[
'username'
],
'password'
:
s
[
'password'
],
'email'
:
s
[
'email'
]}
else
:
output
=
"Wrong Password"
else
:
output
=
"user not found"
return
jsonify
({
'result'
:
output
})
@
app
.
route
(
'/isValidIp'
,
methods
=
[
'POST'
])
def
is_valid_ip
():
source_ip
=
request
.
json
[
'source_ip'
]
protocol
=
request
.
json
[
'protocol'
]
flow_duration
=
request
.
json
[
'flow_duration'
]
bwd_packets
=
request
.
json
[
'bwd_packets'
]
Source_Port
=
request
.
json
[
'source_port'
]
Destination_Port
=
request
.
json
[
'destination_port'
]
notification
=
mongo
.
db
.
notification
#check for the white list
whitelist
=
mongo
.
db
.
whitelist
w
=
whitelist
.
find_one
({
'ip'
:
source_ip
})
if
w
:
return
jsonify
({
'result'
:
True
})
#check for the black list
blacklist
=
mongo
.
db
.
blacklist
b
=
blacklist
.
find_one
({
'ip'
:
source_ip
})
if
b
:
notification
.
insert
({
'ip'
:
source_ip
,
'message'
:
'Black listed ip address tried to access the protected server.'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
({
'result'
:
False
})
#check for mobile Bot net
X
=
pd
.
DataFrame
({
'Source'
:
source_ip
,
'Protocol'
:
protocol
},
index
=
[
4
])
X
=
X
.
to_numpy
()
labelencoder_X_T
=
LabelEncoder
()
X
[:,
0
]
=
labelencoder_X_T
.
fit_transform
(
X
[:,
0
])
labelencoder_X_S
=
LabelEncoder
()
X
[:,
1
]
=
labelencoder_X_S
.
fit_transform
(
X
[:,
1
])
result
=
mobileBotNet
.
predict
(
X
)
if
(
int
(
result
[
0
])
==
0
):
notification
=
mongo
.
db
.
notification
notification
.
insert
(
{
'ip'
:
source_ip
,
'message'
:
'Mobile botnet attack. Blocked by WANHEDA'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
({
'result'
:
False
})
#check for the slowris
X
=
pd
.
DataFrame
({
'Flow Duration'
:
flow_duration
,
'Total Length of Bwd Packets'
:
bwd_packets
},
index
=
[
4
])
result
=
slowloris
.
predict
(
X
)
if
(
int
(
result
[
0
])
<
0.5
):
notification
=
mongo
.
db
.
notification
notification
.
insert
(
{
'ip'
:
source_ip
,
'message'
:
'Slowloris attack. Blocked by WANHEDA'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
({
'result'
:
False
})
#check for ntpamplification
X
=
pd
.
DataFrame
({
'Source Port'
:
Source_Port
,
'Destination Port'
:
Destination_Port
},
index
=
[
4
])
result
=
ntpamplification
.
predict
(
X
)
if
(
int
(
result
[
0
])
==
0
):
notification
=
mongo
.
db
.
notification
notification
.
insert
(
{
'ip'
:
source_ip
,
'message'
:
'NtpAmplification attack. Blocked by WANHEDA'
,
'time'
:
datetime
.
datetime
.
now
()})
return
jsonify
({
'result'
:
True
})
@
app
.
route
(
'/notification/getall'
,
methods
=
[
'GET'
])
def
get_all_notification
():
notification
=
mongo
.
db
.
notification
output
=
[]
for
s
in
notification
.
find
():
output
.
append
({
'ip'
:
s
[
'ip'
],
'message'
:
s
[
'message'
],
'time'
:
s
[
'time'
]})
return
jsonify
({
'result'
:
output
})
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment