公私钥泄露 ¶
首先看代码,发现公私钥都放在了 public 文件夹下面,nodejs 中可以直接访问此文件
/* GET home page. */
router.get('/', function(req, res, next) {
res.type('html');
var privateKey = fs.readFileSync(process.cwd()+'//public//private.key');
var token = jwt.sign({ user: 'user' }, privateKey, { algorithm: 'RS256' });
res.cookie('auth',token);
res.end('where is flag?');
});
router.post('/',function(req,res,next){
var flag="flag_here";
res.type('html');
var auth = req.cookies.auth;
var cert = fs.readFileSync(process.cwd()+'//public/public.key'); // get public key
jwt.verify(auth, cert, function(err, decoded) {
if(decoded.user==='admin'){
res.end(flag);
}else{
res.end('you are not admin');
}
});
});
因此我们在 url 后面分别拼接 private.key 与 public.key 将公私钥下载下来
然后我们看看解码结果
{
"alg": "RS256",
"typ": "JWT"
}
{
"user": "user",
"iat": 1609250966
}
发现果然是 RS256
RS256 (采用 SHA-256 的 RSA 签名) 是一种非对称算法, 它使用公共 / 私钥对: 标识提供方采用私钥生成签名, JWT 的使用方获取公钥以验证签名。由于公钥 (与私钥相比) 不需要保护, 因此大多数标识提供方使其易于使用方获取和使用 (通常通过一个元数据 URL)。
在 jwt.io 中分别把公私钥复制进去,然后替换 Cookie 即可