Web application security


Published on

Web application security

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Web application security

  1. 1. Web application security <ul><li>It's very important topic that's gaining more attention from both the developers who create web applications, and the attackers who try to exploit them </li></ul>
  2. 2. Before move ahead ... <ul><li>PHP gives many tools to protect data with robust encryption, but encryption is just part of the large and often complex security picture. encrypted data can be unlocked with a key, so protecting that key is very important. If your encryption keys are accessible to unauthorized users (because they're stored in a file accessible via your web server or because they're stored in a file accessible by other users in a shared hosting environment, for example), your data is at risk, no matter how secure your chosen encryption algorithm is. </li></ul><ul><li>Sensitive data needs to be protected not only on the server,but also when it's traveling over the network between the server and your users. Data sent over regular HTTP is visible to anyone with access to the network at any point between your server and a user. </li></ul>
  3. 3. Main problems ??? <ul><li>Cross-site scripting (XSS) </li></ul><ul><li>SQL injection vulnerabilities </li></ul><ul><li>Cross-site request forgery (CSRF) </li></ul><ul><li>Other common software settings </li></ul>
  4. 4. Common software settings <ul><li>Disable Remote URLs for File Handling Functions(allow_url_fopen = Off) </li></ul><ul><li>Disable Register Global </li></ul><ul><li>Restricting What PHP can Read & Write(open_basedir = /var/www/html/shiksha) </li></ul><ul><li>Posing Limit(Limiting on PHP's execution time, memory usage,POST and upload data) </li></ul><ul><li>Disable Error Message and enable Logging(display_errors = Off </li></ul><ul><li>log_errors = On) </li></ul>
  5. 5. Common software settings <ul><li>Hiding The Presence Of PHP(expose_php = Off) </li></ul><ul><li>Limit Certain File Name Pattern accessible by public user(like .inc file) </li></ul><ul><li>Upload file content MIME type check </li></ul><ul><li><?php </li></ul><ul><li>// verify the file is a PDF or not </li></ul><ul><li>$mime = &quot;application/pdf; charset=binary&quot;; </li></ul><ul><li>exec(&quot;file -bi &quot; . $_FILES[&quot;myFile&quot;][&quot;tmp_name&quot;], $out); </li></ul><ul><li>if ($out[0] != $mime) { </li></ul><ul><li>// file is not a PDF !!!! </li></ul>
  6. 6. Common software settings <ul><li>Access to ClamAV,(command line to verify uploaded image) </li></ul><ul><li><?php </li></ul><ul><li>exec(&quot;clamscan --stdout &quot; . $_FILES[&quot;myFile&quot;][&quot;tmp_name&quot;], $out, $return); </li></ul><ul><li>if ($return) { // file is infected </li></ul><ul><li>Specify an appropriate value for the upload_max_size, post_max_size, and max_file_uploads directives in php.ini. The upload_max_size directive specifies the maximum size a file upload can be. In addition to the size of the upload, we can limit the size of the entire POST request with the post_max_size directive. max_file_uploads is a newer directive (added in version 5.2.12) which limits the number of file uploads. </li></ul>
  7. 7. Common software settings <ul><li>Hide directory indexing and .svn folders </li></ul>
  8. 8. Forms based Website Authentication <ul><li>how to log in </li></ul><ul><li>how to remain logged in </li></ul><ul><li>how to store passwords </li></ul><ul><li>using secret questions </li></ul><ul><li>forgotten password functionality </li></ul><ul><li>Open ID </li></ul><ul><li>&quot;Remember me&quot; checkbox </li></ul><ul><li>Browser autocompletion of usernames and passwords </li></ul><ul><li>secret urls (public urls protected by digest) </li></ul><ul><li>checking password strength </li></ul><ul><li>email validation </li></ul>
  9. 9. But ... <ul><li>Hashing the password is effective against password disclosure, but not against replay attacks, Man-In-The-Middle attacks / hijackings, or brute-force attacks (since we are handing the attacker both username, salt and hashed password). </li></ul><ul><li>After sending the authentication tokens, the system needs a way to remember that you have been authenticated - this fact should only ever be stored serverside in the session data. A cookie can be used to reference the session data. Wherever possible, the cookie should have the secure and HTTP Only flags set when sent to the browser. The httponly flag provides some protection against the cookie being read by a XSS attack. The secure flag ensures that the cookie is only sent back via HTTPS, and therefore protects against network sniffing attacks. The value of the cookie should not be predictable. Where a cookie referencing a non-existent session is presented, its value should be replaced immediately to prevent session fixation. </li></ul>
  10. 10. And ... <ul><li>CAPTCHA </li></ul><ul><li>Captchas are annoying and they can be broken (except recaptcha !).All of them are ineffective against cheap third-world labor (according to OWASP, the current sweatshop rate is $1 per 1000 tests), and some implementations are technically illegal in some countries.If you must use a CAPTCHA, use reCAPTCHA, since it is OCR-hard by definition (since it uses already OCR-misclassified book scans). </li></ul>
  11. 11. Form Spoofing ... <ul><li><?php </li></ul><ul><li>$memcahe_token = md5(uniqid(mt_rand(), true)); </li></ul><ul><li>?> </li></ul><ul><li><form action=&quot;buy.php&quot; method=&quot;POST&quot;><input type=&quot;hidden&quot; name=&quot;token&quot; value=&quot;<?php echo $memcahe_token; ?>&quot; /></form> </li></ul><ul><li>if ($_POST['token'] != $memcahe_token || </li></ul><ul><li>!isset($memcahe_token) { </li></ul><ul><li>/* Prompt user for password. */ </li></ul><ul><li>} else { </li></ul><ul><li>/* Continue. */ </li></ul><ul><li>} </li></ul>
  12. 12. Avoiding Cross-Site Scripting <ul><li>/* Note the character encoding. */ </li></ul><ul><li>header('Content-Type: text/html; charset=UTF-8'); </li></ul><ul><li>/* Initialize an array for escaped data. */ </li></ul><ul><li>$html = array(); </li></ul><ul><li>/* Escape the filtered data. */ </li></ul><ul><li>$html['username'] = htmlentities($clean['username'], ENT_QUOTES, 'UTF-8'); </li></ul><ul><li>echo &quot;<p>Welcome back, {$html['username']}.</p>&quot;; </li></ul>
  13. 13. Keeping Passwords Out of Your Site Files <ul><li>Because phpinfo( ) displays all of the environment variables, it exposes any passwords you store there. Also, make sure not to expose the contents of $_SERVER in other ways, such as with the print_r( ) function </li></ul><ul><li>separate file from the main configuration file </li></ul><ul><li>SetEnv DB_USER &quot;raviraj@rocking&quot; </li></ul><ul><li>SetEnv DB_PASSWORD &quot; y23a!t@ce8 &quot; </li></ul><ul><li>Inside the <VirtualHost> directive for the site in the main configuration file (httpd.conf), include this separate file as follows: </li></ul><ul><li>Include &quot;/usr/local/apache/database-passwords&quot; </li></ul><ul><li>Make sure that this separate file containing the password (e.g., /usr/local/apache/database-passwords) is not readable by any user other than the one that controls the appropriate virtual host. </li></ul>
  14. 14. Verifying Data with Hashes <ul><li>/* Define a salt. */ </li></ul><ul><li>define('SALT', 'flyingturtle'); </li></ul><ul><li>$id = 1337; </li></ul><ul><li>$idcheck = md5(SALT . $id); </li></ul><ul><li><input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;<?php echo $id; ?>&quot; /> </li></ul><ul><li><input type=&quot;hidden&quot; name=&quot;idcheck&quot; value=&quot;<?php echo $idcheck; ?>&quot; /> </li></ul><ul><li>/* Initialize an array for filtered data. */ </li></ul><ul><li>$clean = array(); </li></ul><ul><li>/* Define a salt. */ </li></ul><ul><li>define('SALT', 'flyingturtle'); </li></ul><ul><li>if (md5(SALT . $_POST['id']) == $_POST['idcheck']) { </li></ul><ul><li>$clean['id'] = $_POST['id']; </li></ul><ul><li>} else { </li></ul><ul><li>/* Error */ </li></ul><ul><li>} </li></ul>
  15. 15. SSL ... <ul><li>if ('on' == $_SERVER['HTTPS']) { </li></ul><ul><li>// </li></ul><ul><li>} else { </li></ul><ul><li>} </li></ul><ul><li>/* Set an SSL-only cookie named &quot;sslonly&quot; with value &quot;yes&quot; that expires at the end of the current browser session. */ </li></ul><ul><li>setcookie('sslonly', 'yes', '', '/', 'example.org', true); </li></ul>
  16. 16. Two way Encrypting and Decrypting Data <ul><li>$algorithm = MCRYPT_BLOWFISH; </li></ul><ul><li>$key = 'That golden key that opens the palace of eternity.'; </li></ul><ul><li>$data = 'The chicken escapes at dawn. Send help with Mr. Blue.'; </li></ul><ul><li>$mode = MCRYPT_MODE_CBC; </li></ul><ul><li>$iv = mcrypt_create_iv(mcrypt_get_iv_size($algorithm, $mode), </li></ul><ul><li>MCRYPT_DEV_URANDOM); </li></ul><ul><li>$encrypted_data = mcrypt_encrypt($algorithm, $key, $data, $mode, $iv); </li></ul><ul><li>$plain_text = base64_encode($encrypted_data); </li></ul><ul><li>echo $plain_text . &quot;n&quot;; </li></ul><ul><li>$encrypted_data = base64_decode($plain_text); </li></ul><ul><li>$decoded = mcrypt_decrypt($algorithm, $key, $encrypted_data, $mode, $iv); </li></ul><ul><li>echo $decoded . &quot;n&quot;; </li></ul>
  17. 17. Two way Encrypting and Decrypting Data ... <ul><li>The mcrypt extension is an interface with mcrypt, a library that implements many different encryption algorithms. The data is encrypted and decrypted by mcrypt_encrypt( ) and mcrypt_decrypt( ), respectively. They each take five arguments. The first is the algorithm to use. To find which algorithms mcrypt supports on your system, call mcrypt_list_algorithms().The second argument is the encryption key; the third argument is the data to encrypt or decrypt. The fourth argument is the mode for the encryption or decryption (a list of supported modes is returned by mcrypt_list_modes()). The fifth argument is an initialization vector (IV), used by some modes as part of the encryption or decryption process. </li></ul>
  18. 18. Think again !!! <ul><li>Security is stunningly thought-provoking question while writting code, how much and which level security you need </li></ul><ul><li>Be aware network level attack (targetting email to individuals in your organisation, containing either malware attachements or links to sites which install malware) </li></ul><ul><li>Keyboard key logger </li></ul><ul><li>DO Your Best …. :-) </li></ul><ul><ul><ul><li>Use multi-factor authentication( with some kind of pseudo-random number gadget) </li></ul></ul></ul><ul><ul><ul><li>Sanitise all your input </li></ul></ul></ul><ul><ul><ul><li>Use SSL </li></ul></ul></ul>
  19. 19. Thanks <ul><li>Ask Anything ... </li></ul><ul><li>[email_address] </li></ul><ul><li>Reading List </li></ul><ul><li>http://www.owasp.org/index.php/Guide_to_Authentication </li></ul><ul><li>http://www.cs.umass.edu/~kevinfu/papers/webauth_tr.pdf </li></ul><ul><li>http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice/ </li></ul><ul><li>http://cups.cs.cmu.edu/soups/2008/proceedings/p13Rabkin.pdf </li></ul><ul><li>http://pdos.csail.mit.edu/papers/webauth%3asec10.pdf </li></ul><ul><li>http://news.ycombinator.com/item?id=55660 </li></ul><ul><li>http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html </li></ul><ul><li>http://en.wikipedia.org/wiki/Password_cracking </li></ul>