Skip to content

Liando18/deteksi-ddos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Hybrid DDoS Detection System

Sistem deteksi DDoS hybrid yang menggunakan Machine Learning (Naive Bayes Gaussian) untuk mengidentifikasi dan memblokir serangan DDoS secara otomatis. Sistem ini dapat berjalan pada server lokal atau terintegrasi dengan router MikroTik untuk perlindungan jaringan yang lebih komprehensif.

πŸ“‹ Daftar Isi

πŸ”§ Persyaratan Sistem

Sistem Operasi

  • Ubuntu Linux 22.04+ (versi stabil yang direkomendasikan)
  • Akses root/sudo untuk menjalankan iptables dan tshark

Python

  • Python 3.11+ (versi minimum yang didukung)

Dependensi Utama

  • pandas==2.1.4
  • scikit-learn==1.3.2
  • joblib==1.3.2
  • colorama==0.4.6
  • requests==2.31.0
  • RouterOS-api==0.17.0
  • python-dotenv==1.0.0
  • numpy==1.26.2
  • matplotlib==3.8.2

πŸ“¦ Instalasi

1. Klon atau Unduh Repository

cd /path/to/your/project

2. Install Dependencies

pip install pandas==2.1.4 scikit-learn==1.3.2 joblib==1.3.2 colorama==0.4.6 requests==2.31.0 RouterOS-api==0.17.0 python-dotenv==1.0.0 numpy==1.26.2 matplotlib==3.8.2

4. Install Wireshark/TShark (Untuk Packet Capture)

sudo apt-get update
sudo apt-get install tshark -y

Izinkan non-root user untuk menjalankan tshark:

sudo usermod -a -G wireshark $USER
sudo chmod +x /usr/bin/dumpcap

Logout dan login kembali agar perubahan grup berlaku.

βš™οΈ Konfigurasi

1. Konfigurasi Server Linux

A. Setup iptables (Opsional, untuk blocking lokal)

Pastikan iptables sudah installed:

sudo apt-get install iptables -y

B. Tentukan Network Interface

Default interface adalah enp0s8. Cek interface Anda dengan:

ip addr show

2. Konfigurasi MikroTik Router

Jika ingin mengintegrasikan dengan MikroTik untuk perlindungan jaringan yang lebih baik, jalankan perintah berikut di MikroTik CLI:

A. Aktifkan API Service

/ip service set api port=8728 disabled=no

B. Buat User API

/user add name=apiuser password=apiuser group=full

C. Buat Address List untuk Blacklist

/ip firewall address-list add list=blacklist address=0.0.0.0 comment="Placeholder"

D. Konfigurasi Sniffer (TZSP Streaming)

/tool sniffer set filter-interface=ether1 streaming-enabled=yes streaming-server=<IP_SERVER> filter-stream=yes

Ganti <IP_SERVER> dengan IP address server Ubuntu Anda.

E. Jalankan Sniffer

/tool sniffer start

3. Integrasi WhatsApp (Fonnte)

Setup WhatsApp Integration:

  1. Daftar di Fonnte

    • Kunjungi fonnte.com
    • Register dengan nomor WhatsApp Anda
    • Verifikasi nomor WhatsApp
  2. Dapatkan Token

    • Login ke Fonnte Dashboard
    • Buka menu "Device"
    • Copy token API Anda
  3. Konfigurasi Token

    • Buka file /messages/whatsapp/wa.py
    • Temukan baris: token = "isikan_token_di_sini"
    • Ganti dengan token Anda:
    token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  4. Update Nomor Tujuan

    • Di server.py dan router.py, cari:
    wa_targets = [
        "6285835524290",  # Ganti dengan nomor Anda
    ]

4. Integrasi Email (Gmail)

Setup Email Integration:

  1. Aktifkan 2-Factor Authentication di Gmail

    • Login ke akun Google Anda
    • Buka Security settings
    • Aktifkan 2-Step Verification
  2. Buat App Password

    • Pergi ke Google Account Security
    • Buka "App passwords" (akan muncul setelah 2FA aktif)
    • Pilih "Mail" dan "Windows Computer" (atau device Anda)
    • Copy 16-digit password yang digenerate
  3. Konfigurasi Credentials

    • Buka file /messages/mail/mail.py
    • Temukan baris:
    self.sender_email = "isikan_email"
    self.password = "isikan_key"
    • Ganti dengan:
    self.sender_email = "your.email@gmail.com"
    self.password = "xxxx xxxx xxxx xxxx"  # 16-digit app password
  4. Update Email Penerima

    • Di server.py dan router.py, cari:
    email_receivers = [
        "liando1804@gmail.com",  # Ganti dengan email Anda
    ]

πŸš€ Penggunaan

Jalankan Server Detection (Lokal)

python3 server.py

Opsi Command Line:

python3 server.py --interface enp0s8 --model model/result/model/ddos_model.pkl

Flags yang tersedia:

  • --interface, -i: Network interface untuk monitoring (default: enp0s8)
  • --model, -m: Path ke model ML (default: model/result/model/ddos_model.pkl)
  • --no-iptables: Disable iptables blocking
  • --no-blackhole: Disable blackhole route
  • --port, -p: Port server (default: 6001)

Jalankan Router Detection (MikroTik)

Pastikan MikroTik sudah menjalankan /tool sniffer start terlebih dahulu!

python3 router.py

Opsi Command Line:

python3 router.py \
  --interface any \
  --host 10.10.18.2 \
  --user apiuser \
  --pass apiuser \
  --mt-port 8728

Flags yang tersedia:

  • --interface, -i: Interface untuk tshark (default: any)
  • --model, -m: Path ke model ML
  • --host: IP address MikroTik (default: 10.10.18.2)
  • --user: Username API MikroTik (default: apiuser)
  • --pass: Password API MikroTik (default: apiuser)
  • --mt-port: Port API MikroTik (default: 8728)
  • --no-iptables: Disable iptables blocking
  • --no-blackhole: Disable blackhole route

Reset Setting Blokir

Untuk menghapus semua IP yang ter-block:

python3 reset.py

πŸ“Š Cara Kerja Sistem

1. Packet Capture

  • System menggunakan TShark untuk menangkap packet dari network
  • Packet dimasukkan ke dalam queue untuk diproses

2. Feature Extraction

Setiap packet dianalisis untuk mengekstrak fitur:

  • Protocol (ICMP, TCP, UDP)
  • Average Packet Length
  • Packet Rate (packets per second)
  • Packet Count
  • IP TTL (Time To Live)

3. Machine Learning Classification

  • Model Naive Bayes Gaussian memprediksi apakah traffic adalah DDoS Attack atau Normal
  • Confidence probability ditampilkan untuk setiap prediksi

4. Detection Confirmation

  • Sistem menggunakan sliding window dengan state memory
  • DDoS dipastikan setelah terdeteksi 3x dalam interval window (5 deteksi berturut-turut)
  • Ini mengurangi false positive

5. Blocking Action

Ketika DDoS dikonfirmasi, sistem melakukan:

  • Server Mode: iptables DROP rule + Blackhole routing
  • Router Mode: MikroTik Address List + Firewall Filter Rules

6. Notification

Sistem mengirimkan notifikasi melalui:

  • WhatsApp (instant alert)
  • Email (detailed report dengan HTML formatting)

πŸ“ Struktur Proyek

project/
β”œβ”€β”€ server.py                 # Deteksi DDoS di server lokal
β”œβ”€β”€ router.py                 # Deteksi DDoS dengan MikroTik router
β”œβ”€β”€ reset.py                  # Reset semua setting blokir
β”‚
β”œβ”€β”€ model/
β”‚   └── result/
β”‚       └── model/
β”‚           β”œβ”€β”€ ddos_model.pkl       # Model ML yang sudah ditraining
β”‚           └── label_encoder.pkl    # Label encoder untuk predictions
β”‚
β”œβ”€β”€ messages/
β”‚   β”œβ”€β”€ message.py           # Class utama untuk mengirim notifikasi
β”‚   β”œβ”€β”€ whatsapp/
β”‚   β”‚   └── wa.py            # WhatsApp integration (Fonnte)
β”‚   └── mail/
β”‚       └── mail.py          # Email integration (Gmail)
β”‚
└── README.md                # Dokumentasi ini

πŸ“ˆ Output dan Monitoring

Sistem akan menampilkan output seperti berikut di terminal:

Datetime             | Source IP       | Protocol  | Packet Length | Packet Rate | Packet Count | IP TTL | Status       | Probability
2024-02-09 14:23:45 | 192.168.64.100  | UDP       |            65 |          250 |          750 | 64     | Normal       |    85.50%
2024-02-09 14:23:48 | 192.168.64.150  | TCP       |           120 |         1500 |         4500 | 128    | DDOS-Attack  |    92.30%

Penjelasan Kolom:

  • Datetime : Timestamp deteksi
  • Source IP : IP address pengirim paket
  • Protocol : Protokol yang digunakan (ICMP, TCP, UDP)
  • Packet Length : Rata-rata ukuran paket (bytes)
  • Packet Rate : Jumlah paket per detik
  • Packet Count : Total paket dalam window
  • IP TTL : Time To Live dari IP header
  • Status : Hasil klasifikasi (Normal/DDOS-Attack)
  • Probability : Confidence level dari model

πŸ”’ Security Considerations

Best Practices:

  1. Change Default Credentials

    /user set apiuser password=strong_password_here
    
  2. Restrict API Access

    /ip service set api address=10.10.18.0/24
    
  3. Monitor Blacklist

    • Periksa /ip firewall address-list secara berkala
    • Hapus IP yang sudah diperbaiki:
    /ip firewall address-list remove [find list=blacklist]
    
  4. Backup Configuration

    cp model/result/model/ddos_model.pkl model/result/model/ddos_model.pkl.backup

πŸ› Troubleshooting

Problem: "Permission denied" saat menjalankan tshark

Solution:

sudo usermod -a -G wireshark $USER
# Logout dan login kembali

Problem: "Cannot connect to MikroTik API"

Solution:

  • Pastikan API service aktif: /ip service set api disabled=no
  • Cek IP address MikroTik: ip addr di MikroTik
  • Pastikan firewall tidak memblok port 8728
  • Test koneksi: telnet <mikrotik_ip> 8728

Problem: "Module 'RouterOS_api' not found"

Solution:

pip install RouterOS-api==0.17.0

Problem: WhatsApp notification tidak terkirim

Troubleshoot:

  • Pastikan token Fonnte valid
  • Cek koneksi internet
  • Verifikasi nomor WhatsApp terdaftar di Fonnte
  • Test token dengan curl:
curl -X GET "https://api.fonnte.com/validate?token=YOUR_TOKEN"

Problem: Email notification tidak terkirim

Troubleshoot:

  • Pastikan 2FA Gmail sudah aktif
  • Verifikasi app password benar (16 digit)
  • Cek di Gmail: Settings > Security > Less secure app access
  • Lihat log di /messages/mail/mail.py

Problem: Model file tidak ditemukan

Solution:

# Pastikan path benar
ls -la model/result/model/
# Jika tidak ada, download/training ulang model

Problem: iptables rule tidak bekerja

Solution:

# Cek iptables rules
sudo iptables -L -n -v

# Flush dan reset rules (HATI-HATI!)
sudo iptables -F
sudo iptables -X
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

Problem: Port sudah digunakan

Solution:

# Temukan proses yang menggunakan port
sudo lsof -i :6001

# Kill process jika perlu
sudo kill -9 <PID>

# Atau gunakan port berbeda
python3 server.py --port 6002

πŸ“ Log dan Debugging

Enable Debug Mode

Tambahkan ke dalam code untuk verbose logging:

import logging
logging.basicConfig(level=logging.DEBUG)

Check System Logs

# Ubuntu system logs
tail -f /var/log/syslog | grep tshark

# Iptables logs
sudo iptables -L -n -v

# MikroTik logs (via SSH)
ssh admin@<mikrotik_ip>
/log print

πŸ“ž Support & Contact

Jika mengalami masalah atau punya pertanyaan:

  1. Cek dokumentasi di README.md (file ini)
  2. Review logs dan error messages
  3. Test komponen secara terpisah
  4. Hubungi tim development (085835524290 / liando1801@gmail.com)

About

Artificial Intelligence (AI) berbasis Machine Learning menggunakan algoritma Naive Bayes dalam mendeteksi dan mencegah serangan Distributed Denial of Service (DDoS)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors