7

我在解决 Fortify 中的 Log Forging 问题时遇到问题。getLongFromTimestamp() 方法中的两个日志调用都引发了“将未经验证的用户输入写入日志”的问题。

public long getLongFromTimestamp(final String value) {
    LOGGER.info("getLongFromTimestamp(" + cleanLogString(value) + ")");

    long longVal = 0;
    Date tempDate = null;
    try {            
        tempDate = new SimpleDateFormat(FORMAT_YYYYMMDDHHMMSS, Locale.US).parse(value);
    } catch (ParseException e) {
        LOGGER.warn("Failed to convert to Date: " + cleanLogString(value) + " Exception: " + cleanLogString(e.getMessage()));
        throw new Exception(e);
    }

    if (tempDate != null) {
        longVal = tempDate.getTime();
    }
    return longVal;
}

private cleanLogString(String logString) {
    String clean = logString.replaceAll("[^A-Za-z0-9]", "");

    if(!logString.equals(clean)) {
        clean += " (CLEANED)";
    }

    return clean;
}

cleanLogString() 方法修复了我项目中的其他 Log Forging Fortify 问题,但它对上述 2 没有影响。

任何帮助,将不胜感激!

4

4 回答 4

4

可以使用 fortify Java 注释告诉 Fortify 从清理函数返回的数据现在是安全的。

在查看我的日志伪造问题时,我有通过 Web API 进入的字符串,因此在我的字符串上有XSS标志WEB。我试图找到只会删除这些标志的注释,但找不到任何删除WEB标志的方法。我找到的唯一文档是Samples/advanced/javaAnnotation目录。

由于我的清理方法确实清理了字符串,因此我选择删除所有标志。但这可能是一个问题,因为它可以隐藏侵犯隐私的行为。

@FortifyValidate("return")
private String sanitizeString(String taintedString) {
    return doSomethingWithTheString(taintedString);
}
于 2015-06-24T10:20:24.617 回答
2

Originally when this question was written our team was using log4j v1.2.8, however we noticed that all the log forging issues disappeared after upgrading to log4j v2.6.2.

Once the log4j version is upgraded the Fortify log forging issues should go away. The cleanLogString() method form the question above is also unnecessary. For example:

LOGGER.info("getLongFromTimestamp(" + value + ")");
于 2016-09-14T14:16:14.327 回答
1

我知道我遇到过应用程序的复杂性会阻止任何恶意输入按预期工作的情况;Fortify 认为这不安全。我敢打赌你会遇到同样的事情。

您正在从日志消息中删除任何真正有用的字符,但是看看如果您在写入日志之前对输出进行一些编码会发生什么。

http://www.jtmelton.com/2010/09/21/preventing-log-forging-in-java/

// 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)";
    }
}
于 2015-06-02T03:23:20.413 回答
-5

使用reflecttry-catch。它很容易作弊强化。

于 2017-06-21T16:44:19.440 回答