ichirin2501's diary

いっちりーん。

mayahu32さんのセキュリティ&プログラミングキャンプ2011感想文を読んだよー

http://d.hatena.ne.jp/mayahu32/20110816/1313505335
id:mayahu32さんのセキュリティ&プログラミングキャンプ2011感想文を読ませてもらいました。
以下ネタバレになります、ご注意ください。


解析環境 Windows7 64bit
使用したツール IDA Pro6.0 Demo, BZエディタ


日記にあるURL http://mayahu32.xxxxxxxx.jp/crackme.txt にアクセスすると、
空白で区切られた16進数表記の羅列が表示されます。
これをそのままバイナリ値としてファイルを作成します

パケット情報らしき文字列が見えます。
この時点でWiresharkに放り投げたら読み込めないと言われてしまいました。
対応してないだとー何のファイルだー?と思って、
http://www.garykessler.net/library/file_sigs.html
こちらでシグネチャを調べてみると、WinDump (winpcap) capture file (Windows)
WinDumpというのはtcpdumpwindows版らしい、よくしりません。
WinDumpをダウンロードして読み込もうと思ったのですが動きませんでした。んー?
仕方がないので、目grep*1

私の目grep検索の網にかかったのがこれです。
よく考えたらstringsコマンドでも良かったですね。
どうして目grepに走ったのか…。


というわけで、
http://mayahu32.sarashi.com/crackme.txt にアクセスします。
ダウンロードしたcrackme.txtをバイナリエディタで開いてみます。

画面後ろに関数名らしき英字が並んでおり、
文字列が逆になっているのがわかります。
KERNEL32.DLL, LoadLibraryA, etc...
どうみても実行ファイルです、本当に(ry
末端のバイナリを確認すると、

やることはわかりましたね。
バイナリのデータ列を反転させるだけです。


修正した実行ファイルを動かしてみます。

どうやら入力待ちのようです。
てきとーに入力しても何も起こらず終了してしまいます。
期待するデータを入力しろってことでしょうか。
内部処理を解析する、前に、
UPX*2でパックされているので、アンパック処理を行います
(http://upx.sourceforge.net/ から upx307w.zip を落としてきた)
あんぱっく!
(アンパック技術を磨く女子高生たちの物語、とか同人誌で出ないかな)

正常にアンパックできたようです。
アンパックされた実行ファイルを動かしてみます。

…どういうことだってばよ。
IDAPro先生に静的解析をお願いします。
異常終了してるので、正しいコードではない可能性があります コードは正しい
Win7が原因?未確認 下記のコメントにて




先に申し上げると、ここの想定解はわかっていません。
最後の画像の右側が本命なのですが、
この処理に辿り着くにはgetsのバッファオーバーフローを利用して、
リターンアドレスを変更するなりしないと無理なのでは?
と、考えたところで挫折*3
想定解法については下記に追記したよ
異常終了と何か関係あるのかなーと思い、
Immunity debuggerを使って、UPXをマニュアルアンパックし、
処理を比較しても同じでした。ぐぬぬ…。
本命の処理は長いですが、以下のようになっていました。

.text:004010E1                 push    offset aA       ; "a"
.text:004010E6                 call    sub_4016FC
.text:004010EB                 add     esp, 4
.text:004010EE                 push    offset asc_40C084 ; "H"
.text:004010F3                 call    sub_4016FC
.text:004010F8                 add     esp, 4
.text:004010FB                 push    offset aR       ; "R"
.text:00401100                 call    sub_4016FC
.text:00401105                 add     esp, 4
.text:00401108                 push    offset a0       ; "0"
.text:0040110D                 call    sub_4016FC
.text:00401112                 add     esp, 4
.text:00401115                 push    offset aC       ; "c"
.text:0040111A                 call    sub_4016FC
.text:0040111F                 add     esp, 4
.text:00401122                 push    offset aD       ; "D"
.text:00401127                 call    sub_4016FC
.text:0040112C                 add     esp, 4
.text:0040112F                 push    offset aO       ; "o"
.text:00401134                 call    sub_4016FC
.text:00401139                 add     esp, 4
.text:0040113C                 push    offset aV       ; "v"
.text:00401141                 call    sub_4016FC
.text:00401146                 add     esp, 4
.text:00401149                 push    offset asc_40C0A0 ; "L"
.text:0040114E                 call    sub_4016FC
.text:00401153                 add     esp, 4
.text:00401156                 push    offset a2       ; "2"
.text:0040115B                 call    sub_4016FC
.text:00401160                 add     esp, 4
.text:00401163                 push    offset a1       ; "1"
.text:00401168                 call    sub_4016FC
.text:0040116D                 add     esp, 4
.text:00401170                 push    offset asc_40C0AC ; "h"
.text:00401175                 call    sub_4016FC
.text:0040117A                 add     esp, 4
.text:0040117D                 push    offset aE       ; "e"
.text:00401182                 call    sub_4016FC
.text:00401187                 add     esp, 4
.text:0040118A                 push    offset aW       ; "W"
.text:0040118F                 call    sub_4016FC
.text:00401194                 add     esp, 4
.text:00401197                 push    offset asc_40C0B8 ; "F"
.text:0040119C                 call    sub_4016FC
.text:004011A1                 add     esp, 4
.text:004011A4                 push    offset aO_0     ; "o"
.text:004011A9                 call    sub_4016FC
.text:004011AE                 add     esp, 4
.text:004011B1                 push    offset aD_0     ; "d"
.text:004011B6                 call    sub_4016FC
.text:004011BB                 add     esp, 4
.text:004011BE                 push    offset aT       ; "T"
.text:004011C3                 call    sub_4016FC
.text:004011C8                 add     esp, 4
.text:004011CB                 push    offset aM       ; "M"
.text:004011D0                 call    sub_4016FC
.text:004011D5                 add     esp, 4
.text:004011D8                 push    offset aY       ; "y"
.text:004011DD                 call    sub_4016FC
.text:004011E2                 add     esp, 4
.text:004011E5                 push    offset asc_40C0D0 ; "L"
.text:004011EA                 call    sub_4016FC
.text:004011EF                 add     esp, 4
.text:004011F2                 push    offset aN       ; "n"
.text:004011F7                 call    sub_4016FC
.text:004011FC                 add     esp, 4
.text:004011FF                 push    offset aN_0     ; "N"
.text:00401204                 call    sub_4016FC
.text:00401209                 add     esp, 4
.text:0040120C                 push    offset asc_40C0DC ; "h"
.text:00401211                 call    sub_4016FC
.text:00401216                 add     esp, 4
.text:00401219                 push    offset aC_0     ; "c"
.text:0040121E                 call    sub_4016FC
.text:00401223                 add     esp, 4
.text:00401226                 push    offset aM_0     ; "m"
.text:0040122B                 call    sub_4016FC
.text:00401230                 add     esp, 4
.text:00401233                 push    offset asc_40C0E8 ; "F"
.text:00401238                 call    sub_4016FC
.text:0040123D                 add     esp, 4
.text:00401240                 push    offset aZ       ; "z"
.text:00401245                 call    sub_4016FC
.text:0040124A                 add     esp, 4
.text:0040124D                 push    offset aA_0     ; "a"
.text:00401252                 call    sub_4016FC
.text:00401257                 add     esp, 4
.text:0040125A                 push    offset aG       ; "G"
.text:0040125F                 call    sub_4016FC
.text:00401264                 add     esp, 4
.text:00401267                 push    offset aK       ; "k"
.text:0040126C                 call    sub_4016FC
.text:00401271                 add     esp, 4
.text:00401274                 push    offset aU       ; "u"
.text:00401279                 call    sub_4016FC
.text:0040127E                 add     esp, 4
.text:00401281                 push    offset aY_0     ; "Y"
.text:00401286                 call    sub_4016FC
.text:0040128B                 add     esp, 4
.text:0040128E                 push    offset a2_0     ; "2"
.text:00401293                 call    sub_4016FC
.text:00401298                 add     esp, 4
.text:0040129B                 push    offset a9       ; "9"
.text:004012A0                 call    sub_4016FC
.text:004012A5                 add     esp, 4
.text:004012A8                 push    offset aT_0     ; "t"
.text:004012AD                 call    sub_4016FC
.text:004012B2                 add     esp, 4
.text:004012B5                 push    offset asc_40C110 ; "L"
.text:004012BA                 call    sub_4016FC
.text:004012BF                 add     esp, 4
.text:004012C2                 push    offset a3       ; "3"
.text:004012C7                 call    sub_4016FC
.text:004012CC                 add     esp, 4
.text:004012CF                 push    offset aJ_0     ; "J"
.text:004012D4                 call    sub_4016FC
.text:004012D9                 add     esp, 4
.text:004012DC                 push    offset asc_40C11C ; "l"
.text:004012E1                 call    sub_4016FC
.text:004012E6                 add     esp, 4
.text:004012E9                 push    offset aC_1     ; "c"
.text:004012EE                 call    sub_4016FC
.text:004012F3                 add     esp, 4
.text:004012F6                 push    offset aG_0     ; "G"
.text:004012FB                 call    sub_4016FC
.text:00401300                 add     esp, 4
.text:00401303                 push    offset a9_0     ; "9"
.text:00401308                 call    sub_4016FC
.text:0040130D                 add     esp, 4
.text:00401310                 push    offset aY_1     ; "y"
.text:00401315                 call    sub_4016FC
.text:0040131A                 add     esp, 4
.text:0040131D                 push    offset aD_1     ; "d"
.text:00401322                 call    sub_4016FC
.text:00401327                 add     esp, 4
.text:0040132A                 push    offset asc_40C134 ; "F"
.text:0040132F                 call    sub_4016FC
.text:00401334                 add     esp, 4
.text:00401337                 push    offset a9_1     ; "9"
.text:0040133C                 call    sub_4016FC
.text:00401341                 add     esp, 4
.text:00401344                 push    offset aZ_0     ; "z"
.text:00401349                 call    sub_4016FC
.text:0040134E                 add     esp, 4
.text:00401351                 push    offset aC_2     ; "c"
.text:00401356                 call    sub_4016FC
.text:0040135B                 add     esp, 4
.text:0040135E                 push    offset aG_1     ; "G"
.text:00401363                 call    sub_4016FC
.text:00401368                 add     esp, 4
.text:0040136B                 push    offset aN_1     ; "N"
.text:00401370                 call    sub_4016FC
.text:00401375                 add     esp, 4
.text:00401378                 push    offset asc_40C14C ; "h"
.text:0040137D                 call    sub_4016FC
.text:00401382                 add     esp, 4
.text:00401385                 push    offset aB       ; "b"
.text:0040138A                 call    sub_4016FC
.text:0040138F                 add     esp, 4
.text:00401392                 push    offset asc_40C154 ; "X"
.text:00401397                 call    sub_4016FC
.text:0040139C                 add     esp, 4
.text:0040139F                 push    offset aA_1     ; "A"
.text:004013A4                 call    sub_4016FC
.text:004013A9                 add     esp, 4
.text:004013AC                 push    offset aY_2     ; "y"
.text:004013B1                 call    sub_4016FC
.text:004013B6                 add     esp, 4
.text:004013B9                 push    offset aM_1     ; "M"
.text:004013BE                 call    sub_4016FC
.text:004013C3                 add     esp, 4
.text:004013C6                 push    offset aD_2     ; "D"
.text:004013CB                 call    sub_4016FC
.text:004013D0                 add     esp, 4
.text:004013D3                 push    offset aE_0     ; "E"
.text:004013D8                 call    sub_4016FC
.text:004013DD                 add     esp, 4
.text:004013E0                 push    offset asc_40C16C ; "x"
.text:004013E5                 call    sub_4016FC
.text:004013EA                 add     esp, 4
.text:004013ED                 push    offset asc_40C170 ; "L"
.text:004013F2                 call    sub_4016FC
.text:004013F7                 add     esp, 4
.text:004013FA                 push    offset aN_2     ; "n"
.text:004013FF                 call    sub_4016FC
.text:00401404                 add     esp, 4
.text:00401407                 push    offset aR_0     ; "R"
.text:0040140C                 call    sub_4016FC
.text:00401411                 add     esp, 4
.text:00401414                 push    offset a4       ; "4"
.text:00401419                 call    sub_4016FC
.text:0040141E                 add     esp, 4
.text:00401421                 push    offset aD_3     ; "d"
.text:00401426                 call    sub_4016FC
.text:0040142B                 add     esp, 4
.text:0040142E                 push    offset aA_2     ; "A"
.text:00401433                 call    sub_4016FC
.text:00401438                 add     esp, 4
.text:0040143B                 push    offset asc_40C188 ; "="
.text:00401440                 call    sub_4016FC
.text:00401445                 add     esp, 4
.text:00401448                 push    offset asc_40C18C ; "=\n"
.text:0040144D                 call    sub_4016FC
.text:00401452                 add     esp, 4
.text:00401455
.text:00401455 loc_401455:                             ; CODE XREF: _main+63j
.text:00401455                                         ; _main+6Ej ...
.text:00401455                 call    sub_408B36
.text:0040145A                 xor     eax, eax
.text:0040145C                 mov     esp, ebp
.text:0040145E                 pop     ebp
.text:0040145F                 retn

文字を順に並べると、
aHR0cDovL21heWFodTMyLnNhcmFzaGkuY29tL3JlcG9ydF9zcGNhbXAyMDExLnR4dA==
base64エンコードされた文字列になっています*4
デコードすると以下の文字列が表示されます。
http://mayahu32.sarashi.com/report_spcamp2011.txt
アクセスすると、またしても16進数の羅列が表示されます。
e382bbe38397...
うーん、説明に困るのですが見た感じが 日本語のUTF-8 ですw
一応、これで日本語の感想文に辿り着けます。
セキュリティ&プログラミングキャンプおつかれさまでした!


(悔いが残るなー、解析部分)


追記 2011/8/18 00:06
やはり想定解はバッファオーバーフローだったようです(本人の解説記事より)。
id:yzxさんからご指摘を受け、
ファイルヘッダのCharacteristicsにIMAGE_FILE_RELOCS_STRIPPEDフラグを
立てれば落ちないし、アドレス変動も起きません!
さっそく想定解で解いてみました。
バッファのサイズはアセンブリからてきとーに計算です。
飛ぶべきアドレスは本命処理の先頭 0x004010E1
printfコマンドを使うためにCygwinから実行
※実行ファイルはフラグを編集済み

$ printf "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbb\xE1\x10\x40\x00" | ./mayahu32_crackme.exe
-----------------------------
CrackMe to #spcamp2011 report
-----------------------------
---------created by @mayahu32
aHR0cDovL21heWFodTMyLnNhcmFzaGkuY29tL3JlcG9ydF9zcGNhbXAyMDExLnR4dA==

おしまい!

*1:バイナリアンの技術?w。私のは「なんちゃって目grep

*2:実行ファイルを実行可能な状態で圧縮するツールの1つ、UPXは有名

*3:起動する度にアドレスが変わってしまうorz

*4:base64はパディングのために記号として=を使用するという特徴があります