我是一名大学生,正在学习计算机安全类(class)。我必须完成一个最终项目,因此我决定做一个项目,在其中创建自己的网站并运行 DoS 攻击和 SQL 注入(inject)(我还将在自己的虚拟机上进行缓冲区溢出攻击)。因此,我正在使用 Visual Studio 2019 使用 ASP.NET 创建一个网站。我还没有真正使用过 ASP.NET,我只是想制作一个基本网站。到目前为止,我有一个网页,您可以在其中查看“客户”并且可以搜索一个。所以我创建了这段代码,以便我可以进行 SQL 注入(inject),并且我了解实现此代码的正确方法。这是到目前为止我的代码
这是一个模型类,我的实际易受攻击的 SQL 命令正在其中运行
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Text;
namespace bullAndTrue.Models
{
public class CustomerContext
{
public string ConnectionString { get; set; }
public CustomerContext(string connectionString)
{
this.ConnectionString = connectionString;
}
private MySqlConnection GetConnection()
{
return new MySqlConnection(ConnectionString);
}
public List<Customer> GetCustomers(string name)
{
List<Customer> customerList = new List<Customer>();
using(MySqlConnection conn = GetConnection())
{
conn.Open();
MySqlCommand myCmd = new MySqlCommand("SELECT * FROM Customers WHERE firstName LIKE '%" + name + "%'", conn);
using(var reader= myCmd.ExecuteReader())
{
while(reader.Read())
{
customerList.Add(new Customer()
{
idCustomers = Convert.ToInt32(reader["idCustomers"]),
firstName = reader["firstName"].ToString(),
lastName = reader["lastName"].ToString(),
address = reader["address"].ToString()
});
}
}
}
return customerList;
}
}
}
在这里我们可以看到我的命令
SELECT * FROM Customers WHERE firstName LIKE '%userInput%'
是脆弱的部分。在这里我应该能够输入 ';-- 并获取所有客户的列表。我没有得到所有客户的列表,而是得到了一个错误(这真是太棒了,有安全选项,但这就是这个项目的重点是跳过安全性!)
错误:
MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '--%'' at line 1'
我一直在尝试找出如何绕过此错误并获取我需要的查询。我一直在尝试使用不同版本的依赖项,但我无法弄清楚是什么阻止了我进行注入(inject)。以下是我的相对依赖项的列表:
- MySql.Data 版本= 8.0.22
- Microsoft.AspNetCore 版本= 2.1.7
我的服务器正在 AWS 实例上运行,其运行的 MySQL 版本为 5.5(我能达到的最低版本;也许这就是问题所在?)
这是我第一次发帖,希望我做得对。如果您需要更多信息,我很乐意发布。如有任何帮助,我们将不胜感激!
最佳答案
In MySQL, the -- (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.7.2.4, “'--' as the Start of a Comment”
关于c# - SQL - ASP.NET Core 2.1.7 中的故意注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64510910/