大网数据广告活动
服务器运维问答

UFW (firewall) not blocking UDP right away

· 52CE 我爱测

I have a PBX (phone system called Astersik ) that works with udp and tcp. Sometimes I get invalid request from some IP addresses that I will like to block. I cannot block those requests until I reboot my computer. I think the rea…

问题原文

原帖链接 · Tono Nam · CC BY-SA 3.0

I have a PBX (phone system called Astersik) that works with udp and tcp. Sometimes I get invalid request from some IP addresses that I will like to block. I cannot block those requests until I reboot my computer. I think the reason is because UFW will not block established connections.

Let's pretend I am getting malicious request from the ip address 1.2.3.4. I then need to block those request by executing the command:

sudo ufw insert 1 deny from 1.2.3.4 to any

I need the insert 1 so that the rule is executed before the other ones.

Anyways even though I execute that command I still see that my computer replies back to 1.2.3.4. The problem is because there is already an establish connection therefore ufw will not drop those packets until those connections are closed.

I found a temporary solution:

1) I open nano /etc/ufw/before.rules

2) Comment the lines:

(In the picture the lines are not commented. But comment everything that is inside the red circle)

3) Restart firewall ufw disable then ufw enable

If I comment those lines then my firewall will work like I want. It will block connections right away!

The problem is that commenting those lines causes my dns to stop working In other words now when I execute ping google.com I see uknown host google.com

Why dns stops working when I comment those lines? In short I need that when I execute sudo ufw insert 1 deny from 1.2.3.4 to any I get no more requests from that IP!. I cannot afford to reboot the computer every time I want to block an ip address.

已采纳解答

原帖链接 · user5870571 · CC BY-SA 3.0

Tono,

ufw is a front end for iptables. What you are asking essentially, is why does DNS fail when you stop accepting Related,Established connections. conntrack keeps track of your connections. If traffic matches an entry in conntrack then it is Established. If an Established connection creates a new connection then it is Related. If the traffic is new and is not Established or Related then it is New.

If there is already an established connection you need to insert a drop instruction at the beginning of the chain. Edit /etc/ufw/before.rules so you have a block section before the rules you commented. Add an entry of -A ufw-before-input -s 1.2.3.4 -j DROP there. Then use sudo ufw reload.

If you still have problems with that think about using iptables instead of ufw because ufw is a front end for iptables and does not include all of its selections.

https://www.cyberciti.biz/faq/how-to-block-an-ip-address-with-ufw-on-ubuntu-linux-server/

https://help.ubuntu.com/community/UFW

https://askubuntu.com/questions/602176/configure-ufw-to-allow-only-established-and-related-conections-on-ipv4

enter link description here

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。