Commit bc6ceb98 authored by Haritha Chanuka's avatar Haritha Chanuka

Upload Files

Upload application.py and picking.py files
parent e9b61355
File added
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+srv://Harry:harry123@cluster0.r1fmj.mongodb.net/wanhedadb?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 WANHEDA")
@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)
import pickle
from Volumetric.volumetric import model
from Slowloris.slowlorisModel import regressor
from MobileBotNet.mobileBotNetModel import classifier
from NTPAmplification.ntpamplification import classifierSVC
pickle.dump(classifier, open('models/mobileBotNet.sav', 'wb'))
pickle.dump(regressor, open('models/slowloris.sav', 'wb'))
pickle.dump(classifierSVC,open('models/ntpamplification.sav','wb'))
pickle.dump(model,open('models/volumetric.sav', 'wb'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment