加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长资讯 > 评论 > 正文

浅谈PHP安全规范

发布时间:2018-10-15 10:42:58 所属栏目:评论 来源:littlepotato
导读:【新品产上线啦】51CTO播客,随时随地,碎片化学习 一、前言 php因天生支持web应用的开发,以其简单易学,开发效率高而备受喜爱。使其占据了大片的市

(4) Impossible level

  1. <?php  
  2.  
  3. // The page we wish to display  
  4. $file = $_GET[ 'page' ];  
  5.  
  6. // Only allow include.php or file{1..3}.php  
  7. if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" ) {  
  8.     // This isn't the page we want!  
  9.     echo "ERROR: File not found!";  
  10.     exit;  
  11. }  
  12.  
  13. ?> 

这是相当的白名单,你赢了。但是想要文件量巨大的场景中维护这么一张白名单有点不太理智,所以笔者这种硬编码的方式不太常用。

6. Upload file

上传漏洞经常可以用来上传任意代码泄露系统信息,如,甚至可以直接上传webshell,拿下服务器权限,所以这个漏洞是十分严重的。

(1) Low level

  1. <?php  
  2.  
  3. if( isset( $_POST[ 'Upload' ] ) ) {  
  4.     // Where are we going to be writing to?  
  5.     $target_path  = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";  
  6.     $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );  
  7.  
  8.     // Can we move the file to the upload folder?  
  9.     if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {  
  10.         // No  
  11.         echo '<pre>Your image was not uploaded.</pre>';  
  12.     }  
  13.     else {  
  14.         // Yes!  
  15.         echo "<pre>{$target_path} succesfully uploaded!</pre>";  
  16.     }  
  17. }  
  18.  
  19. ?> 

可以看到上面的代码对用户上传的文件($_FILE全局数组的形式)没有进行任何的验证操作,就直接将其move到了upload目录,这是相当危险的操作,攻击者可以毫无忌惮的随意日。

(2) Medium level

  1. <?php  
  2.  
  3. if( isset( $_POST[ 'Upload' ] ) ) {  
  4.     // Where are we going to be writing to?  
  5.     $target_path  = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";  
  6.     $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );  
  7.  
  8.     // File information  
  9.     $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];  
  10.     $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];  
  11.     $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];  
  12.  
  13.     // Is it an image?  
  14.     if( ( $uploaded_type == "image/jpeg" || $uploaded_type == "image/png" ) &&  
  15.         ( $uploaded_size < 100000 ) ) { #只判断了MIME 
  16.  
  17.         // Can we move the file to the upload folder?  
  18.         if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {  
  19.             // No  
  20.             echo '<pre>Your image was not uploaded.</pre>';  
  21.         }  
  22.         else {  
  23.             // Yes!  
  24.             echo "<pre>{$target_path} succesfully uploaded!</pre>";  
  25.         }  
  26.     }  
  27.     else {  
  28.         // Invalid file  
  29.         echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';  
  30.     }  
  31. }  
  32.  
  33. ?> 

上面的代码加入了MIME判断,所谓的MIME判断是在请求头中的一个字段,用来指示文件类型,方便服务器进行对应的处理,只要抓包就可以随意修改,达到欺骗服务器的目的。(更多的解释可以查看:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读