php技术 – 青春部落,流年似水 http://www.youthtribe.com 青春是一场远行,总记不起来时的路。 Fri, 07 Jan 2022 11:03:22 +0000 zh-CN hourly 1 https://wordpress.org/?v=6.1.6 php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1932 Thu, 06 Jan 2022 08:15:50 +0000 http://www.youthtribe.com/?p=1932 一些小黑客会试图通过正常的但高频的【正常访问】去查看我们网站的一些隐藏的页面,比如管理员登录页面。还有一种比较常见的是去扫描一些 行业内的 现成的 木马文件,来攻击我们的网站。因为短时间内会有大量的看似正常的请求,会造成性能不高的服务器的报警,如内存或者cpu,甚至带宽 吃紧,影响正常服务。

下图为非法请求示例

可以利用redis的高性能优势去做一些文章。

原理就是用网站程序去判断用户的请求,如果判断出是恶意的,那么把该访问者记录下来存储到redis中去。如果该访问者再来请求,那么直接给他返回500。正常情况来讲,黑客的扫描工具得知500相应码后,应该会停止扫描吧。就算不停止,那么redis的高性能也可以抵挡这些非法扫描请求,从而达到保护服务器的目的。通常可以设置一个小时内禁止该ip的正常访问。时间可以自己设置。

如何判断恶意访问:比如我的是php程序,如果用户的请求中出现了 .asp或者.jsp,或者hack,hacker字样,那么就判断你小子不老实,就打入冷宫。至于加哪些判断字符可以根据自己的需求进行设置。

需要服务器安装redis,我这里是用的php做了一个示例,我的php文件名为【illegal_deny.php】,用户可以直接在站点入口包含此文件即可。

require __DIR__.'/../app/diy/illegal_deny.php';

下边是文件内容。请根据自己的实际情况进行修改。

<?php
//redis的配置
//define('REDIS_SERVER_IP', '121.40.24.38');
define('REDIS_SERVER_IP', '127.0.0.1');
define('REDIS_SERVER_PORT', 6379);
//auth就是密码
define('REDIS_SERVER_AUTH', 'yourpassword');

//禁止访问的时间,单位秒 3600 = 1小时
define('DENY_TIME', 3600);


function getRedis($db = 0){
    
    $redis = new \Redis();
    $res = $redis->connect(REDIS_SERVER_IP,REDIS_SERVER_PORT);
    $redis->auth(REDIS_SERVER_AUTH);
    $redis->select($db);
    return $redis;
}

//如果是访问一些非法网址,比如黑客,就进行一段时间的访问屏蔽 begin
//非法访问次数
$illegal_attempt_count_key = 'ILLEGAL_ATTEMPT_COUNT';

//是否已经被限制访问
$illegal_key = "ILLEGAL_IP_".$_SERVER['REMOTE_ADDR'];
//$illegal_key = "ILLEGAL_IP_".$_SERVER['HTTP_X_FORWARDED_FOR'];



$uri = $_SERVER['REQUEST_URI'];


$redis = getRedis();
//$redis->delete($illegal_key);
if($redis->exists($illegal_key)){
 
    header('HTTP/1.1 500 Internal Server Error');
    exit(0);
}

$limit_time = DENY_TIME;//禁止时间,单位秒
//判断是否要进行限制

//几个禁止访问的目录
if($uri == '/admin'){
    //
    $redis->setex($illegal_key,$limit_time,date("Y-m-d H:i:s",time()));
    $redis->incr($illegal_attempt_count_key);
    header('HTTP/1.1 500 Internal Server Error');
    exit(0);
}

//限制访问地址包含的一些关键词,只要匹配到就进行限制
$illegal_urls = ['hack','hacker','.asp','.jsp'];
foreach ($illegal_urls as $url){
    if(strpos($uri, $url)){
        //
        $redis->setex($illegal_key,$limit_time,date("Y-m-d H:i:s",time()));
        $redis->incr($illegal_attempt_count_key);
        
        header('HTTP/1.1 500 Internal Server Error');
        exit(0);
    }
}
//如果是访问一些非法网址,比如黑客,就进行一段时间的访问屏蔽 over

附:对我的1cpu2G内存的服务器加了这个【护盾】后,服务器就不会再报警了(之前是cpu 一旦过90%使用率就会发邮件,短信,好烦)。真香!谁用谁说好。

]]>
php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1905 http://www.youthtribe.com/archives/1905#respond Thu, 23 Sep 2021 16:43:14 +0000 http://www.youthtribe.com/?p=1905 主要就是要设置一下邮件的编码方式。默认的是CHARSET_ISO88591,改为CHARSET_UTF8就可以了。按说UTF8应该是国际通用的主流吧,但不知道基于什么目的这个PHPMailer默认的不是utf8.

$mail = new PHPMailer(true);
$mail->CharSet = PHPMailer::CHARSET_UTF8;
//或者
//$mail->CharSet = 'utf-8';

版本为phpmailer: 6.1.7

]]>
http://www.youthtribe.com/archives/1905/feed 0
php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1682 http://www.youthtribe.com/archives/1682#respond Fri, 16 Jun 2017 08:03:08 +0000 http://www.youthtribe.com/?p=1682 首先,关于RSA的加密介绍文章,就不多说了。直接看这个网页吧(作者写的计算机科普文章是极好的)

http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html

我要实现的功能就是通过这对公钥和密钥,实现客户端软件对密码进行加密(采用公钥),然后网站端(php)采用密钥进行解密验证。

开始吧。


php服务端利用内置函数生成公钥和密钥。记得这需要php服务器安装了openssl扩展。先生成cer文件和pfx文件。cer文件相当于公钥(可以发给客户端),pfx是密钥(必须严格保存于服务器端不能泄露)。

 $dn = array(
 "countryName" => 'zh', //所在国家名称
 "stateOrProvinceName" => 'GuangDong', //所在省份名称
 "localityName" => 'ShenZhen', //所在城市名称
 "organizationName" => 'baibai', //注册人姓名
 "organizationalUnitName" => 'company', //组织名称
 "commonName" => 'bbb', //公共名称
 "emailAddress" => '123@.qq.com' //邮箱
 );
 $privkeypass = 'cf'; //私钥密码
 $numberofdays = 3650; //有效时长
 $cerpath = "./test.cer"; //生成证书路径
 $pfxpath = "./test.pfx"; //密钥文件路径//生成证书
 $privkey = openssl_pkey_new();
 $csr = openssl_csr_new($dn, $privkey);
 $sscert = openssl_csr_sign($csr, null, $privkey, $numberofdays);
 openssl_x509_export_to_file($sscert, $cerpath); //导出证书到文件
 //openssl_pkcs12_export_to_file($sscert, $pfxpath, $privkey, $privkeypass); //生成密钥文件
 openssl_pkey_export_to_file($privkey, $pfxpath); //生成密钥文件

以下是是php端进行简单测试的代码:

/*
 //私钥加密
 $cer_key = file_get_contents($pfxpath); //获取密钥内容
 openssl_pkcs12_read($cer_key, $certs, $privkeypass);
 openssl_sign($data, $signMsg, $certs['pkey'],OPENSSL_ALGO_SHA1); //注册生成加密信息
 $signMsg = base64_encode($signMsg); //base64转码加密信息
 //echo $signMsg;
 
 
 //公钥解密
 $cer_key = file_get_contents($cerpath); //获取证书内容
 $unsignMsg=base64_decode($signMsg);//base64解码加密信息
 $cer = openssl_x509_read($cer_key); //读取公钥
 $res = openssl_verify($data, $unsignMsg, $cer); //验证
 echo $res; //输出验证结果,1:验证成功,0:验证失败
 */
 
 
 
 $data = "123456";
 $crypted = "";
 $key = file_get_contents($cerpath);
 //公钥加密
 openssl_public_encrypt($data, $crypted, $key);
 echo base64_encode($crypted)."<br>";
 //echo $crypted."<br>";
 
 //私钥解密
 $decrypted = "";
 $s = file_get_contents($pfxpath);
 //echo "<br>$s<br>";
 $key2 = openssl_pkey_get_private(file_get_contents($pfxpath));

 if(openssl_private_decrypt($crypted, $decrypted, $key2)){
 echo $decrypted;
 }
 else{
 echo "failed";
 }

 


客户端通过加载cer文件,将要加密的文本用公钥加密。以下是一个函数

//通过读取本地的cer文件,得到公钥,然后对文本内容加密,返回加密后的文本,最后传到服务器端,与密钥进行比对
public static String getRSAText(Context context,String strText){

    try {
        //读取证书文件

        InputStream inStream = context.getResources().getAssets().open("test.cer");
        //创建X509工厂类
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        //创建证书对象
        X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
        inStream.close();

        Cipher c1 = Cipher.getInstance("RSA/None/PKCS1Padding");

        c1.init(Cipher.PUBLIC_KEY, cert);
        byte[] cipherText = c1.doFinal(strText.getBytes());


        //base64转换一下,方便传输
        String res = Base64.encodeToString(cipherText,Base64.DEFAULT);
        return res;


    }
    catch (InvalidKeyException e){

    }
    catch (IOException e){

    }
    catch (CertificateException e){

    }
    catch (NoSuchPaddingException e){

    }
    catch (IllegalBlockSizeException e){

    }
    catch (BadPaddingException e){

    }
    catch (NoSuchAlgorithmException e){

    }
    return "";
}

下边是php端用pfx文件(里边放有密钥)进行解密的代码。我改写成了一个函数。参数是pfx文件路径,pfx文件名,要解密的字符串

function getPassword( $path,$pfx_file_name,$str ){ 
 $pfxpath = $path . "/public/$pfx_file_name"; //密钥文件路径
 //$cerpath = "./test.cer"; //生成证书路径
 //私钥解密
 $decrypted = "";
 
 $key2 = openssl_pkey_get_private(file_get_contents($pfxpath)); 

 $res = openssl_private_decrypt(base64_decode($str), $decrypted, $key2);
 
 if($res){
 
 return $decrypted;
 }
 else{
 return "";
 }

}
]]>
http://www.youthtribe.com/archives/1682/feed 0
php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1633 http://www.youthtribe.com/archives/1633#respond Thu, 23 Mar 2017 18:01:20 +0000 http://www.youthtribe.com/?p=1633 web网站负载均衡是解决大流量网站的办法。我开始摸索着如何实现nginx下的php站点的负载均衡。

最低要求3台主机。其中一台做前端,两台做后端。所有请求都是通过前端,然后前端根据后端服务器的负载情况进行请求的分发,最后再通过前端返回数据给访问者。

前端要做的就是反向代理(具体这个含义,说实话,我一直没理解太透彻。。)。主要有两步:一是设置后端的所有服务器地址。二是设置前端所截断、获取的请求要转向的后端组,其实也就是第一步中的设置值。

  • 在nginx.conf中设置所有后端服务器,同时起一个名字。通常设置为backend。pustream应该放在http域块中。
  • upstream backend {

    #ip_hash;#请求分发方式
    server backend1.ss.com weight=5;

    server backend2.ss.com weight=2;

    server backend3.ss.com weight=1;

    }

  • 在server域块中,添加我们要截断的请求,进行转发给backend组。在这里我们设置为截断所有请请求。
  • location / {
    #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
    #proxy_set_header Host $host;
    #proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #禁用缓存

    # proxy_buffering off;

    #设置反向代理的地址
    proxy_pass http://backend; #这里是重点!
    }

    ]]>
    http://www.youthtribe.com/archives/1633/feed 0
    php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1596 http://www.youthtribe.com/archives/1596#respond Sat, 24 Sep 2016 03:12:27 +0000 http://www.youthtribe.com/?p=1596 解压到网站根目录,然后复制 libraries/config.default.php 到网站根目录 config.inc.php

    以后相关的配置就要修改 config.inc.php。

    如果你的phpmyadmin是要链接到对应ip的主机,修改

    $cfg[‘Servers’][$i][‘host’] = ‘your ip’;

    等内容。

    在安装时,我还遇到一个小问题,就是如果服务器用了 memcached做session,那么就要再修改一下

    这个  config.inc.php 文件,

    在最上面加上

    ini_set(‘session.save_path’, ”);
    ini_set(‘session.save_handler’, ‘files’);

    这两句就可以了。其实也就是让php的session恢复到默认的文件存储的意思。

    ok.done!

    ]]>
    http://www.youthtribe.com/archives/1596/feed 0
    php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1591 http://www.youthtribe.com/archives/1591#comments Sun, 11 Sep 2016 15:30:13 +0000 http://www.youthtribe.com/?p=1591 问题定位为:$isSign 总为false

    修改这个文件:alipay_notify.class.php

    //unset一个参数就可以
    unset($_GET[‘_url’]);
    $isSign = $this->getSignVeryfy($_GET, $_GET[“sign”]);

     

     

    由这个事我也大概知道了:

    大公司做事其实也很糙。。一些小bug都不会及时更新修复。。

    再比如SignVeryfy中的veryfy,应该是verify吧。。。根本就没有veryfy这个单词。。。这水平也真是。。。

    要不是自己毕业学校不好,我进大公司的能力也应该是有的,哈哈。唉,导致自己现在苦逼创业了。。。

    ]]>
    http://www.youthtribe.com/archives/1591/feed 1
    php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1532 http://www.youthtribe.com/archives/1532#respond Thu, 07 Jul 2016 08:33:00 +0000 http://www.youthtribe.com/?p=1532 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
    <title>无标题文档</title>
    </head>

    <body>
    <?php
    //mysql

    $dsn = ‘mysql:host=localhost’.’;dbname=name’;

    $pdo = new PDO($dsn, “username”, “ps”);
    $pdo->query(“set names utf8”);
    /////////////////////////////////////
    $handle = fopen(“ips.txt”, “r”);
    $i = 0;
    if ($handle) {
    while (!feof($handle))
    {
    /*
    if ($i >100)
    {
    break;
    }
    $i++;
    */
    $str = fgets($handle, 4096);
    echo $str.”<br>”;
    $ip_start = substr($str,0,15);
    $ip_end = substr($str,16,15);
    $ip_start = str_replace(” “,””,$ip_start);
    $ip_end = str_replace(” “,””,$ip_end);

    $county_region = substr($str,32,strlen($str)-1);
    //$county_region = mb_convert_encoding($county_region, “utf-8”, “utf-8”);
    echo $ip_start.”==”.$ip_end.”==”.$county_region.”<br>”;

    $ip_start_to_long_int = ip_to_long_int($ip_start);
    $ip_end_to_long_int = ip_to_long_int($ip_end);

    //insert into mysql
    $sql = “insert into `ips`(`ip_start`,`ip_start_number`,`ip_end`,`ip_end_number`,`info`) values( ‘$ip_start’,$ip_start_to_long_int ,’$ip_end’,$ip_end_to_long_int,’$county_region’)”;
    $res = $pdo->exec($sql);
    if ($res == 0)
    {
    echo $sql.”<br>”;
    }
    }
    }
    echo “ok”;
    function ip_to_long_int($ip){

    $arr = explode(‘.’,$ip);
    $res = $arr[0]*255*255*255 + $arr[1]*255*255 + $arr[2]*255 + $arr[3];
    return $res;

    }
    ?>
    </body>
    </html>

    ]]>
    http://www.youthtribe.com/archives/1532/feed 0
    php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1511 http://www.youthtribe.com/archives/1511#respond Tue, 05 Apr 2016 16:16:31 +0000 http://www.youthtribe.com/?p=1511 1.修改apache conf/http.conf文件

    修改网站根目录地址:

    Define SRVROOT “C:\Users\Administrator\Desktop\server\apache”

    添加apache加载php的扩展及指定加载的php.ini文件夹路径

    LoadModule php5_module “C:\Users\Administrator\Desktop\lnmp\php-5.6.20-Win32-VC11-x64\php5apache2_4.dll”
    PHPIniDir “C:\Users\Administrator\Desktop\lnmp\php-5.6.20-Win32-VC11-x64”

    把php.ini-development复制一份,重新全名为 php.ini

    添加指令让apache可以解析php文件

    AddType application/x-httpd-php .php

    修改文件夹默认首页文件,添加 index.php作为默认首页

    <IfModule dir_module>
    DirectoryIndex index.php index.html
    </IfModule>

    apache 不列出目录 把 Options Indexes FollowSymLinks 改为

    Options FollowSymLinks

    只需要将上面代码中的 Indexes 去掉,就可以禁止 Apache 显示该目录结构。用户就不会看到该目录下的文件和子目录列表了。Indexes 的作用就是当该目录下没有 index.html 文件时,就显示目录结构,去掉 Indexes ,Apache 就不会显示该目录的列表了。

    重要 :在httpd中加入以下两个参数!

    # AcceptFilter: On Windows, none uses accept() rather than AcceptEx() and
    # will not recycle sockets between connections. This is useful for network
    # adapters with broken driver support, as well as some virtual network
    # providers such as vpn drivers, or spam, virus or spyware filters.
    AcceptFilter http none
    AcceptFilter https none


    cmd 命令把 apache 添加到服务

    httpd.exe -k install

    其实可以httpd -help 查看命令帮助

     

    ]]>
    http://www.youthtribe.com/archives/1511/feed 0
    php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1501 http://www.youthtribe.com/archives/1501#respond Fri, 15 Jan 2016 06:15:25 +0000 http://www.youthtribe.com/?p=1501 【linux】编译安装php7-mysql-apache教程

    php7安装包地址:http://cn2.php.net/get/php-7.0.2.tar.gz/from/this/mirror

    wget http://cn2.php.net/get/php-7.0.2.tar.gz/from/this/mirror

    configure 时可能会遇到:xml2-config not found 提示

    安装两个包:

    yum install libxml2

     

    yum install libxml2-devel

     

     

    ]]>
    http://www.youthtribe.com/archives/1501/feed 0
    php技术 – 青春部落,流年似水 http://www.youthtribe.com/archives/1486 http://www.youthtribe.com/archives/1486#respond Sat, 25 Jul 2015 07:37:43 +0000 http://www.youthtribe.com/?p=1486 jquery 实现 html input checkbox的全选,全不选和反选

    用jquery非常简单

    直接上代码了

     

    
    
    ]]>
    http://www.youthtribe.com/archives/1486/feed 0