Advertisement
smeacham

HTB - SQL Injection

Dec 17th, 2013
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. /**
  2. Taken from http://developers.sun.com/prodtech/javatools/jscreator/reference/code/sampleapps/2/Demo_BLOB.zip/Demo_BLOB/src/demo_blob/
  3. This is sample code produced for Java Studio Creator. Find the SQL vulnerability. Hint: think images
  4. */
  5.  
  6. /*
  7.  * DisplayImage.java
  8.  *
  9.  * Created on October 6, 2005, 4:50 PM
  10.  *
  11.  * To change this template, choose Tools | Options and locate the template under
  12.  * the Source Creation and Management node. Right-click the template and choose
  13.  * Open. You can then make changes to the template in the Source Editor.
  14.  */
  15.  
  16. package demo_blob;
  17.  
  18. /**
  19.  *
  20.  * @author USER
  21.  */
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.sql.Connection;
  25. import java.sql.ResultSet;
  26. import java.sql.Statement;
  27. import javax.naming.Context;
  28. import javax.naming.InitialContext;
  29. import javax.servlet.ServletConfig;
  30. import javax.servlet.ServletException;
  31. import javax.servlet.ServletOutputStream;
  32. import javax.servlet.http.HttpServlet;
  33. import javax.servlet.http.HttpServletRequest;
  34. import javax.servlet.http.HttpServletResponse;
  35. import javax.sql.DataSource;
  36.  
  37. public class DisplayImage  extends HttpServlet {
  38.  
  39.  
  40.  
  41.     /** Creates a new instance of DisplayPicture */
  42.     public DisplayImage() {
  43.     }
  44.  
  45.  
  46.     public void init(ServletConfig config) throws ServletException {
  47.         super.init(config);
  48.  
  49.     }
  50.  
  51.     public void destroy() {
  52.     }
  53.  
  54.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  55.     throws ServletException, IOException {
  56.  
  57.  
  58.         String id=request.getParameter("imageid");
  59.         String ct=request.getParameter("contenttype");
  60.         if ((ct==null)||(ct.equals(""))) {
  61.             //ct="image/x-jpeg";
  62.             ct="image/bmp";
  63.         }
  64.         System.out.println("Now displaying image with ID: "+id);
  65.  
  66.         try {
  67.             ServletOutputStream out = response.getOutputStream();
  68.             response.setContentType(ct);
  69.             out.write(this.getImage(id));
  70.         } catch (Exception e) {
  71.             System.out.println(e.getMessage());
  72.             e.printStackTrace();
  73.         }
  74.     }
  75.  
  76.     /** Handles the HTTP <code>GET</code> method.
  77.      * @param request servlet request
  78.      * @param response servlet response
  79.      */
  80.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  81.     throws ServletException, IOException {
  82.         processRequest(request, response);
  83.     }
  84.  
  85.     /** Handles the HTTP <code>POST</code> method.
  86.      * @param request servlet request
  87.      * @param response servlet response
  88.      */
  89.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  90.     throws ServletException, IOException {
  91.         processRequest(request, response);
  92.     }
  93.  
  94.     /** Returns a short description of the servlet.
  95.      */
  96.     public String getServletInfo() {
  97.         return "Displays a picture from the database identified by a parameter IMAGEID";
  98.     }
  99.  
  100.     private byte[] getImage(String id) throws IOException {
  101.         Statement sta=null;
  102.         Connection con=null;
  103.         ResultSet rs=null;
  104.         byte[] result=null;
  105.  
  106.         try {
  107.  
  108.  
  109.             Context initContext = new InitialContext();
  110.             DataSource ds = (DataSource)initContext.lookup("jdbc/Order");
  111.             Connection conn = ds.getConnection();
  112.  
  113.  
  114.  
  115.  
  116.             sta = conn.createStatement();
  117.             rs=sta.executeQuery("SELECT * FROM PBPUBLIC.DEMO_BLOB where IDCOL="+id);
  118.             if (rs.next()) {
  119.                 result=rs.getBytes("BLOBCOL");
  120.             } else {
  121.                 System.out.println("Could find image with the ID specified or there is a problem with the database connection");
  122.             }
  123.             rs.close();
  124.             sta.close();
  125.             conn.close();
  126.         } catch (Exception e) {
  127.             System.out.println(e.getMessage());
  128.             e.printStackTrace();
  129.         }
  130.  
  131.         ByteArrayOutputStream output = new ByteArrayOutputStream();
  132.         //output.write(result, 78, result.length-78);
  133.         //output.flush();
  134.         //output.close();
  135.         //return output.toByteArray();
  136.         return result;
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement