LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-30-2010, 06:07 AM   #1
Ammad
Member
 
Registered: Apr 2004
Distribution: redhat 9.0, fc4, redhat as 4
Posts: 522

Rep: Reputation: 31
bash script and iptables


HI,

I need help to write a script for iptables. i have list of src ip address in seperate file to which i want to allow Internet. but when i remove the particular ip address from the file i need to restart the iptables and due to this all sessions are disconnected. this makes problem when a user is downloading a large file.

I need a script that will delete/add ip address in rules.



thanks.
 
Old 08-30-2010, 08:14 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Here's the way I do it; feel free to modify as needed.
Code:
cat zone.awk
#!/usr/bin/awk
#
#       Purposs:        read county IP range addresses, produce IPTABLES
#                       entries to block all traffic from that range
#

{
        if (substr($0, 1, 1) == "#") {
                printf ("%s\n", $0);
        } else {
                # this entry blocks all traffic
                printf ("iptables -A INPUT -s %s -j DROP\n", $1);
        }
}
where the list of ranges look like
Code:
head cn.zone
# Country: CHINA
# ISO Code: CN
# Total Networks: 1,789
# Total Subnets:  258,216,448
1.12.0.0/14
1.24.0.0/13
1.45.0.0/16
1.48.0.0/15
1.51.0.0/16
1.56.0.0/13
and the output looks like
Code:
head countryblock
# Country: CHINA
# ISO Code: CN
# Total Networks: 1,789
# Total Subnets:  258,216,448
iptables -A INPUT -s 1.12.0.0/14 -j DROP
iptables -A INPUT -s 1.24.0.0/13 -j DROP
iptables -A INPUT -s 1.45.0.0/16 -j DROP
I
Code:
touch countryblock
chmod 755 countryblock
awk -f zone.awk cn.zone > countryblock
awk -f zone.awk kr.zone >> countryblock
(and any others)
Then, as root
Code:
countryblock
and that's that.

Because the purpose is blocking the network range and the subnets (you get that information from whois) and this gets executed at boot it may not fit your exact need for on-the-fly editing of IPTABLES, but it may give you a start.

An alternative is to think about /etc/hosts.deny and /etc/hosts.allow (which you can edit simply with sed). Simple, one-line entries for a single IP address in those are fast and effective for dynamic access; /etc/hosts.deny entries look like this
Code:
head /etc/hosts.deny
#
# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# Version:      @(#)/etc/hosts.deny     1.00    05/28/93
#
# Author:       Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org
#
#
#
# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# Version:      @(#)/etc/hosts.deny     1.00    05/28/93
#
# Author:       Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org
#
#

ALL: 201.11.209.251
ALL: 211.254.130.116
ALL: 189.17.16.163
ALL: 200.24.221.83
ALL: 88.26.207.72
ALL: 216.201.255.234
ALL: 59.120.145.84
ALL: 75.75.19.252
Hope this helps some.

Last edited by tronayne; 08-30-2010 at 08:15 AM.
 
Old 08-30-2010, 02:38 PM   #3
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by tronayne View Post
Here's the way I do it...
I don't want to deny that your post was an interesting read, but the OP seems to have been asking for something rather different; the request seems roughly to be for a system which can dynamically add to a whitelist, rather than manage a large-ish blacklist.

Of course, I'm going to add my two-penny worth by not giving the OP a directly usable approach, either

Firstly, the problem with a lot of these whitelist/blacklist approaches is that it easy to build-in a need to do lots of look-ups whenever a new connection comes in, and that way build in a vulnerability to DoS/DDoS attacks. This is not a good thing and needs to be examined in context to see how serious it could be. Having said that, something like denyhosts seems to take a broadly sensible approach (but is intended for blocking the outside world trying to get in, rather than the other way around).

What the OP is asking for, in most cases (and there will be cases in which this won't work, but we don't have details) is dealt with by allowing addresses or address ranges; this does not need any dynamic building of lists of allowable addresses for internet access (eg, allowing 192.168.0.0/24), but it doesn't dynamically block any addresses in that range.

You have to ask 'What is the risk that I am trying to protect against?' It seems that this looser approach could allow someone within your own network to have internet access at a time at which you didn't want this to happen. Is this a big problem? (Maybe, it is if you know that you have to have insecure wireless within your network, that outsiders can access.)

I think if I just wanted to allow groups within my organisation to have access at certain times and not at others, I'd be using squid for access control (and using iptables to ensure that packets have to funnel through squid, rather than be able to go around squid by fiddling with network settings).

@Ammad
Quote:
...to which i want to allow Internet...
Do you really mean that you want to block all internet access, or just HTTP/HTTPS, and FTP, for example?
 
Old 09-01-2010, 03:29 PM   #4
Ammad
Member
 
Registered: Apr 2004
Distribution: redhat 9.0, fc4, redhat as 4
Posts: 522

Original Poster
Rep: Reputation: 31
I want to block Internet for all users, except allowed list, (still 100+ users out of 1000).

also i want to block songs/video streaming. if you have any idea to stop this using iptables, please suggest.


thanks
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
iptables-save, iptables-restore, how to set up them in some script sarajevo Linux - Networking 1 03-24-2008 11:39 PM
iptables bash script to add offending ip's to temporary chain NinjaGuru Programming 1 01-08-2008 10:05 AM
My iptables script is /etc/sysconfig/iptables. How do i make this baby execute on boo ForumKid Linux - General 3 01-22-2002 07:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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