SlideShare a Scribd company logo
Writing Secure
                      Plugins

                                   Mark Jaquith

                                   @markjaquith
                                  markjaquith.com
                              coveredwebservices.com


Saturday, November 14, 2009
XSS                            privilege




                                           shell execution
                              escalation


   CSRF
   SQL injection
Saturday, November 14, 2009
Plugin
      security is
      hit-or-miss
Saturday, November 14, 2009
Mostly
                miss
Saturday, November 14, 2009
SQL
     Injection
Saturday, November 14, 2009
<?php
    $wpdb->query(
    	 "UPDATE $wpdb->posts
    	 SET post_title = '$newtitle'
    	 WHERE ID = $my_id"
    	 );
    ?>




Saturday, November 14, 2009
<?php
    $newtitle =
    	 	 	 	 	 esc_sql( $newtitle );
    $my_id = absint( $my_id );

    $wpdb->query(
    	 "UPDATE $wpdb->posts
    	 SET post_title = '$newtitle'
    	 WHERE ID = $my_id"
    	 );
    ?>

Saturday, November 14, 2009
$wpdb->update()



Saturday, November 14, 2009
<?php
    $wpdb->update(
    	 $wpdb->posts,
    	 array( 'post_title' => $newtitle ),
    	 array( 'ID' => $my_id )
    	 );
    ?>




Saturday, November 14, 2009
$wpdb->insert()


Saturday, November 14, 2009
<?php
    $wpdb->insert(
    	 $wpdb->posts,
    	 array( 'post_title' => $newtitle )
    	 );
    ?>




Saturday, November 14, 2009
<?php
    $wpdb->update(
    	 $wpdb->posts,
    	 array(
    	 	 'post_title' => $newtitle,
    	 	 'post_content' => $newcontent ),
    	 array(
    	 	 'ID' => $my_id,
    	 	 'post_title' => $old_title )
    	 );
    ?>

Saturday, November 14, 2009
<?php
    $post_title = 'New Title';
    $wheres['ID'] = 123;
    $wheres['post_title'] = 'Old Title';
    $wpdb->update(
    	 $wpdb->posts,
    	 compact( 'post_title' ),
    	 $wheres
    	 );
    ?>

Saturday, November 14, 2009
$wpdb->prepare()



Saturday, November 14, 2009
<?php
    $title = 'Post Title';
    $ID = 123;
    $content = $wpdb->get_var(
    	 $wpdb->prepare(
    	 "SELECT post_content
    	    FROM  $wpdb->posts
    	    WHERE post_title = %s
    	    AND   ID = %d",
    	 $title, $ID )
    	 );
    ?>
Saturday, November 14, 2009
•Uses sprintf() formatting
                    •%s for strings
                    •%d for integers
                    •You should not quote or
                              escape

Saturday, November 14, 2009
Escape
      late
Saturday, November 14, 2009
XSS
Saturday, November 14, 2009
<h1>
    <?php
    	 echo $title;
    ?>
    </h1>




Saturday, November 14, 2009
<?php
    	 $title = '<script> pwnage(); </script>'
    ?>

    <h1>
    <?php
    	 echo $title;
    ?>
    </h1>




Saturday, November 14, 2009
Anything that
     isn’t hardcoded
        is suspect
Saturday, November 14, 2009
Better:
      Everything is suspect

Saturday, November 14, 2009
Saturday, November 14, 2009
esc_html()

Saturday, November 14, 2009
<?php
    	 $title =
    	 	 	 	 '<script> pwnage(); </script>'
    ?>
    <h1>
    <?php
    	 echo esc_html( $title );
    ?>
    </h1>


Saturday, November 14, 2009
<?php
    $title = '" onmouseover="pwnd();';
    ?>
    <a href="#wordcamp" title="
    <?php
    	 echo $title;
    ?>
    ">
    Link Text
    </a>

Saturday, November 14, 2009
esc_attr()



Saturday, November 14, 2009
<?php
    $title = '" onmouseover="pwnd();';
    ?>
    <a href="#wordcamp" title="
    <?php
    	 echo esc_attr( $title );
    ?>
    ">
    Link Text
    </a>

Saturday, November 14, 2009
<?php
     $url = 'javascript:pwnage();';
    ?>
    <a href="
    <?php
    	 echo esc_attr( $url );
    ?>
    ">
                       WRONG
    Link Text
    </a>

Saturday, November 14, 2009
esc_url()

Saturday, November 14, 2009
<?php
     $url = 'javascript:pwnage();';
    ?>
    <a href="
    <?php
    	 echo esc_url( $url );
    ?>
    ">
    Link Text
    </a>

Saturday, November 14, 2009
esc_url_raw(),
                       sister of esc_url()


Saturday, November 14, 2009
esc_ js()

Saturday, November 14, 2009
<script>
         var foo = '<?php echo esc_js( $bar ); ?>';
         </script>




Saturday, November 14, 2009
CSRF
Saturday, November 14, 2009
Authorization
                              vs.

     Intention
Saturday, November 14, 2009
Nonces
              action-, object-,
             user-specific time
            limited secret keys
Saturday, November 14, 2009
Specific to
                    •WordPress user
                    •Action attempted
                    •Object of attempted action
                    •Time window
Saturday, November 14, 2009
wp_nonce_field()



Saturday, November 14, 2009
<form action="process.php"
    method="post">
    <?php
    	 wp_nonce_field('plugin-action_object');
    ?>

    ...
    </form>



Saturday, November 14, 2009
check_admin_referer( )




Saturday, November 14, 2009
<?php
    // before output goes to browser
    check_admin_referer('plugin-
    	 action_object');
    ?>




Saturday, November 14, 2009
Still need to use
     current_user_can()


Saturday, November 14, 2009
AJAX
                CSRF
Saturday, November 14, 2009
• wp_create_nonce(   'your_action' );

                    • &_ajax_nonce=YOUR_NONCE
                    • check_ajax_referer(   'your_action' );




Saturday, November 14, 2009
Privilege
       Escalation
Saturday, November 14, 2009
current_user_can()



Saturday, November 14, 2009
Set your salts!
                http://api.wordpress.org/secret-key/1.1/




Saturday, November 14, 2009
Stupid shit
               I see all
               the time
Saturday, November 14, 2009
exec()

Saturday, November 14, 2009
<form action="<?php echo
           $_SERVER['REQUEST_URI']; ?>">




Saturday, November 14, 2009
<a href="<?php echo $url; ?>"
      title="<?php echo $title; ?>">
      <?php echo $text; ?>
      </a>

      <script>
      var foo = '<?php echo $js; ?>';
      </script>


Saturday, November 14, 2009
<a href="<?php echo esc_url( $url ); ?>"
      title="<?php echo esc_attr( $title ); ?>">
      <?php echo esc_html( $text ); ?>
      </a>

      <script>
      var foo = '<?php echo esc_js( $js ); ?>';
      </script>




Saturday, November 14, 2009
Discussion

Saturday, November 14, 2009

More Related Content

PDF
Mojolicious, real-time web framework
PDF
Mojolicious: what works and what doesn't
ODP
Mojolicious on Steroids
PPTX
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
KEY
Mojolicious - A new hope
PDF
Mojolicious. Веб в коробке!
PDF
Plugin jQuery, Design Patterns
PDF
Mojolicious
Mojolicious, real-time web framework
Mojolicious: what works and what doesn't
Mojolicious on Steroids
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - A new hope
Mojolicious. Веб в коробке!
Plugin jQuery, Design Patterns
Mojolicious

What's hot (20)

PDF
Contributing to WordPress Core - Peter Wilson
KEY
jQuery Plugin Creation
PDF
RESTful web services
KEY
Keeping it small: Getting to know the Slim micro framework
PPTX
Childthemes ottawa-word camp-1919
PDF
Inside Bokete: Web Application with Mojolicious and others
PDF
Make your own wp cli command in 10min
PDF
You Don't Know Query - WordCamp Portland 2011
PDF
Keeping it small - Getting to know the Slim PHP micro framework
PDF
Avinash Kundaliya: Javascript and WordPress
PDF
Developing apps using Perl
ZIP
Mojolicious
PDF
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
PDF
Keeping it Small: Getting to know the Slim Micro Framework
PDF
You Don't Know Query (WordCamp Netherlands 2012)
TXT
Xmpp prebind
PPT
Slim RedBeanPHP and Knockout
PPT
How to learn j query
PDF
Responsive Design with WordPress
PPTX
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Contributing to WordPress Core - Peter Wilson
jQuery Plugin Creation
RESTful web services
Keeping it small: Getting to know the Slim micro framework
Childthemes ottawa-word camp-1919
Inside Bokete: Web Application with Mojolicious and others
Make your own wp cli command in 10min
You Don't Know Query - WordCamp Portland 2011
Keeping it small - Getting to know the Slim PHP micro framework
Avinash Kundaliya: Javascript and WordPress
Developing apps using Perl
Mojolicious
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Keeping it Small: Getting to know the Slim Micro Framework
You Don't Know Query (WordCamp Netherlands 2012)
Xmpp prebind
Slim RedBeanPHP and Knockout
How to learn j query
Responsive Design with WordPress
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Ad

Similar to Writing Secure Plugins — WordCamp New York 2009 (20)

PDF
Writing Your First WordPress Plugin
PDF
Blogluck1
PDF
What I Hate About Wordpress
PDF
Jason Tucker Wordpress 3rd Party Web Services
KEY
CSI: WordPress -- Getting Into the Guts
PDF
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
PDF
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]
PDF
WordPress Security
PDF
What Is Security
PDF
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]
PDF
Wphackergalaxy
PDF
WordPress Security - WordCamp Phoenix
PDF
Drupal security - Configuration and process
PDF
Web Application Security
PDF
The Ultimate IDS Smackdown
PDF
Writing Secure WordPress Code WordCamp NYC 2014
PDF
Web Application Firewalls Detection, Bypassing And Exploitation
PDF
WordPress Development Tools and Best Practices
PDF
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
PPTX
Wordpress Meetup
Writing Your First WordPress Plugin
Blogluck1
What I Hate About Wordpress
Jason Tucker Wordpress 3rd Party Web Services
CSI: WordPress -- Getting Into the Guts
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]
WordPress Security
What Is Security
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]
Wphackergalaxy
WordPress Security - WordCamp Phoenix
Drupal security - Configuration and process
Web Application Security
The Ultimate IDS Smackdown
Writing Secure WordPress Code WordCamp NYC 2014
Web Application Firewalls Detection, Bypassing And Exploitation
WordPress Development Tools and Best Practices
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
Wordpress Meetup
Ad

More from Mark Jaquith (12)

PDF
Cache Money Business
PDF
Scaling WordPress
PDF
Creating and Maintaining WordPress Plugins
PDF
Coding, Scaling, and Deploys... Oh My!
PDF
WordPress Custom Post Types
PDF
BuddyPress and the Future of WordPress Plugins
PDF
"State of the Word" at WordCamp Mid-Atlantic, by Mark Jaquith
PDF
Secure Coding With Wordpress (BarCamp Orlando 2009)
PDF
Wordcamp Charlotte: WordPress Today and Tomorrow
PDF
Secure Coding with WordPress - WordCamp SF 2008
PPT
Amping up your WordPress Blog
PDF
Contributing To WordPress
Cache Money Business
Scaling WordPress
Creating and Maintaining WordPress Plugins
Coding, Scaling, and Deploys... Oh My!
WordPress Custom Post Types
BuddyPress and the Future of WordPress Plugins
"State of the Word" at WordCamp Mid-Atlantic, by Mark Jaquith
Secure Coding With Wordpress (BarCamp Orlando 2009)
Wordcamp Charlotte: WordPress Today and Tomorrow
Secure Coding with WordPress - WordCamp SF 2008
Amping up your WordPress Blog
Contributing To WordPress

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Spectroscopy.pptx food analysis technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
Teaching material agriculture food technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Electronic commerce courselecture one. Pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
Spectroscopy.pptx food analysis technology
MIND Revenue Release Quarter 2 2025 Press Release
Teaching material agriculture food technology
“AI and Expert System Decision Support & Business Intelligence Systems”
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
Spectral efficient network and resource selection model in 5G networks
Electronic commerce courselecture one. Pdf
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx

Writing Secure Plugins — WordCamp New York 2009

  • 1. Writing Secure Plugins Mark Jaquith @markjaquith markjaquith.com coveredwebservices.com Saturday, November 14, 2009
  • 2. XSS privilege shell execution escalation CSRF SQL injection Saturday, November 14, 2009
  • 3. Plugin security is hit-or-miss Saturday, November 14, 2009
  • 4. Mostly miss Saturday, November 14, 2009
  • 5. SQL Injection Saturday, November 14, 2009
  • 6. <?php $wpdb->query( "UPDATE $wpdb->posts SET post_title = '$newtitle' WHERE ID = $my_id" ); ?> Saturday, November 14, 2009
  • 7. <?php $newtitle = esc_sql( $newtitle ); $my_id = absint( $my_id ); $wpdb->query( "UPDATE $wpdb->posts SET post_title = '$newtitle' WHERE ID = $my_id" ); ?> Saturday, November 14, 2009
  • 9. <?php $wpdb->update( $wpdb->posts, array( 'post_title' => $newtitle ), array( 'ID' => $my_id ) ); ?> Saturday, November 14, 2009
  • 11. <?php $wpdb->insert( $wpdb->posts, array( 'post_title' => $newtitle ) ); ?> Saturday, November 14, 2009
  • 12. <?php $wpdb->update( $wpdb->posts, array( 'post_title' => $newtitle, 'post_content' => $newcontent ), array( 'ID' => $my_id, 'post_title' => $old_title ) ); ?> Saturday, November 14, 2009
  • 13. <?php $post_title = 'New Title'; $wheres['ID'] = 123; $wheres['post_title'] = 'Old Title'; $wpdb->update( $wpdb->posts, compact( 'post_title' ), $wheres ); ?> Saturday, November 14, 2009
  • 15. <?php $title = 'Post Title'; $ID = 123; $content = $wpdb->get_var( $wpdb->prepare( "SELECT post_content FROM $wpdb->posts WHERE post_title = %s AND ID = %d", $title, $ID ) ); ?> Saturday, November 14, 2009
  • 16. •Uses sprintf() formatting •%s for strings •%d for integers •You should not quote or escape Saturday, November 14, 2009
  • 17. Escape late Saturday, November 14, 2009
  • 19. <h1> <?php echo $title; ?> </h1> Saturday, November 14, 2009
  • 20. <?php $title = '<script> pwnage(); </script>' ?> <h1> <?php echo $title; ?> </h1> Saturday, November 14, 2009
  • 21. Anything that isn’t hardcoded is suspect Saturday, November 14, 2009
  • 22. Better: Everything is suspect Saturday, November 14, 2009
  • 25. <?php $title = '<script> pwnage(); </script>' ?> <h1> <?php echo esc_html( $title ); ?> </h1> Saturday, November 14, 2009
  • 26. <?php $title = '" onmouseover="pwnd();'; ?> <a href="#wordcamp" title=" <?php echo $title; ?> "> Link Text </a> Saturday, November 14, 2009
  • 28. <?php $title = '" onmouseover="pwnd();'; ?> <a href="#wordcamp" title=" <?php echo esc_attr( $title ); ?> "> Link Text </a> Saturday, November 14, 2009
  • 29. <?php $url = 'javascript:pwnage();'; ?> <a href=" <?php echo esc_attr( $url ); ?> "> WRONG Link Text </a> Saturday, November 14, 2009
  • 31. <?php $url = 'javascript:pwnage();'; ?> <a href=" <?php echo esc_url( $url ); ?> "> Link Text </a> Saturday, November 14, 2009
  • 32. esc_url_raw(), sister of esc_url() Saturday, November 14, 2009
  • 34. <script> var foo = '<?php echo esc_js( $bar ); ?>'; </script> Saturday, November 14, 2009
  • 36. Authorization vs. Intention Saturday, November 14, 2009
  • 37. Nonces action-, object-, user-specific time limited secret keys Saturday, November 14, 2009
  • 38. Specific to •WordPress user •Action attempted •Object of attempted action •Time window Saturday, November 14, 2009
  • 40. <form action="process.php" method="post"> <?php wp_nonce_field('plugin-action_object'); ?> ... </form> Saturday, November 14, 2009
  • 42. <?php // before output goes to browser check_admin_referer('plugin- action_object'); ?> Saturday, November 14, 2009
  • 43. Still need to use current_user_can() Saturday, November 14, 2009
  • 44. AJAX CSRF Saturday, November 14, 2009
  • 45. • wp_create_nonce( 'your_action' ); • &_ajax_nonce=YOUR_NONCE • check_ajax_referer( 'your_action' ); Saturday, November 14, 2009
  • 46. Privilege Escalation Saturday, November 14, 2009
  • 48. Set your salts! http://api.wordpress.org/secret-key/1.1/ Saturday, November 14, 2009
  • 49. Stupid shit I see all the time Saturday, November 14, 2009
  • 51. <form action="<?php echo $_SERVER['REQUEST_URI']; ?>"> Saturday, November 14, 2009
  • 52. <a href="<?php echo $url; ?>" title="<?php echo $title; ?>"> <?php echo $text; ?> </a> <script> var foo = '<?php echo $js; ?>'; </script> Saturday, November 14, 2009
  • 53. <a href="<?php echo esc_url( $url ); ?>" title="<?php echo esc_attr( $title ); ?>"> <?php echo esc_html( $text ); ?> </a> <script> var foo = '<?php echo esc_js( $js ); ?>'; </script> Saturday, November 14, 2009