Jimmy's blog
Difficulty
Hard
Points
400
Description
The technology is always evolving, so why do we still stick with password-based authentication? That makes no sense! That’s why I designed my own password-less login system. I even open-sourced it for everyone interested, how nice of me!
Quick Analysis
From the attached source code.
Content of index.js
Content of article.ejs
Content of edit.ejs
Content of utils.js
NGINX configuration
Analyzing index.js
index.js
The
index.js
requiresutil.js
file.The endpoint
/register
requires a username only and returns a key for authentication.The endpoint
/login
requires a username and akey
file.The flag passed to
article.ejs
viewres.render("article", { article: article, session: req.session, flag: process.env.FLAG });
.The flag is not rendered via
/article
endpoint based on the content of thearticle.ejs
.The flag passed to
edit.js
viewres.render("edit", { article: article, session: req.session, flag: process.env.FLAG });
.The flag is rendered via
GET
/edit?id=<INTEGER>
endpoint based on the content of theedit.ejs
:<p class="mb-5 text-center"><%= flag %></p>
The endpoint
/edit
requires an admin sessionif (!req.session.admin) return res.sendStatus(401);
for both methodsGET
andPOST
.The
POST
endpoint/edit?id=<string>
is vulnerable, where you could path traverse via theid
parameter./articles/<id>
and write content to the traversal path via the articlePOST
parameter.fs.writeFileSync(path.join("articles", req.query.id), req.body.article.replace(/\r/g, ""));
Analyzing utils.js
utils.js
the utils file registered an administrator user with
jimmy_jammy
as a username and a random key with 1024 bytesregister("jimmy_jammy", 1);
.the register function is vulnerable to account takeover, where you could traversal and overwrite an existing user's key.
Analyzing NGINX configuration [1]
the flag is replaced with
oof, that was close, glad i was here to save the day
via NGINXsub_filter
. The ngx_http_sub_module module is a filter that modifies a response by replacing one specified string by another. This module is not built by default, it should be enabled with the --with-http_sub_module configuration parameter.
Exploitation
Register
./jimmy_jammy
to overwrite the actualjimmy_jammy
key.Login with
jimmy_jammy
and use the key that we obtained via the registration function.Edit the
edit.js
view to<%= btoa(flag) %>
which encodes the flag to base64 viaPOST
/edit?id=../views/edit.js
article=<%25%3d+btoa(flag)+%25>
to bypass the NGINX sub_filter.
The flag
Navigate to the endpoint /edit?id=1
to get the base64 flag
References
http://nginx.org/en/docs/http/ngx_http_sub_module.html#sub_filter
Last updated