Rozdíly
Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
Následující verze | Předchozí verze | ||
it:server:fail2ban [2022/11/19 18:05] – vytvořeno Petr Nosek | it:server:fail2ban [2022/11/19 22:21] (aktuální) – Petr Nosek | ||
---|---|---|---|
Řádek 1: | Řádek 1: | ||
====== Fail2ban ====== | ====== Fail2ban ====== | ||
+ | |||
+ | Software k dynamickému blokování IP adres, když dělají něco, co nemají. | ||
+ | |||
+ | <code bash> | ||
+ | apt install fail2ban | ||
+ | |||
+ | cd / | ||
+ | cp jail.conf jail.local | ||
+ | </ | ||
+ | |||
+ | zdroje k nastavení fail2ban: | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | |||
+ | ===== Akce - složka action.d ===== | ||
+ | |||
+ | |||
+ | Pro webserver jsem nastavoval, aby se zablokoval při portscanningu přístup na všechny porty, kromě 80 a 443. K tomu mi posloužil vytvoření soubor **/ | ||
+ | |||
+ | V githubu je soubor ještě více předělaný, | ||
+ | https:// | ||
+ | |||
+ | Pokud bych provozoval ještě nějaké další služby, musel bych je přidat, abych do tohoto skriptu. Protože pokud někdo udělá portscan, tak se zaříznou všechny služby, kromě žádoucích 80 a 443. | ||
+ | |||
+ | |||
+ | <code bash> | ||
+ | # Fail2Ban action configuration file for ufw | ||
+ | # | ||
+ | # You are required to run "ufw enable" | ||
+ | # | ||
+ | # The insert position should be appropriate to block the required traffic. | ||
+ | # A number after an allow rule to the application won't be of much use. | ||
+ | |||
+ | [Definition] | ||
+ | |||
+ | actionstart = | ||
+ | |||
+ | actionstop = | ||
+ | |||
+ | actioncheck = | ||
+ | |||
+ | actionban = [ -n "< | ||
+ | ufw prepend < | ||
+ | ufw prepend allow proto tcp from <ip> to < | ||
+ | #ufw insert < | ||
+ | |||
+ | actionunban = [ -n "< | ||
+ | ufw delete < | ||
+ | ufw delete allow proto tcp from <ip> to < | ||
+ | #ufw delete < | ||
+ | |||
+ | [Init] | ||
+ | # Option: insertpos | ||
+ | # Notes.: | ||
+ | insertpos = 1 | ||
+ | |||
+ | # Option: blocktype | ||
+ | # Notes.: reject or deny | ||
+ | blocktype = deny | ||
+ | |||
+ | # Option: destination | ||
+ | # Notes.: The destination address to block in the ufw rule | ||
+ | destination = any | ||
+ | |||
+ | # Option: application | ||
+ | # Notes.: application from sudo ufw app list | ||
+ | application = | ||
+ | |||
+ | # DEV NOTES: | ||
+ | # | ||
+ | # Author: Guilhem Lettron | ||
+ | # Enhancements: | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Filtry - složka filter.d ===== | ||
+ | |||
+ | Tady je konfigurace k jednotlivým službám. Každá služba zapisuje do syslogu pokusy o přihlášení v různém formátu. Filtry skrz regulární výrazy ukazují, co hledat. Tím, si lze fail2ban rozšířit i na služby, pro které defaultně určen není. | ||
+ | |||
+ | Přidáme **/ | ||
+ | |||
+ | <code bash> | ||
+ | # Fail2Ban filter for SoftEther authentication failures | ||
+ | # Made by quixrick and jonisc | ||
+ | # Thanks to quixrick from Reddit! https:// | ||
+ | |||
+ | [INCLUDES] | ||
+ | |||
+ | # Read common prefixes. If any customizations available -- read them from | ||
+ | # common.local | ||
+ | before = common.conf | ||
+ | |||
+ | #Enable multi line support. Doesn' | ||
+ | [Init] | ||
+ | maxlines = 2 | ||
+ | # The regular expression filter follows | ||
+ | [Definition] | ||
+ | |||
+ | failregex =IP address: < | ||
+ | ignoreregex= | ||
+ | </ | ||
+ | |||
+ | Dále **/ | ||
+ | |||
+ | <code bash> | ||
+ | # Fail2Ban filter for portscan | ||
+ | # https:// | ||
+ | # https:// | ||
+ | # | ||
+ | # | ||
+ | |||
+ | [Definition] | ||
+ | |||
+ | failregex = .*\[UFW BLOCK\] IN=.* SRC=< | ||
+ | |||
+ | ignoreregex = | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | Dále pro **/ | ||
+ | |||
+ | <code bash> | ||
+ | [INCLUDES] | ||
+ | before = common.conf | ||
+ | |||
+ | [Definition] | ||
+ | _groupsre = (?: | ||
+ | failregex = ^\{%(_groupsre)s,? | ||
+ | datepattern = ,? | ||
+ | ignoreregex = | ||
+ | </ | ||
+ | |||
+ | |||
+ | Našel jsem i vlastní ssh - **/ | ||
+ | |||
+ | < | ||
+ | ^Connection closed by < | ||
+ | </ | ||
+ | |||
+ | Tady obsah celého souboru. | ||
+ | < | ||
+ | # Fail2Ban filter for openssh | ||
+ | # | ||
+ | # If you want to protect OpenSSH from being bruteforced by password | ||
+ | # authentication then get public key authentication working before disabling | ||
+ | # PasswordAuthentication in sshd_config. | ||
+ | # | ||
+ | # | ||
+ | # " | ||
+ | # | ||
+ | |||
+ | [INCLUDES] | ||
+ | |||
+ | # Read common prefixes. If any customizations available -- read them from | ||
+ | # common.local | ||
+ | before = common.conf | ||
+ | |||
+ | [DEFAULT] | ||
+ | |||
+ | _daemon = sshd | ||
+ | |||
+ | # optional prefix (logged from several ssh versions) like " | ||
+ | __pref = (?: | ||
+ | # optional suffix (logged from several ssh versions) like " [preauth]" | ||
+ | __suff = (?: \[preauth\])? | ||
+ | __on_port_opt = (?: port \d+)?(?: on \S+(?: port \d+)?)? | ||
+ | |||
+ | # for all possible (also future) forms of "no matching (cipher|mac|MAC|compression method|key exchange method|host key type) found", | ||
+ | # see ssherr.c for all possible SSH_ERR_..._ALG_MATCH errors. | ||
+ | __alg_match = (?:(?:\w+ (? | ||
+ | |||
+ | [Definition] | ||
+ | |||
+ | prefregex = ^< | ||
+ | |||
+ | cmnfailre = ^[aA]uthentication (?: | ||
+ | ^User not known to the underlying authentication module for < | ||
+ | ^Failed \S+ for invalid user < | ||
+ | ^Failed \b(? | ||
+ | ^< | ||
+ | ^[iI](?: | ||
+ | ^User < | ||
+ | ^User < | ||
+ | ^User < | ||
+ | ^Connection closed by < | ||
+ | ^refused connect from \S+ \(< | ||
+ | ^Received < | ||
+ | ^User < | ||
+ | ^User < | ||
+ | ^pam_unix\(sshd: | ||
+ | ^(error: )?maximum authentication attempts exceeded for < | ||
+ | ^User < | ||
+ | ^< | ||
+ | ^< | ||
+ | ^< | ||
+ | ^< | ||
+ | |||
+ | mdre-normal = | ||
+ | |||
+ | mdre-ddos = ^Did not receive identification string from < | ||
+ | ^Connection < | ||
+ | ^< | ||
+ | ^Read from socket failed: Connection < | ||
+ | |||
+ | mdre-extra = ^Received < | ||
+ | ^Unable to negotiate with < | ||
+ | ^Unable to negotiate a < | ||
+ | ^no matching < | ||
+ | |||
+ | mdre-aggressive = %(mdre-ddos)s | ||
+ | %(mdre-extra)s | ||
+ | |||
+ | cfooterre = ^< | ||
+ | |||
+ | failregex = %(cmnfailre)s | ||
+ | < | ||
+ | %(cfooterre)s | ||
+ | |||
+ | # Parameter " | ||
+ | # Usage example (for jail.local): | ||
+ | # | ||
+ | # mode = extra | ||
+ | # # or another jail (rewrite filter parameters of jail): | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | mode = normal | ||
+ | |||
+ | #filter = sshd[mode=aggressive] | ||
+ | |||
+ | ignoreregex = | ||
+ | |||
+ | maxlines = 1 | ||
+ | |||
+ | journalmatch = _SYSTEMD_UNIT=sshd.service + _COMM=sshd | ||
+ | |||
+ | datepattern = {^LN-BEG} | ||
+ | |||
+ | # DEV Notes: | ||
+ | # | ||
+ | # " | ||
+ | # it is coming before use of < | ||
+ | # and later catch-all' | ||
+ | # | ||
+ | # | ||
+ | # Author: Cyril Jaquier, Yaroslav Halchenko, Petr Voralek, Daniel Black and Sergey Brester aka sebres | ||
+ | # Rewritten using prefregex (and introduced " | ||
+ | |||
+ | </ | ||
+ | |||
+ | **/ | ||
+ | |||
+ | <code bash> | ||
+ | [INCLUDES] | ||
+ | before = common.conf | ||
+ | |||
+ | [Definition] | ||
+ | #failregex = .*\[UFW BLOCK\] IN=.* SRC=< | ||
+ | failregex = UFW BLOCK.* SRC=< | ||
+ | ignoreregex = | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Testování regulárního výrazu vlastního filtru ==== | ||
+ | |||
+ | Tady je ukázka, jak lze otestovat vlastní filtr. | ||
+ | |||
+ | <code bash> | ||
+ | fail2ban-regex | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Editace fail2ban.conf ===== | ||
+ | |||
+ | Tady popisuji hodnoty, které jde oproti defaultnímu nastavení změnil. | ||
+ | |||
+ | < | ||
+ | dbpurgeage = 10d | ||
+ | </ | ||
+ | |||
+ | ===== Editace jail.local ===== | ||
+ | |||
+ | |||
+ | Nejprve je třeba v souboru jail.local nastavit banaction i na ipv6 a bannování skrz ipset. V provozu může vzniknout docela dost ip adres, které je třeba bannovat, tak bude rozumné kvůli rychlosti firewallu použít ipset místo defaultního nastavení: | ||
+ | |||
+ | < | ||
+ | banaction = iptables-ipset-proto6 | ||
+ | banaction_allports = iptables-ipset-proto6-allports | ||
+ | </ | ||
+ | |||
+ | Zkoušel jsem zvolit banaction = ufw, ale tady se zabanovala celá ip adresa a já chci blokovat pouze konkrétní službu. Zejména co se ssh týče. | ||
+ | |||
+ | Služby, na kterých má fail2ban vyset se musí zapnout pomocí enabled = true. U SSH jsem volil mód aggressive. Blokace je 10 minut pro všechny služby, pokud zkusí 3 a více pokusů za posledních 10 minut. | ||
+ | |||
+ | < | ||
+ | # " | ||
+ | bantime | ||
+ | |||
+ | # A host is banned if it has generated " | ||
+ | # seconds. | ||
+ | findtime | ||
+ | |||
+ | # " | ||
+ | maxretry = 3 | ||
+ | </ | ||
+ | |||
+ | |||
+ | Tady se odehrává zbytek nastavení fail2ban. Sepisuji zejména změny, které jsem provedl. U SSH jsem si změnil port a to je potřeba promítnout i v konfiguraci. Je tam vidět port 3333. | ||
+ | |||
+ | < | ||
+ | |||
+ | ignoreip = 127.0.0.1/8 ::1 | ||
+ | |||
+ | [sshd] | ||
+ | |||
+ | # To use more aggressive sshd modes set filter parameter " | ||
+ | # normal (default), ddos, extra or aggressive (combines all). | ||
+ | # See " | ||
+ | mode = aggressive | ||
+ | port = ssh,3333 | ||
+ | filter | ||
+ | logpath = %(sshd_log)s | ||
+ | backend = %(sshd_backend)s | ||
+ | enabled = true | ||
+ | |||
+ | [nextcloud] | ||
+ | |||
+ | enabled | ||
+ | port = http,https | ||
+ | filter | ||
+ | logpath | ||
+ | maxretry = 6 | ||
+ | |||
+ | |||
+ | #[ufwban] | ||
+ | #enabled = false | ||
+ | #port = ssh, http, https | ||
+ | #filter = ufwban | ||
+ | #logpath = / | ||
+ | #action = ufw | ||
+ | |||
+ | </ | ||
+ | |||
+ | Dále jsem přidal sshd-slow, který zabanuje ssh pro případ, že se za 1 den někdo zkusí celkem 10x přihlásit. V tomto případě zablokuje na 1week. To už je docela problematický prohřešek. | ||
+ | |||
+ | < | ||
+ | |||
+ | [sshd-slow] | ||
+ | filter | ||
+ | port = ssh, 3333 | ||
+ | maxretry = 6 | ||
+ | findtime = 1d | ||
+ | bantime | ||
+ | logpath | ||
+ | backend | ||
+ | enabled | ||
+ | |||
+ | |||
+ | [vpnserver] | ||
+ | enabled = true | ||
+ | port = 443, | ||
+ | protocol = udp | ||
+ | logpath | ||
+ | banaction = iptables-allports | ||
+ | # Uncomment the following line if you want to be notified about banned IP's | ||
+ | action= %(action_mwl)s | ||
+ | filter=vpnserver | ||
+ | |||
+ | |||
+ | [vpnserver-securenat] | ||
+ | enabled = true | ||
+ | logpath = / | ||
+ | port = all | ||
+ | protocol = all | ||
+ | banaction = iptables-allports | ||
+ | # Uncomment the following line if you want to be notified about banned IP's | ||
+ | action= %(action_mwl)s | ||
+ | filter=vpnserver | ||
+ | </ | ||
+ | |||
+ | Pozapínal jsem další smysluplné věci, jako apache, fakegoogle boty atp. | ||
+ | |||
+ | Zapnul jsem i recidive. Pokud někdo bude mít ban 3x za 12 hodin, bude celá IP adresa zablokovaná na 1 týden. Tady budu muset být opatrný a kontrolovat, | ||
+ | |||
+ | < | ||
+ | [recidive] | ||
+ | enabled = true | ||
+ | logpath | ||
+ | banaction = %(banaction_allports)s | ||
+ | bantime | ||
+ | findtime = 12h | ||
+ | </ | ||
+ | |||
+ | Poslední, co jsem aplikoval je ochrana na portscan. Tady je to opět potenciálně nebezpečná záležitost u webserveru. V praxi to prochází log, hledá jednoduchý řetězec, zda byla ip adresa zablokovaná a pokud byla zablokovaná 10x, tak proběhne ban skrz ufw. | ||
+ | |||
+ | Problémy jsou tam tyto: | ||
+ | * Nečíslovaný seznamNečíslovaný seznamufw mám na loglevel = low, takže když někdo oskenuje všechny porty, tak se neprovedou tisíce záznamů do logu, ale jenom pár. Zvyšovat hodnotu zase nemá smysl kvůli zpomalení sítě a serveru. | ||
+ | * pokud mi tam bude padat moc IP adres, tak ufw nebude dobrá volba, kvůli velkému počtu IP adres. Může pak dojít ke zpomalení. Tady je řešení vytvořit v **action.d/ | ||
+ | |||
+ | < | ||
+ | [ufw-port-scan] | ||
+ | enabled = true | ||
+ | filter | ||
+ | logpath = / | ||
+ | banaction = ufw-portscan | ||
+ | bantime | ||
+ | maxretry | ||
+ | findtime | ||
+ | </ | ||
+ | |||
+ | |||
+ | zdroje ohledně aplikace portscanu: | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | ===== Vyřazení IP adresy z banlistu ===== | ||
+ | |||
+ | <code bash> | ||
+ | fail2ban-client set vpnserver unbanip 89.24.33.115 | ||
+ | fail2ban-client unban 89.24.33.115 | ||
+ | </ | ||
+ | |||
+ | ===== Vypsání všech zabanovaných adres ===== | ||
+ | |||
+ | <code bash> | ||
+ | fail2ban-client banned | ||
+ | </ | ||