Thông tin tài liệu
.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 (http://www.phparch.com), 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
applications and writing new systems with security in mind.
This book gives you a step-by-step guide to each security-related topic,
providing you with real-world examples of proper coding practices and their
implementation in PHP in an accurate, concise and complete way.
¸ Provides techniques applicable to any version of PHP,
including 4.x and 5.x
¸ Includes a step-by-step guide to securing your applications
¸ Includes a comprehensive coverage of security design
¸ Teaches you how to defend yourself from hackers
¸ Shows you how to distract hackers with a “tar pit” to help you
fend off potential attacks
Foreword by Rasmus Lerdorf
Licensed to:
Blake Hartle
hartles99him@gmail.com
User #60389
PHP|ARCHITECT’S GUIDE TO
PHP SECURITY
by Ilia Alshanetsky
Download from Wow! eBook <www.wowebook.com>
php|architect’s Guide to Security
Contents Copyright © 2005 Ilia Alshanetsky – All Rights Reserved
Book and cover layout, design and text Copyright © 2005 Marco Tabini & Associates, Inc. – All Rights Reserved
First Edition: First Edition
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.
Tor onto , ON M3H 1B7
Canada
(416) 630-6202
(877) 630-6202 toll free within North America
info@phparch.com / www.phparch.com
Marco Tabini, Publisher
Edited By Martin Streicher
Tec hnic al R evie wers Marco Tabini
Layout and Design Arbi Arzoumani
Managing Editor Emanuela Corso
Download from Wow! eBook <www.wowebook.com>
About the Author
Ilia Alshanetsky is the principal of Advanced Internet Designs Inc., a company that specializes in security auditing, per-
formance analysis and application development.
He is the author of FUDforum (http://fudforum.org), a highly popular, Open Source bulletin board focused on provid-
ing the maximum functionality at the highest level of security and performance.
Ilia is also a Core PHP Developer who authored or co-authored a series of extensions, including SHMOP, PDO, SQLite,
GD and ncurses. An active member of PHP’s Quality Assurance Team, he is responsible for hundreds of bug fixes, as
well as a sizable number of performance tweaks and features.
Ilia is a regular speaker at PHP-related conferences worldwide and can often be found teaching the Zend Certification
Training and Professional PHP Development courses that he has written for php|architect. He is also a prolific author, with
articles for PHP|Architect, International PHP Magazine, Oracle Technology Network, Zend.com and others to his name.
Ilia maintains an active blog at
http://ilia.ws
, filled tips and tricks on how to get the most out of PHP.
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
To my parents,
Who are and have been my pillar of support
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
Contents
Foreword • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 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
String Validation • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 30
Content Size Validation • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 34
White List Validation • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 36
Download from Wow! eBook <www.wowebook.com>
8
Contents
Being Careful with File Uploads • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 37
Configuration Settings • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 37
File Input • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 38
File Content Validation • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 39
Accessing Uploaded Data • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 41
File Size • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 42
The Dangers of Magic Quotes • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 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
Handling Valid Attributes • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 63
URL Attribute Tricks • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 64
XSS via Environment Variables • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 66
IP Address Information • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 66
Referring URL • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 67
Script Location • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 67
More Severe XSS Exploits • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 68
Cookie/Session Theft • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 69
Form Data Theft • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 70
Changing Page Content • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 71
3 SQL Injection • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 73
Magic Quotes • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 74
Prepared Statements • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 75
No Means of Escape • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 77
The LIKE Quandary • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 78
SQL Error Handling • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 79
Download from Wow! eBook <www.wowebook.com>
9Contents
Authentication Data Storage • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 80
Database Permissions • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 83
Maintaining Performance • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 83
Query Caching • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 85
4 Preventing Code Injection • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 87
Path Validation • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 88
Using Full Paths • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 88
Avoiding Dynamic Paths • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 89
Possible Dangers of Remote File Access • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 89
Validating File Names • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 91
Securing Eval • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 94
Dynamic Functions and Variables • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 95
Code Injection via PCRE • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 97
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! • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 115
Server Side Weakness • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 115
URL Sessions • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 115
Session Fixation • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 117
Surviving Attacks • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 117
Native Protection Mechanism • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 118
User-land Session Theft • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 119
Expiry Time Tricks • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 119
Download from Wow! eBook <www.wowebook.com>
[...]... 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... people to read a book like this one that outlines common security problems 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 should also use this book to take a higher-level look at security. .. 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... subsystem to end-user data input What other sub-systems are in your architecture? Are they appropriately protected against direct user input? There is no security panacea here.—nobody will ever be able to provide one The closest we will get is to try to improve the overall awareness of these issues and to provide better tools for solving them Having a straightforward architecture that is easy to understand... servers PHP s rapid growth can be attributed to its simplicity, its everevolving capabilities, and its excellent performance Unfortunately, the same qualities that have made PHP so popular have also lulled many developers into a sense of complacency, leading them to neglect a very important aspect of development: security When PHP was still young and used primarily for hobbyist applications, security. .. only way to spot uninitialized variables in PHP is to elevate the error reporting level to E_ALL But even then, a red flag is raised only if the script tries to use an uninitialized variable In a scripting language such as PHP, where the script is interpreted each execution, it is inefficient for the compiler to analyze the code for uninitialized variables, so it’s simply not done However, the executor is... the use of htaccess to specify configuration directives, so strive to write code that is register globals-safe When superglobals were added to PHP, a special superglobal was added specifically to simplify the transition from older code The $_REQUEST superglobal combines the values from GET, POST, and cookies into a single array for ease of use But as PHP often demonstrates, the road to hell is paved with... data is to be used For instance, you might expect one parameter to contain numeric values and another to adhere to a certain pattern Validating Numeric Data If a parameter is supposed to be numeric, validating it is exceptionally simple: simply cast the parameter to the desired numeric type $_GET[‘product_id’] = (int) $_GET[‘product_id’]; $_GET[‘price’] = (float) $_GET[‘price’]; A cast forces PHP to convert... 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 Download from Wow! eBook Foreword Foreword issues are likely to appear, and... I started the PHP project years ago, the goal was to develop a tool for solving 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 . 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. 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. 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
Ngày đăng: 29/03/2014, 04:20
Xem thêm: php|architect''''s Guide to PHP Security pdf, php|architect''''s Guide to PHP Security pdf