短信接口盗刷轰炸:指的是黑客利用非法手段获取短信接口的访问权限,然后使用该接口发送大量垃圾短信给目标用户
短信验证码轰炸解决方案一(验证码类解决)-CSDN博客
短信验证码轰炸解决方案二(防止海外ip、限制ip、限制手机号次数解决)-CSDN博客
PHP短信接口防刷防轰炸多重解决方案如下:
下面用到检查ip方法key可以需要去地图官网获取
腾讯位置服务 - 立足生态,连接未来
这段代码是一个名为SecurityCheck的类,用于进行安全检查。该类包含了多个私有属性和方法,用于对用户的手机号和IP地址进行检查。
该类的主要方法是checkAll(),它接收一个手机号作为参数,并依次调用其他的私有方法来进行不同的检查。checkAll()方法返回一个JSON格式的结果,包含了每个检查的状态和消息,以及整体的状态。
具体的检查步骤如下:
- 1. 防止Linux服务器访问:检查User-Agent头部,如果包含"Linux"字样,表示是Linux服务器访问,视为非法访问。
- 2. 手机与IP黑名单:检查手机号和IP地址是否在黑名单中,如果在黑名单中,则视为非法访问。
- 3. 手机与IP限制次数:检查手机号和IP地址的访问次数是否超过设定的限制次数,如果超过限制次数,则视为访问太频繁。
- 4. 防止国外IP:检查IP地址是否为国内IP,如果不是国内IP,则视为非法访问。
- 5.做好错误日志记录
- 6、补充短信服务商后台限制每天发送次数
session_start();
class SecurityCheck {private $backTel=['18888888888'];//黑名单手机号列表private $backIp=['127.0.0.0'];//黑名单ip列表private $sysMsg=[]; //异常信息,系统内部日志private $logPath='log/'; //日志目录private $limitIP = 10; //限制ip次数private $limitTel = 10; //限制手机号次数public function checkAll($tel) {$this->sysMsg[]='校验手机:'.$tel.' ip:'.$this->getIp();$result = [];//1、防止linux服务器访问$result['checkUserAgent'] = $this->checkUserAgent();//2、手机与ip黑名单$result['checkBlacklist'] = $this->checkBlacklist($tel);//3、手机与ip限制次数$result['checkLimit'] = $this->checkLimit();//4、防止国外ip$result['checkOverseasIP'] = $this->checkOverseasIP();$overallStatus = 1;foreach ($result as $checkResult) {if ($checkResult['status'] === 0) {$overallStatus =0;break;}}if($overallStatus==1){$this->sysMsg[]="校验成功";}else{$this->sysMsg[]="校验失败";}$this->w_log( $this->sysMsg);$result['overallStatus'] = $overallStatus;return json_encode($result);}private function checkUserAgent() {$userAgent = $_SERVER['HTTP_USER_AGENT'];if (strpos($userAgent, 'Linux') !== false) {$this->sysMsg[]='非法Linux 服务器访问';return ['status' => 0, 'message' => '非法访问'];}return ['status' =>1, 'message' => '成功'];}private function checkBlacklist($tel) {$result = ['status' => 1, 'message' => '成功'];$ip = $this->getIp();$backIp = $this->backIp;if (in_array($ip, $backIp)) {$this->sysMsg[]=$ip.'非法ip服务器访问';$result['status'] = 0;$result['message'] = '非法访问';}$backTel = $this->backTel;if (in_array($tel, $backTel)) {$this->sysMsg[]=$tel.'非法手机号访问';$result['status'] = 0;$result['message'] = '非法访问!';}return $result;}private function checkLimit() {$result = ['status' =>1, 'message' => '成功'];$limitIP = $this->limitIP;if ($_SESSION['ipNum'] && $_SESSION['ipNum'] > $limitIP-1) {$this->sysMsg[]='ip访问太频繁';$result['status'] =0;$result['message'] = '访问太频繁!';}$limitTel = $this->limitTel;if ($_SESSION['telNum'] && $_SESSION['telNum'] > $limitTel-1) {$this->sysMsg[]='手机号访问太频繁';$result['status'] = 0;$result['message'] = '访问太频繁!!';}return $result;}private function checkOverseasIP() {$ip = $this->getIp();if (!$this->isChinaIP($ip)) {$this->sysMsg[]='非国内ip';return ['status' => 0, 'message' => '访问太频繁!!!'];}return ['status' => 1, 'message' => '成功'];}private function getIp() {static $ip = '';if (isset($_SERVER['REMOTE_ADDR'])) {$ip = $_SERVER['REMOTE_ADDR'];}if (isset($_SERVER['HTTP_CDN_SRC_IP'])) {$ip = $_SERVER['HTTP_CDN_SRC_IP'];} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {$ip = $_SERVER['HTTP_CLIENT_IP'];} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {foreach ($matches[0] as $xip) {if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {$ip = $xip;break;}}}if (preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $ip)) {return $ip;} else {return '127.0.0.1';}}private function isChinaIP($ip) {$key = 'PHYBZ-UOXWV-LUIPZ-DSADSA-SDSDD-ADSADA';$url = "https://apis.map.qq.com/ws/location/v1/ip?ip=" . $ip . "&key=" . $key;$res = file_get_contents($url);if ($res) {$data = json_decode($res, true);if (isset($data['result']['ad_info']['nation']) && $data['result']['ad_info']['nation'] != "中国") {return false;}}return true;}private function w_log($data) {$root_path=str_replace('\\', '/', dirname(__FILE__));//改成你自己目录$dir = $root_path . '/'.$this->logPath;if (!is_dir($dir)) {mkdir($dir, 0777, true);}$log_file = $dir . date('Ymd', time()) . '.txt';file_put_contents($log_file, "\r\n", FILE_APPEND);if (is_array($data)) {$data = json_encode($data,JSON_UNESCAPED_UNICODE); // 将数组转换为JSON格式的字符串}file_put_contents($log_file, "/".date("Y-m-d H:i:s")."--".$data, FILE_APPEND);}}
$check=new SecurityCheck();
echo $check->checkAll($_GET['tel']);sendSMS();
function sendSMS() {//发送短信if ($_SESSION['ipNum']) {++$_SESSION['ipNum'];} else {$_SESSION['ipNum'] = 1;}if ($_SESSION['telNum']) {++$_SESSION['telNum'];} else {$_SESSION['telNum'] = 1;}//添加自己的发送验证码接口//添加自己的发送验证码接口return ['status' => 1, 'message' => '发送成功'];
}