Filtering XSS attacks using filter

Source: Internet
Author: User
filtering XSS attacks using filter Blog Categories:Technology Life filter to achieve foot injection attack filter source

http://winnie825.iteye.com/blog/1170833

First, the realization of the idea:

1. The use of regular expressions to implement script filtering, this method of high accuracy, but may be based on the requirements can not be changed;

2. In order to ensure flexible configuration (including regular expression flexibility), the use of XML configuration file to record configuration information, configuration information contains whether to turn on checksums, whether log, interrupt requests, whether to replace script characters, etc.

3. To ensure that the XML does not conflict with the special characters of regular expressions, use the <! The [cdata[]]> tag holds regular expressions, but requires special handling in the class;

4. Implement the header and parameter information filtering in request by inheriting Httprequestwrapper;

5. XML parsing using DOM4J, will later on the use of this tool to write an article, temporarily hard everyone to go to the site to find information (this article is very good http://www.ibm.com/developerworks/cn/xml/x-dom4j.html);

6. Use Xsssecuritymanager class to implement configuration information loading and processing, xsssecurityconfig record matching information, Xsssecuritycon to identify the required constants of the program;

The attachment contains the source code, if can help me to improve better

  Java code   package com.sg.security;      import java.io.ioexception;    import java.util.enumeration;   import java.util.map;   import  java.util.set;      import javax.servlet.servletexception;   import  javax.servlet.http.httpservletrequest;   import javax.servlet.http.httpservletrequestwrapper;    import javax.servlet.http.httpservletresponse;        /**   *  @author  winnie   *  @date     *  @describe  request Information Encapsulation class, Used to judge, process special characters in request requests    */   public class xsshttprequestwrapper extends  httpservletrequestwrapper {              /**        *  Encapsulation HTTP request        *  @param   request       */       public xsshttprequestwrapper ( Httpservletrequest request)  {           super ( Request);       }                @Override        public string getheader (string name)  {           String value =  Super.getheader (name);           //  If special character substitution is turned on, replace special characters            if (xsssecurityconfig.replace) {                xsssecuritymanager.securityreplace (name);            }            return value;       }           @Override         public string getparameter (string name)  {            string value = super.getparameter (name);            //  if special characters are switched on, replace special characters             if (xsssecurityconfig.replace) {                xsssecuritymanager.securityreplace (name);            }           return value;       }          /**        *  no offending data, return false;       *        *   @return &nbsp      */        @SuppressWarnings ("unchecked")        private boolean checkheader () {            enumeration<string> headerparams = this.getheadernames ();           while (Headerparams.hasmoreelements ()) {                String headerName =  Headerparams.nextelement ();                string headervalue = this.getheader (headername);                if (Xsssecuritymanager.matches (headerValue)) {                    return true;                }            }           return false;        }              /**        *  no offending data, go back to false;       *         *  @return        */       @ Suppresswarnings ("unchecked")        private boolean checkparameter () {            map<string,object> submitparams =  this.getparametermap ();           Set<String>  submitnames = submitparams.keyset ();            for (string Submitname : submitnames) {                object submitvalues = submitparams.get (submitname);                if (submitvalues instanceof string) {                    if ( Xsssecuritymanager.matches ((String) submitvalues) {                        return true;                    }                }else if (submitValues instanceof  string[]) {                    for (String submitvalue :  (string[]) submitvalues) {                        if (XSSSecurityManager.matches ( String) {                submitvalue))             return true;                         }                    }               }            }           return  false;       }               &nbSp     /**       *  No offending data, go back to false;        *  If there is violation data, according to the configuration information to determine whether to jump to the error page        *  @param   response       *  @return        *  @throws  IOException        *  @throws  ServletException        */       public boolean validateparameter (httpservletresponse response)  throws ServletException, IOException{            //  Start header Checksum, check header information             if (xsssecurityconfig.is_check_header) {                if (This.checkheader ()) {                    return true;                }           }           //  Start parameter check, check parameter information             if (xsssecurityconfig.is_check_parameter) {                if (This.checkparameter ()) {                    return true;               }            }           return false;        }         }  

 

  Java code  /**   *    */   package com.sg.security;      import java.io.ioexception;      import javax.servlet.filter;    import javax.servlet.filterchain;   import javax.servlet.filterconfig;   import javax.servlet.servletexception;   import javax.servlet.servletrequest;   import javax.servlet.servletresponse;   import javax.servlet.http.httpservletrequest;    import javax.servlet.http.httpservletresponse;      import  org.apache.log4j.logger;        /**   *  @author  winnie   *  @date     *  @describe   Security information Audit class    */   Public class  XSSSecurityFilter implements Filter{          private  static  Logger logger = logger.getlogger (xsssecurityfilter.class);               /**       *  destroy Operation         */       public void destroy ()  {            logger.info ("Xsssecurityfilter destroy ()  begin");            xsssecuritymanager.destroy ();            logger.info ("Xsssecurityfilter destroy ()  end");       }          /**        *  Security Audit        *  Read configuration information        */        public void dofilter (servletrequest request, servletresponse  response,               filterchain chain)  throws IOException, ServletException {            //  judge whether to use http           checkrequestresponse (request, response);           //  Transformation            HttpServletRequest httpRequest =  ( HttpServletRequest)  request;            httpservletresponse httpresponse =  (httpservletresponse)  response;            // http information Encapsulation class             xsshttprequestwrapper xssrequest = new xsshttprequestwrapper (httpRequest);    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;            //  The Request information encapsulation and verification work, if the verification failure (including illegal characters) , logging and request interrupt processing based on configuration information            if ( Xssrequest.validateparameter (HttpResponse)) {                if (xsssecurityconfig.is_log) {                    //  record attack access log                     //  can use database, log, file and other methods                 }                if (xsssecurityconfig.is_chain) {                    httprequest.getrequestdispatcher ( Xsssecuritycon.filter_error_page). Forward (&nbsP;httprequest, httpresponse);                    return;                }           }            chain.dofilter (xssrequest, response);       }           /**       *  initialization Operation        */       public void init (filterconfig  filterconfig)  throws ServletException {            xsssecuritymanager.init (filterconfig);       }           /**       *  judgment request ,response  type   &NBSP;&NBSP;   *  @param  request       *             ServletRequest       * @ param response       *             ServletResponse       *  @throws  servletexception         */       private void  Checkrequestresponse (servletrequest request,                servletresponse response)  throws ServletException {           if  (!) ( request instanceof httpservletrequest))  {                throw new servletexception ("can  Only process httpservletrequest ");               }           if  ( response instanceof httpservletresponse))  {                throw new servletexception ("can only process  HttpServletResponse ");           }        }  }  

  Java code  /**   *    */   package com.sg.security;      import java.util.iterator;   import java.util.regex.pattern;       import javax.servlet.filterconfig;      import org.apache.log4j.logger;   </

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.