Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.41 KB | None | 0 0
  1.    public void scan(HttpMessage msg, String param, String value) {
  2.        
  3.         try {
  4.             // Inject the 'safe' eyecatcher and see where it appears
  5.             boolean attackWorked = false;
  6.             setParameter(msg, param, Constant.getEyeCatcher());
  7.             sendAndReceive(msg);
  8.            
  9.             HtmlContextAnalyser hca = new HtmlContextAnalyser(msg);
  10.             List<HtmlContext> contexts = hca.getHtmlContexts(Constant.getEyeCatcher(), null, 0);
  11.             if (contexts.size() == 0) {
  12.                 // Lower case?
  13.                 contexts = hca.getHtmlContexts(Constant.getEyeCatcher().toLowerCase(), null, 0);
  14.             }
  15.             if (contexts.size() == 0) {
  16.                 // Upper case?
  17.                 contexts = hca.getHtmlContexts(Constant.getEyeCatcher().toUpperCase(), null, 0);
  18.             }
  19.             if (contexts.size() == 0) {
  20.                 // No luck - try again, appending the eyecatcher to the original value
  21.                 setParameter(msg, param, param + Constant.getEyeCatcher());
  22.                 sendAndReceive(msg);
  23.                 hca = new HtmlContextAnalyser(msg);
  24.                 contexts = hca.getHtmlContexts(value + Constant.getEyeCatcher(), null, 0);
  25.             }
  26.             if (contexts.size() == 0) {
  27.                 // No luck - lets just try a direct attack
  28.                 List<HtmlContext> contexts2 = performAttack (msg, param,
  29.                         "'\"<script>alert(1);</script>", null, 0);
  30.                 if (contexts2.size() > 0) {
  31.                     // Yep, its vulnerable
  32.                     bingo(Alert.RISK_HIGH, Alert.SUSPICIOUS, null, param, contexts2.get(0).getTarget(),
  33.                             "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  34.                     attackWorked = true;
  35.                 }
  36.             }
  37.            
  38.             for (HtmlContext context : contexts) {
  39.                 // Loop through the returned contexts and lauch targetted attacks
  40.                 if (attackWorked) {
  41.                     break;
  42.                 }
  43.                 if (context.getTagAttribute() != null) {
  44.                     // its in a tag attribute - lots of attack vectors possible
  45.          
  46.                     if (context.isInScriptAttribute()) {
  47.                         // Good chance this will be vulnerable
  48.                         // Try a simple alert attack
  49.                         List<HtmlContext> contexts2 = performAttack (msg, param, ";alert(1)", context, 0);
  50.                        
  51.                         for (HtmlContext context2 : contexts2) {
  52.                             if (context2.getTagAttribute() != null &&
  53.                                     context2.isInScriptAttribute()) {
  54.                                 // Yep, its vulnerable
  55.                                 bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, context2.getTarget(),
  56.                                         "", contexts2.get(0).getTarget(), context2.getMsg());
  57.                                 attackWorked = true;
  58.                                 break;
  59.                             }
  60.                         }
  61.                         if (!attackWorked) {
  62.                             log.debug("Failed to find vuln in script attribute on " + msg.getRequestHeader().getURI());
  63.                         }
  64.  
  65.                     } else if (context.isInUrlAttribute()) {
  66.                         // Its a url attribute
  67.                         List<HtmlContext> contexts2 = performAttack (msg, param, "javascript:alert(1);", context, 0);
  68.  
  69.                         for (HtmlContext ctx : contexts2) {
  70.                             if (ctx.isInUrlAttribute()) {
  71.                                 // Yep, its vulnerable
  72.                                 bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, ctx.getTarget(),
  73.                                     "", ctx.getTarget(), ctx.getMsg());
  74.                                 attackWorked = true;
  75.                                 break;
  76.                             }
  77.                         }
  78.                         if (!attackWorked) {
  79.                             log.debug("Failed to find vuln in url attribute on " + msg.getRequestHeader().getURI());
  80.                         }
  81.  
  82.                     }
  83.                     if (! attackWorked && context.isInTagWithSrc()) {
  84.                         // Its in an attribute in a tag which supports src attributes
  85.                         List<HtmlContext> contexts2 = performAttack (msg, param,
  86.                                 context.getSurroundingQuote() + " src=http://badsite.com", context, HtmlContext.IGNORE_TAG);
  87.  
  88.                         if (contexts2.size() > 0) {
  89.                             bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  90.                                 "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  91.                             attackWorked = true;
  92.                         }
  93.                         if (!attackWorked) {
  94.                             log.debug("Failed to find vuln in tag with src attribute on " + msg.getRequestHeader().getURI());
  95.                         }
  96.                     }
  97.                    
  98.                     if (! attackWorked) {
  99.                         // Try a simple alert attack
  100.                         List<HtmlContext> contexts2 = performAttack (msg, param,
  101.                                 context.getSurroundingQuote() + "><script>alert(1);</script>", context, HtmlContext.IGNORE_TAG);
  102.                         if (contexts2.size() > 0) {
  103.                             // Yep, its vulnerable
  104.                             bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  105.                                     "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  106.                             attackWorked = true;
  107.                         }
  108.                         if (!attackWorked) {
  109.                             log.debug("Failed to find vuln with simple script attack " + msg.getRequestHeader().getURI());
  110.                         }
  111.                     }
  112.                     if (! attackWorked) {
  113.                         // Try adding an onMouseOver
  114.                         List<HtmlContext> contexts2 = performAttack (msg, param,
  115.                                 context.getSurroundingQuote() + " onMouseOver=" + context.getSurroundingQuote() + "alert(1);",
  116.                                 context, HtmlContext.IGNORE_TAG);
  117.                         if (contexts2.size() > 0) {
  118.                             // Yep, its vulnerable
  119.                             bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  120.                                     "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  121.                             attackWorked = true;
  122.                         }
  123.                         if (!attackWorked) {
  124.                             log.debug("Failed to find vuln in with simple onmounseover " + msg.getRequestHeader().getURI());
  125.                         }
  126.                     }
  127.                 } else if (context.isHtmlComment()) {
  128.                     // Try breaking out of the comment
  129.                     List<HtmlContext> contexts2 = performAttack (msg, param,
  130.                             "--><script>alert(1);</script><!--", context, HtmlContext.IGNORE_HTML_COMMENT);
  131.                     if (contexts2.size() > 0) {
  132.                         // Yep, its vulnerable
  133.                         bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  134.                                 "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  135.                         attackWorked = true;
  136.                     } else {
  137.                         // Maybe they're blocking script tags
  138.                         contexts2 = performAttack (msg, param,
  139.                                 "--><b onMouseOver=alert(1);>test</b><!--", context, HtmlContext.IGNORE_HTML_COMMENT);
  140.                         if (contexts2.size() > 0) {
  141.                             // Yep, its vulnerable
  142.                             bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  143.                                     "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  144.                             attackWorked = true;
  145.                         }
  146.                     }
  147.                 } else {
  148.                     // its not in a tag attribute
  149.                     if ("body".equalsIgnoreCase(context.getParentTag())) {
  150.                         // Immediately under a body tag
  151.                         // Try a simple alert attack
  152.                         List<HtmlContext> contexts2 = performAttack (msg, param,
  153.                                 "<script>alert(1);</script>", null, HtmlContext.IGNORE_PARENT);
  154.                         if (contexts2.size() > 0) {
  155.                                 // Yep, its vulnerable
  156.                                 bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  157.                                         "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  158.                                 attackWorked = true;
  159.                         } else {
  160.                             // Maybe they're blocking script tags
  161.                             contexts2 = performAttack (msg, param,
  162.                                     "<b onMouseOver=alert(1);>test</b>", context, HtmlContext.IGNORE_PARENT);
  163.                             for (HtmlContext context2 : contexts2) {
  164.                                 if ("body".equalsIgnoreCase(context2.getParentTag()) ||
  165.                                         "script".equalsIgnoreCase(context2.getParentTag())) {
  166.                                     // Yep, its vulnerable
  167.                                     bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  168.                                             "TBI Body tag", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  169.                                     attackWorked = true;
  170.                                     break;
  171.                                 }
  172.                             }
  173.                         }
  174.                     } else if (context.getParentTag() != null){
  175.                         // Its not immediately under a body tag, try to close the tag
  176.                         List<HtmlContext> contexts2 = performAttack (msg, param,
  177.                                 "</" + context.getParentTag() + "><script>alert(1);</script><" + context.getParentTag() + ">",
  178.                                 context, HtmlContext.IGNORE_IN_SCRIPT);
  179.                         if (contexts2.size() > 0) {
  180.                             // Yep, its vulnerable
  181.                             bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  182.                                     "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  183.                             attackWorked = true;
  184.                         } else if ("script".equalsIgnoreCase(context.getParentTag())){
  185.                             // its in a script tag...
  186.                             contexts2 = performAttack (msg, param,
  187.                                     context.getSurroundingQuote() + ";alert(1);" + context.getSurroundingQuote(), context, 0);
  188.                             if (contexts2.size() > 0) {
  189.                                 // Yep, its vulnerable
  190.                                 bingo(Alert.RISK_HIGH, Alert.WARNING, null, param, contexts2.get(0).getTarget(),
  191.                                         "", contexts2.get(0).getTarget(), contexts2.get(0).getMsg());
  192.                                 attackWorked = true;
  193.                             }
  194.                         }
  195.                     }
  196.                 }
  197.             }
  198.         } catch (InvalidRedirectLocationException e) {
  199.             // Not an error, just means we probably attacked the redirect location
  200.         } catch (Exception e) {
  201.             log.error(e.getMessage(), e);
  202.         }
  203.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement