Chia Sẽ Kinh Nghiệm Về IT



Tìm Kiếm Với Google
-


Gởi Ðề Tài Mới  Gửi trả lời
 
Công Cụ Xếp Bài
Tuổi 04-11-2010, 10:09 AM   #1
hoctinhoc
Guest
 
Trả Lời: n/a
Block or unblock: ip, range, subnet, contries trong iptables (block ip nuoc ngoai)
Block or unblock: ip, range, subnet, contries trong iptables (block ip nuoc ngoai)



1. Block 1 dia chi IP

Following iptable rule will drop incoming connection from host/IP 202.54.20.22:


Trích dẫn:

iptables -A INPUT -s 202.54.20.22 -j DROP

iptables -A OUTPUT -d 202.54.20.22 -j DROP



2. Block day IP


a. su dung command


Trích dẫn:

# iptables -A INPUT -s 192.168.100.0/24 -j DROP


b. Edit file /etc/sysconfig/iptables


them vao

Trích dẫn:

sbin/iptables -I INPUT -s 221.0.0.0/255.0.0.0 -j DROP

sbin/iptables -I INPUT -s 221.0.0.0/8 -j DROP


3. Block tat ca ip trong danh sach chi dinh


Cach 1:

If you have lots of IP address use the following shell script:


A) Tao file:


Trích dẫn:


# vi /root/ip.blocked

Nhap vao cac dia chi IP ma ban can block:

# Ip address block file

Trích dẫn:

202.54.20.22
202.54.20.1/24
#65.66.36.87
Save file

B) Tao doan script nhu sau va them vao file iptables:


Trích dẫn:

BLOCKDB=”/root/ip.blocked”
IPS=$(grep -Ev "^#" $BLOCKDB)
for i in $IPS
do
iptables -A INPUT -s $i -j DROP
iptables -A OUTPUT -d $i -j DROP
done
C) Restart lai iptables service


# /etc/init.d/iptables restart

Cach 2:

Nếu bạn đang đối mặt với rất nhiều cuộc tấn công, tốt hơn hết bạn nên sử dụng một phương pháp tự động hơn để thêm địa chỉ IP từ danh sách cấm. Để làm việc này, ta tạo đoạn mã sau:

#!/bin/sh
for i in $(< banned_IPs.cfg) ; do
iptables -I INPUT -i eth1 -s "$i" -j DROP
done

Lưu đoạn mã trên vào một file với tên bất kì như banned_IPs.sh và gán cho nó quyền thực thi:

# chmod +x banned_IPs.sh

Bây giờ tạo một file banned_IPs.cfg và nhập vào danh sách địa chỉ IP bạn muốn khóa, mỗi cái trên một dòng:

122.174.12.228
129.122.10.23
111.154.84.130

Bây giờ chạy file banned_IPs.sh để đưa địa chỉ IP bạn muốn khóa thêm vào danh sách IP cấm trong iptables:

# ./banned_IPs.sh


Chúc Vui vẻ !



4.Unblock / Delete an IP Address Listed in IPtables Tables



List existing chains

Type the following command to list current IPs in tables:


Trích dẫn:

iptables -L -n
iptables -L -n -v
iptables -L chain-name -n -v
iptables -L spamips -n -v




List existing chains with line number


To display line number along with other information, enter:


iptables -L INPUT -n --line-numbers
iptables -L OUTPUT -n --line-numbers
iptables -L OUTPUT -n --line-numbers | less
iptables -L spamips -n -v --line-numbers
iptables -L spamips -n -v --line-numbers | grep 202.54.1.2


Trích dẫn:

Chain droplist (3 references)

num pkts bytes target prot opt in out source destination
1 0 0 LOG 0 -- * * 116.199.128.0/19 0.0.0.0/0 LOG flags 0 level 4 prefix `LASSO DROP Block'
2 0 0 DROP 0 -- * * 116.199.128.0/19 0.0.0.0/0
3 0 0 LOG 0 -- * * 116.50.8.0/21 0.0.0.0/0 LOG flags 0 level 4 prefix `LASSO DROP Block'
4 0 0 DROP 0 -- * * 116.50.8.0/21 0.0.0.0/0
5 0 0 LOG 0 -- * * 128.199.0.0/16 0.0.0.0/0 LOG flags 0 level 4 prefix `LASSO DROP Block'
6 0 0 DROP 0 -- * * 128.199.0.0/16 0.0.0.0/0
7 0 0 LOG 0 -- * * 132.232.0.0/16 0.0.0.0/0 LOG flags 0 level 4 prefix `LASSO DROP Block'
8 0 0 DROP 0 -- * * 132.232.0.0/16 0.0.0.0/0
9 342 23317 LOG 0 -- * * 134.175.0.0/16 0.0.0.0/0 LOG flags 0 level 4 prefix `LASSO DROP Block'
10 342 23317 DROP 0 -- * * 134.175.0.0/16 0.0.0.0/0
11 0 0 LOG 0 -- * * 134.33.0.0/16 0.0.0.0/0 LOG flags 0 level 4 prefix `LASSO DR
You will get the list of all blocked IP. Look at the number on the left, then use number to delete it. For example delete line number 10 (subner 134.175.0.0/16), enter:

iptables -D INPUT 10

You can also use the following syntax to delete / unblock an IP use the following syntax:

iptables -D INPUT -s xx.xxx.xx.xx -j DROP
iptables -D INPUT -s xx.xxx.xx.xx/yy -j DROP
iptables -D spamlist -s 202.54.1.2 -d 0/0 -j DROP
iptables -D spamlist -s 202.54.1.2/29 -d 0/0 -j DROP


http://www.cyberciti.biz/faq/block-e...sing-iptables/


5. Block IP nuoc ngoai

Tạo tập tin


#vi /root/country.block.iptables.sh


với nội dung như bên dưới:

Chú y: Điền mã nước mà bạn muốn chặn vào phần: ISO=

Xem mã nước:

http://www.iana.org/domains/root/db/


Trích dẫn:

#!/bin/bash
### Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code ###
ISO="af cn"

### Set PATH ###
IPT=/sbin/iptables
WGET=/usr/bin/wget
EGREP=/bin/egrep

### No editing below ###
SPAMLIST="countrydrop"
ZONEROOT="/root/iptables"
DLROOT="http://www.ipdeny.com/ipblocks/data/countries"

cleanOldRules(){
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
}

# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT

# clean old rules
cleanOldRules

# create a new iptables list
$IPT -N $SPAMLIST

for c in $ISO
do
# local zone file
tDB=$ZONEROOT/$c.zone

# get fresh zone file
$WGET -O $tDB $DLROOT/$c.zone

# country specific log message
SPAMDROPMSG="$c Country Drop"

# get
BADIPS=$(egrep -v "^#|^$" $tDB)
for ipblock in $BADIPS
do
$IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG"
$IPT -A $SPAMLIST -s $ipblock -j DROP
done
done

# Drop everything
$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST

# call your other iptable script
# /path/to/other/iptables.sh

exit 0


Lập lịch cho file này chạy một tuần 1 lần

Trích dẫn:

@weekly /root/country.block.iptables.sh

Chạy ngay không cần lập lịch



Trích dẫn:

# ./root/country.block.iptables.sh

Chúc bạn thành công!




tham khao:

Range IP của các nước

http://www.find-ip-address.org/ip-country/


http://ipinfodb.com/ip_country_block_iptables.php

http://www.cyberciti.biz/faq/block-e...sing-iptables/

http://howtoforge.com/blocking-ip-addresses-of-any-country-with-iptables


http://www.cyberciti.biz/faq/samba-r...-to-ipc-share/




  Trả lời ngay kèm theo trích dẫn này
Tuổi 30-01-2011, 03:03 AM   #2
ZergoBoch
Newbie
 
Gia nhập: Jan 2011
Trả Lời: 9
3 серая толстовка
Thanks

Last edited by hoctinhoc; 10-03-2011 at 01:31 PM..
ZergoBoch vắng mặt   Trả lời ngay kèm theo trích dẫn này
Gửi trả lời


Công Cụ
Xếp Bài

Quyền Hạn Của Bạn
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 Mở
Hình Cảm xúc đang Mở
[IMG] đang Mở
Mã HTML đang Tắt




Bây giờ là 08:23 PM. Giờ GMT +7



Diễn đàn tin học QuantriNet
quantrinet.com | quantrimang.co.cc
Founded by Trương Văn Phương | Developed by QuantriNet's members.
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.