From c10c2441fc6421cd59798a5c15f9fc5ab693079f Mon Sep 17 00:00:00 2001 From: Meatballs Date: Mon, 1 Jul 2013 15:14:26 +0100 Subject: [PATCH 1/2] Add different methods --- hsql/src/Register.java | 1 + hsql/src/ViewRecords.java | 16 ++++++++++++++-- hsql/test.html | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 hsql/test.html diff --git a/hsql/src/Register.java b/hsql/src/Register.java index ec7980c..08c205c 100644 --- a/hsql/src/Register.java +++ b/hsql/src/Register.java @@ -40,6 +40,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) pst.setString(3, phone); int i=pst.executeUpdate(); out.write(i+" records inserted, View Records"); + out.write("
View Tests"); } catch (SQLException e) { throw new ServletException(e); } diff --git a/hsql/src/ViewRecords.java b/hsql/src/ViewRecords.java index b54ffda..f195229 100644 --- a/hsql/src/ViewRecords.java +++ b/hsql/src/ViewRecords.java @@ -11,9 +11,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; public class ViewRecords extends HttpServlet { Connection con; + @Override public void init() throws ServletException { try { @@ -26,13 +28,23 @@ public void init() throws ServletException { } catch (SQLException e) { e.printStackTrace(System.out); } + + HashMap methods = new HashMap(); + methods.put("str", "select * from contacts where name='%s'"); + methods.put("int_groupby", "SELECT * FROM contacts GROUP BY %s"); + methods.put("int_orderby", "SELECT * FROM contacts ORDER BY %s"); + methods.put("int_inline", "%s"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); try { - String name = request.getParameter("name"); - ResultSet rs =con.createStatement().executeQuery("select * from contacts where name='" + name + "'"); + String inject = request.getParameter("inject"); + String method = request.getParameter("method"); + + String query = String.format(methods.get(method), inject); + + ResultSet rs =con.createStatement().executeQuery(query); while(rs.next()){ out.write("
"+rs.getString(1)); out.write(", "+rs.getString(2)); diff --git a/hsql/test.html b/hsql/test.html new file mode 100644 index 0000000..39d344b --- /dev/null +++ b/hsql/test.html @@ -0,0 +1,23 @@ + + + + + Injection Tests + + +
+ Inject
+ +
+ Method
+ +
+ +
+ + \ No newline at end of file From d9bea591889b568548390e9391ef618eff152041 Mon Sep 17 00:00:00 2001 From: Meatballs Date: Mon, 1 Jul 2013 15:43:39 +0100 Subject: [PATCH 2/2] Fixup java --- hsql/src/ViewRecords.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hsql/src/ViewRecords.java b/hsql/src/ViewRecords.java index f195229..2dd6739 100644 --- a/hsql/src/ViewRecords.java +++ b/hsql/src/ViewRecords.java @@ -15,7 +15,7 @@ public class ViewRecords extends HttpServlet { Connection con; - + HashMap methods; @Override public void init() throws ServletException { try { @@ -29,7 +29,7 @@ public void init() throws ServletException { e.printStackTrace(System.out); } - HashMap methods = new HashMap(); + methods = new HashMap(); methods.put("str", "select * from contacts where name='%s'"); methods.put("int_groupby", "SELECT * FROM contacts GROUP BY %s"); methods.put("int_orderby", "SELECT * FROM contacts ORDER BY %s"); @@ -42,7 +42,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t String inject = request.getParameter("inject"); String method = request.getParameter("method"); - String query = String.format(methods.get(method), inject); + String query = String.format((String)methods.get(method), inject); ResultSet rs =con.createStatement().executeQuery(query); while(rs.next()){