<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>深度VPS &#187; 屏蔽IP</title>
	<atom:link href="http://www.deepvps.com/tag/%E5%B1%8F%E8%94%BDip/feed" rel="self" type="application/rss+xml" />
	<link>http://www.deepvps.com</link>
	<description>专注VPS技术，关注前端技术</description>
	<lastBuildDate>Sat, 04 Feb 2023 14:00:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>用 iptables 屏蔽来自某个国家的 IP</title>
		<link>http://www.deepvps.com/block-traffic-from-a-specific-country-using-iptables.html</link>
		<comments>http://www.deepvps.com/block-traffic-from-a-specific-country-using-iptables.html#comments</comments>
		<pubDate>Thu, 26 Aug 2010 15:20:18 +0000</pubDate>
		<dc:creator>deepvps</dc:creator>
				<category><![CDATA[VPS安全]]></category>
		<category><![CDATA[IPdeny]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[屏蔽IP]]></category>

		<guid isPermaLink="false">http://www.deepvps.com/?p=508</guid>
		<description><![CDATA[星期六我们一位客户受到攻击，我们的网络监测显示有连续6小时的巨大异常流量，我们立即联系了客户，没有得到回应，我们修改和限制了客户的 VPS，使得个别 VPS 受攻击不会对整个服务器和其他 VPS 用户造成任何影响，我们一直保持这个 VPS 为开通状态（尽管一直受攻击），攻击又持续了24小时，星期天攻击仍在继续，我们忍无可忍，但是仍然无法联系到客户，我们向客户网站的另一负责人询问是否需要我们介入来帮助解决，这位负责人答应后我们立即投入到与 DDoS 的战斗中（我们动态扫描屏蔽坏 IP，现在客户网站已恢复。整个过程很有意思，以后有时间再写一篇博客来描述）。登录到客户 VPS 第一件事情就是查当前连接和 IP，来自中国的大量 IP 不断侵占80端口，典型的 DDos. 所以第一件事是切断攻击源，既然攻击只攻80端口，那有很多办法可以切断，直接关闭网站服务器、直接用防火墙/iptables 切断80端口或者关闭所有连接、把 VPS 网络关掉、换一个 IP，&#8230;，等等。因为攻击源在国内，所以 VPSee 决定切断来自国内的所有访问，这样看上去网站好像是被墙了而不是被攻击了，有助于维护客户网站的光辉形象。那么如何屏蔽来自某个特定国家的 IP 呢？
方法很容易，先到 IPdeny 下载以国家代码编制好的 IP 地址列表，比如下载 cn.zone：
# wget http://www.ipdeny.com/ipblocks/data/countries/cn.zone
有了国家的所有 IP 地址，要想屏蔽这些 IP 就很容易了，直接写个脚本逐行读取 cn.zone 文件并加入到 iptables 中：
#!/bin/bash
# Block traffic from a specific country
# written by vpsee.com

COUNTRY = "cn"
IPTABLES = /sbin/iptables
EGREP = /bin/egrep

if [...]]]></description>
			<content:encoded><![CDATA[<p>星期六我们一位客户受到攻击，我们的网络监测显示有连续6小时的巨大异常流量，我们立即联系了客户，没有得到回应，我们修改和限制了客户的 VPS，使得个别 VPS 受攻击不会对整个服务器和其他 VPS 用户造成任何影响，我们一直保持这个 VPS 为开通状态（尽管一直受攻击），攻击又持续了24小时，星期天攻击仍在继续，我们忍无可忍，但是仍然无法联系到客户，我们向客户网站的另一负责人询问是否需要我们介入来帮助解决，这位负责人答应后我们立即投入到与 DDoS 的战斗中（我们动态扫描屏蔽坏 IP，现在客户网站已恢复。整个过程很有意思，以后有时间再写一篇博客来描述）。<span id="more-508"></span>登录到客户 VPS 第一件事情就是查当前连接和 IP，来自中国的大量 IP 不断侵占80端口，典型的 DDos. 所以第一件事是切断攻击源，既然攻击只攻80端口，那有很多办法可以切断，直接关闭网站服务器、直接用防火墙/iptables 切断80端口或者关闭所有连接、把 VPS 网络关掉、换一个 IP，&#8230;，等等。因为攻击源在国内，所以 VPSee 决定切断来自国内的所有访问，这样看上去网站好像是被墙了而不是被攻击了，有助于维护客户网站的光辉形象。那么如何屏蔽来自某个特定国家的 IP 呢？</p>
<p>方法很容易，先到 <a href="http://www.ipdeny.com/ipblocks/" target="_blank">IPdeny</a> 下载以国家代码编制好的 IP 地址列表，比如下载 cn.zone：</p>
<pre># wget http://www.ipdeny.com/ipblocks/data/countries/cn.zone</pre>
<p>有了国家的所有 IP 地址，要想屏蔽这些 IP 就很容易了，直接写个脚本逐行读取 cn.zone 文件并加入到 iptables 中：</p>
<pre>#!/bin/bash
# Block traffic from a specific country
# written by vpsee.com

COUNTRY = "cn"
IPTABLES = /sbin/iptables
EGREP = /bin/egrep

if [ "$(id -u)" != "0" ]; then
   echo "you must be root" 1&gt;&amp;2
   exit 1
fi

resetrules() {
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
$IPTABLES -X
}

resetrules

for c in $COUNTRY
do
        country_file = $c.zone

        IPS = $($EGREP -v "^#|^$" $country_file)
        for ip in $IPS
        do
           echo "blocking $ip"
           $IPTABLES -A INPUT -s $ip -j DROP
        done
done

exit 0</pre>
<p>好 IP 和坏 IP 都被屏蔽掉了，这种办法当然不高明，屏蔽 IP 也没有解决被攻击的问题，但是是解决问题的第一步，屏蔽了攻击源以后我们才有带宽、时间和心情去检查 VPS 的安全问题。公布一份我们客户被攻击的网络流量图，在18点到0点所有带宽都被攻击流量占用，这时候客户无法登录 VPS，访问者也无法访问网站：</p>
<p><a href="http://www.deepvps.com/wp-content/uploads/auto_save_image/2010/08/232018GHJ.png"><img title="ddos" src="http://www.deepvps.com/wp-content/uploads/auto_save_image/2010/08/232018GHJ.png" alt="" width="410" height="121" /></a></p>
<p>转载自：<a href="http://www.vpsee.com/2010/08/block-traffic-from-a-specific-country-using-iptables/" target="_blank">http://www.vpsee.com/2010/08/block-traffic-from-a-specific-country-using-iptables/</a></p>
<p><script src="http://china-addthis.googlecode.com/svn/trunk/addthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.deepvps.com/block-traffic-from-a-specific-country-using-iptables.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
