473,809 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_esca pe_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 3147
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_esca pe_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_esc ape_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 SqlEscapedQuote dString($unesca ped_string) {
return '"' . addslashes($une scaped_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 SqlEscapedQuote dString($unesca ped_string) {
return '"' . addslashes($une scaped_string) . '"';
}

For me, this is way too simple.I use "whitelisti ng" 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_esca pe_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_esca pe_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_esca pe_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
7140
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 profile sites that you would think would have good security in place. In fact, if memory serves, wasn't one of Microsoft's site hacked? Since you have to put in a password to get into the server, obviously there's some other way to do it. How...
1
2039
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 you setup the asp pages so that it is easy to change the text based on where the user is connecting from? Thanks, Mike
1
382
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 Command/Parameters objects to initialize and call the stored procedure Which is a efficient way and wh Appreciate your respons
4
7252
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 function in the world at birth. Defining a parameterless constructor allows it to be created in an unstable state. On the other hand
2
1522
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 .Parameters.Clear() .Parameters.Add("@TextField", SqlDbType.NVarChar, 50).Value = TextField
8
3888
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 suspicious bouncebacks in quite some time and got many custom alerts I had set up for notifying me of injection attempts. However, just the other day, I got a bounceback from an AOL address which leads me to believe that an injection attempt was...
2
1919
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 this code is the best way to find if a user entered a valid URL and to avoid SQL injection from the URL. function checkURL($url, $name) { global $incorrect_input;
2
4365
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 do. As an example of such http://www.sheepeep.com/index.php?p=2'%22%3E%3Cscript%20type='text/javascript'%20src='http://www.helraizer.co.uk/mychatbox/xss.js'%3E%3C/script%3E With PHP (or any other language, come to that) how would one protect...
4
3379
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 green green school, tagore search words should come continous problem
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.