11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 10 • WORKING WITH THE FILE AND OPERATING SYSTEMSystem-Level Program ExecutionTruly lazy programmers know how to make the most of their entire server environment whendeveloping applications, which includes exploiting the functionality of the operating system, file system,installed program base, and programming languages whenever necessary. In this section, you’ll learnhow <strong>PHP</strong> can interact with the operating system to call both OS-level programs and third-party installedapplications. Done properly, it adds a whole new level of functionality to your <strong>PHP</strong> programmingrepertoire. Done poorly, it can be catastrophic not only to your application but also to your server’s dataintegrity. That said, before delving into this powerful feature, take a moment to consider the topic ofsanitizing user input before passing it to the shell level.Sanitizing the InputNeglecting to sanitize user input that may subsequently be passed to system-level functions could allowattackers to do massive internal damage to your information store and operating system, deface ordelete web files, and otherwise gain unrestricted access to your server. And that’s only the beginning.■ Note See Chapter 13 for a discussion of secure <strong>PHP</strong> programming.As an example of why sanitizing the input is so important, consider a real-world scenario. Supposethat you offer an online service that generates PDFs from an input URL. A great tool for accomplishingjust this is the open source program HTMLDOC (www.htmldoc.org), which converts HTML documents toindexed HTML, Adobe PostScript, and PDF files. HTMLDOC can be invoked from the command line,like so:%>htmldoc --webpage –f webpage.pdf http://www.wjgilmore.com/This would result in the creation of a PDF named webpage.pdf, which would contain a snapshot ofthe web site’s index page. Of course, most users will not have command-line access to your server;therefore, you’ll need to create a much more controlled interface, such as a web page. Using <strong>PHP</strong>’spassthru() function (introduced in the later section “<strong>PHP</strong>’s Program Execution Functions”), you can callHTMLDOC and return the desired PDF, like so:$document = $_POST['userurl'];passthru("htmldoc --webpage -f webpage.pdf $document);What if an enterprising attacker took the liberty of passing through additional input, unrelated tothe desired HTML page, entering something like this:http://www.wjgilmore.com/ ; cd /var/www/; rm –rf *Most Unix shells would interpret the passthru() request as three separate commands. The first isthis:251

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!