目錄
Session操控漏洞
在Apache tomcat中,有一個默認的example示例目錄,該example目錄中存着眾多的樣例,其中/examples/servlets/servlet/SessionExample 允許用戶對Session進行操作。由於Session是存儲在服務器端的用於驗證用戶身份的東西。所以,理論上,只要我們可以操控Session,就可以偽造任意用戶身份信息!
如圖,是Apache tomcat 網站根目錄下的文件夾,默認是有一個examples目錄的
這是examples目錄下的文件
我們訪問該SessionExample頁面,該頁面可以對Session進行操控,本來該頁面是Apache tomcat用來給開發者操縱Session示例的頁面。但是,如果實際生產環境中不刪除該頁面的話,可能存在偽造任意用戶身份的漏洞。
http://127.0.0.1:8080/examples/servlets/servlet/SessionExample
那么我們來看看SessionExample頁面是如何通過接收用戶輸入的值,來對Session進行控制的。
表單部分代碼,接收用戶輸入的Name和Value值。
out.println("<P>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.print("\" ");
out.println("method=POST>");
out.println(rb.getString("sessions.dataname"));
out.println("<input type=text size=20 name=dataname>");
out.println("<br>");
out.println(rb.getString("sessions.datavalue"));
out.println("<input type=text size=20 name=datavalue>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");
核心代碼,將接收的用戶輸入的Name和Value值寫入到Session中
HttpSession session = request.getSession(true);
out.println(rb.getString("sessions.id") + " " +session.getId());
out.println("<br>");
out.println(rb.getString("sessions.created") + " ");
out.println(new Date(session.getCreationTime()) +"<br>");
out.println(rb.getString("sessions.lastaccessed") + "");
out.println(new Date(session.getLastAccessedTime()));
String dataName = request.getParameter("dataname");//獲取dataname參數的值
String dataValue = request.getParameter("datavalue");//獲取datavalue參數的值
if (dataName != null && dataValue != null) {
session.setAttribute(dataName, dataValue);//將dataname和datavalue寫入session
}
也就是說,用戶通過表單提交Name和Value參數,然后通過request.getParameter()函數獲取這兩個參數的值,再通過session.setAttribute() 函數將Name和Value的值寫入Session中。
漏洞利用示例:
我們先來編寫 login.jsp 、login_check.jsp 、 index.jsp 這三個頁面,通過這三個頁面來模擬一般網站身份驗證的過程。
login.jsp
<form action=login_check.jsp method="POST" >
用戶名: <input type="text"name="username"><br>
密碼: <input type="text" name="password"><br>
<inputtype="submit" value="登錄"><br>
<form>
login_check.jsp
<%
if(request.getParameter("username") != null &&
request.getParameter("password")!= null) {
String username =request.getParameter("username");
String password =request.getParameter("password");
//驗證身份
if (username.equals("admin")&& password.equals("admin")) {
session.setAttribute("login","admin");
response.sendRedirect("index.jsp");
}else {
response.sendRedirect("login.jsp");
}
}
%>
index.jsp
<%
if(session.getAttribute("login")!= null &&
((String)session.getAttribute("login")).equals("admin")){
out.println("Login");
} else{
response.sendRedirect("login.jsp");
}
%>
我們直接打開網站后台,即 index.jsp
http://127.0.0.1:8080/examples/index.jsp
發現被重定向到login.jsp了。因為我們沒有登錄,所以被重定向到了登錄頁面
打開SessionExample
http://127.0.0.1:8080/examples/servlets/servlet/SessionExample
在Name of Session Attribute: 里輸入 login
在Value of Session Attribute:里輸入 admin
提交后顯示login=admin已經寫入session
再次打開index.jsp,顯示成功登錄
注:但是在現實生產環境中,我們很難知道服務器后端的Session中是通過什么參數(Name)和值(Value)來判斷用戶登錄狀態的。所以就是我們根本很難利用該頁面來進行任意用戶偽造,只是說理論上是可行的。
相關文章:Session認證