PHP Architect's Guide to PHP Security - Pdf 17

.424
7.50 x 9.25
7.50 x 9.25
php|architect’s
Guide to
PHP Security
A Step-by-step Guide to Writing
Secure and Reliable PHP Applications
Ilia Alshanetsky
php|architect’s
Guide to
PHP Security
NanoBooks are excellent, in-depth resources created by the publishers of
php|architect (), the world’s premier magazine dedicated
to PHP professionals.
NanoBooks focus on delivering high-quality content with in-depth analysis and
expertise, centered around a single, well-defined topic and without any of the fluff
of larger, more expensive books.
Shelve under PHP/Web Development/Internet Programming
From the publishers of
php|architect’s Guide to PHP Security
Ilia Alshanetsky
US $32.99
Canada $47.99
UK (net) £18.99
With the number of security flaws and exploits discovered and released
every day constantly on the rise, knowing how to write secure and reliable
applications is become more and more important every day.
Written by Ilia Alshanetsky, one of the foremost experts on PHP security in
the world, php|architect’s Guide to PHP Security focuses on providing you
with all the tools and knowledge you need to both secure your existing

ISBN 0-9738621-0-6
Produced in Canada
Printed in the United States
No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, with
-
out the prior written permission of the publisher, except in the case of brief quotations embedded in critical reviews or
articles.
Disclaimer
Although every effort has been made in the preparation of this book to ensure the accuracy of the information contained
therein, this book is provided “as-is” and the publisher, the author(s), their distributors and retailers, as well as all af
-
filiated, related or subsidiary parties take no responsibility for any inaccuracy and any and all damages caused, either
directly or indirectly, by the use of such information.
We have endeavoured to properly provide trademark information on all companies and products mentioned in this book
by the appropriate use of capitals. However, we cannot guarantee the accuracy of such information.
Marco Tabini & Associates, The MTA logo, php|architect, the php|architect logo, NanoBook and NanoBook logo are trade
-
marks or registered trademarks of Marco Tabini & Associates Inc.
Bulk Copies
Marco Tabini & Associates, Inc. offers trade discounts on purchases of ten or more copies of this book. For more informa-
tion, please contact our sales offices at the address or numbers below.
Credits
Written by Ilia Alshanetsky
Published by
Marco Tabini & Associates, Inc.
28 Bombay Ave.
Toronto, ON M3H 1B7
Canada
(416) 630-6202
(877) 630-6202 toll free within North America

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
13
Introduction
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
17
1
Input Validation
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
21
The Trouble with Input
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
22
An Alternative to Register Globals: Superglobals
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
25
The Constant Solution
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
25
The $_REQUEST Trojan Horse
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
27
Validating Input
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
28
Validating Numeric Data
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
28
Locale Troubles
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
29

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
43
Magic Quotes Normalization
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
44
Magic Quotes & Files
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
46
Validating Serialized Data
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
47
External Resource Validation
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
49
2
Cross-Site Scripting Prevention
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
53
The Encoding Solution
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
54
Handling Attributes
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
54
HTML Entities & Filters
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
56
Exclusion Approach
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
60

3
SQL Injection
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
73
Magic Quotes
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
74
Prepared Statements
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
75
No Means of Escape
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
77
The LIKE Quandary
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
78
SQL Error Handling
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
79
9Contents
Authentication Data Storage
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
80
Database Permissions
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
83
Maintaining Performance
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
83
Query Caching

5
Command Injection
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
101
Resource Exhaustion via Command Injection
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
102
The PATH Exploit
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
104
Hidden Dangers
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
105
Application Bugs and Setting Limits
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
106
PHP Execution Process
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
108
6
Session Security
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
113
Sessions & Cookies
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
114
Man in the Middle Attacks
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
114
Encryption to the Rescue!

121
Securing Session Storage
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
122
Session ID Rotation
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
126
IP Based Validation
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
128
Browser Signature
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
129
Referrer Validation
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
130
User Education
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
131
7
Securing File Access
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
135
The Dangers of “Worldwide” Access
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
136
Securing Read Access
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
137
PHP Encoders

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
146
File Masking
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
147
8
Security through Obscurity
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
153
Words of Caution
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
153
Hide Your Files
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
154
Obscure Compiled Templates
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
156
Transmission Obfuscation
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
158
Obscure Field Names
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
158
Field Name Randomization
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
159
Use POST
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
160

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
171
Smart Cookie Tricks
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
173
Record the Referring URL
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
173
Capture all Input Data
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
174
Build a Tar Pit
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
176
10
Securing Your Applications
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
179
Enable Verbose Error Reporting
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
180
Replace the Usage of Register Globals
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
180
Avoid $_REQUEST
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
181
Disable Magic Quotes
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
182

ing the Web problem by removing barriers and simplifying the interaction between
the web server and the hundreds of sub-systems required to solve a wide variety of
problems. Over the years, I think we have achieved that. PHP has allowed people with all sorts
of different backgrounds to put their ideas on the Web. To me, this is the success of PHP and
what keeps me motivated to continue working on it.
With all the success of PHP, I will be the first to admit that there are areas where we haven’t
done a very good job of educating and providing people with the tools they need. Security is
at the top of that list—we have simplified access to things, provided a language and a set of
functions to do anything anybody could want to do, but we have not provided much in the way
of tools or guidance aimed at helping people write secure applications. We have been content
with being on par with other environments in this respect, while in almost all other areas we
have strived to be better.
Security is not easy. People have to understand their systems well to know where security
Foreword
14
Foreword
issues are likely to appear, and they have to remember to actually check. Like a small hole in
a balloon, one missed security check will burst their application. PHP provides a number of
tools to help people address security problems, but without a good understanding of when
and how to apply them, they aren’t very useful. We will therefore need a combined effort to try
to collectively achieve better security. Users need to become better educated, and we need to
provide better tools.
Recently, a number of automated security scanners have appeared. Primarily, these detect
cross-site scripting problems, but they also catch the occasional SQL injection. The main thing
I have gotten out of seeing the results of these scans is that the web application security prob-
lem is pervasive and doesn’t care what language an application is written in.
A first step is for people to read a book like this one that outlines common security prob-
lems in web applications. And, while the solutions presented here are all PHP-based using the
tools provided by PHP, most of the problems apply to any language and environment. People
should use this book to solve their PHP-based web application security problems, but they

quite straightforward. And that’s important: if implementing security is prohibitively difficult,
few developers will bother.


P
ractically all software applications depend on some form of user input to create out-
put. This is especially true for web applications, where just about all output depends on
what the user provides as input.
First and foremost, you must realize and accept that any user-supplied data is inherently
unreliable and cannot be trusted. By the time input reaches PHP, it’s passed through the user’s
browser, any number of proxy servers and firewalls, filtering tools on your server, and possibly
other processing modules. Any one of those “hops” have an opportunity—be it intentional or
accidental—to corrupt or alter the data in some unexpected manner. And because the data ul-
timately originates from a user, the input could be coerced or tailored out of curiosity or malice
to explore or push the limits of your application. It is absolutely imperative to validate all user
input to ensure it matches the expected form.
There’s no “silver bullet” that validates all input, no universal solution. In fact, an attempt to
devise a broad solution tends to cause as many problems as it solves—as PHP’s “magic quotes”
will soon demonstrate. In a well-written, secure application, each input has its own validation
1
Input Validation
22
Input Validation
routine, specifically tailored to the expected data and the ways it’s used. For example, integers
can be verified via a fairly simple casting operation, while strings require a much more verbose
approach to account for all possible valid values and how the input is utilized.
This chapter focuses on three things:
• How to identify input methods. (Understanding how external data makes its way into
a script is essential.)
• How each input method can be exploited by an attacker.

php.ini
directives control the result of the merge: the older
gpc_order
and the newer
variables_order
. Both settings reflect the relative priority of each input source. The default or-
der for
gpc_order
is
GPC
(for
GET
,
POST
, cookie, respectively), where cookie has the highest prior-
ity; the default order for
variables_order
is
EGPCS
(system Environment,
GET
,
POST
, cookie, and
Server environment, respectively). According to both defaults, if parameter
id
is supplied via
a
GET
and a cookie, the cookie’s value for

of the request.
However, while this approach doesn’t lose data and makes it very clear where data is
coming from, the
$HTTP_*_VARS
variables aren’t global and using them from within func-
tions and methods makes for very tedious code. For instance, to import
$HTTP_GET_VARS

into the scope of a method or function, you must use the special
$GLOBALS
variable, as in
$GLOBALS[‘HTTP_GET_VARS’]
, and to access the value of id, you must write the longwinded
$GLOBALS[‘HTTP_GET_VARS’][‘id’]
.
In comparison, the variable
$id
can be imported into the function via the much simpler
(but error-prone)
$GLOBALS[‘id’]
. It’s hardly surprising that many developers chose the path
of least resistance and used the simpler, but much less secure register global variables. Indeed,
the vulnerability of register globals ultimately led to the option being disabled by default.
For a perspective, consider the following code:
if (is_authorized_user()) {
$auth = TRUE;
}
if ($auth) {
/* display content intended only for authorized users */
}

However, the executor is aware of uninitialized variables and raises notices (
E_NOTICE
) if your
error reporting level is set to
E_ALL
.
# Inside PHP configuration
error_reporting=E_ALL
# Inside httpd.conf or .htacces for Apache
# numeric values must be used
php_value error_reporting 2047
# You can even change the error
# reporting level inside the script itself
error_reporting(E_ALL);
While raising the reporting level eventually detects most uninitialized variables, it doesn’t de-
tect all of them. For example, PHP happily appends values to a nonexistent array, automatically
creating the array if it doesn’t exist. This operation is quite common and unfortunately isn’t
flagged. Nonetheless, it is very dangerous, as demonstrated in this code:
# Assuming script.php?del_user[]=1&del_user[]=2 & register_globals=On
$del_user[] = “95”; // add the only desired value
foreach ($del_user as $v) {
mysql_query(“
DELETE FROM users WHERE id=”.(int)$v);
}
Above, the list of users to be removed is stored inside the
$del_user
array, which is supposed
to be created and initialized by the script. However, since register globals is enabled,
$del_user


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status