SlideShare a Scribd company logo
Secure Web App Programming

            Akash Mahajan
   Talk at NULL Bangalore Meeting
            8th August 2009
Cross Site Scripting - XSS
• Injecting HTML/JS into the site.
   – Non-persistent/Reflected/First Order
      • Script is taken from the request and displayed in the browser
        directly
      • example.com/search?q=<script>alert(‘hi’);</script>
      • Example.com/index.php?lang=path to php shell
   – Persistent/Stored/Second Order
      • First name of a registration form is vuln and the value is
        stored in the database
      • Hello <iframe src=http://f1y.in/0.js></iframe>
   – DOM Based
      • No example, mentioned by Amit Klien in his paper XSS of the
        Third Kind
XSS mitigation in PHP
• Sanitize all globals ($_GET, $_POST, $_COOKIE)
   – Use strip_tags()
   – Use inpekt library code.google.com/p/inspekt
• Escape everything before displaying
   – htmlentities(), htmlspeciachars()
• Client headers like user agent can be malicious as well.
• Thumb rule, if its not your data consider it bad. If you
  can verify it, consider it trusted good data.
• White listing helps in verifying good data more than
  black listing.
• See examples at xssed.com
SQL Injection
• Allowing SQL to be injected in the database
  query.
• Most common attack point is the search of any
  dynamic website and registration forms. These
  two will be definitely talking to the database.
• $sql = "SELECT * FROM table WHERE id = '" .
  $_REQUEST['id'] . "'";
• id = ‘ OR 1 UNION ALL SELECT * FROM table;
• Excellent examples
  http://google.com/search?q=site:slideshare.net
  sql injection
SQL Injection - Mitigation
• mysql_real_escape_string()
   – $dbquery = sprintf(“SELECT name FROM user WHERE
     id=‘%s’”, mysql_real_escape_string(‘id’));
• Parameterized queries
   – $res = $query(“SELECT name FROM user WHERE id=?”,
     $id);
   – Standard mysql module in PHP doesn’t allow for
     parameterized queries. You need mysqli
• Stored Procedures
   – See a kickass example of stored proc used to hack more
     than hundred thousand websites
      • http://www.breach.com/resources/breach-security-
        labs/alerts/mass-sql-injection-attack-evolutio
File Uploads
• Web apps add a directory in document root
  for storing file uploads and give write access.
• They don’t randomize filenames. So a specially
  crafted image file which has PHP code written
  in it gets saved there.
• The malicious user is now free to call it using a
  GET request and it gets executed.
• http://www.scanit.be/uploads/php-file-
  upload.pdf
File Uploads - Mitigation
• The usual use case is uploading of image files.
• Use getimageinfo() to get the correct mime
  type of the file from the file header.
• Generate a random file name
  – $rand = time() . substr(md5(microtime()), 0,
    rand(5, 12));
  – Return $rand and append file extension
• Ideally noexec permission should be set on
  the directory where files are copied to.
Endgame
• At this point you have reasonable ensured that
  your PHP web application is not compromised.
• But the user connecting to your website are
  vulnerable to session hijacking, CSRF from your
  site etc.
• There are work around to the standard PHP
  functions like this one for
  mysql_real_escape_strings()
  – http://shiflett.org/blog/2006/jan/addslashes-versus-
    mysql-real-escape-string

More Related Content

PDF
Entity provider selection confusion attacks in JAX-RS applications
PDF
Doing Drupal security right
PDF
Unsafe JAX-RS: Breaking REST API
PDF
Securing REST APIs
PDF
Drupal campleuven: Secure Drupal Development
PPTX
PHP Profiling/performance
PPTX
Intro to Apache Shiro
PDF
Super simple application security with Apache Shiro
Entity provider selection confusion attacks in JAX-RS applications
Doing Drupal security right
Unsafe JAX-RS: Breaking REST API
Securing REST APIs
Drupal campleuven: Secure Drupal Development
PHP Profiling/performance
Intro to Apache Shiro
Super simple application security with Apache Shiro

What's hot (20)

PDF
Drupal and Security: What You Need to Know
PPTX
Owasp Top 10 - A1 Injection
PDF
4 andrii kudiurov - web application security 101
PDF
Is Drupal secure?
PPT
Drupal security
PDF
What should a hacker know about WebDav?
PDF
Hacking Adobe Experience Manager sites
PPT
How To Detect Xss
PPT
season management in php (WT)
PPTX
Rest Essentials
PPTX
An approach to app security - For beginners
PDF
Php workshop L04 database
PDF
Defeating Cross-Site Scripting with Content Security Policy
PDF
Php workshop L03 superglobals
PDF
Alfresco DevCon 2018: Role Based Access Control with Apache Shiro
PDF
Android stepbystep
PPTX
Intro to Pentesting Jenkins
PDF
Making Web Development "Secure By Default"
PPT
Introduction To Ruby Watir (Web Application Testing In Ruby)
PDF
Unleashing Creative Freedom with MODX - 2015-08-26 at PHP Zwolle
Drupal and Security: What You Need to Know
Owasp Top 10 - A1 Injection
4 andrii kudiurov - web application security 101
Is Drupal secure?
Drupal security
What should a hacker know about WebDav?
Hacking Adobe Experience Manager sites
How To Detect Xss
season management in php (WT)
Rest Essentials
An approach to app security - For beginners
Php workshop L04 database
Defeating Cross-Site Scripting with Content Security Policy
Php workshop L03 superglobals
Alfresco DevCon 2018: Role Based Access Control with Apache Shiro
Android stepbystep
Intro to Pentesting Jenkins
Making Web Development "Secure By Default"
Introduction To Ruby Watir (Web Application Testing In Ruby)
Unleashing Creative Freedom with MODX - 2015-08-26 at PHP Zwolle
Ad

Similar to null Bangalore meet - Php Security (20)

PPTX
Open source security
PPTX
OWASP Top 10 vs Drupal - OWASP Benelux 2012
PDF
The top 10 security issues in web applications
PPTX
Secure Programming In Php
PPT
OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
PPTX
Owasp Top 10 A1: Injection
PDF
Rails Security
PDF
2013 OWASP Top 10
PDF
Evolution Of Web Security
PDF
Codeigniter
PPTX
Mr. Mohammed Aldoub - A case study of django web applications that are secur...
PPTX
Sql Injection attacks and prevention
PPTX
Case Study of Django: Web Frameworks that are Secure by Default
PDF
Drupal Security from Drupalcamp Bratislava
PDF
Web Application Security
PDF
Lets Make our Web Applications Secure
PDF
How to Destroy a Database
PDF
Devbeat Conference - Developer First Security
PDF
Web Security 101
PDF
Ruby on Rails Security Guide
Open source security
OWASP Top 10 vs Drupal - OWASP Benelux 2012
The top 10 security issues in web applications
Secure Programming In Php
OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
Owasp Top 10 A1: Injection
Rails Security
2013 OWASP Top 10
Evolution Of Web Security
Codeigniter
Mr. Mohammed Aldoub - A case study of django web applications that are secur...
Sql Injection attacks and prevention
Case Study of Django: Web Frameworks that are Secure by Default
Drupal Security from Drupalcamp Bratislava
Web Application Security
Lets Make our Web Applications Secure
How to Destroy a Database
Devbeat Conference - Developer First Security
Web Security 101
Ruby on Rails Security Guide
Ad

More from n|u - The Open Security Community (20)

PDF
Hardware security testing 101 (Null - Delhi Chapter)
PPTX
SSRF exploit the trust relationship
PDF
PDF
Api security-testing
PDF
Introduction to TLS 1.3
PDF
Gibson 101 -quick_introduction_to_hacking_mainframes_in_2020_null_infosec_gir...
PDF
Talking About SSRF,CRLF
PPTX
Building active directory lab for red teaming
PPTX
Owning a company through their logs
PPTX
Introduction to shodan
PDF
Detecting persistence in windows
PPTX
Frida - Objection Tool Usage
PDF
OSQuery - Monitoring System Process
PDF
DevSecOps Jenkins Pipeline -Security
PDF
Extensible markup language attacks
PPTX
PDF
Hardware security testing 101 (Null - Delhi Chapter)
SSRF exploit the trust relationship
Api security-testing
Introduction to TLS 1.3
Gibson 101 -quick_introduction_to_hacking_mainframes_in_2020_null_infosec_gir...
Talking About SSRF,CRLF
Building active directory lab for red teaming
Owning a company through their logs
Introduction to shodan
Detecting persistence in windows
Frida - Objection Tool Usage
OSQuery - Monitoring System Process
DevSecOps Jenkins Pipeline -Security
Extensible markup language attacks

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
KodekX | Application Modernization Development
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Unlocking AI with Model Context Protocol (MCP)
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Mobile App Security Testing_ A Comprehensive Guide.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Big Data Technologies - Introduction.pptx
Understanding_Digital_Forensics_Presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
KodekX | Application Modernization Development
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Chapter 3 Spatial Domain Image Processing.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Modernizing your data center with Dell and AMD
Reach Out and Touch Someone: Haptics and Empathic Computing

null Bangalore meet - Php Security

  • 1. Secure Web App Programming Akash Mahajan Talk at NULL Bangalore Meeting 8th August 2009
  • 2. Cross Site Scripting - XSS • Injecting HTML/JS into the site. – Non-persistent/Reflected/First Order • Script is taken from the request and displayed in the browser directly • example.com/search?q=<script>alert(‘hi’);</script> • Example.com/index.php?lang=path to php shell – Persistent/Stored/Second Order • First name of a registration form is vuln and the value is stored in the database • Hello <iframe src=http://f1y.in/0.js></iframe> – DOM Based • No example, mentioned by Amit Klien in his paper XSS of the Third Kind
  • 3. XSS mitigation in PHP • Sanitize all globals ($_GET, $_POST, $_COOKIE) – Use strip_tags() – Use inpekt library code.google.com/p/inspekt • Escape everything before displaying – htmlentities(), htmlspeciachars() • Client headers like user agent can be malicious as well. • Thumb rule, if its not your data consider it bad. If you can verify it, consider it trusted good data. • White listing helps in verifying good data more than black listing. • See examples at xssed.com
  • 4. SQL Injection • Allowing SQL to be injected in the database query. • Most common attack point is the search of any dynamic website and registration forms. These two will be definitely talking to the database. • $sql = "SELECT * FROM table WHERE id = '" . $_REQUEST['id'] . "'"; • id = ‘ OR 1 UNION ALL SELECT * FROM table; • Excellent examples http://google.com/search?q=site:slideshare.net sql injection
  • 5. SQL Injection - Mitigation • mysql_real_escape_string() – $dbquery = sprintf(“SELECT name FROM user WHERE id=‘%s’”, mysql_real_escape_string(‘id’)); • Parameterized queries – $res = $query(“SELECT name FROM user WHERE id=?”, $id); – Standard mysql module in PHP doesn’t allow for parameterized queries. You need mysqli • Stored Procedures – See a kickass example of stored proc used to hack more than hundred thousand websites • http://www.breach.com/resources/breach-security- labs/alerts/mass-sql-injection-attack-evolutio
  • 6. File Uploads • Web apps add a directory in document root for storing file uploads and give write access. • They don’t randomize filenames. So a specially crafted image file which has PHP code written in it gets saved there. • The malicious user is now free to call it using a GET request and it gets executed. • http://www.scanit.be/uploads/php-file- upload.pdf
  • 7. File Uploads - Mitigation • The usual use case is uploading of image files. • Use getimageinfo() to get the correct mime type of the file from the file header. • Generate a random file name – $rand = time() . substr(md5(microtime()), 0, rand(5, 12)); – Return $rand and append file extension • Ideally noexec permission should be set on the directory where files are copied to.
  • 8. Endgame • At this point you have reasonable ensured that your PHP web application is not compromised. • But the user connecting to your website are vulnerable to session hijacking, CSRF from your site etc. • There are work around to the standard PHP functions like this one for mysql_real_escape_strings() – http://shiflett.org/blog/2006/jan/addslashes-versus- mysql-real-escape-string