Firewall Blocken mit IP-Range  [GELÖST]

Diskussionen zur Bedienung von KeyHelp.
Post Reply
Engholm
Posts: 151
Joined: Fri 1. Feb 2019, 11:06

Firewall Blocken mit IP-Range

Post by Engholm »

Moin,

ich blockiere via Firewall-Regel gelegentlich manuell IPs, die einige Kontaktformulare vollspammen. Sehr viele unterschiedliche IPs kommen kommen aus dem Bereich 46.161.11.*

Die komplette IP hinterlegen,kein Problem. Wenn ich versuche ein Wildcard-Sternchen zu setzen, wird die Eingabe nicht akzeptiert. Würde gerne den o.g. Breich kommen

Frage nun: Kann man evtl. doch eine Range setzen und ich habe das nur falsch abgekürzt?

Noch eine erweiterte Frage zum Thema:
Worin genau besteht der Unterschied zwischen Verweigern und Zurückweisen bzw. welche Aktion wäre die erste Wahl bei einer IP Blacklist gegen Spammer?
User avatar
24unix
Posts: 1560
Joined: Sun 21. Jun 2020, 17:16
Location: Kollmar
Contact:

Re: Firewall Blocken mit IP-Range  [GELÖST]

Post by 24unix »

Engholm wrote: Sat 11. Jun 2022, 14:01 Moin,

ich blockiere via Firewall-Regel gelegentlich manuell IPs, die einige Kontaktformulare vollspammen. Sehr viele unterschiedliche IPs kommen kommen aus dem Bereich 46.161.11.*

Die komplette IP hinterlegen,kein Problem. Wenn ich versuche ein Wildcard-Sternchen zu setzen, wird die Eingabe nicht akzeptiert. Würde gerne den o.g. Breich kommen
Dann blockst Du 46.161.11.0/24
Engholm wrote: Sat 11. Jun 2022, 14:01
Frage nun: Kann man evtl. doch eine Range setzen und ich habe das nur falsch abgekürzt?

Noch eine erweiterte Frage zum Thema:
Worin genau besteht der Unterschied zwischen Verweigern und Zurückweisen bzw. welche Aktion wäre die erste Wahl bei einer IP Blacklist gegen Spammer?
Verweigern: Das Paket wird verworfen, die Gegenseite weiß nicht, ob auf dem Port ein Dienst läuft.
Zurückweisen: Die Gegenseite wird informiert, dass da ein Dienst ist, aber ihn nicht bedient.
Ich würde verweigern …
mfg Micha
--
If Bill Gates had a nickel for every time Windows crashed …
… oh wait, he does.
User avatar
Toorms
Posts: 137
Joined: Wed 18. Mar 2020, 20:11

Re: Firewall Blocken mit IP-Range

Post by Toorms »

Ich arbeite hiermit im cron. Hält mir eine ganze menge Schmutz vom Hals! Lasse ich @reboot sowie um 3AM (ip-blacklister) und 4AM (geo-blacklister) durchlaufen.

ip-blacklist.sh

Code: Select all

#!/bin/bash
export PATH="/bin:/usr/bin:/sbin:/usr/sbin"

strChainName=BLACKLIST
strWorkingDirectory=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
strBlacklistFolder=$strWorkingDirectory/blacklists
strLogFolder=$strWorkingDirectory/logs
strConsoleLogFile=$strLogFolder/ip-blacklister-console.log
strBlacklistFile=$strBlacklistFolder/blacklist
strBlacklistTempFile=$strBlacklistFolder/tmp-blacklist

intBlacklistingINPUTLineNo=$(iptables -n --list INPUT --line-numbers | grep -i $strChainName | sed -e 's/^\(.\{2\}\).*/\1/')
strDDOSProtectionRuleCheck=$(iptables -n --list INPUT --line-numbers | grep -i "UPDATE seconds: 60 hit_count: 10")

function ConsoleLog() {

    if [ ! -d $strLogFolder ]; then
        mkdir -p $strLogFolder
    fi

    echo "[ $(date) ] | $1 " | tee -a $strConsoleLogFile
}

function ApplicationCheck(){
    which "$1" | grep -o "$1" > /dev/null &&  return 0 || return 1 
}

function CheckRuntimePreparing(){
    aryApplications=( iptables iptables-save ipset wget tee )
    aryFolders=( $strBlacklistFolder )

    for objApps in "${aryApplications[@]}"; do
        if ( ! ApplicationCheck $objApps ); then
            ConsoleLog "$objApps not found .. install ipset, iptables and iptables-save on your server"
            exit 1
        fi
    done

    for objFolders in "${aryFolders[@]}"; do
        if [ ! -d $objFolders ]; then
            mkdir -p $objFolders
        fi
    done
}

function PrepareBacklist() {
    if [ -f $strBlacklistFile ]; then
        rm -rf $strBlacklistFile
    fi
        wget http://lists.blocklist.de/lists/all.txt -O $strBlacklistTempFile
        grep -v ":" $strBlacklistTempFile > $strBlacklistFile
        rm -rf $strBlacklistTempFile
}

function ddosProectionRules() {
    if [ -z $strDDOSProtectionRuleCheck ]; then

        iptables -t mangle -A PREROUTING -m conntrack --ctstate INVALID -j DROP
        iptables -t mangle -A PREROUTING -p tcp ! --syn -m conntrack --ctstate NEW -j DROP
        iptables -t mangle -A PREROUTING -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP
        iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
        iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
        iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
        iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP
        iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP
        iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP
        iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP
        iptables -t mangle -A PREROUTING -s 224.0.0.0/3 -j DROP
        iptables -t mangle -A PREROUTING -s 169.254.0.0/16 -j DROP
        iptables -t mangle -A PREROUTING -s 172.16.0.0/12 -j DROP
        iptables -t mangle -A PREROUTING -s 192.0.2.0/24 -j DROP
        iptables -t mangle -A PREROUTING -s 192.168.0.0/16 -j DROP
        iptables -t mangle -A PREROUTING -s 10.0.0.0/8 -j DROP
        iptables -t mangle -A PREROUTING -s 0.0.0.0/8 -j DROP
        iptables -t mangle -A PREROUTING -s 240.0.0.0/5 -j DROP
        iptables -t mangle -A PREROUTING -s 127.0.0.0/8 ! -i lo -j DROP
        iptables -t mangle -A PREROUTING -p icmp -j DROP
        iptables -t mangle -A PREROUTING -f -j DROP
        iptables -A INPUT -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset
        iptables -A INPUT -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT
        iptables -A INPUT -p tcp --tcp-flags RST RST -j DROP
        iptables -A INPUT -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT
        iptables -A INPUT -p tcp -m conntrack --ctstate NEW -j DROP
        iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --set
        iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 10 -j DROP
        iptables -N port-scanning
        iptables -A port-scanning -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s --limit-burst 2 -j RETURN
        iptables -A port-scanning -j DROP

    fi
}

ConsoleLog "Check runtime preparing for ip-blacklister ..."
CheckRuntimePreparing

ConsoleLog "Download new blacklists ..."
PrepareBacklist >> /dev/null 2>&1

ConsoleLog "Stopping fail2ban services ..."
systemctl stop fail2ban

ConsoleLog "Flush or create ipset $strChainName table ..."
if ! ipset flush $strChainName ; then
    ipset create $strChainName hash:ip
fi

ConsoleLog "Importing blacklisted ip addresses from allblacklist ..."
while IFS= read -r ipAddress
do
    ipset add $strChainName $ipAddress
done < "$strBlacklistFile"

ConsoleLog "Purging old blacklists ..."
rm -rf $strBlacklistFolder/*.* >> /dev/null 2>&1

ConsoleLog "Checking INPUT rule for inserting  of $strChainName on 1 ..."
if [ -z "$intBlacklistingINPUTLineNo" ]; then
    iptables -I INPUT 1 -m set --match-set $strChainName src -j DROP >> /dev/null 2>&1
    ConsoleLog "Rule-set $strChainName successfully insert in chain INPUT to 1 ..."
else
    ConsoleLog "All fine, rule-set $strChainName already found in chain INPUT ..."
fi

ConsoleLog "Executing ddos procetion ruleset configuration ..."
ddosProectionRules >> /dev/null 2>&1

ConsoleLog "iptables-save makes iptables rules persistent ..."
iptables-save >> /dev/null 2>&1

ConsoleLog "Starting fail2ban services ..."
systemctl start fail2ban

exit 0
geo-blacklister.sh - hierfür kannst du dich am besten hier dran orientieren -> https://docs.rackspace.com/support/how- ... -iptables/ - also du musst mir xtables addons arbeiten.

Code: Select all

#!/bin/bash

strWorkingDirectory=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
cd $strWorkingDirectory

/usr/libexec/xtables-addons/xt_geoip_dl
/usr/libexec/xtables-addons/xt_geoip_build -D /usr/share/xt_geoip *.csv

strGEOlistingINPUTLineNo=$(iptables -n --list INPUT --line-numbers | grep -i geoip)

if [ -z "$strGEOlistingINPUTLineNo" ]; then
        iptables -I INPUT 2 -m geoip --src-cc KR,CN,IN,RU,SA,TR,VN,UA,BR,VE,PK,JP -j DROP >> /dev/null 2>&1
fi

exit 0
Sieht dann im iptables in ewta so aus: (von den DDOS default rules mal abgesehen nun)

Code: Select all

Chain INPUT (policy DROP 1409 packets, 229K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     2367  146K DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            match-set BLACKLIST src
2    10101  579K DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            -m geoip --source-country KR,CN,IN,RU,SA,TR,VN,UA,BR,VE,PK,JP 
Engholm
Posts: 151
Joined: Fri 1. Feb 2019, 11:06

Re: Firewall Blocken mit IP-Range

Post by Engholm »

Vielen Dank für euern Input!

[Gelöst]
Post Reply