Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading excel file and saving data to the DB via Fileupload control in asp.net with c#.

The issue is that the files uploaded history screen user can download and injection attacks can be set to the files uploaded. So i need to block the injection attack during the upload time itself

I need to read the file fully and check the same contain these characters (+, =, "", @) and validate on uploading itself. If it is to read line by line it will cause performance/time out issue especially with bulk files.

What I have tried:

Core code is as below:
(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
		Server.MapPath("example.xls") + ";
		Extended Properties=Excel 8.0");//OledbConnection and 
				// connectionstring to connect to the Excel Sheet
        try
        {
            //After connecting to the Excel sheet here we are selecting the data 
	   //using select statement from the Excel sheet
	   OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
	            oconn.Open();  //Here [Sheet1$] is the name of the sheet 
				//in the Excel file where the data is present
            OleDbDataReader odr = ocmd.ExecuteReader();
            string fname = "";
            string lname = "";
            string mobnum = "";
            string city = "";
            string state = "";
            string zip = "";
            while (odr.Read())
            {
                fname = valid(odr, 0);//Here we are calling the valid method
                lname = valid(odr, 1);
                mobnum = valid(odr, 2);
                city = valid(odr, 3);
                state = valid(odr, 4);
                zip = valid(odr, 5);
                //Here using this method we are inserting the data into the database
	       insertdataintosql(fname, lname, mobnum, city, state, zip);
            }
            oconn.Close();
        }
        catch (DataException ee)
        {
            lblmsg.Text = ee.Message;
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        finally
        {
            lblmsg.Text = "Data Inserted Sucessfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;
        }
Posted
Updated 7-May-18 21:43pm
v4

If you are worried about injection attacks, then you need to look at your insertdataintosql method - which we can't see - and all your other code that accesses the spreadsheet. What you are looking for is string concatenation.

Never concatenate strings to build a command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because the source receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as the source is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then the source receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Here is a little lecture about SQL Injection:
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
Quote:
csv injection attack or so was told.

Does not exist.
 
Share this answer
 
v2
Comments
ranio 8-May-18 2:50am    
What i need is to validate special characters like +, =, "", @ exists in the bulk files(excel/csv). Not related to Sql.
Patrice T 8-May-18 2:56am    
What kind of "injection attacks" are you talking about ?
ranio 8-May-18 3:20am    
csv injection attack or so was told. For that special characters are mentioned as above and i need to block if there without reading line by line which cause performance issue.
First of all, you're in real trouble with your code:
C#
while (odr.Read())
{
    //...
    insertdataintosql(fname, lname, mobnum, city, state, zip); //here!
}

Imagine, every time when you call insertdatainstosql method, your sql server opens new transaction[^]. Note that transactions allow you to protect yourself from the negative effects of data actuality situation by linking many operations on the basis into one logical whole. Instructions executed as part of the transaction are treated as a whole and until they are approved, they do not modify the data. But when 2 processes with are competing for exclusive access to a resource but is unable to obtain exclusive access to it, because the other process is preventing it, then deadlock occurs. See:
SQL Server Deadlocks by Example - Simple Talk[^]
Handling Deadlocks in SQL Server - Simple Talk[^]

Instaed of inserting data in a loop you should learn about: Performing Bulk Copy Operations (Microsoft Docs)[^]. There's special class: SqlBulkCopy Class (System.Data.SqlClient)[^], which provides a way to efficiently bulk load a SQL Server table with data from another source.


You can prevent inserting unwanted characters (+, =, "", @) into database by using REPLACE[^] function with SELECT statement
SQL
SELECT REPLACE(REPLACE(REPLACE(Column1, '+', ''), '=', ''), '@', '') AS Column1
FROM [Sheet1$]
 
Share this answer
 

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