linux服务器配合php和cron自动实现封锁攻击者ip
我已经好久没有写点东西了,主要是因为最近实在是太忙了,有个白痴天天用webbench攻击公司的服务器.
俗话说得好,久病成医,今天我这个蒙古大夫就分享一下自动防攻击的部分.
首先我来说一下这个被攻击的网站的大概部署状况.这个网站主要是nginx+mysql+php,有两台服务器分别放了web和数据库,web只对外开启了80端口操作系统是centos,而数据库服务器则在内网,攻击者的手段其实很简单,用webbench网站压力测试工具发送大量的请求到服务器,之前的时候发送每一个请求之后数据库就会相应,然后读取内容最终显示,造成数据库和web之间大量的交换数据,甚至导致mysql达到连接数上限,请求被拒绝,而且攻击者时间挺多的,他不停地换浮动ip,因此直接用防火墙封锁ip没意义.
刚开始我的做法是,用php取得攻击者的agent头,判断是不是webbench来访,如果是就die掉,不在请求数据库,确实挺有效,数据库不会再超出限制了,但是对方频繁的发送请求过来,导致网络带宽被严重消耗,看来得想想其他办法,最终想到了一个解决方法且实际测试发现可行,因此分享给大家,其实我的做法原理很简单:用php取得用户agent头判断是否是webbench来源,如果是的话就在服务器上写一个shell档案,这个档案的内容就是封锁IP的规则,然后再用chmod函数修改一下这个档案让其可执行,再用cron服务读取这个档案执行,把ip封锁掉,整个过程全部自动化完成不需要人为干预,另外在封锁的时候给我发一封email通知我有个倒霉蛋被干掉了,这样就行了.
具体实现代码如下:
IF(isSet($_SERVER['HTTP_USER_AGENT']) And Trim($_SERVER['HTTP_USER_AGENT'])!='') { $_SERVER['HTTP_USER_AGENT']=StrToLower($_SERVER['HTTP_USER_AGENT']); IF(StriStr($_SERVER['HTTP_USER_AGENT'],'webbench')!==False) { $p='/home/www/webbench.sh'; $_SERVER['REMOTE_ADDR']=isSet($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknow'; File_Put_Contents($p,"#!/bin/bash\niptables -I INPUT -s {$_SERVER['REMOTE_ADDR']} -j DROP;\n",LOCK_EX); Chmod($p,0755); chown($p,'www'); Function sMail($to,$tit,$msg) { IF(Filter_var($to,FILTER_VALIDATE_EMAIL)==''){ throw new Exception('??地址??!'); } $tit='=?UTF-8?B?'.Base64_Encode($tit).'?='; $msg = str_replace("\n.","\n..",$msg); //Windows如果在一行开头发现一个句号则会被删掉,要避免此问题将单个句号替换成两个句号 Return Mail($to,$tit,$msg,'From:No-reply@adm.bossadm.com.tw'."\n".'Content-Type:text/html;charset=utf-8'); } sMail('see7di@gmail.com','【WebBench又?始了-by http://www.7di.net】!',date('Y-m-d H:i:s',time())." {$_SERVER['REMOTE_ADDR']}"); Header('Location:http://127.0.0.1'); Die(); }}
後?我又做了一次?整,把?email的部份?入了shell文件?,不再用php?email,因?那?灌爆你的信箱,把上?的代?修改成:
IF(isSet($_SERVER['HTTP_USER_AGENT']) And Trim($_SERVER['HTTP_USER_AGENT'])!='') { $_SERVER['HTTP_USER_AGENT']=StrToLower($_SERVER['HTTP_USER_AGENT']); IF(StriStr($_SERVER['HTTP_USER_AGENT'],'webbench')!==False) { $p='/home/www/webbench.sh'; $_SERVER['REMOTE_ADDR']=isSet($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknow'; File_Put_Contents($p,"#!/bin/bash\niptables -I INPUT -s {$_SERVER['REMOTE_ADDR']} -j DROP;\necho \"{$_SERVER['REMOTE_ADDR']} - `date`\" | mail -s \"WebBench-www.7di.net\" see7di@gmail.com\n",LOCK_EX); Chmod($p,0755); chown($p,'www'); Header('Location:http://127.0.0.1'); Die(); }}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
