package servlets.module.challenge;
import dbProcs.Database;
import dbProcs.Getter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.owasp.encoder.Encode;
import utils.Hash;
import utils.ShepherdLogManager;
import utils.Validate;
/**
* Session Management Challenge Six - Security Question Does not return result key
*
* This file is part of the Security Shepherd Project.
*
*
The Security Shepherd project is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
*
The Security Shepherd project is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
*
You should have received a copy of the GNU General Public License along with the Security
* Shepherd project. If not, see
" + bundle.getString("response.welcome") + " " + userKey + "" + "
"; } else { log.debug("Bad Answer Submitted"); htmlOutput = new String( "" + bundle.getString("question.whoAreYou")); } Database.closeConnection(conn); } else { log.debug("Invalid data submitted"); htmlOutput = new String("" + bundle.getString("question.invalidData") + ": "); if (subAns.length() < 5) { htmlOutput += bundle.getString("question.invalidAns"); } else { htmlOutput += bundle.getString("question.invalidEmail"); } } } catch (SQLException e) { log.error(levelName + " SQL Error: " + e.toString()); } log.debug("Outputting HTML"); out.write(htmlOutput); } catch (Exception e) { out.write(errors.getString("error.funky")); log.fatal(levelName + " - " + e.toString()); } } else { log.error(levelName + " servlet accessed with no session"); } } /** * A user submits an email address to get that user's Secret QUestion. This is vulnerable to SQL * injection * * @param subEmail Sub schema user email to search DB with */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String levelName = "Session Management Challenge Six (Get Question)"; // Setting IpAddress To Log and taking header for original IP if forwarded from proxy ShepherdLogManager.setRequestIp(request.getRemoteAddr(), request.getHeader("X-Forwarded-For")); HttpSession ses = request.getSession(true); // Translation Stuff Locale locale = new Locale(Validate.validateLanguage(request.getSession())); ResourceBundle errors = ResourceBundle.getBundle("i18n.servlets.errors", locale); ResourceBundle bundle = ResourceBundle.getBundle( "i18n.servlets.challenges.sessionManagement.sessionManagement6", locale); if (Validate.validateSession(ses)) { ShepherdLogManager.setRequestIp( request.getRemoteAddr(), request.getHeader("X-Forwarded-For"), ses.getAttribute("userName").toString()); log.debug(levelName + " servlet accessed by: " + ses.getAttribute("userName").toString()); PrintWriter out = response.getWriter(); out.print(getServletInfo()); String htmlOutput = new String(); log.debug(levelName + " Servlet accessed"); try { log.debug("Getting Cookies"); Cookie userCookies[] = request.getCookies(); int i = 0; Cookie theCookie = null; for (i = 0; i < userCookies.length; i++) { if (userCookies[i].getName().compareTo("ac") == 0) { theCookie = userCookies[i]; break; // End Loop, because we found the token } } if (theCookie != null) { byte[] decodedCookieBytes = Base64.decodeBase64(theCookie.getValue()); String decodedCookie = new String(decodedCookieBytes, "UTF-8"); log.debug("Decoded Cookie: " + decodedCookie); if (decodedCookie.equals("doNotReturnAnswers")) // Untampered Cookie { log.debug("Getting Parameter"); Object emailObj = request.getParameter("subEmail"); String subEmail = Validate.validateParameter(emailObj, 75); log.debug("subEmail = " + subEmail); String ApplicationRoot = getServletContext().getRealPath(""); try { if (subEmail.length() < 10) { log.debug("Invalid data submitted"); htmlOutput = new String( "" + bundle.getString("question.invalidData") + ": " + bundle.getString("question.invalidEmail")); } else { Connection conn = Database.getChallengeConnection( ApplicationRoot, "BrokenAuthAndSessMangChalSix"); log.debug("Getting Secret Question"); PreparedStatement callstmt = conn.prepareStatement( "SELECT secretQuestion FROM users WHERE userAddress = \"" + subEmail + "\""); ResultSet rs = callstmt.executeQuery(); if (rs.next()) { log.debug("'Valid' User Detected"); log.debug("Encoding for output: " + rs.getString(1)); // rs.getString(1) contains the question for the user to answer. This question is // asked in English as it must be answered in English to successfully pass the // level htmlOutput = new String(Encode.forHtml(rs.getString(1))); } else { log.debug("No question found for user"); htmlOutput = bundle.getString("question.noQuestion"); } Database.closeConnection(conn); } } catch (SQLException e) { log.debug(levelName + " SQL Error: " + e.toString()); log.debug("Outputting error to user"); htmlOutput = new String(e.toString()); } } else { log.debug("Tampered cookie detected"); htmlOutput = new String(bundle.getString("response.configError")); } } else { log.debug("Tampered cookie detected"); htmlOutput = new String(bundle.getString("response.configError")); } log.debug("Outputting HTML"); out.write(htmlOutput); } catch (Exception e) { out.write(errors.getString("error.funky")); log.fatal(levelName + " - " + e.toString()); } } else { log.error(levelName + " servlet accessed with no session"); } } }