cvs: presentations / web-app-security.xml /slides/web-app-security attack.jpg brine-injection.jpeg command-injection.xml crypto.xml
db-cmd-injection-stop.xml db-cmd-injection.xml db-esc-chars.xml defeating-xss-encode.xml defeating-xss.xml error-display.xml error-handler.xml
esc-shell-args.xml esc-shell-cmds.xml hash-receive-cookie.xml hash-receive-form-multi.xml hash-receive-form.xml hash-send-cookie.xml
hash-send-form-multi.xml hash-send-form.xml hash.xml laptop-saw.jpeg mcrypt-retrieve.xml mcrypt-store.xml more-info.xml potato.jpg
shell-cmd-injection.xml squid.jpg t2phage.gif titlepage.xml toc.xml validate-regex.xml validate-syntax.xml validate-value.xml xss-unicode.xml xss.xml
| From: | David Sklar | Date: | Mon, 05 May 2003 14:38:27 +0000 |
| Subject: | cvs: presentations / web-app-security.xml /slides/web-app-security attack.jpg brine-injection.jpeg command-injection.xml crypto.xml db-cmd-injection-stop.xml db-cmd-injection.xml db-esc-chars.xml defeating-xss-encode.xml defeating-xss.xml error-display.xml error-handler.xml esc-shell-args.xml esc-shell-cmds.xml hash-receive-cookie.xml hash-receive-form-multi.xml hash-receive-form.xml hash-send-cookie.xml hash-send-form-multi.xml hash-send-form.xml hash.xml laptop-saw.jpeg mcrypt-retrieve.xml mcrypt-store.xml more-info.xml potato.jpg shell-cmd-injection.xml squid.jpg t2phage.gif titlepage.xml toc.xml validate-regex.xml validate-syntax.xml validate-value.xml xss-unicode.xml xss.xml |
||
| Groups: | php.pres | ||
| Request: | Send a blank email to pres+get-691@lists.php.net to get a copy of this message | ||
sklar Mon May 5 10:38:27 2003 EDT
Added files:
/presentations web-app-security.xml
/presentations/slides/web-app-security attack.jpg
brine-injection.jpeg
command-injection.xml
crypto.xml
db-cmd-injection-stop.xml
db-cmd-injection.xml
db-esc-chars.xml
defeating-xss-encode.xml
defeating-xss.xml
error-display.xml
error-handler.xml
esc-shell-args.xml
esc-shell-cmds.xml
hash-receive-cookie.xml
hash-receive-form-multi.xml
hash-receive-form.xml
hash-send-cookie.xml
hash-send-form-multi.xml
hash-send-form.xml hash.xml
laptop-saw.jpeg
mcrypt-retrieve.xml
mcrypt-store.xml
more-info.xml potato.jpg
shell-cmd-injection.xml
squid.jpg t2phage.gif
titlepage.xml toc.xml
validate-regex.xml
validate-syntax.xml
validate-value.xml
xss-unicode.xml xss.xml
Log:
Intl PHP Con 2003 Presentation
Index: presentations/web-app-security.xml +++ presentations/web-app-security.xml <presentation> <topic>PHP</topic> <title>PHP Web Application Security Vulnerabilities</title> <event>International PHP Conference</event> <location>Amsterdam, NL</location> <date>May 8, 2003</date> <speaker>David Sklar</speaker> <email>sklar@sklar.com</email> <url>http://talks.php.net/show/web-app-security</url> <slide>slides/web-app-security/titlepage.xml</slide> <slide>slides/web-app-security/toc.xml</slide> <slide>slides/web-app-security/validate-syntax.xml</slide> <slide>slides/web-app-security/validate-value.xml</slide> <slide>slides/web-app-security/validate-regex.xml</slide> <slide>slides/web-app-security/hash.xml</slide> <slide>slides/web-app-security/hash-send-cookie.xml</slide> <slide>slides/web-app-security/hash-receive-cookie.xml</slide> <slide>slides/web-app-security/hash-send-form.xml</slide> <slide>slides/web-app-security/hash-receive-form.xml</slide> <slide>slides/web-app-security/hash-send-form-multi.xml</slide> <slide>slides/web-app-security/hash-receive-form-multi.xml</slide> <slide>slides/web-app-security/xss.xml</slide> <slide>slides/web-app-security/defeating-xss.xml</slide> <slide>slides/web-app-security/defeating-xss-encode.xml</slide> <slide>slides/web-app-security/xss-unicode.xml</slide> <slide>slides/web-app-security/command-injection.xml</slide> <slide>slides/web-app-security/db-esc-chars.xml</slide> <slide>slides/web-app-security/db-cmd-injection.xml</slide> <slide>slides/web-app-security/db-cmd-injection-stop.xml</slide> <slide>slides/web-app-security/shell-cmd-injection.xml</slide> <slide>slides/web-app-security/esc-shell-args.xml</slide> <slide>slides/web-app-security/esc-shell-cmds.xml</slide> <slide>slides/web-app-security/error-display.xml</slide> <slide>slides/web-app-security/error-handler.xml</slide> <slide>slides/web-app-security/crypto.xml</slide> <slide>slides/web-app-security/mcrypt-store.xml</slide> <slide>slides/web-app-security/mcrypt-retrieve.xml</slide> <slide>slides/web-app-security/more-info.xml</slide> </presentation> Index: presentations/slides/web-app-security/command-injection.xml +++ presentations/slides/web-app-security/command-injection.xml <slide title="Command Injection"> <list><li>Like XSS, but nasty stuff is intended for your server or database</li></list> <list><li>Different set of code/data delimiters:</li></list> <list marginleft="2em"> <li>Database escapes and wildcard characters</li> <li>Shell escapes, wildcards, and metacharacters</li> </list> <image filename="brine-injection.jpeg" align="center"/> </slide> Index: presentations/slides/web-app-security/crypto.xml +++ presentations/slides/web-app-security/crypto.xml <slide title="Cryptography"> <list> <bullet>Use SSL to prevent network sniffing</bullet> <bullet>Use %mcrypt%</bullet> <bullet>If you store keys, mind the security on the key storage</bullet> <bullet>Don't store plaintext passwords</bullet> <bullet>Watch out for misconfigured shared hosting environments</bullet> </list> </slide> Index: presentations/slides/web-app-security/db-cmd-injection-stop.xml +++ presentations/slides/web-app-security/db-cmd-injection-stop.xml <slide title="DB Command Injection"> <example title="Use the %?% placeholder with PEAR DB" fontsize="1.6em"><![CDATA[<?php $sth = $dbh->query('INSERT INTO songs (title,artist) VALUES (?,?)', array($title, $artist)); ?>]]></example> <example title="Use %strtr()% to escape %%% and %_% after escaping %'%" fontsize="1.6em"><![CDATA[<?php $title = $dbh->quote($title); $title = strtr($title, array('_' => '\_', '%' => '\%')); $sth = $dbh->query("DELETE FROM songs WHERE title LIKE $title"); ?>]]></example> <blurb title="You can also use %mysql_real_escape_string()% or %addslashes()%." fontsize="2em"/> </slide> Index: presentations/slides/web-app-security/db-cmd-injection.xml +++ presentations/slides/web-app-security/db-cmd-injection.xml <slide title="DB Command Injection"> <example title="Escape %'% %%% and %_% or users can write their own queries">DELETE FROM songs WHERE title LIKE '$title' // What if $title = "%' OR 'a' = 'a"? DELETE FROM songs WHERE title LIKE '%' OR 'a' = 'a' // All rows are deleted! </example> <example>INSERT INTO songs (title, artist) VALUES ('$title','$artist') // What if $title = "a','b'),('c" and $artist = "d" INSERT INTO songs (title, artist) VALUES ('a','b'),('c','d') // Multiple records inserted! </example> </slide> Index: presentations/slides/web-app-security/db-esc-chars.xml +++ presentations/slides/web-app-security/db-esc-chars.xml <slide title="DB Escape Characters"> <example title="Escape %'% to prevent errors">SELECT * FROM songs WHERE title LIKE 'Don\'t Be Cruel'</example> <example title="Escape %%% and %_% to prevent unexpected wildcarding">SELECT * FROM solution WHERE concentration LIKE '10\%'</example> </slide> Index: presentations/slides/web-app-security/defeating-xss-encode.xml +++ presentations/slides/web-app-security/defeating-xss-encode.xml <slide title="Defeating XSS"> <example title="You must encode special characters!" fontsize="1.6em" result="1"><![CDATA[<?php $bad = '<script>alert("boo!")</script>'; $ok = htmlspecialchars($bad,ENT_QUOTES); // < > & " ' print "Alert...$bad"; print $ok; ?>]]></example> </slide> Index: presentations/slides/web-app-security/defeating-xss.xml +++ presentations/slides/web-app-security/defeating-xss.xml <slide title="Defeating XSS"> <blurb>Before displaying it, modify user input to defang scripts.</blurb> <example title="You can remove all HTML and PHP tags" fontsize="1.6em" result="1"><![CDATA[<?php $bad = "<?php header(); ?><blink>Ugh!</blink><?php footer(); ?>"; $ok = strip_tags($bad); print $ok; ?>]]></example> </slide> Index: presentations/slides/web-app-security/error-display.xml +++ presentations/slides/web-app-security/error-display.xml <slide title="Error Handling"> <example title="Don't Display Raw Errors to Users">display_errors = off log_errors = on </example> <blurb> This logs errors, but puts them in the server's error log, not in the browser. </blurb> </slide> Index: presentations/slides/web-app-security/error-handler.xml +++ presentations/slides/web-app-security/error-handler.xml <slide title="Error Handlers"> <example title="Use a Custom Error Handler"><![CDATA[<?php set_error_handler('do_errors'); function do_errors($errno, $errstr, $errfile, $errline) { error_log("ERROR ($errfile:$errline): $errstr"); header('Location: http://'.$_SERVER['HTTP_HOST'].'/error.php'); exit(); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/esc-shell-args.xml +++ presentations/slides/web-app-security/esc-shell-args.xml <slide title="Escaping Shell Arguments"> <list> <li>%escapeshellarg()% puts single quotes around an argument and escapes single quotes inside the argument</li> </list> <example><![CDATA[<?php $bad_arg = '-al; rm -rf /'; $ok_arg = escapeshellarg($bad_arg); // list all files, then delete everything! system("ls $bad_arg"); // is there a file called "-al; rm -rf /"? system("ls $ok_arg"); ?>]]></example> </slide> Index: presentations/slides/web-app-security/esc-shell-cmds.xml +++ presentations/slides/web-app-security/esc-shell-cmds.xml <slide title="Escaping Shell Commands"> <blurb align="center" title="%escapeshellcmd()% escapes but doesn't quote:">%<![CDATA[# & ; ` ' " | * ? ~ < > ^ ( ) [ ] { } $ \ 0x0A 0xFF]]>%</blurb> <example fontsize="1.6em"><![CDATA[<?php $bad_format = 'html <a>'; $also_bad_format = escapeshellarg($bad_format); $ok_format = escapeshellcmd($bad_format); // redirection error system("/usr/local/bin/formatter-$bad_format"); // no such program system("/usr/local/bin/formatter-$also_bad_format"); // runs /usr/local/bin/formatter-html with correct argument system("/usr/local/bin/formatter-$ok_format"); ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-receive-cookie.xml +++ presentations/slides/web-app-security/hash-receive-cookie.xml <slide title="Hash with Cookie"> <example title="Receiving and verifying the cookie"><![CDATA[<?php list($c_id,$c_hash) = explode('-',$_COOKIE['id']); if (md5($secret_word.$c_id) == $c_hash) { $id = $c_id; } else { die('Invalid cookie.'); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-receive-form-multi.xml +++ presentations/slides/web-app-security/hash-receive-form-multi.xml <slide title="Hash with Form"> <example title="Processing the Form"><![CDATA[<?php if (md5($secret_work.$_POST['vars']) == $_POST['hash']) { $vars = unserialize($_POST['vars']); // put back into global namespace extract($vars); } else { die('Invalid vars'); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-receive-form.xml +++ presentations/slides/web-app-security/hash-receive-form.xml <slide title="Hash with Form"> <example title="Processing the Form"><![CDATA[<?php if (md5($secret_word.$_POST['id']) == $_POST['hash']) { $id = $_POST['id']; } else { die('Invalid ID'); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-send-cookie.xml +++ presentations/slides/web-app-security/hash-send-cookie.xml <slide title="Hash with Cookie"> <example title="Sending the cookie"><![CDATA[<?php $secret_word = 'salt-baked squid'; $id = 31415926; $hash = md5($secret_word.$id); setcookie('id',$id.'-'.$hash); ?>]]></example> <image filename="squid.jpg" align="center"/> </slide> Index: presentations/slides/web-app-security/hash-send-form-multi.xml +++ presentations/slides/web-app-security/hash-send-form-multi.xml <slide title="Hash with Form"> <example title="Hashing Multiple Values"><![CDATA[<?php $secret_word = 'salt-baked squid'; $var_names = array('id','shopping_cart','size'); $vars_string = serialize(compact($var_names)); $hash = md5($secret_word.$vars_string); $vars_safe = htmlspecialchars($vars_string); print<<<_HTML_ <form method="POST" action="save.php"> <input type="hidden" name="vars" value="$vars_safe"> <input type="hidden" name="hash" value="$hash"> ... other form elements ... </form> _HTML_; ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-send-form.xml +++ presentations/slides/web-app-security/hash-send-form.xml <slide title="Hash with Form"> <example title="Displaying the Form"><![CDATA[<?php $secret_word = 'salt-baked squid'; $id = 31415926; $hash = md5($secret_word.$id); print<<<_HTML_ <form method="POST" action="save.php"> <input type="hidden" name="id" value="$id"> <input type="hidden" name="hash" value="$hash"> ... other form elements ... </form> _HTML_; ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash.xml +++ presentations/slides/web-app-security/hash.xml <slide title="Data Tampering"> <image filename="laptop-saw.jpeg" align="right" marginright="5em"/> <list title="Sending the hash"> <li>For cookies or hidden form fields</li> <li>Hash: data plus secret word</li> <li>Add hash to a cookie or form</li> </list> <list title="Checking the hash"> <li>When you get the data back, re-hash</li> <li>Compare received and calculated hashes</li> </list> </slide> Index: presentations/slides/web-app-security/mcrypt-retrieve.xml +++ presentations/slides/web-app-security/mcrypt-retrieve.xml <slide title="Decrypting Data"> <example fontsize="1.5em"><![CDATA[<?php $alg = MCRYPT_BLOWFISH; $mode = MCRYPT_MODE_CBC; $id = intval($_REQUEST['id']); $r = mysql_query("SELECT iv, ciphertext FROM secrets WHERE id = $id"); $ar = mysql_fetch_assoc($r); $plaintext = mcrypt_decrypt($alg, $_REQUEST['key'], $ar['ciphertext'], $mode, $ar['iv']); ?>]]></example> <image filename="potato.jpg" align="center"/> </slide> Index: presentations/slides/web-app-security/mcrypt-store.xml +++ presentations/slides/web-app-security/mcrypt-store.xml <slide title="Storing Encrypted Data"> <example fontsize="1.5em"><![CDATA[<?php $key = 'Is this the potato farm?' $s = 'The account balance is $2000'; $alg = MCRYPT_BLOWFISH; $mode = MCRYPT_MODE_CBC; $iv = mcrypt_create_iv(mcrypt_get_iv_size($alg,$mode), MCRYPT_DEV_URANDOM); $ciphertext = mcrypt_encrypt($alg, $key, $s, $mode, $iv); $quoted_iv = mysql_real_escape_string($iv); $quoted_ct = mysql_real_escape_string($ciphertext); mysql_query("INSERT INTO secrets (iv, ciphertext) VALUES ('$quoted_iv','$quoted_ct')"); ?>]]></example> </slide> Index: presentations/slides/web-app-security/more-info.xml +++ presentations/slides/web-app-security/more-info.xml <slide title="More Information"> <list marginbottom="0em" title="PHP Security Resources"> <li>This presentation: <![CDATA[<a href="http://talks.php.net/show/web-app-security">http://talks.php.net/show/web-app-security</a>]]></li> <li>PHP Manual - Chapter 5 ("Security"): <![CDATA[<a href="http://www.php.net/security">http://www.php.net/security</a>]]></li> <li>Open Web Application Security Project: <![CDATA[<a href="http://www.owasp.org">http://www.owasp.org</a>]]></li> <li>PHP and the OWASP Top Ten: <![CDATA[<a href="http://www.sklar.com/page/article/owasp-top-ten">http://www.sklar.com/page/article/owasp-top-ten</a>]]></li> <li>Secure Programming HOWTO: <![CDATA[<a href="http://tldp.org/HOWTO/Secure-Programs-HOWTO/">http://tldp.org/HOWTO/Secure-Programs-HOWTO/</a>]]></li> </list> </slide> Index: presentations/slides/web-app-security/shell-cmd-injection.xml +++ presentations/slides/web-app-security/shell-cmd-injection.xml <slide title="Shell Command Injection"> <list> <li>There can't be command injection if there's no command -- do you really need to run an external program?</li> <li>Plenty of PHP built-ins for file operations:</li> </list> <list marginleft="2em"> <li>%mkdir()%</li><li>%rmdir()%</li><li>%copy()%</li> <li>%move()%</li><li>%unlink()%</li><li>%chmod()%</li> <li>%chown()%</li><li>%chgrp()%</li><li>%...%</li> </list> </slide> Index: presentations/slides/web-app-security/titlepage.xml +++ presentations/slides/web-app-security/titlepage.xml <slide template="titlepage" fontsize="4em"> <title>Welcome!</title> </slide> Index: presentations/slides/web-app-security/toc.xml +++ presentations/slides/web-app-security/toc.xml <slide title="Topics"> <image filename="attack.jpg" align="right" marginright="2em"/> <list talign="center" fontsize="4em" title="Vulnerabilities"> <bullet>Unvalidated Parameters</bullet> <bullet>Cross-Site Scripting Flaws</bullet> <bullet>Command Injection Flaws</bullet> <bullet>Error Handling Problems</bullet> <bullet>Insecure Use of Cryptography</bullet> </list> </slide> Index: presentations/slides/web-app-security/validate-regex.xml +++ presentations/slides/web-app-security/validate-regex.xml <slide title="Regular Expressions"> <example title="Test Values with Regular Expressions"><![CDATA[<?php if (! preg_match('/^\d{5}(-\d{4})?$/', $_POST['zipcode'])) { ... } $email_rx = '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i'; if (! preg_match($email_rx, $_POST['email'])) { ... } ?>]]></example> </slide> Index: presentations/slides/web-app-security/validate-syntax.xml +++ presentations/slides/web-app-security/validate-syntax.xml <slide title="Validating Data"> <list> <li> Turn off %register_globals% and rely on %$_GET%, %$_POST%, %$_COOKIE%, and %$_REQUEST%</li> <li> Test submitted data for expected syntax:</li> </list> <example><![CDATA[<?php if (is_numeric($_POST['age'])) { ... } ?>]]></example> <example><![CDATA[<?php if (strlen(trim($_GET['address']))) { ... } ?>]]></example> </slide> Index: presentations/slides/web-app-security/validate-value.xml +++ presentations/slides/web-app-security/validate-value.xml <slide title="Testing Values"> <example title="Check allowable range of numeric values"><![CDATA[<?php if (($_GET['age'] < 13) || ($_GET['age'] > 65)) {...} ?>]]></example> <example title="Restrict %&lt;select>% choices to a preset array"><![CDATA[<?php $choices = array(1 => 'Trout', 'Halibut', 'Tilapia'); if (! $choices[$_POST['fish']]) { ... } ?>]]></example> </slide> Index: presentations/slides/web-app-security/xss-unicode.xml +++ presentations/slides/web-app-security/xss-unicode.xml <slide title="UTF-8 Encoding"> <example title="Remove UTF-8 Encoding" fontsize="1.6em"><![CDATA[<?php $iso_8859_1 = utf8_decode($bad); ?>]]></example> <example title="Force ISO-8859-1 on the Browser" fontsize="1.6em"><![CDATA[<?php print '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">'; ?>]]></example> </slide> Index: presentations/slides/web-app-security/xss.xml +++ presentations/slides/web-app-security/xss.xml <slide title="Cross-Site Scripting"> <image filename="t2phage.gif" align="left" marginleft="1em"/> <list marginleft="1em"> <li>Interactive features of your site are used as unwitting carriers of client-side attacks</li> </list> <list marginleft="1em"> <li> Anywhere you display external input:</li> </list> <list marginleft="3em"> <li> User profile </li> <li> Forums</li> <li> Error pages (the URL is external input)</li> </list> </slide>
Index: presentations/web-app-security.xml +++ presentations/web-app-security.xml <presentation> <topic>PHP</topic> <title>PHP Web Application Security Vulnerabilities</title> <event>International PHP Conference</event> <location>Amsterdam, NL</location> <date>May 8, 2003</date> <speaker>David Sklar</speaker> <email>sklar@sklar.com</email> <url>http://talks.php.net/show/web-app-security</url> <slide>slides/web-app-security/titlepage.xml</slide> <slide>slides/web-app-security/toc.xml</slide> <slide>slides/web-app-security/validate-syntax.xml</slide> <slide>slides/web-app-security/validate-value.xml</slide> <slide>slides/web-app-security/validate-regex.xml</slide> <slide>slides/web-app-security/hash.xml</slide> <slide>slides/web-app-security/hash-send-cookie.xml</slide> <slide>slides/web-app-security/hash-receive-cookie.xml</slide> <slide>slides/web-app-security/hash-send-form.xml</slide> <slide>slides/web-app-security/hash-receive-form.xml</slide> <slide>slides/web-app-security/hash-send-form-multi.xml</slide> <slide>slides/web-app-security/hash-receive-form-multi.xml</slide> <slide>slides/web-app-security/xss.xml</slide> <slide>slides/web-app-security/defeating-xss.xml</slide> <slide>slides/web-app-security/defeating-xss-encode.xml</slide> <slide>slides/web-app-security/xss-unicode.xml</slide> <slide>slides/web-app-security/command-injection.xml</slide> <slide>slides/web-app-security/db-esc-chars.xml</slide> <slide>slides/web-app-security/db-cmd-injection.xml</slide> <slide>slides/web-app-security/db-cmd-injection-stop.xml</slide> <slide>slides/web-app-security/shell-cmd-injection.xml</slide> <slide>slides/web-app-security/esc-shell-args.xml</slide> <slide>slides/web-app-security/esc-shell-cmds.xml</slide> <slide>slides/web-app-security/error-display.xml</slide> <slide>slides/web-app-security/error-handler.xml</slide> <slide>slides/web-app-security/crypto.xml</slide> <slide>slides/web-app-security/mcrypt-store.xml</slide> <slide>slides/web-app-security/mcrypt-retrieve.xml</slide> <slide>slides/web-app-security/more-info.xml</slide> </presentation> Index: presentations/slides/web-app-security/command-injection.xml +++ presentations/slides/web-app-security/command-injection.xml <slide title="Command Injection"> <list><li>Like XSS, but nasty stuff is intended for your server or database</li></list> <list><li>Different set of code/data delimiters:</li></list> <list marginleft="2em"> <li>Database escapes and wildcard characters</li> <li>Shell escapes, wildcards, and metacharacters</li> </list> <image filename="brine-injection.jpeg" align="center"/> </slide> Index: presentations/slides/web-app-security/crypto.xml +++ presentations/slides/web-app-security/crypto.xml <slide title="Cryptography"> <list> <bullet>Use SSL to prevent network sniffing</bullet> <bullet>Use %mcrypt%</bullet> <bullet>If you store keys, mind the security on the key storage</bullet> <bullet>Don't store plaintext passwords</bullet> <bullet>Watch out for misconfigured shared hosting environments</bullet> </list> </slide> Index: presentations/slides/web-app-security/db-cmd-injection-stop.xml +++ presentations/slides/web-app-security/db-cmd-injection-stop.xml <slide title="DB Command Injection"> <example title="Use the %?% placeholder with PEAR DB" fontsize="1.6em"><![CDATA[<?php $sth = $dbh->query('INSERT INTO songs (title,artist) VALUES (?,?)', array($title, $artist)); ?>]]></example> <example title="Use %strtr()% to escape %%% and %_% after escaping %'%" fontsize="1.6em"><![CDATA[<?php $title = $dbh->quote($title); $title = strtr($title, array('_' => '\_', '%' => '\%')); $sth = $dbh->query("DELETE FROM songs WHERE title LIKE $title"); ?>]]></example> <blurb title="You can also use %mysql_real_escape_string()% or %addslashes()%." fontsize="2em"/> </slide> Index: presentations/slides/web-app-security/db-cmd-injection.xml +++ presentations/slides/web-app-security/db-cmd-injection.xml <slide title="DB Command Injection"> <example title="Escape %'% %%% and %_% or users can write their own queries">DELETE FROM songs WHERE title LIKE '$title' // What if $title = "%' OR 'a' = 'a"? DELETE FROM songs WHERE title LIKE '%' OR 'a' = 'a' // All rows are deleted! </example> <example>INSERT INTO songs (title, artist) VALUES ('$title','$artist') // What if $title = "a','b'),('c" and $artist = "d" INSERT INTO songs (title, artist) VALUES ('a','b'),('c','d') // Multiple records inserted! </example> </slide> Index: presentations/slides/web-app-security/db-esc-chars.xml +++ presentations/slides/web-app-security/db-esc-chars.xml <slide title="DB Escape Characters"> <example title="Escape %'% to prevent errors">SELECT * FROM songs WHERE title LIKE 'Don\'t Be Cruel'</example> <example title="Escape %%% and %_% to prevent unexpected wildcarding">SELECT * FROM solution WHERE concentration LIKE '10\%'</example> </slide> Index: presentations/slides/web-app-security/defeating-xss-encode.xml +++ presentations/slides/web-app-security/defeating-xss-encode.xml <slide title="Defeating XSS"> <example title="You must encode special characters!" fontsize="1.6em" result="1"><![CDATA[<?php $bad = '<script>alert("boo!")</script>'; $ok = htmlspecialchars($bad,ENT_QUOTES); // < > & " ' print "Alert...$bad"; print $ok; ?>]]></example> </slide> Index: presentations/slides/web-app-security/defeating-xss.xml +++ presentations/slides/web-app-security/defeating-xss.xml <slide title="Defeating XSS"> <blurb>Before displaying it, modify user input to defang scripts.</blurb> <example title="You can remove all HTML and PHP tags" fontsize="1.6em" result="1"><![CDATA[<?php $bad = "<?php header(); ?><blink>Ugh!</blink><?php footer(); ?>"; $ok = strip_tags($bad); print $ok; ?>]]></example> </slide> Index: presentations/slides/web-app-security/error-display.xml +++ presentations/slides/web-app-security/error-display.xml <slide title="Error Handling"> <example title="Don't Display Raw Errors to Users">display_errors = off log_errors = on </example> <blurb> This logs errors, but puts them in the server's error log, not in the browser. </blurb> </slide> Index: presentations/slides/web-app-security/error-handler.xml +++ presentations/slides/web-app-security/error-handler.xml <slide title="Error Handlers"> <example title="Use a Custom Error Handler"><![CDATA[<?php set_error_handler('do_errors'); function do_errors($errno, $errstr, $errfile, $errline) { error_log("ERROR ($errfile:$errline): $errstr"); header('Location: http://'.$_SERVER['HTTP_HOST'].'/error.php'); exit(); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/esc-shell-args.xml +++ presentations/slides/web-app-security/esc-shell-args.xml <slide title="Escaping Shell Arguments"> <list> <li>%escapeshellarg()% puts single quotes around an argument and escapes single quotes inside the argument</li> </list> <example><![CDATA[<?php $bad_arg = '-al; rm -rf /'; $ok_arg = escapeshellarg($bad_arg); // list all files, then delete everything! system("ls $bad_arg"); // is there a file called "-al; rm -rf /"? system("ls $ok_arg"); ?>]]></example> </slide> Index: presentations/slides/web-app-security/esc-shell-cmds.xml +++ presentations/slides/web-app-security/esc-shell-cmds.xml <slide title="Escaping Shell Commands"> <blurb align="center" title="%escapeshellcmd()% escapes but doesn't quote:">%<![CDATA[# & ; ` ' " | * ? ~ < > ^ ( ) [ ] { } $ \ 0x0A 0xFF]]>%</blurb> <example fontsize="1.6em"><![CDATA[<?php $bad_format = 'html <a>'; $also_bad_format = escapeshellarg($bad_format); $ok_format = escapeshellcmd($bad_format); // redirection error system("/usr/local/bin/formatter-$bad_format"); // no such program system("/usr/local/bin/formatter-$also_bad_format"); // runs /usr/local/bin/formatter-html with correct argument system("/usr/local/bin/formatter-$ok_format"); ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-receive-cookie.xml +++ presentations/slides/web-app-security/hash-receive-cookie.xml <slide title="Hash with Cookie"> <example title="Receiving and verifying the cookie"><![CDATA[<?php list($c_id,$c_hash) = explode('-',$_COOKIE['id']); if (md5($secret_word.$c_id) == $c_hash) { $id = $c_id; } else { die('Invalid cookie.'); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-receive-form-multi.xml +++ presentations/slides/web-app-security/hash-receive-form-multi.xml <slide title="Hash with Form"> <example title="Processing the Form"><![CDATA[<?php if (md5($secret_work.$_POST['vars']) == $_POST['hash']) { $vars = unserialize($_POST['vars']); // put back into global namespace extract($vars); } else { die('Invalid vars'); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-receive-form.xml +++ presentations/slides/web-app-security/hash-receive-form.xml <slide title="Hash with Form"> <example title="Processing the Form"><![CDATA[<?php if (md5($secret_word.$_POST['id']) == $_POST['hash']) { $id = $_POST['id']; } else { die('Invalid ID'); } ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-send-cookie.xml +++ presentations/slides/web-app-security/hash-send-cookie.xml <slide title="Hash with Cookie"> <example title="Sending the cookie"><![CDATA[<?php $secret_word = 'salt-baked squid'; $id = 31415926; $hash = md5($secret_word.$id); setcookie('id',$id.'-'.$hash); ?>]]></example> <image filename="squid.jpg" align="center"/> </slide> Index: presentations/slides/web-app-security/hash-send-form-multi.xml +++ presentations/slides/web-app-security/hash-send-form-multi.xml <slide title="Hash with Form"> <example title="Hashing Multiple Values"><![CDATA[<?php $secret_word = 'salt-baked squid'; $var_names = array('id','shopping_cart','size'); $vars_string = serialize(compact($var_names)); $hash = md5($secret_word.$vars_string); $vars_safe = htmlspecialchars($vars_string); print<<<_HTML_ <form method="POST" action="save.php"> <input type="hidden" name="vars" value="$vars_safe"> <input type="hidden" name="hash" value="$hash"> ... other form elements ... </form> _HTML_; ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash-send-form.xml +++ presentations/slides/web-app-security/hash-send-form.xml <slide title="Hash with Form"> <example title="Displaying the Form"><![CDATA[<?php $secret_word = 'salt-baked squid'; $id = 31415926; $hash = md5($secret_word.$id); print<<<_HTML_ <form method="POST" action="save.php"> <input type="hidden" name="id" value="$id"> <input type="hidden" name="hash" value="$hash"> ... other form elements ... </form> _HTML_; ?>]]></example> </slide> Index: presentations/slides/web-app-security/hash.xml +++ presentations/slides/web-app-security/hash.xml <slide title="Data Tampering"> <image filename="laptop-saw.jpeg" align="right" marginright="5em"/> <list title="Sending the hash"> <li>For cookies or hidden form fields</li> <li>Hash: data plus secret word</li> <li>Add hash to a cookie or form</li> </list> <list title="Checking the hash"> <li>When you get the data back, re-hash</li> <li>Compare received and calculated hashes</li> </list> </slide> Index: presentations/slides/web-app-security/mcrypt-retrieve.xml +++ presentations/slides/web-app-security/mcrypt-retrieve.xml <slide title="Decrypting Data"> <example fontsize="1.5em"><![CDATA[<?php $alg = MCRYPT_BLOWFISH; $mode = MCRYPT_MODE_CBC; $id = intval($_REQUEST['id']); $r = mysql_query("SELECT iv, ciphertext FROM secrets WHERE id = $id"); $ar = mysql_fetch_assoc($r); $plaintext = mcrypt_decrypt($alg, $_REQUEST['key'], $ar['ciphertext'], $mode, $ar['iv']); ?>]]></example> <image filename="potato.jpg" align="center"/> </slide> Index: presentations/slides/web-app-security/mcrypt-store.xml +++ presentations/slides/web-app-security/mcrypt-store.xml <slide title="Storing Encrypted Data"> <example fontsize="1.5em"><![CDATA[<?php $key = 'Is this the potato farm?' $s = 'The account balance is $2000'; $alg = MCRYPT_BLOWFISH; $mode = MCRYPT_MODE_CBC; $iv = mcrypt_create_iv(mcrypt_get_iv_size($alg,$mode), MCRYPT_DEV_URANDOM); $ciphertext = mcrypt_encrypt($alg, $key, $s, $mode, $iv); $quoted_iv = mysql_real_escape_string($iv); $quoted_ct = mysql_real_escape_string($ciphertext); mysql_query("INSERT INTO secrets (iv, ciphertext) VALUES ('$quoted_iv','$quoted_ct')"); ?>]]></example> </slide> Index: presentations/slides/web-app-security/more-info.xml +++ presentations/slides/web-app-security/more-info.xml <slide title="More Information"> <list marginbottom="0em" title="PHP Security Resources"> <li>This presentation: <![CDATA[<a href="http://talks.php.net/show/web-app-security">http://talks.php.net/show/web-app-security</a>]]></li> <li>PHP Manual - Chapter 5 ("Security"): <![CDATA[<a href="http://www.php.net/security">http://www.php.net/security</a>]]></li> <li>Open Web Application Security Project: <![CDATA[<a href="http://www.owasp.org">http://www.owasp.org</a>]]></li> <li>PHP and the OWASP Top Ten: <![CDATA[<a href="http://www.sklar.com/page/article/owasp-top-ten">http://www.sklar.com/page/article/owasp-top-ten</a>]]></li> <li>Secure Programming HOWTO: <![CDATA[<a href="http://tldp.org/HOWTO/Secure-Programs-HOWTO/">http://tldp.org/HOWTO/Secure-Programs-HOWTO/</a>]]></li> </list> </slide> Index: presentations/slides/web-app-security/shell-cmd-injection.xml +++ presentations/slides/web-app-security/shell-cmd-injection.xml <slide title="Shell Command Injection"> <list> <li>There can't be command injection if there's no command -- do you really need to run an external program?</li> <li>Plenty of PHP built-ins for file operations:</li> </list> <list marginleft="2em"> <li>%mkdir()%</li><li>%rmdir()%</li><li>%copy()%</li> <li>%move()%</li><li>%unlink()%</li><li>%chmod()%</li> <li>%chown()%</li><li>%chgrp()%</li><li>%...%</li> </list> </slide> Index: presentations/slides/web-app-security/titlepage.xml +++ presentations/slides/web-app-security/titlepage.xml <slide template="titlepage" fontsize="4em"> <title>Welcome!</title> </slide> Index: presentations/slides/web-app-security/toc.xml +++ presentations/slides/web-app-security/toc.xml <slide title="Topics"> <image filename="attack.jpg" align="right" marginright="2em"/> <list talign="center" fontsize="4em" title="Vulnerabilities"> <bullet>Unvalidated Parameters</bullet> <bullet>Cross-Site Scripting Flaws</bullet> <bullet>Command Injection Flaws</bullet> <bullet>Error Handling Problems</bullet> <bullet>Insecure Use of Cryptography</bullet> </list> </slide> Index: presentations/slides/web-app-security/validate-regex.xml +++ presentations/slides/web-app-security/validate-regex.xml <slide title="Regular Expressions"> <example title="Test Values with Regular Expressions"><![CDATA[<?php if (! preg_match('/^\d{5}(-\d{4})?$/', $_POST['zipcode'])) { ... } $email_rx = '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i'; if (! preg_match($email_rx, $_POST['email'])) { ... } ?>]]></example> </slide> Index: presentations/slides/web-app-security/validate-syntax.xml +++ presentations/slides/web-app-security/validate-syntax.xml <slide title="Validating Data"> <list> <li> Turn off %register_globals% and rely on %$_GET%, %$_POST%, %$_COOKIE%, and %$_REQUEST%</li> <li> Test submitted data for expected syntax:</li> </list> <example><![CDATA[<?php if (is_numeric($_POST['age'])) { ... } ?>]]></example> <example><![CDATA[<?php if (strlen(trim($_GET['address']))) { ... } ?>]]></example> </slide> Index: presentations/slides/web-app-security/validate-value.xml +++ presentations/slides/web-app-security/validate-value.xml <slide title="Testing Values"> <example title="Check allowable range of numeric values"><![CDATA[<?php if (($_GET['age'] < 13) || ($_GET['age'] > 65)) {...} ?>]]></example> <example title="Restrict %&lt;select>% choices to a preset array"><![CDATA[<?php $choices = array(1 => 'Trout', 'Halibut', 'Tilapia'); if (! $choices[$_POST['fish']]) { ... } ?>]]></example> </slide> Index: presentations/slides/web-app-security/xss-unicode.xml +++ presentations/slides/web-app-security/xss-unicode.xml <slide title="UTF-8 Encoding"> <example title="Remove UTF-8 Encoding" fontsize="1.6em"><![CDATA[<?php $iso_8859_1 = utf8_decode($bad); ?>]]></example> <example title="Force ISO-8859-1 on the Browser" fontsize="1.6em"><![CDATA[<?php print '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">'; ?>]]></example> </slide> Index: presentations/slides/web-app-security/xss.xml +++ presentations/slides/web-app-security/xss.xml <slide title="Cross-Site Scripting"> <image filename="t2phage.gif" align="left" marginleft="1em"/> <list marginleft="1em"> <li>Interactive features of your site are used as unwitting carriers of client-side attacks</li> </list> <list marginleft="1em"> <li> Anywhere you display external input:</li> </list> <list marginleft="3em"> <li> User profile </li> <li> Forums</li> <li> Error pages (the URL is external input)</li> </list> </slide>