PHP Classes

File: firewall/geoip.sh

Recommend this page to a friend!
  Packages of Moises Espindola   Sentinela   firewall/geoip.sh   Download  
File: firewall/geoip.sh
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Sentinela
Linux protection tools with Web dashboard panel
Author: By
Last change:
Date: 6 days ago
Size: 3,865 bytes
 

Contents

Class file image Download
#!/usr/bin/env bash #============================================================================= # Sentinela ? Intelligent Linux Security Framework # firewall/geoip.sh ? Reglas de geoip #============================================================================= # # NOTA: GeoIP requiere iptables geoip match (xt_geoip). # Esto no está disponible en kernels estándar. # # Por ahora, GeoIP se maneja a nivel IDS (post-procesamiento de logs) # y a través de la API de AbuseIPDB. # # En el futuro, se integrará con nftables que soporta GeoIP nativamente. #============================================================================= # shellcheck source=lib/common.sh source "${SENTINELA_DIR}/lib/common.sh" #============================================================================= # Verificar si xt_geoip está disponible #============================================================================= geoip_available() { if [[ -d /proc/net/xt_geoip ]] || lsmod 2>/dev/null | grep -q xt_geoip; then return 0 fi # Verificar si el módulo existe if modinfo xt_geoip &>/dev/null 2>&1; then return 0 fi return 1 } #============================================================================= # Verificar si la base de datos GeoIP existe #============================================================================= geoip_db_exists() { [[ -f "${GEOIP_DB}" ]] return $? } #============================================================================= # Obtener país de una IP (usando geoiplookup o mmdblookup) #============================================================================= geoip_lookup_country() { local ip="$1" if command -v geoiplookup &>/dev/null; then geoiplookup "${ip}" 2>/dev/null | grep -oP 'Country:\s+\K\w+' || echo "Desconocido" elif command -v mmdblookup &>/dev/null && geoip_db_exists; then mmdblookup --file "${GEOIP_DB}" --ip "${ip}" country names en 2>/dev/null | \ grep -oP '"[^"]+"' | head -1 | tr -d '"' || echo "Desconocido" else echo "No disponible" fi } #============================================================================= # Obtener ASN de una IP #============================================================================= geoip_lookup_asn() { local ip="$1" if command -v mmdblookup &>/dev/null && [[ -f "${GEOIP_ASN_DB}" ]]; then local asn asn=$(mmdblookup --file "${GEOIP_ASN_DB}" --ip "${ip}" autonomous_system_number 2>/dev/null | \ grep -oP '\d+' | head -1 || echo "0") local org org=$(mmdblookup --file "${GEOIP_ASN_DB}" --ip "${ip}" autonomous_system_organization 2>/dev/null | \ grep -oP '"[^"]+"' | head -1 | tr -d '"' || echo "Desconocido") echo "AS${asn} - ${org}" else echo "No disponible" fi } #============================================================================= # Instalar dependencias GeoIP #============================================================================= geoip_install() { log_info "Instalando dependencias GeoIP..." apt-get install -y geoip-bin geoip-database mmdb-bin libmaxminddb0 2>/dev/null || true # Descargar GeoLite2 si no existe if [[ ! -f "${GEOIP_DB}" ]]; then log_info "Descargando GeoLite2-Country..." mkdir -p "$(dirname "${GEOIP_DB}")" curl -sL "https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb" \ -o "${GEOIP_DB}" 2>/dev/null || log_warn "No se pudo descargar GeoLite2-Country" fi if [[ ! -f "${GEOIP_ASN_DB}" ]]; then log_info "Descargando GeoLite2-ASN..." curl -sL "https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-ASN.mmdb" \ -o "${GEOIP_ASN_DB}" 2>/dev/null || log_warn "No se pudo descargar GeoLite2-ASN" fi }