LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-22-2005, 06:38 AM   #1
tantric
LQ Newbie
 
Registered: Oct 2003
Location: Hampshire, UK
Distribution: SuSE 8.2 RH9
Posts: 20

Rep: Reputation: 0
Iptables syntax puzzler


Hi

I have been trying to ban IP addresses from port 25 using iptables.

To check it worked okay, I thought I would try and ban yahoo and gmail, as I could test these okay.

Unfortnately the emails seem to be getting through.

I know that yahoo and gmail use lots of mail servers, but I thought I banned a range and also I looked in my headers of emails that got through and saw IP addresses that were listed in etc/sysconfig/iptables.

Here are some examples of the syntax I have tried.


-A RH-Lokkit-0-50-INPUT -p tcp -m tcp -s 217.12.10.188 --dport 25 -j REJECT

also

-A INPUT -i eth0 -p tcp -s 217.12.10.0/20 --destination-port 25 -j REJECT

and

-A INPUT -i eth0 -p tcp -s 217.12.10.171 --destination-port 25 -j REJECT

and finally

-A INPUT -i eth0 -p tcp -s wproxy.gmail.com --destination-port 25 -j REJECT

I have also tried DROP as well as REJECT. I have read as much as my brain can take on iptables and was wondering if anyone could spot a glaring error I have made.

The linux distribution I am using is RH9 and this seems to use a script called LOKKIT to set-up iptables. I am directly editing etc/sysconfig/iptables. I know that the rules are working as iptables -L lists all of them for me.

Oh and I am restarting the iptables with service iptables restart.

what am I doing wrong??!!

Many Thanks

Tantric
 
Old 03-22-2005, 06:52 AM   #2
Capt_Caveman
Senior Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 3,658

Rep: Reputation: 69
Could be a problem with rule order. If there are any other rules preceding that rule, they may accept the packet before your reject rule. It might help if you posted your full iptables rules (with any public IP addresses removed).
 
Old 03-22-2005, 07:01 AM   #3
tantric
LQ Newbie
 
Registered: Oct 2003
Location: Hampshire, UK
Distribution: SuSE 8.2 RH9
Posts: 20

Original Poster
Rep: Reputation: 0
Hi

Thanks for the swift reply, you pointed me in the right direction

I hashed out the line

-A RH-Lokkit-0-50-INPUT -i eth0 -j ACCEPT

and this seemed to do the trick

I can only think that it was allowing everything on eth0 so that was open all the time. Therfore the entries after were having no affect.

Many Thanks for your help

Tantric

Last edited by tantric; 03-22-2005 at 07:17 AM.
 
Old 03-22-2005, 07:21 AM   #4
Capt_Caveman
Senior Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 3,658

Rep: Reputation: 69
Yeah, looks like it. There are actually several rules that would match smtp packets before your rules would even apply. Do the following:

At the command line enter your rules like this:
iptables -I INPUT -i eth0 -p tcp -s xx.xx.xx.xx --destination-port 25 -j DROP

Notice that there is an 'I' before the words "iptables" and "INPUT". This will insert the rule at the very beginning of the firewall rather than append it to the bottom (which is what the -A does). Once you've entered all your rules like that, then do:
service iptables save

which will save your rules to /etc/sysconfig/iptables
 
Old 03-22-2005, 07:28 AM   #5
Capt_Caveman
Senior Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 3,658

Rep: Reputation: 69
Quote:
Originally posted by tantric
Hi
Thanks for the swift reply, you pointed me in the right direction
I hashed out the line
-A RH-Lokkit-0-50-INPUT -i eth0 -j ACCEPT
and this seemed to do the trick
That was the one I was thinking of . Was that rule included as part of the lokkit rules or did you add that on your own? It made me curious 'cause it basically disabled all the other rules that followed as well.
 
Old 03-22-2005, 05:22 PM   #6
tantric
LQ Newbie
 
Registered: Oct 2003
Location: Hampshire, UK
Distribution: SuSE 8.2 RH9
Posts: 20

Original Poster
Rep: Reputation: 0
Hi

-A RH-Lokkit-0-50-INPUT -i eth0 -j ACCEPT

That was part of the lokkit rules.

Further complication!!

I found tonight that I couldnt see any of my server websites, send mail or use ssh!! Aaargh! Luckily a phone call to a guy who houses the server and he removed the hashed out entries.

I guess I may put my rules before the lokkit ones now to see how that goes

Then use the accept on eth0 at the end.

I have also written a php page that allows me to build a Iptables line when I enter an IP address after the page URL ?ip=1.1.1.1 or whatever. It then appends it to a file in my httdocs directory.

Is there any way I can write a bash script to pull the lines out of the file and insert them into Iptables. I could then run it regular as a cron job. Guess a crash course in bash scripting is called for!!

Its all a learning thing!!

Thanks

Tantric

Last edited by tantric; 03-22-2005 at 05:25 PM.
 
Old 03-22-2005, 06:46 PM   #7
Capt_Caveman
Senior Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 3,658

Rep: Reputation: 69
If you list all the services that you need and a basic description of your network, I'm sure we can come up with a basic firewall that is better than the lokkit one you posted earlier.

Is there any way I can write a bash script to pull the lines out of the file and insert them into Iptables. I could then run it regular as a cron job. Guess a crash course in bash scripting is called for!!

Yup. If you have the IP addresses listed one after another in a single text file, then it's pretty simple (famous last words ) to have a script run by cron that can update iptables with new addresses. Here's an example:

Code:
#!/bin/bash
FILE=`cat /path/to/blocklist/file`

for i in $FILE
do
/sbin/iptables -A INPUT -s $i -j DROP
done
Though you'd probably want to do a check to see if an IP was already added as well.

--edit--
To be honest, you should be very carefull about how you implement the PHP part of this. Having PHP interact with a file that's then executed by cron running under root is a dangerous proposition and could be abused in a number of ways if an attacker had access or new how it worked. Could explain what the PHP part does? Is this just a web interface for adding IPs to iptables? If so, there are probably a lot more secure ways.

Last edited by Capt_Caveman; 03-22-2005 at 06:51 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong with this iptables syntax?? registering Linux - Security 3 06-16-2004 03:54 PM
iptables syntax question Poetics Linux - Security 4 12-24-2003 03:32 PM
iptables syntax Ge64 Linux - Security 3 08-20-2003 10:56 PM
iptables SYNTAX jrgalan Linux - Security 2 07-31-2003 12:54 AM
ipaddress syntax - iptables arobinson74 Linux - Networking 3 03-31-2003 12:34 PM

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

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