(安全篇)继续谈php安全
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz/QibLP1rpwH8sYPCicktMM1yia3vo4v3k9ssiaEZXbTu2fkQicEHe46BdZuW7icruIo9j4QExzqe5rude9MkwF2OCdRdA/0?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;"><span style="color: black;">更加多</span>学习视频</span><strong style="color: blue;">分享</strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">想要领取整套学习视频教程(<span style="color: black;">包含</span><span style="color: black;">更加多</span>的IT学习视频)的<span style="color: black;">朋友</span>,<span style="color: black;">能够</span>加</span>QQ <span style="color: black;"><strong style="color: blue;">2084533608,</strong><span style="color: black;">加入时验证信息填:</span><span style="color: black;"><strong style="color: blue;">学习</strong></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">“<strong style="color: blue;"><span style="color: black;">容易</span>学PHP</strong>”,有<span style="color: black;">学习文档</span>,框架<span style="color: black;">视频教程</span>,<span style="color: black;">网站教程</span>,<span style="color: black;">供给</span>百度分享给<span style="color: black;">大众</span>下载,<span style="color: black;">保藏</span>。<span style="color: black;">大众</span>关注后回复关键词,便可获取!平台将与<span style="color: black;">大众</span>分享前端到后端的实例,学习语法,学习教程,技巧等!欢迎<span style="color: black;">大众</span>关注!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/QibLP1rpwH8v2Yo5tzSas9cfKYz54WdSNKm41UwibynwMWWIwDMeXLIyU4EwxDegpSFiaNgFypJGIjU8X227dO8Bw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/QibLP1rpwH8v2Yo5tzSas9cfKYz54WdSNwC6QRicxDJPdveXO1diaS3JaFSoGB5eR58J29kpIicX56nia4y8NeTIPicA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz/QibLP1rpwH8uxZnQoiaPFbMicItRW2JrXnyzjSia1geLkrzHrTMkWP51nmU54kKiabKwScZRngJEgMQL44ibT71maibyw/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;"><span style="color: black;">正文如下:</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">把握整站的结构,避免<span style="color: black;">泄密</span>站点<span style="color: black;">敏锐</span>目录</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在写代码之初,我<span style="color: black;">亦</span>是像<span style="color: black;">非常多</span>老源码<span style="color: black;">同样</span>,在根目录下放上index.php、register.php、login.php,用户点击注册页面,就<span style="color: black;">转</span>到http://localhost/register.php。并<span style="color: black;">无</span>太多的结构的思想,像<span style="color: black;">这般</span>的代码结构,最大的问题倒不是安全性问题,而是代码扩展与移植问题。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在写代码的过程中,<span style="color: black;">咱们</span>常要对代码进行修改,<span style="color: black;">此时</span>候<span style="color: black;">倘若</span>代码<span style="color: black;">无</span>统一的一个入口点,<span style="color: black;">咱们</span>可能要改<span style="color: black;">非常多</span><span style="color: black;">地区</span>。后来我读了一点emlog的代码,<span style="color: black;">发掘</span>网站真正的前端代码都在模板目录里,而根目录下就<span style="color: black;">仅有</span>入口点文件和配置文件。这才顿悟,对<span style="color: black;">全部</span>网站的结构进行了修改。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">网站根目录下放上一个入口点文件,让它来对<span style="color: black;">全部</span>网站所有页面进行管理,这个时候注册页面变<span style="color: black;">成为了</span>http://localhost/?act=register,任何页面只是act的一个参数,在得到这个参数后,再用一个switch来<span style="color: black;">选取</span>要<span style="color: black;">包括</span>的文件内容。在这个入口点文件中,还<span style="color: black;">能够</span><span style="color: black;">包括</span><span style="color: black;">有些</span>常量的定义,比如网站的绝对路径、网站的<span style="color: black;">位置</span>、数据库用户<span style="color: black;">秘码</span>。以后<span style="color: black;">咱们</span>在脚本的编写中,<span style="color: black;">尽可能</span><span style="color: black;">运用</span>绝对路径而不要<span style="color: black;">运用</span>相对路径(否则脚本<span style="color: black;">倘若</span>改变位置,代码<span style="color: black;">亦</span>要变),而这个绝对路径就来自入口点文件中的定义。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然,在安全性上,一个入口点文件<span style="color: black;">亦</span>能<span style="color: black;">隐匿</span>后台<span style="color: black;">位置</span>。像<span style="color: black;">这般</span>的<span style="color: black;">位置</span>http://localhost/?act=xxx不会暴露后台绝对路径,<span style="color: black;">乃至</span><span style="color: black;">能够</span>经常更改,<span style="color: black;">不消</span>改变太多代码。一个入口点文件<span style="color: black;">亦</span><span style="color: black;">能够</span>验证<span style="color: black;">拜访</span>者的身份,<span style="color: black;">例如</span>一个网站后台,不是管理员就不<span style="color: black;">准许</span>查看任何页面。在入口点文件中就<span style="color: black;">能够</span>验证身份,<span style="color: black;">倘若</span><span style="color: black;">无</span>登录,就输出404页面。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 有了入口点文件,我就把所有非入口点文件前面加上了这句话:</p><span style="color: black;"><?</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php </p>
</span><span style="color: black;">if</span><span style="color: black;">(!</span><span style="color: black;">defined</span><span style="color: black;">(</span><span style="color: black;">WWW_ROOT</span><span style="color: black;">))</span><span style="color: black;">{</span><span style="color: black;">header</span><span style="color: black;">(</span><span style="color: black;">"HTTP/1.1 404 Not Found"</span><span style="color: black;">);</span><span style="color: black;">exit</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;}</p>
</span><span style="color: black;">?></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WWW_ROOT是我在入口点中定义的一个常量,<span style="color: black;">倘若</span>用户是<span style="color: black;">经过</span>这个页面的绝对路径<span style="color: black;">拜访</span>(http://localhost/register.php),我就输出404错误;<span style="color: black;">仅有</span><span style="color: black;">经过</span>入口点<span style="color: black;">拜访</span>(http://localhost/?act=register),<span style="color: black;">才可</span>执行后面的代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">运用</span>预编译语句,避免sql注入</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 注入是早前很大的一个问题,<span style="color: black;">不外</span>近些年<span style="color: black;">由于</span><span style="color: black;">大众</span>比较<span style="color: black;">注重</span>这个问题,<span style="color: black;">因此</span>慢慢变得好了<span style="color: black;">非常多</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实<span style="color: black;">非常多</span>漏洞,像sql注入或xss,都是将“数据”和“代码”<span style="color: black;">无</span>区<span style="color: black;">掰开</span>。“代码”是程序员写的内容,“数据”是用户<span style="color: black;">能够</span>改变的内容。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>写一个sql语句select * from admin where username=admin password=xxxxx, admin和xxxxx<span style="color: black;">便是</span>数据,是用户输入的用户名和<span style="color: black;">秘码</span>,但<span style="color: black;">倘若</span><span style="color: black;">无</span>任何处理,用户输入的就可能是“代码”,<span style="color: black;">例如</span>or =,<span style="color: black;">这般</span>就造<span style="color: black;">成为了</span>漏洞。“代码”是绝对不能让用户接触的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在php中,<span style="color: black;">针对</span>mysql数据库有两个模块,mysql和mysqli,mysqli的意思<span style="color: black;">便是</span>mysql improve。mysql的改进版,这个模块中就含有“预编译”这个概念。像上面那个sql语句,改一改:select * from admin where username=? password=?,它就不是一个sql语句了,<span style="color: black;">然则</span><span style="color: black;">能够</span><span style="color: black;">经过</span>mysqli的预编译功能先把他编译成stmt对象,在后期用户输入账号<span style="color: black;">秘码</span>后,用stmt->bind_param将用户输入的“数据”绑定到这两个问号的位置。<span style="color: black;">这般</span>,用户输入的内容就只能是“数据”,而不可能变成“代码”。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这两个问号限定了“数据”的位置,以及sql语句的结构。<span style="color: black;">咱们</span><span style="color: black;">能够</span>把<span style="color: black;">咱们</span>所有的数据库操作都封装到一个类中,所有sql语句的执行都进行预编译。<span style="color: black;">这般</span>就完全避免了sql注入,这<span style="color: black;">亦</span>是吴翰清最<span style="color: black;">举荐</span>的<span style="color: black;">处理</span><span style="color: black;">方法</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 下面是<span style="color: black;">运用</span>mysqli的<span style="color: black;">有些</span>代码部分(所有的判断函数运行成功或失败的代码我都省略了,但不<span style="color: black;">表率</span>不重要):</p><span style="color: black;"><?</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//用户输入的数据</p>
</span><span style="color: black;">$name</span><span style="color: black;">=</span><span style="color: black;">admin</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
</span><span style="color: black;">$pass </span><span style="color: black;">=</span><span style="color: black;">123456</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//<span style="color: black;">首要</span>新建mysqli对象,构造函数参数中<span style="color: black;">包括</span>了数据库<span style="color: black;">关联</span>内容。</p>
</span><span style="color: black;">$conn </span><span style="color: black;">=</span><span style="color: black;">new</span><span style="color: black;"> mysqli</span><span style="color: black;">(</span><span style="color: black;">DB_HOST</span><span style="color: black;">,</span><span style="color: black;"> DB_USER</span><span style="color: black;">,</span><span style="color: black;"> DB_PASS</span><span style="color: black;">,</span><span style="color: black;"> DB_NAME</span><span style="color: black;">,</span><span style="color: black;"> DB_PORT</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//设置sql语句默认编码</p>
</span><span style="color: black;">$this</span><span style="color: black;">-></span><span style="color: black;">mysqli</span><span style="color: black;">-></span><span style="color: black;">set_charset</span><span style="color: black;">(</span><span style="color: black;">"utf8"</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//创建一个<span style="color: black;">运用</span>通配符的sql语句</p>
</span><span style="color: black;">$sql </span><span style="color: black;">=</span><span style="color: black;">SELECT user_id FROM admin WHERE username=? AND password=?;</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//编译该语句,得到一个stmt对象.</p>
</span><span style="color: black;">$stmt </span><span style="color: black;">=</span><span style="color: black;"> $conn</span><span style="color: black;">-></span><span style="color: black;">prepare</span><span style="color: black;">(</span><span style="color: black;">$sql</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/********************</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">之后的内容就能重复利用,<span style="color: black;">不消</span>再次编译</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">*************************/</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//用bind_param<span style="color: black;">办法</span>绑定数据</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//<span style="color: black;">大众</span><span style="color: black;">能够</span>看出来,<span style="color: black;">由于</span>我留了两个?,<span style="color: black;">亦</span><span style="color: black;">便是</span>要向其中绑定两个数据,<span style="color: black;">因此</span><span style="color: black;">第1</span>个参数是绑定的数据的类型(s=string,i=integer),第二个以后的参数是要绑定的数据</p>
</span><span style="color: black;">$stmt</span><span style="color: black;">-></span><span style="color: black;">bind_param</span><span style="color: black;">(</span><span style="color: black;">ss</span><span style="color: black;">,</span><span style="color: black;"> $name</span><span style="color: black;">,</span><span style="color: black;"> $pass</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//调用bind_param<span style="color: black;">办法</span>绑定结果(<span style="color: black;">倘若</span>只是<span style="color: black;">检测</span>该用户与<span style="color: black;">秘码</span><span style="color: black;">是不是</span>存在,或只是一个DML语句的时候,<span style="color: black;">不消</span>绑定结果)</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//这个结果<span style="color: black;">便是</span>我select到的字段,有几个就要绑定几个</p>
</span><span style="color: black;">$stmt</span><span style="color: black;">-></span><span style="color: black;">bind_result</span><span style="color: black;">(</span><span style="color: black;">$user_id</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//执行该语句</p>
</span><span style="color: black;">$stmt</span><span style="color: black;">-></span><span style="color: black;">execute</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">();</p>
</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//得到结果</p>
</span><span style="color: black;">if</span><span style="color: black;">(</span><span style="color: black;">$stmt</span><span style="color: black;">-></span><span style="color: black;">fetch</span><span style="color: black;">()){</span><span style="color: black;"> echo </span><span style="color: black;">登陆成功</span><span style="color: black;">;</span><span style="color: black;">//<span style="color: black;">必定</span>要<span style="color: black;">重视</span>释放结果资源,否则后面会出错</span><span style="color: black;"> $stmt</span><span style="color: black;">-></span><span style="color: black;">free_result</span><span style="color: black;">();</span><span style="color: black;">return</span><span style="color: black;"> $user_id</span><span style="color: black;">;</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//返回刚才select到的内容</p>
</span><span style="color: black;">}</span><span style="color: black;">else</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
</span><span style="color: black;">echo </span><span style="color: black;">登录失败</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
</span><span style="color: black;">?></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">预防XSS代码,<span style="color: black;">倘若</span>不需要<span style="color: black;">运用</span>cookie就不<span style="color: black;">运用</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 在我的网站中并<span style="color: black;">无</span><span style="color: black;">运用</span>cookie,更<span style="color: black;">由于</span>我对权限限制的很死,<span style="color: black;">因此</span><span style="color: black;">针对</span>xss<span style="color: black;">来讲</span>危险性比较小。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>xss的防御,<span style="color: black;">亦</span>是一个道理,处理好“代码”和“数据”的关系。当然,<span style="color: black;">这儿</span>的代码指的<span style="color: black;">便是</span>javascript代码或html代码。用户能<span style="color: black;">掌控</span>的内容,<span style="color: black;">咱们</span><span style="color: black;">必定</span>要<span style="color: black;">运用</span>htmlspecialchars等函数来处理用户输入的数据,并且在javascript中要<span style="color: black;">小心</span>把内容输出到页面中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">限制用户权限,预防CSRF</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">此刻</span>脚本漏洞比较火的<span style="color: black;">便是</span>越权<span style="color: black;">行径</span>,<span style="color: black;">非常多</span>重要操作<span style="color: black;">运用</span>GET方式执行,或<span style="color: black;">运用</span>POST方式执行而<span style="color: black;">无</span>核实执行者<span style="color: black;">是不是</span>知情。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CSRF<span style="color: black;">非常多</span><span style="color: black;">朋友</span>可能比较陌生,其实举一个小例子就行了:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">A、B都是某论坛用户,该论坛<span style="color: black;">准许</span>用户“赞”某篇<span style="color: black;">文案</span>,用户点“赞”其实是<span style="color: black;">拜访</span>了这个页面:http://localhost/?act=support&articleid=12。这个时候,B<span style="color: black;">倘若</span>把这个URL发送给A,A在不知情的<span style="color: black;">状况</span>下打开了它,等于说给articleid=12的<span style="color: black;">文案</span>赞了一次。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">因此</span>该论坛换了种方式,<span style="color: black;">经过</span>POST方式来赞某篇<span style="color: black;">文案</span>。</p><span style="color: black;"><form</span><span style="color: black;">action</span><span style="color: black;">=</span><span style="color: black;">"http://localhost/?act=support"</span><span style="color: black;">method</span><span style="color: black;">=</span><span style="color: black;">"POST"</span><span style="color: black;">></span><span style="color: black;"><input</span><span style="color: black;">type</span><span style="color: black;">=</span><span style="color: black;">"hidden"</span><span style="color: black;">value</span><span style="color: black;">=</span><span style="color: black;">"12"</span><span style="color: black;">name</span><span style="color: black;">=</span><span style="color: black;">"articleid"</span><span style="color: black;">></span><span style="color: black;"><input</span><span style="color: black;">type</span><span style="color: black;">=</span><span style="color: black;">"submit"</span><span style="color: black;">value</span><span style="color: black;">=</span><span style="color: black;">"赞"</span><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">></p>
</span><span style="color: black;"></form></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">能够</span>看到一个<span style="color: black;">隐匿</span>的input框里含有该<span style="color: black;">文案</span>的ID,<span style="color: black;">这般</span>就<span style="color: black;">不可</span><span style="color: black;">经过</span>一个URL让A点击了。<span style="color: black;">然则</span>B<span style="color: black;">能够</span>做一个“极具<span style="color: black;">引诱</span>力”的页面,其中某个按钮就写成<span style="color: black;">这般</span>一个表单,来<span style="color: black;">引诱</span>A点击。A一点击,依旧还是赞了这篇<span style="color: black;">文案</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,该论坛只好把表单中<span style="color: black;">增多</span>了一个验证码。<span style="color: black;">仅有</span>A输入验证码<span style="color: black;">才可</span>点赞。<span style="color: black;">这般</span>,彻底死了B的心。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">然则</span>,你见过哪个论坛点“赞”<span style="color: black;">亦</span>要输入验证码?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>在<span style="color: black;">这儿</span><span style="color: black;">举荐</span>了最好的方式,<span style="color: black;">便是</span>在表单中加入一个随机字符串token(由php生成,并<span style="color: black;">保留</span>在SESSION中),<span style="color: black;">倘若</span>用户提交的这个随机字符串和SESSION中<span style="color: black;">保留</span>的字符串一致,<span style="color: black;">才可</span>赞。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在B不<span style="color: black;">晓得</span>A的随机字符串时,就<span style="color: black;">不可</span>越权操作了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 我在网站中<span style="color: black;">亦</span>多次<span style="color: black;">运用</span>了TOKEN,不管是GET方式还是POST方式,<span style="color: black;">一般</span>就能抵御99%的CSRF估计了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">严格<span style="color: black;">掌控</span>上传文件类型</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 上传漏洞是很致命的漏洞,只要存在任意文件上传漏洞,就能执行任意代码,拿到webshell。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我在上传这部分,写了一个php类,<span style="color: black;">经过</span>白名单验证,来<span style="color: black;">掌控</span>用户上传恶意文件。在客户端,我<span style="color: black;">经过</span>javascript先验证了用户<span style="color: black;">选取</span>的文件的类型,但这只是善意地提醒用户,<span style="color: black;">最后</span>验证部分,还是在服务端。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 白名单是必要的,你<span style="color: black;">倘若</span>只<span style="color: black;">准许</span>上传<span style="color: black;">照片</span>,就设置成array(jpg,gif,png,bmp),当用户上传来文件后,取它的文件名的后缀,用in_array验证<span style="color: black;">是不是</span>在白名单中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在上传文件数组中,会有一个MIME类型,告诉服务端上传的文件类型是什么,<span style="color: black;">然则</span>它是不<span style="color: black;">靠谱</span>的,是<span style="color: black;">能够</span>被修改的。在<span style="color: black;">非常多</span>存在上传漏洞的网站中,都是只验证了MIME类型,而<span style="color: black;">无</span>取文件名的后缀验证,<span style="color: black;">引起</span>上传任意文件。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">因此</span><span style="color: black;">咱们</span>在类中完全<span style="color: black;">能够</span>忽略这个MIME类型,而只取文件名的后缀,<span style="color: black;">倘若</span>在白名单中,才<span style="color: black;">准许</span>上传。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然,服务器的解析漏洞<span style="color: black;">亦</span>是<span style="color: black;">非常多</span>上传漏洞的突破点,<span style="color: black;">因此</span><span style="color: black;">咱们</span><span style="color: black;">尽可能</span>把上传的文件重命名,以“日期时间+随机数+白名单中后缀”的方式对上传的文件进行重命名,避免<span style="color: black;">由于</span>解析漏洞而<span style="color: black;">导致</span>任意代码执行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">加密混淆javascript代码,<span style="color: black;">加强</span>攻击门槛</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">非常多</span>xss漏洞,都是黑客<span style="color: black;">经过</span>阅读javascript代码<span style="color: black;">发掘</span>的,<span style="color: black;">倘若</span><span style="color: black;">咱们</span>能把所有javascript代码混淆以及加密,让代码就算解密后<span style="color: black;">亦</span>是混乱的(<span style="color: black;">例如</span>把所有变量名替换成其MD5 hash值),<span style="color: black;">加强</span>阅读的难度。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">运用</span>更高级的hash算法<span style="color: black;">保留</span>数据库中重要信息</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在这个硬盘容量大增的时期,<span style="color: black;">非常多</span>人<span style="color: black;">持有</span>很大的彩虹表,再加上类似于cmd5<span style="color: black;">这般</span>的网站的大行其道,单纯的md5<span style="color: black;">已然</span>等同于无物,<span style="color: black;">因此</span><span style="color: black;">咱们</span>迫切的需要更高级的hash算法,来<span style="color: black;">保留</span><span style="color: black;">咱们</span>数据库中的<span style="color: black;">秘码</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">因此</span>后来<span style="color: black;">显现</span>了加salt的md5,<span style="color: black;">例如</span>discuz的<span style="color: black;">秘码</span><span style="color: black;">便是</span>加了salt。其实salt<span style="color: black;">便是</span>一个<span style="color: black;">秘码</span>的“附加值”,<span style="color: black;">例如</span>A的<span style="color: black;">秘码</span>是123456,而<span style="color: black;">咱们</span>设置的salt是abc,<span style="color: black;">这般</span><span style="color: black;">保留</span>到数据库的可能<span style="color: black;">便是</span>md5(123456abc),<span style="color: black;">增多</span>了破解的难度。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>黑客只要得知了该用户的salt<span style="color: black;">亦</span>能跑md5跑出来。<span style="color: black;">由于</span><span style="color: black;">此刻</span>的计算机的计算速度<span style="color: black;">已然</span>非常快了,一秒<span style="color: black;">能够</span>计算10亿次md5值,弱一点的<span style="color: black;">秘码</span>分把钟就能跑出来。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>后来<span style="color: black;">秘码</span>学上改进了hash,引进了一个概念:密钥延伸。说简单点<span style="color: black;">便是</span><span style="color: black;">增多</span>计算hash的难度(<span style="color: black;">例如</span>把<span style="color: black;">秘码</span>用md5()函数循环计算1000次),故意减慢计算hash所用的时间,以前一秒<span style="color: black;">能够</span>计算10亿次,改进后1秒只能计算100万次,速度慢了1000倍,<span style="color: black;">这般</span>,所需的时间<span style="color: black;">亦</span>就<span style="color: black;">增多</span>了1000倍。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span><span style="color: black;">针对</span><span style="color: black;">咱们</span>,怎么<span style="color: black;">运用</span>一个安全的hash计算<span style="color: black;">办法</span>?<span style="color: black;">大众</span><span style="color: black;">能够</span>翻阅emlog的源码,<span style="color: black;">能够</span>在include目录里面找到一个HashPaaword.php的文件,其实这<span style="color: black;">便是</span>个类,emlog用它来计算<span style="color: black;">秘码</span>的hash。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 这个类有一个特点,每次计算出的hash值都不<span style="color: black;">同样</span>,<span style="color: black;">因此</span>黑客<span style="color: black;">不可</span><span style="color: black;">经过</span>彩虹表等方式破解<span style="color: black;">秘码</span>,只能用这个类中一个checkpassword<span style="color: black;">办法</span>来返回用户输入<span style="color: black;">秘码</span>的正确性。而该函数又特意<span style="color: black;">增多</span>了计算hash的时间,<span style="color: black;">因此</span>黑客很难破解<span style="color: black;">她们</span>拿到的hash值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在最新的php5.5中,这种hash算法<span style="color: black;">作为</span>了一个正式的函数,以后就能<span style="color: black;">运用</span>该函数来hash<span style="color: black;">咱们</span>的<span style="color: black;">秘码</span>了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">验证码安全性</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">验证码<span style="color: black;">一般</span><span style="color: black;">是由于</span>php脚本生成的随机字符串,<span style="color: black;">经过</span>GD库的处理,制作成<span style="color: black;">照片</span>。真正的验证码字符串<span style="color: black;">保留</span>在SESSION中,<span style="color: black;">而后</span>把生成的<span style="color: black;">照片</span>展示给用户。用户填写了验证码提交后,在服务端上SESSION中的验证码进行比对。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 由此想到了我之前犯过的一个错误。验证码比对完成之后,不管是正确还是错误,我都<span style="color: black;">无</span>清理SESSION。<span style="color: black;">这般</span>产生了一个问题,一旦一个用户<span style="color: black;">第1</span>次提交验证码成功,第二次以后<span style="color: black;">再也不</span><span style="color: black;">拜访</span>生成验证码的脚本,<span style="color: black;">此时</span>候SESSION中的验证码并<span style="color: black;">无</span>更新,<span style="color: black;">亦</span><span style="color: black;">无</span>删除,<span style="color: black;">引起</span>验证码重复<span style="color: black;">运用</span>,起不到验证的<span style="color: black;">功效</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再就说到了验证码被识别的问题,wordpress<span style="color: black;">包含</span>emlog的程序我经常会借鉴,但<span style="color: black;">她们</span>所<span style="color: black;">运用</span>的验证码我却不敢恭维。<span style="color: black;">非常多</span>垃圾评论都是验证码被<span style="color: black;">设备</span>识别后产生的,<span style="color: black;">因此</span>我后来<span style="color: black;">亦</span><span style="color: black;">运用</span>了一个<span style="color: black;">繁杂</span>一点的验证码,<span style="color: black;">据述</span>是w3c<span style="color: black;">举荐</span><span style="color: black;">运用</span>的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">实质</span>运用中用到的东西<span style="color: black;">亦</span>就这么多了。这<span style="color: black;">亦</span>仅仅是在写代码中<span style="color: black;">累积</span>的<span style="color: black;">有些</span>对代码安全性的一个见解,如果<span style="color: black;">大众</span>还有更好的想法,<span style="color: black;">能够</span>和我交流。<span style="color: black;">期盼</span><span style="color: black;">大众</span><span style="color: black;">亦</span>能写出更安全的代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz/QibLP1rpwH8srtSicKnJLDzujn2dRhmc2nvqziamdvTia2Spkj8ZbIOvAWmAWWzCha73cMpM4OfqKx5SYl8Nn9HfZQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">点击上面<span style="color: black;">微X</span>号关注我</span><img src="http://mmbiz.qpic.cn/mmbiz/c22Db9HN9KgxCtyyxvZ6MNQB8Xh4lIkUibknXjCF9TN58Q7bP01nCJUo0tgUoYduUyMuS2SFjdduk85KZapmmLA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"><span style="color: black;">关注我哟</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">博主</span> 隔天</span><span style="color: black;">推送</span><span style="color: black;">php教程</span><span style="color: black;"><span style="color: black;"><span style="color: black;">,</span><span style="color: black;">php技巧</span><span style="color: black;"><span style="color: black;">,</span><span style="color: black;">php视频教程</span><span style="color: black;">,</span><span style="color: black;">MySQL</span><span style="color: black;">,</span><span style="color: black;">笔试题</span></span></span><span style="color: black;">等<span style="color: black;">许多</span><span style="color: black;">优秀</span>内容,</span><span style="color: black;">最接地气</span><span style="color: black;">、</span><span style="color: black;">重服务</span><span style="color: black;">的本地<span style="color: black;">微X</span>平台!关注<span style="color: black;">咱们</span>妥妥没错!</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(<span style="color: black;">宣传</span>合作联系QQ:<strong style="color: blue;"><span style="color: black;">2230304070</span></strong>)</span></p><strong style="color: blue;"><span style="color: black;"><span style="color: black;">宣传</span><span style="color: black;">亦</span>是一种生活</span><span style="color: black;">平台需要你</span></strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">阅读完<span style="color: black;">文案</span>后,望<span style="color: black;">大众</span>花个5秒的时间顺手点击一下底部的<span style="color: black;">宣传</span>,无需仔细阅读,无需关注,只要点击一下就好。平台需要你,<span style="color: black;">博主</span>感谢你!!</p>
可以发布外链的网站 http://www.fok120.com/
页:
[1]