Linux中是否有任何日志告诉你是否有一个端口被拒绝

假设我有防火墙活跃或任何其他安全像SE Linux等

现在假设用户想要连接到端口21并且Iptables不允许它。

现在,当用户被拒绝时,这个消息被logging在任何地方,这样我就可以看到被使用的是什么被阻塞或者为什么特定的端口被阻塞。

而不是挖掘每一个环境,以找出我为什么不通过它。

我已经chnaged默认的SSH端口8022但我越来越conenction拒绝。

我已经检查过telnet并在端口上侦听。 我有空的iptables。

是否有任何日志,我可以检查谁拒绝连接

第一个答案

不,默认情况下没有日志 ,显示这个,但是

显示当前的防火墙configuration

看看你的防火墙是如何configuration的:

 iptables -L 

首先查找Chain [INPUT|OUTPUT] policy 。 如果除了ACCEPT之外还有其他东西,那么使用的端口可能必须被明确地ACCEPT

 iptables -L INPUT | grep `port=2[01]` 

要显示有关端口20和端口21的显式规则,但是要小心,可能需要阅读整个防火墙configuration,检查multiportuser-defined chains等。如果根本不了解iptables ,这可能会变得困难。

空的打开的防火墙configuration可能如下所示:

 iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination 

看到:

 man iptables 

知道什么可以阻止你的规则的东西

我使用这个技巧:

 touch /tmp/tmp_iptable_stat.txt getChanges() { pushd /tmp >/dev/null for table in $(</proc/self/net/ip_tables_names);do echo $RANDOM: - $table iptables -t $table -nvxL --line-number done | diff -u tmp_iptable_stat.txt - | tee >(patch -p0) | sed ' s/^+[0-9]*: - /TABLE /p; s/^+//p; d' popd >/dev/null } 

比第一次调用getChanges将转储所有表和计数器。 后继者调用相同的函数将仅打印计数器被修改的规则。 这可能有助于find哪些规则阻止了某些东西。

显示当前的networking堆栈状态:

内核networking堆栈可以被转储

 netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN tcp 0 2364 192.168.1.1:21 192.168.1.35:49179 ESTABLISHED 

对于TCP套接字或

 netstat -uan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State 

为UDP套接字。

由于我的FTP服务器使用TCP套接字,所以我可以看到当前在我的服务器和主机之间build立了一个交换… 35 ,(服务器目前有2364个包发送到客户端,也许是一个文件,也许是一个列表…)

跟踪特定界面上的stream量

而不是使用log ,你可以看你的界面上发生了什么:

 tcpdump -i ethX 

这将在ethX上转储关于stream量的有用信息,但是由于默认情况下并且更易于读取 ,所以此工具将尝试parsing每个IP的名称。 所以事件本身和terminal上的转储之间可能会有一些延迟。 所以:

 tcpdump -ani ethX 

将不会尝试parsing(opt -n )IP和服务名称,并将显示遍历该接口的所有( -a )数据包。

更精细:

 tcpdump -ani ethX port 21 or port 20 09:17:58.264453 IP 192.168.1.1.21 > 192.168.24.91.45951: Flags [S.], seq 3593971599, ack 1942867644, win 5792, options [mss 1460,sackOK,TS val 1168768120 ecr 62841986,nop,wscale 7], length 0 09:17:58.299693 IP 192.168.1.35.56485 > 192.168.1.1.21: Flags [S], seq 3334605998, win 5840, options [mss 1368,sackOK,TS val 1936641509 ecr 0,nop,wscale 7], length 0 09:17:58.299728 IP 192.168.1.1.21 > 192.168.1.35.56485: Flags [S.], seq 980554936, ack 3334605999, win 5792, options [mss 1460,sackOK,TS val 1168768129 ecr 1936641509,nop,wscale 7], length 0 ... 

更详细的说明: ... use -v or -vv for full protocol decode

 tcpdump -anvvi ethX port 21 or port 20 tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes 09:22:40.047486 IP (tos 0x0, ttl 62, id 31488, offset 0, flags [DF], proto TCP (6), length 60) 192.168.24.91.46011 > 192.168.1.1.21: Flags [S], cksum 0x5985 (correct), seq 3989081263, win 14600, options [mss 1368,sackOK,TS val 62912431 ecr 0,nop,wscale 6], length 0 09:22:40.047525 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 60) 192.168.1.1.21 > 192.168.24.91.46011: Flags [S.], cksum 0x926d (correct), seq 2283473829, ack 3989081264, win 5792, options [mss 1460,sackOK,TS val 1168838566 ecr 62912431,nop,wscale 7], length 0 09:22:40.817248 IP (tos 0x0, ttl 62, id 31489, offset 0, flags [DF], proto TCP (6), length 52) 192.168.24.91.46011 > 192.168.1.1.21: Flags [.], cksum 0xd6e9 (correct), seq 1, ack 1, win 229, options [nop,nop,TS val 62912442 ecr 1168838566], length 0 09:22:40.817567 IP (tos 0x0, ttl 62, id 31490, offset 0, flags [DF], proto TCP (6), length 52) 192.168.24.91.46011 > 192.168.1.1.21: Flags [F.], cksum 0xd6e3 (correct), seq 1, ack 1, win 229, options [nop,nop,TS val 62912447 ecr 1168838566], length 0 ... 

哪里可以按照每个操作。

在禁用iptables和SElinux的情况下,可能没有日志可读。 我不太明白telnet适合这个场景的地方,因为它与ssh没有任何关系。 目前的SELinux策略只阻止1023以下非标准端口上的ssh连接,所以不太可能是这样。

Connection refused消息通常意味着没有任何内容正在侦听请求的端口。 您可以使用netstat检查是否正在监听

 netstat -tunlp | grep 8022 tcp 0 0 0.0.0.0:8022 0.0.0.0:* LISTEN 2178/sshd tcp6 0 0 :::8022 :::* LISTEN 2178/sshd 

以上显示sshd在所有IPv4和IPv6接口上监听8022端口。

如果你在你的iptables规则上设置了LOG ,那么你应该在/var/adm/messages得到一个日志条目。 这里是一个例子:

 # --- Log new connections: -A INPUT -m state --state NEW -j LOG --log-prefix "NEW: " --log-level info 

在你的IP表规则集中,像下面这样的规则可能会让你开始:

 # Enable port 8022 (ssh) but rate limit it: -A INPUT -p tcp -m tcp --dport 8022 ! --syn -j ACCEPT -A INPUT -p tcp -m tcp --dport 8022 --syn -m limit --limit 3/minute -j ACCEPT 

sestatus命令会告诉你selinux是否被启用:

 [root@seadog ~]# sestatus SELinux status: enabled SELinuxfs mount: /selinux Current mode: enforcing Mode from config file: enforcing Policy version: 24 Policy from config file: targeted 

来自selinux的消息默认为/var/log/audit/audit.log

semanage命令(在rpm:policycoreutils-python中find)也可能用于列出selinux控制的端口:

 root@seadog log]# semanage port -l SELinux Port Type Proto Port Number afs_bos_port_t udp 7007 afs_client_port_t udp 7001 afs_fs_port_t tcp 2040 afs_fs_port_t udp 7000, 7005 afs_ka_port_t udp 7004 afs_pt_port_t udp 7002 afs_vl_port_t udp 7003 agentx_port_t tcp 705 

TCP Wrapper也可能被启用。 检查/etc/hosts.allow/etc/hosts.deny文件。

在虚拟机中尝试这些东西是学习如何将这些东西放在一起并在将规则投入生产之前build立信心的好方法。