This post talks about exploiting the SQL queries with LIKE operator in use. However, this situation and target can be specific in nature but one can use the concept that is discussed below to go after exploiting the SQL injection. In order to discuss this part, let's take an example as presented below
$search_name = htmlentities(mysql_real_escape_string($_POST['search_user']));
$age = stripslashes($_POST['age']);
$query1 = mysql_query("SELECT * FROM user_table WHERE username LIKE '$search_user' AND age=('$age')") or die("SQL Error Mate");
In this example, "search_user" is the parameter that is provided as an initial input point to the application user. As one can see, this parameter is escaped using mysql_real_escape_string(escapes special characters in a string for use in an SQL statement) and then with htmlentities (convert all applicable characters to HTML entities). There is another parameter as "age" which is set with stripslashes(returns a string with backslashes stripped off)
So in this case, where the SQL injection can be done. Following consideration leads to successful SQL injection
1. There is an age parameter which takes the value from the application user. This parameter is expected by the server in the POST request.
2. As the age parameter is using stripslashes function, it is good to inject legitimate value and then closing it appropriately.
3. In this case, one must not concentrate on exploiting the search_user parameter rather hit on the age parameter. As it is POST request, it is easy to play with proxy to set up the value for the age parameter.
4. In general, when anyone run the successful query with the legitimate username such as root, another information such as age will be thrown on the HTML page. This clearly indicates the fact that query is consuming some another parameter too.
5. So the payloads such as age=';-- produces an error as follows
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';--')' at line 1"
This means the application is vulnerable to POST based SQL injection and appropriate query is required to exploit it successfully.
6. One can try for fuzzing it with different requests such as Union SQL Poisoning tricks. For example: age=27') union select 1,2,3,4,5,6,7,password from user_table -- -
For any POST based SQL injection, always try to verify the fact which field is required to be attacked and whether a new parameter can be injected or not.
You may encounter this type of scenario in hacking challenges. :)
This solution is an outcome of collaborative work with Rohit (Rb1337). Hope to share some more thoughts on SQL injections.