Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/keep-backup-daily/inc/functions.php
+++ b/keep-backup-daily/inc/functions.php
@@ -26,49 +26,19 @@
return (isset($kbd_backup_aliases[$key])?$kbd_backup_aliases[$key]:$key);
}
- if(!function_exists('kbd_encrypt')){
-
-
- function kbd_encrypt($decrypted, $password, $salt=''){
-
-
- // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
-
-
- $key = hash('SHA256', $salt . $password, true);
-
-
- // Build $iv and $iv_base64. We use a block size of 128 bits (AES compliant) and CBC mode. (Note: ECB mode is inadequate as IV is not used.)
-
-
- srand();
-
-
- if(function_exists('mcrypt_create_iv'))
- $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
- else
- $iv = '鶵�^)W�D';
-
-
- if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
-
-
- // Encrypt $decrypted and an MD5 of $decrypted using $key. MD5 is fine to use here because it's just to verify successful decryption.
-
-
- $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
-
-
- // We're done!
-
-
- return $iv_base64 . $encrypted;
-
-
- }
-
-
+ function kbd_encrypt($decrypted, $password, $salt = '') {
+ $key = hash('sha256', $salt . $password, true);
+ $iv_length = openssl_cipher_iv_length('aes-256-cbc');
+ $iv = random_bytes($iv_length);
+
+ $encrypted = openssl_encrypt($decrypted . md5($decrypted), 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
+ $iv_base64 = rtrim(base64_encode($iv), '=');
+
+ $hmac = hash_hmac('sha256', $encrypted, $key, true);
+ return $iv_base64 . ':' . base64_encode($encrypted) . ':' . base64_encode($hmac);
+
}
+
//FOR QUICK DEBUGGING
@@ -325,26 +295,17 @@
wp_localize_jquery_ui_datepicker();
}
-
- if(!function_exists('init_sessions')){
-
-
- function init_sessions(){
-
-
- if (!session_id()){
-
- ob_start();
- @session_start();
-
-
+
+ if (!function_exists('init_sessions')) {
+ function init_sessions() {
+ // Only start session in admin or frontend AJAX requests
+ if (session_status() === PHP_SESSION_NONE && (is_admin() || defined('DOING_AJAX'))) {
+ session_start();
}
-
-
}
+ }
- }
if(!function_exists('load_kbd_settings')){
@@ -486,9 +447,10 @@
if ( is_wp_error( $response ) ) {
- $error_message = $response->get_error_message();
- echo __("Something went wrong",'wpkbd').": $error_message";
+ $error_message = $response->get_error_message();
+ echo __("Something went wrong",'wpkbd').": $error_message";
} else {
+ $body = wp_remote_retrieve_body($response);
//$response['body'];
}
@@ -616,6 +578,9 @@
add_action( 'wp_ajax_update_kbd_bkup_alias', 'update_kbd_bkup_alias' );
function update_kbd_bkup_alias() {
+ if ( ! current_user_can('manage_options') ) {
+ wp_send_json_error('Insufficient permissions.');
+ }
global $wpdb, $kbd_backup_aliases, $kbd_db_prefix; // this is how you get access to the database
if(isset($_POST['key']) && $_POST['key']!='' && isset($_POST['val']) && $_POST['val']!=''){
@@ -718,11 +683,12 @@
if ( !function_exists( 'kbd_unserialize_replace' ) ) {
function kbd_unserialize_replace( $from = '', $to = '', $data = '', $serialised = false ) {
try {
- if ( false !== is_serialized( $data ) ) {
- $unserialized = unserialize( $data );
- $data = kbd_unserialize_replace( $from, $to, $unserialized, true );
- }
- elseif ( is_array( $data ) ) {
+ if ( file_exists($file) && $data = file_get_contents($file) ) {
+ $unserialized = @unserialize($data);
+ if ($unserialized !== false) {
+ $data = kbd_unserialize_replace($from, $to, $unserialized, true);
+ }
+ }elseif ( is_array( $data ) ) {
$_tmp = array( );
foreach ( $data as $key => $value ) {
$_tmp[ $key ] = kbd_unserialize_replace( $from, $to, $value, false );
@@ -871,6 +837,13 @@
$kbd_path = sanitize_kbd_data($_POST['kbd_path']);
$kbd_path= str_replace('\', '/', $kbd_path);
$kbd_path= str_replace('\', '/', $kbd_path);
+
+ $allowed_directory = realpath(WP_CONTENT_DIR . '/uploads');
+ $real_path = realpath($kbd_path);
+
+ if ($real_path === false || strpos($real_path, $allowed_directory) !== 0) {
+ wp_send_json_error('Invalid directory path.');
+ }
kbd_get_dir_list_html($kbd_path, 'sub');
@@ -946,7 +919,7 @@
if(!isset($_POST['kbd_nonce_field']) || !wp_verify_nonce($_POST['kbd_nonce_field'], 'kbd_nonce_action')){
- print __('Sorry, your nonce did not verify.','wpkbd');
+ wp_send_json_error(__('Nonce verification failed','wpkbd'));
exit;
}else{
$kbd_export_dir_selection = (isset($_POST['kbd_export_dir_selection']) ? sanitize_kbd_data($_POST['kbd_export_dir_selection']) : array());
--- a/keep-backup-daily/inc/kbd_cron.php
+++ b/keep-backup-daily/inc/kbd_cron.php
@@ -279,8 +279,8 @@
$stats = $db_size;
echo '<li>';
- echo '<input type="text" value="'.$title.'" />';
- echo '<a title="'.__('Click here to edit this title','wpkbd').'" class="kbd-bkup-title" data-key="'.$name.'">'.$title.'</a>';
+ echo '<input type="text" value="' . esc_attr($title) . '" />';
+ echo '<a title="' . esc_attr(__('Click here to edit this title','wpkbd')) . '" class="kbd-bkup-title" data-key="' . esc_attr($name) . '">' . esc_html($title) . '</a>';
echo '<a style="margin-left:100px; font-size:12px; color:blue;" href="'.$file.'" >'.__('Download','wpkbd').'</a>';
echo '<a style="margin-left:100px; font-size:12px; color:red;" href="'.$file.'&rm">'.__('Delete','wpkbd').'</a>';
echo '<span style="margin-left:100px">'.($b == 1 ? '[LATEST] ' : '').$stats.'</span>';
@@ -502,8 +502,8 @@
$stats = $db_size;
echo '<li>';
- echo '<input type="text" value="'.$title.'" />';
- echo '<a title="'.__('Click here to edit this title','wpkbd').'" class="kbd-bkup-title" data-key="'.$name.'">'.$title.'</a>';
+ echo '<input type="text" value="' . esc_attr($title) . '" />';
+ echo '<a title="' . esc_attr(__('Click here to edit this title','wpkbd')) . '" class="kbd-bkup-title" data-key="' . esc_attr($name) . '">' . esc_html($title) . '</a>';
echo '<a style="margin-left:100px; font-size:12px; color:blue;" href="'.$file.'" >'.__('Download','wpkbd').'</a>';
echo '<a class="kbd_del_backup" style="margin-left:100px; font-size:12px; color:red;" href="'.$file.'&rm">'.__('Delete','wpkbd').'</a>';
echo '<span style="margin-left:100px">'.($b == 1 ? '[LATEST] ' : '').$stats.'</span>';
--- a/keep-backup-daily/index.php
+++ b/keep-backup-daily/index.php
@@ -3,7 +3,7 @@
Plugin Name: Keep Backup Daily
Plugin URI: http://androidbubble.com/blog/website-development/php-frameworks/wordpress/plugins/wordpress-plugin-keep-backup-daily/1046
Description: This plugin will backup the mysql tables and email to a specified email address daily, weekly, monthly or even yearly.
-Version: 2.1.1
+Version: 2.1.3
Author: Fahad Mahmood
Author URI: https://www.androidbubbles.com
Text Domain: wpkbd
@@ -98,7 +98,7 @@
//KBD END WILL REMOVE .DAT FILES
register_deactivation_hook(__FILE__, 'kbd_end' );
- add_action('init', 'init_sessions');
+ add_action('init', 'init_sessions', 1);
add_action( 'admin_menu', 'kbd_menu' );