SV.IL.SESSION

记录会话 ID

当服务器或客户端的会话 ID 被记录到应用程序日志中时,Klocwork 会报告 SV.IL.SESSION 缺陷。

漏洞与风险

会话 ID 等敏感数据不应包含在日志中,以保护会话日志,防止会话 ID 在本地或远程遭到泄露或未经授权的访问。

缓解与预防

记录会话 ID 的加盐哈希,而不是会话 ID 本身,以便在不暴露会话 ID 的情况下允许特定于会话的日志关联。

漏洞代码示例 1

复制
import javax.servlet.http.*;
import javax.servlet.*;
import java.security.*;
import java.io.*;
 
class Test{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         HttpSession session = request.getSession();
         String id = session.getId();
         request.getSession().getServletContext().log("Session id is: " + id);
    }
}

Klocwork 在第 10 行报告了 SV.IL.SESSION 缺陷,指出“记录会话 ID 可能导致活动会话劫持”。

修正代码示例 1

复制
import jakarta.servlet.http.*;
import jakarta.servlet.*;
import java.security.*;
import java.io.*;
 
class Test{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         HttpSession session = request.getSession();
         String id = session.getId();
         String hashid = getHashValue(id);
         request.getSession().getServletContext().log("Session id: " + hashid);
    }
 
    public static String getHashValue(String str)
    {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] messageDigest = md.digest(str.getBytes("UTF-8"));
            BigInteger no = new BigInteger(1, messageDigest);
           String hashtext = no.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
}
}

在此修正示例中,Klocwork 不再报告问题,因为已记录会话 ID 的哈希值。

漏洞代码示例 2

复制
import jakarta.servlet.http.*;
import jakarta.servlet.*;
import java.security.*;
import java.io.*;
 
class Test{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         String id = request.getRequestedSessionId();
         request.getSession().getServletContext().log("Session id is: " + id);
    }
}

Klocwork 在第 9 行报告了 SV.IL.SESSION 缺陷,指出“记录会话 ID 可能导致活动会话劫持”。

修正代码示例 2

复制
import javax.servlet.http.*;
import javax.servlet.*;
import java.security.*;
import java.io.*;
 
class Test{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         String id = request.getRequestedSessionId();
         String hashid = getHashValue(id);
         request.getSession().getServletContext().log("Session id: " + hashid);
    }
 
    public static String getHashValue(String str)
    {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] messageDigest = md.digest(str.getBytes("UTF-8"));
            BigInteger no = new BigInteger(1, messageDigest);
           String hashtext = no.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
}
}

在此修正示例中,Klocwork 不再报告问题,因为已记录会话 ID 的哈希值。

安全培训

应用程序安全培训材料由 Secure Code Warrior 提供。