2,511 153 11MB
Pages 330 Page size 252 x 323.64 pts Year 2008
From the Library of Lee Bogdanoff
Download at WoweBook.Com
Securing PHP Web Applications
From the Library of Lee Bogdanoff
Download at WoweBook.Com
This page intentionally left blank
From the Library of Lee Bogdanoff
Download at WoweBook.Com
Securing PHP Web Applications
Tricia Ballad William Ballad
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City
From the Library of Lee Bogdanoff
Download at WoweBook.Com
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the United States please contact: International Sales [email protected] Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data Ballad, Tricia. Securing PHP web applications / Tricia Ballad, William Ballad. p. cm. Includes index. ISBN 978-0-321-53434-7 (pbk. : alk. paper) 1. PHP (Computer program language) 2. Web services—Security measures. 3. Internet—Computer programs—Security measures. 4. Application software—Development. I. Ballad, Bill. II. Title. QA76.73.P224B35 2009 005.8—dc22 2008042783 Copyright © 2009 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671-3447 ISBN-13: 978-0-321-53434-7 ISBN-10: 0-321-53434-4 Text printed in the United States on recycled paper at Donnelley in Crawfordsville, Indiana First printing, December 2008
From the Library of Lee Bogdanoff
Download at WoweBook.Com
Contents
Acknowledgments
xiii
About the Authors
xv
Part I Chapter 1
Part II Chapter 2
Web Development Is a Blood Sport—Don't Wander onto the Field Without a Helmet
1
Security Is a Server Issue and Other Myths
3
Reality Check Security Is a Server Issue Hackers Gain Control Through Insecure Applications Programmers Can Harden Their Own Applications Security Through Obscurity Native Session Management Provides Plenty of Security “My Application Isn’t Major Enough to Get Hacked” The “Barbarians at the Gate” Syndrome Wrapping It Up
3 5 5 6 7 9 9 10 10
Is That Hole Really Big Enough to Drive a Truck Through?
11
Error Handling
13
The Guestbook Application Program Summary Primary Code Listing
13 13 14
v From the Library of Lee Bogdanoff
Download at WoweBook.Com
CONTENTS
Chapter 3
Users Do the Darnedest Things . . . I Wonder What Will Happen If I Do This? Expecting the Unexpected Building an Error-Handling Mechanism Test for Unexpected Input Decide What to Do with Erroneous Data Make the System Mind-Numbingly Easy to Use Wrapping It Up
15 15 18 19 20 23 24 26
System Calls
27
Navigating the Dangerous Waters of exec(), system(), and Backticks Using System Binaries with the SUID Bit and sudo Using System Resources Using escapeshellcmd() and escapeshellarg() to Secure System Calls
Create an API to Handle All System Calls Why Not Just Escape the Arguments and Be Done? Validate User Input Patch the Guestbook Application The moveFile()Function Changes to the Application Wrapping It Up
27 28 29 30 30 30 31 31 32 32 32 34 34
What's In a Name? More Than You Expect
35
escapeshellcmd() escapeshellarg()
Part III Chapter 4
Chapter 5
Buffer Overflows and Variable Sanitation
37
What Is a Buffer, How Does It Overflow, and Why Should You Care? Buffers, Stacks, Heaps, and Memory Allocation Consequences of a Buffer Overflow Memory Allocation and PHP Pay Attention to the Latest Security Alerts Prevent Buffer Overflows by Sanitizing Variables Premise: Data Is Guilty Until Proven Innocent, Especially If It Comes from Outside the Application Where Does Data Come From? How to Sanitize Data to Prevent Buffer Overflows Patch the Application Verify That We’re Running the Latest Stable Versions Check Variable Sanitation Wrapping It Up
37 39 42 42 44 46
Input Validation
53
New Feature: Allow Users to Sign Their Guestbook Comments
53
46 48 48 49 49 51 52
vi From the Library of Lee Bogdanoff
Download at WoweBook.Com
CONTENTS
The Problem: Users Who Give You More Than You Asked For Spammers Injection Attacks Assumptions: You Know What Your Data Looks Like Database Constraints Logical Constraints The Solution: Regular Expressions to Validate Input Tainted Data Regexes 101 That Greedy, Lazy . . . Regex! Common Input Validation Patterns Wrapping It Up
54 55 55 55 56 56 57 57 58 62 65 67
Filesystem Access: Accessing the Filesystem for Fun and Profit
69
Opening Files Local Filesystem Access Remote Filesystem Access Preventing Remote Filesystem Exploits Creating and Storing Files Allowing File Uploads Storing Files Safely Changing File Properties Safely Changing File Permissions in UNIX, Linux, and Mac OS X Changing Windows File Permissions Changing File Permissions in PHP Patching the Application to Allow User-Uploaded Image Files Modify the API Create the Upload Form Wrapping It Up
69 69 71 72 73 73 75 76 76 77 87 88 88 90 90
Part IV
“Aw come on man, you can trust me”
93
Chapter 7
Authentication
95
Chapter 6
What Is User Authentication? Usernames and Passwords Image Recognition Privileges How to Authenticate Users Directory-Based Authentication User Database Storing Usernames and Passwords Encryption Password Strength Assess Your Vulnerability
95 97 99 100 101 101 114 115 115 116 117
vii From the Library of Lee Bogdanoff
Download at WoweBook.Com
CONTENTS
Chapter 8
Chapter 9
Chapter 10
Patching the Application to Authenticate Users Add User Database Table and Double-Check Database Security Create Authentication API Wrapping It Up
117 118 119 120
Encryption
121
What Is Encryption? Choosing an Encryption Type Algorithm Strength Speed Versus Security Use of the Data Password Security Patching the Application to Encrypt Passwords Modifying the User Table Create the Encryption and Salting Functions Modify the Password Validation System Wrapping It Up
121 123 123 124 124 125 125 126 126 127 128
Session Security
129
What Is a Session Variable? Major Types of Session Attacks Session Fixation Session Hijacking Session Poisoning Patching the Application to Secure the Session Wrapping It Up
129 129 130 131 133 133 136
Cross-Site Scripting
137
What Is XSS? Reflected XSS Stored XSS Patching the Application to Prevent XSS Attacks Wrapping It Up
137 137 138 138 139
Part V
Locking Up for the Night
141
Chapter 11
Securing Apache and MySQL
143
Programming Languages, Web Servers, and Operating Systems Are Inherently Insecure Securing a UNIX, Linux, or Mac OS X Environment Update the Operating System Securing Apache Upgrade or Install the Latest Stable Version of Apache Give Apache Its Own User and Group
143 144 145 147 147 149
viii From the Library of Lee Bogdanoff
Download at WoweBook.Com
CONTENTS
Chapter 12
Chapter 13
Hide the Version Number and Other Sensitive Information Restrict Apache to Its Own Directory Structure Disable Any Options You Don’t Explicitly Need Install and Enable ModSecurity Securing MySQL Upgrade or Install the Latest Version Disable Remote Access Change Admin Username and Password Delete Default Database Users and Create New Accounts for Each Application Delete the Sample Databases Wrapping It Up
164 165 166
Securing IIS and SQL Server
167
Securing a Windows Server Environment Update the Operating System Securing IIS Reduce the Server’s Footprint Secure the Web Root Securing SQL Server Install or Upgrade to the Latest Version Secure Microsoft SQL Server Wrapping It Up
167 168 177 177 179 187 187 200 205
Securing PHP on the Server
207
Using the Latest Version of PHP Examining the Zend Framework and Zend Optimizer Finding the Latest Stable Version of PHP Using the Suhosin Patch and Extension Using the Security Features Built into PHP and Apache SuEXEC Using ModSecurity Hardening php.ini Wrapping It Up
207 208 212 213 213 213 214 215 216 218
Introduction to Automated Testing
219
Why Are We Talking About Testing in a Security Book? Testing Framework Types of Tests Unit Tests System Tests Choosing Solid Test Data Wrapping It Up
219 220 222 222 223 223 224
safe_mode
Chapter 14
151 152 153 154 159 159 163 163
ix From the Library of Lee Bogdanoff
Download at WoweBook.Com
CONTENTS
Chapter 15
Part VI Chapter 16
Chapter 17
Epilogue
Introduction to Exploit Testing
225
What Is Exploit Testing? Fuzzing Installing and Configuring PowerFuzzer Using PowerFuzzer Testing Toolkits Obtaining CAL9000 Using CAL9000 Proprietary Test Suites Benefits and Features of a Proprietary Test Suite Using a Proprietary Test Suite to Scan Your Application Wrapping It Up
225 226 227 231 233 234 235 246 246 247 254
“Don’t Get Hacked” Is Not a Viable Security Policy
255
Plan A: Designing a Secure Application from the Beginning
257
Before You Sit Down at the Keyboard . . . Concept Summary Workflow and Actors Diagram Data Design Infrastructure Functions Identifying Points of Failure Login and Logout File Upload User Input Filesystem Access Wrapping It Up
257 257 260 260 267 269 269 270 270 271 271
Plan B: Plugging the Holes in Your Existing Application
273
Set Up Your Environment Using a Three-Stage Deployment Using Version Control Application Hardening Checklist Check Your Server Security Find the Vulnerabilities in Your Code Fix the Most Obvious Problems Have Your Code Peer-Reviewed Wrapping It Up
273 273 275 276 276 276 277 278 278
Security Is a Lifestyle Choice: Becoming a Better Programmer
279
Avoid Feature Creep Write Self-Documenting Code Use the Right Tools for the Job Have Your Code Peer-Reviewed Wrapping It Up
279 280 282 283 284
x From the Library of Lee Bogdanoff
Download at WoweBook.Com
CONTENTS
Appendix
Additional Resources
285
PEAR Books Web Sites Tools Integrated Development Environments (IDE) and Frameworks Exploit Testing Tools Automated Testing Tools
285 286 287 288 288 288 288
Glossary
289
Index
293
xi From the Library of Lee Bogdanoff
Download at WoweBook.Com
This page intentionally left blank
From the Library of Lee Bogdanoff
Download at WoweBook.Com
Acknowledgments
We would like to thank the entire production team at Addison-Wesley, especially our acquisitions editor, Jessica Goldstein, Romny French, and our developmental editor, Chris Zahn, for their heroic patience throughout this process, and for recovering so gracefully when life trampled all over various deadlines. Thanks also to our copy editor, Barbara Wood, for going out of her way to be sure we had every Web site name typed correctly and for gently pointing out the value of consistency when it comes to things like formatting. Finally, a special thanks to our tech reviewers, especially Andy Lester, for catching things we were just too close to the material to see. When this adventure first began, two colleagues with very different perspectives offered the encouragement and enthusiasm for this project that convinced us that this book needed to be written. Tony Bradley at About.com took time he didn’t really have to review our initial proposal and offer suggestions for strengthening it before we sent it out. Susan Scheid, developer of the OptionCart e-commerce system, pointed out to us how many PHP developers routinely disregard security issues because those issues simply haven’t been explained in a clear, straightforward manner. In a very real sense, Susan, we wrote this book for you. We hope it clears things up a bit. Finally, our deepest appreciation to Dad, Mary Lou and David, and Mom and Dad Forsha for the many weekends they spent keeping three young boys entertained when they could have been enjoying peace and quiet. This book literally would not exist without you.
xiii From the Library of Lee Bogdanoff
Download at WoweBook.Com
This page intentionally left blank
From the Library of Lee Bogdanoff
Download at WoweBook.Com
About the Authors
Tricia Ballad spent several years as a Web applications developer on the LAMP (Linux, Apache, MySQL, PHP/Perl) platform before becoming a full-time writer and technical editor. She writes online courseware on various consumer electronics and computing subjects. William Ballad has worked in every aspect and at every level of information technology, from his days as a hardware technician at a small mom-and-pop ISP to architecting and maintaining Windows-based servers and heterogeneous networks for some of the world’s largest corporations. He has been an active member of the online information security community for many years and recently led an effort to counter an international hacker group exploiting OptionCart, a widely used e-commerce solution. William and Tricia have collaborated on and co-authored several books on Web application programming, including PHP & MySQL Web Development All-in-One Desk Reference for Dummies (Wiley Publishing, 2008). They have seen firsthand the damage that can be done to shared hosting through a single insecure application.
xv From the Library of Lee Bogdanoff
Download at WoweBook.Com
This page intentionally left blank
From the Library of Lee Bogdanoff
Download at WoweBook.Com
PART I WEB DEVELOPMENT IS A BLOOD SPORT— DON'T WANDER ONTO THE FIELD WITHOUT A HELMET
1 From the Library of Lee Bogdanoff
Download at WoweBook.Com
This page intentionally left blank
From the Library of Lee Bogdanoff
Download at WoweBook.Com
Security Is a Server Issue and Other Myths
Welcome! The purpose of this chapter is to tackle some of the most common PHP security myths head-on. The last thing we want is for novice PHP programmers to get a false sense of security because they obfuscate their filenames or directory structure. Those tricks simply don’t work against hackers who have plenty of time and computer resources. The chapter will focus on five common myths.
REALITY CHECK If you’re reading this, we know two things about you: First, you write PHP applications that run online. Second, you’re not a hard-core security guru. In fact, you’re probably holding this book right now because other security books left you with more questions than you started with, or because this is the first time you’ve really thought about securing your applications. Our goal in writing this book is to give you the tools you need to make your applications more secure. By their nature, Web applications are inherently insecure. You are allowing unknown users to have direct access to your server. Even if you have a firewall, you have to poke a hole in it to allow your Web application to be accessible to the outside world. These are not security-minded actions. Add to that the fact that we are writing insecure applications in PHP, a language that is inherently insecure. It doesn’t have strongly typed variables, it utilizes global variables, and users can make function calls through the browser. Many programmers
3 From the Library of Lee Bogdanoff
Download at WoweBook.Com
CHAPTER 1
SECURITY IS A SERVER ISSUE AND OTHER MYTHS
consider these to be features of PHP, not liabilities, but we’re examining Web applications from a security standpoint, not from a convenience or functional standpoint. If you want a truly secure application, don’t connect it to the Web. If you want to truly secure PHP code, write a wrapper that sits between PHP and everything else, keeping it safe. The Hardened-PHP Group is working on this type of wrapper, but we’ll get to that in Chapter 13, “Securing PHP on the Server.” All we are trying to do—all we can do—is make it harder for malicious users to attack our applications. We can never create truly secure code, but we can write code that is secure enough. The good news is that most hackers are fundamentally lazy. If our applications are reasonably secure, the vast majority of hackers will leave them alone because there are plenty of easier targets. We don’t need to run faster than the bad guys; we just need to run faster than the pack so they will pick an easier target. There are a few points to keep in mind as we try to outrun the pack. First, security in depth is key. Never rely on just one method of protecting your applications. If that one method is compromised, you’re out of luck. A multilayered approach to security, one that involves your server, network, code, files, database, users, etc., will mitigate compromises in any one level. In this book, we focus mainly on the code, touching lightly on the rest. Out in the field you may not have control over some of the other aspects, such as server security, but you can keep depth in mind and insist on knowing what security measures your vendors, such as your Web hosting company, have implemented. The second point to remember is one that will keep you sane in any aspect of IT: Assume everyone else you deal with is either incompetent or malicious; never fully trust the security and error-handling measure that is handed to you by other programmers, other applications, etc. This sounds harsh, but in the world of Internet security, you have to be a little bit paranoid. Trust in the basic goodness of humanity later. While you’re securing a Web application, trust no one—especially your users and the data they send you. Verify every scrap of data that goes into or out of your application, regardless of its source. You can never know if other code has a hole in it or not (remember, Web applications are inherently insecure), so verify that data looks the way your application expects it to look before you act on it. Finally, let’s get our terms straight. Throughout this book we’ve used the term hacker to refer to malicious users whose goal is to break into or crash Web servers and otherwise make life difficult for the rest of us. There are some who will object to this usage because the word hacker also refers to anyone who digs into the guts of a system (whether it’s a server, an application, or the cable box) to see how it works and to improve upon it. If you prefer that usage, feel free to mentally substitute cracker for hacker throughout the book. Since the point of this book is to introduce security concepts to those who have no prior experience, we chose to use the term that the widest
4 From the Library of Lee Bogdanoff
Download at WoweBook.Com
SECURITY IS A SERVER ISSUE
possible audience would immediately understand. Let’s not get bogged down in terminology when there are bad guys out there right now who don’t care what we call them as long as we leave our applications nice and insecure.
SECURITY IS A SERVER ISSUE One of the most common misconceptions surrounding application security is that keeping the Web server secure is the job of the system administrator, not the application programmer. In reality, keeping hackers at bay is the responsibility of everyone involved with the server. The purpose of this book is to demonstrate two crucial points to application programmers: • Hackers usually gain control of servers through holes created by insecure applications. • Application programmers can close the holes in their applications without dropping everything to earn a degree in computer science. System administrators do have a role in securing the Web server, and if you happen to wear both the system administration and application programming hats, be sure to read Chapter 13, “Securing PHP on the Server.” The rest of the book, however, is devoted to the ways that hackers exploit insecure applications and how you can be sure that yours isn’t one of them.
HACKERS GAIN CONTROL THROUGH INSECURE APPLICATIONS Some hackers do attack servers and networks directly, but most search for insecure applications running on those servers and use them as a gateway to the server and network. Why do they focus on applications, rather than the true targets—servers and networks? They target applications because those are often the weakest parts of the system. Physical security and the network protect the server itself. The network is protected by a firewall. But the applications running on the server are often an open door that bypasses both physical and network security, as shown in Figure 1.1. That’s why hackers target applications—they’re a lot easier to break into than either the physical server room or most networks. Securing the server room can be as simple as installing a good deadbolt lock on the front door of the building. You can get more complex locks, but a simple deadbolt will give you a reasonable level of physical security. Networks are similar—as long as you have a firewall and perhaps an
5 From the Library of Lee Bogdanoff
Download at WoweBook.Com
CHAPTER 1
SECURITY IS A SERVER ISSUE AND OTHER MYTHS
Network Security Physical Security Application
The Internet
Applications running on the server are often an open door that bypasses both physical and network security. Figure 1.1
intrusion detection system running, you have a reasonably secure network. Security at the application level requires that the programs running on the server be designed with security in mind. That’s the purpose of this book—to give application programmers the tools and knowledge they need to harden their own applications, one step at a time.
PROGRAMMERS CAN HARDEN THEIR OWN APPLICATIONS As with most topics in the world of information technology, security has a reputation for being difficult, complicated, and better left to experts with a dozen certifications, a Ph.D. in computer science, and 20 years of experience in the field. Once you understand the basics, you’ll find that most security concepts really aren’t as difficult as they seemed at first. There are times to call in a security guru, but you don’t need to be an expert to significantly improve the security of your own application. This book distills the information you really need to harden your application, and it gives you a solid understanding of basic application security concepts. Before we get into specific security techniques, let’s take a moment to examine why you need to understand security. As soon as you release your application to the public—even if yours is the only server it ever runs on—you’re a target for hackers.
6 From the Library of Lee Bogdanoff
Download at WoweBook.Com
SECURITY THROUGH OBSCURITY
Even a fairly simple application you write for your own personal use is a potential opening for hackers. Having said that, hackers aren’t necessarily smarter or more highly trained than the average programmer. What they do have is a lot of time on their hands and a desire to test themselves against system administrators and application programmers. As soon as your code is run on a public server, you should assume that a hacker will eventually find it and attempt to break it. It may take years, or you could see the first attempts within days, depending on how attractive your server is to the hacker and how obvious the security holes are. Does this mean you should give up trying to keep hackers out of your code? Of course not. Security breaches aren’t inevitable. They’re so common because most programmers don’t understand the basic methods for securing an application. Once you’ve read this book, you’ll have all the tools you need to make your application more secure than most. Hackers focus their energies on the easiest targets, and you’re taking the first steps to make sure they pass by your application. Don’t worry; all the techniques you’ll learn here are fairly simple and easy, but they make a big difference in the security of your application.
SECURITY THROUGH OBSCURITY Some programmers create complicated directory structures and files with random, meaningless names in the hope of confusing hackers. Unfortunately, because of the way hackers operate, obfuscating filenames and hiding them in complicated directory structures really doesn’t work. This strategy does make your code difficult to maintain and update, but that’s about it. Most hackers don’t personally dig through your application code looking for signs of a vulnerability. They’re fundamentally lazy (in a good way). Rather than doing the long and tedious work of finding vulnerable applications themselves, they write scripts to dig through application code for them. With plenty of CPU cycles and time to burn, eventually those scripts will find their way through the most complex directory structure, as shown in Figure 1.2. Having said that, there is a place for security through obfuscation, if it is part of a larger, more in-depth security plan. William worked with a system administrator in the 1990s who made very good use of the concept of security through obfuscation. He created a false login screen for the server, making it look as if the server were running one operating system when it was really running something else entirely. It was an interesting idea, and it did provide some measure of security because when hackers attempted to break in, they were looking for common vulnerabilities in the fake OS rather than targeting the true OS.
7 From the Library of Lee Bogdanoff
Download at WoweBook.Com
CHAPTER 1
SECURITY IS A SERVER ISSUE AND OTHER MYTHS
Web Root /
/Stuff
/Stuff/Junk
/Stuff/Junk/collect.php /Images
/Images/ Screenshots /Images/Screenshots/ screenshot.jpg /Random
/Random/ Data /Random/Data/ secret_stuff.txt Figure 1.2
Hackers use scripts that methodically traverse any directory structure.
You can use the same technique to provide a layer of security in your application. For example, rather than calling your files *.php, you can call them *.html. No one will be fooled into thinking that your application is pure HTML, but at least you aren’t announcing to the world what language the program is written in. Simply changing the filenames does nothing to actually secure your program, but it does make the hacker work a little harder to find the vulnerabilities. Just don’t forget to tell
8 From the Library of Lee Bogdanoff
Download at WoweBook.Com
“MY APPLICATION ISN’T MAJOR ENOUGH TO GET HACKED”
the Web server to send your .html files through the PHP interpreter before serving them up to the user. In the end, securing your application by hiding important configuration files (such as the one that holds your database connection information) and changing filenames doesn’t hurt anything, but don’t rely on this method alone to keep hackers out of your code.
NATIVE SESSION MANAGEMENT PROVIDES PLENTY OF SECURITY PHP’s native session management capabilities give application programmers some tools to create a secure session environment, but they don’t automatically protect your application against session hijacking, fixation, or poisoning, any more than simply owning a fire extinguisher protects your home from fire. Sessions are widely used in modern Web applications to store everything from authentication information to browsing history, and often they’re used by programmers with only a cursory understanding of them. This makes them a natural target for hackers. In Chapter 9, “Session Security,” we go over three types of session attacks and show you how to defend against them.
“MY APPLICATION ISN’T MAJOR ENOUGH TO GET HACKED” Every day, hackers target minor applications. Why? Because they’re easier targets than bigger, better-known applications. Small, relatively obscure applications—like yours, perhaps?—are easier to break because they are usually written by a single individual with little or no formal security training or access to code reviews and penetration testing facilities. This fact—that small applications are so often the targets of hacker attacks—is the very reason we wrote this book. When we owned a small Web hosting company, several of our clients used a variant of OptionCart, an e-commerce application designed for small Web-based retailers. The particular variation we worked with was not that widely used, but for a few weeks it was at the top of the charts—specifically the CERT security advisories. CERT is the Computer Emergency Response Team based at Carnegie Mellon University. It is one of the security watchdogs on the Internet, and it publishes regular reports of compromised servers, networks, and applications. You do not want your application to gain fame through CERT! We worked with the developer of OptionCart to close several security holes in the application and have expanded on the advice we gave her to create this book.
9 From the Library of Lee Bogdanoff
Download at WoweBook.Com
CHAPTER 1
SECURITY IS A SERVER ISSUE AND OTHER MYTHS
THE “BARBARIANS AT THE GATE” SYNDROME There’s one last idea to tackle before we get down to the business of securing Web applications: the idea that as long as you have strong network security, you don’t have to worry about securing each and every application that runs on the server. After all, if nobody can hack into the network, then nobody can get to the applications, right? Wrong! This is especially true of a Web server, which has to be open to the public in order to serve Web sites. On a server, every single application, from the operating system to the Web server to individual Web applications, is a point of entry. One vulnerability in one application can give a hacker control of the entire server—and the rest of the servers on the network as well. Let’s assume for a moment that your network is completely secure. There’s only one point of entry, and it’s protected by a firewall that’s locked up tight. Only authorized users can access the resources behind the firewall. What happens when one of those authorized users loses his or her temper? We worked with a company that spared no expense to create the most perfectly secure network possible—only to have it compromised within weeks when the system administrator quit and left a Trojan horse behind. There was nothing wrong with the security of the network, except that the guy holding the keys to the gate wasn’t as trustworthy as everyone assumed he was. He had full access to the network after he left the company, and he compromised every server within hours. The good news was, the network remained secure. Unfortunately, having a secure network doesn’t do any good if the servers on the network are wide open. Even securing the servers doesn’t guarantee that hackers will be kept out of the data stored on those servers, because hackers can gain legitimate access to server resources through insecure applications. The point here is to avoid having a single point of failure; if the network is compromised, everything is exposed to attack. If a server is compromised, the data stored on the server is vulnerable. If an application is insecure, even a secured server can be taken over. The better way to secure any system is a three-pronged defense: Secure the network, secure the server, and secure every application. This way, even if one of the three parts of the equation is compromised, the other two should withstand the attack.
WRAPPING IT UP We’ve given you some food for thought in this chapter and convinced you, we hope, that although outwitting hackers isn’t impossible, it’s also not something that “just happens.” The rest of this book gives you the tools and step-by-step information you need to secure your application against attack.
10 From the Library of Lee Bogdanoff
Download at WoweBook.Com
PART II IS THAT HOLE REALLY BIG ENOUGH TO DRIVE A TRUCK THROUGH?
11 From the Library of Lee Bogdanoff
Download at WoweBook.Com
This page intentionally left blank
From the Library of Lee Bogdanoff
Download at WoweBook.Com
Error Handling
In Chapter 1, “Security Is a Server Issue and Other Myths,” we discussed the need to integrate security measures into every application. In this chapter, we tackle one of the most basic ways you can secure your application: handling erroneous data.
THE GUESTBOOK APPLICATION This chapter also gets us into the sample application we’ll be working on throughout the book. It’s a simple guestbook application, but as you’ll see, there is plenty of room for security holes even in the smallest program. If you haven’t written your application, be sure to read through Chapter 16, “Plan A: Designing a Secure Application from the Beginning.”
PROGRAM SUMMARY The guestbook application will allow visitors to enter comments on the Web site. The comments will be stored in a database, and the ten most recent comments will be displayed on the Web site. Comments will also be e-mailed to a customer service address. The feature list includes the following: • Allow anonymous comments (Phase I). • Allow users to enter a name along with the comment, regardless of whether or not they are logged in to an account (Phase I).
13 From the Library of Lee Bogdanoff
Download at WoweBook.Com
CHAPTER 2
ERROR HANDLING
• Allow users to create accounts. Once they have created an account, they can view and modify their past comments (Phase II). • Allow users to upload a small image with their comment (Phase II). • Allow administrative users to view and delete user accounts and moderate comments (Phase III).
PRIMARY CODE LISTING The following is a first shot at the guestbook code. It implements the first requirement— to allow anonymous comments.