LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (http://www.linuxquestions.org/questions/linux-networking-3/)
-   -   IPTABLES help (http://www.linuxquestions.org/questions/linux-networking-3/iptables-help-856378/)

stratosmacker 01-14-2011 03:10 PM

IPTABLES help
 
Im working on my minecraft server so that with the exception of outgoing web acess, incoming minecraft server acess, and incoming ssh, all other ports and services are blocked. This is on the inside of my router. Unfortunately while the ssh works, web acees and the minecraft protocol don't. here is the script im using.

#!/bin/sh
SERVER_IP=192.168.1.95
# 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 http
sudo iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT
sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT

#allow inbound
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow incoming ssh only
iptables -A INPUT -p tcp -s 0/0 -d $SERVER_IP --sport 513:65535 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP -d 0/0 --sport 22 --dport 513:65535 -m state --state ESTABLISHED -j ACCEPT

#allow minecraft
iptables -A INPUT -p tcp --dport 25565 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 25565 -j ACCEPT

# make sure nothing comes or goes out of this box
iptables -A INPUT -j DROP
iptables -A OUTPUT -j DROP

tsg 01-14-2011 03:28 PM

It looks like you don't have any rule to allow output for web traffic. Does minecraft actually use the same port for outgoing as incoming? I'm not familiar with the protocol, but most tcp protocols don't.

You can add the following lines before the DROP lines for troubleshooting:
Code:

iptables -A INPUT -j LOG --log-prefix "input drop "
iptables -A OUTPUT -j LOG --log-prefix "output drop "

Then your log files will show you where it's getting stopped.

stratosmacker 01-14-2011 05:29 PM

Quote:

Originally Posted by tsg (Post 4224605)
It looks like you don't have any rule to allow output for web traffic. Does minecraft actually use the same port for outgoing as incoming? I'm not familiar with the protocol, but most tcp protocols don't.

You can add the following lines before the DROP lines for troubleshooting:
Code:

iptables -A INPUT -j LOG --log-prefix "input drop "
iptables -A OUTPUT -j LOG --log-prefix "output drop "

Then your log files will show you where it's getting stopped.

Does the area with the #http comment not allow web traffic? Networking is a new thing for me..... haha

unSpawn 01-14-2011 09:05 PM

Code:

#!/bin/sh --
# Flush all rules
iptables -F; iptables -X
# Setting default filter policy
iptables -P INPUT DROP; iptables -P FORWARD DROP; iptables -P OUTPUT ACCEPT
# Allow unlimited traffic on loopback
iptables -A INPUT -i lo -j ACCEPT; iptables -A OUTPUT -o lo -j ACCEPT

# Allow inbound existing connections first as this is the majority of traffic
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow SSH and Minecraft as example in one rule
iptables -A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 22,25565 -j ACCEPT
iptables -A INPUT -p udp -m state --state NEW -m tcp --dport 25565 -j ACCEPT

# Allow HTTP but as example flood-limit access to max 30 requests per minute
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -m limit --limit 10/minute --limit-burst 30 -j ACCEPT

# Drop ICMP frags but allow some ICMP diagnostic messages:
iptables -A INPUT -p icmp --fragment -j DROP
for ICMPTYPE in echo-reply time-exceeded fragmentation-needed; do
iptables -A INPUT -p icmp --icmp-type ${ICMPTYPE} -j ACCEPT; done

# Just in case catchall rule to reject anything else (should not occur):
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited

# End
exit 0

- Unless your server gets compromised outbound traffic can be new connections like DNS resolution, accessing a web page or getting remote email and established ones like your Minecraft server responding to connected players. So while completely locking down the machine is commendable it is not the primary security concern and I set your outbound policy to ACCEPT which you may or may not like.
- Unless you host your own Domain Name Service your only inbound DNS traffic will be of state "ESTABLISHED" so the port 53 rule is gone.
- I'm not certain Minecraft doesn't need UDP so I added that.
- ICMP is used to check connection problems so you should allow some of it.
- Some network ranges should not be allowed in. They're called "bogons" which you will find here: http://www.team-cymru.org/Services/B...gon-bn-agg.txt
- There are more states that iptables recognizes like INVALID you can filter. See the standard work on iptables for more information: http://www.frozentux.net/iptables-tu...tml/index.html
- For running SSH 0) do deny root logins in /etc/sshd_config, 1) use an unprivileged account to login with pubkey auth and only *then* use sudo and 2) install fail2ban.
- At a later point you might find your connection saturated: you might want to read up on rate-limiting traffic and bandwidth shaping.
- Install Logwatch and read those reports.
* And please harden your machine properly before exposing it to the 'net.

stratosmacker 01-14-2011 09:43 PM

Quote:

Originally Posted by unSpawn (Post 4224805)
Code:

#!/bin/sh --
# Flush all rules
iptables -F; iptables -X
# Setting default filter policy
iptables -P INPUT DROP; iptables -P FORWARD DROP; iptables -P OUTPUT ACCEPT
# Allow unlimited traffic on loopback
iptables -A INPUT -i lo -j ACCEPT; iptables -A OUTPUT -o lo -j ACCEPT

# Allow inbound existing connections first as this is the majority of traffic
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow SSH and Minecraft as example in one rule
iptables -A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 22,25565 -j ACCEPT
iptables -A INPUT -p udp -m state --state NEW -m tcp --dport 25565 -j ACCEPT

# Allow HTTP but as example flood-limit access to max 30 requests per minute
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -m limit --limit 10/minute --limit-burst 30 -j ACCEPT

# Drop ICMP frags but allow some ICMP diagnostic messages:
iptables -A INPUT -p icmp --fragment -j DROP
for ICMPTYPE in echo-reply time-exceeded fragmentation-needed; do
iptables -A INPUT -p icmp --icmp-type ${ICMPTYPE} -j ACCEPT; done

# Just in case catchall rule to reject anything else (should not occur):
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited

# End
exit 0

- Unless your server gets compromised outbound traffic can be new connections like DNS resolution, accessing a web page or getting remote email and established ones like your Minecraft server responding to connected players. So while completely locking down the machine is commendable it is not the primary security concern and I set your outbound policy to ACCEPT which you may or may not like.
- Unless you host your own Domain Name Service your only inbound DNS traffic will be of state "ESTABLISHED" so the port 53 rule is gone.
- I'm not certain Minecraft doesn't need UDP so I added that.
- ICMP is used to check connection problems so you should allow some of it.
- Some network ranges should not be allowed in. They're called "bogons" which you will find here: http://www.team-cymru.org/Services/B...gon-bn-agg.txt
- There are more states that iptables recognizes like INVALID you can filter. See the standard work on iptables for more information: http://www.frozentux.net/iptables-tu...tml/index.html
- For running SSH 0) do deny root logins in /etc/sshd_config, 1) use an unprivileged account to login with pubkey auth and only *then* use sudo and 2) install fail2ban.
- At a later point you might find your connection saturated: you might want to read up on rate-limiting traffic and bandwidth shaping.
- Install Logwatch and read those reports.
* And please harden your machine properly before exposing it to the 'net.

Thanks so much. Im going to try this configuration out right now. Ill keep you posted.

stratosmacker 01-15-2011 12:15 PM

Quote:

Originally Posted by stratosmacker (Post 4224824)
Thanks so much. Im going to try this configuration out right now. Ill keep you posted.

I tried the config, as soon as i tried to set the file with the config as the source eg source /etc......./firewallconfig
it booted me out of su -. It shot some error i think but i couldnt read it

stratosmacker 01-15-2011 04:04 PM

also now im locked out even with ssh. Im going to go to the physical source.

unSpawn 01-15-2011 07:09 PM

I'm sorry to hear that, this should not have happened.

stratosmacker 01-16-2011 10:03 AM

Its fine. I appreciate the insight either way. If you do happen to realize whats wrong though feel free to pipe up. In the mean time im going to try a few things

tsg 01-17-2011 08:31 AM

On this line:
Code:

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
you have to specify "-p tcp" or it won't accept the --state parameters.

To answer your earlier question:
Quote:

Does the area with the #http comment not allow web traffic? Networking is a new thing for me..... haha
You've allowed incoming traffic, but not outgoing. In practice, most firewalls usually allow any outgoing traffic on an established connection, so specifying the port number isn't required like you've done for ssh and minecraft.

stratosmacker 01-17-2011 03:09 PM

Quote:

Originally Posted by tsg (Post 4227375)
On this line:
Code:

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
you have to specify "-p tcp" or it won't accept the --state parameters.

To answer your earlier question:


You've allowed incoming traffic, but not outgoing. In practice, most firewalls usually allow any outgoing traffic on an established connection, so specifying the port number isn't required like you've done for ssh and minecraft.

I changed the line to:
iptables -p tcp -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

but it still kicks me out of su - when I point to it as the source

unSpawn 01-17-2011 04:38 PM

Quote:

Originally Posted by tsg (Post 4227375)
you have to specify "-p tcp" or it won't accept the --state parameters.

Does it now? If it does then how come I haven't got any problems:
Code:

~]# /sbin/iptables -t filter -n -v -x --line-numbers -L INPUT|grep EST
1    98287665 43703590608 ACCEPT    all  --  *      *      0.0.0.0/0            0.0.0.0/0          state RELATED,ESTABLISHED

(and no, there's no "-p" rules feeding into the INPUT chain).

unSpawn 01-17-2011 04:39 PM

Quote:

Originally Posted by stratosmacker (Post 4227876)
I changed the line (..) but it still kicks me out of su - when I point to it as the source

If and when you get back in check your syslog log files for clues? Right now I'm not having any.

tsg 01-18-2011 09:03 AM

Quote:

Originally Posted by unSpawn (Post 4227965)
Does it now? If it does then how come I haven't got any problems:

My bad. That must have been an ipchains requirement.

stratosmacker 01-19-2011 06:11 PM

Quote:

Originally Posted by unSpawn (Post 4227965)
Does it now? If it does then how come I haven't got any problems:
Code:

~]# /sbin/iptables -t filter -n -v -x --line-numbers -L INPUT|grep EST
1    98287665 43703590608 ACCEPT    all  --  *      *      0.0.0.0/0            0.0.0.0/0          state RELATED,ESTABLISHED

(and no, there's no "-p" rules feeding into the INPUT chain).

where exactly do I pass the rules then? IPTABLES is a fun cookie.


All times are GMT -5. The time now is 08:56 AM.