Solved

ASP Function Char replace issue

Posted on 2008-10-10
6
948 Views
Last Modified: 2012-05-05
I am trying to stop SQL injection attacks.
I have written the following function that will help filter out the commands.
But i can't get it to output the VAR.

+ i'm not sure if how i'm going to use this funiction again and again for dirrent fields
from request.form().  like username and password.

But first things first.  Please can someone let me know why i can't get an output from the following:
<% 
function killChars(inputtext) 
			dim badChars 
			dim newChars 
			
			badChars = array("select", "drop", ";", "--", "insert", "delete", "xp_", "=") 
			newChars = inputtext 
			
			for i = 0 to uBound(badChars) 
			newChars = replace(newChars, badChars(i), "") 
			next 
			 response.write(inputtext & "done | ")
end function 
 
text1 = "Hello ' select bye"
 
call KillChars(text1)
response.write("CLEAN:" & newChars)
%>

Open in new window

0
Comment
Question by:myhc
6 Comments
 
LVL 6

Expert Comment

by:RemcovC
ID: 22686181
You are using a var outside a function in which you declared it, that wont work

You could try something like this:

<%
function killChars(inputtext)
      dim badChars
                  
      badChars = array("select", "drop", ";", "--", "insert", "delete", "xp_", "=")
                  
      for i = 0 to uBound(badChars)
            killChars = replace(inputtext, badChars(i), "")
      next
      response.write(inputtext & "done | ")
end function
 
text1 = "Hello ' select bye"

response.write("CLEAN:" & KillChars(text1))
%>
0
 
LVL 3

Expert Comment

by:sam_norian
ID: 22686189
Hi,
 
 This uses a slightly different approach but should achieve the same result....

function killChars(inputtext)
    if not isNull(inputtext) then
        inputtext = Replace(inputtext,"select","")
        inputtext = Replace(inputtext,"drop","")
        inputtext = Replace(inputtext,"insert","")
        inputtext = Replace(inputtext,"delete","")
        inputtext = Replace(inputtext,"xp_","")
        inputtext = Replace(inputtext,"=","")
        inputtext = Replace(inputtext,"--","")
        inputtext = Replace(inputtext,";","")
        killChars = inputtext
     else
        inputtext = ""
     end if
end function
 
'''''''''''''To Call You Can Then Do..'''''''''''''''''''''''
 
killChars(request.form("YourField"))

Open in new window

0
 
LVL 6

Expert Comment

by:RemcovC
ID: 22686214
made a small error in the code....

<%
function killChars(inputtext)
      dim badChars
      killChars = inputtext          
      badChars = array("select", "drop", ";", "--", "insert", "delete", "xp_", "=")
                 
      for i = 0 to uBound(badChars)
            killChars = replace(killChars , badChars(i), "")
      next
      response.write(inputtext & "done | ")
end function
 
text1 = "Hello ' select bye"

response.write("CLEAN:" & KillChars(text1))
%>
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 7

Accepted Solution

by:
bluV11t earned 230 total points
ID: 22690678
<%
function killChars(inputtext)
                        dim badChars
                        dim newChars
                       
                        badChars = array("select", "drop", ";", "--", "insert", "delete", "xp_", "=")
                        newChars = inputtext
                       
                        for i = 0 to uBound(badChars)
                        newChars = replace(newChars, badChars(i), "")
                        next
                         response.write(inputtext & "done | ")
'THIS IS STEP1
killChars = inputtext

end function
 
text1 = "Hello ' select bye"
 
'THIS IS STEP2
myNewVariable = KillChars(text1)
response.write("CLEAN:" & myNewVariable )
%>
0
 
LVL 12

Assisted Solution

by:R_Harrison
R_Harrison earned 20 total points
ID: 22695342
By the way you also want to add single quote ' and colon : to you list of badChars, otherwise hackers will still be able to run SQL injection.

Also develop a second function for values that should be numeric (to ensure they are numeric) as these are the most vunerable to sql injection.    For you second function a simple isNumeric() should suffice.
0
 
LVL 7

Author Closing Comment

by:myhc
ID: 31504976
Sorted my code, works fine - Thank You
Harrison, Advised on more issues with SQL.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Autosum input type=text when checkbox is true 28 94
Highlight a word from some text 16 56
Bypass cross origin issues on development site 3 76
If-Then-Else ASP problem 6 59
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question