SQL Injection Attack against escaping single quotes - sql

I have been told that the method of escaping single quotes is easy to bypass in a sql injection attack. For example, if I were to have the line:
username='admin' and password='$password'
where the user types in "$password", and any single quote they type gets replaced by a double quote, could you give me an example command that would break this? I know the backslash character ( \ ) is used to escape a character, but I'm not sure how it would work out still.

Related

Single quote ( ' ) vs double quote ( " ) in SQL or SQL in Google Cloud

When I write SQL queries, I sometimes get error messages and am not sure if it's because I am not using a backtick, or single quote, or double-quotation marks. I just keep trying different quotation marks until it works. Sometimes it doesn't require any.
When are you supposed to use quotes and how do you know which ones to use?

AS400 - Token "!" not valid

I'm running a SQL query using RUNSQL into a CL program. This query is a basic SELECT statement and uses the exclamation mark to concatenate strings.
For years until yesterday, it worked fine. Now, out of nowhere, I've got a SQL0104 message displaying Token '!' not valid every time I run the program.
If I run the query manually using STRSQL, it works.
Did this occur to someone ?
Best regards.
DB2's operator for string concatenation is actually the double pipe ||.
The documentation says:
Use the concatenation operator (||) to join two values of an expression into a single string. In some non-English, single-byte character sets, the || can display as !! (exclamation marks) or other special characters.
So your issue may be caused by a change in the character set of your client. Just use the standard operator, and your code will work regardless.

Querying an apostrophe in R

In SQL, you are supposed to query a name such as "St. John's" as 'St. Johns'''s, where you replace the single quotes with two single quotes, or you can use double quotes and not do the double single quote. However, no matter what I try in R, it still tells me "unexpected string literal". Any help would be appreciated.code and error
Remove the quote characters around your database, schema and table name.

Perl: escape sql strings without having a db connection

I know the right way to sanitize sql strings in Perl is to use a prepared statement, however, this particular Perl script is generating statements to be executed later in a different environment, not Perl. It has no database to connect to.
How can I safely escape a string for insertion into a MySQL query. The solution doesn't have to be portable.
Unfortunately the quoting function used by DBD::mysql, and the MySQL client library in general, requires an active database handle. According to the documentation, "this is needed because the escaping depends on the character set in use by the server".
I can think of a few hacky solutions, but none of them are really satisfying, so let's work with this from the docs:
Characters encoded are “\”, “'”, “"”, NUL (ASCII 0), “\n”, “\r”, and Control+Z. Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped.
This suggests that you can probably get away with a quoting function that does either
s/([\\"'])/\\$1/g;
or
s/([\\"'\0\n\r\cZ])/\\$1/g;
although I would still be wary.
You could just check for special chars in the variables you add to your query string that are required to do an SQL-Injection such as ";" or brackets and replace them or throw them out?!?

Single Quote Error

When I insert single quote in search box and press search button it gives error like:
[Microsoft][SQL Server Native Client
10.0][SQL Server]Unclosed quotation mark after the character string ' '.
You should be using parameterized queries instead of constructing your SQL by concatenation.
This will avoid SQL Injection attacks as well as resolve any single quote issues.
The quick fix it to escape the ' by doubling it (''), but this would just be a temporary workaround and your code will still be vulnerable.
Parameterize your SQL queries. There are more serious issues than this called SQL Injection.
You need to escape single quotes, like \' as you're using single quotes to surround where-statements, like where i = 'foo', then you need to write where i = '\'' to match a single quote, or lie where i = 'it\'s a good day today'