CTF-【NSCTF 2015】WEB11 条件竞争

目录

简介

源代码

复现

攻击

参考


简介

条件竞争是指多进程/多线程情况下对于共享资源没有加锁,导致的问题。

源代码

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>NSCTF</title>
</head>
<body alink="#007000" background="../images/dot.gif" bgcolor="#000000" link="gold" text="#008000" vlink="#00c000">
<br>
<br>
<br>
<center>
<head><h1>请上传文件!</h1></head><br>
<br>
<br><br>
<form method="post" action="index.php" enctype="multipart/form-data">
 <input type="file" name="file" value="1111"/>
 <input type="submit" name="submit" value="upload"/>
</form>
</center>

<?php

error_reporting(0);

if(isset($_POST['submit'])){
  
  $savefile = $_FILES['file']['name'];
  $savefile = preg_replace("/\.\.|\%/", "", $savefile);
  $tempfile = $_FILES['file']['tmp_name'];
  //$savefile = preg_replace("/(php|php3|php4)(\.|$)/i", "_\\1\\2", $savefile);
  $savefile = preg_replace("/(php|phtml|php3|php4|jsp|exe|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i", "_\\1\\2", $savefile);
  $savefile = 'upload/'.$savefile;

  if(move_uploaded_file($tempfile,$savefile)){
    
    //$filename = substr($savefile,0,strlen($savefile)-1);
    $filename = $savefile;
    
    if(file_exists($filename) && ( (substr($savefile, -5) == '.php5') )){
        file_put_contents($filename, "flag:{NSCTF_8f0fc74ddf786103ed56d20af3bf269}");
        sleep(0.5);
        unlink($filename);
        exit('上传成功,文件地址为:'.$savefile."<br>"."但是系统检测到恶意上传立马又被删了~");
      }else{
    unlink($filename);
        exit('上传成功,文件地址为:'.$savefile."<br>");
      }
  }else{
    exit('上传失败~'."<br>");
  }

}

function upload($src,$dst){
 
  if(move_uploaded_file($src,$dst)){
      return true;
  }

  return false;

}
?>

我进行了一点修改,背景图片改为了同级目录,改为了png,表单action为空,不再跳转,方便你们看。

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>NSCTF</title>
</head>
<body alink="#007000" background="dot.png" bgcolor="#000000" link="gold" text="#008000" vlink="#00c000">
<br>
<br>
<br>
<center>
<head><h1>请上传文件!</h1></head><br>
<br>
<br><br>
<form method="post" action="" enctype="multipart/form-data">
 <input type="file" name="file" value="1111"/>
 <input type="submit" name="submit" value="upload"/>
</form>
</center>

<?php

error_reporting(0);

if(isset($_POST['submit'])){
  
  $savefile = $_FILES['file']['name'];
  $savefile = preg_replace("/\.\.|\%/", "", $savefile);
  $tempfile = $_FILES['file']['tmp_name'];
  $savefile = preg_replace("/(php|phtml|php3|php4|jsp|exe|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i", "_\\1\\2", $savefile);
  $savefile = 'upload/'.$savefile;

  if(move_uploaded_file($tempfile,$savefile)){
    
    $filename = $savefile;
    
    if(file_exists($filename) && ( (substr($savefile, -5) == '.php5') )){
        file_put_contents($filename, "flag:{NSCTF_8f0fc74ddf786103ed56d20af3bf269}");
        sleep(0.5);
        unlink($filename);
        exit('上传成功,文件地址为:'.$savefile."<br>"."但是系统检测到恶意上传立马又被删了~");
      }else{
    unlink($filename);
        exit('上传成功,文件地址为:'.$savefile."<br>");
      }
  }else{
    exit('上传失败~'."<br>");
  }

}

function upload($src,$dst){
 
  if(move_uploaded_file($src,$dst)){
      return true;
  }

  return false;

}
?>

正则表达式分析

"/\.\.|\%/",\表示转义,|表示或,故可匹配".."或者"%",配合preg_replace替换为空。

"/(php|phtml|php3|php4|jsp|exe|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i",()内包含一个正则表达式,|表示或,\表示转义,i表示匹配时不区分大小写,故可匹配两个()加上"."或"$",配合preg_replace替换为"_()()",可能表达的不清楚,举个例子,"xxx.php"会被改为"xxx._php",不会写入flag,xxx.php.php3会被改为"xxx._php_php3"。

后面if语句可以看到,文件后缀是".php5"就可以了。

复现

将上述代码保存到本地,例如,phpStudy的WWW目录下(D:\phpStudy\WWW\CTFs\NSCTF\2015WEB_condition_race.php)。同级目录下建立一个upload文件夹,放一张dot.png。

网站结构

打开phpStudy,使用浏览器访问。

未上传时

 

攻击

通过正则表达式,可知使用后缀为.php5的文件可绕过,上传成功截图如下:

上传文件成功

使用BP抓包

POST /CTFs/NSCTF/index.php HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------132767563241187676712547406197
Content-Length: 353
Origin: http://127.0.0.1
Connection: close
Referer: http://127.0.0.1/CTFs/NSCTF/2015WEB_condition_race.php
Upgrade-Insecure-Requests: 1

-----------------------------132767563241187676712547406197
Content-Disposition: form-data; name="file"; filename="flag.php5"
Content-Type: application/octet-stream


-----------------------------132767563241187676712547406197
Content-Disposition: form-data; name="submit"

upload
-----------------------------132767563241187676712547406197--

在Instruder模块进行条件竞争攻击,即不断上传文件,导致系统来不及删,在攻击过程中服务器一直存在文件。

刷新发现
打开查看

BP设置可看下方参考

参考

网络安全-条件竞争(《CTF特训营》第7章复现)

更多内容查看:网络安全-自学笔记

喜欢本文的请动动小手点个赞,收藏一下,有问题请下方评论,转载请注明出处,并附有原文链接,谢谢!如有侵权,请及时联系。如果您感觉有所收获,自愿打赏,可选择支付宝18833895206(小于),您的支持是我不断更新的动力。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lady_killer9

感谢您的打赏,我会加倍努力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值