LinuxQuestions.org
Help answer threads with 0 replies.
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 04-30-2007, 08:30 AM   #1
Richtown
Member
 
Registered: Apr 2006
Location: Portsmouth
Distribution: CentOS & Ubuntu
Posts: 49

Rep: Reputation: 15
iptables problem!!


Hi Guys,

I'm building a gateway and i have two interface eth0(Internet) and eth1 (Private Network). I'm tring to write a rule that will block ip addresses recieved on eth0 and sent to eth1 for example (www.google.com). So the private network will not be able to access www.google.com for example.

I can't figure out the syntax i have something like:

iptables -A INPUT -s www.google.com 0 eth1 -j DROP

But this does not work, is there anyone that nows the correct sytax to do this?

Thanks Rich.
 
Old 04-30-2007, 08:38 AM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Quote:
Originally Posted by Richtown
iptables -A INPUT -s www.google.com 0 eth1 -j DROP
I see a couple of things wrong with your syntax. First if you are going from the internal interface to the external interface you will want to use the FORWARD chain not the input chain. Second I don't think that "0" should be in your rule. Finally you list the link (www.google.com) as a source when it is really the destination address. Fourth, you don't list which interface eth1 is, it must be specified as incoming our outgoing, but in this case if you just want to block the address you don't really need it.

Try this:

Code:
iptables -A FORWARD -d www.google.com -j DROP
 
Old 04-30-2007, 08:47 AM   #3
Richtown
Member
 
Registered: Apr 2006
Location: Portsmouth
Distribution: CentOS & Ubuntu
Posts: 49

Original Poster
Rep: Reputation: 15
Smile

Thanks for the speedy reply, ive only just learning iptables so im not really to hot on the commands. I can see where i went wrong now.

Thanks Rich
 
Old 04-30-2007, 08:50 AM   #4
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Probably the best tutorial out there: IPTables Tutorial 1.2.2

Definitely worth a read!!

Let me know if you need any other help.

Centinul
 
Old 05-01-2007, 04:49 AM   #5
Richtown
Member
 
Registered: Apr 2006
Location: Portsmouth
Distribution: CentOS & Ubuntu
Posts: 49

Original Poster
Rep: Reputation: 15
Post

I'm having a bit of a problem with this script, im building an internat gateway. Eth0 for outside connection, eth1 for LAN connection.

First off:
I block all ports into and out of my Gateway
iptables -P INPUT DROP
iptables -P OUTPUT DROP


Then i block all FORWARD connections to my LAN
iptables -P FORWARD DROP

I then block all Telnet access to my LAN
iptables -A INPUT -p tcp --sport telnet -j REJECT
iptables -A INPUT -p udp --sport telnet -j REJECT


OK, so now i want to open port 80 (http) to my LAN but the following code does not work:
iptables -A FORWARD -p tcp -dport 80 -j ACCEPT
iptables -A FORWARD -p tcp -sport 80 -j ACCEPT


Is the iptables line correct of have i done something wrong elsewhere?

Thanks Rich.

Last edited by Richtown; 05-01-2007 at 04:53 AM.
 
Old 05-01-2007, 05:50 AM   #6
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Quote:
Originally Posted by Richtown
I then block all Telnet access to my LAN
iptables -A INPUT -p tcp --sport telnet -j REJECT
iptables -A INPUT -p udp --sport telnet -j REJECT
If you want to block telnet access to your LAN you'll want to change it to the DESTINATION port. Also the input rules only block telnet access to the gateway device not the PCs behind it. So you may want to add another rule to do that. An example would be:

Code:
iptables -A INPUT -p tcp --dport telnet -j REJECT
iptables -A FORWARD -p tcp --dport telnet -j REJECT
Technically speaking though since your default policies are to drop any packets that don't match anyways there really is no need to explicitly reject these packets because they will be dropped anyways. But it is good practice to make sure.

Quote:
Originally Posted by Richtown
OK, so now i want to open port 80 (http) to my LAN but the following code does not work:
iptables -A FORWARD -p tcp -dport 80 -j ACCEPT
iptables -A FORWARD -p tcp -sport 80 -j ACCEPT
Now I'm confused as to what you want here. Do you want the clients in your LAN to be able to browse the NET? Or do you have a web server in your LAN that you want people on the outside to be able to view?

If it's the former then:

Code:
iptables -A FORWARD -p tcp --dport 80 -j ACCEPT
Should work. Also don't forget to allow all established and related packets back through your gateway

Code:
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
if it's a web server that you want people from the outside to view then you will need to explain your network setup a little bit more (ala ip addresses NAT or no NAT, etc.).

HTH,

Centinul
 
Old 05-01-2007, 06:16 AM   #7
Richtown
Member
 
Registered: Apr 2006
Location: Portsmouth
Distribution: CentOS & Ubuntu
Posts: 49

Original Poster
Rep: Reputation: 15
Question

Sorry yeah i would like the Internal Network to access the NET.

Code:
iptables -A FORWARD -p tcp --dport 80 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
I tried the about but i still cannot gain access to the NET via a LAN Machine.

If i enter iptables -A FOWARD ACCEPT then i have access to the NET. This is my iptables -L

Code:
Chain INPUT (policy DROP)
target     prot opt source
ACCEPT     all -- anywhere        anywhere      stat RELATED,ESTABLISHED

Chain FORWARD (policy DROP)
target     prot opt source
ACCEPT     tcp  --  anywhere      anywhere      tcp dpt:http
ACCEPT     all  -- anywhere       anywhere      state RELATED,ESTABLISHED

Chain OUTPUT (policy DROP)
target     prot opt source
Would it have anything to do with no ACCEPT defined in the OUTPUT chain?
 
Old 05-01-2007, 06:34 AM   #8
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
That first rule I gave you should work in theory, we should try and investigate why it isn't working. Read the link to the tutorial about logging. You need to log those dropped packets. That way when you try and access the internet from a LAN machine you should be able to see WHY the packets are being dropped.

Please post the "iptables -L" output when the rule I gave you is in place.

The OUTPUT chain is used on the gateway. If you wanted your gateway machine to access the internet you would put the same rule in place except you would use the OUTPUT chain instead.

Just for the fun of it try this rule:

Code:
iptables -A FORWARD -i eth1 -o eth0 -p tcp --dport 80 -j ACCEPT
Also don't forget to drop and recreate all your rules everytime that way you make sure there aren't any extraneous rules in there.
 
Old 05-01-2007, 07:12 AM   #9
Richtown
Member
 
Registered: Apr 2006
Location: Portsmouth
Distribution: CentOS & Ubuntu
Posts: 49

Original Poster
Rep: Reputation: 15
Iptables Rules Entered:
Code:
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables -A FORWARD -i eth1 -o eth0 -p tcp --dport 80 -j ACCEPT
iptables -A FORWARD -j LOG --log-prefix "IPF FWD DROP"
iptables -L
Code:
Chain INPUT (policy DROP)
target     prot opt source                              destination

Chain FORWARD (policy DROP)
target     prot opt source                               destination
ACCEPT     tcp   --  anywhere       anywhere      tcp dpt:http
LOG        all   --  anywhere       LOG level warning prefix 'IPF FWD DROP'

Chain OUTPUT (policy DROP)
target     prot opt source                                destination
From my LAN machine i tried www.google.com. Log on the Gateway was:
Code:
IPF FWD DROPIN=eth2 OUT=eth0 SRC=192.168.2.150
 DST=172.16.87.2 LEN=66 TOS=0X00 PREC=0X00 TTL=63 ID=3194 DF PROTO=UDP SPT=33035 DTP=53 LEN=40
eth2 will be used for my DMZ but its not setup at the moment it has a network subnet of 192.168.3./24. The 172.16.87.2 is the DNS server. The gateway is on a Virtual Machine (VMWARE) The IP Address of the gateway is 172.16.87.128 ITS gateway is 172.16.87.1.
192.168.2.150 is the LAN machine that executed the google request. As said before if i remove the OUPUT,INPUT,FORWARD DROP and replace with ACCEPT i have connectivity to the NET. So that illimunates that problem.

Would it have anything to do with port 53 (DNS) if it cannot resolve google.com then it wont be able to connect to it.

Thanks Rich
 
Old 05-01-2007, 07:45 AM   #10
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Quote:
Originally Posted by Richtown
Would it have anything to do with port 53 (DNS) if it cannot resolve google.com then it wont be able to connect to it.
Yes that is most likely your problem. You will also have to allow DNS access from your LAN machines because if you don't they won't be able to resolve the addresses to the correct IP addresses. A rule that would allow that would be the following:

Code:
iptables -A FORWARD -i eth1 -o eth0 -p tcp --dport 53 -j accept
Unfortunately the example that you gave for the rules that were entered would fail no matter way because you specified the eth1 interface as the input interface when you tried from the eth2 interface. Make sure that you add the proper rules to support that interface.

Let me know if you have any other questions.

Thanks,

Centinul
 
Old 05-02-2007, 04:15 AM   #11
maxut
Senior Member
 
Registered: May 2003
Location: istanbul
Distribution: debian - redhat - others
Posts: 1,188

Rep: Reputation: 50
Quote:
Originally Posted by Centinul
Code:
iptables -A FORWARD -i eth1 -o eth0 -p tcp --dport 53 -j accept
Centinul
let me correct please..
i think it must be UDP port 53 instead of TCP 53
so
Code:
iptables -A FORWARD -i eth1 -o eth0 -p udp --dport 53 -j accept
no need "TCP 53" to resolve domain names. Also UDP 53 packets are shown in logs..

best regards.
 
Old 05-02-2007, 04:24 AM   #12
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Quote:
Originally Posted by maxut
let me correct please..
i think it must be UDP port 53 instead of TCP 53
so
Code:
iptables -A FORWARD -i eth1 -o eth0 -p udp --dport 53 -j accept
no need "TCP 53" to resolve domain names. Also UDP 53 packets are shown in logs..

best regards.
Thanks for the correction. I must have been sleeping when I wrote that.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
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 problem lalata Linux - Software 14 12-07-2005 06:56 AM
Iptables problem,help me please. ryanux Linux - Security 1 05-21-2004 09:59 PM
iptables problem fleshwound Linux - Networking 6 12-27-2003 05:30 PM
iptables problem chandra Linux - Newbie 4 03-26-2002 09:35 PM

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

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