-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmangya_waf.py
134 lines (103 loc) · 3.77 KB
/
mangya_waf.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
# -*- coding: utf-8 -*-
"""mms waf modeling ml algo.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1h2OcDhgHiLVx6_u5HlfTz9roYKkPH9-K
# Modeling and evaluation of machine learning model
Import all dependencies
"""
# Commented out IPython magic to ensure Python compatibility.
# %matplotlib inline
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import string
import pickle
from sklearn.externals import joblib
from IPython.display import display
import sys
"""
import warnings
warnings.filterwarnings("ignore")
with warnings.catch_warnings():
warnings.simplefilter("ignore")
warnings.simplefilter(action='ignore', category=FutureWarning)
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
"""
#evaluations
payload = str(sys.argv[1])
data = pd.read_csv("/home/ubuntu/tools/mms_waf/final-allpayload.csv",index_col="index")
#data
"""Selecting dependent and independent variables"""
Y = data['is_malicious']
"""independent_variables = data.columns
independent_variables = independent_variables.delete(1)
independent_variables"""
X = data.iloc[:,3:]
"""# Modeling data with Xgboost Classifier"""
import xgboost as xgb
#?xgb.XGBClassifier()
#xgb_classifer = xgb.XGBClassifier()
xgb_classifer_joblib = joblib.load("/home/ubuntu/tools/mms_waf/mms_waf_joblib")
"""
with open("/home/ubuntu/tools/mms_waf/mms_maf_final","rb") as file:
xgb_classifer_pickle = pickle.load(file)
xgb_classifer_pickle.fit(X,Y)
"""
"""
# Commented out IPython magic to ensure Python compatibility.
# %time xgb_classifer.fit(X,Y)
data['predicted_is_malicious'] = xgb_classifer_pickle.predict(X)
data.head(30)
data[["is_malicious","predicted_is_malicious"]]
#?plt.plot()
"""
"""# Integration with website"""
independent_variables=['length', 'non-printable','punctuation', 'min-byte', 'max-byte', 'mean-byte', 'std-byte','distinct-byte', 'sql-keywords', 'js-keywords']
independent_variables
independent_variables=data.columns
sql_keywords = pd.read_csv('/home/ubuntu/tools/mms_waf/SQLKeywords.txt', index_col=False)
js_keywords = pd.read_csv("/home/ubuntu/tools/mms_waf/JavascriptKeywords.txt",index_col=False)
def calculate_features_and_predict(payload):
features = {}
payload = str(payload)
features['length'] = len(payload)
features['non-printable'] = len([1 for letter in payload if letter not in string.printable])
features['punctuation'] = len([1 for letter in payload if letter in string.punctuation])
features['min-byte'] = min(bytearray(payload,'utf-8'))
features['max-byte'] = max(bytearray(payload,'utf-8'))
features['mean-byte'] = np.mean(bytearray(payload,'utf-8'))
features['std-byte'] = np.std(bytearray(payload,'utf-8'))
features['distinct-byte'] = len(set(bytearray(payload,'utf-8')))
features['sql-keywords'] = len([1 for keyword in sql_keywords['Keyword'] if str(keyword).lower() in payload.lower()])
features['js-keywords'] = len([1 for keyword in js_keywords['Keyword'] if str(keyword).lower() in payload.lower()])
#payload_df = pd.DataFrame(data=features,index=[0],columns=independent_variables)
payload_df = pd.DataFrame(features,index=[0])
#display(payload_df)
result = xgb_classifer_joblib.predict(payload_df)
#display(result)
r = result[0]
#print(r)
if(r > 0):
print("malicious - 403 error")
else:
print("safe")
"""
calculate_features_and_predict("<>")
payload=''
while (payload != 'exit' ):
payload = input("Enter payload")
result = calculate_features_and_predict(payload)
if(result > 0):
print(f"Your payload {payload} is malicious - 403 error\n")
else:
print(f"Your payload {payload} is safe 200 OK\n")
"""
calculate_features_and_predict(payload)