November 22, 2024, 03:29:45 PM

News : LinuxSolved.com Linux Help Community Forum..


Author Topic: How to block certain IPs and Website on Iptables  (Read 27696 times)

Offline ronaldjayr_jover

  • Linux Noob !
  • *
  • Posts: 29
How to block certain IPs and Website on Iptables
« on: August 17, 2005, 01:27:02 AM »
Good day!!!

I have a proxy server, 2 lancard. 1 Lancard is connected to a Public IP DSL and one on our Private Network. I want to an IP add or a website on the 1st Lancard(Public) and at the same time block from the 2nd Lancard(Private network) that IP add or Website.

Thanks... :)

Offline Ricky

  • LST CareTaker
  • Specially Skilled
  • *****
  • Posts: 2381
How to block certain IPs and Website on Iptables
« Reply #1 on: August 17, 2005, 10:33:45 AM »
hmm.. that is so simple , infact I have answered that so many times already.

iptables -A FORWARD -d domain.com -j DROP

and ..

iptables -I INPUT -s XXX.XXX.XXX.XXX -j DROP

Offline ronaldjayr_jover

  • Linux Noob !
  • *
  • Posts: 29
How to block certain IPs and Website on Iptables
« Reply #2 on: August 18, 2005, 12:18:12 AM »
Hi Ricky,

 Thanks for the reply, gladly appreciate it.

  :D

Offline veno

  • New Member
  • Posts: 3
How to block certain IPs and Website on Iptables
« Reply #3 on: September 03, 2005, 04:10:04 PM »
Quote from: "Ricky"
hmm.. that is so simple , infact I have answered that so many times already.

iptables -A FORWARD -d domain.com -j DROP

and ..

iptables -I INPUT -s XXX.XXX.XXX.XXX -j DROP


ok how do i release the ip that i firewall?

Offline Ricky

  • LST CareTaker
  • Specially Skilled
  • *****
  • Posts: 2381
How to block certain IPs and Website on Iptables
« Reply #4 on: September 04, 2005, 07:24:07 AM »
you can add them in rc.local file so that it get executed everytime you start your computer.

Offline veno

  • New Member
  • Posts: 3
How to block certain IPs and Website on Iptables
« Reply #5 on: September 04, 2005, 01:31:54 PM »
Quote from: "Ricky"
you can add them in rc.local file so that it get executed everytime you start your computer.

yes i know that what i'm tryign to do is block the master server from quake 3 arena for 3,5 hours and open it for 30 mins and close it again so what i tried is this in crontab

* */3 * * * /sbin/iptables -D OUTPUT 1
30 */3 * * * /sbin/iptables -A OUTPUT -p tcp -d 192.246.40.56 --destination-port 27950 -j REJECT

not sure if that will work but maybe u can tell me or someone

Offline Ricky

  • LST CareTaker
  • Specially Skilled
  • *****
  • Posts: 2381
How to block certain IPs and Website on Iptables
« Reply #6 on: September 06, 2005, 11:24:36 AM »
rather executing them directly you should made two or more script files. Say Ist one is the one who blocks the quake servers and IInd one is the one who allow the quake server.

then execute the IInd file when you want it to be open and Ist when you want it to be block.

BTW.. I don't get that when you want to block and when to allow the access so that I can correct your cronjob if its wrong.

Offline Kal

  • Linux Noob !
  • *
  • Posts: 6
How to block certain IPs and Website on Iptables
« Reply #7 on: September 10, 2006, 02:06:04 AM »
I have been trying to create a similar script for my quake3 server but with not much luck.  Is there anyone out there who has successfully created such a script?  Please let me know!

Offline Ricky

  • LST CareTaker
  • Specially Skilled
  • *****
  • Posts: 2381
How to block certain IPs and Website on Iptables
« Reply #8 on: September 10, 2006, 11:59:55 AM »
As I suggested earlier you can make two files ie script, one is to block and other is to release then execute those scripts using the cron job .

Offline Kal

  • Linux Noob !
  • *
  • Posts: 6
How to block certain IPs and Website on Iptables
« Reply #9 on: September 10, 2006, 09:30:57 PM »
Code: [Select]

if [ "$1" = "-u" ]
then
        iptables -D OUTPUT -d 192.246.40.56 -j REJECT
else
        iptables -A OUTPUT -d 192.246.40.56 -j REJECT
fi


Cron Job
Code: [Select]
30 0,4,8,12,16,20 * * * /path/to/q3block
0 0,4,8,12,16,20 * * * /path/to/q3block -u


I found this code on a forum and was wondering if this is what you were talking about.  I am unfortunately a newbie when it comes to writting shell scripts.  Would this code work for hiding the server 3.5 hours then releasing it for 30 minutes?  Thanks for any help you can provide.

Offline Ricky

  • LST CareTaker
  • Specially Skilled
  • *****
  • Posts: 2381
How to block certain IPs and Website on Iptables
« Reply #10 on: September 11, 2006, 07:19:38 PM »
just simple file containing command to block the server using iptables and other simple file to unblock the server and make them executables by giving execute permission . Then using cron run them accordingly. !!!
Read little about blocking a IP in iptables.

Offline Kal

  • Linux Noob !
  • *
  • Posts: 6
How to block certain IPs and Website on Iptables
« Reply #11 on: September 11, 2006, 11:33:09 PM »
Ok I just want to say thanks so far for the help you have provided.  I did some research on blocking/allowing ip addresses in iptables and how crontabs work.  Here is the code I came up with.  BTW: The forums don't like it when I put in shell script code so I have left it out.

block.sh
Code: [Select]


iptables -p all -d 192.246.40.56 -j DROP


allow.sh
Code: [Select]


iptables -p all -d 192.246.40.56 -j ACCEPT


crontab
Code: [Select]


Block 192.246.40.56 heartbeats
0 0,4,8,12,16,20 * * * /path/to/block.sh
Allow 192.246.40.56 heartbeats
30 3,30 7,30 11,30 15,30 19,30 23 * * * /path/to/allow.sh


I am not sure if I setup the crontab correctly for the release/block times.  Here is the schedule for blocking/releasing.

Block 12 am 4 am 8 pm 12 pm 4pm 8 pm
Release 3:30 am, 7:30 am, 11:30 am, 3:30 pm, 7:30 pm, 11:30 pm

Please let me know if I am doing this correctly.  Thanks for any help!

Offline Ricky

  • LST CareTaker
  • Specially Skilled
  • *****
  • Posts: 2381
How to block certain IPs and Website on Iptables
« Reply #12 on: September 12, 2006, 03:37:44 PM »
so far its alright but I want to let you know that I need to read cron docs to confirm it though you can test it.

Offline Kal

  • Linux Noob !
  • *
  • Posts: 6
How to block certain IPs and Website on Iptables
« Reply #13 on: September 12, 2006, 05:36:37 PM »
8)

Offline gauravbajaj

  • LST CareTaker
  • Experienced
  • *****
  • Posts: 658
How to block certain IPs and Website on Iptables
« Reply #14 on: September 13, 2006, 02:03:44 AM »
I think the last line should be like this:

30 3,7,11,15,19,23 * * * /path/to/allow.sh

Gaurav