CTF文件上传漏洞总结

admin 2022年6月14日22:52:28安全文章CTF文件上传漏洞总结已关闭评论8 views12105字阅读40分21秒阅读模式

本文使用环境来自于:https://github.com/c0ny1/upload-labs

 

客户端

 

js检查

一般都是在网页上写一段javascript脚本,校验上传文件的后缀名,有白名单形式也有黑名单形式。

查看源代码可以看到有如下代码对上传文件类型进行了限制:

  1. <script type="text/javascript"> function checkFile() {

  2. var file = document.getElementsByName('upload_file')[0].value;

  3. if (file == null || file == "") {

  4. alert("请选择要上传的文件!");

  5. return false;

  6. var allow_ext = ".jpg|.png|.gif";

  7. var ext_name = file.substring(file.lastIndexOf("."));

  8. if (allow_ext.indexOf(ext_name) == -1) {

  9. var errMsg = "该文件不允许上传,请上传" + allow_ext + "类型的文件,当前文件类型为:" + ext_name;

  10. alert(errMsg);

  11. return false;

我们可以看到对上传文件类型进行了限制。

绕过方法

1.我们直接删除代码中onsubmit事件中关于文件上传时验证上传文件的相关代码即可。

或者可以不加载所有js,还可以将html源码copy一份到本地,然后对相应代码进行修改,本地提交即可。

2.burp改包,由于是js验证,我们可以先将文件重命名为js允许的后缀名,在用burp发送数据包时候改成我们想要的后缀。

即可上传成功:

 

服务端

黑名单

特殊可解析后缀

  1. if (file_exists(UPLOAD_PATH)) {

  2. $deny_ext = array('.asp','.aspx','.php','.jsp');

  3. $file_name = trim($_FILES['upload_file']['name']);

  4. $file_name = deldot($file_name);//删除文件名末尾的点

  5. $file_ext = strrchr($file_name, '.');

  6. $file_ext = strtolower($file_ext); //转换为小写

  7. $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA

  8. $file_ext = trim($file_ext); //收尾去空

  9. if(!in_array($file_ext, $deny_ext)) {

  10. $temp_file = $_FILES['upload_file']['tmp_name'];

  11. $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;

  12. if (move_uploaded_file($temp_file,$img_path)) {

  13. $is_upload = true;

  14. $msg = '上传出错!';

  15. $msg = '不允许上传.asp,.aspx,.php,.jsp后缀文件!';

  16. $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';

这里做了黑名单处理,我们可以通过特殊可解析后缀进行绕过。

绕过方法

之前在https://www.jianshu.com/p/1ccbab572974中总结过,这里不再赘述,可以使用php3,phtml等绕过。

 

上传.htaccess

  1. if (isset($_POST['submit'])) {

  2. if (file_exists(UPLOAD_PATH)) {

  3. $deny_ext = array(".php",".php5",".php4",".php3",".php2","php1",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2","pHp1",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf");

  4. $file_name = trim($_FILES['upload_file']['name']);

  5. $file_name = deldot($file_name);//删除文件名末尾的点

  6. $file_ext = strrchr($file_name, '.');

  7. $file_ext = strtolower($file_ext); //转换为小写

  8. $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA

  9. $file_ext = trim($file_ext); //收尾去空

  10. if (!in_array($file_ext, $deny_ext)) {

  11. $temp_file = $_FILES['upload_file']['tmp_name'];

  12. $img_path = UPLOAD_PATH.'/'.$file_name;

  13. if (move_uploaded_file($temp_file, $img_path)) {

  14. $is_upload = true;

  15. $msg = '上传出错!';

  16. $msg = '此文件不允许上传!';

  17. $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';

我们发现黑名单限制了很多后缀名,但是没有限制.htaccess

.htaccess文件是Apache服务器中的一个配置文件,它负责相关目录下的网页配置.通过htaccess文件,可以实现:网页301重定向、自定义404页面、改变文件扩展名、允许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档等功能。

绕过方法

我们需要上传一个.htaccess文件,内容为:

SetHandler application/x-httpd-php

这样所有的文件都会解析为php,接下来上传图片马即可

 

后缀大小写绕过

  1. if (isset($_POST['submit'])) {

  2. if (file_exists(UPLOAD_PATH)) {

  3. $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");

  4. $file_name = trim($_FILES['upload_file']['name']);

  5. $file_name = deldot($file_name);//删除文件名末尾的点

  6. $file_ext = strrchr($file_name, '.');

  7. $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA

  8. $file_ext = trim($file_ext); //首尾去空

  9. if (!in_array($file_ext, $deny_ext)) {

  10. $temp_file = $_FILES['upload_file']['tmp_name'];

  11. $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;

  12. if (move_uploaded_file($temp_file, $img_path)) {

  13. $is_upload = true;

  14. $msg = '上传出错!';

  15. $msg = '此文件类型不允许上传!';

  16. $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';

我们发现对.htaccess也进行了检测,但是没有对大小写进行统一。

绕过方法

后缀名改为PHP即可

 

空格绕过

  1. if (isset($_POST['submit'])) {

  2. if (file_exists(UPLOAD_PATH)) {

  3. $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");

  4. $file_name = $_FILES['upload_file']['name'];

  5. $file_name = deldot($file_name);//删除文件名末尾的点

  6. $file_ext = strrchr($file_name, '.');

  7. $file_ext = strtolower($file_ext); //转换为小写

  8. $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA

  9. if (!in_array($file_ext, $deny_ext)) {

  10. $temp_file = $_FILES['upload_file']['tmp_name'];

  11. $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;

  12. if (move_uploaded_file($temp_file,$img_path)) {

  13. $is_upload = true;

  14. $msg = '上传出错!';

  15. $msg = '此文件不允许上传';

  16. $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';

黑名单没有对文件中的空格进行处理,可在后缀名中加空格绕过。

绕过方法

CTF文件上传漏洞总结
 

点绕过

  1. if (isset($_POST['submit'])) {

  2. if (file_exists(UPLOAD_PATH)) {

  3. $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");

  4. $file_name = trim($_FILES['upload_file']['name']);

  5. $file_ext = strrchr($file_name, '.');

  6. $file_ext = strtolower($file_ext); //转换为小写

  7. $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA

  8. $file_ext = trim($file_ext); //首尾去空

  9. if (!in_array($file_ext, $deny_ext)) {

  10. $temp_file = $_FILES['upload_file']['tmp_name'];

  11. $img_path = UPLOAD_PATH.'/'.$file_name;

  12. if (move_uploaded_file($temp_file, $img_path)) {

  13. $is_upload = true;

  14. $msg = '上传出错!';

  15. $msg = '此文件类型不允许上传!';

  16. $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';

windows会对文件中的点进行自动去除,所以可以在文件末尾加点绕过,不再赘述。

::$DATA绕过

同windows特性,可在后缀名中加” ::$DATA”绕过,不再赘述。

路径拼接绕过

  1. if (isset($_POST['submit'])) {

  2. if (file_exists(UPLOAD_PATH)) {

  3. $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");

  4. $file_name = trim($_FILES['upload_file']['name']);

  5. $file_name = deldot($file_name);//删除文件名末尾的点

  6. $file_ext = strrchr($file_name, '.');

  7. $file_ext = strtolower($file_ext); //转换为小写

  8. $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA

  9. $file_ext = trim($file_ext); //首尾去空

  10. if (!in_array($file_ext, $deny_ext)) {

  11. $temp_file = $_FILES['upload_file']['tmp_name'];

  12. $img_path = UPLOAD_PATH.'/'.$file_name;

  13. if (move_uploaded_file($temp_file, $img_path)) {

  14. $is_upload = true;

  15. $msg = '上传出错!';

  16. $msg = '此文件类型不允许上传!';

  17. $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';

这里对文件名进行了处理,删除了文件名末尾的点,并且把处理过的文件名拼接到路径中。

绕过方法

这里我们可以构造文件名1.PHP. . (点+空格+点),经过处理后,文件名变成1.PHP.,即可绕过。

双写绕过

  1. if (file_exists(UPLOAD_PATH)) {

  2. $deny_ext = array("php","php5","php4","php3","php2","html","htm","phtml","pht","jsp","jspa","jspx","jsw","jsv","jspf","jtml","asp","aspx","asa","asax","ascx","ashx","asmx","cer","swf","htaccess");

  3. $file_name = trim($_FILES['upload_file']['name']);

  4. $file_name = str_ireplace($deny_ext,"", $file_name);

  5. $temp_file = $_FILES['upload_file']['tmp_name'];

  6. $img_path = UPLOAD_PATH.'/'.$file_name;

  7. if (move_uploaded_file($temp_file, $img_path)) {

  8. $is_upload = true;

  9. $msg = '上传出错!';

  10. $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';

绕过方法

这里我们可以看到将文件名替换为空,我们可以采用双写绕过:1.pphphp

CTF文件上传漏洞总结

 

白名单

MIME检查

  1. if (isset($_POST['submit'])) {

  2. if (file_exists(UPLOAD_PATH)) {

  3. if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif')) {

  4. $temp_file = $_FILES['upload_file']['tmp_name'];

  5. $img_path = UPLOAD_PATH . '/' . $_FILES['upload_file']['name'];

  6. if (move_uploaded_file($temp_file, $img_path)) {

  7. $is_upload = true;

  8. $msg = '上传出错!';

  9. $msg = '文件类型不正确,请重新上传!';

  10. $msg = UPLOAD_PATH.'文件夹不存在,请手工创建!';

绕过方法

这里检查Content-type,我们burp抓包修改即可绕过:

 

%00 截断

  1. if(isset($_POST['submit'])){

  2. $ext_arr = array('jpg','png','gif');

  3. $file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);

  4. if(in_array($file_ext,$ext_arr)){

  5. $temp_file = $_FILES['upload_file']['tmp_name'];

  6. $img_path = $_GET['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;

  7. if(move_uploaded_file($temp_file,$img_path)){

  8. $is_upload = true;

  9. $msg = '上传出错!';

  10. $msg = "只允许上传.jpg|.png|.gif类型文件!";

$img_path直接拼接,因此可以利用%00截断绕过

绕过方法

然后直接访问/upload/1.php即可

00截断(post)

  1. if(isset($_POST['submit'])){

  2. $ext_arr = array('jpg','png','gif');

  3. $file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);

  4. if(in_array($file_ext,$ext_arr)){

  5. $temp_file = $_FILES['upload_file']['tmp_name'];

  6. $img_path = $_POST['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;

  7. if(move_uploaded_file($temp_file,$img_path)){

  8. $is_upload = true;

  9. $msg = "上传失败";

  10. $msg = "只允许上传.jpg|.png|.gif类型文件!";

save_path是通过post传进来的,还是利用00截断,但这次需要在二进制中进行修改,因为post不会像get对%00进行自动解码。

绕过方法

CTF文件上传漏洞总结
接下来访问1.php即可

文件内容检查

文件幻数检测

主要是检测文件内容开始处的文件幻数,比如图片类型的文件幻数如下,
要绕过jpg 文件幻数检测就要在文件开头写上下图的值:

Value = FF D8 FF E0 00 10 4A 46 49 46

要绕过gif 文件幻数检测就要在文件开头写上下图的值

Value = 47 49 46 38 39 61
要绕过png 文件幻数检测就要在文件开头写上下面的值

Value = 89 50 4E 47

然后在文件幻数后面加上自己的一句话木马代码就行了

文件相关信息检测

图像文件相关信息检测常用的就是getimagesize()函数

只需要把文件头部分伪造好就ok 了,就是在幻数的基础上还加了一些文件信息

有点像下面的结构

GIF89a
(...some binary data for image...)
<?php phpinfo(); ?>
(... skipping the rest of binary data ...)

本次环境中的文件头检测,getimagesize,php_exif都可以用图片马绕过:

copy normal.jpg /b + shell.php /a webshell.jpg

文件加载检测

一般是调用API 或函数去进行文件加载测试,常见的是图像渲染测试,甚至是进行二次渲染(过滤效果几乎最强)。对渲染/加载测试的攻击方式是代码注入绕过,对二次渲染的攻击方式是攻击文件加载器自身。

对渲染/加载测试攻击- 代码注入绕过

可以用图像处理软件对一张图片进行代码注入

用winhex 看数据可以分析出这类工具的原理是

在不破坏文件本身的渲染情况下找一个空白区进行填充代码,一般会是图片的注释区

对于渲染测试基本上都能绕过,毕竟本身的文件结构是完整的

 

二次渲染

imagecreatefromjpeg二次渲染它相当于是把原本属于图像数据的部分抓了出来,再用自己的API 或函数进行重新渲染在这个过程中非图像数据的部分直接就隔离开了

  1. if (isset($_POST['submit'])){

  2. // 获得上传文件的基本信息,文件名,类型,大小,临时文件路径

  3. $filename = $_FILES['upload_file']['name'];

  4. $filetype = $_FILES['upload_file']['type'];

  5. $tmpname = $_FILES['upload_file']['tmp_name'];

  6. $target_path=UPLOAD_PATH.basename($filename);

  7. $fileext= substr(strrchr($filename,"."),1);

  8. //判断文件后缀与类型,合法才进行上传操作

  9. if(($fileext == "jpg") && ($filetype=="image/jpeg")){

  10. if(move_uploaded_file($tmpname,$target_path))

  11. //使用上传的图片生成新的图片

  12. $im = imagecreatefromjpeg($target_path);

  13. if($im == false){

  14. $msg = "该文件不是jpg格式的图片!";

  15. @unlink($target_path);

  16. //给新图片指定文件名

  17. srand(time());

  18. $newfilename = strval(rand()).".jpg";

  19. $newimagepath = UPLOAD_PATH.$newfilename;

  20. imagejpeg($im,$newimagepath);

  21. //显示二次渲染后的图片(使用用户上传图片生成的新图片)

  22. $img_path = UPLOAD_PATH.$newfilename;

  23. @unlink($target_path);

  24. $is_upload = true;

  25. $msg = "上传出错!";

  26. }else if(($fileext == "png") && ($filetype=="image/png")){

  27. if(move_uploaded_file($tmpname,$target_path))

  28. //使用上传的图片生成新的图片

  29. $im = imagecreatefrompng($target_path);

  30. if($im == false){

  31. $msg = "该文件不是png格式的图片!";

  32. @unlink($target_path);

  33. //给新图片指定文件名

  34. srand(time());

  35. $newfilename = strval(rand()).".png";

  36. $newimagepath = UPLOAD_PATH.$newfilename;

  37. imagepng($im,$newimagepath);

  38. //显示二次渲染后的图片(使用用户上传图片生成的新图片)

  39. $img_path = UPLOAD_PATH.$newfilename;

  40. @unlink($target_path);

  41. $is_upload = true;

  42. $msg = "上传出错!";

  43. }else if(($fileext == "gif") && ($filetype=="image/gif")){

  44. if(move_uploaded_file($tmpname,$target_path))

  45. //使用上传的图片生成新的图片

  46. $im = imagecreatefromgif($target_path);

  47. if($im == false){

  48. $msg = "该文件不是gif格式的图片!";

  49. @unlink($target_path);

  50. //给新图片指定文件名

  51. srand(time());

  52. $newfilename = strval(rand()).".gif";

  53. $newimagepath = UPLOAD_PATH.$newfilename;

  54. imagegif($im,$newimagepath);

  55. //显示二次渲染后的图片(使用用户上传图片生成的新图片)

  56. $img_path = UPLOAD_PATH.$newfilename;

  57. @unlink($target_path);

  58. $is_upload = true;

  59. $msg = "上传出错!";

  60. $msg = "只允许上传后缀为.jpg|.png|.gif的图片文件!";

本关综合判断了后缀名、content-type,以及利用imagecreatefromgif判断是否为gif图片,最后再做了一次二次渲染。

绕过方法

得去找图片经过GD库转化后没有改变的部分,再将未改变的部分修改为相应的php代码。

 

条件竞争

  1. if(isset($_POST['submit'])){

  2. $ext_arr = array('jpg','png','gif');

  3. $file_name = $_FILES['upload_file']['name'];

  4. $temp_file = $_FILES['upload_file']['tmp_name'];

  5. $file_ext = substr($file_name,strrpos($file_name,".")+1);

  6. $upload_file = UPLOAD_PATH . '/' . $file_name;

  7. if(move_uploaded_file($temp_file, $upload_file)){

  8. if(in_array($file_ext,$ext_arr)){

  9. $img_path = UPLOAD_PATH . '/'. rand(10, 99).date("YmdHis").".".$file_ext;

  10. rename($upload_file, $img_path);

  11. $is_upload = true;

  12. $msg = "只允许上传.jpg|.png|.gif类型文件!";

  13. unlink($upload_file);

这里先将文件上传到服务器,然后通过rename修改名称,再通过unlink删除文件,因此可以通过条件竞争的方式在unlink之前,访问webshell。

绕过方法

然后不断访问webshell:

上传成功。

参考链接:

  1. https://blog.csdn.net/Kevinhanser/article/details/81613003
  2. https://secgeek.net/bookfresh-vulnerability/
  3. https://xz.aliyun.com/t/2435

作者:Echocipher
链接:https://www.jianshu.com/p/74ca4e884645
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

特别标注: 本站(CN-SEC.COM)所有文章仅供技术研究,若将其信息做其他用途,由用户承担全部法律及连带责任,本站不承担任何法律及连带责任,请遵守中华人民共和国安全法.
  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年6月14日22:52:28
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                  CTF文件上传漏洞总结 http://cn-sec.com/archives/1116089.html