SQL注入基礎知識(1)
目錄
- SQL注入基礎知識(1)
- 1. MySQL基礎語法
- select
- where
- 注釋
- union
- order by
- group by
- 2. 常用函式
- length()
- if()
- sleep()
- concat()
- concat_ws()
- group_concat()
- left()
- floor()
- rand()
- user()
- database()
- current_user()
- version()
- @@datadir
- substring()
- ascii()
1. MySQL基礎語法
Show databases; 顯示所有資料庫
Show tables; 顯示所有表資訊
Select user(); 查詢用戶資訊
Select database(); 查詢資料庫名稱
Select version(); 查詢資料庫版本
Select @@basedir; 查詢資料庫基本路徑
select
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[OFFSET M ][LIMIT N]
-
查詢陳述句中你可以使用一個或者多個表,表之間使用逗號(,)分割,并使用WHERE陳述句來設定查詢條件,
-
SELECT 命令可以讀取一潭訓者多條記錄,
-
可以使用 WHERE 陳述句來包含任何條件,
-
可以通過OFFSET指定SELECT陳述句開始查詢的資料偏移量,默認情況下偏移量為0,
-
可以使用 LIMIT 屬性來設定回傳的記錄數,
例如:查詢sqli-labs中的security資料庫的users表
mysql> select * from security.users;
+----+----------+------------+
| id | username | password |
+----+----------+------------+
| 1 | Dumb | Dumb |
| 2 | Angelina | I-kill-you |
| 3 | Dummy | p@ssword |
| 4 | secure | crappy |
| 5 | stupid | stupidity |
| 6 | superman | genious |
| 7 | batman | mob!le |
| 8 | admin | admin |
| 9 | admin1 | admin1 |
| 10 | admin2 | admin2 |
| 11 | admin3 | admin3 |
| 12 | dhakkan | dumbo |
| 14 | admin4 | admin4 |
+----+----------+------------+
13 rows in set (0.00 sec)
where
SELECT field1, field2,...fieldN FROM table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
-
查詢陳述句中你可以使用一個或者多個表,表之間使用逗號(,)分割,并使用WHERE陳述句來設定查詢條件,
-
你可以在WHERE子句中指定任何條件,
-
你可以使用AND或者OR指定一個或多個條件,
-
WHERE子句也可以運用于SQL的 DELETE 或者 UPDATE 命令,
-
WHERE 子句類似于程式語言中的if條件,根據 MySQL 表中的欄位值來讀取指定的資料,
-
運算子也可以使用在where陳述句中,
| 運算子 | 描述 |
|---|---|
| = | 等號,檢測兩個值是否相等,如果相等回傳true |
| <> 或 != | 不等于,檢測兩個值是否相等,如果不相等回傳true |
| > | 大于號,檢測左邊的值是否大于右邊的值, 如果左邊的值大于右邊的值回傳true |
| < | 大于等于號,檢測左邊的值是否大于或等于右邊的值, 如果左邊的值大于或等于右邊的值回傳true |
| <= | 小于等于號,檢測左邊的值是否小于于或等于右邊的值, 如果左邊的值小于或等于右邊的值回傳true |
例如:
mysql> select * from users where username='Dumb';
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1 | Dumb | Dumb |
+----+----------+----------+
1 row in set (0.00 sec)
注釋
方法1:使用 #
mysql> #select * from security.users;
方法2:使用 /**/
mysql> /*select * from security.users;*/
方法3:使用 –
mysql> -- select * from security.users;
主要注意:使用 – 進行注釋,需要加一個空格進行分割,在注入中用到的 –+ 就是將空格轉換為URL編碼
union
UNION 運算子用于合并兩個或多個 SELECT 陳述句的結果集,其內部的每個 SELECT 陳述句必須擁有相同數量的列,列也必須擁有相似的資料型別,同時,每個 SELECT 陳述句中的列的順序必須相同,
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
注入時,使用id=-1或者超出其范圍的值 + union select可以獲得一些資訊,
例如:
mysql> select -1 union select database();
+----------+
| -1 |
+----------+
| -1 |
| security |
+----------+
2 rows in set (0.00 sec)
order by
order by 關鍵字用于對結果集按照一個列或者多個列進行排序,默認按照升序對記錄進行排序,
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
通過使用 order by + 數字 可以對資料庫表中的列數進行判斷,
例如:
mysql> select * from users order by 3;
+----+----------+------------+
| id | username | password |
+----+----------+------------+
| 8 | admin | admin |
| 9 | admin1 | admin1 |
| 10 | admin2 | admin2 |
| 11 | admin3 | admin3 |
| 14 | admin4 | admin4 |
| 4 | secure | crappy |
| 1 | Dumb | Dumb |
| 12 | dhakkan | dumbo |
| 6 | superman | genious |
| 2 | Angelina | I-kill-you |
| 7 | batman | mob!le |
| 3 | Dummy | p@ssword |
| 5 | stupid | stupidity |
+----+----------+------------+
13 rows in set (0.00 sec)
mysql> select * from users order by 4;
ERROR 1054 (42S22): Unknown column '4' in 'order clause'
group by
GROUP BY 陳述句用于結合 Aggregate 函式,根據一個或多個列對結果集進行分組,
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
例如:
首先,展示所有成員資訊
mysql> select * from student;
+------+-------+------+
| id | name | sex |
+------+-------+------+
| 1 | jack | 00 |
| 2 | bill | 00 |
| 3 | tom | 00 |
| 4 | niki | 11 |
| 5 | joan | 11 |
| 6 | linda | 11 |
+------+-------+------+
6 rows in set (0.00 sec)
根據性別欄位進行查詢
mysql> select * from student group by sex;
+------+------+------+
| id | name | sex |
+------+------+------+
| 1 | jack | 00 |
| 4 | niki | 11 |
+------+------+------+
2 rows in set (0.00 sec)
但是每組都顯示了一個資訊,不夠明顯
使用group_concat()來查看所有資訊
mysql> select group_concat(id,'~',name,'~',sex,'~') from student group by sex;
+---------------------------------------+
| group_concat(id,'~',name,'~',sex,'~') |
+---------------------------------------+
| 1~jack~00~,2~bill~00~,3~tom~00~ |
| 4~niki~11~,5~joan~11~,6~linda~11~ |
+---------------------------------------+
2 rows in set (0.00 sec)
2. 常用函式
length()
length用于獲取字串的長度
例如:
mysql> select length('hello,word');
+----------------------+
| length('hello,word') |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
if()
if函式根據條件的結果為true或false,來回傳一個值或者執行一段內容
IF(condition, value_if_true, value_if_false)
| 引數 | 描述 |
|---|---|
| condition | 必須具備的引數,是判斷條件陳述句 |
| value_if_true | 可選,當結果為true,執行該陳述句 |
| value_if_false | 可選,當結果為false,執行該陳述句 |
通常用在時間盲注上,
例如:
mysql> select if(1>2,'YES','NO');
+--------------------+
| if(1>2,'YES','NO') |
+--------------------+
| NO |
+--------------------+
1 row in set (0.00 sec)
sleep()
使用sleep,設定N秒,強制停留N秒,然后繼續執行后續步驟,
例如:
mysql> select sleep(5);
+----------+
| sleep(5) |
+----------+
| 0 |
+----------+
1 row in set (5.01 sec)
concat()
concat進行字串的連接,
回傳結果為連接引數產生的字串,如有任何一個引數為NULL ,則回傳值為 NULL,
CONCAT(str1,str2,……)
可以有一個或者多個的引數,
例如:
mysql> select concat('123','my','sql');
+--------------------------+
| concat('123','my','sql') |
+--------------------------+
| 123mysql |
+--------------------------+
1 row in set (0.00 sec)
concat_ws()
concat_ws() 代表 CONCAT With Separator ,是concat()的特殊形式,第一個引數是其它引數的分隔符,
分隔符的位置放是連接的兩個字串之間,分隔符可以是一個字串,也可以是其它引數,
CONCAT_WS(separator,str1,str2,…)
注意:如果分隔符為 NULL,則結果為 NULL,函式會忽略任何分隔符引數后的 NULL 值,
例如:
mysql> select concat_ws('~','hello','word');
+-------------------------------+
| concat_ws('~','hello','word') |
+-------------------------------+
| hello~word |
+-------------------------------+
1 row in set (0.00 sec)
group_concat()
group_concat函式回傳一個字串結果,該結果由分組中的值連接組合而成,
group_concat([DISTINCT] 要連接的欄位 [Order BY ASC/DESC 排序欄位] [Separator])
例如:
mysql> select group_concat(column_name,'~') from information_schema.columns where table_name='users' and table_schema='security';
+-------------------------------+
| group_concat(column_name,'~') |
+-------------------------------+
| id~,username~,password~ |
+-------------------------------+
1 row in set (0.01 sec)
left()
left函式是一個字串函式,從左開始截取字串,對應的是right()函式(從右開始截取字串),
left(str,length);
例如:
mysql> select left('hello,world',6);
+-----------------------+
| left('hello,world',6) |
+-----------------------+
| hello, |
+-----------------------+
1 row in set (0.00 sec)
floor()
floor函式用于向下取整,回傳整數值,
例如:
mysql> select floor(5.98);
+-------------+
| floor(5.98) |
+-------------+
| 5 |
+-------------+
1 row in set (0.00 sec)
rand()
rand(N)函式用于產生0~1之間的亂數,
如果一個整數引數N被指定,它被當做種子值使用(用于產生一個可重復的數值):
例如:
mysql> select rand();
+---------------------+
| rand() |
+---------------------+
| 0.14505084896930573 |
+---------------------+
1 row in set (0.00 sec)
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.5408434202343629 |
+--------------------+
1 row in set (0.00 sec)
mysql> select rand(1);
+---------------------+
| rand(1) |
+---------------------+
| 0.40540353712197724 |
+---------------------+
1 row in set (0.00 sec)
mysql> select rand(1);
+---------------------+
| rand(1) |
+---------------------+
| 0.40540353712197724 |
+---------------------+
1 row in set (0.00 sec)
user()
user用來顯示當前用戶名
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
database()
database用來顯示當前所用資料庫
mysql> select database();
+------------+
| database() |
+------------+
| security |
+------------+
1 row in set (0.00 sec)
current_user()
current_user可以查詢當前用戶名、可以用來查看權限
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
version()
version()用來查看資料庫的版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.53 |
+-----------+
1 row in set (0.00 sec)
@@datadir
@@datadir用來查看資料庫路徑
mysql> select @@datadir;
+-------------------------------------+
| @@datadir |
+-------------------------------------+
| D:\PHPstudy\PHPTutorial\MySQL\data\ |
+-------------------------------------+
1 row in set (0.00 sec)
substring()
substring用來截取欄位
substring(str, pos, length)
| 引數 | 描述 |
|---|---|
| str | 被截取字串 |
| pos | 從第N位開始截取,起始是從1開始 |
| length | 截取長度,從第N位開始截取到結尾,該引數可以省略 |
例如:
mysql> select substring('hello,world',1,1);
+------------------------------+
| substring('hello,world',1,1) |
+------------------------------+
| h |
+------------------------------+
1 row in set (0.00 sec)
mysql> select substring('hello,world',3);
+----------------------------+
| substring('hello,world',3) |
+----------------------------+
| llo,world |
+----------------------------+
1 row in set (0.00 sec)
ascii()
可以獲取字符的ASCII碼,只能獲取一個字符,
注意:如果引數為長度大于1的字串,只獲取第一個字符的ASCII碼,
mysql> select ascii('h');
+------------+
| ascii('h') |
+------------+
| 104 |
+------------+
1 row in set (0.00 sec)
mysql> select ascii('hello');
+----------------+
| ascii('hello') |
+----------------+
| 104 |
+----------------+
1 row in set (0.00 sec)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/235701.html
標籤:其他
下一篇:求購一個銀行信用卡的復雜存過
