且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于Mysql + asp.net注射能支持多语句的感慨

更新时间:2022-09-21 23:26:27

在一篇老外的文章里面看到一个图表,致使Mysql + Asp.Net 注入时支持多语句

真的?有点不信,测试了下:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Response.Write("使用Mysql.Data.MySqlClient<br /><br />");
            string myConnectionString = "server=localhost;user id=root;password=123456;database=test";
            MySqlConnection myConnection = new MySqlConnection(myConnectionString);
            string myQuery = "select * from admin where id=" + Request.QueryString["id"];
            Response.Write("SQL:" + myQuery + "<br />");
            MySqlCommand myCommand = new MySqlCommand(myQuery, myConnection);
            myConnection.Open();
            myCommand.ExecuteReader();
            myCommand.Connection.Close();
        }
        catch (MySqlException err)
        {
            Response.Write("Error:" + err.Message);
        }
       
        //try
        //{
        //    Response.Write("使用Mysql ODBC驱动<br /><br />");
        //    string myConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;UID=root;PASSWORD=123456;OPTION=3";
        //    string myQuery = "select * from admin where id=" + Request.QueryString["id"];
        //    Response.Write("SQL:" + myQuery + "<br />");
        //    OdbcConnection myConnection = new OdbcConnection(myConnectionString);
        //    OdbcCommand myCommand = new OdbcCommand(myQuery, myConnection);
        //    myConnection.Open();
        //    myCommand.ExecuteReader();
        //    myCommand.Connection.Close();
        //}
        //catch(OleDbException err)
        //{
        //    Response.Write("Error:" + err.Message);
        //}
    }

这里面是连接mysql常用的两个方式,一个是用Mysql.Data.MySqlClient这个类。二个try是使用更加传统的odbc方式。

Step 1:注释第一个代码段,访问http://localhost:2928/MysqlInj_test/MysqlInj_test.aspx?id=1;create table test(i int) 结果得到如下的错误信息:

“/MysqlInj_test”应用程序中的服务器错误。
--------------------------------------------------------------------------------

ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.45-community-nt]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 ';create table test(i int)' at line 1

到test库下面看,表test没有被创建

Step 2:注释第二个代码段,刷新页面。页面返回

使用Mysql.Data.MySqlClient

SQL:select * from admin where id=1;create table test(i int)

继续按F5刷新这个页面,得到如下消息

使用Mysql.Data.MySqlClient

SQL:select * from admin where id=1;create table test(i int)
Error:Table 'test' already exists

跑到test库看,果然有个叫test的表被创建了。乖乖,看来老外说的是真的,但是对了一半。

原来Mysql能不能多语句执行,这和处理它的数据库驱动有很大关系。不知道是不是绝对联系,但是至少是一个重要的因素。

推而广之呢?