Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Team,
please provide some solution for this SQL injection flaw in veracode and find below code.

source code:

flaws @ both com.ExecuteNonQuery();
C#
string sql = "";
int length = 30;

adr = adr.Trim();
									
if(adr.Length<length)>
adr = adr.Substring(0,length);

cl.DTime = DateTime.Now.ToString("dd.MM.yy");

sql += "INSERT INTO Report (Datum,VG_UG,NameVN,VstNr,ArtPr,Sti,Prov,ArtEnt) ";
sql += "VALUES ( ";
sql += "'" + cl.DTime + "'," + "'" + vg + "'," + "'" + adr + "'," + cl.VSTNr 
+ ",'Risiko'," + "'" + sti
+ "'," + pRisik.ToString().Replace(",",".") + ",'Bestan');";  
com.CommandText =  sql;
com.ExecuteNonQuery();///flaw at this line

sql = "INSERT INTO Report (Datum,VG_UG,NameVN,VstNr,ArtPr,Sti,Prov,ArtEnt)";
sql += "VALUES ( ";
sql += "'" + cl.DTime + "'," + "'" + vg + "'," + "'" + adr + "'," + cl.VSTNr
+ ",'Sparpr'," + "'" + sti
+ "'," + pSparp.ToString().Replace(",",".") + ",'Bestan');";  					
com.CommandText =  sql;
com.ExecuteNonQuery();

sql = "INSERT INTO Report (VG_UG) VALUES ('')";
com.CommandText = sql;
com.ExecuteNonQuery();/// flaw at this line
Posted
Updated 15-Jul-15 2:03am
v4

The problem is that you're concatenating data directly to the SQL statement. The correct way is to use SqlParameter[^]

So for all values use parameters and define the values for them.
 
Share this answer
 
may be this link will help you
How To: Protect From SQL Injection in ASP.NET[^]
 
Share this answer
 
Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but the possibility of SQL injection is way more important issue here.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA
 
Share this answer
 
v2

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