LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2005, 05:45 AM   #1
greenthing
Member
 
Registered: Jan 2005
Distribution: Suse 9.3 pro
Posts: 117

Rep: Reputation: 17
Unhappy reversing program (BASH)


ok i used a script (source seen at the end of this post) to add ip adresses to iptables but now i want to remove the instead. I have searched google and tryed commands like iptables --flush but after i do i cant connect to the internet. So what i want/need is to modify the script to instead of adding the ips of the downloaded list it should remove them, i have tried to do it my self but i failed misserabley so please help.

Code:
#!/bin/sh

#
# peerguard - Version 0.2
# Author: Brad Cable
# License: GPL Version 2
#


###  Configuration  ###


# uncomment and change this to use a HTTP proxy to retreive the list
#export http_proxy="0.0.0.0:80"

# file to download from the peerguardian database, go to http://www.methlabs.org/sync/ for possible values
pgfile="guarding.p2p"

# temporary directory to use
workdirectory="/tmp/pg"

# path to iptables
iptables="/usr/sbin/iptables"


### End of Configuration ###




########################################
###     DO NOT TOUCH THE REST!!!     ###
########################################


if [ "$UID" != "0" ]; then
	echo "You must be root to run this script.";
	exit;
fi

cleanup(){
	if [ -d "$workdirectory" ]; then
		rm -r $workdirectory
	fi
	rm -rf $pgfile
	rm -rf $pgfile.zip
}

cleanup

mkdir $workdirectory
chown -R nobody $workdirectory
chmod +w $workdirectory
cd $workdirectory

reject="-j REJECT --reject-with icmp-host-unreachable"
nob="su - nobody -c "
wget="cd $workdirectory;wget -q"
if [ ! -z "$http_proxy" ]; then
	wget="export http_proxy='${http_proxy}';$wget"
fi

echo
echo -n "Downloading PeerGuardian File: $pgfile"
$nob"$wget http://www.methlabs.org/sync/$pgfile.zip"

echo -n "... Unzipping..."
unzip $pgfile.zip &> /dev/null
echo -n " Done."
echo

for line in `cat $pgfile`; do

	iprange=`echo $line | cut -d ':' -f2`
	if [ "$iprange" == "$line" ] || [ "$iprange" == "" ]; then
		continue
	fi
	iprange=${iprange:0:${#iprange}-1}

	$iptables -A INPUT -m iprange --src-range $iprange -j DROP
	$iptables -A OUTPUT -m iprange --dst-range $iprange $reject

	echo "     Blocked: $iprange"

done


echo "Blocking Complete"
echo

cleanup

Last edited by greenthing; 02-27-2005 at 09:20 AM.
 
Old 02-27-2005, 09:46 AM   #2
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi,

Looking at "man iptables" it seems to me you need to replace the command "-A" with "-D" and iptables will look for the rule and, if it is found, will delete it.
 
Old 02-27-2005, 10:37 AM   #3
greenthing
Member
 
Registered: Jan 2005
Distribution: Suse 9.3 pro
Posts: 117

Original Poster
Rep: Reputation: 17
I tried that and all i get is this

Code:
Downloading PeerGuardian File: guarding.p2p... Unzipping... Done.
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 1.0.0.0-3.255.255.255
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.25.146-4.0.25.148
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.26.14-4.0.29.24
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.38.34-4.0.38.36
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.38.42-4.0.38.52
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.38.58-4.0.38.60
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.38.74-4.0.38.80
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.38.86-4.0.38.88
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.38.114-4.0.38.120
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.38.254-4.0.38.255
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.159.38-4.0.159.40
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.181.86-4.0.181.88
iptables: Bad rule (does a matching rule exist in that chain?)
iptables: Bad rule (does a matching rule exist in that chain?)
     Blocked: 4.0.242.66-4.0.242.80
and so on
 
Old 02-27-2005, 08:08 PM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi,
I got your first rule for a test in a machine which does not run firewall, so I could put any rule for a run:
Code:
Opiii:~# iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Opiii:~# iptables -A INPUT -m iprange --src-range 21.21.0.0-21.21.0.254 -j DROP
Opiii:~# iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere            source IP range 21.21.0.0-21.21.0.254

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Opiii:~# iptables -D INPUT -m iprange --src-range 21.21.0.0-21.21.0.253 -j DROP
iptables: Bad rule (does a matching rule exist in that chain?)
Opiii:~# iptables -D INPUT -m iprange --src-range 21.21.0.0-21.21.0.254 -j DROP
Opiii:~# iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
I did the following operations:
- list the rules, showing it's clean;
- append an iprange rule;
- list the rules to see if it was accepted;
- try to delete an non existing rule;
- try to delete an existing rule;
- list the rules again;
So, as you can see, the "bad rule message" is for rules you don't have in your firewall. When a rule is accepted, it is done quietly.
As you try to delete two rules at once, one for input and another for output, you receive 2 "bad rule" messages for each "Blocked:' message from your script.

Osvaldo.
 
  


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
reversing rrdtool graphs whysyn Linux - Software 0 06-05-2005 04:31 PM
reversing connectors on SCSI cable randyding Linux - Hardware 1 04-08-2005 05:26 PM
java, reversing digit Lifesaver Programming 5 03-03-2004 04:09 PM
reversing a number narendra_i Linux - Software 1 11-15-2003 09:25 AM
reversing videocard lackluster Linux - Hardware 1 11-16-2002 02:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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