Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-27-2006, 05:02 PM
|
#1
|
Member
Registered: Sep 2004
Location: Norge
Distribution: Debian
Posts: 116
Rep:
|
Problems with IPtables on Debian
Iv been sitting here for the last few days and cant seem to get hang of the iptables stuff.
How can I open port 80 to the web server whos on another box on the lan? I'v tried a zillion things but nothing works. All i found so far either old or for different distro and...well Iam about to give it up. Running fresh install of Debian with no gui btw.
And how come port 22 is open? I thought I closed it.
Code:
#!/bin/sh
PATH=/usr/sbin:/sbin:/bin:/usr/bin
#
# delete all existing rules.
#
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X
# Always accept loopback traffic
iptables -A INPUT -i lo -j ACCEPT
# Allow established connections, and those not coming from the outside
iptables -A OUTPUT -o eth0 -p tcp --dport 22 -j DROP
iptables -A FORWARD -p tcp -s 192.168.0.0/24 -i eth0 -j DROP
iptables -A FORWARD -p udp -s 192.168.0.0/24 -i eth0 -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW -i ! eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow outgoing connections from the LAN side.
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
# Masquerade.
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# Don't forward from the outside to the inside.
iptables -A FORWARD -i eth0 -o eth0 -j REJECT
# Enable routing.
echo 1 > /proc/sys/net/ipv4/ip_forward
|
|
|
12-27-2006, 05:34 PM
|
#2
|
Senior Member
Registered: Mar 2003
Location: Beautiful BC
Distribution: RedHat & clones, Slackware, SuSE, OpenBSD
Posts: 1,791
Rep:
|
Kanon
If you want to redirect traffic to another box - port 80 on another box, you need to use
PREROUTING and DNAT.
iptables -A FORWARD -i eth0 -d <ip of webserver> -j ACCEPT
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-dest <IP of webserver>:80
You have only closed ssh traffic going out.
iptables -A INPUT -i eth0 -p tcp --dport 22 -j DROP
will close port 22 to incoming traffic on eth0. the OUTPUT rule for port 22 is not necessary.
Suggest you start your rules with
iptables -P INPUT DROP
iptables -P FORWARD DROP
This will enforce the default deny stance, effectively seals your system.
Last edited by ppuru; 12-27-2006 at 05:38 PM.
|
|
|
12-27-2006, 05:35 PM
|
#3
|
Senior Member
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250
Rep:
|
Working with iptables is an art as well as a science. Do you have to do it manually? Firestarter is an easy and fairly secure GUI. Shorewall or some other front-end for iptables is easier and as secure as you want to make it.
|
|
|
12-27-2006, 06:00 PM
|
#4
|
Member
Registered: Jan 2006
Location: Vancouver BC
Distribution: LFS, SLak, Gentoo, Debian
Posts: 291
Rep:
|
Quote:
Originally Posted by rickh
Working with iptables is an art as well as a science. Do you have to do it manually? Firestarter is an easy and fairly secure GUI. Shorewall or some other front-end for iptables is easier and as secure as you want to make it.
|
true enough, but the op did state that this is a cli only box he is working on, so not really a viable option.
|
|
|
12-28-2006, 12:55 AM
|
#5
|
Member
Registered: Sep 2004
Location: Norge
Distribution: Debian
Posts: 116
Original Poster
Rep:
|
Quote:
Originally Posted by ppuru
Kanon
If you want to redirect traffic to another box - port 80 on another box, you need to use
PREROUTING and DNAT.
iptables -A FORWARD -i eth0 -d <ip of webserver> -j ACCEPT
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-dest <IP of webserver>:80
You have only closed ssh traffic going out.
iptables -A INPUT -i eth0 -p tcp --dport 22 -j DROP
will close port 22 to incoming traffic on eth0. the OUTPUT rule for port 22 is not necessary.
Suggest you start your rules with
iptables -P INPUT DROP
iptables -P FORWARD DROP
This will enforce the default deny stance, effectively seals your system.
|
Nice! ssh port is now closed.
But I dont have PREROUTING. How do I add the file/filter? Im assuming its going into the netfilter.
Thanks for the solution to port 22!
|
|
|
12-28-2006, 08:43 AM
|
#6
|
Member
Registered: Apr 2005
Distribution: Debian, OpenBSD,Fedora,RedHat
Posts: 228
Rep:
|
Quote:
Originally Posted by Kanon
Nice! ssh port is now closed.
But I dont have PREROUTING. How do I add the file/filter? Im assuming its going into the netfilter.
Thanks for the solution to port 22!
|
I think solution before will solve problem related to forwarding.
But you have to put
echo 1 >/proc/sys/net/ipv4/ip_forward
in order to make your machine able to forward packets.
Just put this as last line inside your script
Regards and best wishes
|
|
|
12-28-2006, 09:28 AM
|
#7
|
Member
Registered: Sep 2004
Location: Norge
Distribution: Debian
Posts: 116
Original Poster
Rep:
|
I got "echo 1 >/proc/sys/net/ipv4/ip_forward" as the last line. But I get "Unable to connect" in my browser. And I get ann error:
Code:
bound to my.static.ip.10 -- renewal in 10800 seconds.
iptables: No chain/target/match by that name
iptables: No chain/target/match by that name
done.
if I remove or comment out the PREROUTING line I get no error. And no access to the web server; "Unable to connect".
Thanks for helping me out here every one!
|
|
|
12-28-2006, 12:23 PM
|
#8
|
Senior Member
Registered: Mar 2003
Location: Beautiful BC
Distribution: RedHat & clones, Slackware, SuSE, OpenBSD
Posts: 1,791
Rep:
|
My sincere apologies Kanon
the prerouting line should be
iptables -t nat -A PREROUTING ...
|
|
|
12-28-2006, 02:23 PM
|
#9
|
Member
Registered: Sep 2004
Location: Norge
Distribution: Debian
Posts: 116
Original Poster
Rep:
|
W00T!! I got no hair left now!! Just kidding
Ok, now its kinda working. Well, that is the port is open, but clients dont get any pages from the web server.
This is my iptables now:
Code:
#!/bin/sh
PATH=/usr/sbin:/sbin:/bin:/usr/bin
#
# delete all existing rules.
#
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X
#iptables -P INPUT DROP
#iptables -P FORWARD DROP
# Always accept loopback traffic
iptables -A INPUT -i lo -j ACCEPT
# Allow established connections, and those not coming from the outside
iptables -A INPUT -s the.bad.guy.ip -j DROP
iptables -A OUTPUT -o eth0 -p tcp --dport 31337 --sport 31337 -j DROP
iptables -A INPUT -i eth0 -p tcp --dport 22 -j DROP
#iptables -A FORWARD -p tcp -s 192.168.0.0/24 -i eth0 -j DROP
#iptables -A FORWARD -p udp -s 192.168.0.0/24 -i eth0 -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW -i ! eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow outgoing connections from the LAN side.
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
# Masquerade.
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# Don't forward from the outside to the inside.
iptables -A FORWARD -i eth0 -d 192.168.0.120 -j ACCEPT
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-dest 192.168.0.120:80
iptables -A FORWARD -i eth0 -o eth0 -j REJECT
# Enable routing.
echo 1 > /proc/sys/net/ipv4/ip_forward
Now, I can connect to the web server from the inside with no problem. But not from the outside. And I can get out from the web server (Lynx) with no problem.
Can any one see any problems with the table? Think Im going iptables-blind :P
|
|
|
12-28-2006, 02:48 PM
|
#10
|
Member
Registered: Nov 2003
Location: The Colony, TX
Distribution: Slackware, Debian Etch, FreeBSD, MicroSh*t free.
Posts: 209
Rep:
|
Try changing this line, the one right before your PREROUTING line
Code:
iptables -A FORWARD -i eth0 -d 192.168.0.120 -j ACCEPT
to this:
Code:
iptables -A FORWARD -p tcp -d 192.168.0.120 --dport 80 -j ACCEPT
And you may also need this line before those two...
Code:
iptables -A INPUT -p tcp -i eth0 -s 0.0.0.0/0 --dport 80 -j ACCEPT
|
|
|
12-28-2006, 05:06 PM
|
#11
|
Member
Registered: Sep 2004
Location: Norge
Distribution: Debian
Posts: 116
Original Poster
Rep:
|
Hmm Did not help any. How can I check to see if the gw establishes connection at all between the machines?
|
|
|
12-28-2006, 05:52 PM
|
#12
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
It appears that you are trying to build a firewall to protect a home network on a DSL connection. May I suggest the nicely tailored setup HomeLANSecurity, which is easy to figure out & easy to add stuff for your specific purposes. It just makes a lot of sense to me, to use a mature configuration, in which someone has paid a lot of attention to details, and has had the benefit of many previous users' input. You can still modify it to your specification, in case it doesn't do everything you want (ie. hacking is encouragead).
--- rod.
|
|
|
12-28-2006, 05:57 PM
|
#13
|
Member
Registered: Sep 2004
Location: Norge
Distribution: Debian
Posts: 116
Original Poster
Rep:
|
Yeah, looks good. And its cli too!
I'll have a go at it after a few hours of sleep
Thanks all, for all the help!!
|
|
|
All times are GMT -5. The time now is 05:07 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|