見出し画像

Metasploitable2をLXDコンテナで動かす(docker→LXDのイメージ変換)

ずっとDockerを使ったハッキングラボ環境を作ってたりしてましたが、コンテナ使いとしてはLXDもずっと気になっていて、どのくらい違いがでるだろうかとか、そもそもちゃんと構築できるのか?ってのを試してみたくなりました。
LXD自体は使ったことあったけど詳しくは触れてないので、手探りですがやってみます。

LXDのインストール

環境は以下の通りです。

  • OS:Ubuntu20.04 LTS(VirtualBox7上)

  • メモリ:4GB

  • CPU:2コア

  • ストレージ:20GB

VirtualBox7も出てたのでせっかくだからと思って使ってみましたが結構見た目もちょいちょい変わってる感じですね。なお、ストレージは後々OpenVASのコンテナも作る時に足りなくなって40GBまで拡張しました。

VirtualBox 7.0

Appleシリコン対応との話もまだβで全然使えないとかなんとか。手元に新しいMac Studio来たら試してみよう(近日予定)
とりあえず一通りアップデートとかして、snapを使ってLXDをインストールして、初期設定みたいなのがあるので言われるがまま(ほぼdefault)設定しました。

$ sudo snap install lxd
lxd 5.7-c62733b from Canonical✓ installed

$ sudo lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: 
Do you want to configure a new storage pool? (yes/no) [default=yes]: 
Name of the new storage pool [default=default]: 
Name of the storage backend to use (cephobject, dir, lvm, zfs, btrfs, ceph) [default=zfs]: 
Create a new ZFS pool? (yes/no) [default=yes]: 
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]: 
Size in GiB of the new loop device (1GiB minimum) [default=19GiB]: 
Would you like to connect to a MAAS server? (yes/no) [default=no]: 
Would you like to create a new local network bridge? (yes/no) [default=yes]: 
What should the new bridge be called? [default=lxdbr0]: 
What IPv4 address should be used? (CIDR subnet notation, “autoor “none”) [default=auto]: 
What IPv6 address should be used? (CIDR subnet notation, “autoor “none”) [default=auto]: none
Would you like the LXD server to be available over the network? (yes/no) [default=no]: 
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]: 
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: 

<試しにコンテナ起動・確認>
$ lxc launch ubuntu:22.04
Creating the instance
Instance name is: settled-albacore            
Starting settled-albacore

$ lxc list
+------------------+---------+-----------------------+------+-----------+-----------+
|       NAME       |  STATE  |         IPV4          | IPV6 |   TYPE    | SNAPSHOTS |
+------------------+---------+-----------------------+------+-----------+-----------+
| settled-albacore | RUNNING | 10.239.250.172 (eth0) |      | CONTAINER | 0         |
+------------------+---------+-----------------------+------+-----------+-----------+



<lxdの操作は以下の通り(つかったものメインに抜粋)>
lxc
Description:
  Command line client for LXD
Usage:
  lxc [command]
Available Commands:
  config      Manage instance and server configuration options
  console     Attach to instance consoles
  copy        Copy instances within or in between LXD servers
  delete      Delete instances and snapshots
  exec        Execute commands in instances
  export      Export instance backups
  image       Manage images
  import      Import instance backups
  info        Show instance or server information
  launch      Create and start instances from images
  list        List instances
  move        Move instances within or in between LXD servers
  network     Manage and attach instances to networks
  restart     Restart instances
  snapshot    Create instance snapshots
  start       Start instances
  stop        Stop instances

なんとなく、こんなことできるのかな?ってのはコマンド見たらわかりますよね。なんでLXDなのにコマンドはlxcなんだ!っていうのは多分最初はみんな通る道としても。そもそもLXDとは・・・・みたいな話になっちゃうので割愛。

Metasploitable2イメージの作成

さてこれが今回のメインにして最大の難関であろうことですが、そもそもどうやってイメージ作ればいいんだ?LXDで通常使えるイメージはこんな感じみたいです。

よく見るLinux distributionがズラリ・・・あ、Kaliもある。これは検証としてはかなり嬉しいですね。まあでもコンテナだから多分また立ち上げ後にいっぱいダウンロードしてインストールして・・・ってことなのでしょう(後に確認したけどやっぱそうでした)
とりあえず考えたのはDockerのイメージをそのまま使えないのかな、ってことですが、まあ無理なのでなんとか変換させてあげないといけません。
調べたところ、コンテナのイメージって一応こういうオープンフォーマットが定義されてるみたいです。

ということで、このOCI準拠イメージをLXCコンテナに変換できるみたい。ってことでDockerのイメージもこれでいけるんじゃなかろうか。

調べ物ばかりですが、ものは試しでこのコマンドでやってみたのですが・・・。

$ lxc-create metasploitable2 -t oci -- --url docker://tleemcjr/metasploitable2
Command 'lxc-create' not found, but can be installed with:
sudo apt install lxc-utils

ねーよ。だそうなので追加して再チャレンジしてもなんか怒られたので、sudoでやってみたら今度はどうやらskopeoがないよってことみたい。じゃあ入れりゃあいいじゃんってことなんだけど、ここは結構苦労した。これ、ubuntu20.04だとpodmanのリポジトリ追加しないと難しいのかも。ってことで、下記参照。最初公式っぽいとこのコマンドチュートリアルのままやったらなぜかうまくいかなかったので、別のとこからコピー。

結局のところ、lxc-utils、skopeo、umoci、jqという4ツールが必要でした。これ、あとで確認したところubuntu22.04は標準のリポジトリで普通にいけます。ということで改めて実行。

$ sudo lxc-create metasploitable2 -t oci -- --url docker://tleemcjr/metasploitable2
Getting image source signatures
Copying blob 7aee18c98c59 done  
Copying blob da9129f8f7ad done  
Copying blob b1494b474174 done  
Copying blob 84da87a98ea3 done  
Copying blob 47fb2fcd8445 done  
Copying blob 8b6e3bfdb228 done  
Copying blob 36d703894057 done  
Copying blob 43cf3a9e2a40 done  
Copying config 9a95a14214 done  
Writing manifest to image destination
Storing signatures
Unpacking the rootfs
jq: error (at <stdin>:0): Cannot iterate over null (null)

む。最後jqのエラー出てる。でも、なんかそのほかはちゃんと動いてるっぽい。これはもしかしたらもしかする。コンテナの実態がどこにあるのか確認してみたところ・・・

/var/lib/lxc# ls
metasploitable2

なんかできてる!ので、起動してみます。

# lxc-start metasploitable2
# lxc-ls --fancy --fancy-format=name,state,ipv4,pid,ram
NAME            STATE   IPV4 PID   RAM    
metasploitable2 RUNNING -    17039 0.00MB 

起動しましたね。LXCコンテナとして動いてます。これをLXDのコンテナにしてあげるために、下記コマンドを実行します(実行前にコンテナはlxc-stopで停止)。マイグレーションについて詳しくは下記参照。goのインストールが必要です。この辺りでも、標準リポジトリで対応可能なubuntu22.04のほうが楽かも?

# lxd.lxc-to-lxd --containers metasploitable2
Parsing LXC configuration
Checking for unsupported LXC configuration keys
Skipping container 'metasploitable2': Found unsupported config key 'lxc.init.cwd'

む。エラー。サポートしてないコンフィグがあるよってことみたいなので、lxcコンテナのコンフィグを覗いてみます。

/var/lib/lxc/metasploitable2# cat config 
# Template used to create this container: /usr/share/lxc/templates/lxc-oci
# Parameters passed to the template: --url docker://tleemcjr/metasploitable2
# For additional config options, please look at lxc.container.conf(5)

# Uncomment the following line to support nesting containers: #lxc .include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)

lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:3d:ff:84
lxc.rootfs.path = dir:/var/lib/lxc/metasploitable2/rootfs
lxc.execute.cmd = '"sh" "-c" "/bin/services.sh && bash" '
lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed
lxc.environment = 
lxc.include = /usr/share/lxc/config/common.conf
lxc.include = /usr/share/lxc/config/oci.common.conf
lxc.uts.name = metasploitable2
lxc.init.uid = 0
lxc.init.gid = 0
lxc.init.cwd = /

ありますねlxc.init.cwd = /の行。これどうしたらいいのだろう。適切な対応がわかりませんが、めんどくさいからとりあえず消しちゃえ。
結局のところ、最終的にlxc.init.cwd、lxc.execute.cmd、lxc.init.uid、lxc.init.gidの行を削除して実行したら進みました。execute.cmdは動いて欲しかったけど、まあコンテナ起動後に直接実行してあげればいいか。

$ sudo lxd.lxc-to-lxd --containers metasploitable2
Parsing LXC configuration
Checking for unsupported LXC configuration keys
Checking for existing containers
Checking whether container has already been migrated
Validating whether incomplete AppArmor support is enabled
Validating whether mounting a minimal /dev is enabled
Validating container rootfs
Processing network configuration
Processing storage configuration
Processing environment configuration
Processing container boot configuration
Processing container apparmor configuration
Processing container seccomp configuration
Processing container SELinux configuration
Processing container capabilities configuration
Processing container architecture configuration
Couldn't find container architecture, assuming native
Creating container
Transferring container: metasploitable2: 502MB (5.12MB/s)

できた!あとあと試してみたところ、Ubuntu22.04ではCan't find container archtectureとかなんとか出てたので、configファイルにxlc.arc = x86_64という行を追加することで進め・・・ません。最後のTransferring…に進まない。なんでだろう。go関係かな。ともかく、コンテナがSTOPPEDの状態でできていたので、起動とネットワークの設定を入れてみました。

$ lxc list
+------------------+---------+---------------------+------+-----------+-----------+
|       NAME       |  STATE  |        IPV4         | IPV6 |   TYPE    | SNAPSHOTS |
+------------------+---------+---------------------+------+-----------+-----------+
| desktop          | STOPPED |                     |      | CONTAINER | 0         |
+------------------+---------+---------------------+------+-----------+-----------+
| metasploitable2  | RUNNING | 10.88.157.92 (eth1) |      | CONTAINER | 0         |
+------------------+---------+---------------------+------+-----------+-----------+
| ubuntu-container | RUNNING | 10.88.157.91 (eth0) |      | CONTAINER | 0         |
+------------------+---------+---------------------+------+-----------+-----------+

動いてます。eth1になってるのは特に意味なくインターフェースの追加でやってみたってだけで、サービス起動のスクリプトもコンテナ内の/bin/services.shを手動で動かしてあげたら問題なく起動しました。このあたりはおいおい設定してみます。

webもちゃんと起動

Dockerと違ってシステムコンテナとして起動してくれるってことは、新しくコンテナ作っていろんなwebサービス動かしたりしたほうがより現実的な環境かもしれませんが、とりあえずDockerと同等の環境を作れるという意味では今回の目的は達成できました。物理や仮想マシンからのマイグレーションもツールがあるようなので、試してみてちゃんと脆弱性の再現とか問題ないかを比較できたらと思ってます。

ちゃんと脆弱性検査もできました

この記事が気に入ったらサポートをしてみませんか?