LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-15-2006, 03:55 AM   #1
cylarz
Member
 
Registered: Aug 2005
Location: California
Distribution: CentOS 5
Posts: 54

Rep: Reputation: 15
banning multiple IP's with iptables


Hey guys. I'm running Fedora Core 5 and want to customize my IPTABLES firewall in order to bolster system security. I have two separate questions that aren't being answered by the tutorials I've read:

1. I want to ban an entire range of IP address within a given network, not just a single IP. There's got to be a way to do that w/o typing out 256 or more addresses and entering them in one-by-one! I typed the following:
-----
root# iptables -A INPUT -j DROP 123.456.789.0/24
Bad argument `123.456.789.0/24'
Try `iptables -h' or 'iptables --help' for more information.
root#
------

Where of course 123.456.789.0 is the class C network whose incoming packets I'm trying to stop at my firewall. It is to be completely prohibited from contacting the system in any way and any packets that do arrive from there are to go unacknowledged. I don't even want users on that network being able to view my web pages.

So what's wrong with my syntax? The tutorial I was using swears up and down that the command *should* work as advertised. Maybe iptables has changed since it was written, so can anyone tell me the correct syntax?

2. I entered a long list of individual IP addresses into the firewall using the command given above. I confirmed that they'd been loaded by running iptables -L. It showed me the rules as I expected to see. HOWEVER, the rules were all gone when I rebooted the entire system and ran iptables -L a second time. What do I need to do in order to make the iptables rules permanent so that they'll survive a system reboot?

Thanks in advance, Matt
 
Old 05-15-2006, 07:12 AM   #2
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
1. The ip address(es) is/are specified via the -s (source address) option, e.g.

Code:
iptables -A INPUT -s 123.456.789.0/24 -j DROP
2. Use iptables-save and iptables-restore. You need to redirect the input/output, e.g. 'iptables-save > /root/iptables.conf', and put 'iptables-restore < /root/iptables.conf' in your rc.local.
 
Old 05-15-2006, 11:58 AM   #3
cylarz
Member
 
Registered: Aug 2005
Location: California
Distribution: CentOS 5
Posts: 54

Original Poster
Rep: Reputation: 15
re: banning multiple IP's with iptables

Thanks for your help. Apparently I was being tripped up by a simple typo.

One more question. I'd like to write a rule that says "Ban ALL connections from ALL systems, except for the ones explictly allowed to connect." I'd also like to write a rule that says, "If a system wants to connect to port 80, check the banned list. If it's not there, let it in."

Where in the iptables rule list would I put such rules - the beginning or the end? I'm afraid of guessing wrong and locking myself out of my own server. Does iptables look at the "allow" section before it looks at the "deny" section (the way TCP wrappers does), or does it just apply the rules sequentially?

Thanks
 
Old 05-15-2006, 12:06 PM   #4
cylarz
Member
 
Registered: Aug 2005
Location: California
Distribution: CentOS 5
Posts: 54

Original Poster
Rep: Reputation: 15
One more thing...when you say put that command in rc.local, do you mean simply edit the file and type in a line that says "iptables-restore" after running the command above?
 
Old 05-15-2006, 01:17 PM   #5
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
Quote:
Originally Posted by cylarz
I'd like to write a rule that says "Ban ALL connections from ALL systems, except for the ones explictly allowed to connect." I'd also like to write a rule that says, "If a system wants to connect to port 80, check the banned list. If it's not there, let it in."
Ccheck post #2 in this thread: http://www.linuxquestions.org/questi...d.php?t=438127 for my iptables script. I think it doeas a lot of what you are wanting to do. Basically, just set your default policy to DROP, and then add ACCEPT rules for those you want ot allow in.
 
Old 05-15-2006, 01:31 PM   #6
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
Yes, the kernel applies the rules sequentially.

haertig has covered the rest.

As for rc.local, yeah, just would type the line in somewhere, but if you use haertig's script, you don't need iptables-{save,restore} anymore.

Last edited by ioerror; 05-15-2006 at 02:11 PM.
 
Old 05-22-2006, 03:42 PM   #7
gloomy
Member
 
Registered: Jan 2006
Location: Finland
Distribution: Mainly Gentoo
Posts: 119

Rep: Reputation: 15
I might add a quick note:

is there any difference between 0.0.0.0/24 and the one I've been using; e.g. -m iprange --src-range 1.1.1.1-2.2.2.2 -j LOG?
 
  


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
Multiple IP's on one NIC lts Linux - Networking 2 04-19-2006 09:51 AM
Multiple IP's iptables doublejoon Linux - Security 1 02-01-2006 03:00 PM
IPTables - Multiple Public IP's to private IP's matneyc Linux - Security 8 05-27-2005 12:23 PM
Multiple Apache Sites, on Multiple IP's, on Same Box?? RickyJ Linux - General 1 06-19-2003 10:55 AM
Multiple Apache Sites, on Multiple IP's, on Same Box?? RickyJ Linux - Software 0 06-19-2003 09:50 AM

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

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