Automated capability analysis in Wordpress plugins Using static and - - PowerPoint PPT Presentation

automated capability analysis in wordpress plugins
SMART_READER_LITE
LIVE PREVIEW

Automated capability analysis in Wordpress plugins Using static and - - PowerPoint PPT Presentation

Automated capability analysis in Wordpress plugins Using static and dynamic analysis SNE RP2 Frank Uijtewaal Collab: DongIT (dongit.nl) Initial project goal Find a new and interesting type of security analysis for web applications


slide-1
SLIDE 1

Automated capability analysis in Wordpress plugins

Using static and dynamic analysis

SNE RP2 Frank Uijtewaal Collab: DongIT (dongit.nl)

slide-2
SLIDE 2

4 July 2016 2

Initial project goal

  • Find a new and interesting type of security analysis for

web applications

  • Static code analysis
  • WordPress
  • Plugins
  • A7 Missing Function Level Access Control
slide-3
SLIDE 3

4 July 2016 3

!

slide-4
SLIDE 4

4 July 2016 4

!

slide-5
SLIDE 5

4 July 2016 5

!

slide-6
SLIDE 6

4 July 2016 6

!

slide-7
SLIDE 7

4 July 2016 7

Static Code Analysis Machine Learning

slide-8
SLIDE 8

4 July 2016 8

Static Code Analysis Machine Learning

slide-9
SLIDE 9

4 July 2016 9

Static Code Analysis Machine Learning

slide-10
SLIDE 10

4 July 2016 10

Static Code Analysis part

Not so easy Turns out No parsing & analysis tools available

slide-11
SLIDE 11

4 July 2016 11

Updated goal

  • Static analyis
  • Static & Dynamic analysis

Future work here

slide-12
SLIDE 12

4 July 2016 12

WordPress

  • Capability checking

– Functions current_user_can, user_can

if (!current_user_can('manage_options')) { wp_die(__("you don't have the clearance")); }

  • User roles

– Administrator, editor, author, etc.

  • URL handlers: events and callbacks

– Functions add_action, add_filter

( string $tag, callable $function_to_add, ....)

slide-13
SLIDE 13

4 July 2016 13

Static analysis summary

  • Simple, conformative code quite doable
  • Lots of variation means stronger tools required
  • Modeling and analysis tools a must to do this

research

  • No such tools freely available
slide-14
SLIDE 14

4 July 2016 14

Static analysis example

  • Extract required capabilities
  • Done!

– Req capability == 'manage_options'

  • Easy

if (!current_user_can('manage_options')) { wp_die(__("you don't have the clearance")); }

slide-15
SLIDE 15

4 July 2016 15

Not always straightforward

if ( is_wp_error( $error = $this->validate_call( $blog_id, $this->needed_capabilities ) ) ) { return $error; } $must_pass = ( isset( $capability['must_pass'] ) && is_int( $capability['must_pass'] ) ? $capability['must_pass'] : count( $capabilities ) ); $failed = array(); // store the failed capabilities $passed = 0; // foreach ( $capabilities as $cap ) { if ( current_user_can( $cap ) ) { $passed ++; } else { $failed[] = $cap; } }

slide-16
SLIDE 16

4 July 2016 16

Static ... += dynamic

  • No fancy tools
  • Given current options, static = no go
  • No time to create all tools necessary
  • How far do we get without those “tools”, then?
  • But... PHP interpreter?
slide-17
SLIDE 17

4 July 2016 17

Static & dynamic, principles

  • Gather as much data as possible statically
  • Run URL handlers:

– Directly: my_url_handler($arg1, $arg2, $arg3); – Not through webserver as is normally the case – Use gathered data to make a nice baby room

  • Record as much data from run

– Use data to repeat and finetune environment – Use data to analyse code paths & capabilities

slide-18
SLIDE 18

4 July 2016 18

Static & Dynamic approach

Find entrypoints Analyse parameters Run unittest with Various capabilities Analyse required globals Populate and run Unittest template Analyse outputs

slide-19
SLIDE 19

4 July 2016 19

Static & Dynamic approach

Find entrypoints Analyse parameters Run unittest with Various capabilities Analyse required globals Populate and run Unittest template Analyse parameters Analyse outputs

slide-20
SLIDE 20

4 July 2016 20

Analyse parameters

  • Known hooks
  • Custom hooks
  • Trace parameters to known functions
  • Constraint analysis

public function a_func($post_id) {}

  • add_ping ( int $post_id, string $uri )
  • add_post_meta ( int $post_id, string $meta_key, mixed $meta_value,

bool $unique = false )

  • check_and_publish_future_post ( int|WP_Post $post_id )
slide-21
SLIDE 21

4 July 2016 21

Static & Dynamic approach

Find entrypoints Analyse parameters Run unittest with Various capabilities Analyse required globals Populate and run Unittest template Analyse outputs

slide-22
SLIDE 22

4 July 2016 22

Static & Dynamic approach

Find entrypoints Analyse parameters Run unittest with Various capabilities Analyse required globals Populate and run Unittest template Analyse required globals Analyse outputs

slide-23
SLIDE 23

4 July 2016 23

Static & Dynamic approach

Find entrypoints Analyse parameters Analyse output Analyse required globals Populate and run Unittest template Analyse outputs

slide-24
SLIDE 24

4 July 2016 24

Static & Dynamic approach

Find entrypoints Analyse parameters Analyse output Analyse required globals Populate and run Unittest template Populate and run Unittest template Analyse outputs

slide-25
SLIDE 25

4 July 2016 25

Unittest template

  • Run callback:

– WordPress has support for PHPunit – So, unittests

  • WP_UnitTestCase

– Sets up environment – Creates temporary tables – Offers nice functionality

  • Xdebug for call traces and code coverage
slide-26
SLIDE 26

4 July 2016 26

class TemplateTest extends WP_Ajax_UnitTestCase { private $roles = array(); public function setUp() { parent::setUp(); $this->role_names = array('subscriber', 'contributor', 'author', 'editor', 'administrator'); } public function test_do_handling() { foreach ($this->role_names as $role_name) { // 1 {Setup per role} $this->_setRole($role_name); $filename = __DIR__ . "/traces/trace-{$role_name}"; xdebug_start_code_coverage(XDEBUG_CC_UNUSED); xdebug_start_trace($filename); // 2 {Call to URL handler} xdebug_stop_trace(); $coverage = xdebug_get_code_coverage(); xdebug_stop_code_coverage(); file_put_contents($filename . '.cov', coverage_to_json($coverage)); // 3 {Teardown for per-role setup} } } }

slide-27
SLIDE 27

4 July 2016 27

Static & Dynamic approach

Find entrypoints Analyse parameters Analyse outputs Analyse required globals Populate and run Unittest template

slide-28
SLIDE 28

4 July 2016 28

Static & Dynamic approach

Find entrypoints Analyse parameters Analyse outputs Analyse required globals Populate and run Unittest template Analyse outputs

slide-29
SLIDE 29

4 July 2016 29

Output 1: Traces

Record type 1 6 10 11 12 - ... Entry level function name line number

  • no. of parameters

parameters Exit level empty Return level return value empty

  • Headers
  • Example output

19 wpdb->_real_escape 1209 1 $string = 'setting_b' 20 mysqli_real_escape_string 1127 2 class mysqli { ... }, 'setting_b' 1 20 R 20 'setting_b' 1 19 R 19 'setting_b'

slide-30
SLIDE 30

4 July 2016 30

13 - E: MyPlugin->plugin_settings_page ([]) 14 - E: current_user_can (["$capability = 'manage_options'"]) 14 - R: FALSE 14 - E: __ (["$text = 'you don\\'t have the clearance'", '$domain = ???']) 15 - E: translate (["$text = 'you don\\'t have the clearance'", "$domain = 'default'"]) 16 - E: apply_filters (["$tag = 'gettext'", "$value = 'you don\\'t have the clearance'", "'you don\\'t have the clearance'", "'default'"]) 16 - R: 'you don\'t have the clearance' 15 - R: 'you don\'t have the clearance' 14 - R: 'you don\'t have the clearance' 14 - E: wp_die (["$message = 'you don\\'t have the clearance'", '$title = ???', '$args = ???'])

function plugin_settings_page() { if (!current_user_can('manage_options')) { wp_die(__("you don't have the clearance")); } include(sprintf("%s/settings.php", dirname(__FILE__))); }

slide-31
SLIDE 31

4 July 2016 31

Output 2: Code coverage

Subscriber coverage admin coverage Admin noncoverage 20 20 54 21 21 22 22 53 53 54 57 58

slide-32
SLIDE 32

4 July 2016 32

Did the run/test succeed?

  • Meaning:

– Did we predict the correct arguments and globals?

  • Metrics:

– Full Code coverage? – Joined set should hit all return paths?

  • If deemed not:

– Rerun with different parameters/globals – Test's output may help here

slide-33
SLIDE 33

4 July 2016 33

Static & Dynamic approach

Find entrypoints Analyse parameters Analyse outputs Analyse required globals Populate and run Unittest template

slide-34
SLIDE 34

4 July 2016 34

Conclusions

  • Lack of good parsing & analysis tools inhibits

research.

– Exploring code, extracting features difficult – Testing hypotheses not possible on large scale

  • If they were available:

– Static & dynamic analysis may be quite fruitful here

slide-35
SLIDE 35

4 July 2016 35

Future work

  • Design & build a good working modeling tool

– Modular and extendible – Allows for queries to be done

  • What (symbolic) value does this variable have here?
  • What should x be to evaluate this to true? (constraint solving)
  • What is this variable used for?

– Needs to build a very good representation of the code

  • Next steps are Modeling & Predictions

– WordPress knowledge needed for classification