Popular Vulnerable Code

Pictures – SQL Injection

Details

Affected Software: Short URL Plugin

Fixed in Version: 2.0

Issue Type:SQL Injection

Original Code: Found Here

Description

This was a vulnerability that affected the Short URL WordPress plugin.  The vulnerability is very straightforward and should have been easily detected by a security code reviewer.  The vulnerable code section takes attacker controlled data directly from $_POST[‘form_url’],$_POST[‘form_desc’],and $_POST[‘id’] and uses the tainted value immediately in dynamically built SQL statements.  One interesting piece of this particular code fix is that the developers chose to implement the code fixes near the assignment of the variable (as opposed to near consumption,in the SQL statement).

Another interesting piece of the code fix is the logic for the following conditional:

if($action == “delete”){

looks like the devs may have forgotten something :)

Developers Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
   dbDelta($sql);
  
   }
   if(isset($_POST['action'])) {
      $action = $_POST['action'];

if($action == "create"){
-  $add_url = $_POST['form_url'];
-  $add_desc = $_POST['form_desc'];
+  $add_url = $wpdb->escape($_POST['form_url']);
+  $add_desc = $wpdb->escape($_POST['form_desc']);
   if($add_url == "http://"|| (!$add_url)){ $ERR = $ERR . "<br>You must enter a URL to redirect to!";}
   if(!$ERR){
      $wpdb->query("INSERT INTO $table_name (link_url,link_desc) VALUES ('$add_url','$add_desc')");
         $new_url = get_option("siteurl") . "/u/". mysql_insert_id();
         $MES = $MES . "<br>The redirect URL has been added. Your new Short URL is:". $new_url;
         }
      }

if($action == "edit"){
-  $edit_id = $_POST['id'];
-  $edit_url = $_POST['form_url'];
-  $edit_desc = $_POST['form_desc'];
+  $edit_id = $wpdb->escape($_POST['id']);
+  $edit_url = $wpdb->escape($_POST['form_url']);
+  $edit_desc = $wpdb->escape($_POST['form_desc']);
   if($edit_url == "http://"|| (!$edit_url)){ $ERR = $ERR . "<br>You must enter a URL to redirect to!";}
   if(!$ERR){
      $wpdb->query("UPDATE $table_name SET link_url='$edit_url',link_desc='$edit_desc' WHERE link_id = $edit_id");
         $MES = $MES . "<br>The redirect URL has been modified.";
         }
      }

  
if($action == "delete"){
   $delete_id = $_POST['id'];
   $wpdb->query("DELETE FROM $table_name WHERE link_id = '$delete_id'");
   $MES = $MES . "<br>Redirect deleted!";
   }
  
if($action == "clearall"){
        $wpdb->query("UPDATE $table_name SET link_count='0' WHERE link_count >0");
   $MES = $MES . "<br>Counts have been reset!";
   }
}
   ?>
   <div>
   <form method="post">
      <h2>Short URL Admin</h2>
<?php if($ERR){ echo "<p>". $ERR . "</p>";}
if($MES){ echo "<p>". $MES . "</p>";} ?>
      <p>Short URL allows you to create shorter URL's and keeps track of how many
times a link has been clicked. It's useful for managing downloads,keeping track
of outbound links and for masking URL's. Clicking the Clear All Clicks button
will reset the count for each entry. Visit the <a href="<a href="http://www.harleyquine.com/php-scripts/short-url-plugin/%22%3Eplugin">http://www.harleyquine.com/php-scripts/short-url-plugin/">plugin</a>page</a>for more information about this plugin.</p>

<h2>Current Redirects</h2>
<table>
   <thead>
   <tr>
   <th scope="col">Short URL (The URL to use)</th>
   <th scope="col">Real URL (Where it redirects to)</th>
   <th scope="col">Notes</th>
   <th scope="col">Amount of Clicks</th>
   <th scope="col">Manage</th>
   </tr>
      </thead>
   <tbody id="the-list">
?>