Spaces:
Running
Running
File size: 9,342 Bytes
ba2b0bb |
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
#!/bin/bash
# firewall_manager.sh - Manajemen firewall rules terpadu dengan iptables, ufw, dan firewalld
#
# Fitur:
# - Validasi input yang lebih kompleks untuk aturan
# - Logging setiap aksi ke /var/log/firewall_manager.log
# - Integrasi dengan firewalld (jika aktif) secara langsung
#
# Usage:
# sudo ./firewall_manager.sh {start|stop|status|list|add-rule|del-rule|backup|restore}
#
# Contoh:
# sudo ./firewall_manager.sh start
# sudo ./firewall_manager.sh add-rule udp 1000:2000 9999
set -euo pipefail
LOG_FILE="/var/log/firewall_manager.log"
# === Fungsi Logging ===
log_msg() {
local level="$1"
shift
local message="$*"
local timestamp
timestamp=$(date +'%Y-%m-%d %H:%M:%S')
echo "${timestamp} [${level}] ${message}" | tee -a "${LOG_FILE}"
}
# === Fungsi Validasi ===
validate_protocol() {
local protocol="$1"
if [[ "$protocol" != "udp" && "$protocol" != "tcp" ]]; then
log_msg "ERROR" "Protocol harus 'udp' atau 'tcp'. Diberikan: ${protocol}"
exit 1
fi
}
validate_port_range() {
local port_range="$1"
if ! [[ "$port_range" =~ ^[0-9]{1,5}(:[0-9]{1,5})?$ ]]; then
log_msg "ERROR" "Format port range tidak valid: ${port_range}"
exit 1
fi
}
validate_port() {
local port="$1"
if ! [[ "$port" =~ ^[0-9]{1,5}$ ]]; then
log_msg "ERROR" "Format port tidak valid: ${port}"
exit 1
fi
if (( port < 1 || port > 65535 )); then
log_msg "ERROR" "Port harus berada di antara 1 dan 65535: ${port}"
exit 1
fi
}
# === Fungsi Helper ===
usage() {
cat << EOF
Usage: $0 {start|stop|status|list|add-rule|del-rule|backup|restore}
Commands:
start : Enable IP forwarding dan pasang aturan default.
(Default: UDP CUSTOM (1:5999->3671) dan ZIVPN (6000:19999->5667) + aturan ufw dan firewalld jika aktif)
stop : Flush aturan NAT dan disable IP forwarding.
status : Tampilkan status IP forwarding dan aturan firewall saat ini.
list : List aturan iptables (tabel NAT) dan status ufw.
add-rule : Tambah aturan kustom.
Sintaks: $0 add-rule <protocol> <port_range> <destination_port>
Contoh: $0 add-rule udp 1000:2000 9999
del-rule : Hapus aturan kustom.
Sintaks: $0 del-rule <chain> <protocol> <port_range> <destination_port>
Contoh: $0 del-rule PREROUTING udp 1000:2000 9999
backup : Backup aturan iptables NAT ke file backup.
restore : Restore aturan iptables NAT dari file backup.
EOF
exit 1
}
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Jalankan script ini sebagai root." >&2
exit 1
fi
}
enable_ip_forwarding() {
log_msg "INFO" "Mengaktifkan IP forwarding"
sysctl -w net.ipv4.ip_forward=1 >/dev/null
}
disable_ip_forwarding() {
log_msg "INFO" "Menonaktifkan IP forwarding"
sysctl -w net.ipv4.ip_forward=0 >/dev/null
}
get_default_interface() {
local iface
iface=$(ip route | awk '/default/ {print $5; exit}')
echo "$iface"
}
is_firewalld_active() {
if systemctl is-active --quiet firewalld; then
return 0
else
return 1
fi
}
# === Integrasi Firewalld ===
apply_firewalld_rule() {
local action="$1" # add or remove
local rule="$2"
# Contoh penggunaan:
# firewall-cmd --permanent --direct --add-rule ipv4 nat PREROUTING 0 -i ${interface} -p udp --dport 1:7299 -j DNAT --to-destination :3671
firewall-cmd --permanent --direct --"${action}"-rule ipv4 nat PREROUTING 0 ${rule}
}
reload_firewalld() {
firewall-cmd --reload
}
# === Fungsi Aturan Default ===
apply_default_rules() {
local interface
interface=$(get_default_interface)
if [[ -z "$interface" ]]; then
log_msg "ERROR" "Interface default tidak ditemukan."
exit 1
fi
log_msg "INFO" "Menggunakan interface: ${interface}"
./port-forward.sh
# Udp Custom: izinkan port untuk Udp Custom
ufw allow 1:5999/udp && ufw allow 3671/udp
log_msg "INFO" "Aturan UFW diterapkan untuk port 1:5999/udp dan 3671/udp"
# UFW: izinkan port untuk ZIVPN
ufw allow 6000:19999/udp && ufw allow 5667/udp
log_msg "INFO" "Aturan UFW diterapkan untuk port 6000:19999/udp dan 5667/udp"
# Integrasi firewalld jika aktif
if is_firewalld_active; then
log_msg "INFO" "firewalld terdeteksi. Menerapkan aturan ke firewalld..."
# Aturan untuk UDP CUSTOM
apply_firewalld_rule "add" "-i ${interface} -p udp --dport 1:7299 -j DNAT --to-destination :3671"
# Aturan untuk ZIVPN
apply_firewalld_rule "add" "-i ${interface} -p udp --dport 6000:19999 -j DNAT --to-destination :5667"
reload_firewalld
log_msg "INFO" "Aturan firewalld diterapkan dan reload"
fi
log_msg "INFO" "Aturan default berhasil diterapkan."
}
flush_firewall_rules() {
log_msg "INFO" "Menghapus semua aturan di NAT table iptables..."
iptables -t nat -F
log_msg "INFO" "Aturan NAT table iptables telah dihapus."
# Integrasi firewalld: Hapus aturan default jika firewalld aktif
if is_firewalld_active; then
local interface
interface=$(get_default_interface)
log_msg "INFO" "Menghapus aturan firewalld..."
apply_firewalld_rule "remove" "-i ${interface} -p udp --dport 1:7299 -j DNAT --to-destination :3671"
apply_firewalld_rule "remove" "-i ${interface} -p udp --dport 6000:19999 -j DNAT --to-destination :5667"
reload_firewalld
log_msg "INFO" "Aturan firewalld default dihapus."
fi
}
list_firewall_rules() {
echo "Aturan iptables (NAT table):"
iptables -t nat -L -n -v
echo
echo "Status UFW:"
ufw status verbose
}
backup_firewall_rules() {
local backup_file="/root/iptables_nat_backup_$(date +%F).txt"
iptables-save -t nat > "${backup_file}"
log_msg "INFO" "Backup aturan NAT table iptables telah disimpan ke ${backup_file}"
}
restore_firewall_rules() {
local backup_file="/root/iptables_nat_backup_$(date +%F).txt"
if [ ! -f "${backup_file}" ]; then
log_msg "ERROR" "File backup ${backup_file} tidak ditemukan."
exit 1
fi
iptables-restore < "${backup_file}"
log_msg "INFO" "Aturan NAT table iptables direstore dari ${backup_file}"
}
add_custom_rule() {
# Ekspektasi: protocol, port_range, destination_port
if [ $# -ne 3 ]; then
echo "Usage: $0 add-rule <protocol> <port_range> <destination_port>"
exit 1
fi
local protocol="$1"
local port_range="$2"
local dest_port="$3"
validate_protocol "${protocol}"
validate_port_range "${port_range}"
validate_port "${dest_port}"
local interface
interface=$(get_default_interface)
if [[ -z "$interface" ]]; then
log_msg "ERROR" "Interface default tidak ditemukan."
exit 1
fi
iptables -t nat -A PREROUTING -i "${interface}" -p "${protocol}" --dport "${port_range}" -j DNAT --to-destination :${dest_port}
log_msg "INFO" "Aturan kustom iptables ditambahkan: ${protocol} ${port_range} -> ${dest_port} pada interface ${interface}"
# Integrasi firewalld jika aktif
if is_firewalld_active; then
apply_firewalld_rule "add" "-i ${interface} -p ${protocol} --dport ${port_range} -j DNAT --to-destination :${dest_port}"
reload_firewalld
log_msg "INFO" "Aturan kustom firewalld ditambahkan: ${protocol} ${port_range} -> ${dest_port}"
fi
}
delete_custom_rule() {
# Ekspektasi: chain, protocol, port_range, destination_port
if [ $# -ne 4 ]; then
echo "Usage: $0 del-rule <chain> <protocol> <port_range> <destination_port>"
exit 1
fi
local chain="$1"
local protocol="$2"
local port_range="$3"
local dest_port="$4"
validate_protocol "${protocol}"
validate_port_range "${port_range}"
validate_port "${dest_port}"
local interface
interface=$(get_default_interface)
if [[ -z "$interface" ]]; then
log_msg "ERROR" "Interface default tidak ditemukan."
exit 1
fi
iptables -t nat -D "${chain}" -i "${interface}" -p "${protocol}" --dport "${port_range}" -j DNAT --to-destination :${dest_port}
log_msg "INFO" "Aturan kustom iptables dihapus: ${chain} ${protocol} ${port_range} -> ${dest_port}"
# Integrasi firewalld jika aktif
if is_firewalld_active; then
apply_firewalld_rule "remove" "-i ${interface} -p ${protocol} --dport ${port_range} -j DNAT --to-destination :${dest_port}"
reload_firewalld
log_msg "INFO" "Aturan kustom firewalld dihapus: ${chain} ${protocol} ${port_range} -> ${dest_port}"
fi
}
# === Main Program ===
check_root
if [ $# -eq 0 ]; then
usage
fi
COMMAND=$1
shift
case "${COMMAND}" in
start)
enable_ip_forwarding
apply_default_rules
;;
stop)
flush_firewall_rules
disable_ip_forwarding
;;
status)
echo "Status IP forwarding:"
sysctl net.ipv4.ip_forward
echo
list_firewall_rules
;;
list)
list_firewall_rules
;;
add-rule)
add_custom_rule "$@"
;;
del-rule)
delete_custom_rule "$@"
;;
backup)
backup_firewall_rules
;;
restore)
restore_firewall_rules
;;
*)
usage
;;
esac
|