Skip to content

Instantly share code, notes, and snippets.

@baraths84
Created October 20, 2015 06:27
Show Gist options
  • Save baraths84/a573f7f3fdf4ed94afb8 to your computer and use it in GitHub Desktop.
Save baraths84/a573f7f3fdf4ed94afb8 to your computer and use it in GitHub Desktop.
//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.sqli.sqli1", 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();
Encoder encoder = ESAPI.encoder();
try
{
String aUserId = request.getParameter("aUserId");
log.debug("User Submitted - " + aUserId);
String ApplicationRoot = getServletContext().getRealPath("");
log.debug("Servlet root = " + ApplicationRoot );
log.debug("Getting Connection to Database");
Connection conn = Database.getChallengeConnection(ApplicationRoot, "SqlChallengeOne");
Statement stmt = conn.createStatement();
log.debug("Gathering result set");
ResultSet resultSet = stmt.executeQuery("SELECT * FROM customers WHERE customerId = \"" + aUserId + "\"");
int i = 0;
htmlOutput = "<h2 class='title'>" + bundle.getString("response.searchResults")+ "</h2>";
htmlOutput += "<table><tr><th>"+ bundle.getString("response.table.name") +"</th><th>"+ bundle.getString("response.table.address") +"</th><th>"+ bundle.getString("response.table.comment") +"</th></tr>";
log.debug("Opening Result Set from query");
while(resultSet.next())
{
log.debug("Adding Customer " + resultSet.getString(2));
htmlOutput += "<tr><td>"
+ encoder.encodeForHTML(resultSet.getString(2)) + "</td><td>"
+ encoder.encodeForHTML(resultSet.getString(3)) + "</td><td>"
+ encoder.encodeForHTML(resultSet.getString(4)) + "</td></tr>";
i++;
}
htmlOutput += "</table>";
if(i == 0)
{
htmlOutput = "<p>"+bundle.getString("response.noResults")+"</p>";
}
}
catch (SQLException e)
{
log.debug("SQL Error caught - " + e.toString());
htmlOutput += "<p>"+errors.getString("error.detected")+"</p>" +
"<p>" + encoder.encodeForHTML(e.toString()) + "</p>";
}
catch(Exception e)
{
out.write(errors.getString("error.funky"));
log.fatal(levelName + " - " + e.toString());
}
log.debug("Outputting HTML");
out.write(htmlOutput);
}
else
{
log.error(levelName + " servlet accessed with no session");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment