<?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; CC攻击</title>
	<atom:link href="http://www.deepvps.com/tag/cc%E6%94%BB%E5%87%BB/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>判断CC攻击，netstat命令详解</title>
		<link>http://www.deepvps.com/determine-the-cc-attack-netstat-command-detailed.html</link>
		<comments>http://www.deepvps.com/determine-the-cc-attack-netstat-command-detailed.html#comments</comments>
		<pubDate>Sat, 04 Dec 2010 15:23:58 +0000</pubDate>
		<dc:creator>deepvps</dc:creator>
				<category><![CDATA[VPS管理维护]]></category>
		<category><![CDATA[80端口]]></category>
		<category><![CDATA[CC]]></category>
		<category><![CDATA[CC攻击]]></category>
		<category><![CDATA[netstat]]></category>
		<category><![CDATA[TCP连接]]></category>
		<category><![CDATA[命令]]></category>

		<guid isPermaLink="false">http://www.deepvps.com/?p=669</guid>
		<description><![CDATA[查看所有80端口的连接数
netstat -nat&#124;grep -i &#8220;80&#8243;&#124;wc -l
对连接的IP按连接数量进行排序
netstat -ntu &#124; awk &#8216;{print $5}&#8217; &#124; cut -d: -f1 &#124; sort &#124; uniq -c &#124; sort -n
查看TCP连接状态
netstat -nat &#124;awk &#8216;{print $6}&#8217;&#124;sort&#124;uniq -c&#124;sort -rn
netstat -n &#124; awk &#8216;/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}&#8217;
netstat -n &#124; awk &#8216;/^tcp/ {++state[$NF]}; END {for(key in state) print key,&#8221;\t&#8221;,state[key]}&#8217;
netstat -n &#124; awk &#8216;/^tcp/ {++arr[$NF]};END [...]]]></description>
			<content:encoded><![CDATA[<h3>查看所有80端口的连接数</h3>
<blockquote><p>netstat -nat|grep -i &#8220;80&#8243;|wc -l</p></blockquote>
<h3>对连接的IP按连接数量进行排序</h3>
<blockquote><p>netstat -ntu | awk &#8216;{print $5}&#8217; | cut -d: -f1 | sort | uniq -c | sort -n</p></blockquote>
<h3>查看TCP连接状态</h3>
<blockquote><p>netstat -nat |awk &#8216;{print $6}&#8217;|sort|uniq -c|sort -rn<br />
netstat -n | awk &#8216;/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}&#8217;<br />
netstat -n | awk &#8216;/^tcp/ {++state[$NF]}; END {for(key in state) print key,&#8221;\t&#8221;,state[key]}&#8217;<br />
netstat -n | awk &#8216;/^tcp/ {++arr[$NF]};END {for(k in arr) print k,&#8221;\t&#8221;,arr[k]}&#8217;<br />
netstat -n |awk &#8216;/^tcp/ {print $NF}&#8217;|sort|uniq -c|sort -rn<br />
netstat -ant | awk &#8216;{print $NF}&#8217; | grep -v &#8216;[a-z]&#8216; | sort | uniq -c</p></blockquote>
<p><span id="more-669"></span></p>
<h3>查看80端口连接数最多的20个IP</h3>
<blockquote><p>netstat -anlp|grep 80|grep tcp|awk &#8216;{print $5}&#8217;|awk -F: &#8216;{print $1}&#8217;|sort|uniq -c|sort -nr|head -n20<br />
netstat -ant |awk &#8216;/:80/{split($5,ip,&#8221;:&#8221;);++A[ip[1]]}END{for(i in A) print A,i}&#8217; |sort -rn|head -n20</p></blockquote>
<h3>用tcpdump嗅探80端口的访问看看谁最高</h3>
<blockquote><p>tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F&#8221;.&#8221; &#8216;{print $1&#8243;.&#8221;$2&#8243;.&#8221;$3&#8243;.&#8221;$4}&#8217; | sort | uniq -c | sort -nr |head -20</p></blockquote>
<h3>查找较多time_wait连接</h3>
<blockquote><p>netstat -n|grep TIME_WAIT|awk &#8216;{print $5}&#8217;|sort|uniq -c|sort -rn|head -n20</p></blockquote>
<h3>查找较多的SYN连接</h3>
<blockquote><p>netstat -an | grep SYN | awk &#8216;{print $5}&#8217; | awk -F: &#8216;{print $1}&#8217; | sort | uniq -c | sort -nr | more</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.deepvps.com/determine-the-cc-attack-netstat-command-detailed.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Linux VPS下简单解决CC攻击</title>
		<link>http://www.deepvps.com/linux-vps-cc-attack-solution-methods.html</link>
		<comments>http://www.deepvps.com/linux-vps-cc-attack-solution-methods.html#comments</comments>
		<pubDate>Tue, 08 Jun 2010 07:01:23 +0000</pubDate>
		<dc:creator>deepvps</dc:creator>
				<category><![CDATA[VPS安全]]></category>
		<category><![CDATA[CC]]></category>
		<category><![CDATA[CC攻击]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[VPS]]></category>

		<guid isPermaLink="false">http://www.deepvps.com/?p=457</guid>
		<description><![CDATA[一，准备工作
1，登录进VPS控制面板，准备好随时重启VPS。
2，关闭Web Server先，过高的负载会导致后面的操作很难进行，甚至直接无法登录SSH。
3，以防万一，把设置的Web Server系统启动后自动运行去掉。
（如果已经无法登录进系统，并且重启后负载过高导致刚刚开机就已经无法登录，可联系管理员在母机上封掉VPS的IP或80端口，在母机上用虚拟控制台登录进系统，然后进行2&#38;3的操作，之后解封）
二，找出攻击者IP
1，在网站根目录建立文件ip.php，写入下面的内容。
&#60;?php
$real_ip = getenv(&#8216;HTTP_X_FORWARDED_FOR&#8217;);
if(isset($real_ip)){
shell_exec(&#8220;echo $real_ip &#62;&#62; real_ip.txt&#8221;);
shell_exec(&#8220;echo $_SERVER['REMOTE_ADDR'] &#62;&#62; proxy.txt&#8221;);
}else{
shell_exec(&#8220;echo $_SERVER['REMOTE_ADDR'] &#62;&#62; ips.txt&#8221;);
}
echo &#8216;服务器受到攻击，正在收集攻击源，请在5分钟后访问本站，5分钟内多次访问本站有可能会被当作攻击源封掉IP。谢谢合作！&#8217;;
?&#62;
2，设置伪静态，将网站下的所有访问都rewrite到ip.php。
Nginx规则：
rewrite (.*) /ip.php;
Lighttpd规则：
url.rewrite = (
&#8220;^/(.+)/?$&#8221; =&#62; &#8220;/ip.php&#8221;
)
3，启动Web Server开始收集IP
进行完1和2的设置后，启动Web Server，开始记录IP信息。
收集时间建议为3到5分钟，然后再次关闭Web Server。
real_ip.txt，这个文件中保存的IP有80%以上都相同的，这个IP就是攻击者实施攻击的平台的IP。
proxy.txt，这个文件中保存的是攻击者调用的代理服务器的IP，需要封掉。
ips.txt，这里记录的是未表现出代理服务器特征的IP，根据访问次数判断是否为攻击源。
三，对上一段的补充
如果VPS上启用了WEB日志，可以查看日志文件的增长速度来判断是哪个站点被攻击。
如果没有启用日志，并且站点数量很少，临时启用日志也很方便 。
如果没有启用日志，并且站点数量过多，可以使用临时的Web Server配置文件，不绑定虚拟主机，设置一个默认的站点。然后在ip.php里加入下面一行
shell_exec(“echo $_SERVER['HTTP_HOST'] &#62;&#62; domain.txt”);
四，开始封堵IP
建立文件ban.php
$num){
if($num &#62; $threshold){
$ip = trim($ip);
$cmd = &#8220;iptables -I INPUT -p tcp &#8211;dport 80 -s $ip -j DROP&#8221;;
shell_exec($cmd);
echo &#8220;$ip baned!\n&#8221;;
$ban_num ++;
}
}
$proxy_arr = array_unique(file(&#8216;ips.txt&#8217;));
foreach($proxy_arr as $proxy){
$proxy = trim($proxy);
$cmd = [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="font-size: small;">一，准备工作</span></strong><br />
1，登录进VPS控制面板，准备好随时重启VPS。<br />
2，关闭Web Server先，过高的负载会导致后面的操作很难进行，甚至直接无法登录SSH。<br />
3，以防万一，把设置的Web Server系统启动后自动运行去掉。<br />
（如果已经无法登录进系统，并且重启后负载过高导致刚刚开机就已经无法登录，可联系管理员在母机上封掉VPS的IP或80端口，在母机上用虚拟控制台登录进系统，然后进行2&amp;3的操作，之后解封）</p>
<p><strong><span style="font-size: small;"><span id="more-457"></span>二，找出攻击者IP</span></strong><br />
1，在网站根目录建立文件ip.php，写入下面的内容。</p>
<blockquote><p>&lt;?php<br />
$real_ip = getenv(&#8216;HTTP_X_FORWARDED_FOR&#8217;);<br />
if(isset($real_ip)){<br />
shell_exec(&#8220;echo $real_ip &gt;&gt; real_ip.txt&#8221;);<br />
shell_exec(&#8220;echo $_SERVER['REMOTE_ADDR'] &gt;&gt; proxy.txt&#8221;);<br />
}else{<br />
shell_exec(&#8220;echo $_SERVER['REMOTE_ADDR'] &gt;&gt; ips.txt&#8221;);<br />
}</p>
<p>echo &#8216;服务器受到攻击，正在收集攻击源，请在5分钟后访问本站，5分钟内多次访问本站有可能会被当作攻击源封掉IP。谢谢合作！&#8217;;<br />
?&gt;</p></blockquote>
<p>2，设置伪静态，将网站下的所有访问都rewrite到ip.php。<br />
Nginx规则：</p>
<blockquote><p>rewrite (.*) /ip.php;</p></blockquote>
<p>Lighttpd规则：</p>
<blockquote><p>url.rewrite = (<br />
&#8220;^/(.+)/?$&#8221; =&gt; &#8220;/ip.php&#8221;<br />
)</p></blockquote>
<p>3，启动Web Server开始收集IP<br />
进行完1和2的设置后，启动Web Server，开始记录IP信息。<br />
收集时间建议为3到5分钟，然后再次关闭Web Server。<br />
real_ip.txt，这个文件中保存的IP有80%以上都相同的，这个IP就是攻击者实施攻击的平台的IP。<br />
proxy.txt，这个文件中保存的是攻击者调用的代理服务器的IP，需要封掉。<br />
ips.txt，这里记录的是未表现出代理服务器特征的IP，根据访问次数判断是否为攻击源。</p>
<p><strong><span style="font-size: small;">三，对上一段的补充</span></strong><br />
如果VPS上启用了WEB日志，可以查看日志文件的增长速度来判断是哪个站点被攻击。<br />
如果没有启用日志，并且站点数量很少，临时启用日志也很方便 。<br />
如果没有启用日志，并且站点数量过多，可以使用临时的Web Server配置文件，不绑定虚拟主机，设置一个默认的站点。然后在ip.php里加入下面一行</p>
<blockquote><p>shell_exec(“echo $_SERVER['HTTP_HOST'] &gt;&gt; domain.txt”);</p></blockquote>
<p><strong><span style="font-size: small;">四，开始封堵IP</span></strong><br />
建立文件ban.php</p>
<blockquote><p><!--threshold = 10; $ips = array_count_values(file('ips.txt')); $ban_num = 0; foreach($ips as $i-->$num){<br />
if($num &gt; $threshold){<br />
$ip = trim($ip);<br />
$cmd = &#8220;iptables -I INPUT -p tcp &#8211;dport 80 -s $ip -j DROP&#8221;;<br />
shell_exec($cmd);<br />
echo &#8220;$ip baned!\n&#8221;;<br />
$ban_num ++;<br />
}<br />
}</p>
<p>$proxy_arr = array_unique(file(&#8216;ips.txt&#8217;));<br />
foreach($proxy_arr as $proxy){<br />
$proxy = trim($proxy);<br />
$cmd = &#8220;iptables -I INPUT -p tcp &#8211;dport 80 -s $ip -j DROP&#8221;;<br />
shell_exec($cmd);<br />
echo &#8220;$ip baned!\n&#8221;;<br />
$ban_num ++;<br />
}</p>
<p>echo &#8220;total: $ban_num ips\n&#8221;;<br />
?&gt;</p></blockquote>
<p>用下面的命令执行脚本（确保php命令在PATH中）</p>
<blockquote><p>php ban.php</p></blockquote>
<p>这个脚本依赖于第二段中ips.txt里保存的结果，当其中记录的IP访问次数超过10次，就被当作攻击源给屏蔽掉。如果是代理服务器，则不判断次数直接封掉。<br />
封完IP之后，把所有的网站设置恢复正常，站点可以继续正常运行了。</p>
<p><strong><span style="font-size: small;">五，一些细节</span></strong><br />
为保持对操作过程的描述尽量简洁，没有在上面的内容中加入过多的解释，留在这段统一讲述。<br />
1，关于“代理服务器”的一些本质<br />
两个与TCP&amp;HTTP协议相关的值，REMOTE_ADDR和HTTP_X_FORWARDED_FOR。<br />
（1）REMOTE_ADDR总是取离Web服务器最接近的一台主机的IP，如果没有使用代理，这个值就是访问者本身的IP，如果使用了代理，这个值就是代理服务器的IP，如果通过多个代理服务器进行的连接，这个值就是到达Web服务器前最后一台代理服务器的IP。<br />
REMOTE_ADDR是由TCP/IP层决定的，不能修改不能伪造。<br />
（2）HTTP_X_FORWARDED_FOR，因为这个值是属于HTTP部分，而不是TCP/IP，所以这个值不管是什么，都不影响数据的传输。事实上，一般情况下，如果是访问者直接访问Web服务器，这个值为空；通过透明代理的时候，这个值会被代理服务器设置为访问者的IP；通过匿名代理连接时，这个值可能为代理服务器的IP也可能是空的也有可能是随机的。<br />
HTTP_X_FORWARDED_FOR可以被任意修改。大多数代理服务器都是透明代理，也就是说，会把这个值设置为最原始访问者的IP。</p>
<p>2，关于解决CC攻击的层面问题<br />
按处理效率从高到低排列。<br />
（由于本文是针对VPS服务器所写，而VPS简单来说就是服务器的低端替代品，内存和CPU等资源普遍偏低，当然是处理效率越高越好。）<br />
（1）网络传输层。也就是本文所用的iptables，这个工具本身是工作于系统内核，在建立网络连接时直接把攻击者的连接给否了。在这一层面上将攻击源处理掉后，消耗掉的资源几乎可以忽略不计。<br />
（2）Web Server层，大多数Web Server都可以设置禁止访问的IP。在这一层上解决的意义和上面的差不多，但是效率要差些。<br />
（3）脚本层，从脚本程序上制定适合于本身的策略过滤掉攻击源。网络上有很多流传的在这一层面的解决方案，但是不太适用于VPS，而且设置难度可能要增加几倍或者几十倍。</p>
<p>3，为什么不是从日志收集IP？<br />
主要是考虑两点，一是大多数VPS使用者都因为硬盘空间过小，经常清除日志很麻烦，而直接禁止了日志。<br />
二是如果从日志收集IP，脚本复杂程度要高很多，而且可能要根据情况做些调整，考虑到将要读到本文的人大多数都未必掌握更多的技术，本文的目的就是按部就班的依本文进行操作，即可解决问题。</p>
<p><strong><span style="font-size: small;">六，其他</span></strong><br />
本文版权归<a href="http://www.diahosting.com/" target="_blank">www.diahosting.com</a>所有，转载请保留超链接。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deepvps.com/linux-vps-cc-attack-solution-methods.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
