您的位置 首页 php

渗透测试之SQL注入(一)

本文原载于公众号:猪猪谈安全

作者:随风kali

什么是SQL注入

SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。

这里以一个PHP的SQL语句代码为例

$SQL = “select * from ‘某字段’ where id = $id”;

由于这里的参数id可以控制,且这个id被带进了数据库查询,所以一些意图不轨的人可以通过拼接SQL语句来进行攻击。

产生SQL注入需要的条件

SQL注入的产生需要两个条件

1.我们传递给后端的参数是可以控制的

2.参数内容会被带入到数据库查询

验证是否存在SQL注入

还是以这个代码为例 $SQL = “select * from ‘某字段’ where id = $id”;

在这里我们可以控制的参数是id这个参数,所以我们输入1‘的时候。这时查询语句执行的内容就为

select * from ‘某字段’ where id = 1’

由于后面有一个单引号,这样的语句不符合数据库语法的规范,所以会报错,从而判断出该处是否存在SQL注入。

SQL注入的分类

SQL注入分为很多种,有联合注入、布尔注入、报错注入、时间注入、堆叠注入、二次注入、宽字节注入、cookie注入等等等。当然这些注入所产生的原理都是一样,如上文所讲。在接下来的文章呢,笔者也会对这些注入结合实例进行全部写出来。

SQL注入的防御

方案一

采用预编译技术

INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)

使用预编译的SQL语句,SQL语句的语义不会是不会发生改变的。预编译语句在创建的时候就已经将指定的SQL语句发送给了DBMS,完成了解析,检查,编译等工作,所以攻击者无法改变SQL语句的结构,只是把值赋给?,然后将?这个变量传给SQL语句。当然还有一些通过预编译绕过某些安全防护的操作,大家感兴趣可以去搜索一下。

方案二

严格控制数据类型

在java、c等强类型语言中一般是不存在数字型注入的,因为在接受到用户输入id时,代码一般会做一个int id 的数据类型转换,假如我们输入的是字符串的话,那么这种情况下,程序就会报错。但是在PHP、ASP这些没有强调处理数据类型的语言,一般我们看到的接收id的代码都是如下等代码。

$id = $_GET[‘id’];

$SQL = “select * from ‘某字段’ where id = $id;”;

这样的代码攻击者就可以通过构造id参数运用联合查询等手法进行SQL注入,假如这里我们加入一个检查数字类型函数is_numeric()这样就可以防止数字型注入了。

方案三

对特殊的字符进行转义

数字型注入可以通过检查数据类型防止,但是字符型不可以,那么怎么办呢,最好的办法就是对特殊的字符进行转义了。比如在MySQL中我们可以对” ‘ “进行转义,这样就防止了一些恶意攻击者来闭合语句。当然我们也可以通过一些安全函数来转义特殊字符。如addslashes()等,但是这些函数并非一劳永逸,攻击者还可以通过一些特殊的方式绕过。

文章来源:智云一二三科技

文章标题:渗透测试之SQL注入(一)

文章地址:https://www.zhihuclub.com/44065.shtml

关于作者: 智云科技

热门文章

评论已关闭

33条评论

  1. She carried three shopping bags plus her purse, while Eugenie, her dark hair pulled into a casual bun, was empty handed

  2. Serious Use Alternative 1 cimetidine will increase the level or effect of tacrolimus by affecting hepatic intestinal enzyme CYP3A4 metabolism

  3. CD44 has been described as a cancer stem cell marker in breast, ovarian, pancreatic, prostate, colon, gastric, and other cancer types see Li et al, 2007, and Takaishi et al, 2009

  4. I been reading and shows that one of the reason could be by C reactive protein being high out of the range 1 The addition of a taxane to an anthracycline containing regimen, whether after or concurrently with anthracycline treatment, further reduces the risk of relapse

  5. The GMC said he did not have the ethical approval or relevant qualifications for such tests In the LORIS and LORD trials, wide bore vacuum assisted biopsy is mandated, rather than 14 or 16 g core biopsy alone, because the risk of an unexpected invasive carcinoma in patients when DCIS is been diagnosed on core biopsy i

  6. Pattern of progression after stereotactic body radiotherapy for oligometastatic prostate cancer nodal recurrences Identify theft can start with a weak password, since hacking into an online account can lead to bank account information and other personal details such as birth dates and credit card numbers

  7. All these properties make rifaximin a good candidate to treat various gastrointestinal diseases A potential limitation of this study was that patients self selected at the two locations and could not be randomized

  8. Some of the side effects of Clomid include the following I suppose what I am saying is that it s not really possible to say that something is safe or that certain actions will eventually cause dementia

  9. The hypothesis that there was a significant difference between the reduced PH model for DR based on patient age, pathologic tumor size, local tumor grade, and treatment versus a full PH model, in which the RS was also included, was tested in the prospectively defined primary analysis of N0, hormone receptor positive patients treated either with tamoxifen or anastrozole

  10. F Relative mRNA levels of indicated genes from either control N 3 or SPDEF N 3 tumor enteroids were quantitated by RT qPCR after SPDEF induction in vitro by doxycycline 2 Ојg ml for 72h

  11. Primary mammospheres were collected for generation of secondary mammospheres as described previously 32 Enhanced AMPA Receptor Mediated Excitation And Attenuated 5 HT 1A Autoreceptor Mediated Inhibition in DRN 5 HT Neurons

  12. Insulin, which plays a job in managing blood glucose, has an unfortunate relationship with nitric oxide sparkling water good for blood sugar How To Prevent Diabetes production in blood vessels Nitric oxide is essential to blood vessels as a outcome of it is doubtless considered one of the very few methods to expand the scale of the blood vessels and decrease blood pressure I m a big fan of blood sugar testing and this summer I spent a month using a steady glucose monitor from an excellent company called Nutrisense This tool is very helpful when evaluating meals you want to embody in your total wholesome way of life plan

  13. Tell your doctor right away if any of these unlikely but serious side effects occur mental mood changes, trouble speaking, clumsiness, trouble walking, decreased increased interest in sex, tremor, trouble urinating, sleep disturbances

  14. But that s not always the case It is likely that differences in study design may account for the differences seen

  15. In most situations, optimal treatment is with the first dose of antibiotic administered in the emergency department or the physician s office

  16. Member question Since I ve started temp charting, I ve noticed I start light bleeding spotting, really, but red two to five days before real flow

  17. And what a story full of quixotic characters, therapeutic triumphs and setbacks, and recent historical events, with all the hubris and pathos of Greek tragedy Similar efficacy of tamoxifen was observed in murine peritoneal sepsis model by susceptible and MDR clinical isolates of A

网站地图