LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 10-02-2008, 02:08 PM   #1
Hammett
Senior Member
 
Registered: Aug 2003
Location: Barcelona, Catalunya
Distribution: Gentoo
Posts: 1,074

Rep: Reputation: 59
Need advice on my iptables config


Hello everyone!

I'm a complete noob regarding iptables configuration, but reading some documentation and howtos, I ended up with this little script.

It should block all inbound and outbound connections unless sepcifically allowed, but testing the firewall indicates that a lot of ports are open which shouldn't.

I need advice on how to improve my firewall so I can only give access to those ports I really want to have connection.

Code:
# Generated by Hammett on Mon 10th Sep 2007
*filter
# Shut down all traffic
-P FORWARD DROP
-P INPUT DROP
-P OUTPUT DROP

# Clear all rules
-F
-X

# accept all from localhost
-A INPUT -s 127.0.0.1 -j ACCEPT
-A OUTPUT -o 127.0.0.1 -j ACCEPT

# Allow DNS
-A OUTPUT -p tcp --sport 0:65535 --dport 53:53 -j ACCEPT
-A OUTPUT -p udp --sport 0:65535 --dport 53:53 -j ACCEPT

# accept all previously established connections
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# Allow HTTP,HTTPS
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT

# Allow ftp
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 20:21 -j ACCEPT
# Allow rsync (for portage)
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT

# Allow ping
-A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
-A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

# Allow Bit-torrent connections
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6881:6889 -j ACCEPT
-A INPUT -p udp -m state --state NEW -m udp --dport 6881:6999 -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 6881:6889 -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 2710 -j ACCEPT

#Allow Skype
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 23399 -j ACCEPT

#Allow GIT
-A OUTPUT -p udp -m state --state NEW -m udp --dport 9418 -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 9418 -j ACCEPT

#Allow TitleTrading
-A OUTPUT -p udp -m state --state NEW -m udp --dport 19010 -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 19010 -j ACCEPT

#Allow World of Warcraft
 -A OUTPUT -p udp -m state --state NEW -m udp --dport 3724 -j ACCEPT
 -A OUTPUT -p tcp -m state --state NEW -m tcp --dport 3724 -j ACCEPT

# Allow MSN Messenger
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 1863 -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 6891:6899 -j ACCEPT
-A OUTPUT -p udp -m state --state NEW -m udp --dport 6891:6899 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6891:6899 -j ACCEPT
-A INPUT -p udp -m state --state NEW -m udp --dport 6891:6899 -j ACCEPT

# Allow SoulSeek
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 2240 -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 2234:2239 -j ACCEPT

# Allow IRC
-A OUTPUT -p tcp -m state --state NEW -m tcp --dport 6665:6669 -j ACCEPT

COMMIT
Firewall test done under GRC ShieldsUP! shows that:
Code:
Results from scan of ports: 0-1055

  786 Ports Open
    1 Ports Closed
  269 Ports Stealth
---------------------
 1056 Ports Tested
Any help is more than appretiated.
 
Old 10-02-2008, 02:19 PM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
My first piece of advice is that you stop messing with the iptables configuration file. You should make yourself an iptables script, and the only thing editing the configuration file should be the iptables-save binary itself. Here's a script to get you started:
Code:
#!/bin/sh

IPT="/sbin/iptables"

$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT DROP

$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT

$IPT -t mangle -P PREROUTING ACCEPT
$IPT -t mangle -P INPUT ACCEPT
$IPT -t mangle -P FORWARD ACCEPT
$IPT -t mangle -P OUTPUT ACCEPT
$IPT -t mangle -P POSTROUTING ACCEPT

$IPT -t raw -P PREROUTING ACCEPT
$IPT -t raw -P OUTPUT ACCEPT

$IPT -F
$IPT -F -t nat
$IPT -F -t mangle
$IPT -F -t raw

$IPT -X
$IPT -X -t nat
$IPT -X -t mangle
$IPT -X -t raw

$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

$IPT -A INPUT -i lo -j ACCEPT

$IPT -A INPUT -p TCP -i eth0 --dport 6881 \
-m state --state NEW -j ACCEPT

$IPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

$IPT -A OUTPUT -o lo -j ACCEPT

$IPT -A OUTPUT -p UDP -o eth0 --dport 53 \
-m state --state NEW -j ACCEPT

$IPT -A OUTPUT -p TCP -o eth0 --dport 443 \
-m state --state NEW -j ACCEPT

$IPT -A OUTPUT -p TCP -o eth0 --dport 80 \
-m state --state NEW -j ACCEPT

$IPT -A OUTPUT -p TCP -o eth0 --dport 21 \
-m state --state NEW -j ACCEPT

$IPT -A OUTPUT -p TCP -o eth0 --dport 6881:6999 \
-m state --state NEW -j ACCEPT

$IPT -A OUTPUT -p ICMP -o eth0 --icmp-type 8 \
-m state --state NEW -j ACCEPT
All you gotta do is follow the examples to add your own rules.

Last edited by win32sux; 10-02-2008 at 02:30 PM.
 
Old 10-02-2008, 04:44 PM   #3
Hammett
Senior Member
 
Registered: Aug 2003
Location: Barcelona, Catalunya
Distribution: Gentoo
Posts: 1,074

Original Poster
Rep: Reputation: 59
Thanks for the advice, I followed and I just created the script you suggested.

The problem though, still persists.

If I understand it well, the iptables -P INPUT DROP will stealth all incoming connections, no matter what port.

If so, why I still have some ports open not specified in the rules?
 
Old 10-02-2008, 05:06 PM   #4
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
The only way for a port to be "open" is if there is something listening on it. I seriously doubt you have 786 things listening on your computer, so I would make sure the scanner you are using is not giving false positives. The script, as posted, only allows connections to one port (6881/TCP). So a port scanner should show that port as "closed" if you don't have anything listening on it, and "open" if you do (such as your BitTorrent application). All other ports should appear as "stealthed" or "filtered".

Last edited by win32sux; 10-02-2008 at 05:30 PM.
 
  


Reply



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
advice on iptables rule satimis Linux - Security 2 02-21-2008 03:49 AM
Config help and advice needed slackuser93 Linux - Hardware 2 07-25-2007 04:32 PM
Iptables script advice? Nickj Linux - Security 1 07-28-2005 09:06 PM
Need advice on this IPtables code wardialer Linux - Security 13 11-15-2004 04:02 AM
IPTables Firewall Advice... Bomber Linux - Security 5 04-11-2004 01:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 07:04 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