Implementasi CrowdSec dengan OpenResty melibatkan dua komponen utama yaitu Bouncer (untuk memblokir IP berdasarkan aktivitas seperti brute-force) dan AppSec (Web Application Firewall/WAF untuk memblokir serangan aplikasi seperti SQL Injection dan XSS secara real-time).
Instalasi #
Langkah pertama adalah menginstall agen CrowdSec dan modul Bouncer khusus untuk OpenResty. Modul ini menggunakan Lua untuk mengintercept request sebelum diproses oleh aplikasi backend.
Jalankan perintah berikut untuk mengistall repositori resmi dan paket yang diperlukan
sudo dnf update
sudo dnf install -y curl gnupg ca-certificates
# Instal OpenResty
sudo dnf install -y yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/rhel/openresty2.repo
# Instal CrowdSec
curl -sS https://install.crowdsec.net | sudo bash
sudo dnf install -y crowdsec
# Instal OpenResty dan CrowdSec OpenResty Bouncer
sudo dnf install -y openresty openresty-resty crowdsec-openresty-bouncer
# Instal dependensi
sudo opm get ledgetech/lua-resty-http
sudo opm get fffonion/lua-resty-acmeSetelah instalasi selesai, pastikan semua service berjalan dengan baik
systemctl is-active crowdsec
systemctl is-active openresty
sudo cscli bouncers listKonfigurasi WAF (AppSec) #
Fitur AppSec berfungsi memeriksa setiap permintaan masuk ke aplikasi web secara mendalam untuk mendeteksi pola serangan umum. Kita akan menggunakan bersama rule standar industri OWASP Core Rule Set (CRS)
Buat File Konfigurasi AppSec #
Buat file konfigurasi agar CrowdSec dapat menerima request HTTP pada port lokal 7422
sudo tee /etc/crowdsec/acquis.d/appsec.yaml > /dev/null <<'EOF'
appsec_config: custom/crs-inband
labels:
type: appsec
listen_addr: 127.0.0.1:7422
source: appsec
EOFInstall Rule OWASP CRS #
Download collection rule dari repositori resmi
sudo cscli hub update
sudo cscli parsers install crowdsecurity/geoip-enrich
sudo cscli collections install crowdsecurity/appsec-virtual-patching
sudo cscli collections install crowdsecurity/appsec-generic-rules
sudo cscli collections install crowdsecurity/appsec-crsAktifkan Mode Blokir (Inband) #
Buat konfigurasi kustom untuk memaksa rule CRS memblokir request yang mencurigakan secara langsung, bukan hanya mencatatnya
sudo tee /etc/crowdsec/appsec-configs/crs-inband.yaml > /dev/null <<'EOF'
name: custom/crs-inband
default_remediation: ban
inband_rules:
- crowdsecurity/base-config
- crowdsecurity/crs
- crowdsecurity/vpatch-*
- crowdsecurity/generic-*
outofband_rules:
- crowdsecurity/experimental-*
- crowdsecurity/appsec-generic-test
EOFRestart service CrowdSec untuk menerapkan perubahan
sudo systemctl restart crowdsec
# Verifikasi listener berjalan di port 7422
sudo ss -tlnp | grep 7422Hubungkan OpenResty ke AppSec #
Konfigurasi Bouncer agar mengirim setiap request masuk ke AppSec untuk diperiksa sebelum diteruskan ke aplikasi
Edit file konfigurasi bouncer /etc/crowdsec/bouncers/crowdsec-openresty-bouncer.conf
# Arahkan bouncer ke listener AppSec
sudo sed -i 's|^APPSEC_URL=.*|APPSEC_URL=http://127.0.0.1:7422|' \
/etc/crowdsec/bouncers/crowdsec-openresty-bouncer.conf
# Wajibkan pemeriksaan AppSec untuk setiap request
sudo sed -i 's|^ALWAYS_SEND_TO_APPSEC=.*|ALWAYS_SEND_TO_APPSEC=true|' \
/etc/crowdsec/bouncers/crowdsec-openresty-bouncer.confEdit file /usr/local/openresty/nginx/conf/nginx.conf dan tambahkan baris berikut di dalam blok http:
include /usr/local/openresty/nginx/conf/conf.d/crowdsec_openresty.conf;Setelah itu, restart OpenResty
sudo systemctl restart openrestyTest & Verifikasi #
Untuk memastikan semua berkerja, Anda dapat mensimulasikan serangan. Jika dikonfigurasi dengan benar, OpenResty akan mengembalikan HTTP 403 Forbidden.
Test SQL Injection:
curl -v "http://localhost/?id=1%20UNION%20SELECT%20*%20FROM%20users"Test Path Traversal
curl -v "http://localhost/.env"Anda dapat memantau log secara realtime untuk melihat aktivitas crowdsec
sudo journalctl -u crowdsec -f