Skip to content

Instantly share code, notes, and snippets.

@zacscott
Created June 9, 2015 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacscott/278247b5a1b5de4b2515 to your computer and use it in GitHub Desktop.
Save zacscott/278247b5a1b5de4b2515 to your computer and use it in GitHub Desktop.
WpVulnDb.php
<?php
/** @file WpVulnDb.php - WordPress utility class for the WPScan API
* Uses the WPVulnDB - https://wpvulndb.com/api
*/
/**
* WPScan vulnerability database/API interface.
*
* @author Zachary Scott <zscott.dev@gmail.com>
*/
class WpVulnDb {
// The API URLs
const LOOKUP_CORE_API_URL = 'https://wpvulndb.com/api/v1/wordpresses/';
const LOOKUP_PLUGINS_API_URL = 'https://wpvulndb.com/api/v1/plugins/';
const LOOKUP_THEMES_API_URL = 'https://wpvulndb.com/api/v1/themes/';
/** Pings the API are returns whenther it is working or not. */
public function ping() {
$resp = $this->check_core( '4.1.1' ); // an older version
return null !== $resp;
}
/**
* Checks the given WordPress core version for vulnerabilities.
*
* @param $version string The WordPress version string.
* Should be in form X.X.X
* @return A list of all known vulnerabilties reported, `null` if failed.
*/
public function check_core( $version ) {
assert( ! empty( $version ) );
// Convert version number to API format
$version = trim( $version );
$version = str_replace( '.', '', $version );
// Make the API call
$api_url = self::LOOKUP_CORE_API_URL . $version;
return $this->api_call( $api_url );
}
/**
* Checks the given plugin for vulnerabilities.
*
* @param $slug string The slug of the plugin, eg 'akismet'.
* @return A list of all known vulnerabilties reported, `false` if failed.
*/
public function check_plugin( $slug ) {
assert( ! empty( $slug ) );
$slug = trim( (string) $slug );
// Make the API call
$api_url = self::LOOKUP_PLUGINS_API_URL . $slug;
return $this->api_call( $api_url );
}
/**
* Checks the given theme for vulnerabilities.
*
* @param $slug string The slug of the theme, eg 'twentyfifteen'.
* @return A list of all known vulnerabilties reported, `false` if failed.
*/
public function check_theme( $slug ) {
assert( ! empty( $slug ) );
$slug = trim( (string) $slug );
// Make the API call
$api_url = self::LOOKUP_THEMES_API_URL . $slug;
return $this->api_call( $api_url );
}
// Makes an API call t the gven URL and parses the result
private function api_call( $url ) {
assert( ! empty( $url ) );
// Do API call
$http_resp = wp_safe_remote_get( $url );
if ( is_wp_error( $http_resp ) ) {
return false;
}
// Check if vulnerabilties reported
$empty_resp = (
isset( $http_resp['response']['code'] ) &&
404 === (int) $http_resp['response']['code']
) || (
isset( $http_resp['headers']['content-type'] ) &&
!preg_match( '/json/', $http_resp['headers']['content-type'] )
);
if ( $empty_resp ) {
// Yay - no vulerabilities
return array();
}
// Parse the response
$json_resp = $http_resp['body'];
$resp = json_decode( $json_resp, true );
if ( empty( $resp ) ) {
return false;
}
return $this->parse_response( $resp );
}
// Parses an API response into something a bit more usable
private function parse_response( $resp ) {
assert( ! empty( $resp ) );
$vuln = array();
// WordPress core lookup
if ( isset( $resp['wordpress']['vulnerabilities'] ) ) {
return $resp['wordpress']['vulnerabilities'];
}
// plugin lookup
if ( isset( $resp['plugin']['vulnerabilities'] ) ) {
return $resp['plugin']['vulnerabilities'];
}
// theme lookup
if ( isset( $resp['theme']['vulnerabilities'] ) ) {
return $resp['theme']['vulnerabilities'];
}
return $resp;
}
}
/**
* Copyright (c) 2014, 2015 <zscott.dev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment