很多时候我们会遇到服务器遭受 cc 或 syn 等攻击,如果发现自己的网站访问异常缓慢且流量异常。可以使用系统内置 netstat 命令,简单判断一下服务器是否被攻击。常用的 netstat 命令。
如果没有安装netstat的执行如下命令:
yum install netstat -y
yum install net-tools -y
1.该命令将显示所有活动的网络连接。
netstat -na
2.查看同时连接到哪个服务器 IP 比较多,cc 攻击用。使用双网卡或多网卡可用。
netstat -an|awk '{print $4}'|sort|uniq -c|sort -nr|head
3.查看哪些 IP 连接到服务器连接多,可以查看连接异常 IP。
netstat -an|awk -F: '{print $2}'|sort|uniq -c|sort -nr|head
4.显示所有 80 端口的网络连接并排序。这里的 80 端口是 http 端口,所以可以用来监控 web 服务。如果看到同一个 IP 有大量连接的话就可以判定单点流量攻击了。
netstat -an | grep :80 | sort
5.这个命令可以查找出当前服务器有多少个活动的 SYNC_REC 连接。正常来说这个值很小,最好小于 5。 当有 Dos 攻击或的时候,这个值相当的高。但是有些并发很高的服务器,这个值确实是很高,因此很高并不能说明一定被攻击。
netstat -n -p|grep SYN_REC | wc -l
6.列出所有连接过的 IP 地址。
netstat -n -p | grep SYN_REC | sort -u
7.列出所有发送 SYN_REC 连接节点的 IP 地址。
netstat -n -p | grep SYN_REC | awk '{print $5}' | awk -F: '{print $1}'
按数量排列:
netstat -n -p | grep SYN_REC | awk '{print $5}' | awk -F: '{print $1}' | cut -d: -f1 | sort | uniq -c | sort -n
8.使用 netstat 命令计算每个主机连接到本机的连接数。
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
9.列出所有连接到本机的 UDP 或者 TCP 连接的 IP 数量。
netstat -anp |grep 'tcp|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
10.检查 ESTABLISHED 连接并且列出每个 IP 地址的连接数量。
netstat -ntu | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
11.列出所有连接到本机 80 端口的 IP 地址和其连接数。80 端口一般是用来处理 HTTP 网页请求。
netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1
12.显示连接 80 端口前 10 的 ip,并显示每个 IP 的连接数。这里的 80 端口是 http 端口,所以可以用来监控 web 服务。如果看到同一个 IP 有大量连接的话就可以判定单点流量攻击了。
netstat -antp | awk '$4 ~ /:80$/ {print $4" "$5}' | awk '{print $2}'|awk -F : {'print $1'} | uniq -c | sort -nr | head -n 10
13.查看所有80端口的连接总数
netstat -nat | grep -i "80" | wc -l
14.对连接的IP按连接数量进行排序
netstat -ntu | awk '{print $5}' | cut -d : -f1 | sort | uniq -c | sort -n
15.查看TCP连接状态
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -rn
netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"t",state[key]}'
netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"t",arr[k]}'
netstat -n | awk '/^tcp/ {print $NF}' | sort | uniq -c | sort -rn
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
16.查看80端口连接数最多的20个IP
netstat -anlp | grep 80 | grep tcp | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n20
netstat -ant | awk '/:80 / {split($5,ip,":") ; ++A [ip[1]]} END {for(i in A) print i, A[i] }' | sort -rn | head -n20
17.用tcpdump嗅探80端口的访问看看谁最高
tcpdump -i em1 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr | head -20
18.查找较多time_wait连接
netstat -n | grep TIME_WAIT | awk '{print $5}' | sort | uniq -c | sort -rn | head -n20
查找较多的SYN连接
netstat -an | grep SYN | awk '{print $5}' | awk -F : '{print $1}' | sort | uniq -c | sort -nr | more
如果是攻击,可以结合防火墙规则,进行封禁。详细规则参考
http://www.33f.net/network/centos7_firewall.html
关于firewall-cmd的相关命令集锦:
#查看firewall状态,LINUX7默认是安装并开启的;
firewall-cmd --state
#安装
yum install firewalld
#启动,
systemctl start firewalld
#设置开机启动
systemctl enable firewalld
#关闭
systemctl stop firewalld
#取消开机启动
systemctl disable firewalld
#禁止IP(123.56.161.140)访问机器
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="123.56.161.140" drop'
#禁止一个IP段,比如禁止123.56..
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="123.56.0.0/16" drop'
#禁止一个IP段,比如禁止123.56.161.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="123.56.161.0/24" drop'
#禁止机器IP(123.56.161.140)从防火墙中删除
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address="123.56.161.140" drop'
#允许http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --add-service=http
#关闭http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --remove-service=http
#允许端口:3389
firewall-cmd --permanent --add-port=3389/tcp
#允许端口:1-3389
firewall-cmd --permanent --add-port=1-3389/tcp
允许某ip 1.1.1.1访问该服务器:
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="1.1.1.1" accept'
允许某IP 1.1.1.1 访问服务器的80端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="1.1.1.1" port protocol="tcp" port="80" accept'
禁止某个服务器上的IP被访问某端口,用于某个ip被攻击的情况。
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" destination address="1.1.1.1" port protocol="tcp" port="80" drop'
#关闭放行中端口:3389
firewall-cmd --permanent --remove-port=3389/tcp
#查看firewall的状态
firewall-cmd --state
#查看防火墙规则(只显示/etc/firewalld/zones/public.xml中防火墙策略,在配置策略前,我一般喜欢先CP,以后方便直接还原)
firewall-cmd --list-all
#查看所有的防火墙策略(即显示/etc/firewalld/zones/下的所有策略)
firewall-cmd --list-all-zones
#重新加载配置文件
firewall-cmd --reload
#更改配置后一定要重新加载配置文件:
firewall-cmd --reload
批量屏蔽目标IP的命令:前20条
1、输入前20条连接过多的ip
netstat -an|awk -F: '{print $2}'|sort|uniq -c|sort -nr|head -n 20 > /root/ccips.txt2、批量屏蔽
awk '{sub("IP:", "", $3); print $3}' /root/ccips.txt | xargs -n1 -I{} firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address={} drop'
如果是iptables那使用
awk '{sub("IP:", "", $2); print $2}' /tmp/test | xargs -n1 -I{} iptables -A INPUT -s {} -j DROP 版权属于: 三三世界-百宝箱
本文链接: http://33f.net/safe/linux_netstat_attack.html
本文最后更新于2022年05月08日 ,已超过1425天没有更新,若内容或图片失效,请留言反馈。
本文允许转载,但请在转载时请以超链接或其它形式标明文章出处
@Doug Shume it's ok for me , you can post here.
zh.us.to 有效
kms.03k.org 有效
kms.chinancce.com
kms.shuax.com 有效
kms.dwhd.org 有效
kms.luody.info 有效
kms.digiboy.ir 有效
kms.lotro.cc 有效
www.zgbs.cc 有效
cy2617.jios.org 有效
@ 权限问题,试试sudo 再加命令。
你好提示Permission denied 怎么办啊