SQL注入原理(转)

做测试不会 SQL?超详细的 SQL 查询语法教程来啦! 作为一名测试工程师,工作中在对测试结果进行数据比对的时候,或多或少要和数据库打交道的,要和数据库打交道,那么一些常用的sql查询语法必须要掌握。最近有部分做测试小伙伴表示sql查询不太会,问我有没有sql查询语法这一块的文档可以学习,于是小编就整理了这篇超详细的SQL查询语法教程,来给大家参考学习! 阅读详情

1.1.1 摘要

日前,国内最大的程序员社区优快云网站的用户数据库被黑客公开发布,600万用户的登录名及密码被公开泄露,随后又有多家网站的用户密码被流传于网络,连日来引发众多网民对自己账号、密码等互联网信息被盗取的普遍担忧。

网络安全成为了现在互联网的焦点,这也恰恰触动了每一位用户的神经,由于设计的漏洞导致了不可收拾的恶果,验证了一句话“出来混的,迟早是要还的”,所以我想通过专题博文介绍一些常用的攻击技术和防范策略。

SQL Injection也许很多人都知道或者使用过,如果没有了解或完全没有听过也没有关系,因为接下来我们将介绍SQL Injection。

1.1.2 正文

SQL Injection:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。

具体来说,它是利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。

首先让我们了解什么时候可能发生SQL Injection。

假设我们在浏览器中输入URL www.sample.com,由于它只是对页面的简单请求无需对数据库动进行动态请求,所以它不存在SQL Injection,当我们输入www.sample.com?testid=23时,我们在URL中传递变量testid,并且提供值为23,由于它是对数据库进行动态查询的请求(其中?testid=23表示数据库查询变量),所以我们可以该URL中嵌入恶意SQL语句。

现在我们知道SQL Injection适用场合,接下来我们将通过具体的例子来说明SQL Injection的应用,这里我们以pubs数据库作为例子。

我们通过Web页面查询job表中的招聘信息,job表的设计如下:

sqlinjection5

图1 jobs表

接着让我们实现Web程序,它根据工作Id(job_id)来查询相应的招聘信息,示意代码如下:

<span style="word-break: break-all; line-height: normal !important; color: gray;">/// <summary>
///</span><span style="word-break: break-all; line-height: normal !important; color: green;">Handles the Load event of the Page control.</span><span style="word-break: break-all; line-height: normal !important; color: gray;">/// </summary>
/// <param name="sender"></span><span style="word-break: break-all; line-height: normal !important; color: green;">The source of the event.</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></param>
/// <param name="e"></span><span style="word-break: break-all; line-height: normal !important; color: green;">The</span><span style="word-break: break-all; line-height: normal !important; color: gray;"><see cref="System.EventArgs"/></span><span style="word-break: break-all; line-height: normal !important; color: green;">instance containing the event data.</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></param></span><span style="word-break: break-all; line-height: normal !important; color: blue;">protected void</span>Page_Load(<span style="word-break: break-all; line-height: normal !important; color: blue;">object</span>sender,<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">EventArgs</span>e)
{<span style="word-break: break-all; line-height: normal !important; color: blue;">if</span>(!IsPostBack)
    {<span style="word-break: break-all; line-height: normal !important; color: green;">// Gets departmentId from http request.</span><span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>queryString = Request.QueryString[<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"departmentID"</span>];<span style="word-break: break-all; line-height: normal !important; color: blue;">if</span>(!<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>.IsNullOrEmpty(queryString))
        {<span style="word-break: break-all; line-height: normal !important; color: green;">// Gets data from database.</span>gdvData.DataSource = GetData(queryString.Trim());<span style="word-break: break-all; line-height: normal !important; color: green;">// Binds data to gridview.</span>gdvData.DataBind();
        }
    }
}

现在我们已经完成了Web程序,接下来让我们查询相应招聘信息吧。

sqlinjection6

图2 job表查询结果

如图所示,我们要查询数据库中工作Id值为1的工作信息,而且在页面显示了该工作的Id,Description,Min Lvl和Max Lvl等信息。

现在要求我们实现根据工作Id查询相应工作信息的功能,想必大家很快可以给出解决方案,SQL示意代码如下:

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>job_desc<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>min_lvl<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>max_lvl<span style="word-break: break-all; line-height: normal !important; color: blue;">FROM</span>jobs<span style="word-break: break-all; line-height: normal !important; color: blue;">WHERE</span><span style="word-break: break-all; line-height: normal !important; color: gray;">(</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">=</span>1<span style="word-break: break-all; line-height: normal !important; color: gray;">)</span>

假设现在要求我们获取Department表中的所有数据,而且必须保留WHERE语句,那我们只要确保WHERE恒真就OK了,SQL示意代码如下:

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT     job_id</span><span style="word-break: break-all; line-height: normal !important; color: gray;">, job_desc, min_lvl, max_lvl</span><span style="word-break: break-all; line-height: normal !important; color: blue;">FROM         jobs
WHERE</span><span style="word-break: break-all; line-height: normal !important; color: gray;">(job_id = 1) OR 1 = 1</span>

上面我们使得WHERE恒真,所以该查询中WHERE已经不起作用了,其查询结果等同于以下SQL语句。

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT     job_id</span><span style="word-break: break-all; line-height: normal !important; color: gray;">, job_desc, min_lvl, max_lvl</span><span style="word-break: break-all; line-height: normal !important; color: blue;">FROM         jobs</span>

SQL查询代码实现如下:

<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>sql1 =<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>.Format(<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"SELECT job_id, job_desc, min_lvl, max_lvl FROM jobs WHERE job_id='{0}'"</span>, jobId);

现在我们要通过页面请求的方式,让数据库执行我们的SQL语句,我们要在URL中嵌入恶意表达式1=1(或2=2等等),如下URL所示:

http://localhost:3452/ExcelUsingXSLT/Default.aspx?jobid=1'or'1'='1

等效SQL语句如下:

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>job_desc<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>min_lvl<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>max_lvl<span style="word-break: break-all; line-height: normal !important; color: blue;">FROM</span>jobs<span style="word-break: break-all; line-height: normal !important; color: blue;">WHERE</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">=</span><span style="word-break: break-all; line-height: normal !important; color: red;">'1'</span><span style="word-break: break-all; line-height: normal !important; color: gray;">OR</span><span style="word-break: break-all; line-height: normal !important; color: red;">'1'</span><span style="word-break: break-all; line-height: normal !important; color: gray;">=</span>1<span style="word-break: break-all; line-height: normal !important; color: red;">'</span>

sqlinjection7

图3 job表查询结果

现在我们把job表中的所有数据都查询出来了,仅仅通过一个简单的恒真表达式就可以进行了一次简单的攻击。

虽然我们把job表的数据都查询出来了,但数据并没有太大的价值,由于我们把该表临时命名为job表,所以接着我们要找出该表真正表名。

首先我们假设表名就是job,然后输入以下URL:

http://localhost:3452/ExcelUsingXSLT/Default.aspx?jobid=1'or 1=(select count(*) from job)--

等效SQL语句如下:

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>job_desc<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>min_lvl<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>max_lvl<span style="word-break: break-all; line-height: normal !important; color: blue;">FROM</span>jobs<span style="word-break: break-all; line-height: normal !important; color: blue;">WHERE</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">=</span><span style="word-break: break-all; line-height: normal !important; color: red;">'1'</span><span style="word-break: break-all; line-height: normal !important; color: gray;">or</span>1<span style="word-break: break-all; line-height: normal !important; color: gray;">=(</span><span style="word-break: break-all; line-height: normal !important; color: blue;">select</span><span style="word-break: break-all; line-height: normal !important; color: rgb(255, 0, 255);">count</span><span style="word-break: break-all; line-height: normal !important; color: gray;">(*)</span><span style="word-break: break-all; line-height: normal !important; color: blue;">from</span>job<span style="word-break: break-all; line-height: normal !important; color: gray;">)</span><span style="word-break: break-all; line-height: normal !important; color: green;">--'</span>

sqlinjection8

图4 job表查询结果

当我们输入了以上URL后,结果服务器返回我们错误信息,这证明了我们的假设是错误的,那我们该感觉到挫败吗?不,其实这里返回了很多信息,首先它证明了该表名不是job,而且它还告诉我们后台数据库是SQL Server,不是MySQL或Oracle,这也设计一个漏洞把错误信息直接返回给了用户。

接下假定表名是jobs,然后输入以下URL:

http://localhost:3452/ExcelUsingXSLT/Default.aspx?jobid=1'or1=(select count(*) from jobs) --

等效SQL语句如下:

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>job_desc<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>min_lvl<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>max_lvl<span style="word-break: break-all; line-height: normal !important; color: blue;">FROM</span>jobs<span style="word-break: break-all; line-height: normal !important; color: blue;">WHERE</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">=</span><span style="word-break: break-all; line-height: normal !important; color: red;">'1'</span><span style="word-break: break-all; line-height: normal !important; color: gray;">or</span>1<span style="word-break: break-all; line-height: normal !important; color: gray;">=(</span><span style="word-break: break-all; line-height: normal !important; color: blue;">select</span><span style="word-break: break-all; line-height: normal !important; color: rgb(255, 0, 255);">count</span><span style="word-break: break-all; line-height: normal !important; color: gray;">(*)</span><span style="word-break: break-all; line-height: normal !important; color: blue;">from</span>jobs<span style="word-break: break-all; line-height: normal !important; color: gray;">)</span><span style="word-break: break-all; line-height: normal !important; color: green;">--'</span>

sqlinjection

图5 job表查询结果

现在证明了该表名是jobs,这可以迈向成功的一大步,由于我们知道了表名就可以对该表进行增删改操作了,而且我们还可以猜测出更多的表对它们作出修改,一旦修改成功那么这将是一场灾难。

现在大家已经对SQL Injection的攻击有了初步的了解了,接下让我们学习如何防止SQL Injection。

总的来说有以下几点:

1.永远不要信任用户的输入,要对用户的输入进行校验,可以通过正则表达式,或限制长度,对单引号和双"-"进行转换等。

2.永远不要使用动态拼装SQL,可以使用参数化的SQL或者直接使用存储过程进行数据查询存取。

3.永远不要使用管理员权限的数据库连接,为每个应用使用单独的权限有限的数据库连接。

4.不要把机密信息明文存放,请加密或者hash掉密码和敏感的信息。

5.应用的异常信息应该给出尽可能少的提示,最好使用自定义的错误信息对原始错误信息进行包装,把异常信息存放在独立的表中。

通过正则表达校验用户输入

首先我们可以通过正则表达式校验用户输入数据中是包含:对单引号和双"-"进行转换等字符。

然后继续校验输入数据中是否包含SQL语句的保留字,如:WHERE,EXEC,DROP等。

现在让我们编写正则表达式来校验用户的输入吧,正则表达式定义如下:

<span style="word-break: break-all; line-height: normal !important; color: blue;">private static readonly</span><span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">Regex</span>RegSystemThreats =<span style="word-break: break-all; line-height: normal !important; color: blue;">new</span><span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">Regex</span>(<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">@"\s?or\s*|\s?;\s?|\s?drop\s|\s?grant\s|^'|\s?--|\s?union\s|\s?delete\s|\s?truncate\s|"</span>+<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">@"\s?sysobjects\s?|\s?xp_.*?|\s?syslogins\s?|\s?sysremote\s?|\s?sysusers\s?|\s?sysxlogins\s?|\s?sysdatabases\s?|\s?aspnet_.*?|\s?exec\s?"</span>,<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">RegexOptions</span>.Compiled |<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">RegexOptions</span>.IgnoreCase);

上面我们定义了一个正则表达式对象RegSystemThreats,并且给它传递了校验用户输入的正则表达式。

由于我们已经完成了对用户输入校验的正则表达式了,接下来就是通过该正则表达式来校验用户输入是否合法了,由于.NET已经帮我们实现了判断字符串是否匹配正则表达式的方法——IsMatch(),所以我们这里只需给传递要匹配的字符串就OK了。

示意代码如下:

<span style="word-break: break-all; line-height: normal !important; color: gray;">/// <summary>
///</span><span style="word-break: break-all; line-height: normal !important; color: green;">A helper method to attempt to discover [known] SqlInjection attacks.</span><span style="word-break: break-all; line-height: normal !important; color: gray;">/// </summary>
/// <param name="whereClause"></span><span style="word-break: break-all; line-height: normal !important; color: green;">string of the whereClause to check</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></param>
/// <returns></span><span style="word-break: break-all; line-height: normal !important; color: green;">true if found, false if not found</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></returns></span><span style="word-break: break-all; line-height: normal !important; color: blue;">public static bool</span>DetectSqlInjection(<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>whereClause)
{<span style="word-break: break-all; line-height: normal !important; color: blue;">return</span>RegSystemThreats.IsMatch(whereClause);
}<span style="word-break: break-all; line-height: normal !important; color: gray;">/// <summary>
///</span><span style="word-break: break-all; line-height: normal !important; color: green;">A helper method to attempt to discover [known] SqlInjection attacks.</span><span style="word-break: break-all; line-height: normal !important; color: gray;">/// </summary>
/// <param name="whereClause"></span><span style="word-break: break-all; line-height: normal !important; color: green;">string of the whereClause to check</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></param>
/// <param name="orderBy"></span><span style="word-break: break-all; line-height: normal !important; color: green;">string of the orderBy clause to check</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></param>
/// <returns></span><span style="word-break: break-all; line-height: normal !important; color: green;">true if found, false if not found</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></returns></span><span style="word-break: break-all; line-height: normal !important; color: blue;">public static bool</span>DetectSqlInjection(<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>whereClause,<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>orderBy)
{<span style="word-break: break-all; line-height: normal !important; color: blue;">return</span>RegSystemThreats.IsMatch(whereClause) || RegSystemThreats.IsMatch(orderBy);
}

现在我们完成了校验用的正则表达式,接下来让我们需要在页面中添加校验功能。

<span style="word-break: break-all; line-height: normal !important; color: gray;">/// <summary>
///</span><span style="word-break: break-all; line-height: normal !important; color: green;">Handles the Load event of the Page control.</span><span style="word-break: break-all; line-height: normal !important; color: gray;">/// </summary>
/// <param name="sender"></span><span style="word-break: break-all; line-height: normal !important; color: green;">The source of the event.</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></param>
/// <param name="e"></span><span style="word-break: break-all; line-height: normal !important; color: green;">The</span><span style="word-break: break-all; line-height: normal !important; color: gray;"><see cref="System.EventArgs"/></span><span style="word-break: break-all; line-height: normal !important; color: green;">instance containing the event data.</span><span style="word-break: break-all; line-height: normal !important; color: gray;"></param></span><span style="word-break: break-all; line-height: normal !important; color: blue;">protected void</span>Page_Load(<span style="word-break: break-all; line-height: normal !important; color: blue;">object</span>sender,<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">EventArgs</span>e)
{<span style="word-break: break-all; line-height: normal !important; color: blue;">if</span>(!IsPostBack)
    {<span style="word-break: break-all; line-height: normal !important; color: green;">// Gets departmentId from http request.</span><span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>queryString = Request.QueryString[<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"jobId"</span>];<span style="word-break: break-all; line-height: normal !important; color: blue;">if</span>(!<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>.IsNullOrEmpty(queryString))
        {<span style="word-break: break-all; line-height: normal !important; color: blue;">if</span>(!DetectSqlInjection(queryString) && !DetectSqlInjection(queryString, queryString))
            {<span style="word-break: break-all; line-height: normal !important; color: green;">// Gets data from database.</span>gdvData.DataSource = GetData(queryString.Trim());<span style="word-break: break-all; line-height: normal !important; color: green;">// Binds data to gridview.</span>gdvData.DataBind();
            }<span style="word-break: break-all; line-height: normal !important; color: blue;">else</span>{<span style="word-break: break-all; line-height: normal !important; color: blue;">throw new</span><span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">Exception</span>(<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"Please enter correct field"</span>);
            }
        }
    }
}

当我们再次执行以下URL时,被嵌入的恶意语句被校验出来了,从而在一定程度上防止了SQL Injection。

http://localhost:3452/ExcelUsingXSLT/Default.aspx?jobid=1'or'1'='1

sqlinjection9

图6 添加校验查询结果

但使用正则表达式只能防范一些常见或已知SQL Injection方式,而且每当发现有新的攻击方式时,都要对正则表达式进行修改,这可是吃力不讨好的工作。

通过参数化存储过程进行数据查询存取

首先我们定义一个存储过程根据jobId来查找jobs表中的数据。

<span style="word-break: break-all; line-height: normal !important; color: green;">-- =============================================
-- Author:        JKhuang
-- Create date: 12/31/2011
-- Description:    Get data from jobs table by specified jobId.
-- =============================================</span><span style="word-break: break-all; line-height: normal !important; color: blue;">ALTER PROCEDURE</span>[dbo]<span style="word-break: break-all; line-height: normal !important; color: gray;">.</span>[GetJobs]<span style="word-break: break-all; line-height: normal !important; color: green;">-- ensure that the id type is int</span>@jobId<span style="word-break: break-all; line-height: normal !important; color: blue;">INT
AS
BEGIN</span><span style="word-break: break-all; line-height: normal !important; color: green;">--    SET NOCOUNT ON;</span><span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>job_desc<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>min_lvl<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>max_lvl<span style="word-break: break-all; line-height: normal !important; color: blue;">FROM</span>dbo<span style="word-break: break-all; line-height: normal !important; color: gray;">.</span>jobs<span style="word-break: break-all; line-height: normal !important; color: blue;">WHERE</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">=</span>@jobId<span style="word-break: break-all; line-height: normal !important; color: blue;">GRANT EXECUTE ON</span>GetJobs<span style="word-break: break-all; line-height: normal !important; color: blue;">TO</span>pubs<span style="word-break: break-all; line-height: normal !important; color: blue;">END</span>

接着修改我们的Web程序使用参数化的存储过程进行数据查询。

<span style="word-break: break-all; line-height: normal !important; color: blue;">using</span>(var com =<span style="word-break: break-all; line-height: normal !important; color: blue;">new</span><span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">SqlCommand</span>(<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"GetJobs"</span>, con))
{<span style="word-break: break-all; line-height: normal !important; color: green;">// Uses store procedure.</span>com.CommandType =<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">CommandType</span>.StoredProcedure;<span style="word-break: break-all; line-height: normal !important; color: green;">// Pass jobId to store procedure.</span>com.Parameters.Add(<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"@jobId"</span>,<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">SqlDbType</span>.Int).Value = jobId;
    com.Connection.Open();
    gdvData.DataSource = com.ExecuteScalar();
    gdvData.DataBind(); 
}

现在我们通过参数化存储过程进行数据库查询,这里我们把之前添加的正则表达式校验注释掉。

sqlinjection10

图7 存储过程查询结果

大家看到当我们试图在URL中嵌入恶意的SQL语句时,参数化存储过程已经帮我们校验出传递给数据库的变量不是整形,而且使用存储过程的好处是我们还可以很方便地控制用户权限,我们可以给用户分配只读或可读写权限。

但我们想想真的有必要每个数据库操作都定义成存储过程吗?而且那么多的存储过程也不利于日常的维护。

参数化SQL语句

还是回到之前动态拼接SQL基础上,我们知道一旦有恶意SQL代码传递过来,而且被拼接到SQL语句中就会被数据库执行,那么我们是否可以在拼接之前进行判断呢?——命名SQL参数。

<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>sql1 =<span style="word-break: break-all; line-height: normal !important; color: blue;">string</span>.Format(<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"SELECT job_id, job_desc, min_lvl, max_lvl FROM jobs WHERE job_id = @jobId"</span>);<span style="word-break: break-all; line-height: normal !important; color: blue;">using</span>(var con =<span style="word-break: break-all; line-height: normal !important; color: blue;">new</span><span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">SqlConnection</span>(<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">ConfigurationManager</span>.ConnectionStrings[<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"SQLCONN1"</span>].ToString()))<span style="word-break: break-all; line-height: normal !important; color: blue;">using</span>(var com =<span style="word-break: break-all; line-height: normal !important; color: blue;">new</span><span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">SqlCommand</span>(sql1, con))
{<span style="word-break: break-all; line-height: normal !important; color: green;">// Pass jobId to sql statement.</span>com.Parameters.Add(<span style="word-break: break-all; line-height: normal !important; color: rgb(163, 21, 21);">"@jobId"</span>,<span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">SqlDbType</span>.Int).Value = jobId;
    com.Connection.Open();
    gdvData.DataSource = com.ExecuteReader();
    gdvData.DataBind(); 
}

sqlinjection10

图8 参数化SQL查询结果

这样我们就可以避免每个数据库操作(尤其一些简单数据库操作)都编写存储过程了,而且当用户具有数据库中jobs表的读权限才可以执行该SQL语句。

添加新架构

数据库架构是一个独立于数据库用户的非重复命名空间,您可以将架构视为对象的容器(类似于.NET中的命名空间)。

首先我们右击架构文件夹,然后新建架构。

clip_image002

sqlinjection12

图9 添加HumanResource架构

上面我们完成了在pubs数据库中添加HumanResource架构,接着把jobs表放到HumanResource架构中。

sqlinjection15

sqlinjection13

图 10 修改jobs表所属的架构

当我们再次执行以下SQL语句时,SQL Server提示jobs无效,这是究竟什么原因呢?之前还运行的好好的。

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>job_desc<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>min_lvl<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>max_lvl<span style="word-break: break-all; line-height: normal !important; color: blue;">FROM</span>jobs

sqlinjection14

图 11 查询输出

当我们输入完整的表名“架构名.对象名”(HumanResource.jobs)时,SQL语句执行成功。

<span style="word-break: break-all; line-height: normal !important; color: blue;">SELECT</span>job_id<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>job_desc<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>min_lvl<span style="word-break: break-all; line-height: normal !important; color: gray;">,</span>max_lvl<span style="word-break: break-all; line-height: normal !important; color: blue;">FROM</span>HumanResource<span style="word-break: break-all; line-height: normal !important; color: gray;">.</span>jobs

sqlinjection16

为什么之前我们执行SQL语句时不用输入完整表名dbo.jobs也可以执行呢?

这是因为默认的架构(default schema)是dbo,当只输入表名时,Sql Server会自动加上当前登录用户的默认的架构(default schema)——dbo。

由于我们使用自定义架构,这也降低了数据库表名被猜测出来的可能性。

LINQ to SQL

前面使用了存储过程和参数化查询,这两种方法都是非常常用的,而针对于.NET Framework的ORM框架也有很多,如:NHibernate,Castle和Entity Framework,这里我们使用比较简单LINQ to SQL。

sqlinjection17

图 12 添加jobs.dbml文件

var dc =<span style="word-break: break-all; line-height: normal !important; color: blue;">new</span><span style="word-break: break-all; line-height: normal !important; color: rgb(43, 145, 175);">pubsDataContext</span>();<span style="word-break: break-all; line-height: normal !important; color: blue;">int</span>result;<span style="word-break: break-all; line-height: normal !important; color: green;">// Validates jobId is int or not.</span><span style="word-break: break-all; line-height: normal !important; color: blue;">if</span>(<span style="word-break: break-all; line-height: normal !important; color: blue;">int</span>.TryParse(jobId,<span style="word-break: break-all; line-height: normal !important; color: blue;">out</span>result))
{
    gdvData.DataSource = dc.jobs.Where(p => p.job_id == result);
    gdvData.DataBind();
}

相比存储过程和参数化查询,LINQ to SQL我们只需添加jobs.dbml,然后使用LINQ对表进行查询就OK了。

1.1.3 总结

我们在本文中介绍了SQL Injection的基本原理,通过介绍什么是SQL Injection,怎样进行SQL Injection和如何防范SQL Injection。通过一些程序源码对SQL的攻击进行了细致的分析,使我们对SQL Injection机理有了一个深入的认识,作为一名Web应用开发人员,一定不要盲目相信用户的输入,而要对用户输入的数据进行严格的校验处理,否则的话,SQL Injection将会不期而至。

sql注入详解 目录前言? ?一、漏洞原因分析二、漏洞危害三、sql注入防范四、如何挖掘sql注入漏洞五、常见的注入手法联合查询(union注入)报错注入基于布尔的盲注基于时间的盲注HTTP头注入宽字节注入堆叠查询二阶注入六、sql注入getshell的几种方式结构化查询语言(Structured Query Language,缩写:SQL),是一种特殊的编程语言,用于数据库中的标准数据查询语言。SQL注入(SQL Injection)是一种常见的Web安全漏洞,主要形成的原因是在数据交互中,前端的数据传入到后台处理时,没 阅读详情

相关推荐

jsql-injection:jSQL Injection是用于自动SQL数据库注入的Java应用程序

描述 jSQL Injection是一个轻量级应用程序,用于从远程服务器查找数据库信息。 它是免费的,开源的和跨平台的,适用于Windows,Linux和Mac OS X(带有Java,版本8至15)。 jSQL Injection也是官方渗透测试发行版一部分,并包含在其他各种发行版中,例如 , , 和 。 产品特点 自动注入33种数据库:Access,Altibase,C-treeACE,CockroachDB,CUBRID,DB2,Derby,Exasol,Firebird,FrontBase,H2,Hana,HSQLDB,Informix,Ingres,InterSystems-IRIS,MaxDB,Mckoi,MemSQL, MimerSQL,MonetDB,MySQL,Neo4j,Netezza,NuoDB,Oracle,PostgreSQL,Presto,SQLite,SQL Server,Sybase,Teradata和Vertica 多种注入策略:正常,错误,堆叠,盲注和时间 各种注入过程:默认,Zip,Dios 用于SQL和篡改脚本的沙箱 列出以注入

SQL注入原理讲解,很不错!

原文地址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html 1.1.1 摘要 日前,国内最大的程序员社区优快云网站的用户数据库被黑客公开发布,600万用户的登录名及密码被公开泄露,随后又有多家网站的用户密码被流传于网络,连日来引发众多网民对自己账号、密码等互联网信息被盗取的普遍担忧。 网络安全成为了现在互联网的焦点,

stilling2006的专栏 39万+

长期换衣行人重识别(Long-Term Clothes-Changing Person Reid)数据集汇总

长期换衣行人重识别任务中目前常用的数据集总览目前换衣数据集的主要问题数据集换衣/总体情况集统计DeepChange(2021)LaST(2021)COCAS(2020)VC-Clothes(2020)LTCC(2020)PRCC(2019)Celebrities-reID(2019) 总览 在长期换衣行人重识别的任务当中,数据集的获取(尤其是对于学术环境下)是一个难点,主要的困难有以下几点: 采集大规模的数据集比较困难。 采集不同的衣服的数据集比较困难。 采集不同衣服不同角度的数据集比较困难。 目前也有

KingsMan666的博客 8380

白话sql注入(sql Injection)系统总结

数据库就是一个存储数据的仓库,数据库是以一定方式存储在一起,能与多个用户共享,具有尽可能小的冗余,与应用程序彼此独立的数据集合。mysql中存在4个控制权限的表,分别为user表,db表,tables_priv表,columns_priv表,mysql权限表的验证过程为:先从user表中的Host,User,Password这3个字段中判断连接的ip、用户名、密码是否存在,存在则通过验证。通过身份认证后,进行权限分配,

weixin_54603520的博客 1300

jenkins连接外部k8s集群

k8s连接集群外Jenkins

liuchao666888的博客 1527

DSP28335与Vofa+上位机通信实战:SCIb配置避坑指南(附完整代码)

本文提供DSP28335与Vofa+上位机通信的实战指南,重点解析SCIb模块配置的常见问题与解决方案。针对研旭开发板的硬件连接、波特率精确计算、FIFO高效使用以及FireWater协议封装进行了详细说明,并附有完整代码,帮助开发者快速实现实时数据可视化与波形分析,提升调试效率。

weixin_29172963的博客 150

转:SQL注入原理

转自:https://www.cnblogs.com/csguo/p/7499171.html 1.1.1 摘要 日前,国内最大的程序员社区优快云网站的用户数据库被黑客公开发布,600万用户的登录名及密码被公开泄露,随后又有多家网站的用户密码被流传于网络,连日来引发众多网民对自己账号、密码等互联网信息被盗取的普遍担忧。 网络安全成为了现在互联网的焦点,这也恰恰触动了每一位用户的神经,由于设...

u013155359的博客 194

优刻得首个「国产千卡智算集群」落地,支持智源千亿大模型训练

在人工智能引领的时代浪潮中,算力已成为技术进步与创新的核心驱动力。面对当下AI算力需求的飙升、高端AI芯片供应受限的挑战,加之OpenAI带来的技术封锁,唯有坚定不移的发展自主可控的国产技术方案,持续壮大国产智算集群规模,才能一路突破围追堵截,进一步促进国产大模型产业生态繁荣。集群还提供了完善高效的软件栈工具,集成多种主流的深度学习编程框架,通过测试验证,确保了对行业领先人工智能模型的高效支持,做到开箱即用,用户可快速投入模型的开发和部署,加速了其在人工智能领域的布局和发展。方面取得了显著成效。

Ucloud_TShare的博客 1473

转:SQL注入攻击的原理

转自:http://netsecurity.51cto.com/art/200808/87178.htm   对于Web应用来说,注射式攻击由来已久,攻击方式也五花八门,常见的攻击方式有SQL注射、命令注射以及新近才出现的XPath注射等等。本文将以SQL注射为例,在源码级对其攻击原理进行深入的讲解。 一、注射式攻击的原理  注射式攻击的根源在于,程序命令和用户数据(即用户输入)之间没有做到泾...

weixin_34137799的博客 77

深入解析 RLHF(Reinforcement Learning from Human Feedback)

随着人工智能(AI)技术的快速发展,大规模语言模型(如 OpenAI 的 GPT、Anthropic 的 Claude 等)已经能够生成高质量的文本。RLHF 是一种基于强化学习(Reinforcement Learning, RL)的优化方法,它结合人类反馈(Human Feedback)来训练奖励模型(Reward Model, RM),进而优化语言模型的生成策略。整个强化学习的目标是找到最优策略 π∗\pi^*π∗,使得模型在与环境交互的过程中,最大化累积奖励。RLHF 是一种结合强化学习(RL)

lqq 1771

【微服务】SpringBoot 对接飞书审批流程使用详解

SpringBoot 对接飞书审批流程使用详解

congge_study的博客 3771

【转】sql注入式攻击的原理及实例分析

转自:http://www.oecp.cn/hi/right_way/blog/2273 什么是sql注入呢? 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL注入式攻击. ...

wujay 250

Modbus协议介绍及解析

Modbus协议介绍及解析 1.Modbus简介 Modbus是一种广泛应用于工业控制领域串行通信协议,以其开放性、高可靠性、高效简单性、免费等优点,成为了工业领域通信协议的业界标准,是工业现场电子设备之间常用的连接方式。Modbus按其格式可分为Modbus-RTU,Modbus-ASCII,Modbus-TCP,其中前两者适用于串行通信控制网络中,例如RS485,RS232等,而Modbus-TCP主要应用于基于以太网TCP/IP通信的控制网络中。通过此协议,控制器相互之间、或控制器和其它设备之间进行通

逍遥子的博客 1万+

转:mybatis是防止SQL注入原理

SQL注入是一种很简单的攻击手段,但直到今天仍然十分常见。究其原因不外乎:No patch for stupid。为什么这么说,下面就以JAVA为例进行说明:假设数据库中存在这样的表:[java] view plain copytable user(  id   varchar(20)    PRIMARY KEY ,         name     varchar(20)           ...

oook 282

YOLOv8改进策略【Conv和Transformer】| CVPR-2023 BiFormer 稀疏自注意力,减少内存占用

本文记录的是利用双级路由注意力机制优化的目标检测网络模型。传统的复杂度高,随着输入空间分辨率增加,计算量呈平方增长,导致严重的可扩展性问题。而本文所使用的在获取全局信息的同时,通过区域到区域路由和令牌到令牌注意力的合理设计,减少了不必要的计算量。BiFormer: Vision Transformer with Bi-Level Routing Attention采用四阶段金字塔结构,与一些先进的视觉Transformer类似。在每个阶段,首先使用重叠的补丁嵌入(在第一阶段)或补丁合并模块(在第二到第四阶段

Limiiiing的博客 614

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

(转)SQL注入原理

原文地址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html SQL Injection:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。 具体来说,它是利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输...

weixin_30315723的博客 126

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

sql注入式攻击的原理及防范措施(转)

        SQL注入就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL注入式攻击.        SQL注入攻击的简单示例:          statement := "SELECT * FROM Users WHERE...

kai_vic2009的博客 317

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

(转)sql注入原理和过程

一、sql注入原理 程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患。用户可以提交一段数据库查询代码,根据程序返回的结果,获得某些他想得知的数据,这就是所谓的SQL Injection,即SQL注入。 受影响的系统:对输入的参数不进行检查和过滤的系统。 二、sql注入过程 1.找到链接我找的链接是:htt...

weixin_30457881的博客 76

SQL手工注入原理(一)

原理 服务器端程序将用户输入参数作为查询条件,直接拼接SQL语句,并将查询结果返回给客户端浏览器。 靶机 metasploitable2 DVWA ( mysql 数据库) 注入类型分类 主要分为 数字型 字符型 基于布尔注入 and 和 or 首先来分析下完整的SQL语句: select fist_name,surname from users where id="1" 为了方便,我把 "...

初学者L_言的博客 1442

Sql注入详解(原理篇)

SQL 注入漏洞非常复杂,区分各种数据库类型,提交方法,数据类型等注入,同样此类漏洞是WEB安全中严重的安全漏洞,学习如何利用,挖掘,修复也是很重要的。SQL 注入就是指 Web 应用程序对用户输入的数据合法性没有过滤或者是判断,攻击者可以在Web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。

Naive的博客 2万+

常见SQL注入类型及原理

SQL注入 Mysql基础 1、Mysql安装 这里我们直接使用phpstudy集成环境中的mysql 2、Mysql常用命令 (1)mysql本地连接 mysql -h localhost -uroot –proot 参数 说明 -h 表示数据库连接地址,连接本机可不填,直接mysql -uroot -p -u 表示要登录的用户 -p 表示使用密码登录 默认账/密:root/root 注:登录mysql时,-p后面不能有空格加密码,但-p空格,后面不加值是

1stPeak's Blog 6501

SQL注入攻击的原理以及如何防止SQL注入

主要原因是程序对用户输入数据的合法性没有判断和处理,导致攻击者可以在 Web 应用程序中事先定义好的 SQL 语句中添加额外的 SQL 语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步获取到数据信息。简而言之,SQL 注入就是在用户输入的字符串中加入 SQL 语句,如果在设计不良的程序中忽略了检查,那么这些注入进去的 SQL 语句就会被数据库服务器误认为是正常的 SQL 语句而运行,攻击者就可以执行计划外的命令或访问未被授权的数据。条件一般为真值表达式。

sweetser的博客 2020

【SQL注入01】SQL注入漏洞简介、原理及类型

1. 了解SQL注入漏洞的概念和危害; 2. 理解SQL注入漏洞产生的两个条件; 3. 了解SQL注入分类方式; 4. 加深SQL和数据库相关知识的理解。

Fighting_hawk的博客 5942

sql注入的原理详解

sql注入原理详解(一) 我们围绕以下几个方面来看这个问题: 1.什么是sql注入? 2.为什么要sql注入? 3.怎样sql注入? 1.什么是sql注入?   所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL...

牛不牛先生 8974

SQL注入原理

SQL Injection:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。它是利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。

qq_43036356的博客 3073

SQL注入原理简解

SQL注入原理简单介绍

weixin_49182737的博客 4063
上一篇: 具体实例教你如何做LoadRunner结果分析
下一篇: 一个Web页面的问题分析
honckly
博客等级 码龄20年 3粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值