29-Openwrt 防攻击

DOS攻击很常见,路由器可以通过iptables做一些简单的攻击防护,这边主要介绍模拟攻击的工具和防护攻击的手段。

1、SYN Flood

是当前最流行的DoS(拒绝服务攻击)与DDoS(Distributed Denial Of Service分布式拒绝服务攻击)的方式之一,这是一种利用TCP协议缺陷,发送大量伪造的TCP连接请求,使被攻击方资源耗尽(CPU满负荷或内存不足)的攻击方式 [3] 。

SYN Flood攻击的过程在TCP协议中被称为三次握手(Three-way Handshake),而SYN Flood拒绝服务 [3] 。

攻击就是通过三次握手而实现的 [3] 。
(1)攻击者向被攻击服务器发送一个包含SYN标志的TCP报文,SYN(Synchronize)即同步报文。同步报文会指明客户端使用的端口以及TCP连接的初始序号。这时同被攻击服务器建立了第一次握手 [3] 。
(2)受害服务器在收到攻击者的SYN报文后,将返回一个SYN+ACK的报文,表示攻击者的请求被接受,同时,TCP序号被加一,ACK(Acknowledgment)即确认,这样就同被攻击服务器建立了第二次握手 [3] 。
(3)攻击者也返回一个确认报文ACK给受害服务器,同样TCP序列号被加一,到此一个TCP连接完成,三次握手完成 [3] 。

具体原理是:TCP连接的三次握手中,假设一个用户向服务器发送了SYN报文后突然死机或掉线,那么服务器在发出SYN+ACK应答报文后是无法收到客户端的ACK报文的(第三次握手无法完成),这种情况下服务器端一般会重试(再次发送SYN+ACK给客户端)并等待一段时间后丢弃这个未完成的连接。这段时间的长度我们称为SYN Timeout,一般来说这个时间是分钟的数量级(大约为30秒-2分钟);一个用户出现异常导致服务器的一个线程等待1分钟并不是什么很大的问题,但如果有一个恶意的攻击者大量模拟这种情况(伪造IP地址),那么服务器端将为了维护一个非常大的半连接列表而消耗非常多的资源。即使是简单的保存并遍历也会消耗非常多的CPU时间和内存,何况还要不断对这个列表中的IP进行SYN+ACK的重试。

实际上,如果服务器的TC P/IP栈不够强大,那么最后的结果往往是堆栈溢出崩溃——即使服务器端的系统足够强大,服务器端也将忙于处理攻击者伪造的TCP连接请求而无暇理睬客户的正常请求(毕竟客户端的正常请求比率非常之小),此时,从正常客户的角度看来,服务器失去响应,这种情况就称做:服务器端受到了SYN Flood攻击(SYN洪水攻击) [3] 。

1.1 使用hping3发起sys flood 攻击

hping3 -q -n -a 攻击ip -S -s 源端口 --keep -p 目的端口 --flood 被攻击IP。使用CTRL+C停止攻击

源地址固定-a 10.1.2.111,10.1.2.111的33端口一直往10.1.2.1的80端口发送sync。

linye@linye-ubuntu:~$ sudo hping3 -q -n -a 10.1.2.111 -S -s 33 --keep  -p 80 --flood 10.1.2.1
HPING 10.1.2.1 (enx2c16dba41e59 10.1.2.1): S set, 40 headers + 0 data bytes
hping in flood mode, no replies will be shown
^C
--- 10.1.2.1 hping statistic ---
4822572 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max = 0.0/0.0/0.0 ms

可以看到这是访问10.1.2.1的web页面会很卡,ssh进入后台敲命令都有点卡。

查看netstat,很多TIME_WAIT的连接。

root@openwrt:/# netstat 
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 localhost:1883          localhost:54072         ESTABLISHED 
tcp        0      0 192.168.18.145:47936    120.53.213.248:https    TIME_WAIT   
tcp        0      0 192.168.18.145:57474    42.81.176.157:www       TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:41932  TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:42010  TIME_WAIT   
tcp        0      0 localhost:54076         localhost:1883          ESTABLISHED 
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:42000  TIME_WAIT   
tcp        0      0 192.168.18.145:47902    120.53.213.248:https    TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:41960  TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:42058  TIME_WAIT   
tcp        0      0 192.168.18.145:52624    120.53.129.190:7885     ESTABLISHED 
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:41974  TIME_WAIT   
tcp        0      0 openwrt.lan:6885         px30_64.lan:35320       ESTABLISHED 
tcp        0      0 localhost:1883          localhost:54076         ESTABLISHED 
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:41956  TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:41972  TIME_WAIT   
tcp        0      0 localhost:54080         localhost:1883          ESTABLISHED 
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:41964  TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:42038  TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:42064  TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:42050  TIME_WAIT   
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:42060  ESTABLISHED 
tcp        0      0 openwrt.lan:www          linye-ubuntu.lan:41962  TIME_WAIT   

CPU使用率也达到97%

root@openwrt:~# mpstat -P ALL 1
Linux 4.19.81 (openwrt)  03/09/23        _aarch64_       (2 CPU)

15:45:58     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
15:45:59     all    0.52    0.00    0.52    0.00    1.03   92.78    0.00    0.00    5.15
15:45:59       0    0.00    0.00    2.13    0.00    2.13   85.11    0.00    0.00   10.64
15:45:59       1    1.00    0.00    0.00    0.00    0.00   99.00    0.00    0.00    0.00
1.2 疑问:一键有syn_flood防护了,怎么还是这样

openwrt默认已经有syn_flood了

config defaults
        option syn_flood '1'
        option input 'ACCEPT'
        option output 'ACCEPT'
        option forward 'REJECT'

fw3_load_defaults里面会初始化syn_flood

root@openwrt:~# fw3 print | grep flood
iptables -t filter -N syn_flood
iptables -t filter -A syn_flood -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m limit --limit 25/sec --limit-burst 50 -m comment --comment "!fw3" -j RETURN
iptables -t filter -A syn_flood -m comment --comment "!fw3" -j DROP
iptables -t filter -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m comment --comment "!fw3" -j syn_flood

查看iptables都没有进入到DROP里面,丢弃

oot@openwrt:~# iptables -nvL syn_flood
Chain syn_flood (1 references)
 pkts bytes target     prot opt in     out     source               destination         
 1573 94024 RETURN     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x17/0x02 limit: avg 25/sec burst 50 /* !fw3 */
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */

之后发送syn flood的时候使用源地址随机–rand-source

linye@linye-ubuntu:~$ sudo hping3 -q -n --rand-source -S -s 33 --keep  -p 80 --flood 10.1.2.1
HPING 10.1.2.1 (enx2c16dba41e59 10.1.2.1): S set, 40 headers + 0 data bytes
hping in flood mode, no replies will be shown
^C
--- 10.1.2.1 hping statistic ---
17987172 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max = 0.0/0.0/0.0 ms

可以看到进入了iptables的DROP里面,但是还是卡,CPU还是高。。。

root@openwrt:~# iptables -nvL syn_flood
Chain syn_flood (1 references)
 pkts bytes target     prot opt in     out     source               destination         
 1179 50484 RETURN     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x17/0x02 limit: avg 25/sec burst 50 /* !fw3 */
2681K  107M DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */
2、ICMP Flood
2.1 使用hping3发起icmp flood 攻击

ubuntu安装hping3工具

linye@linye-ubuntu:~$ sudo hping3 -d 500 --icmp 10.1.2.1 --rand-source --flood
[sudo] linye 的密码: 
HPING 10.1.2.1 (enx2c16dba41e59 10.1.2.1): icmp mode set, 28 headers + 500 data bytes
hping in flood mode, no replies will be shown
^C
--- 10.1.2.1 hping statistic ---
1575816 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max = 0.0/0.0/0.0 ms
2.2 添加防护

iptables添加icmp规则,设置每秒的最高流速,当流速达到阈值后,就直接DROP。

iptables -t filter -N icmp_flood
iptables -t filter -I FORWARD 4 -p icmp --icmp-type 8 -j icmp_flood
iptables -t filter -F icmp_flood

iptables -t filter -A icmp_flood -p icmp --icmp-type 8 -m limit --limit 15/s --limit-burst 30 -j RETURN
iptables -t filter -A icmp_flood -j DROP

删除规则

iptables -t filter -D FORWARD -p icmp --icmp-type 8 -j icmp_flood
iptables -t filter -F icmp_flood
iptables -t filter -X icmp_flood

实用安全工具hping3之ICMP Flood攻击:https://zhuanlan.zhihu.com/p/518544059?utm_id=0

https://blog.csdn.net/qq_22310551/article/details/124427092

3、DHCP Flood

攻击者持续大量地向DHCP服务器申请IP地址,直到耗尽DHCP服务器地址池的IP地址,使DHCP服务器无法再给正常的主机分配IP地址

在PC机给DHCP Server发送的DHCP Discover 报文中有一个CHADDR字段,该字段是由DHCP客户端填写的,用来表示客户端的硬件地址(MAC地址),而DHCP Server 也是根据CHADDR字段来分配IP地址的,对于不同的CHADDR,DHCP Server会分配不同的IP地址,因为DHCP Server 无法识别CHADDR的合法性,攻击者就利用这个漏洞,不断的改变CHADDR字段的值,来冒充不同的用户申请IP地址,使DHCP Server 中IP池枯竭,从而达到攻击目的。

dhcp报文_「网络安全」常见攻击篇(22)——DHCP攻击:https://blog.csdn.net/weixin_39989973/article/details/110636291

3.1 发送攻击

安装运行

sudo apt-get install yersinia
sudo yersinia -G

使用-G页面运行的时候,选择网卡总是报错,后面找到可以使用命令行的方式,如下:

sudo yersinia dhcp -interface enx2c16dba41e59 -attack 1
3.2 防护攻击
  • 针对第一种发送大量请求的攻击,我们可以通过限速来实施防御。
  • 针对第二种地址的消耗,又可以分以下两种情况:
    如果二层src MAC不变,DHCP包中的MAC发生变化;可以检测这两者的一致性,如果不一致则丢弃。
    如果二层src MAC和DHCP包中MAC一致,则构建MAC-IP-PORT-VLAN映射表,查看DHCP包中的src MAC与表中的MAC是否一致,不一致则丢弃或关闭接口

添加规则:

iptables -t filter -N dhcp_flood
iptables -t filter -I INPUT 5 -p udp --dport 67 -j dhcp_flood
iptables -t filter -F dhcp_flood

iptables -t filter -A dhcp_flood -p udp --dport 67  -m limit --limit 45 --limit-burst 90 -j RETURN
iptables -t filter -A dhcp_flood -j DROP

删除规则

iptables -t filter -D INPUT -p udp --dport 67 -j dhcp_flood
iptables -t filter -F dhcp_flood
iptables -t filter -X dhcp_flood 

DHCP欺骗攻击—模拟:https://www.cnblogs.com/zhaotu/p/16048902.html
yersinia工具的使用(vlan跳跃,stp攻击等):https://blog.csdn.net/qq_41725312/article/details/89502604

4、ARP欺骗

arpspoof 命令的工作过程分为两部分:

1.发送伪造的ARP请求包,修改目标主机中ARP缓存表,实施ARP欺骗;

2.当攻击完成后,再次发送伪造的ARP请求包,修改目标主机中ARP缓存表为正确的值,结束ARP欺骗。

-i 参数用于指定一块网卡,必须显式地指定,没有默认值,若不指定,会报错为:arpspoof: couldn’t arp for host xxx.xxx.xxx.xxx。 若不清楚自己机器上都有哪些网卡,可用命令 ifconfig 查看。

-t 和 -r 参数与第一部分有关,-c 参数与第二部分有关。第二部分被作者称为:cleaning up,-c 中的c大概就是从这里来的。

-t 后可以是IP地址,也可以是域名,这里只使用IP地址。

-t 参数后的第一个IP地址是要欺骗的主机,第二个IP地址是你要伪装成的主机,如:

ubuntu关闭ip转发

linye@linye-ubuntu:~$ sudo su
root@linye-ubuntu:/home/linye# echo 0 > /proc/sys/net/ipv4/ip_forward
root@linye-ubuntu:/home/linye# exit
exit
linye@linye-ubuntu:~$ cat /proc/sys/net/ipv4/ip_forward
0

ubuntu上执行如下命令,欺骗window(10.1.2.152),ubuntu的地址是10.1.2.1

linye@linye-ubuntu:~$ sudo arpspoof -i enx2c16dba41e59 -t 10.1.2.152 10.1.2.1
2c:16:db:a4:1e:59 0:c:29:a6:b8:8a 0806 42: arp reply 10.1.2.1 is-at 2c:16:db:a4:1e:59
2c:16:db:a4:1e:59 0:c:29:a6:b8:8a 0806 42: arp reply 10.1.2.1 is-at 2c:16:db:a4:1e:59
2c:16:db:a4:1e:59 0:c:29:a6:b8:8a 0806 42: arp reply 10.1.2.1 is-at 2c:16:db:a4:1e:59
2c:16:db:a4:1e:59 0:c:29:a6:b8:8a 0806 42: arp reply 10.1.2.1 is-at 2c:16:db:a4:1e:59
2c:16:db:a4:1e:59 0:c:29:a6:b8:8a 0806 42: arp reply 10.1.2.1 is-at 2c:16:db:a4:1e:59
2c:16:db:a4:1e:59 0:c:29:a6:b8:8a 0806 42: arp reply 10.1.2.1 is-at 2c:16:db:a4:1e:59

此时在window上面查arp -a,发现如下,10.1.2.1的物理地址变成ubuntu。此时window ping www.baidu.com失败。

因为ping baidu的数据包被转发给了ubuntu,然后ubuntu又关闭了IP转发。

C:\Users\linye>arp -a

接口: 10.1.2.152 --- 0x1a
  Internet 地址         物理地址              类型
  10.1.2.1              2c-16-db-a4-1e-59     动态
  10.1.2.178            2c-16-db-a4-1e-59     动态
  10.1.2.255            ff-ff-ff-ff-ff-ff     静态

ARP欺骗工具arpspoof的用法:https://blog.csdn.net/who_im_i/article/details/120234324

使用iptables抵抗常见攻击:https://blog.csdn.net/ixidof/article/details/42584329

拒绝服务攻击:https://baike.baidu.com/item/%E6%8B%92%E7%BB%9D%E6%9C%8D%E5%8A%A1%E6%94%BB%E5%87%BB/421896?fromtitle=dos%E6%94%BB%E5%87%BB&fromid=3792374&fr=aladdin

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值