Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9
|
BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9
This is the forum to discuss the Wrox book PHP and MySQL: Create-Modify-Reuse by Timothy Boronczyk, Martin E. Psinas; ISBN: 9780470192429
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 12th, 2010, 05:01 PM
Authorized User
 
Join Date: Jul 2009
Posts: 77
Thanks: 4
Thanked 6 Times in 6 Posts
Default Chap 12 - Security, SQL Injection vulnerability illustration

Injection

SQL Injection

Page 321, the code shown in the gray box, in the
middle of the page, is not the same as the download code.

The download code just has "1 = 1" for the query. This does
illustrate the point of exploiting the vulnerability and
getting back all the email addresses, but it does not
illustrate the case of using $_POST['user_id'].
PHP Code:
$query 'SELECT USERNAME, EMAIL_ADDR FROM WROX_USER WHERE 1 = 1'
Now the code in the book uses $_POST['user_id'] that is passed in,
but it needs a form to actually use it.
To do this I basically took exploit_01.php and
put this code into the if block section
In the form, I changed name to user_id, but the
rest of it is the same as in exploit_01.php

Here is the complete vulnerable code I used which
illustrates the SQL injection vulnerability.

PHP Code:
<?php
include "../lib/common.php";
include 
"../lib/db.php";

if (isset(
$_POST['submitted']))
{
$query 'SELECT USERNAME, EMAIL_ADDR FROM USER WHERE USER_ID = ' $_POST['user
_id'
];

$result mysql_query($query$GLOBALS['DB']);

echo 
'<p>Welcome!<br/>Here is the information we have on file for you:</p>';
echo 
'<table>';
while (
$row mysql_fetch_assoc($result))
{
    echo 
'<tr><td>Name:</td>';
    echo 
'<td>' htmlspecialchars($row['USERNAME']) . '</td></tr>';
    echo 
'<tr><td>Email:</td>';
    echo 
'<td>' htmlspecialchars($row['EMAIL_ADDR']) . '</td></tr>';
}
echo 
'</table>';
echo 
'<p>Is this correct?</p>';

mysql_free_result($result);
mysql_close($GLOBALS['DB']);

}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <div>
  Enter your user id: <input type="text" name="user_id"/>
  <input type="submit" value="Submit"/>
  <input type="hidden" name="submitted" value="true"/>
 </div>
</form>
<?php
Here is a copy of the authors corrected code; I added
a structure around it so it can use the form.
(I don't use the prefix WROX on USER)

PHP Code:
<?php
include "../lib/common.php";
include 
"../lib/db.php";

if (isset(
$_POST['submitted']))
{
   
$query sprintf('SELECT USERNAME, EMAIL_ADDR FROM USER WHERE ' .
   
'USER_ID = %d'$_POST['user_id']);
   
   
$result mysql_query($query$GLOBALS['DB']);
   
   
$row mysql_fetch_assoc($result); 
   
mysql_free_result($result);

   echo 
'<p>Welcome!<br/>Here is the information we have on file for you:</p>';
   echo 
'<table>';
   echo 
'<tr><td>Name:</td>';
   echo 
'<td>' htmlspecialchars($row['USERNAME']) . '</td></tr>';
   echo 
'<tr><td>Email:</td>';
   echo 
'<td>' htmlspecialchars($row['EMAIL_ADDR']) . '</td></tr>';
   echo 
'</table>';
   echo 
'<p>Is this correct?</p>';

   
mysql_close($GLOBALS['DB']);
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <div>
  Enter your user id: <input type="text" name="user_id"/>
  <input type="submit" value="Submit"/>
  <input type="hidden" name="submitted" value="true"/>
 </div>
</form>
<?php
}
?>
</html>
The authors got the point across very well. I just posted this code
here for anyone who wants to see it work using an actual form.

NEXT ISSUE

On page 323, about 12 lines down, there is a gray box
illustrating a line of code. There is a typo here,
there needs to be a single quote before USER on the
second line. They do have this correct in the completed
code at the bottom of the page. Anyway, here is how it
should look
PHP Code:
$query sprintf('SELECT USERNAME, EMAIL_ADDR FROM USER WHERE ' .
'USER_ID = %d'$_POST['user_id']); 





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 12 - Security, Path Traversal kenj BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 0 November 12th, 2010 03:57 PM
sql injection trufla Classic ASP Basics 2 June 16th, 2008 02:54 PM
SQl Injection through ASP and MS SQl 2000 cancer10 Classic ASP Databases 1 October 27th, 2007 03:21 AM
SQL Injection cygnusx04 Classic ASP Databases 1 November 6th, 2004 11:06 AM
Security: Preventing SQL Injection taliesin Classic ASP Professional 2 July 4th, 2003 02:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.