简体   繁体   中英

Regular Expression to detect query in java

I am trying to search a String using Regular Expression. For example: this is my sample String

  **if (c == 0) {
                count = 0;
                du.insert(ipAddress, c);
            } else {
                count = c;
            }
            getDate();
            String query1 = "select * from loginmaster where  username = '" + username + "' and password = '" + password + "' ;";
            //out.println(query1);
            //out.println(request.getParameter("Group1"));
            session.setAttribute("group", request.getParameter("Group1"));
            if (count < 3) {
                if (request.getParameter("Group1").equals("With")) {
                    LoginQuery q = new LoginQuery();
                    checked = q.Checker(query1);
                    if (checked == false) {
                        connection.getConnection();
                        connection.getDML("insert into attack values('"+ipAddress+"','"+date+"','Attack Detected')");
                    }
                }**

and i am trying to find querys in this String using Regular Expression

String regExp = "\b(ALTER|CREATE|DELETE|DROP|EXEC(UTE){0,1}|INSERT( +INTO){0,1}|MERGE|SELECT|UPDATE|UNION( +ALL){0,1})\b";

and

String regExp = "(;|\\s)(exec|execute|select|insert|update|delete|create|alter|drop|rename|truncate|backup|restore)\\s";

But i am not getting any Output nor Error.

Remaining Code is:

    Pattern p = Pattern.compile(regExp, Pattern.CASE_INSENSITIVE);                
                while ((line = reader.readLine()) != null) {
                    Matcher m = p.matcher(line);
                    if (m.matches()) {
                        JOptionPane.showMessageDialog(this, "innnnnnnnnnn");
                        System.err.println(m.group(1));
                    }
}

Pls help

Your regexes will not match with the input string, because of case mismatch .

Your regular expressions written in upper-case but your input string contains lower-case matches. So, either make the regexes case-insensitive or convert it to lower-case .

By the way, your regexes couldn't separate query insert into attack ... and method: du.insert(ipAddress, c);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM