-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathml_model.h5
40 lines (32 loc) · 1.32 KB
/
ml_model.h5
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
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
# Dataset contoh (payload dan label untuk serangan SQL Injection)
payloads = [
"' OR 1=1 --",
"' UNION SELECT null, username, password FROM users --",
"<script>alert('XSS')</script>",
"1' OR '1'='1' --",
"' DROP TABLE users;"
]
labels = [1, 1, 0, 1, 1] # 1 = serangan berbahaya, 0 = tidak berbahaya
# Mengubah payload menjadi fitur numerik
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(payloads).toarray()
# Membagi data menjadi pelatihan dan pengujian
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.3, random_state=42)
# Membangun model jaringan saraf
model = Sequential()
model.add(Dense(128, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Menyusun model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Melatih model
model.fit(X_train, y_train, epochs=10, batch_size=2, validation_data=(X_test, y_test))
# Menyimpan model ke file H5
model.save('ml_model.h5')
print("Model telah disimpan dalam 'ml_model.h5'")