GPEN · Question #343
John works as a Professional Penetration Tester. He has been assigned a project to test the ='or''=' as a username and successfully logs on to the user page of the Web site. Now, John asks the we-are-
The correct answer is C. Use the mysql_real_escape_string() function for escaping input. The mysql_real_escape_string() function sanitizes user input by escaping SQL-significant characters, directly preventing the SQL injection technique demonstrated in this scenario.
Question
John works as a Professional Penetration Tester. He has been assigned a project to test the ='or''=' as a username and successfully logs on to the user page of the Web site. Now, John asks the we-are-secure Inc. to improve the login page PHP script. Which of the following suggestions can John give to improve the security of the we-are-secure Website login page from the SQL injection attack?
Options
- AUse the escapeshellarg() function
- BUse the session_regenerate_id() function
- CUse the mysql_real_escape_string() function for escaping input
- DUse the escapeshellcmd() function
How the community answered
(48 responses)- A13% (6)
- B2% (1)
- C81% (39)
- D4% (2)
Why each option
The mysql_real_escape_string() function sanitizes user input by escaping SQL-significant characters, directly preventing the SQL injection technique demonstrated in this scenario.
escapeshellarg() wraps a string for safe use in OS shell commands by escaping shell metacharacters, and provides no protection against SQL query manipulation.
session_regenerate_id() creates a new PHP session identifier to defend against session fixation attacks and has no bearing on SQL query construction or input sanitization.
The mysql_real_escape_string() function escapes characters such as single quotes, backslashes, and null bytes that are exploited in payloads like ='or''=', ensuring the input is treated as a literal string rather than executable SQL logic. By sanitizing user-supplied data before it is embedded in a query, the function eliminates the injection vector at the point where untrusted input meets the database layer.
escapeshellcmd() escapes metacharacters in shell commands to prevent OS command injection, not SQL injection, making it irrelevant to securing database queries.
Concept tested: PHP SQL injection prevention using mysql_real_escape_string
Source: https://www.php.net/manual/en/function.mysql-real-escape-string.php
Topics
Community Discussion
No community discussion yet for this question.