常用命令
最近在云主機(jī)的日志`/var/log/secure`里發(fā)現(xiàn)一些惡意嘗試登陸的IP继控,具體表現(xiàn)是不斷使用root用戶或其他用戶嘗試登陸失敗,盡管設(shè)置了強(qiáng)密碼吊输,但被這樣掃描還是不爽,所以考慮用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維護(hù)IP規(guī)則表刽肠,要封停或者解封IP免胃,其實(shí)就是在IP規(guī)則表中對(duì)入站部分的規(guī)則進(jìn)行添加操作音五。
要封停一個(gè)IP,可以使用以下命令:
```
iptables -I INPUT -s ... -j DROP
```
要解封一個(gè)IP羔沙,使用以下命令:
```
iptables -D INPUT -s ... -j DROP
```
參數(shù)-l表示Insert(添加)躺涝,-D表示Delete(刪除)。后面跟的是規(guī)則扼雏,INPUT表示入站坚嗜,...表示要封的IP,DROP表示放棄連接诗充。
例如苍蔬,想封掉`112.85.42.175`這個(gè)IP,可以使用:
```
iptables -I INPUT -s 112.85.42.175 -j DROP
```
封IP段的命令:
```
iptables -I INPUT -s 124.85.0.0/16 -j DROP
```
封整個(gè)段:
```
iptables -I INPUT -s 124.85.0.0/8 -j DROP
```
只封幾個(gè)段的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
```
可以通過(guò)以下命令查看當(dāng)前的IP規(guī)則:
```
iptables -L/--list
```
如果想清空封掉的IP地址蝴蜓,可以輸入:
```
iptables -F/--flush
```
ipset
當(dāng)需要屏蔽的IP較多時(shí)碟绑,直接使用iptables就比較麻煩,這時(shí)候可以使用ipset。
ipset是iptables的擴(kuò)展蜈敢,也就是允許創(chuàng)建匹配地址的集合辜荠。與普通的iptables鏈只能單IP匹配不同,通過(guò)ipset創(chuàng)建的抓狭,ip集合存儲(chǔ)在帶索引的數(shù)據(jù)結(jié)構(gòu)中伯病,這種結(jié)構(gòu)在集合較大時(shí)也能進(jìn)行高效的查找。ipsets也具備一些新防火墻設(shè)計(jì)方法,并簡(jiǎn)化了配置.官網(wǎng):http://ipset.netfilter.org/
安裝與使用
yum 安裝:
```
[root@VM_0_12_centos ~]# yum install ipset
```
創(chuàng)建一個(gè)ipset:
```
[root@VM_0_12_centos ~]# ipset create allset hash:net? (也可以是hash:ip 否过,這指的是單個(gè)ip)
```
可以通過(guò)`ipset -h`查看能創(chuàng)建的ipset類型午笛。
查看創(chuàng)建的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默認(rèn)可以存儲(chǔ)65536個(gè)元素,使用maxelem指定數(shù)量:
```
[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:
```
加入一個(gè)黑名單ip:
```
[root@VM_0_12_centos ~]# ipset add allset 112.85.42.175
```
創(chuàng)建防火墻規(guī)則苗桂,指定allset這個(gè)ip集合中的ip都無(wú)法訪問(wèn)80端口:
```
iptables -I INPUT -m set --match-set allset src -p tcp --destination-port 80 -j DROP
service iptables save
```
其中`-m set`表示使用擴(kuò)展模塊set的功能药磺,
>> 注意`-m tcp`表示使用`tcp`擴(kuò)展模塊的功能 (`tcp`擴(kuò)展模塊提供了 `--dport`, `--tcp-flags`, `--sync`等功能),`-p tcp` 和`-m tcp`是兩個(gè)不同層面的東西煤伟,一個(gè)是說(shuō)當(dāng)前規(guī)則作用于`tcp` 協(xié)議包癌佩,而后一是說(shuō)明要使用`iptables`的`tcp`模塊的功能 (`--dport`等)
去掉黑名單的IP:
```
ipset del allset 112.85.42.175
```
該地址就又能訪問(wèn)了
保存ipset規(guī)則到文件中:
```
ipset save allset -f allset.txt
```
導(dǎo)入ipset規(guī)則:
```
ipset restore -f allset.txt
```
刪除ipset:
```
ipset destroy allset
```
>> ipset的一個(gè)優(yōu)勢(shì)是集合可以動(dòng)態(tài)的修改,即使ipset的iptables規(guī)則目前已經(jīng)啟動(dòng)便锨,新加的入ipset的ip也生效
實(shí)例
例: 某服務(wù)器被CC攻擊围辙,經(jīng)過(guò)抓包或者一序列手段發(fā)現(xiàn)有一批IP是源攻擊ip,由于ip較多放案,如果用iptables一條一條加就麻煩些了姚建。
1. 確定IP
對(duì)TIME_WAIT的外部ip以及此對(duì)ip出現(xiàn)的次數(shù)經(jīng)行求重排序:
```
netstat -ptan | grep TIME_WAIT | awk '{print $5}' | awk -F: '{print $1}' |sort |uniq -c | sort -n -r
```
tcpdump 抓取100個(gè)包,訪問(wèn)本機(jī)80的ip進(jìn)行求重排序只顯示前20個(gè)吱殉,數(shù)量多的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. 導(dǎo)入文件到ipset集中
```
ipset restore -f allset.txt
```
4. 查看導(dǎo)入是否成功
```
ipset list
```
5. 建立iptables規(guī)則友雳,攔截這些攻擊ip訪問(wèn)服務(wù)器的80端口
```
iptables -I INPUT -m set --match-set allset src -p tcp --destination-port 80 -j DROP
```
回到剛開始的問(wèn)題稿湿,發(fā)現(xiàn)`/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導(dǎo)入ipset中沥阱,建立屏蔽規(guī)則缎罢。如果惡意ip很頻繁,可以把以上步驟編寫為shell腳本加到crontab中考杉,定期屏蔽這些ip策精。