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

DROP packets (instead of REJECT) from specific port when no application is listening?

· 52CE 我爱测

I'm using Ubuntu 18.04 with iptables . I want that attackers would see a consistent state when trying to map my ports. I want all my ports to appear as filtered (i.e., drop everything). However, some applications need a specific …

问题原文

原帖链接 · Liran Funaro · CC BY-SA 4.0

I'm using Ubuntu 18.04 with iptables.

I want that attackers would see a consistent state when trying to map my ports. I want all my ports to appear as filtered (i.e., drop everything). However, some applications need a specific open port when I'm using them.

I would like that for this specific port, the firewall will drop everything if there is no application listening on that port (instead of replying with reject). And allow everything if there is an application that is listening.

Is it possible?

已采纳解答

原帖链接 · Daniele Santi · CC BY-SA 4.0

While I'm not aware of a single/simple iptables rule to have it open/close a port based on a service actually listening on that port, you can achieve what you need by changing the default iptables policy to DROP and allow only the ports you need.

For example, say you have sshd listening on port 22 and apache2 on port 80 (don't do this if connected via ssh!)

# Start with empty tables
iptables -F
iptables -X

# Change INPUT policy to DROP
iptables -P INPUT DROP

# Make sure the machine itself can connect to the internet
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Maybe allow ICMP's echo/reply (i.e. ping)
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

# Add ports for which there are services listening
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

This way, for every port but 22 and 80, iptables drops the packets.

If you need a new port open, just add ("append") a new rule

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

If you don't need a port anymore, delete the relevant rule

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

If you want a sort of automatic allowing/denying based on services (it looks so from your question), you can always have a basic iptables ruleset and then modify your services' scripts to append (-A) the relevant rule on startup and delete it (-D) on stop.

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

资料来源与适用范围

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

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

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