Linux Netfilter discussions
 help / color / mirror / Atom feed
* iptables scripts
@ 2003-10-31 11:25 Gilles Yue
  2003-10-31 12:12 ` Chris Brenton
  0 siblings, 1 reply; 8+ messages in thread
From: Gilles Yue @ 2003-10-31 11:25 UTC (permalink / raw)
  To: netfilter

[-- Attachment #1: Type: text/plain, Size: 375 bytes --]

Dear all,

 

What is the difference between saving iptables rules by typing
/sbin/service save and putting it in a script which executes when the pc
is restarted?

 

Secondly, if you were to put all your firewall rules in a script, where
(on which path) would you put it to have it executed when the machine
reboots.

 

Thanks & Regards

gy

 

 


[-- Attachment #2: Type: text/html, Size: 2462 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: iptables scripts
  2003-10-31 11:25 iptables scripts Gilles Yue
@ 2003-10-31 12:12 ` Chris Brenton
  2003-10-31 12:26   ` Robert P. J. Day
  0 siblings, 1 reply; 8+ messages in thread
From: Chris Brenton @ 2003-10-31 12:12 UTC (permalink / raw)
  To: Gilles Yue; +Cc: netfilter

On Fri, 2003-10-31 at 06:25, Gilles Yue wrote: 
> 
> What is the difference between saving iptables rules by typing
> /sbin/service save and putting it in a script which executes when the
> pc is restarted?

IMHO this is a personal preference thing. Some people prefer to use the
save/restore scripts. Some people (like myself) prefer to write their
own shell script. Its all a matter of personal preference.

For me, I just find working with a shell script easier. I typically
remotely manage my firewalls. I find it easier to vi a file rather than
work from the command line (you are also less likely to shoot yourself
in the foot by messing up your rules and blocking your remote session.
Been there, done that ;-). I also like being able to add in additional
functionality like variables, do loops, etc. Your mileage may vary.

> Secondly, if you were to put all your firewall rules in a script,
> where (on which path) would you put it to have it executed when the
> machine reboots.

Again this is somewhat personal choice. I create /root/firewall and
place all my firewall related scripts in there. You could put it in
something like /usr/local/sbin, but now you have a longer path to type
(ya I know, I'm *very* lazy ;-) and other unrelated files to contend
with in the same directory. 

HTH,
C




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: iptables scripts
  2003-10-31 12:12 ` Chris Brenton
@ 2003-10-31 12:26   ` Robert P. J. Day
  2003-10-31 13:11     ` Chris Brenton
  0 siblings, 1 reply; 8+ messages in thread
From: Robert P. J. Day @ 2003-10-31 12:26 UTC (permalink / raw)
  To: Chris Brenton; +Cc: Gilles Yue, iptables mailing list

On 31 Oct 2003, Chris Brenton wrote:

> On Fri, 2003-10-31 at 06:25, Gilles Yue wrote: 
> > 
> > What is the difference between saving iptables rules by typing
> > /sbin/service save and putting it in a script which executes when the
> > pc is restarted?
> 
> IMHO this is a personal preference thing. Some people prefer to use the
> save/restore scripts. Some people (like myself) prefer to write their
> own shell script. Its all a matter of personal preference.
> 
> For me, I just find working with a shell script easier. I typically
> remotely manage my firewalls. I find it easier to vi a file rather than
> work from the command line (you are also less likely to shoot yourself
> in the foot by messing up your rules and blocking your remote session.
> Been there, done that ;-). I also like being able to add in additional
> functionality like variables, do loops, etc. Your mileage may vary.

that's the big bonus -- that you can do some preliminary setup in a
shell script like setting variables for convenience, setting kernel
parameters, loading modules and the like.

for the iptables tutorial i was talking about that i'm giving on monday,
here's the first part of my script, just to show folks what they can do:

--------------------------------------------

#!/bin/sh

# Commands.

IPT="/sbin/iptables"

# Interfaces.

INET_IF="eth0"
LOOPBACK_IF="lo"

# Addresses.

MY_IP="192.168.1.101"

# Special addresses.

LOOPBACK="127.0.0.0/8"
PRIVATE_CLASS_A="10.0.0.0/8"
PRIVATE_CLASS_B="176.16.0.0/12"
PRIVATE_CLASS_C="192.168.0.0/16"
CLASS_D="224.0.0.0/4"
CLASS_E="240.0.0.0/5"
BROADCAST_SRC="0.0.0.0"
BROADCAST_DEST="255.255.255.255"

# Ports.

PRIVPORTS="0:1023"
UNPRIVPORTS="1024:65535"

# Collective addresses.

BAD_SOURCE_ADDRS="$LOOPBACK $CLASS_D $CLASS_E $MY_IP"

ALLOWED_INCOMING_SERVICES="ssh http"
DISALLOWED_OUTGOING_SERVICES="telnet"

#######################################################
# Load necessary modules netfilter modules.
#######################################################

/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_state

#######################################################
# Set some /proc/sys settings to nail some bad stuff.
#######################################################

echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

echo 1 > /proc/sys/net/ipv4/tcp_syncookies

for f in /proc/sys/net/ipv4/conf/*/accept_source_route ; do
	echo 0 > $f
done

for f in /proc/sys/net/ipv4/conf/*/accept_redirects ; do
	echo 0 > $f
done

for f in /proc/sys/net/ipv4/conf/*/send_redirects ; do
	echo 0 > $f
done

for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do
	echo 1 > $f
done

for f in /proc/sys/net/ipv4/conf/*/log_martians ; do
	echo 1 > $f
done

#######################################################
# Set the chain policies.
#######################################################

$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT	# Purists probably hate this.



... etc etc, you get the idea ...

shell scripts are indeed the way to go.

rday



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: iptables scripts
  2003-10-31 12:26   ` Robert P. J. Day
@ 2003-10-31 13:11     ` Chris Brenton
  2003-10-31 13:22       ` Robert P. J. Day
  0 siblings, 1 reply; 8+ messages in thread
From: Chris Brenton @ 2003-10-31 13:11 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: Gilles Yue, iptables mailing list

On Fri, 2003-10-31 at 07:26, Robert P. J. Day wrote:
>
> for the iptables tutorial i was talking about that i'm giving on monday,
> here's the first part of my script, just to show folks what they can do:

This is *totally* cool. Thank you for sharing this with the list! :)

The only thing I would add would be:
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
iptables --table nat --flush

or what ever you need. This way you can run it from the command line and
clear out all existing rules before you write everything back in.

HTH,
C




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: iptables scripts
  2003-10-31 13:11     ` Chris Brenton
@ 2003-10-31 13:22       ` Robert P. J. Day
  2003-10-31 13:54         ` Alistair Tonner
  2003-10-31 13:56         ` Achim Dreyer
  0 siblings, 2 replies; 8+ messages in thread
From: Robert P. J. Day @ 2003-10-31 13:22 UTC (permalink / raw)
  To: Chris Brenton; +Cc: Gilles Yue, iptables mailing list

On 31 Oct 2003, Chris Brenton wrote:

> On Fri, 2003-10-31 at 07:26, Robert P. J. Day wrote:
> >
> > for the iptables tutorial i was talking about that i'm giving on monday,
> > here's the first part of my script, just to show folks what they can do:
> 
> This is *totally* cool. Thank you for sharing this with the list! :)
> 
> The only thing I would add would be:
> iptables -F INPUT
> iptables -F OUTPUT
> iptables -F FORWARD
> iptables --table nat --flush
> 
> or what ever you need. This way you can run it from the command line and
> clear out all existing rules before you write everything back in.

ah, grasshopper, i didn't show you the other two scripts i'm going
to demo.  first, there's the lockdown script, to be run if you realize
you've been hacked:
---------------------------------------------------------
#!/bin/sh

# PANIC!  Lock the machine down.

IPT="/sbin/iptables"

# Flush all chains.

$IPT -F			# by default filter
$IPT -t nat -F
$IPT -t mangle -F

# Delete all user-defined chains.

for table in filter nat mangle ; do
	$IPT -t $table -X
done

# Reset all policies to DROP.

for chain in INPUT OUTPUT FORWARD ; do
	$IPT -P $chain DROP
done

echo "System totally locked down."
-----------------------------------------------------------

  and then there's the "clear all" script, which you would run
if you made a total mess of your rules and just want to clear
them out:

----------------------------------------------------------
#!/bin/sh

# PANIC!  We've screwed up our tables.

IPT="/sbin/iptables"

# Flush all chains.

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

# Delete all user-defined chains.

for table in filter nat mangle ; do
	$IPT -t $table -X
done

# Reset all policies to ACCEPT.

for chain in INPUT OUTPUT FORWARD ; do
	$IPT -P $chain ACCEPT
done

echo "System totally open, you are now fair game."
-------------------------------------------------

  the tutorial will suggest that users can incorporate 
the above in their main script any way they want.

rday



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: iptables scripts
  2003-10-31 13:22       ` Robert P. J. Day
@ 2003-10-31 13:54         ` Alistair Tonner
  2003-10-31 13:55           ` Robert P. J. Day
  2003-10-31 13:56         ` Achim Dreyer
  1 sibling, 1 reply; 8+ messages in thread
From: Alistair Tonner @ 2003-10-31 13:54 UTC (permalink / raw)
  To: Robert P. J. Day, Chris Brenton; +Cc: Gilles Yue, iptables mailing list

On October 31, 2003 08:22 am, Robert P. J. Day wrote:
> On 31 Oct 2003, Chris Brenton wrote:
> > On Fri, 2003-10-31 at 07:26, Robert P. J. Day wrote:
> > > for the iptables tutorial i was talking about that i'm giving on
> > > monday, here's the first part of my script, just to show folks what
> > > they can do:
> >
> > This is *totally* cool. Thank you for sharing this with the list! :)
> >
> > The only thing I would add would be:
> > iptables -F INPUT
> > iptables -F OUTPUT
> > iptables -F FORWARD
> > iptables --table nat --flush
> >
> > or what ever you need. This way you can run it from the command line and
> > clear out all existing rules before you write everything back in.
>
> ah, grasshopper, i didn't show you the other two scripts i'm going
> to demo.  first, there's the lockdown script, to be run if you realize
> you've been hacked:





	You might NOT want to run this from ssh sessions!!!
              *grin* ... sure to most of us this is obvious....not 
	however to everyone ... 

> ---------------------------------------------------------
> #!/bin/sh
>
> # PANIC!  Lock the machine down.
>
> IPT="/sbin/iptables"
>
> # Flush all chains.
>
> $IPT -F			# by default filter
> $IPT -t nat -F
> $IPT -t mangle -F
>
> # Delete all user-defined chains.
>
> for table in filter nat mangle ; do
> 	$IPT -t $table -X
> done
>
> # Reset all policies to DROP.
>
> for chain in INPUT OUTPUT FORWARD ; do
> 	$IPT -P $chain DROP
> done
>
> echo "System totally locked down."
> -----------------------------------------------------------
>
>   and then there's the "clear all" script, which you would run
> if you made a total mess of your rules and just want to clear
> them out:
>
> ----------------------------------------------------------
> #!/bin/sh
>
> # PANIC!  We've screwed up our tables.
>
> IPT="/sbin/iptables"
>
> # Flush all chains.
>
> $IPT -F
> $IPT -t nat -F
> $IPT -t mangle -F
>
> # Delete all user-defined chains.
>
> for table in filter nat mangle ; do
> 	$IPT -t $table -X
> done
>
> # Reset all policies to ACCEPT.
>
> for chain in INPUT OUTPUT FORWARD ; do
> 	$IPT -P $chain ACCEPT
> done
>
> echo "System totally open, you are now fair game."
> -------------------------------------------------
>
>   the tutorial will suggest that users can incorporate
> the above in their main script any way they want.
>
> rday

-- 

	Alistair Tonner
	nerdnet.ca
	Senior Systems Analyst - RSS
	
     Any sufficiently advanced technology will have the appearance of magic.
	Lets get magical!


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: iptables scripts
  2003-10-31 13:54         ` Alistair Tonner
@ 2003-10-31 13:55           ` Robert P. J. Day
  0 siblings, 0 replies; 8+ messages in thread
From: Robert P. J. Day @ 2003-10-31 13:55 UTC (permalink / raw)
  To: Alistair Tonner; +Cc: Chris Brenton, Gilles Yue, iptables mailing list

On Fri, 31 Oct 2003, Alistair Tonner wrote:

> 	You might NOT want to run this from ssh sessions!!!
>               *grin* ... sure to most of us this is obvious....not 
> 	however to everyone ... 

oh, come on ... i have to leave *something* as a lesson for
the student.

rday



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: iptables scripts
  2003-10-31 13:22       ` Robert P. J. Day
  2003-10-31 13:54         ` Alistair Tonner
@ 2003-10-31 13:56         ` Achim Dreyer
  1 sibling, 0 replies; 8+ messages in thread
From: Achim Dreyer @ 2003-10-31 13:56 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: iptables mailing list

On Fri, 31 Oct 2003, Robert P. J. Day wrote:

[..]
> ah, grasshopper, i didn't show you the other two scripts i'm going
> to demo.  first, there's the lockdown script, to be run if you realize
> you've been hacked:
> ---------------------------------------------------------
> #!/bin/sh
> 
> # PANIC!  Lock the machine down.
> 
> IPT="/sbin/iptables"
> 
> # Flush all chains.
> 
> $IPT -F			# by default filter
> $IPT -t nat -F
> $IPT -t mangle -F
> 
> # Delete all user-defined chains.
> 
> for table in filter nat mangle ; do
> 	$IPT -t $table -X
> done
> 
> # Reset all policies to DROP.
> 
> for chain in INPUT OUTPUT FORWARD ; do
> 	$IPT -P $chain DROP
> done
> 
> echo "System totally locked down."
> -----------------------------------------------------------


I shurely would set the default lockdown policy _before_ flushing the 
tables as there is no matching rule forcing a drop of packets between
the flush and the policy set ->  packets could slip through!



>   and then there's the "clear all" script, which you would run
> if you made a total mess of your rules and just want to clear
> them out:
> 
> ----------------------------------------------------------
> #!/bin/sh
> 
> # PANIC!  We've screwed up our tables.
> 
> IPT="/sbin/iptables"
> 
> # Flush all chains.
> 
> $IPT -F
> $IPT -t nat -F
> $IPT -t mangle -F
> 
> # Delete all user-defined chains.
> 
> for table in filter nat mangle ; do
> 	$IPT -t $table -X
> done
> 
> # Reset all policies to ACCEPT.
> 
> for chain in INPUT OUTPUT FORWARD ; do
> 	$IPT -P $chain ACCEPT
> done
> 
> echo "System totally open, you are now fair game."
> -------------------------------------------------


in this case it doesn't matter as the end result will open all gates ;-)





Regards,
Achim Dreyer
--
A. Dreyer, Senior SysAdmin (UNIX&Network) / Internet Security Consultant



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2003-10-31 13:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-10-31 11:25 iptables scripts Gilles Yue
2003-10-31 12:12 ` Chris Brenton
2003-10-31 12:26   ` Robert P. J. Day
2003-10-31 13:11     ` Chris Brenton
2003-10-31 13:22       ` Robert P. J. Day
2003-10-31 13:54         ` Alistair Tonner
2003-10-31 13:55           ` Robert P. J. Day
2003-10-31 13:56         ` Achim Dreyer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox