Results 1 to 5 of 5

Thread: How to stop SQL Injection...need help.

  1. #1

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    How to stop SQL Injection...need help.

    Hello,

    I am coding an ASP.Net app, and I need a search function. I understand that SQL injection can be a problem, so I am trying to prevent it if it happens. Here is what I have so far:

    Code:
    private string RemoveSqlInjectionAttacks(string searchString)
    {
    	string[] badStrings = {"select", "drop", ";", "--", "insert", "delete", "xp_"};
    
    	// Replace single quotes with double single quotes.
    	searchString = searchString.Replace("'", "''");
    			
    	// Remove all the strings that can do damage
    	for(int i=0; i <= badStrings.GetUpperBound(0); i++)
    	{
    		searchString = searchString.Replace(badStrings[i].ToString(), "");
    	}
    
    	return searchString;
    }
    How does it look? Will this stop all of them? If anyone has anything to add to it, I would appreciate it.

  2. #2
    Member
    Join Date
    Mar 2003
    Posts
    34
    I have one suggestion. I would store all of the injection characters in your xml application settings file, so if you need to add or delete any elements, it's as easy as opening your file in notepad and making the modification. Rather than hard coding your array elements, you can dynamically load the array from your xml. Not only do I think this will make your code cleaner, but it will also make it much easier to maintain in the future. Let me know if you need an example
    AKA 'Lethal'

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Also, you can avoid SQL injection if you use stored procedures instead of building the sql statements on the fly. I recently got a book from MS called writing secure code, which tells you how to avoid situations like this.

    When I get home I'll post some more info.
    Dont gain the world and lose your soul

  4. #4

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    DevGrp: I would appreciate that.

    SimonVega: I would end up doing that because the app needs to be highly configurable. Thanks

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Dont gain the world and lose your soul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width