美文网首页
iptables使用

iptables使用

作者: leitty | 来源:发表于2019-12-19 09:07 被阅读0次

    常用命令

    最近在云主机的日志`/var/log/secure`里发现一些恶意尝试登陆的IP,具体表现是不断使用root用户或其他用户尝试登陆失败,尽管设置了强密码,但被这样扫描还是不爽,所以考虑用iptables屏蔽它。如下:

    ```

    ...

    Dec 15 03:16:42 vultr sshd[8242]: Failed password for root from 111.67.202.86 port 47456 ssh2

    Dec 15 03:16:42 vultr sshd[8246]: Failed password for root from 61.177.172.128 port 37898 ssh2

    Dec 15 03:16:42 vultr sshd[8248]: Failed password for root from 20.188.4.3 port 33470 ssh2

    ...

    Dec 17 23:25:17 vultr sshd[26352]: Invalid user patrice from 92.50.249.166 port 58804

    Dec 17 23:25:18 vultr sshd[26354]: Invalid user carlo from 115.94.204.156 port 59334

    Dec 17 23:25:22 vultr sshd[26356]: Invalid user papakyriakou from 201.161.58.237 port 48404

    Dec 17 23:25:32 vultr sshd[26360]: Invalid user passwd444 from 154.8.138.184 port 35270

    Dec 17 23:27:13 vultr sshd[26370]: Invalid user debost from 193.109.123.210 port 56728

    Dec 17 23:28:03 vultr sshd[26375]: Invalid user nw from 217.182.74.125 port 59100

    Dec 17 23:28:22 vultr sshd[26378]: Invalid user dauchez from 104.248.117.234 port 60202

    Dec 17 23:28:23 vultr sshd[26380]: Invalid user maimai from 182.61.28.191 port 35232

    Dec 17 23:28:25 vultr sshd[26382]: Invalid user bouillon from 139.59.72.210 port 58025

    Dec 17 23:28:30 vultr sshd[26384]: Invalid user it from 80.211.133.219 port 48879

    ...

    ```

    先从`/var/log/secure`中筛选出待屏蔽的IP,比如xx.xx.xx.xx。

    在linux中,使用iptables维护IP规则表,要封停或者解封IP,其实就是在IP规则表中对入站部分的规则进行添加操作。

    要封停一个IP,可以使用以下命令:

    ```

    iptables -I INPUT -s ... -j DROP

    ```

    要解封一个IP,使用以下命令:

    ```

    iptables -D INPUT -s ... -j DROP

    ```

    参数-l表示Insert(添加),-D表示Delete(删除)。后面跟的是规则,INPUT表示入站,...表示要封的IP,DROP表示放弃连接。

    例如,想封掉`112.85.42.175`这个IP,可以使用:

    ```

    iptables -I INPUT -s 112.85.42.175 -j DROP

    ```

    封IP段的命令:

    ```

    iptables -I INPUT -s 124.85.0.0/16 -j DROP

    ```

    封整个段:

    ```

    iptables -I INPUT -s 124.85.0.0/8 -j DROP

    ```

    只封几个段的80端口:

    ```

    iptables -I INPUT -p tcp –dport 80 -s 124.115.0.0/24 -j DROP

    ```

    禁止指定的端口:

    ```

    iptables -A INPUT -p tcp --dport 80 -j DROP

    ```

    开放指定的端口:

    ```

    iptables -A INPUT -p tcp --dport 80 -j ACCEPT

    ```

    拒绝所有的端口:

    ```

    iptables -A INPUT -j DROP

    ```

    可以通过以下命令查看当前的IP规则:

    ```

    iptables -L/--list

    ```

    如果想清空封掉的IP地址,可以输入:

    ```

    iptables -F/--flush

    ```

    ipset

    当需要屏蔽的IP较多时,直接使用iptables就比较麻烦,这时候可以使用ipset。

    ipset是iptables的扩展,也就是允许创建匹配地址的集合。与普通的iptables链只能单IP匹配不同,通过ipset创建的,ip集合存储在带索引的数据结构中,这种结构在集合较大时也能进行高效的查找。ipsets也具备一些新防火墙设计方法,并简化了配置.官网:http://ipset.netfilter.org/

    安装与使用

    yum 安装:

    ```

    [root@VM_0_12_centos ~]# yum install ipset

    ```

    创建一个ipset:

    ```

    [root@VM_0_12_centos ~]# ipset create allset hash:net  (也可以是hash:ip ,这指的是单个ip)

    ```

    可以通过`ipset -h`查看能创建的ipset类型。

    查看创建的ipset:

    ```

    [root@VM_0_12_centos ~]# ipset list

    Name: allset

    Type: hash:net

    Revision: 3

    Header: family inet hashsize 1024 maxelem 65536

    Size in memory: 16784

    References: 0

    Members:

    ```

    ipset默认可以存储65536个元素,使用maxelem指定数量:

    ```

    [root@VM_0_12_centos ~]# ipset create openapi hash:net maxelem 1000000

    [root@VM_0_12_centos ~]# ipset list

    Name: allset

    Type: hash:net

    Revision: 3

    Header: family inet hashsize 1024 maxelem 65536

    Size in memory: 16784

    References: 0

    Members:

    Name: openapi

    Type: hash:net

    Revision: 3

    Header: family inet hashsize 1024 maxelem 1000000

    Size in memory: 16784

    References: 0

    Members:

    ```

    加入一个黑名单ip:

    ```

    [root@VM_0_12_centos ~]# ipset add allset 112.85.42.175

    ```

    创建防火墙规则,指定allset这个ip集合中的ip都无法访问80端口:

    ```

    iptables -I INPUT -m set --match-set allset src -p tcp --destination-port 80 -j DROP

    service iptables save

    ```

    其中`-m set`表示使用扩展模块set的功能,

    >> 注意`-m tcp`表示使用`tcp`扩展模块的功能 (`tcp`扩展模块提供了 `--dport`, `--tcp-flags`, `--sync`等功能),`-p tcp` 和`-m tcp`是两个不同层面的东西,一个是说当前规则作用于`tcp` 协议包,而后一是说明要使用`iptables`的`tcp`模块的功能 (`--dport`等)

    去掉黑名单的IP:

    ```

    ipset del allset 112.85.42.175

    ```

    该地址就又能访问了

    保存ipset规则到文件中:

    ```

    ipset save allset -f allset.txt

    ```

    导入ipset规则:

    ```

    ipset restore -f allset.txt

    ```

    删除ipset:

    ```

    ipset destroy allset

    ```

    >> ipset的一个优势是集合可以动态的修改,即使ipset的iptables规则目前已经启动,新加的入ipset的ip也生效

    实例

    例: 某服务器被CC攻击,经过抓包或者一序列手段发现有一批IP是源攻击ip,由于ip较多,如果用iptables一条一条加就麻烦些了。

    1. 确定IP

    对TIME_WAIT的外部ip以及此对ip出现的次数经行求重排序:

    ```

    netstat -ptan | grep TIME_WAIT | awk '{print $5}' | awk -F: '{print $1}' |sort |uniq -c | sort -n -r

    ```

    tcpdump 抓取100个包,访问本机80的ip进行求重排序只显示前20个,数量多的ip可能为攻击源IP,我们需要封掉它:

    ```

    tcpdump -tnn dst port 80 -c 100 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -n -r |head -20

    ```

    2. 新建文件,加入这些ip

    ```

    vim allset.txt

      add setname xxx.xxx.xxx.xxx

    ```

    3. 导入文件到ipset集中

    ```

    ipset restore -f allset.txt

    ```

    4. 查看导入是否成功

    ```

    ipset list

    ```

    5. 建立iptables规则,拦截这些攻击ip访问服务器的80端口

    ```

    iptables -I INPUT -m set --match-set allset src -p tcp --destination-port 80 -j DROP

    ```

    回到刚开始的问题,发现`/var/log/secure`中存在大量恶意尝试登陆的IP,先筛选出这些IP:

    ```

    cat /var/log/secure|grep "Failed password"| awk -F "from" '{print $2}' | awk -F "port" '{print $1}' | sort | uniq -c | sort -n -r |head -20

    ```

    在将这些ip导入ipset中,建立屏蔽规则。如果恶意ip很频繁,可以把以上步骤编写为shell脚本加到crontab中,定期屏蔽这些ip。

    相关文章

      网友评论

          本文标题:iptables使用

          本文链接:https://www.haomeiwen.com/subject/vkzpnctx.html