.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
PHP
|
ARCHITECT
’
S
G
UIDE
TO
PHP S
ECURITY
php|architect’sGuidetoSecurity
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 tradetrade-marks of Marco Tabini & Associates Inc.
BulkCopies
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 info@phparch.com / www.phparch.com Marco Tabini, Publisher
AbouttheAuthor
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.
Tomyparents,
Contents
Foreword
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •13
Introduction
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •17
1
InputValidation
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •21
TheTroublewithInput •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 22
AnAlternativetoRegisterGlobals:Superglobals•••••••••••••••••••••••••••••••••••••••••••••• 25
TheConstantSolution ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 25
The$_REQUESTTrojanHorse ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 27
ValidatingInput ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 28
ValidatingNumericData •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 28
LocaleTroubles•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 29
StringValidation••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 30
ContentSizeValidation •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 34
8 Contents
BeingCarefulwithFileUploads••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 37
ConfigurationSettings ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 37
FileInput•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 38
FileContentValidation •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 39
AccessingUploadedData ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 41
FileSize•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 42
TheDangersofMagicQuotes•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 43
MagicQuotesNormalization••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 44
MagicQuotes&Files ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 46
ValidatingSerializedData••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 47
ExternalResourceValidation ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 49
2
Cross-SiteScriptingPrevention
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •53
TheEncodingSolution •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 54
HandlingAttributes •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 54
HTMLEntities&Filters•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 56
ExclusionApproach •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 60
HandlingValidAttributes ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 63
URLAttributeTricks•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 64
XSSviaEnvironmentVariables •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 66
IPAddressInformation •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 66
ReferringURL•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 67
ScriptLocation •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 67
MoreSevereXSSExploits•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 68
Cookie/SessionTheft •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 69
FormDataTheft••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 70
ChangingPageContent••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 71
3
SQLInjection
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •73
MagicQuotes •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 74
PreparedStatements••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 75
NoMeansofEscape •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 77
TheLIKEQuandary•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 78
9 Contents
AuthenticationDataStorage ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 80
DatabasePermissions•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 83
MaintainingPerformance •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 83
QueryCaching••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 85
4
PreventingCodeInjection
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •87
PathValidation••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 88
UsingFullPaths ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 88
AvoidingDynamicPaths •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 89
PossibleDangersofRemoteFileAccess••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 89
ValidatingFileNames •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 91
SecuringEval••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 94
DynamicFunctionsandVariables •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 95
CodeInjectionviaPCRE••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 97
5
CommandInjection
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •101
ResourceExhaustionviaCommandInjection ••••••••••••••••••••••••••••••••••••••••••••••••• 102
ThePATHExploit ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 104
HiddenDangers ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 105
ApplicationBugsandSettingLimits•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 106
PHPExecutionProcess •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 108
6
SessionSecurity
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •113
Sessions&Cookies ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 114
ManintheMiddleAttacks •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 114
EncryptiontotheRescue!••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 115
ServerSideWeakness••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 115
URLSessions••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 115
SessionFixation••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 117
SurvivingAttacks•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 117
NativeProtectionMechanism ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 118
User-landSessionTheft ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 119