iptables에 IP, 포트, IP 추가

iptables에 IP, 포트, IP 추가

이제 이것을 실행합니다.

create blockipset hash:ip
add blockipset 192.168.1.5 -exist
add blockipset 192.168.3.115 -exist

iptables 및 ipset이 ip, 포트 및 ip를 차단할 수 있습니까? 예를 들어 목록에는 다음이 포함됩니다.

192.168.1.5
192.168.3.115
192.168.1.55,80
192.168.1.53,22

답변1

ipset이 명령을 사용하여 서로 다른 유형의 요소를 동일한 컬렉션에 넣을 수는 없습니다 . 그러나 각 유형마다 하나씩 다른 컬렉션을 사용할 수 있습니다(전체 목록은 를 통해 사용 가능 ipset help).

hash:ip
hash:ip,port

예를 들어:

ipset create blocklistip hash:ip
ipset create blocklistipport hash:ip,port

ipset add blocklistip 192.0.2.3
ipset add blocklistipport 192.0.2.2,80
ipset add blocklistipport 192.0.2.3,udp:53

udp:위에서 언급한 대로, 달리 명시적으로 명시하지 않는 한( UDP의 경우, sctp:SCTP의 경우 ...) 포트의 프로토콜은 기본적으로 TCP입니다 .

이제 스크립트는 내용을 알게 된 요소의 유형을 확인해야 합니다.IP 세트추가하겠습니다. 여기의 간단한 예는 ,파일에서 목록을 읽을 때 목록을 어디에 넣을지 확인하는 것 입니다 blocklist.txt.

while read -r element; do
    if echo $element|grep -q ,; then
        ipset add blocklistipport $element
    else
        ipset add blocklistip $element
    fi
done < blocklist.txt

목록의 모든 항목을 차단할 수 있습니다. 예를 들면 다음과 같습니다.

iptables -A INPUT -m set --match-set blocklistip src -j DROP
iptables -A INPUT -m set --match-set blocklistipport src,dst -j DROP

위의 내용은 src,dst패킷에서 일치하는 항목을 찾을 때 패킷의 소스 IP 주소와 대상 포트 주소를 사용하는 것을 의미합니다 hash:ip,port.

반품,IP 세트list:set다른 컬렉션 목록으로 구성된 특수 컬렉션이 있습니다. 명령을 사용하여 컬렉션을 개별적으로 채우는 방법은 변경되지 않지만 ipset다음과 같이 할 수 있습니다.

ipset create blocklist list:set
ipset add blocklist blocklistip
ipset add blocklist blocklistipport

이전 두 개를 교체합니다.iptables다음 중 하나만 포함하는 규칙:

iptables -A INPUT -m set --match-set blocklist src,dst -j DROP

이것은 당신의 목표를 달성하는 데 도움이 될 것입니다: 이 싱글iptables규칙은 포트가 있거나 없는 컬렉션 요소를 올바르게 처리합니다.ipset에 녹화됨.

관련 정보