Slashdot Mirror


Essential PHP Security

Michael J. Ross writes "Given the remarkable popularity of PHP for developing dynamic Web sites, as well as the ever-increasing need for security on those same sites, one would think that there would be great demand for — and comparable supply of — books that explain how to create secure sites using PHP. However, such is not the case, and even the most extensive general purpose PHP books may only devote a single chapter to this critical topic, if that much. Essential PHP Security, written by PHP expert Chris Shiflett, aims to fill the gap." Read the rest of Michael's review. Essential PHP Security author Chris Shiflett pages 109 publisher O'Reilly Media rating 7 reviewer Michael J. Ross ISBN 059600656X summary A concise introduction to PHP security principles and practices.

O'Reilly has a Web page for the book, where they offer a sample chapter (Chapter 4: Sessions and Cookies), in PDF format, as well as the book's table of contents, index, errata, and links to the online version of the book, in O'Reilly's Safari service. As of the writing of this review, the confirmed errata is reassuringly sparse, and the unconfirmed errata is nonexistent, which speaks well of the author keeping on top of reader feedback — a worthy quality not shared by all technical writers. The author also has his own Web site dedicated to the book, where he has posted a table of contents, brief reader reviews, and two free chapters in PDF format: Chapters 2 (Forms and URLs) and 4.

In the book's forward, Andi Gutmans briefly explains how increasing Internet usage has resulted in a corresponding increase in security risks, for individuals and businesses operating online. He also notes that most of the security problems related to PHP-based applications, are not the result of weaknesses in the language itself, but rather in the way that developers have used the language in creating those applications. The intent of the book is to bring together the guidelines and lessons learned for writing secure PHP code, into a single volume. He concludes by noting that most of the principles presented in the book apply equally well to other Web development languages.

The bulk of the book's material is organized into seven chapters, focusing on the following topics: forms and URLs, databases and SQL, sessions and cookies, includes, files and commands, authentication and authorization, and shared hosting. These are preceded by an introduction, which oddly is labeled as a chapter. The true chapters are succeeded by three appendices, which cover the topics of configuration directives, functions, and cryptography. A short index rounds out the volume.

In the introduction, Shiflett presents the security-related PHP features, principles, and best practices that he uses as a foundation throughout the rest of the book, when focusing on the specific PHP topics covered by all of the subsequent chapters. The two features of PHP discussed are: register globals, of which most experienced PHP developers know the dangers, and PHP's error reporting capabilities. The four principles espoused by the author for writing secure PHP systems are: safeguard redundancy, minimum privileges, clarity through simplicity, and minimizing data exposure. The heart of the book appears to be his four recommended practices: tempering usability with security, tracking input and output data, filtering all input, and escaping or encoding output to preserve its meaning.

The seven topic chapters that follow the introduction provide fairly terse coverage of how those principles and practices are put to use, when designing and implementing forms, URLs, SQL commands, sessions, cookies, etc. Each subtopic within them is discussed briefly, and illustrated with code snippets.

If anyone is well-suited to writing such a work, it is Chris Shiflett, a well-known authority on PHP security, a respected contributor to the PHP community, founder and spokesman of the PHP Security Consortium, and founder and President of Brain Bulb, a PHP consulting firm.

In light of the author's expertise, one would presume that he would make every effort to write the definitive volume on PHP security — covering every conceivable topic, including: execution of system commands, verification of user IDs and authorization, e-mail spamming via Web forms, (the related topic of) exclusion of bots, and remote procedure calls. However, Essential PHP Security does not discuss those critical matters specifically. Moreover, the topics chosen are discussed in a rather cursory manner. The code samples throughout the book are generally quite minimal, with little to no explanation as to how they work. In addition, many of the techniques presented are but variations on the theme of "filter user input." These weaknesses may be why the book clocks in at only 109 pages. In fact, the seven core chapters comprise only 71 pages, leaving the reader to wonder how PHP security could possibly be adequately plumbed by such a short treatment.

On the other hand, there is something to be said for terse writing, as wizened fans of Kernighan and Richie's C language classic can attest. In agreement would be any developer who has purchased one of the many 700+ page technical tomes that turn out to be padded with excessive margins, poorly-tested code, and pointless appendices lifted from the respective products' documentation. Perhaps Shiflett intended his book to be more a primer on PHP security, rather than a comprehensive coverage — and hence the title of the book. As such, it would primarily be of value to PHP developers unfamiliar with basic security pitfalls and defenses. Regardless, any PHP developer would be wise to begin with this book as a first step towards PHP security mastery, but even wiser if they were to follow it up with more substantial works, as well as keeping current by reading security-focused Web sites and other current publications.

Michael J. Ross is a freelance writer, computer consultant, and the editor of PristinePlanet.com's free newsletter."

You can purchase Essential PHP Security from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

132 comments

  1. Rule #1 by Spy+der+Mann · · Score: 3, Insightful

    Don't use a shared host.

    'Nuff said.

    1. Re:Rule #1 by Anonymous Coward · · Score: 2, Informative

      Great, that's a good general security tip, but what does that have to do with how secure your scripts are???

      The book is about PHP script security. You could have the most secure server in the world but if your scripts allow unfiltered user input then you're still screwed. NO amount of server securitty will help that.

    2. Re:Rule #1 by Spy+der+Mann · · Score: 1

      You could have the most secure server in the world but if your scripts allow unfiltered user input then you're still screwed. NO amount of server securitty will help that.

      Of course, that's rule #2 :)

    3. Re:Rule #1 by petermgreen · · Score: 1

      or at least if you do make sure they run it in a way that isolates your scripts from other peoples.

      virtual private servers are an option and quite handy to have arround as a general box to throw stuff other than websites on that you wan't internet accessible but are a fair bit pricer than normal webhosting.

      --
      note: i'm known as plugwash most places but i screwd up registering that here somehow in the past and now can't register
    4. Re:Rule #1 by qw(name) · · Score: 3, Informative


      The author specifically deals with this issue in Chapter 8.

  2. As Ben Franklin once said... by Dr.+Photo · · Score: 4, Funny

    "Those who use essential PHP and expect security deserve neither."

    (*sniff, sniff*... mmm... do I smell karma roasting?)

    1. Re:As Ben Franklin once said... by dansf · · Score: 1

      Agreed. Why are advertisements like this allowed to be posted?

    2. Re:As Ben Franklin once said... by rsax · · Score: 0
      Agreed. Why are advertisements like this allowed to be posted?

      I believe they are called book reviews.

  3. a first for everything by Anonymous Coward · · Score: 3, Informative

    this is the first time i've already owned a book before a slashdot review of it came up. It gives me that warm and fuzzy feeling inside.

    I think the reviewer hit just about everything important about this book. The only thing I would add is I didnt feel the discussion about sessions was in depth enough. Nothing about how session data is actually stored on the server or how to secure it.

    other than that great book, because it is so short everything is easy to find.

  4. Christ Shiflett by Bogtha · · Score: 5, Informative

    What are the credentials of Chris Shiflett? He's widely touted as a "PHP security expert", but Stefan Esser has a beef with him, and claims that this book contains serious flaws and misunderstandings.

    I understand that people in the public eye like book authors are vulnerable to any crank that comes their way, but the problems that Stefan has highlighted do seem to point to a severe credibility problem, and Stefan, while prone to flaming, certainly knows what he is talking about.

    In the interests of fairness, you should also read Chris Shiflett's response.

    --
    Bogtha Bogtha Bogtha
    1. Re:Christ Shiflett by C_Kode · · Score: 4, Insightful

      They both are very good PHP security guys, but Stefan Esser is quite pathetic. Reading him can be like listening to someone speaking Spanglish. (flipping back and forth between languages) One minute he is professional, the next minute he is a 12 year old. Chris Shiflett should just ignore him and do what he needs to do to get it right. Obviously he made a mistake. Fix it and move on.

    2. Re:Christ Shiflett by dasil003 · · Score: 2, Insightful

      What are the credentials of Chris Shiflett? He's widely touted as a "PHP security expert", but Stefan Esser has a beef with him, and claims that this book contains serious flaws and misunderstandings.

      After reading the post I don't really give a shit what Stefan Esser has to say about the book. Yes there's a flaw, but you won't convince people with such an inflammatory blog post. It looks like he scanned everything to find some flaw so he can rip Chris a new one. I would question if, perhaps, he's envious that Chris got a book published?

      The flaw itself is not some glaring security hole. It's an Apache configuration issue that's pretty tangential to PHP security. It's the responsibility of the web host to avoid this problem. If people are configuring this stuff for themselves then they need to know a lot more than just PHP security. The information to the PHP programmer at worst doesn't work. So what? There are a million ways Apache could be configured that would break some example code. You don't get secure code by mixing and matching a bunch of examples anyway. For the book to be good it needs to impart the principles of PHP security to the reader so that their own code is secure. I don't know whether this book is successful in that, and certainly there is an issue here that could be addressed, but I think it's a stretch to claim that this one example discredits the book. Especially when it comes from someone with a vendetta.

    3. Re:Christ Shiflett by Bogtha · · Score: 2, Informative

      I think you misunderstood the point of the logging example. It doesn't work properly in the default configuration, the only circumstance in which it would work properly is if the server was set up in a really insecure way, and the reason why the mistake isn't immediately obvious is due to sheer dumb luck. Those three things are pretty damning when they come from a supposed PHP security expert, wouldn't you say?

      There are a million ways Apache could be configured that would break some example code.

      This is the default configuration we are talking about here, not some obscure hacked up config. And the Apache manual explicitly tells you not to configure your server in the only configuration that this sample code could work in.

      As for Stefan's attitude, I agree he does his credibility damage to flame so much, but to ignore his criticisms because of it is just silly. The mistakes in the book don't just disappear because Stefan wasn't polite when he pointed them out.

      --
      Bogtha Bogtha Bogtha
    4. Re:Christ Shiflett by Leperous · · Score: 1

      I hope I'm the first (and the last) person to point out he's a band member of the Foo Fighters.

    5. Re:Christ Shiflett by iamsure · · Score: 2, Interesting

      Please do note that Stefan, while occasionally not the most well-spoken advocate is both well-informed, and intelligent. He is an active contributor to the internals code of PHP itself, runs the hardened PHP project, reports vulnerabilities in a wide variety of opensource applications (responsibly!).. the list goes on.

      He's also the first man to crack the Xbox using software-only exploits.

      He's got a solid set of credentials. I happen to respect both Stefan and Chris, and I've found value in the work of both men. With that said, I find Chris to be more eloquent, and Stefan to be more vigilent and active in fixing problems. (Chris is an educator, Stefan is more of a developer).

      Humorously, Chris recently commented in a blog post that he wanted to code more again soon, and Stefan is working on completing his graduate work. Perhaps I'm not the only one thinking those things. :)

      Think of them as Linus Torvalds and Alan Cox - a powerful combination.

    6. Re:Christ Shiflett by Anonymous Coward · · Score: 0

      They both are very good PHP security guys

      That isn't really saying much. Since they're both advocating using PHP for stuff where it actually matters whether the code is secure or not, I don't particularly trust either of them. PHP is the wrong tool for the job. It's not like there's a shortage of less insane alternatives.

    7. Re:Christ Shiflett by Anonymous Coward · · Score: 0

      Chris Shiflett should just ignore him and do what he needs to do to get it right.

      Chris Shiflett just got busted fiddling with the Wikipedia entry for PHP, adding a plug for his book and removing information about the Hardened PHP Project, of which Stefan is the founder. It should also be noted that the Hardened PHP Project is a competitor to Chris Shifflet's business.

      The more I hear about these two, the more I dislike Chris Shiflett. He seems like a marketing guy who ignores real security problems. Stefan, on the other hand, seems like an immature, but ultimately well-meaning geek who does care about security. I know which one I think is pathetic, and it ain't Stefan.

  5. Two thumbs up by knightinshiningarmor · · Score: 3, Informative

    I have read this book in the past month, and it is very good at explaining PHP security from a very general perspective. It talks about encryption theory, SQL injection in genereal, filesystem permissions, etc. Very good read/reference for web developers who aren't as familiar with system/network security.

  6. Huh? O.o by Spy+der+Mann · · Score: 1

    WTF? Who modded this down?

    While you CAN control whether people can access your website or not, you CANNOT control what number of amateur insecure scripts reside on the same host.

    Where I work, we've had a number of problems due to using a shared host for our website. Mass site defacements is one of them.

    1. Re:Huh? O.o by TheSpoom · · Score: 1

      Most amateur and small budget PHP coders can't afford a dedicated server though. What's your advice to them? Is there a way they can protect themselves from others on the same server?

      --
      It's better to vote for what you want and not get it than to vote for what you don't want and get it.
      - E. Debs
    2. Re:Huh? O.o by Ma�djeurtam · · Score: 2, Informative

      Virtual Private Servers are probably the answer for small budget coders. You can found pretty good VPS services at $50 a month.

      --
      Instant Karma's gonna get you, Gonna knock you right on the head (John Lennon, 1970)
    3. Re:Huh? O.o by Anonymous Coward · · Score: 0

      And I would have recommended that different PHP applications just run as different UIDs.

    4. Re:Huh? O.o by Anonymous Coward · · Score: 0

      Yes, because I want to pay TEN TIMES as much per month for my site (or an extra $540 per year) just to nab myself a VPS. Thanks, but I'll stick with shared hosting and regular backups.

    5. Re:Huh? O.o by eneville · · Score: 1

      I don't think there's anything wrong if the admins just use php_suexec, (quite easy to use on debian based systems, as apache has the required support built in). This then stops the php module needed to run as the webserver, and it can run as the virtual site owner, without the privs of the apache process. Saves much trouble. If it's still open to the same problems as using plain old mod_php then please, let me know, but as yet I think it's safe. Only other alternative is to use php as a cgi, with su_exec.

  7. Looking for a more holistic approach? by psydeshow · · Score: 4, Informative

    If you're looking for a system-wide approach to PHP Security, one that covers everything from shell commands and service tuning up through application-level security policy implementation, you should check out Apress' Pro PHP Security.

    Cheers!

    1. Re:Looking for a more holistic approach? by Fozzyuw · · Score: 1

      I actually purchased this book as well. It was the first PHP security book I found. I've not read it from cover to cover but there has been a lot of good tips I've picked up from just browsing some of the chapters I'm interested in. Cheers, Foz

      --
      "The past was erased, the erasure was forgotten, the lie became truth." ~1984 George Orwell
  8. The problem is not PHP security by Anonymous Coward · · Score: 4, Informative

    The problem is Not PHP Security, but general Coding practice. If you code bad, you will do it in any language. There are lots of thumb up rules (like defensive programming, checking value input, never trusting anyone, etc...) - know these, and you will not need to read books like these.
    A good code on that (and a lot more) probably would be Code Complete, and Code Complete2

    1. Re:The problem is not PHP security by lukewarmfusion · · Score: 4, Insightful

      There are two kinds of audiences for books like these (my wild speculation to follow):

      1. Developers switching languages who need to know how to implement these security practices in a new language - when I moved from ASP to PHP and others (thank God!) I had to rebuild much of my code library in a new language. Obvious things (to me) like input validation were just a little more difficult without a resource. I've had formal programming education and plenty of real-world experience - now it's just a matter of porting the concepts from one technology to another.

      2. New developers that don't have any idea about secure programming practices - many web developers become programmers to meet their clients' needs. These developers often go from designing and building static websites to building database-driven apps. Whereas your brochure site usually doesn't need to validate input, your web app does - from SQL injection to cross-site scripting, these concepts are foreign to someone.

    2. Re:The problem is not PHP security by masklinn · · Score: 2, Insightful

      There is also a huge gap between languages (or widespread modules features or idioms of the language) helping the writing of secure code, such as Ruby's string having a taintedness flag or Python's DBAPI2, languages that don't go either way, and languages that are just stupid and hard to secure by design.

      And PHP is the latter. Globals, magic_quotes, 15 different functions to escape quotes in DB requests (14 of which don't work, or only work when the moon is green and you have red socks), retarded APIs, stupid naming schemes, lack of deprecation of insecure/retarded/out-of-date APIs (sometimes, deprecating whole APIs or modules is GOOD god damn it, if a function is retarded then you get rid of it, even if it breaks code), PHP is full of dumb features that make it hard to write good, secure code.

      --
      "The way we can tell it's C# instead of Haskell is because it's nine lines instead of two." -- wadler
    3. Re:The problem is not PHP security by Anonymous Coward · · Score: 0

      If you cannot write secure PHP code, then you must not be a very good coder.

    4. Re:The problem is not PHP security by masklinn · · Score: 1

      How about learning to read? I never said that I could not write secure PHP, I said that PHP was needlessly hard to secure, the language has not been designed with security in mind (truth be told, one would say that it hasn't been designed period) and it is therefore a damn pain to write secure large-scale PHP applications.

      --
      "The way we can tell it's C# instead of Haskell is because it's nine lines instead of two." -- wadler
    5. Re:The problem is not PHP security by Schraegstrichpunkt · · Score: 1

      Touché!

    6. Re:The problem is not PHP security by Anonymous Coward · · Score: 0

      I agree totaly. If you can't program in c or c++ you really should not be called a PHP prgrammer. A person (untrained, unlike you or I) knows nothing about what it takes to develop PHP code if they came from a "static web page" developer mentality.

      Raj

      ifyouspeaknoenglishyoucannotreallybeataxidriver.co m
      secretguidetobarrierstoentry.com
      freemasonsbuddyguide.com
      capitalismmeansimbetterthanyou.com
      howtotrustyourownthoughts.com

    7. Re:The problem is not PHP security by mcrbids · · Score: 1

      If you can't program in c or c++ you really should not be called a PHP prgrammer.

      Except that they are out there, and they call themselves PHP Programmers.

      It's easy to pass judgements as above when you don't have to deal with the mechanics of enforcement. Would you rather pass some law or decree for what constitutes a "programmer", or make it clear, immediate, and obvious that a "programmer" needs to keep certain things in mind as they develop?

      I lean heavily towards the latter - I've been a "PHP programmer" for some years, and pay very close attention to articles on security, as well as general *nix security. I look at some of my early works and shudder - but they were part of my clear and steady progress towards what I am today.

      I invest a significant amount of time on trying new things, as well as finding creative and workable solutions to problems such as XSS (wrapper calls to handle input) and SQL injection. (prepared statements, anyone?)

      PHP is a wonderful language, I use it to manage large datastructures Gigabytes in size. It's rock-solid stable, and lets me focus on the problem being solved rather than the minutiae of its solution. I love it!

      --
      I have no problem with your religion until you decide it's reason to deprive others of the truth.
  9. Better session system by DavidPesta · · Score: 5, Interesting

    For sessions, I find it more practical to drop the native PHP session system and create my own session system by connecting a user cookie to a database entry. Then you can have better access to the session data and more security, even encrypting the session data inside the database if you want. You can also modify the cookie "key" every so often to hinder someone who may have compromised the user's machine and is looking for session cookies.

    Also the advantages of doing this:
    1. You are given the option to separate the user sessions database from page navigation/scripts on different servers if you anticipate massive amounts of traffic someday and want a cluster of servers.
    2. It is not less efficient than the PHP session system. The native PHP sessions are file-based and also access the disk. With the user account_id as a primary key as a part of their cookie, session data access is very fast, perhaps faster in some cases.

    It wouldn't surprise me if that is why the author doesn't talk about PHP sessions much. Extremely high-traffic applications shouldn't use them IMO.

    1. Re:Better session system by DavidTC · · Score: 3, Informative

      You can use the PHP native session support and write your own backend for it, thus giving you the advantage of PHP handling the cookies and rewriting the URLs, but the ability to put it in a database.

      --
      If corporations are people, aren't stockholders guilty of slavery?
    2. Re:Better session system by Bogtha · · Score: 4, Insightful

      It's not clear from your comment, but you are aware that file-based sessions are just the default in PHP, aren't you? You can implement everything you say within the existing PHP framework by using session_set_save_handler(). You don't have to drop PHP's session handling entirely, just implement your own de/serialisation functions and give them to PHP.

      --
      Bogtha Bogtha Bogtha
    3. Re:Better session system by bobbyjack · · Score: 1

      The thing about PHP seesions that has always annoyed me is the seeming inability to handle the 'session timeout' event. Am I missing something obvious?

    4. Re:Better session system by Anonymous Coward · · Score: 0
    5. Re:Better session system by Schraegstrichpunkt · · Score: 1

      And don't forget locking...

  10. PHP Security Book Roundup by Anonymous Coward · · Score: 4, Informative
    In the interest of completeness, here's a complete round-up of all the books dedicated to PHP security currently on the market:

  11. Re:What's the big deal? by Anonymous Coward · · Score: 0

    yeah big deal AFAIK slashdot s in perl....

  12. Support the Author by shiflett · · Score: 4, Informative

    If you want to save some money and also support the author (me), please use this link:

    http://phpsecurity.org/buy

    You get the book for less than $20. :-)

    1. Re:Support the Author by 70Bang · · Score: 1



      How much less support will you get if we hit AddAll or BookPool [1] and get the book+shipping for the price of Amazon?

      _________________________________
      [1] Although it's out of stock at BookPool.

    2. Re:Support the Author by B3ryllium · · Score: 1

      I got the book for Christmas from Amazon, it's been an interesting read. I've certainly learned a few things and had a few ideas on safer future implementations because of the book. Thanks for writing it :)

  13. Re:What's the big deal? by Jordan+Catalano · · Score: 1

    I have a feeling people read the first few words of my post, saw that it looked whiny, and completely missed the joke (therefore labeling it "offtopic", which baffles me). Sad. It wasn't that bad a joke.

  14. cursory mannerisms by digitaldc · · Score: 1

    Moreover, the topics chosen are discussed in a rather cursory manner. The code samples throughout the book are generally quite minimal, with little to no explanation as to how they work.

    Here! This is what I mean! YOU figure out the rest!!

    [html]
    [head]
    [title]
    PHP Code sample etc. etc. etc.

    --
    He who knows best knows how little he knows. - Thomas Jefferson
  15. There's a wonderful utility that secures PHP by Anonymous Coward · · Score: 0, Troll

    http://tomcat.apache.org/

    Seriously, I've worked on a whole bunch of PHP sites, and every single one of them had numerous vulnerabilities, primarily SQL injection vulnerabilities. How do you avoid SQL injection attacks? You must use a DB layer that prevents them. PEAR provides protection IF it is used consistently and correctly (sorta like something else that provides protection, maybe the Slashdot crowd doesn't know about it though).

    But really, PHP is hostile to the idea of secure code. Every variable can be a string, variables don't need to be declared, a function has no idea what type of input its getting, and most sites don't use proper database layers.

    In the Tomcat world, every variable has a type, a variable may not be used before it is declared AND initialized, both these are enforced at COMPILE TIME (something which doesn't exist in PHP), and most sites use reasonable database layers, either PreparedStatements or even better, Hibernate.

    I think PHP would be better if sites ran in strict mode (or whatever they call it) but I think few of them do, and to get a legacy codebase to run in strict mode is a major task.

    All software should go through a syntax check before it is deployed, a syntax check which checks over every line of code, without waiting for the code to be exercised. That syntax check is often performed by a COMPLIER. Code should be compiled.

    1. Re:There's a wonderful utility that secures PHP by LordLucless · · Score: 1

      So you're not saying that PHP is insecure, you're saying that all scripting languages (loosely-typed, interpreted not compiled) are insecure.

      --
      Just because you're paranoid doesn't mean there isn't an invisible demon about to eat your face
    2. Re:There's a wonderful utility that secures PHP by jshpro2 · · Score: 2, Interesting

      Every variable in PHP has a type as well, and using an undeclared variable gives an E_NOTICE level error, PHP recommends running in E_ALL which will cause scripts to output errors when you use uninitialized variables. FYI PHP does check the syntax of your script, if you try to run a script with a parse error it will not run at all. Whether or not a developer chooses to run their applications with E_ALL error reporting is their problem, there's also pros to running an interpreted script over compiled code.

  16. Response to the Review by shiflett · · Score: 4, Interesting

    I wanted to reply to one thing, because it's a very valid point:

    In light of the author's expertise, one would presume that he would make every effort to write the definitive volume on PHP security -- covering every conceivable topic, including: execution of system commands, verification of user IDs and authorization, e-mail spamming via Web forms, (the related topic of) exclusion of bots, and remote procedure calls.

    I deliberately chose to focus this book on the 80%, and I'm actually happy that I did. PHP's reputation suffers because of security concerns, and I'm sure you'll see some of that expressed here. I want PHP developers who read this book to focus on what's most important, and the principles and practices that they learn along the way should prepare them to deal with more minor concerns.

    The execution of system commands is covered, but you're right that email injection is missing. HTTP response splitting is another. The second edition might include these, but they really boil down to the same thing as so many other vulnerabilities. If you filter input and escape output, neither are a concern. (After a recent change to header(), HTTP response splitting is no longer a concern, but we'll have to work with older versions of PHP for quite some time.)

    Thanks for reading, and I hope it helps!

    1. Re:Response to the Review by Anonymous Coward · · Score: 0

      That's a good response, and shows a valid point - the book is called Essential PHP Security, not Complete PHP Security. That leaves plenty of room for a second book (Advanced PHP Security?) covering some of the more complex, in-depth, or obscure topics. One shouldn't expect all the answers from a single source.

  17. Essential PHP Security by Iron+E · · Score: 1, Funny

    #!/usr/bin/perl -w

    1. Re:Essential PHP Security by ocelotbob · · Score: 1

      Of course, this is if you're ignorant of things like running php as a cgi, using mod_suphp, etc, to get the php environment as close to perl as possible. And this is further ignorant of things like open_basedir and safe_mode, which allow you to jail your php code to a pretty decent degree and stop a lot of the kiddy problems that people think about when they think of php.

      --

      Marxism is the opiate of dumbasses

    2. Re:Essential PHP Security by Vindaloo · · Score: 1
      #!/usr/bin/perl -w
      s/-w/-t/
    3. Re:Essential PHP Security by Anonymous Coward · · Score: 0

      Sheesh! Get it right guys!

      s/-w/-wT/

      "man perlrun" and "man perlsec" for more details.

  18. Re:Chris Shiflett by shiflett · · Score: 3, Informative

    You're welcome to read the reviews and the (very thorough) errata yourself:

    http://phpsecurity.org/reviews

    http://phpsecurity.org/errata

    You'll be hard-pressed to find anything beyond simple typos and unclear sentences, I think, and the reviews have been very positive. :-)

  19. The real problem with PHP and security by ben_1432 · · Score: 2, Interesting

    The real problem with PHP and security is that it's perceived as insecure. There are countless stories of people losing their forums, blogs, websites etc to hackers, defacers and script kiddies.

    This book might address how to code in PHP more securely, but that is not going to address the much more perceivable problem of "THIS SITE HAS BEEN H4X0RZED".

    What's needed is for some *real* professionals to sit down and go through all the popular open source packages - phpbb, nuke etc - and identify and remove as many problems as possible.

    That would obviously be a huge effort, but it's a necessary step imho.

    At the least, a solid, secure framework should be released that the softwares could be based on so there is a rigorous, thorough filtering of all input/output, and the onus is partially removed from the people who mean well but write shitty software.

    1. Re:The real problem with PHP and security by MrTufty · · Score: 2, Informative

      The last I heard on this subject from the phpBB guys is that they're doing precisely this sort of code audit to make sure the new version of the software isn't plagued by so many vulnerabilities as the 2.0 line has been... but only time will tell whether it's manageable.

      Trouble with this sort of job is that the real professionals also happen to charge a fortune for their services, and that's something that most open-source projects can't afford.

    2. Re:The real problem with PHP and security by ben_1432 · · Score: 1

      The last I heard on this subject from the phpBB guys is that they're doing precisely this sort of code audit to make sure the new version of the software isn't plagued by so many vulnerabilities as the 2.0 line has been... but only time will tell whether it's manageable.

      That's a great start, but it needs to be done on all of the massively popular packages.

      Trouble with this sort of job is that the real professionals also happen to charge a fortune for their services, and that's something that most open-source projects can't afford.
      True, but then on the other side of the spectrum you've got some amazing software & os's coming out that're robust and secure, and written by real professionals who aren't charging anything.

      It's kind of weird that the hugely popular open-source php apps don't attract the quality attention the niche desktop apps do.

    3. Re:The real problem with PHP and security by Tony+Hoyle · · Score: 1

      The problem with phpBB is their plugin system... it's got loads of plugins (good) but you have to hand-edit the source files to install them (bad) which means you can't keep track of the security updates without scrapping your site and reloading all the plugins manually again (really bad).

      Looking around for a replacement I found SMF... which inisists as part of its install you make all your files and directories chmod 777.. an curiously the author has no problem with this (and even tries to say this is not insecure). So that's out.

      Still looking for a good, extensible BB system.

    4. Re:The real problem with PHP and security by moof1138 · · Score: 2, Informative

      The perception that PHP is insecure is based on the fact that it was in the past incredibly insecure, not just because of bad programmers, but by design as a language/deployment environment. Perhaps it was too long ago for you top recall, but it used to be that register_globals was on by default. register_globals was inherently insecure, and including it in the language at all was a huge security compromise, and pathetically enough since there are still PHP packages out there that depend on it, it has not been removed from PHP.

      Also letting include() take a URL is batshit crazy. While handy (like most security compromises), it means that if anyone can figure out a way to sneak a string in to that include call, then they can import any malicious code they want. register_globals was often a handy way to do that in the past...

      There are a few other things that I think are a sign of poor security design, but most would likely just cause language wars, so I will leave it to those above to illustrate that PHP, especially in its earlier incarnations had a poor design with respect to security.

      PHP itself has had its fair share of security vulns, for instance:

      http://www.hardened-php.net/advisory_202005.79.htm l
      http://www.hardened-php.net/advisory_152005.67.htm l
      http://www.hardened-php.net/advisory_142005.66.htm l
      http://www.sans.org/newsletters/risk/display.php?v =3&i=50#widely4
      http://www.sans.org/newsletters/risk/display.php?v =3&i=23#other1
      http://www.sans.org/newsletters/risk/display.php?v =3&i=28#widely4
      http://www.sans.org/newsletters/risk/display.php?v =3&i=48#exploit1

      There are more, but I am too lazy to find a more complete list, and that is enough to illustrate the point.

      So while it's true that there are a lot of bad security habits that many PHP programmers have used, PHP's bad reputation is something they have earned all by themselves.

      --

      Hyperbole is the worst thing ever.
  20. Re:Wrong answer by psbrogna · · Score: 2, Insightful

    Out of curiosity, what's wrong with gluing together components? The painting by numbers analogy to programming can work as long as the template and palette is acceptable. Granted, you still have to understand how the systems behaves in its entirety, but you have to in either case.

  21. Re:What's the big deal? by larry+bagina · · Score: 2, Informative
    --
    Do you even lift?

    These aren't the 'roids you're looking for.

  22. The problem is not PHP by loconet · · Score: 2, Funny

    As an experienced PHP developer who has been involved in many small and large projects, usually involving several other programmers, I can say that the problem with PHP's security is not PHP itself but rather the inability of exploits to punch the programmer in the face.

    --
    [alk]
  23. Re:PHP/CSS and Safari (MAC)... by Anonymous Coward · · Score: 0

    PHP runs on the server, it behaves exactly the same no matter what broweser you use (assuming you don't have any browser checking code). You probably mean a HTML/CSS bug.

  24. Re:Wrong answer by Nos. · · Score: 2, Insightful

    Lets see, I work in the security area for my employer and have done web development (not design) work as well. I say that PHP can be used to create rich dynamic sites that are secure. So, now, would you care to back up your accusation of PHP as a programming language being insecure with some real facts?

    Sure, there have been holes in countless PHP applications. However, this is not the fault of the language. In fact, almost all of these problems come down to programmers not properly validating user input before processing it. This is true of pretty much all languages. PHP gets a bad name because its easy to learn and people pick it up and write insecure apps with it. Thats like blaming the hammer when the house falls down.

  25. PHP specific? by techefnet · · Score: 0

    So, I guess this is more generic web development security right? I'd appriciate a book that covered generic web development security, I don't think it has to be language specific. It's all the same principles in most languages. :)

  26. Re:Wrong answer by dk.r*nger · · Score: 5, Insightful

    Would you mind accommodating your +1 Insightful and tell the world why?
    The same reason that MySQL is crap, because you really, really need stored procedures, views and transactions to keep track of 20.000 messages in 1.500 threads?
    The same reason that Java sucks for everything, always and C never does?
    The same reason that compiled languages are always better than interpreted ones?

    Of course, that reason I'm referring to is arrogance.

    Don't get me wrong. I'm actually halfway to a MSc in Computer Science, and frequently have my ego challenged by kids and their flash 'applications', drag'n'drop VB crap and funny web apps that trust me to let it pass critical information in the URL.. These kids tend to think that I'm learning useless crap because they already know. Naturally I'm all warm inside when I get to give their 'application' 500% speedup by adding an index to a table.

    But what makes PHP itself unsecure? Yes, PHP wants to be proporly configured. And if you let 50 kids run amok on the same server, sure they'll fuck something up (though never outside of the PHP user).

    Now imagine a production webserver, to where only qualified developers has access, and only tested PHP code is put on. Works for me, has for a long time.

    Oh, and concluding that all PHP is paint by numbers because it's a scripting language is just ignorant.
    #include stdio.h anyone? Not enough of a real man to write your own IO routines, so you're stuck with gluing together libc stuff "in a paint-by-numbers style"? bah..

  27. Re:Wrong answer by 228e2 · · Score: 1

    This isnt Jeopardy. give us an "answer" to the question, or i cant see why this shouldnt be modded as flamebait ;/

    --
    Since when does being a Socialist mean 'someone who has a different opinion than me'?
  28. No book can teach you because the bad don't read by SmallFurryCreature · · Score: 2, Insightful
    Two students developed a new website for a volunteer organisation as part of their education. Something like an internship, anyway for them it was a temp job. They didn't finish it and since I volunteerd in the past for doing work for them I am now putting the finisching touches on it.

    Yuck.

    To look at the code I am once again reminded why I always feel sick when someone tells me that I am going to have to work together with a person with a degree. As far as I know they passed with this project yet it is totally and utterly crap. Oh of course it does not work cross-browser. Yes it has many bits of codes wich don't actually doesn't do shit. A 100 line switch statement that always does the same thing for instance but that ain't the worst of it.

    The worst of it is that security is non-existent. They use the old '?page=page1' in the url to switch content. I like this approach in itself as it leaves you with only 1 code file wich is accessible from the outside. I also like to make fucking sure that 'page' is filled only with values that I expect. They just insert it in an sql statement and execute it.

    Shudder.

    Could a book teach guys like this about security? NO.

    It is not the first time I see shit like this. To many IT students just are to young and naive to think about security. Or rather to not think about security but just do it. Nobody had to teach me that trusting userinput is bad. I know it is. How?

    Well, I don't know I just do. Perhaps it is all the years of low level cracking of games where you alter a string somewhere to give you more health/money whatever. Perhaps it is just being a suspicious bastard.

    Security is not a set of easy to follow rules, security is not trusting people.

    PHP is a usefull enough language that unfortunally in its basic install comes with some features that can really bite you in the ass. I always disable them on any server I control and then have to spend a lot of time correcting everyone elses code to work on a secured server. Oh and if I see one more person use PHP native sessions I am going to kill that motherfucker. Especially when it is used to store 1 value. Just use a fucking cookie instead of glogging the HD in resource eating insecure way.

    ANyway, the review of the book? Well it covers the very basics. If you still need to be told this, just stay away from the web. This is akin to a cooking book telling you not to have a boiling pot of water on the first burners of your stove when a little kid is around. What I read of it all falls into the 'duh' category.

    --

    MMO Quests are like orgasms:

    You may solo them, I prefer them in a group.

  29. Re:PHP/CSS and Safari (MAC)... by __aaclcg7560 · · Score: 1

    It's a definitely a web browser problem. Unfortunately, because I'm using PHP and the way my template is laid out, CSS is not working on Safari. Unlike a security problem, this probably won't be fixed soon unless I re-work my template.

  30. Re:PHP/CSS and Safari (MAC)... by paranoidgeek · · Score: 1

    http://jigsaw.w3.org/css-validator/validator?uri=h ttp%3A%2F%2Fwww.creimer.ws%2F

    ( Enter your web site URL into http://jigsaw.w3.org/css-validator/ )

    It looks like you have some parse errors in your CSS.

    Maybe those are causing Safari to mis-render it ?

    --
    Lima India November Uniform X-ray
  31. Save some money by buying the book here! by pmc255cool · · Score: 1

    Save yourself some money by buying the book here: Essential PHP Security. And if you use the "secret" A9.com Instant Reward discount, you can save an extra 1.57%!

  32. HackThisSite.org? by SheeEttin · · Score: 1

    So, I guess HTS will have to update some of their hacking challenges.

  33. Best PHP Security Tip: by spacemky · · Score: 1
    --
    640YB ought to be enough for anybody.
  34. Re:Wrong answer by vgaphil · · Score: 1

    Why? What should we use?

    --
    A clever person solves a problem. A wise person avoids it. -- Einstein
  35. Re:No book can teach you because the bad don't rea by Tony+Hoyle · · Score: 1

    That's no surprise... I personally wouldn't touch code written by someone just out of school without (a) extra money, and (b) the option to scrap and rewrite it.

    Like any other job, it takes a while to actually get good. It's not unique to the computer industry.

  36. Re:No book can teach you because the bad don't rea by Anonymous Coward · · Score: 1, Interesting

    I disagree with the last part of your post the whole everything in this book is under the 'duh' category. myself for example my current job is the first time i've worked with php and also with databases in any substantial way. so describing sql injection and xss attacks doesnt tell me not to trust user input it tells me what type of input to look for as malicious. and yes yes i know you should not try to exclude what a user cant do you should find the minimun subset of input that the user requires to do whatever they are trying to do in that field. more restrictive is always better. But as you should know thats not always an option.

  37. Shared server admin by Anonymous Coward · · Score: 1, Interesting

    What about administrator perspective? I administer a LAMP site with lots of daily hammering in the style of "index.php?wget; chmod 755; ./script;blah...". Some of it actually got through, and it launched a DOS attack on some other site. Although server is grsecurityised, apache runs under nobody, there are still problems. How can I contain each virtual host in its own environment, cpu time limiting, stuff like that? Any keywords for google?

    1. Re:Shared server admin by porl · · Score: 1

      not sure about the cpu time limiting etc, but couldn't you run each virtual server in it's own chroot jail?

    2. Re:Shared server admin by Anonymous Coward · · Score: 0

      Hmm, there are something like 30 domains. Setting up separate chroots would be an overkill (some of them have several hits per day and 3-4 MB used disk space). I was thinking of User mode Linux, but I need to learn how to install it yet :)

  38. Re:No book can teach you because the bad don't rea by onedotzero · · Score: 2, Interesting

    The worst of it is that security is non-existent. They use the old '?page=page1' in the url to switch content.

    Oh and if I see one more person use PHP native sessions I am going to kill that motherfucker.

    Do you have any examples of the alternatives? On the whole these methods seem very straightforward (and I use the first method myself) but I'd very much like to learn alternate, more secure ways of doing this kind of thing, especially as they are the most common ways to access and deliver content.

    --
    onedotzero
    thedigitalfeed.co.uk

  39. SPAMMER don't give him your money by Anonymous Coward · · Score: 0

    Mod down please

  40. Re:No book can teach you because the bad don't rea by LordLucless · · Score: 1

    Judging from his comment (which was a bit hard to read at times), he doesn't mind the ?page=page1 technique. What he objects to is then doing something like "SELECT * FROM Pages WHERE $_REQUEST['QUERY_STRING']". Which is perfectly understandable.

    As for native sessions, I don't know why he's so vehement against them, but implementing session stuff yourself isn't to hard. When a user logs in, generate a unique ID, give it to them in a cookie and store it in a database table. At the top of every page, have a bit of code that checks for the cookie and, if it exists, look it up in the database. Add a time field, a user field, and a data field with a serialized PHP array and you've pretty much got all the stuff you need to do whatever the native system does.

    I'm not sure what advantage the homebrew type system has it's database based (may be more efficient I/O wise, but that's only going to be an issue on sites expecting a very large audience) and you can encrypt the session data stored on disc (a bit of overkill IMO, especially if you're on a dedicated server - I never store anything important in session vars anyway)

    --
    Just because you're paranoid doesn't mean there isn't an invisible demon about to eat your face
  41. Mod parent WRONG by scragz · · Score: 2, Funny
    There's a wonderful utility that secures PHP. . .http://tomcat.apache.org/

    FYI to other readers of parent comment, that does nothing to help secure PHP or your PHP code. It won't even run my sample application below:
    <?php
    if (isset($_POST['name'])) {
        exec("echo 'your name is: '. $_POST['name']");
    }
    ?>
    <form action="./" method="post"><input name="name" /></form>
    1. Re:Mod parent WRONG by colinrichardday · · Score: 1

      What is setting an HTML form action to a directory supposed to do? Also, doesn't the input tag require a type attribute? Does your browser handle "post" as opposed to "POST" properly? Have you tried escaping the inner quotes on the third line?

      Also, I tried that on my machine without Tomcat and it didn't do anything, either.

    2. Re:Mod parent WRONG by Anonymous Coward · · Score: 0

      God, you're stupid.

    3. Re:Mod parent WRONG by kjamez · · Score: 1

      ...

      exec("echo \' your name is: ".$_POST['name']." \'");

      --
      you can't have everything, where would you put it?
    4. Re:Mod parent WRONG by scragz · · Score: 1

      Hint: the funny bit is where it directly execs unescaped user input.

      But to answer your questions anyway,

      What is setting an HTML form action to a directory supposed to do?
      I dunno, I typed this in 5 seconds for a /. comment.

      Also, doesn't the input tag require a type attribute?
      Input defaults to type="text".

      Does your browser handle "post" as opposed to "POST" properly?
      The form you typed in to post that comment has "post" instead of "POST".

      Have you tried escaping the inner quotes on the third line?
      Escaping the inner quotes would end up executing echo \'Your name is...., which is not valid.

    5. Re:Mod parent WRONG by colinrichardday · · Score: 1

      I misread my output, not noticing the error when I escaped the inner quotes. I did, however, get different output when I escape the brackets. Escaping the brackets led to the actual display of an input box, which when filled and submitted, led to a 404 error.

      Also, have you tried it without Tomcat?

      As for the post vs. POST distinction, I thought I had read that some browsers couldn't read post correctly, but I can't find the source now.

  42. Re:PHP/CSS and Safari (MAC)... by Anonymous Coward · · Score: 0
  43. Re:Chris Shiflett by PktLoss · · Score: 1

    You're mixing up replies.

    The reply by Chris Shiflett you point to is in response to a much earlier comment by Stefan Esser. It is not a reply to the comments Stefan made with regards to his book.

  44. Re:PHP/CSS and Safari (MAC)... by __aaclcg7560 · · Score: 1

    So Safari is choking on the CSS file even though the elements causing a parse error are not being used? Is Safari that sensitive to the CSS file?

  45. Someone else already answered for me by SmallFurryCreature · · Score: 1
    He is pretty accurate. the ?page=page1 is itself not the problem, I like it and use it myself but as the other guy points out the way they use it is wrong. Why? "select * from pages where page = '$page'" is a HUGE security risk.

    What happens when someone alters the url by hand, to say something like ?page=fuckofyoufuckingfucker.

    Well nothing, except it won't load a page (probably I don't know what you called your pages) but what if they actually make it more complex and insert a complete sql statement with escape codes? You see the problem is that you can do that, you can insert one sql statement into another. I am to lazy to search for an example but it is a widely used attack.

    What should you do instead? Well at least "select * from pages where page = 'Mysql_Escape_String($page)'" (not proper PHP) wich escapes any escape codes making it a simple string with no harmfull side effects.

    Personally I like to go further. I use a regex to make sure it only contains those characters I expect and in the case of pages where I know the range of options I check wether it is a legal page name. For instance not to long or to short. Just to be sure. Remember it is okay to be paranoid on the web where everyone really is out to get you.

    As for PHP native sessions, the other post again answers it very accurate but not why I dislike them. A it is not enabled on all servers. This is bad because PHP code should be portable. B it is blocking, only 1 thread can work on a sessions data at a time. This is bad on high performance servers. C. it uses the filesystem for something wich it is not meant to do. D. it is a bitch to admin. E. the alternatives are just so much better.

    What is the alternative. A simple database (whatever form you like) with a cookie.

    So the first method is perfectly fine just CHECK the content of $_GET['page'] before you use it. The second method just doesn't scale well. It is overkill for simple stuff where you only need a user side stored cookie and it is to simple and inefficient for more complex needs.

    --

    MMO Quests are like orgasms:

    You may solo them, I prefer them in a group.

    1. Re:Someone else already answered for me by Anonymous Coward · · Score: 0

      You should actually use mysql_real_escape_string instead of just mysql_escape_string or addslashes:

      http://shiflett.org/archive/184

    2. Re:Someone else already answered for me by Anonymous Coward · · Score: 0

      Actually, you should use a real database layer rather than the mysql specific crap that ships with PHP. Or better yet, don't use PHP at all because your app will inevitably be filled with garbage code suggested by PHP 'experts'.

    3. Re:Someone else already answered for me by mgkimsal2 · · Score: 1

      As for PHP native sessions, the other post again answers it very accurate but not why I dislike them. A it is not enabled on all servers. This is bad because PHP code should be portable. B it is blocking, only 1 thread can work on a sessions data at a time. This is bad on high performance servers. C. it uses the filesystem for something wich it is not meant to do. D. it is a bitch to admin. E. the alternatives are just so much better.

      A) It's enabled on nearly all servers running PHP, except for some which may explicitly lock down some things (though I've never come across something like this). session 'auto-start' may be disabled by default, but few people go through the bother of removing 'session_start()' from the available function list. The reason most people leave it on is to enjoy the portability of the hundreds/thousands of pre-done PHP apps which expect it to be on.

      B) Blocking? Default PHP session write to the file system, and there can be more than one thing writing to the file system at the same time. Not to the same *file*, but rare - never? - is the case where two different processes should be updating the session data at the same time. I'm speaking of standard out-of-the-box LAMP setups. Unless you go to the trouble of separating each session var into a distinct field in a database, storing them in a database won't help the 'blocking' issue when you're talking about multiple distinct processes needing to update the same single blob of session data.

      C) File systems were meant to store text files, which is exactly what the default PHP session handler does.

      D) What is a bitch to admin about PHP session files? What is there to admin about them in general? Do you mean permissions during backup procedures or something?

      E) All alternatives have their tradeoffs to make.

      Personally, I don't normally use PHP's native session handling for a couple reasons.

      1) The default in a shared host environment is to store everything in /tmp or some other equally world-readable directory. Yes, there are CGI or proxied setups or safe_mode which help deal with the promiscuous reading issues, but they entail extra work.

      2) The default session handling process will always write out the entire session blob of data (to the file system, for example) even if nothing in the $_SESSION array has been changed, added or deleted. It's a bit wasteful in large-scale environments, although it was probably easier to develop that way.

    4. Re:Someone else already answered for me by cortana · · Score: 1

      You should do something like:

        $db->query ('SELECT * FROM pages WHERE page = ?', array ($_GET['page']));

      PEAR::DB makes it possible to query databases from PHP without going insane.

  46. Rule #3 by Schraegstrichpunkt · · Score: 1

    Rule #3: User Contributed Notes are Wrong and Probably Dangerous.

  47. Proof is in the pudding by lgordon · · Score: 0, Flamebait

    I bet if you posted the url to your server to slashdot as a response, you'd find out how secure it was...

  48. the book, and real world systems security by ubiquitin · · Score: 1
    Hi Chris:
    I'd like to invite you and encourage you to master the ten domains of the Common Body of Knowledge (www.isc2.org) and to take the CISSP exam. Obviously you have a lot to contribute and are eager to do so. One of the major benefits of the CISSP exam is that it shows that you're well-rounded and can converse, at least on a basic level, about all parts of security. The challenge faced by all security professionals is to be comprehensive, which involves, among other things, realizing the limitations of specific layers and approaches. A cursory look through your book seems to indicate a lack of fundamental grasp of access control models, something that the PHP community as a whole can use some guidance on. Every one of these domains relates to "PHP Security" on some level. Here's the CBK list:

    Access Control Systems and Methodology

    Applications and Systems Development Security

    Business Continuity Planning (BCP) and Disaster Recovery Planning (DRP)

    Cryptography

    Law, Investigation and Ethics

    Operations Security

    Physical Security

    Security Architecture and Models

    Security Management Practices

    Telecommunications and Network Security
    I'm confident that the time you spend working toward the CISSP credential will be genuinely useful to you in your consulting practice, as it has been to me, especially insofar as it involves information systems security.
    Regards,
    MKC, CISSP
    PHP Consulting

    --
    http://tinyurl.com/4ny52
    1. Re:the book, and real world systems security by Dirkpitt007 · · Score: 1

      To much study not enough practice... get a grip man, this guy has written a book on general PHP security holes.. if you cloud it with all the other garbage it defeats the purpose of his book, which is to provide a fast and simple reference to common php security issues.

    2. Re:the book, and real world systems security by Anonymous Coward · · Score: 0

      When they were teaching you how to converse, at least on a basic level, about all parts of security, they should have mentioned that access control and authorization are the same thing. Oops.

      As a reader, I'm glad this book is focused on the details of one topic, web application security. It sounds like you would be more satisfied with a bloated, basic overview of all aspects of security. O'Reilly books are not for you.

      Hope your clients are fooled by your certification. Slashdot isn't.

  49. Perhaps if people learned the OS they use .... by tinkertim · · Score: 4, Informative

    I can't but get a little sick when I see a whole book written on something so incredibly simple.

    The reason you see PHP being exploited is not the security of the host OS, not the security of PHP (well almost never) , its the lack of knowledge by the person owning the computer hosting the sites and companies like The Planet who hand them out to literally anyone with a Paypal account or credit card number.

    I can in 20 minutes show any experienced Linux system administrator how to run PHP completely wide open as far as functionality is concerned on a shared hosting environment and how to do it relatively safely.

    Your average web hosting company is a business person who has money to buy servers with idiot proof (nearly) control panels such as C-Panel / WHM.

    They're also likely to come with RHEL, Centos 3 or 4 or Fedora. Very rarely do I see a Debian server used in a shared hosting situation (That should also tell you something).

    These boxes are not secure yet they go immediately into production.

    SO! To anyone who cares, (and reads this far) here is Tinkertim's checklist :

    1 - Egress filtering (firewall the damn box),

    2 - Get rid of that fat, bloated leaky modular kernel. Monolithic kernels are too easy to build not to do it. Don't forget to keep iptables, test with your firewall when done.

    3 - Seek and loop world writeable directories, or mount them as noexec. Even doing that is not going to save you all of your trouble. As nobody I can run /bin/sh -x /tmp/mybot.sh just fine on most linux distros even if /tmp is noexec. So dammit go toss the 3 lines of code in /bin/sh that keps uid/gid 99 from doing that.

    4 - Don't even THINK about using apache/proxy on a shared hosting setup. Thats just incredibly stupid and self destructive.

    5 - Look around in /dev ... make sure you took ALL the tools away that helps people get bad code onto your box in the first place. /dev/tcp is just as lethal as leaving wget available on a fedora / RHEL installation. Use mknod and make them safe. Same with /dev/udp .. remake them.

    6 - Get rid of what you don't need. Rename what you do and use scripts to help govern them. Lynx / wget / POST / GET (and everything else RHEL/Centos comes with) can be used to do dastardly things. Take advantage of user / group ownership that is found in Unix.

    7 - lsof is your friend. Write a script to check for open accepting inet sockets that don't belong.

    8 - (finally) VERIFY YOUR ORDERS ... stop making instant setup hosting accounts. Use fraud screening services. Remember a security hole is only a problem if you sell space to someone who's intention is to exploit it.

    Web hosts are the scurge of the planet. I know , I am one :) But I do things a bit differently than most. There's things you (yourself) can do if your stuck on shared hosting to ensure and nudge your host into securing their boxes.

    I may just re-post later or re submit with that list too. I'm off the soap box now. My point is this. We (shared web hosts) made this problem. We have a responsibility to admit it and stop it. I'll work on some checklists and scripts to do it for the lazy bastards and GPL them. Tired of people getting rich writing books making hype about what (should be) a very trivial issue.

    1. Re:Perhaps if people learned the OS they use .... by Anonymous Coward · · Score: 0

      Fine. Do all that.

      Your server might not get compromised, but the websites on the servers are still open for sql injection attacks and cross site scripting attacks. Either of which may leave the people whose accounts were hosted very unhappy.

      The moral is that people lump a lot of very different things in together under the phrase "security". And the security problems that you are able to address is a very small part of that list.

    2. Re:Perhaps if people learned the OS they use .... by porl · · Score: 1

      Very rarely do I see a Debian server used in a shared hosting situation (That should also tell you something).

      it tells me that the majority of the people running these things want paid support for their OS (fair enough), nothing more. I don't know what that has to do with 'Essential PHP Security'

    3. Re:Perhaps if people learned the OS they use .... by tinkertim · · Score: 1

      Because by default installation (which is what many run with, out of the box), Debian does not suffer from many of the issues that the more popular (and sometimes commercial) Linux distributions suffer from. Most people install, update and figure that their system is secure. This is not the case on ANY distribution of Linux. The security issues you see surrounding PHP are not the fault of PHP. 90% of the time a properly secured server would have prevented the issue. The issue is this : people demand hosting that allows virtually unrestricted use of PHP to power whatever script. Web hosts give them what they want - sadly, most of the time, without properly securing the OS to provide it. PHP Security starts at the OS level. If all we had to worry about was SQL injection vulnerabilities and holes in session handlers the book would probably not have been written. I'm not demeaning the book. I'm demeaning people that give unrestricted access to their boxes for under $2.50 and wonder why it isn't secure :) That "crap" gets mistakingly hung on PHP. And nobody who presents themselves as an authority seems to address the issue. I'm saying, Its an issue. And Debian fixes much of it by default.

    4. Re:Perhaps if people learned the OS they use .... by porl · · Score: 1

      oops, sorry i read it as 'debian isn't often used for real hosting business' not 'debian isn't often used for those without a clue' :) my bad and apologies to the parent i thought they were putting debian down. maybe i need sleep. :)

    5. Re:Perhaps if people learned the OS they use .... by AnotherDaveB · · Score: 1
      it tells me that the majority of the people running these things want paid support for their OS (fair enough), nothing more.

      Then the voices in your head are wrong.

      The most common webhost linux flavour is Fedora Core, which is not to be confused with the commercially supported Red Hat Enterprise Linux.

  50. Must-have for all the PHP developers by garyli · · Score: 3, Insightful
    Chris Shiflett has definitely created a masterpiece that I personally believe only he is capable of. His experience and precise, easy-to-read manner of writing are unparalleled when it comes to PHP security.

    One of the things I liked about this book is that you don't need to be sat next to your PC to read it. Though it has many nice and clear code examples, it's mainly about principles and theory. Excellent to have on the bedside table.

    It isn't a very thick book, but is written in a clear and accessible style, and I found myself going 'aha' all the way through. I read it quickly but have a feeling that I'll return to it often until all those best practices are memorised and I'm 'doing' them.

    What is most useful about this book is the aggregation in one place of descriptions of all of these security attacks and vulnerabilities in PHP code, along with suggestions on dealing with them.

    The only specific attack missing which I would like to have seen information about is email spamming through website forms. However the general principles described in the book will help prevent these attacks as well.

    This book will definitely be a long-term desktop reference for me and mandatory reading for all the PHP developers in my work place. I would definitely reccomend this book to aspiring PHP developers and think it would also benefit some of the more experienced folks out there.

    --
    Webmaster of Spy

  51. Re:No book can teach you because the bad don't rea by DreadHarn · · Score: 1

    Don't discount someone right out of school. A person with a two-year technical degree may not understand the concepts of security; but a person with a masters in computer science or engineering most likely would. It doesn't take an engineer to program PHP, but it does take some common programming knowledge to create well-structured applications.

  52. This book will look great next to ... by malraid · · Score: 1, Funny

    ... other fine books like the Amish Phone Book or List of Human Rights in Comunist China
     
      Laugh, it's funny....

    --
    please excuse my apathy
  53. Re:No book can teach you because the bad don't rea by T-Ranger · · Score: 1

    That may or may not be true. A CS student may have a solid foundation in security, but they might have no foundation at all in security. If your doing "pure" CS such as queue theory, or algorithm analysis, then security, let alone things like exception handling and even logging, are irrelevent. One could argue that the two year diploma types know only about all that irrelevent fluff, except in the real world that irrelevent fluff takes up 80% of the project. Consider the distinction between physics and engineering; a scientist may give you an answer to the Nth decimal place (ignoring friction), but an engineer will give you one rounded up to the next highest existing part, after a 50% safety factor. Both answers are right, but only one is right in the real world.

  54. If an echo execs and there's no string to ... by Magic5Ball · · Score: 1

    The code does something, mainly, echoing 'your name is:... and then not displaying the return value to the browser.

    --
    There are 1.1... kinds of people.
  55. Re:No book can teach you because the bad don't rea by DreadHarn · · Score: 1

    Unfortunately you are right it is one a case to case basis. One student may have enrolled in a security course while another completely skipped over it. Security really should be a requirement for all CSE and optional for CS even though the degrees (these days) seem interchangeable.

  56. hah by Heembo · · Score: 0, Flamebait

    I'm sorry - with all the PHP holes over the last year - PHP and Security seems like a contradiction in terms!

    --
    Horns are really just a broken halo.
  57. Stefan Esser Is No Expert by Anonymous Coward · · Score: 0

    "The mistakes in the book don't just disappear because Stefan wasn't polite when he pointed them out."

    I totally agree with this statement, in principle, but I've looked at the errata. Unless I'm missing something, there are no real mistakes to speak of. As the reviewer says, "As of the writing of this review, the confirmed errata is reassuringly sparse, and the unconfirmed errata is nonexistent, which speaks well of the author keeping on top of reader feedback."

    I also just read that blog entry - what a waste of time.

    Stefan Esser is clearly some kid who's jealous of the experts and their "fame" or whatever. You see this all the time in open source. Nothing new here, but it's still annoying.

    If he went through the entire book, and all he can come up with is this "mistake" that is based on his own assumptions (which are wrong), it speaks highly of the book. These are clearly arbitrary paths, and although I haven't read the book, I'm sure it has nothing to do with what's being discussed. Try again, kid.

    Plus, you can tell he struggled to even find his own point:

    "However when you look at the example above and have a clue about file permissions you should start to laugh, because it should not work. Then you try it and wonder that it actually works."

    Can't you just see this kid? "Oh, I've got them this time! Oh wait, it actually works. Awww man." Give me a break.

    The other thing you pointed to is yet another example of this:

    "I suggest that you also read Chris Shiflett's comments about this article in his blog. It is an entertaining read, because he says that I haven't understood the CSRF problem, and blames me for not initialising the variables in my example. Which is kinda strange, because the example was copied one to one from his security guide and therefore any error in it, is his fault."

    Seriously, if this kid doesn't understand that session variables persist across pages (and therefore examples), he's not worth listening to. He assumes (wrongly, again) that you shouldn't initialize session variables, and based on this assumption, he thinks he's found an error. Then he thinks he can weasel his way out. I can't believe anyone takes this guy seriously. My freshmen students could code circles around this kid.

    The author should just ignore punks like this. Hopefully he already is.

  58. Re:Wrong answer by Anonymous Coward · · Score: 0

    I know it's been ages since you posted that and you're probably not going to see this, but I can't let a comment like that get by without telling you how wrong you are.

    Let's take an overly extreme example. Some people need to walk over a tightrope between some mountains. Sure, it's possible to cross. So why are all those people falling and dying? It can't be the fault of the language; it must be the fault of the people for falling off. Those people need to learn not to fall off and die! There's no need to make the tightrope wider or stronger, add safety nets so people don't die, or add guiding ropes for people to hold on to. No, it's not the fault of the language.

    PHP the language is very much at fault. Take register_globals. How can the user be at fault when PHP encouraged (thankfully it encourages it no more) insecure features like this? Take the lack of taint checking. No-one was really going to use it anyway, were they?

    Oh, and if I build a house using a hammer that breaks nails, it sure as hell is the hammer's fault.