弹簧安全:如何实施蛮力检测(BFD)?
-
30-09-2019 - |
题
我的Web应用程序安全由 春季安全性 3.02但是我找不到任何开箱即用的蛮力检测支持。
我想实施一些应用程序级BFD保护。例如,通过存储数据库(JPA)中每个用户的登录尝试失败。然后,受攻击的用户帐户可以通过电子邮件获得锁定期限或强制帐户重新激活。
通过Spring Security实施此功能的最佳方法是什么?任何身体都有示例代码或最佳实践吗?
解决方案
滚动自己的BFD并不难。与Spring Security 3.0一样,您只需添加应用程序侦听器(谢谢) 斯蒂芬c 指向我指向正确的方向)。
当身份验证失败出现时,将调用此听众:
@Component
public class AuthenticationFailureListener
implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
@Autowired
private UserDao userDao;
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent ev) {
String username = ev.getAuthentication().getName();
User user = userDao.find("name", username);
if (user != null) { // only for existing users
user.reportLoginFailure();
userDao.commit();
}
}
}
现在,每个身份验证故障现在都将告知用户。例如,用户会增加身份验证故障计数器,并在达到一定的阈值时将其停用。
正确身份验证用户后,以下侦听器将通知用户(例如,谁可以重置其身份验证故障计数器):
@Component
public class AuthenticationSuccessEventListener
implements ApplicationListener<AuthenticationSuccessEvent>{
@Autowired
private UserDao userDao;
public void onApplicationEvent(AuthenticationSuccessEvent event) {
String username = event.getAuthentication().getName();
User user = userDao.find("name", username);
user.reportLoginOK();
userDao.commit();
}
}
上面的听众将不需要其他XML配置,并且会在春季自动拾取(如果它们在Spring组件扫描包中)。
根据您的交易配置,如果同时发生交易,该解决方案可能会错过一些失败的登录计数。如果您使用单个更新查询更新计数器而不是加载用户然后保存更改,则可以防止这种情况。
上面的侦听器也可以扩展以检测其他BDF模式,例如,正在对(随机)用户名进行扫描的单个IP。
其他提示
您还应该知道,锁定受攻击的帐户意味着使您的服务可点缀。
众所周知的例子是:您提供拍卖服务,鲍勃想购买一些职位并攻击爱丽丝的帐户,因此,爱丽丝在鲍勃获得职位时没有下注,而是试图恢复她的帐户。即使是临时(5秒)的锁,也可能阻止爱丽丝根据需要使用服务。
检测蛮力攻击(密码猜测)的正常方法是使身份验证方案日志失败登录尝试,并有单独的应用程序尝试检测日志文件中的可疑模式。我想可以关闭循环并让探测器采取行动将帐户锁定在攻击下等。
有一个例子 这一页.