SQL Injection code analysis

Source: Internet
Author: User

I carefully read the SQL Injection in dvwa and analyzed the cause. The following code provides the lowest security level:

<?phpif(isset($_GET['Submit'])){// Retrieve data$id = $_GET['id'];$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";$result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' );$num = mysql_numrows($result);$i = 0;while ($i < $num) {$first = mysql_result($result,$i,"first_name");$last = mysql_result($result,$i,"last_name");echo '<pre>';echo 'ID: ' . $id . '<br>First name: ' . $first . '<br>Surname: ' . $last;echo '</pre>';$i++;}}?>

 

In the beginning, only the submitted data is judged to be empty. If not empty, the data is directly imported into the query, resulting in SQL injection. Data is not filtered by any means. Attackers only need to enter non-empty data to inject data. Let's look at the moderate security-level code:
<?phpif (isset($_GET['Submit'])) {// Retrieve data$id = $_GET['id'];$id = mysql_real_escape_string($id);$getid = "SELECT first_name, last_name FROM users WHERE user_id = $id";$result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' );$num = mysql_numrows($result);$i=0;while ($i < $num) {$first = mysql_result($result,$i,"first_name");$last = mysql_result($result,$i,"last_name");echo '<pre>';echo 'ID: ' . $id . '<br>First name: ' . $first . '<br>Surname: ' . $last;echo '</pre>';$i++;}}?>

 

The only improvement of this level is the conversion of symbols, but it cannot prevent our injection. The mysql_real_escape_string function is only used to escape some special symbols, such as the 'sign, which can be escaped directly. So that it cannot be injected. This is different from intermediate. Let's look at the advanced:
<?phpif (isset($_GET['Submit'])) {// Retrieve data$id = $_GET['id'];$id = stripslashes($id);$id = mysql_real_escape_string($id);if (is_numeric($id)){$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";$result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' );$num = mysql_numrows($result);$i=0;while ($i < $num) {$first = mysql_result($result,$i,"first_name");$last = mysql_result($result,$i,"last_name");echo '<pre>';echo 'ID: ' . $id . '<br>First name: ' . $first . '<br>Surname: ' . $last;echo '</pre>';$i++;}}}?>

 

In advanced mode, a newline symbol is removed, which is the concept of context .,

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.