简体   繁体   English

在Debian上使用iptables启用特定访问

[英]Enabling specific access with iptables on Debian

Iam running a Debian server that hosts several docker containers but also works as my router and internet gateway. 我正在运行一个Debian服务器,该服务器托管多个Docker容器,但也可以用作我的路由器和Internet网关。 Iam having trouble getting ssh access from outside (specific ipaddresses) working on the host (not in the docker containers. All access to docker containers works just fine. 我很难从主机上工作的外部(特定ipaddress)(而不是在docker容器中)获取ssh访问。对docker容器的所有访问都可以正常工作。

I can see with a nmap from the inside of my network to the local IP and my external IP that the port is open although it seems to be open just by enabling ssh on that port. 通过从网络内部到本地IP和外部IP的nmap,我可以看到该端口是打开的,尽管仅通过在该端口上启用ssh即可打开该端口。 So if I set the ssh port in the sshd_config to be 5501 and restarts the ssh daemon, an nmap to that port shows the port is open, if I change the port to 22 it shows that port is open although I haven't changed anything in my iptables script, so my worries besides not being able to reach the server from sepcific external IP's is that the script is to openly configured. 因此,如果我将sshd_config中的ssh端口设置为5501并重新启动ssh守护程序,则对该端口的nmap显示该端口处于打开状态,如果我将该端口更改为22,则表明该端口处于打开状态,尽管我没有进行任何更改。在我的iptables脚本中,因此除了无法从单独的外部IP到达服务器之外,我担心的是该脚本是公开配置的。

An iptables -L -a command don't list the port for SSH (5501), although it lists all other ports in the script. iptables -L -a命令没有列出SSH的端口(5501),尽管它列出了脚本中的所有其他端口。 Is it because I don't have a table for the rules for the host itself? 是因为我没有主机本身的规则表吗? Searching around stackoverflow for similar questions dosen't seems to be requiring a table? 在stackoverflow周围搜索类似问题似乎不需要表格?

Below is my complete IP tables script. 以下是我完整的IP表脚本。

PATH=/usr/sbin:/sbin:/usr/bin:/bin
#!/bin/sh
##################################################################################################
# General variables for this script                                                              #
##################################################################################################
SERVER_IP="192.168.0.1"
A="xxx.xxx.xxx.xxx"
B="xxx.xxx.xxx.xxx"
C="xxx.xxx.xxx.xxx"
D="xxx.xxx.xxx.xxx"
E="xxx.xxx.xxx.xxx"
InternalNet="192.168.0.1-192.168.0.100"

##################################################################################################
# Docker stop                                                                                    #
##################################################################################################
service docker stop

##################################################################################################
# Flushing all rules                                                                             #
##################################################################################################
iptables -F
iptables -X

##################################################################################################
# Setting default filter policy                                                                  #
##################################################################################################
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

##################################################################################################
# Allow unlimited traffic on loopback                                                            #
##################################################################################################
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

##################################################################################################
# Allow unlimited traffic to eth1 (homeserver internal)                                          #
##################################################################################################
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A OUTPUT -o eth1 -j ACCEPT

##################################################################################################
# Allow all traffic form internal to all outbound                                                #
# And all related and established traffic from the internet                                      #
##################################################################################################
iptables -A OUTPUT -o eth0 -j ACCEPT 
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

##################################################################################################
# Start Docker                                                                                   #
##################################################################################################
service docker start

##################################################################################################
# Create a PRE_DOCKER table                                                                      #
##################################################################################################
iptables -N PRE_DOCKER

##################################################################################################
# Insert this as the first table on the FORWARD chain.                                           #
##################################################################################################
iptables -I FORWARD -o docker0 -j PRE_DOCKER

#################################################################################################
# Docker internal use                                                                            #
##################################################################################################
iptables -A PRE_DOCKER -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A PRE_DOCKER -i docker0 ! -o docker0 -j ACCEPT
iptables -A PRE_DOCKER -m state --state RELATED -j ACCEPT
iptables -A PRE_DOCKER -i docker0 -o docker0 -j ACCEPT

##################################################################################################
#  Allow specific trafic from specific ips                                                       #
##################################################################################################
iptables -I PRE_DOCKER -p tcp -s $A,$B,$C,$D,$E -d $SERVER_IP --dport 19999 -m state --state NEW,ESTABLISHED -j ACCEPT #Netdata
iptables -I PRE_DOCKER -p tcp -s $SERVER_IP -d A,$B,$C,$D,$E t --sport 19999 -m state --state ESTABLISHED -j ACCEPT #Netdata

iptables -I PRE_DOCKER -p tcp -s A,$B,$C,$D,$E -d $SERVER_IP --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT #Tomcat
iptables -I PRE_DOCKER -p tcp -s $SERVER_IP -d A,$B,$C,$D,$E --sport 8080 -m state --state ESTABLISHED -j ACCEPT #Tomcat

iptables -I PRE_DOCKER -p tcp -s A,$B,$C,$D,$E -d $SERVER_IP --dport 8081 -m state --state NEW,ESTABLISHED -j ACCEPT #Phabricator
iptables -I PRE_DOCKER -p tcp -s $SERVER_IP -d A,$B,$C,$D,$E --sport 8081 -m state --state ESTABLISHED -j ACCEPT #Phabricator
##################################################################################################
# Allow access from internet                                                                     #
##################################################################################################

#HTTPS(443) from internet to docker apache
iptables -A INPUT -p tcp -d $SERVER_IP --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 443 -m state --state ESTABLISHED -j ACCEPT

iptables -I PRE_DOCKER -p tcp -d $SERVER_IP --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT #Apache
iptables -I PRE_DOCKER -p tcp -s $SERVER_IP --sport 443 -m state --state ESTABLISHED -j ACCEPT #Apache

##################################################################################################
# Allow specific traffic from specific ips to homeserver                                         #
##################################################################################################

#SSH(on port 5501) from known IPs
iptables -A INPUT -p tcp -s A,$B,$C,$D,$E -d $SERVER_IP --dport 5501 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP -d A,$B,$C,$D,$E --sport 5501 -m state --state RELATED,ESTABLISHED -j ACCEPT

#Netdata(on port 19999) from known IPS
iptables -A INPUT -p tcp -s A,$B,$C,$D,$E -d $SERVER_IP --dport 19999 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP -d A,$B,$C,$D,$E --sport 19999 -m state --state RELATED,ESTABLISHED -j ACCEPT
#iptables -t nat -A PREROUTING -i eth1 -d $SERVER_IP -p tcp --dport 5501 -j  DNAT --to-destination 192.168.0.1:5501

The A in this line is missing a $ sign - iptables -A INPUT -p tcp -s $A,$B,$C,$D,$E -d $SERVER_IP --dport 5501 -m state --state NEW,ESTABLISHED -j ACCEPT 此行中的A缺少$符号iptables -A INPUT -p tcp -s $A,$B,$C,$D,$E -d $SERVER_IP --dport 5501 -m state --state NEW,ESTABLISHED -j ACCEPT

If that doesn't change a thing, replace the above with iptables -A INPUT -p tcp --dport 5501 -j ACCEPT . 如果那没有改变,请用iptables -A INPUT -p tcp --dport 5501 -j ACCEPT替换以上内容。 Does it work? 它行得通吗?

Also, these rules are effectively iptables -P OUTPUT ACCEPT : 而且,这些规则实际上是iptables -P OUTPUT ACCEPT

iptables -A OUTPUT -o eth1 -j ACCEPT
iptables -A OUTPUT -o eth0 -j ACCEPT

You can use iptables -P OUTPUT ACCEPT and remove all rules in the OUTPUT chain. 您可以使用iptables -P OUTPUT ACCEPT并删除OUTPUT链中的所有规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM