Is it safe to not parameterize an SQL query when the parameter is not a string?

前端 未结 11 830
一生所求
一生所求 2020-12-23 15:57

In terms of SQL injection, I completely understand the necessity to parameterize a string parameter; that\'s one of the oldest tricks in the book. But when can

11条回答
  •  执笔经年
    2020-12-23 16:19

    No you can get an SQL injection attack that way. I have written an old article in Turkish which shows how here. Article example in PHP and MySQL but concept works same in C# and SQL Server.

    Basically you attack following way. Lets consider you have a page which shows information according to integer id value. You do not parametrized this in value, like below.

    http://localhost/sqlEnjeksiyon//instructors.aspx?id=24
    

    Okay, I assume you are using MySQL and I attack following way.

    http://localhost/sqlEnjeksiyon//instructors.aspx?id=ASCII((SELECT%20DATABASE()))
    

    Note that here injected value is not string. We are changing char value to int using ASCII function. You can accomplish same thing in SQL Server using "CAST(YourVarcharCol AS INT)".

    After that I use length and substring functions to find about your database name.

    http://localhost/sqlEnjeksiyon//instructors.aspx?id=LEN((SELECT%20DATABASE()))
    
    http://localhost/sqlEnjeksiyon//instructors.aspx?id=ASCII(SUBSTR(SELECT%20DATABASE(),1,1))
    

    Then using database name, you start to get table names in database.

    http://localhost/sqlEnjeksiyon//instructors.aspx?id=ASCII(SUBSTR((SELECT table_name FROM INFORMATION_SCHEMA.TABLES LIMIT 1),1,1))
    

    Of course you have to automate this process, since you only get ONE character per query. But you can easily automate it. My article shows one example in watir. Using only one page and not parameterized ID value. I can learn every table name in your database. After that I can look for important tables. It will take time but it is doable.

提交回复
热议问题