fohnbit
Goto Top

UFW allow für nur deutschsprachige Länder

Hallo!

Ich habe einen kleinen Webserver, denn ich gerne absichern möchte. Zugriffe sollen nur aus den Ländern:
Österreich
Deutschland
Schweiz
erfolgen können. Jedoch möchte ich dann noch extra ips hinzufügen, die doch verbinden dürfen.

Soweit ich die Infos gesammelt habe:
  • Würde das gerne mit UFW machen.
Länderliste der iOS wäre hier zu bekommen:
Viele Einträge in die UFW sollen das System langsamer machen, aber hiermit wäre das möglich:

Sind die gesammelten Infos soweit korrekt und würde das klappen?

Danke!

Content-Key: 32540709506

Url: https://administrator.de/contentid/32540709506

Printed on: April 27, 2024 at 12:04 o'clock

Mitglied: 8585324113
8585324113 Oct 28, 2023 updated at 08:06:08 (UTC)
Goto Top
Definiere klappen.
Dein Filter muss fehlerfrei die IPs bewerten.

Ich bin mir einer Frankfurter IP online für z.b. Web und die IP ist dennoch den USA logisch zugeordnet.

Edith: Gibt auch Lookup Dienste die die Adresse in UK sehen.
Member: StefanKittel
StefanKittel Oct 28, 2023 at 08:10:53 (UTC)
Goto Top
Hallo,
Du könntest HAProxy davorschalten und dem eine GEO-Datei mitgeben.
Stefan
Member: Fohnbit
Fohnbit Oct 28, 2023 at 09:39:02 (UTC)
Goto Top
Zitat von @StefanKittel:

Hallo,
Du könntest HAProxy davorschalten und dem eine GEO-Datei mitgeben.
Stefan

Das wäre ganz gut, da ich diesen schon am laufen habe. Müsste aber dann nginx komplett rausschmeissen und alles auf HAProxy machen. Das aber später.

Der bestehende HAProxy nimmt https auf Port 8443 an und leitet es dann an weitere Server auf Port 8443 weiter.

Ein erster Test war nicht ganz erfolgreich. Meine HAProxy config:
global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    maxconn 4096
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    tcp
    option  tcplog
    option  dontlognull
    timeout connect 15s
    timeout client  15s
    timeout server  15s
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend localhost8443
    bind *:8443
    option tcplog
#    log-format "%ci:%cp_%[src,map_ip(/etc/haproxy/haproxy_geo_ip.txt)] [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r" 
    mode tcp

    acl tls req.ssl_hello_type 1

    tcp-request inspect-delay 5s
    tcp-request content accept if tls
# --- GEO Block
#    acl acl_geoloc_block src,map_ip(/etc/haproxy/haproxy_geo_ip.txt) -m reg -i (RU|IR)
#    http-request silent-drop if acl_geoloc_block
# ---

    acl is_client1 req.ssl_sni -i client1.demo.com   #10.11.0.2
    acl is_client2 req.ssl_sni -i client2.demo.com    #10.11.0.6

    use_backend client1 if is_client1
    use_backend client2  if is_client2

backend client1
    mode tcp
    option ssl-hello-chk
    server client1 10.11.0.2:8443 check


backend client2
    mode tcp
    option ssl-hello-chk
    server client2 10.11.0.6:8443 check

http-request silent-drop if acl_geoloc_block
Da meckert er aber, dass ich "mode http" nutzen soll. Aber wie kann ich die tcp config abändern, damit weiterhin alles funktioniert?
Member: Fohnbit
Fohnbit Oct 28, 2023 at 09:46:57 (UTC)
Goto Top
Edit:

    tcp-request inspect-delay 5s
# --- GEO Block
    acl acl_geoloc_block src,map_ip(/etc/haproxy/haproxy_geo_ip.txt) -m reg -i (RU|IR)
#   http-request silent-drop if acl_geoloc_block
    tcp-request connection reject if acl_geoloc_block
# ---
    tcp-request content accept if tls

Da meldet er mir keinen Fehler mehr. Ist die config soweit richtig?
Member: aqui
aqui Oct 28, 2023 at 10:26:51 (UTC)
Goto Top
Member: Fohnbit
Fohnbit Oct 30, 2023 at 09:55:46 (UTC)
Goto Top
Danke, ich bleib erstmal bei HAProxy. Das Geo klappt, aber bei den logs hätte ich gerne eine Änderung, aber weiß nicht wie:
Der Anfang meiner Config:
global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    maxconn 4096
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    tcp
    log-format "%ci:%cp [%[src,map_ip(/etc/haproxy/haproxy_geo_ip.txt)]] [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"  
    option  dontlognull
    timeout connect 15s
    timeout client  15s
    timeout server  15s
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend demo.company.com
    bind *:8443
    mode tcp
    acl tls req.ssl_hello_type 1

    tcp-request inspect-delay 5s
# --- GEO Block
    acl acl_geoloc_block src,map_ip(/etc/haproxy/haproxy_geo_ip.txt) -m reg -i (CH|AT|DE|IT|FR)
    tcp-request connection reject if !acl_geoloc_block
# ---
    tcp-request content accept if tls

Er schreibt die Log Zeile erst, wenn die Verbindung geschlossen wurde. Ich würde aber gerne die Logzeile haben, wenn sich jemand über den Proxy verbindet

Und zweites: Schön wäre wenn ich auch die Logzeile hätte, wenn eine IP geblockt wird.

Danke!
Member: Dani
Dani Oct 30, 2023 at 13:40:07 (UTC)
Goto Top
Moin,
Und zweites: Schön wäre wenn ich auch die Logzeile hätte, wenn eine IP geblockt wird.
es gibt doch sicherlich auch eine Variable für das Logfile, welche die IP-Adresse des Clients beinhaltet. Diese Variable an der Stelle einer Wahl ergänzen und dein Wunsch ist erfüllt.


Gruß,
Dani
Member: Fohnbit
Fohnbit Oct 30, 2023 at 15:02:05 (UTC)
Goto Top
Zitat von @Dani:
es gibt doch sicherlich auch eine Variable für das Logfile, welche die IP-Adresse des Clients beinhaltet. Diese Variable an der Stelle einer Wahl ergänzen und dein Wunsch ist erfüllt.

Die IP wird mir eh im log angezeigt. Aber im Log erscheint es erst, wenn ich die TCP Session schließe.
Daher wird wohl auch keine geblocked Verbindung angezeigt, da diese gar nie geöffnet wird.
Member: StefanKittel
StefanKittel Oct 31, 2023 at 12:30:39 (UTC)
Goto Top
Zitat von @Fohnbit:
Die IP wird mir eh im log angezeigt. Aber im Log erscheint es erst, wenn ich die TCP Session schließe.
Das ist normal und auch bei Webservern so.
Die schreiben die Zeile erst wenn alle Informationen (z.B. Dauer der Verbindung) bekannt sind.
Es sind halt "dumme" Textzeilen. Eine Vorabzeile zu schreiben und diese danach zu aktualisieren ist aufwendig.

Daher wird wohl auch keine geblocked Verbindung angezeigt, da diese gar nie geöffnet wird.
Ich mache das so, dass ich über die Geo-Geschickt und ACL ein eigenes Backend habe.
Dabei steht in der Zeile im Access-Log der Name des Backends.

frontend haproxy-main
	mode http
	no option httpclose
	option forwardfor

	acl acl_geoloc src,map_ip(/etc/haproxy/geoip/geoip.txt) -m reg -i (DE|AT|CH|DK|PL|FR|ES|PT|BE|GR|MT|IT|NL|CY|US)
	use_backend tarpit_geo if !acl_geoloc

	acl acl_fail2ban src,map_ip(/etc/haproxy/fail2ban.txt) -m reg -i (IP)
	use_backend tarpit_fail2ban if acl_fail2ban

backend tarpit_fail2ban
	timeout tarpit 5s
	errorfile 404 /etc/haproxy/403ip.html
	http-request tarpit deny_status 404
	
backend tarpit_geo
	timeout tarpit 5s
	errorfile 404 /etc/haproxy/403geo.html
	http-request tarpit deny_status 404
Member: Fohnbit
Fohnbit Nov 02, 2023 at 08:02:02 (UTC)
Goto Top
Die Idee ist perfekt. Hab ich eingebaut. Nun möchte ich aber gerne von mode TCP auf HTTP wechseln.

Leider bekomme ich mit der config nachstehend ein 503 vom Server, wenn ich demo2.company.com:8443 teste

Weiß jemand wo der Fehler liegt?
global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    maxconn 4096
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    http
    no option httpclose
    option forwardfor
    timeout connect 15s
    timeout client  15s
    timeout server  15s
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend cloud.company.com
    bind *:8443
    mode http


    # acl tls req.ssl_hello_type 1
    log-format "%ci:%cp [%[src,map_ip(/etc/haproxy/haproxy_geo_ip.txt)]] [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"  
#    log /dev/log local1 info

    #tcp-request inspect-delay 5s
    #tcp-request content accept if tls

# --- GEO Block
    acl acl_geoloc_block src,map_ip(/etc/haproxy/haproxy_geo_ip.txt) -m reg -i (CH|AT|DE|IT|FR)
#    tcp-request connection reject if !acl_geoloc_block
    use_backend block_geo if !acl_geoloc_block
# ---
    

    acl is_demo1 ssl_fc -i demo1.company.com   #10.11.0.2
    acl is_demo2  ssl_fc -i demo2.company.com    #10.11.0.6

    use_backend demo1 if is_demo1
    use_backend demo2  if is_demo2

backend block_geo
	timeout tarpit 5s
	errorfile 404 /etc/haproxy/errors/403.http
    http-request tarpit deny_status 404
#	tcp-request connection reject

backend demo1
    mode http
   # option ssl-hello-chk
    server demo1 10.11.0.2:8443 check


backend demo2
    mode http
    cookie    SERVERID insert indirect nocache maxidle 30m
    ## set the host name in the header
    acl h_host_exists req.hdr(Host) -m found
    http-request del-header Host if h_host_exists
    http-request set-header Host cloud.company.com

  #  option ssl-hello-chk
    server demo2 10.11.0.6:8443 check