简体   繁体   English

Log4j2 安全热点问题

[英]Log4j2 security hotspot issue

This is the code for configuring log4j2.xml file.这是配置 log4j2.xml 文件的代码。 The problem is that sonar is showing security hotspot issue at setConfiguration.问题是声纳在 setConfiguration 显示安全热点问题。 How to avoid it?如何避免?

String propFile = "log4j2.xml";

LoggerContext logcontext = (org.apache.logging.log4j.core.LoggerContext) 
LogManager.getContext(false);
File logFile = new File(propFile);

logcontext.setConfigLocation(logFile.toURI());

Sonar is showing security hotspot issue.声纳显示安全热点问题。

It is not an issue.这不是问题。 It is Sonar advising you that you need to review that section of code for possible security problems. Sonar 建议您需要检查该代码部分是否存在可能的安全问题。

This is what theSonarQube documentation says about Security Hotspots:这就是SonarQube 文档中关于安全热点的内容:

What is a Security Hotspot?什么是安全热点?

A Security Hotspot highlights a security-sensitive piece of code that the developer needs to review.安全热点突出显示开发人员需要审查的安全敏感代码段。 Upon review, you'll either find there is no threat or you need to apply a fix to secure the code.经过审查,您会发现没有威胁,或者您需要应用修复程序来保护代码。

Another way of looking at hotspots may be the concept of defense in depth in which several redundant protection layers are placed in an application so that it becomes more resilient in the event of an attack.查看热点的另一种方法可能是深度防御的概念,其中在应用程序中放置了几个冗余保护层,以便在发生攻击时变得更有弹性。

Vulnerability or Hotspot?漏洞还是热点?

The main difference between a hotspot and a vulnerability is the need of a review before deciding whether to apply a fix:热点和漏洞之间的主要区别是在决定是否应用修复之前需要进行审查:

  • With a Hotspot, a security-sensitive piece of code is highlighted, but the overall application security may not be impacted.使用热点,一段安全敏感的代码会被突出显示,但整体应用程序的安全性可能不会受到影响。 It's up to the developer to review the code to determine whether or not a fix is needed to secure the code.由开发人员检查代码以确定是否需要修复来保护代码。
  • With a vulnerability, a problem that impacts the application's security has been discovered that needs to be fixed immediately.通过漏洞,发现了一个影响应用程序安全的问题,需要立即修复。

An example of a hotspot is the RSPEC-2092 where the use of cookie secure flag is recommended to prevent cookies from being sent over non-HTTPS connections but a review is needed because:热点的一个示例是 RSPEC-2092,其中建议使用 cookie 安全标志来防止通过非 HTTPS 连接发送 cookie,但需要进行审查,因为:

  • HTTPS is the main protection against MITM attacks and so the secure flag is only an additional protection in case of some failures of network security. HTTPS 是针对 MITM 攻击的主要保护措施,因此安全标志只是在某些网络安全故障的情况下提供的额外保护。
  • The cookie may be designed to be sent everywhere (non-HTTPS websites included) because it's a tracking cookie or similar.该 cookie 可能被设计为发送到任何地方(包括非 HTTPS 网站),因为它是一个跟踪 cookie 或类似的。

With hotspots we try to give some freedom to users and to educate them on how to choose the most relevant/appropriate protections depending on the context (budget, threats, etc).对于热点,我们尝试为用户提供一些自由,并教育他们如何根据上下文(预算、威胁等)选择最相关/最合适的保护措施。


In this case, the Hotspot message says:在这种情况下,热点消息​​说:

"Make sure that this logger's configuration is safe. Configuring loggers is security-sensitive." “确保此记录器的配置是安全的。配置记录器是安全敏感的。” java:S4792爪哇:S4792

It is saying ... make sure that you are loading the logger configurations from a safe place;它是说...确保您从安全的地方加载记录器配置; eg somewhere that is protected so that "bad actors" (hackers, unauthorized users, etc) can't read (or worse) change the logging config.例如,某个受保护的地方,以便“不良行为者”(黑客、未经授权的用户等)无法读取(或更糟)更改日志配置。

If you don't have a good reason to configure Log4j2 programmatically, don't to it that way.如果您没有充分的理由以编程方式配置 Log4j2,请不要那样做。 Use the Log4j2 automatic configuration mechanism(s) instead.请改用 Log4j2 自动配置机制。

First, please read Stephen C answer, as it provides the documented explanation of what a Security Hotspot is.首先,请阅读 Stephen C 的回答,因为它提供了关于什么是安全热点的文档说明。

Second, this is from the documentation Sonar provides about the specific SH in particular:其次,这来自 Sonar 提供的关于特定 SH 的文档

Ask Yourself Whether问问自己是否

  • unauthorized users might have access to the logs, either because they are stored in an insecure location or because the application gives access to them.未经授权的用户可能有权访问日志,因为它们存储在不安全的位置,或者因为应用程序允许访问它们。 the logs contain sensitive information on a production server.日志包含生产服务器上的敏感信息。 This can happen when the logger is in debug mode.当记录器处于调试模式时,可能会发生这种情况。
  • the log can grow without limit.日志可以无限增长。 This can happen when additional information is written into logs every time a user performs an action and the user can perform the action as many times as he/she wants.当用户每次执行操作时将附加信息写入日志并且用户可以根据需要多次执行操作时,就会发生这种情况。
  • the logs do not contain enough information to understand the damage an attacker might have inflicted.日志不包含足够的信息来了解攻击者可能造成的损害。 The loggers mode (info, warn, error) might filter out important information.记录器模式(信息、警告、错误)可能会过滤掉重要信息。 They might not print contextual information like the precise time of events or the server hostname.他们可能不会打印上下文信息,例如事件的精确时间或服务器主机名。
  • the logs are only stored locally instead of being backuped or replicated.日志仅存储在本地,而不是备份或复制。

There is a risk if you answered yes to any of those questions.如果您对任何这些问题的回答是肯定的,则存在风险。

Recommended Secure Coding Practices推荐的安全编码实践

  • Check that your production deployment doesn't have its loggers in "debug" mode as it might write sensitive information in logs.检查您的生产部署的记录器是否处于“调试”模式,因为它可能会在日志中写入敏感信息。 Production logs should be stored in a secure location which is only accessible to system administrators.生产日志应存储在只有系统管理员才能访问的安全位置。
  • Configure the loggers to display all warnings, info and error messages.配置记录器以显示所有警告、信息和错误消息。 Write relevant information such as the precise time of events and the hostname.写下相关信息,例如事件的准确时间和主机名。
  • Choose log format which is easy to parse and process automatically.选择易于自动解析和处理的日志格式。 It is important to process logs rapidly in case of an attack so that the impact is known and limited.在发生攻击时快速处理日志非常重要,以便了解和限制影响。
  • Check that the permissions of the log files are correct.检查日志文件的权限是否正确。 If you index the logs in some other service, make sure that the transfer and the service are secure too.如果您在其他服务中索引日志,请确保传输和服务也是安全的。
  • Add limits to the size of the logs and make sure that no user can fill the disk with logs.添加对日志大小的限制,并确保没有用户可以用日志填充磁盘。 This can happen even when the user does not control the logged information.即使用户不控制记录的信息,也会发生这种情况。 An attacker could just repeat a logged action many times.攻击者可以多次重复记录的操作。

Remember that configuring loggers properly doesn't make them bullet-proof.请记住,正确配置记录器并不能使它们防弹。 Here is a list of recommendations explaining on how to use your logs:以下是说明如何使用日志的建议列表:

  • Don't log any sensitive information.不要记录任何敏感信息。 This obviously includes passwords and credit card numbers but also any personal information such as user names, locations, etc…​ Usually any information which is protected by law is good candidate for removal.这显然包括密码和信用卡号,还包括任何个人信息,如用户名、位置等……通常任何受法律保护的信息都可以删除。
  • Sanitize all user inputs before writing them in the logs.在将所有用户输入写入日志之前对其进行清理。 This includes checking its size, content, encoding, syntax, etc…​ As for any user input, validate using whitelists whenever possible.这包括检查其大小、内容、编码、语法等……对于任何用户输入,尽可能使用白名单进行验证。 Enabling users to write what they want in your logs can have many impacts.使用户能够在您的日志中写入他们想要的内容会产生很多影响。 It could for example use all your storage space or compromise your log indexing service.例如,它可能会使用您所有的存储空间或损害您的日志索引服务。
  • Log enough information to monitor suspicious activities and evaluate the impact an attacker might have on your systems.记录足够的信息以监控可疑活动并评估攻击者可能对您的系统造成的影响。 Register events such as failed logins, successful logins, server side input validation failures, access denials and any important transaction.注册事件,例如登录失败、成功登录、服务器端输入验证失败、访问拒绝和任何重要事务。
  • Monitor the logs for any suspicious activity.监控日志中的任何可疑活动。

In other words, you need to validate whether your implementation covers these recommendations or if you even need to worry about any of them.换句话说,您需要验证您的实施是否涵盖了这些建议,或者您是否需要担心其中的任何一个。 In case there's nothing to worry about, you can just set it "as reviewed".如果没有什么可担心的,您可以将其设置为“已审核”。 I would advice that someone other than you check this too, however.但是,我建议您以外的其他人也检查一下。 Perhaps whoever is in charge of the server where the log is written to.也许谁负责写入日志的服务器。

The only way to "get rid" of this hotspot issue is by not using a log or manually checking that everything is ok. “摆脱”这个热点问题的唯一方法是不使用日志或手动检查一切是否正常。 Of course, you should do the latter if you need a logger.当然,如果你需要一个记录器,你应该做后者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM