Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#java# 规范 可预测的伪随机数生成器 增补修订建议 #15

Open
k4n5ha0 opened this issue May 24, 2021 · 0 comments
Open

#java# 规范 可预测的伪随机数生成器 增补修订建议 #15

k4n5ha0 opened this issue May 24, 2021 · 0 comments

Comments

@k4n5ha0
Copy link

k4n5ha0 commented May 24, 2021

当在某些安全关键的上下文中使用可预测的随机值时,可能会导致漏洞。

例如,当该值用作:

  • CSRF令牌:可预测的令牌可能导致CSRF攻击,因为攻击者将知道令牌的值
  • 密码重置令牌(通过电子邮件发送):可预测的密码令牌可能会导致帐户被接管,因为攻击者会猜测“更改密码”表单的URL
  • 任何其他敏感值

脆弱代码:

String generateSecretToken() {
	Random r = new Random();
	return Long.toHexString(r.nextLong());
}

解决方案:

替换 java.util.Random 使用强度更高的 java.security.SecureRandom

import org.apache.commons.codec.binary.Hex;

String generateSecretToken() {
	SecureRandom secRandom = new SecureRandom();

	byte[] result = new byte[32];
	secRandom.nextBytes(result);
	return Hex.encodeHexString(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant