CVE-2023-6981: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: The WP SMS – Messaging & SMS Notification for WordPress, WooCommerce, GravityForms, etc plugin for WordPress is vulnerable to SQL Injection via the 'group_id' parameter in all versions up to, and including, 6.5 due to insufficient escaping on the user supplied parameter and lack of sufficient preparation on the existing SQL query.  This makes it possible for authenticated attackers, with contributor-level access and above, to append additional SQL queries into already existing queries that can be used to extract sensitive information from the database. This can leveraged to achieve Reflected Cross-site Scripting.
CWE description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
CVSS v3 base score: 4.9
Published: 2024-01-03T06:15Z
Repository: https://github.com/wp-sms/wp-sms
Language: PHP
Files changed: includes/admin/subscribers/class-wpsms-subscribers-table.php
Commit date: 2023-12-23 16:23:12 +0400
Commit message: Fix XSS vulnerability in subscribers page

Diff: diff --git a/includes/admin/subscribers/class-wpsms-subscribers-table.php b/includes/admin/subscribers/class-wpsms-subscribers-table.php
index fd26f9d8..71f2ae90 100644
--- a/includes/admin/subscribers/class-wpsms-subscribers-table.php
+++ b/includes/admin/subscribers/class-wpsms-subscribers-table.php
@@ -166,16 +166,17 @@ class Subscribers_List_Table extends \WP_List_Table
     }
 
     public function process_bulk_action()
     {
         $current_action = $this->current_action();
-        //Detect when a bulk action is being triggered...
+        // Detect when a bulk action is being triggered
+
         // Search action
         if (isset($_GET['s']) and $_GET['s']) {
             $metaValue   = Helper::prepareMobileNumberQuery($this->db->esc_like($_GET['s']));
             $metaValue   = "'" . implode("','", $metaValue) . "'";
-            $prepare     = $this->db->prepare("SELECT * from `{$this->tb_prefix}sms_subscribes` WHERE name LIKE %s OR mobile IN ({$metaValue})", '%' . $this->db->esc_like($_GET['s']) . '%');
+            $prepare     = $this->db->prepare("SELECT * FROM `{$this->tb_prefix}sms_subscribes` WHERE name LIKE %s OR mobile IN ({$metaValue})", '%' . $this->db->esc_like($_GET['s']) . '%');
             $this->data  = $this->get_data($prepare);
             $this->count = $this->get_total($prepare);
         }
 
         // Bulk delete action
@@ -310,47 +311,54 @@ class Subscribers_List_Table extends \WP_List_Table
         $result  = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
 
         return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
     }
 
-    //set $per_page item as int number
+    // Set $per_page item as int number
     public function get_data($query = '')
     {
         $page_number = ($this->get_pagenum() - 1) * $this->limit;
         $orderby     = "ORDER BY {$this->tb_prefix}sms_subscribes.date DESC";
         $where       = "";
+        $params      = array();
 
         if (isset($_REQUEST['orderby'])) {
             $orderby = "ORDER BY {$this->tb_prefix}sms_subscribes.{$_REQUEST['orderby']} {$_REQUEST['order']}";
         }
 
         if (!$query) {
             if (isset($_GET['group_id']) && $_GET['group_id']) {
                 $group_id = sanitize_text_field($_GET['group_id']);
-                $where    = "WHERE group_ID = {$group_id}";
+                $where    = "WHERE group_ID = %d";
+                $params[] = intval($group_id);
             }
 
             if (isset($_GET['country_code']) && $_GET['country_code']) {
                 $country_code = sanitize_text_field($_GET['country_code']);
 
                 if ($where) {
-                    $where .= " AND mobile LIKE '{$country_code}%'";
+                    $where .= " AND mobile LIKE %s";
                 } else {
-                    $where = "WHERE mobile LIKE '{$country_code}%'";
+                    $where = "WHERE mobile LIKE %s";
                 }
+
+                $params[] = $country_code . '%';
             }
-            $query = $this->db->prepare("SELECT * FROM {$this->tb_prefix}sms_subscribes {$where} {$orderby} LIMIT %d OFFSET %d", $this->limit, $page_number);
+
+            $params[] = $this->limit;
+            $params[] = $page_number;
+
+            $query = $this->db->prepare("SELECT * FROM {$this->tb_prefix}sms_subscribes {$where} {$orderby} LIMIT %d OFFSET %d", $params);
         } else {
             $query .= $this->db->prepare(" LIMIT %d OFFSET %d", $this->limit, $page_number);
         }
 
         $result = $this->db->get_results($query, ARRAY_A);
-
         return $result;
     }
 
-    //get total items on different Queries
+    // Get total items on different Queries
     public function get_total($query = '')
     {
         if (!$query) {
             $query = 'SELECT * FROM `' . $this->tb_prefix . 'sms_subscribes`';
         }

Vulnerable code:         //Detect when a bulk action is being triggered...
            $prepare     = $this->db->prepare("SELECT * from `{$this->tb_prefix}sms_subscribes` WHERE name LIKE %s OR mobile IN ({$metaValue})", '%' . $this->db->esc_like($_GET['s']) . '%');
    //set $per_page item as int number
                $where    = "WHERE group_ID = {$group_id}";
                    $where .= " AND mobile LIKE '{$country_code}%'";
                    $where = "WHERE mobile LIKE '{$country_code}%'";
            $query = $this->db->prepare("SELECT * FROM {$this->tb_prefix}sms_subscribes {$where} {$orderby} LIMIT %d OFFSET %d", $this->limit, $page_number);

    //get total items on different Queries
Fixed code:         // Detect when a bulk action is being triggered

            $prepare     = $this->db->prepare("SELECT * FROM `{$this->tb_prefix}sms_subscribes` WHERE name LIKE %s OR mobile IN ({$metaValue})", '%' . $this->db->esc_like($_GET['s']) . '%');
    // Set $per_page item as int number
        $params      = array();
                $where    = "WHERE group_ID = %d";
                $params[] = intval($group_id);
                    $where .= " AND mobile LIKE %s";
                    $where = "WHERE mobile LIKE %s";

                $params[] = $country_code . '%';

            $params[] = $this->limit;
            $params[] = $page_number;

            $query = $this->db->prepare("SELECT * FROM {$this->tb_prefix}sms_subscribes {$where} {$orderby} LIMIT %d OFFSET %d", $params);
    // Get total items on different Queries