-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSecondOrderSqlInjection.cs
More file actions
54 lines (47 loc) · 1.8 KB
/
SecondOrderSqlInjection.cs
File metadata and controls
54 lines (47 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Data.SqlClient;
namespace Test
{
using System.Data.SQLite;
using System.IO;
using System.Text;
class SecondOrderSqlInjection
{
public void ProcessRequest()
{
using (SqlConnection connection = new SqlConnection(""))
{
connection.Open();
SqlCommand customerCommand = new SqlCommand("SELECT * FROM customers", connection);
SqlDataReader customerReader = customerCommand.ExecuteReader(); // $ Source[cs/sql-injection]
while (customerReader.Read())
{
// BAD: Read from database, write it straight to another query
SqlCommand secondCustomerCommand = new SqlCommand("SELECT * FROM customers WHERE customerName=" + customerReader.GetString(1), connection); // $ Alert[cs/sql-injection]
}
customerReader.Close();
}
}
public void RunSQLFromFile()
{
using (FileStream fs = new FileStream("myfile.txt", FileMode.Open)) // $ Source[cs/sql-injection]
{
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
var sql = String.Empty;
while ((sql = sr.ReadLine()) != null)
{
sql = sql.Trim();
if (sql.StartsWith("--"))
continue;
using (var connection = new SQLiteConnection(""))
{
var cmd = new SQLiteCommand(sql, connection); // $ Alert[cs/sql-injection]
cmd.ExecuteScalar();
}
}
}
}
}
}
}