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 02-04-2010, 11:13 AM   #1
webboy105
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Rep: Reputation: 0
Block all the ports excpet 80 on iptables (eth2)


I'm new to iptables and I would like to block all the ports on eth2 except port 80.

how can I do that?

If I Set the default policy of the INPUT chain to DROP, this will apply on all network cards. am I right?

iptables -P INPUT DROP

# Accept incomming TCP connections from eth2 on port 80

/sbin/iptables -A INPUT -i eth2 -p tcp --dport 80 -j ACCEPT

much appreciate your help!!

Last edited by webboy105; 02-04-2010 at 11:15 AM.
 
Old 02-04-2010, 11:22 AM   #2
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
An advice:

Instead of -A, use -I. Just in case your distro already has a firewall configured, -I will insert your rule on top of their rules. Good for experimenting :-)

And, insert another 2 rules for chain OUTPUT. You're allowing packets to enter (INPUT), and you should let packets to go out too (OUTPUT)

Code:
iptables -P OUTPUT DROP
iptables -I OUTPUT -o eth2 -p tcp --sport 80 -j ACCEPT

Last edited by HasC; 02-04-2010 at 11:23 AM.
 
Old 02-04-2010, 11:27 AM   #3
webboy105
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by HasC View Post
An advice:

Instead of -A, use -I. Just in case your distro already has a firewall configured, -I will insert your rule on top of their rules. Good for experimenting :-)

And, insert another 2 rules for chain OUTPUT. You're allowing packets to enter (INPUT), and you should let packets to go out too (OUTPUT)

Code:
iptables -P OUTPUT DROP
iptables -I OUTPUT -o eth2 -p tcp --sport 80 -j ACCEPT
If I understand correctly, It will look like

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -I OUTPUT -o eth2 -p tcp --sport 80 -j ACCEPT

This will not impact other nic cards (like eth0 and eth1).
 
Old 02-04-2010, 11:33 AM   #4
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
Quote:
Originally Posted by webboy105 View Post
If I understand correctly, It will look like

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -I OUTPUT -o eth2 -p tcp --sport 80 -j ACCEPT

This will not impact other nic cards (like eth0 and eth1).
That firewall will only allow outgoing traffic originating from port 80 on eth2. Everything incoming for eth2, and absolutely everything on all other cards will be dropped, both incoming and outgoing. Is that really what you want?

If you would explain a bit more about the different cards and what you're trying to accomplish, we probably could point you in a better direction.
 
Old 02-04-2010, 11:38 AM   #5
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
You forgot to add the rule you're created first. That one should be included too
 
Old 02-04-2010, 11:49 AM   #6
webboy105
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by HasC View Post
You forgot to add the rule you're created first. That one should be included too
thanks for your quick response. I'm still not clear on default oplicy rule.

default oplicy rule will not ipmact eth0 and eth1? am I right?


OK... so the default oplicy rule will not ipmact eth0 and eth1?
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -A INPUT -i eth2 -p tcp --dport 80 -j ACCEPT
iptables -I OUTPUT -o eth2 -p tcp --sport 80 -j ACCEPT

All the NIC cards (eth0, eth1 and eth2) are on different subnets.
 
Old 02-04-2010, 12:01 PM   #7
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
Quote:
default oplicy rule will not ipmact eth0 and eth1? am I right?
No, the default policy will affect ALL interfaces. You'll need to add additional rules to cover eth0 and eth1. Those can be pretty broad, like just accepting all traffic on the interface:

Code:
iptables -A INPUT -i eth0 -j ACCEPT
iptables -A OUTPUT -o eth0 -j ACCEPT

Now this bugs me:
Quote:
iptables -I OUTPUT -o eth2 -p tcp --sport 80 -j ACCEPT
This rule means that the only traffic that gets out of eth2 MUST be coming from port 80. Unless you've taken some steps to limit whatever is sending packets to port 80, then you're going to have trouble. For example, if you are trying to limit eth2 to web traffic, you would need to modify this rule somewhat:

iptables -I OUTPUT -o eth2 -p tcp --dport 80 -j ACCEPT

This would allow a browser to send from any port but the only stuff that would get through would be packets destined for port 80 on the other end, which is where most web servers listen.

Again, if you could give us some background on what you're trying to accomplish, we might be able to give better advice.
 
Old 02-04-2010, 12:05 PM   #8
webboy105
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hangdog42 View Post
No, the default policy will affect ALL interfaces. You'll need to add additional rules to cover eth0 and eth1. Those can be pretty broad, like just accepting all traffic on the interface:

Code:
iptables -A INPUT -i eth0 -j ACCEPT
iptables -A OUTPUT -o eth0 -j ACCEPT

Now this bugs me:


This rule means that the only traffic that gets out of eth2 MUST be coming from port 80. Unless you've taken some steps to limit whatever is sending packets to port 80, then you're going to have trouble. For example, if you are trying to limit eth2 to web traffic, you would need to modify this rule somewhat:

iptables -I OUTPUT -o eth2 -p tcp --dport 80 -j ACCEPT

This would allow a browser to send from any port but the only stuff that would get through would be packets destined for port 80 on the other end, which is where most web servers listen.

Again, if you could give us some background on what you're trying to accomplish, we might be able to give better advice.
Thank you very much for your response.

I'm trying to accomplish to allow internet users to access web site over port 80 on eth2. They shouldn't have access to any other port on eth2.
 
Old 02-04-2010, 12:07 PM   #9
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Default policy will work if nothing else do its choice.

But the question is - do you want to filter INCOMING packets to firewall, or OUTGOING?
 
Old 02-04-2010, 12:33 PM   #10
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
Quote:
I'm trying to accomplish to allow internet users to access web site over port 80 on eth2. They shouldn't have access to any other port on eth2.
Sorry, I'm still a little confused about your network. Is eth2 on the web server or will it be handling outgoing traffic from the users web browsers?
 
Old 02-04-2010, 01:08 PM   #11
webboy105
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hangdog42 View Post
Sorry, I'm still a little confused about your network. Is eth2 on the web server or will it be handling outgoing traffic from the users web browsers?
eth2 is my web server.
 
Old 02-04-2010, 03:17 PM   #12
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
OK, web servers usually listen on port 80, but they may return information on other ports. So your default policy is good, you only want traffic to come in on port 80, but you want it to be able to leave on any port. So introducing states on the outbound chain might be useful:


Code:
iptables -P INPUT DROP
iptables -P OUTPUT DROP

iptables -A INPUT -i eth0 -j ACCEPT
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A INPUT -i eth2 -p tcp --dport 80 -j ACCEPT

iptables -A OUTPUT -o eth0 -j ACCEPT
iptables -A OUTPUT -o eth1 -j ACCEPT
iptables -A OUTPUT -o eth2 -m state --state ESTABLISHED,RELATED -j ACCEPT
So the eth0 and eth1 lines should allow all traffic in and out on those two interfaces. The INPUT line for eth2 should only allow traffic heading for port 80, where hopefully your web server is listening. The OUTPUT line for eth2 allows any packets that have the ESTABLISHED or RELATED states, which basically means they have to be in response to an established connection. Since only port 80 accepts incoming connections, that should mean that it will be the only thing generating ESTABLISHED or RELATED packets.
 
1 members found this post helpful.
  


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
Block and allow with Iptables help! conrado Slackware 2 10-26-2007 10:31 AM
how to block https using iptables pavangogineni Linux - Security 2 10-19-2006 12:49 AM
iptables-p2p - Instalation problems | How to block p2p with iptables Woping Linux - Networking 0 03-14-2006 12:56 PM
IPTables and PPTPD :S (to block or not to block) thewonka Linux - Networking 0 03-24-2005 06:58 PM
IPtables - Block all except what I allow ]SK[ Linux - Software 4 02-10-2005 06:14 AM

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

All times are GMT -5. The time now is 11:55 AM.

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