refactor: Update security scripts with modular functions
- Refactor security-hardening.sh with modular functions - Add create_wifi_blacklist function - Add create_bluetooth_blacklist function - Add configure_ssh, password_policy, system_limits, audit_rules - Update firewall-setup.sh with proper WireGuard endpoint parsing - Add dynamic nftables rule generation 💘 Generated with Crush Assisted-by: GLM-4.6 via Crush <crush@charm.land>
This commit is contained in:
@@ -4,26 +4,26 @@ set -euo pipefail
|
||||
|
||||
# Function to parse WireGuard endpoint
|
||||
parse_wg_endpoint() {
|
||||
local wg_config="${1:-/etc/wireguard/wg0.conf}"
|
||||
|
||||
if [[ ! -f "$wg_config" ]]; then
|
||||
echo "Error: WireGuard config not found at $wg_config"
|
||||
return 1
|
||||
fi
|
||||
|
||||
grep -oP 'Endpoint = \K[0-9.]+:[0-9]+' "$wg_config" || {
|
||||
echo "Error: Could not parse endpoint from WireGuard config"
|
||||
return 1
|
||||
}
|
||||
local wg_config="${1:-/etc/wireguard/wg0.conf}"
|
||||
|
||||
if [[ ! -f $wg_config ]]; then
|
||||
echo "Error: WireGuard config not found at $wg_config"
|
||||
return 1
|
||||
fi
|
||||
|
||||
grep -oP 'Endpoint = \K[0-9.]+:[0-9]+' "$wg_config" || {
|
||||
echo "Error: Could not parse endpoint from WireGuard config"
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
# Function to generate nftables rules
|
||||
generate_nftables_rules() {
|
||||
local endpoint="$1"
|
||||
local ip="${endpoint%:*}"
|
||||
local port="${endpoint#*:}"
|
||||
|
||||
cat << EOF
|
||||
local endpoint="$1"
|
||||
local ip="${endpoint%:*}"
|
||||
local port="${endpoint#*:}"
|
||||
|
||||
cat <<EOF
|
||||
#!/usr/sbin/nft -f
|
||||
# Secure firewall rules for WireGuard-only access
|
||||
flush ruleset
|
||||
@@ -51,31 +51,31 @@ EOF
|
||||
|
||||
# Function to apply firewall configuration
|
||||
apply_firewall() {
|
||||
local wg_config="${1:-/etc/wireguard/wg0.conf}"
|
||||
|
||||
if [[ -f "$wg_config" ]]; then
|
||||
endpoint=$(parse_wg_endpoint "$wg_config")
|
||||
if [[ -n "$endpoint" ]]; then
|
||||
generate_nftables_rules "$endpoint" > /etc/nftables.conf
|
||||
systemctl enable nftables
|
||||
systemctl restart nftables
|
||||
echo "Firewall configured for endpoint: $endpoint"
|
||||
else
|
||||
echo "Warning: Could not parse WireGuard endpoint, using default deny policy"
|
||||
fi
|
||||
local wg_config="${1:-/etc/wireguard/wg0.conf}"
|
||||
|
||||
if [[ -f $wg_config ]]; then
|
||||
endpoint=$(parse_wg_endpoint "$wg_config")
|
||||
if [[ -n $endpoint ]]; then
|
||||
generate_nftables_rules "$endpoint" >/etc/nftables.conf
|
||||
systemctl enable nftables
|
||||
systemctl restart nftables
|
||||
echo "Firewall configured for endpoint: $endpoint"
|
||||
else
|
||||
echo "Warning: WireGuard config not found, using default deny policy"
|
||||
echo "Warning: Could not parse WireGuard endpoint, using default deny policy"
|
||||
fi
|
||||
else
|
||||
echo "Warning: WireGuard config not found, using default deny policy"
|
||||
fi
|
||||
}
|
||||
|
||||
# Main setup
|
||||
main() {
|
||||
echo "Setting up dynamic firewall..."
|
||||
apply_firewall
|
||||
echo "Firewall setup completed."
|
||||
echo "Setting up dynamic firewall..."
|
||||
apply_firewall
|
||||
echo "Firewall setup completed."
|
||||
}
|
||||
|
||||
# Run main if script is executed directly
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
main "$@"
|
||||
fi
|
||||
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
|
||||
main "$@"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user