LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2011, 03:10 PM   #1
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Rep: Reputation: 0
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
 
Old 01-14-2011, 03:28 PM   #2
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
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.
 
Old 01-14-2011, 05:29 PM   #3
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tsg View Post
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
 
Old 01-14-2011, 09:05 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 01-14-2011, 09:43 PM   #5
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
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.
 
Old 01-15-2011, 12:15 PM   #6
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by stratosmacker View Post
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
 
Old 01-15-2011, 04:04 PM   #7
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Original Poster
Rep: Reputation: 0
also now im locked out even with ssh. Im going to go to the physical source.
 
Old 01-15-2011, 07:09 PM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I'm sorry to hear that, this should not have happened.
 
Old 01-16-2011, 10:03 AM   #9
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Original Poster
Rep: Reputation: 0
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
 
Old 01-17-2011, 08:31 AM   #10
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
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.
 
Old 01-17-2011, 03:09 PM   #11
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tsg View Post
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
 
Old 01-17-2011, 04:38 PM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by tsg View Post
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).
 
Old 01-17-2011, 04:39 PM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by stratosmacker View Post
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.
 
Old 01-18-2011, 09:03 AM   #14
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
Quote:
Originally Posted by unSpawn View Post
Does it now? If it does then how come I haven't got any problems:
My bad. That must have been an ipchains requirement.
 
Old 01-19-2011, 06:11 PM   #15
stratosmacker
LQ Newbie
 
Registered: Jun 2010
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
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.
 
  


Reply

Tags
centos, iptables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
On what basis CHAIN integer values are generated in IPtables under iptables file? haariseshu Linux - Server 3 11-05-2009 04:25 AM
[SOLVED] Rather huge IPtables chain, iptables: Memory allocation problem. Gangrif Linux - Networking 10 09-11-2009 03:30 PM
iptables v1.2.9: Unknown arg `/sbin/iptables' Try `iptables -h' or 'iptables --help' Niceman2005 Linux - Security 4 12-29-2005 08:20 PM
IPtables Log Analyzer from http://www.gege.org/iptables/ brainlego Linux - Software 0 08-11-2003 06:08 AM
My iptables script is /etc/sysconfig/iptables. How do i make this baby execute on boo ForumKid Linux - General 3 01-22-2002 07:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration