VivekM8737 commited on
Commit
990caa6
·
verified ·
1 Parent(s): f7f55ff

Upload 5 files

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. app.py +127 -0
  3. cnn_model.h5 +3 -0
  4. scaler.pkl +3 -0
  5. uniqueVal.csv +0 -0
  6. validation.csv +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ validation.csv filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from sklearn.preprocessing import MinMaxScaler
4
+
5
+ def preprocess_data(input_data, pmap, fmap, features, sc):
6
+ """Preprocesses the input data."""
7
+ input_df = pd.DataFrame([input_data], columns=features)
8
+ input_df['protocol_type'] = input_df['protocol_type'].map(pmap)
9
+ input_df['flag'] = input_df['flag'].map(fmap)
10
+ input_df = input_df[features]
11
+ input_data_scaled = sc.transform(input_df)
12
+ return input_data_scaled.reshape((-1,30,1)) # Reshape for CNN
13
+ def predict_attack(preprocessed_data, cnn_model):
14
+ """Predicts the attack type using the CNN model."""
15
+ prediction = cnn_model.predict(preprocessed_data)
16
+ amap = {0: 'dos', 1: 'normal', 2: 'probe', 3: 'r2l', 4: 'u2r'}
17
+ predicted_attack_type = amap[np.argmax(prediction)]
18
+ return predicted_attack_type
19
+
20
+ def network_attack_pipeline(input_data, cnn_model, pmap, fmap, features, sc):
21
+ """
22
+ A pipeline for predicting network attacks.
23
+
24
+ Args:
25
+ input_data (dict): Input data dictionary.
26
+ cnn_model (keras.Model): Trained CNN model.
27
+ pmap (dict): Mapping for protocol_type.
28
+ fmap (dict): Mapping for flag.
29
+ features (list): List of features used in training.
30
+ sc (MinMaxScaler): Scaler object.
31
+
32
+ Returns:
33
+ str: Predicted attack type.
34
+ """
35
+ preprocessed_data = preprocess_data(input_data, pmap, fmap, features, sc)
36
+ predicted_attack = predict_attack(preprocessed_data, cnn_model)
37
+ return predicted_attack
38
+
39
+ pmap = {'icmp': 0, 'tcp': 1, 'udp': 2}
40
+ fmap = {'SF': 0, 'S0': 1, 'REJ': 2, 'RSTR': 3, 'RSTO': 4, 'SH': 5, 'S1': 6, 'S2': 7, 'RSTOS0': 8, 'S3': 9, 'OTH': 10}
41
+ features = ['duration', 'protocol_type', 'flag', 'src_bytes', 'dst_bytes', 'land',
42
+ 'wrong_fragment', 'urgent', 'hot', 'num_failed_logins', 'logged_in',
43
+ 'num_compromised', 'root_shell', 'su_attempted', 'num_file_creations',
44
+ 'num_shells', 'num_access_files', 'is_guest_login', 'count',
45
+ 'srv_count', 'serror_rate', 'rerror_rate', 'same_srv_rate',
46
+ 'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count',
47
+ 'dst_host_srv_count', 'dst_host_diff_srv_rate',
48
+ 'dst_host_same_src_port_rate', 'dst_host_srv_diff_host_rate']
49
+
50
+ from tensorflow.keras.models import load_model
51
+ import joblib
52
+ import pickle
53
+ cnn_model = load_model("cnn_model.h5")
54
+
55
+ # Load the MinMaxScaler
56
+ scaler = joblib.load("scaler.pkl")
57
+
58
+
59
+ # Now this section in for frontend....
60
+ import streamlit as st
61
+ st.caption('Input must be in given sequence: ')
62
+ st.caption(features)
63
+ input=st.text_input("Enter the input with comma Seprated value: ")
64
+ listVal=input.split(',')
65
+ def predict():
66
+ inputList=[]
67
+ inputList.append((int)(listVal[0]))
68
+ inputList.append(listVal[1])
69
+ inputList.append(listVal[2])
70
+ inputList.append((int)(listVal[3]))
71
+ inputList.append((int)(listVal[4]))
72
+ inputList.append((int)(listVal[5]))
73
+ inputList.append((int)(listVal[6]))
74
+ inputList.append((int)(listVal[7]))
75
+ inputList.append((int)(listVal[8]))
76
+ inputList.append((int)(listVal[9]))
77
+ inputList.append((int)(listVal[10]))
78
+ inputList.append((int)(listVal[11]))
79
+ inputList.append((int)(listVal[12]))
80
+ inputList.append((int)(listVal[13]))
81
+ inputList.append((int)(listVal[14]))
82
+ inputList.append((int)(listVal[15]))
83
+ inputList.append((int)(listVal[16]))
84
+ inputList.append((int)(listVal[17]))
85
+ inputList.append((int)(listVal[18]))
86
+ inputList.append((int)(listVal[19]))
87
+ inputList.append((float)(listVal[20]))
88
+ inputList.append((float)(listVal[21]))
89
+ inputList.append((float)(listVal[22]))
90
+ inputList.append((float)(listVal[23]))
91
+ inputList.append((float)(listVal[24]))
92
+ inputList.append((int)(listVal[25]))
93
+ inputList.append((int)(listVal[26]))
94
+ inputList.append((float)(listVal[27]))
95
+ inputList.append((float)(listVal[28]))
96
+ inputList.append((float)(listVal[29]))
97
+ ans=''
98
+ try:
99
+ ans=network_attack_pipeline(inputList,cnn_model, pmap, fmap, features, scaler)
100
+ except:
101
+ return "Input is not in valid form:"
102
+ return ans
103
+
104
+ if(st.button('Predict')):
105
+ st.title(predict())
106
+
107
+
108
+ input2 =st.number_input("Enter the input between(0-494021)",step=1)
109
+ def predict1():
110
+ df=pd.read_csv('validation.csv')
111
+ x=df.iloc[input2,:-1]
112
+ lb=df.iloc[input2,-1]
113
+ st.title(f"Labeled as: {lb.upper()}")
114
+ print(df.iloc[input2,-1])
115
+ try:
116
+ res=network_attack_pipeline(x,cnn_model, pmap, fmap, features, scaler)
117
+ st.title(f"Predicted as: {res.upper()}")
118
+ except:
119
+ st.title("There is some essue try on another value...")
120
+
121
+ if(st.button('Validate')):
122
+ predict1()
123
+
124
+
125
+
126
+
127
+
cnn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4f367f984c37e973e0786040fde9780b5a12e9040ab7e058af0d3c1c75264bff
3
+ size 5921324
scaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:795484f9454d840ee8ecc116a32792a958c691f9b07698ca22b8e6ddad771d52
3
+ size 2743
uniqueVal.csv ADDED
The diff for this file is too large to render. See raw diff
 
validation.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:da1cb967ad328ca8a397f4c92505d8e981ff33967e8a8c90009c82eaf60e7ce4
3
+ size 47011410