Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello everybody.
Who can explain me how to generally improve my coding against SQL injection and hackers.It means how to design a secure website or windows application that is isolated from SQL injection hackers?
for example i insert a function behind the log in form that remove dangerous characters from username and password.
C#
static public string MakeSafeData(string Unsafe_String)
    {
        string Safe_String = null;
        string[] Valid_Chars = new string[36] { 
                     "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 
                     "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                     "u", "v", "w", "x", "y", "z" };

        //remove Unsafe Characters
        for (int c = 0; c < Unsafe_String.Length; c++)
        {
            foreach (string s in Valid_Chars)
            {
                if (Unsafe_String[c].ToString() == s || 
                    Unsafe_String[c].ToString() == s.ToUpper())
                {
                    Safe_String += Unsafe_String[c];
                }
            }
        }

        return Safe_String;
    }
Posted
Comments
Sergey Alexandrovich Kryukov 1-Jul-12 16:39pm    
This code sample is not related to SQL.
--SA
Abolfazl Beigi 1-Jul-12 16:43pm    
Yes your are right but this a preparation for SQL query.
and my question generally is about SQL Injection.

In one line, to avoid SQL Injectioning use SqlParameter in queries. For a demo, you can check out the following video[^] it also has a sample source to download which you can have a look at.
 
Share this answer
 
Comments
Abolfazl Beigi 1-Jul-12 10:40am    
Thanks a lot for your very nice video link.
Pankaj Nikam 1-Jul-12 12:09pm    
:) you are welcome Ni8max!
Prevent SQL Injection use always SqlParameter instead of string validations like above.

check below QnA related to above, all answers suggesting to use sqlparameters
http://stackoverflow.com/questions/2329499/regex-for-detecting-sql-injections-in-winforms[^]
http://stackoverflow.com/questions/5495753/do-sql-injection-works-in-winforms[^]
 
Share this answer
 
v2
Comments
Abolfazl Beigi 1-Jul-12 10:39am    
Thank you for your helpfully guide.
The best way is: never concatenate strings to form SQL queries. If you always use parametrised queries, then you leave nothing open to SQL Injection attack, and you don't have to change any characters.

Having said that, the way you are doing things is very inefficient:
1) Use a StringBuilder instead of a string to assemble strings from parts. Strings are immutable: they cannot be changed after they are constructed, so each time you add a character you generate a new string a character longer than it was and copy the data over. This is a very, very inefficient way to do it!
2) You don't need local data for your comparison array - use a class level static instead, and it is constructed once, instead of each time the method is called.
3) If you use a List instead of an array, it has a Contains method!
4) If you use char instead of string for your comparison anyway, you don't need to waset time with unecesary ToString calls:
C#
private static char[] Valid_Chars = new char[] {
              '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
              'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
              'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
              'u', 'v', 'w', 'x', 'y', 'z' };
 private static List<char> valid = new List<char>(Valid_Chars);

 static public string MakeSafeData(string Unsafe_String)
     {
     StringBuilder sb = new StringBuilder();
     foreach (char c in Unsafe_String)
         {
         if (valid.Contains(c) || valid.Contains(char.ToLower(c)))
             {
             sb.Append(c);
             }
         }
     return sb.ToString();
     }



[edit]Should have been ToLower not ToUpper to match characters in compare list - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Abolfazl Beigi 1-Jul-12 10:37am    
Thank you so much for your perfect solution.
OriginalGriff 1-Jul-12 10:56am    
You're welcome!
 
Share this answer
 
v2
Comments
Abolfazl Beigi 2-Jul-12 10:42am    
Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900