473,434 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

The best way to protect SQL injection?

Hi all.

I would like to ask if Regular expression is the best way to deal with
SQL injection attack, and no mysql_real_escape_string() is used:

if(preg_match("[A-Za-z0-9](4,6)")){
print "Success!";
}

In the above example, only character and digit are allowed. Other
injection technique is no used.

Is it correct? Did I make any foolish assumptions or mistakes? Please
let me know.

Thank you very much.

Dec 26 '06 #1
6 3128
Alucard schreef:
Hi all.

I would like to ask if Regular expression is the best way to deal with
SQL injection attack, and no mysql_real_escape_string() is used:
Imho there are two things you have to take care of:

1) Validate user input (a regular expression can be used)
2) Prepare the data for use in a MySQL query (mysql_real_escape_string
can be used for but these day's i'd opt for parameter binding instead...)


--
Tim Van Wassenhove <url:http://www.timvw.be/>
Dec 26 '06 #2
I wrote a simple PHP function for handling string arguments to a SQL
Query:

function SqlEscapedQuotedString($unescaped_string) {
return '"' . addslashes($unescaped_string) . '"';
}

I never accept a SQL Query from the client, only an Argument to a SQL
Query on the server.

Gerard Vignes
http://www.GerardVignes.com
Seattle, WA

Dec 26 '06 #3
I wrote a simple PHP function for handling string arguments to a SQL
Query:

function SqlEscapedQuotedString($unescaped_string) {
return '"' . addslashes($unescaped_string) . '"';
}

For me, this is way too simple.I use "whitelisting" for the SQL values:
any character that is valid SQL is allowed (though escaped for some
characters), and a string containing any other character is sent as a
hexadecimal string.

Best regards
Dec 26 '06 #4
The suggested way to protect user-supplied input to with MySQL involves
using a special PHP function for MySQL:

mysql_real_escape_string (PHP 4 >= 4.3.0, PHP 5)

http://www.php.net/manual/en/functio...ape-string.php

This takes the character set used by the database into account.

Gerard Vignes
http://www.GerardVignes.com
Seattle, WA

Dec 26 '06 #5
>I would like to ask if Regular expression is the best way to deal with
>SQL injection attack, and no mysql_real_escape_string() is used:

if(preg_match("[A-Za-z0-9](4,6)")){
print "Success!";
}

In the above example, only character and digit are allowed. Other
You mean letter and digit, don't you?
Certain characters (e.g. single quote, double quote, backslash) are
ones that cause trouble.
>injection technique is no used.
Your approach will not work where valid input (e.g. of human names)
includes characters which need to be escaped (e.g. 'Miles O'Brien')
and spaces. On the other hand, it may work fine (if you change the
length limit) for inputting license plate numbers and possibly
product serial numbers. It will NOT work for inputting serial
numbers on US currency, which sometimes contain '*' as the last
character.
>Is it correct? Did I make any foolish assumptions or mistakes? Please
let me know.
Dec 27 '06 #6
The suggested way to protect user-supplied input to with MySQL involves
using a special PHP function for MySQL:

mysql_real_escape_string (PHP 4 >= 4.3.0, PHP 5)

http://www.php.net/manual/en/functio...ape-string.php

This takes the character set used by the database into account.

I know. And that is a severe problem for me. At the time I build the
queries, there may not even be a database connection. I do not want it
to work with a current database connection, I want it to work with _all_
database connections. SQL itself is just normal 7-bits ASCII (there may
be ways to configure the server otherwise, but I don't do that) and it
is only the strings that have to be escaped. So what is safer than
building the entire command in 7-bits ASCII?

Best regards
Dec 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Doc | last post by:
I keep reading about various websites being hacked into and wonder, how is this done? Not for any nefarious reasons, but to take measures to protect mine. Apparently this has been done to some high...
1
by: mike | last post by:
Hello all, any advice or links to pages about how to have multiple language asp pages? For example, you have a database driven website that you want customers around the world to use. How do...
1
by: mike | last post by:
H There are two ways to execute a stored procedure using ADO.NE - call "exec sp_myproc val1,val2" statement over ADO SqlCommand myCommand = new SqlCommand(SQLQuery, mySqlConnection) - use...
4
by: Robert Zurer | last post by:
Hello All, Is it considered a best practice to always write a parameterless constructor for any object - just in case? I'm not sure. I want my object to have all it absolutely requires to...
2
by: Fred Flintstone | last post by:
What's the difference between these two methods? 1 - Parameterrized SQL queries: Dim CommandObject As New Data.SqlClient.SqlCommand With CommandObject .Connection = myConnection...
8
by: stirrell | last post by:
Hello, One problem that I had been having is stopping email injections on contact forms. I did some research, read up on it and felt like I had created a working solution. I hadn't gotten any...
2
by: Rick Stem | last post by:
I have checkURL(http://globalwarmingawareness2007.org.uk, globalwarmingawareness2007.org.uk) I see almost everyone using regular expressions. But I don't completely trust them. Don't know if...
2
by: helraizer1 | last post by:
Hi all, I've noticed on my friend's site www.sheepeep.com/index.php?p=1 that is easy to manipulate with XSS, I don't mean it in a malicious way at all, just to give an idea as to what people can...
4
by: kkshansid | last post by:
i have a database as table name school field name location eg in location column data green school,tagore garden,chink road,jammu i want to make search on location such that when user enter ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.