今回からネットワークの理解を進めていきます。
今回は、VirtualBox + Ubuntu 22.04 で、環境構築していきます。
それでは、やっていきます。
はじめに
まずは、パケットキャプチャするために、Wireshark をインストールします。
その後、いろんなツールを使って、VirtualBox のネットワークが、どういう構成になっているのかを調べていきたいと思います。
Wiresharkのインストール
コマンドラインからインストールしていきます。
$ sudo apt update
$ sudo apt install wireshark
次のような画面になります。root権限ではなくても、Wireshark のグループのメンバに、パケットをキャプチャさせるかどうかを聞いています。
その方が便利なので、はいを選択します。
Wiresharkの設定
インストールが完了したら、現在のユーザを Wireshark のグループに追加します。
$ sudo usermod -a -G wireshark daisuke
早速、Wireshark を起動してみましたが、Wireshark のグループ追加が反映されてませんでした。再起動します。
再起動したら、起動してみます。
Wiresharkを起動する
enp0s8 は、VirtualBox のホストオンリーアダプタで、enp0s3 がインターネットに繋がる方のアドレスです。ここでは、enp0s3 を見たいので、ダブルクリックします。
キャプチャするインタフェースを選択する
たくさん受信しています。フィルタを入れてみます。選択した enp0s3 の IP アドレスは、10.0.2.15 なので、同じネットワークから送信されたパケットだけを見てみます。
フィルタに、`ip.src == 10.0.2.0/24' を入力します。自分自身が送信したパケットばかりが見えています。
ちなみに、図の赤枠で囲ったところの左端のブックマークのアイコン?を押すと、いくつかのフィルタの例が見れますし、何度も入力するフィルタを保存したりできるようです。
フィルタを入力した
正しく動作しているようなので、Wireshark のインストールは以上となります。
VirtualBoxのネットワークについて調べる(Ubuntu 22.04)
Windows10 にインストールした VirtualBox は、仮想マシンとして、Ubuntu 22.04 が動作しています。
その Ubuntu 22.04 で、ネットワークについて調査します。
手動でいくつかのコマンドを実行してみる
まずは、ifconfig コマンドを確認して、その後、route コマンドで確認します。
$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:58:d5:1e:0b txqueuelen 0 (イーサネット)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::eb16:b65:e9c5:ddc9 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:54:d5:cb txqueuelen 1000 (イーサネット)
RX packets 294 bytes 327933 (327.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 231 bytes 28292 (28.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.56.101 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::2d19:efaa:a12d:f974 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:c0:67:7d txqueuelen 1000 (イーサネット)
RX packets 207 bytes 29245 (29.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 166 bytes 26916 (26.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (ローカルループバック)
RX packets 270 bytes 31265 (31.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 270 bytes 31265 (31.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
$ route
カーネルIP経路テーブル
受信先サイト ゲートウェイ ネットマスク フラグ Metric Ref 使用数 インタフェース
default _gateway 0.0.0.0 UG 100 0 0 enp0s3
10.0.2.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
link-local 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.56.0 0.0.0.0 255.255.255.0 U 101 0 0 enp0s8
Ubuntu 22.04 のインターネットに繋がるのが、10.0.2.15 です。
他に、VirtualBox のホストオンリーアダプターと、Docker のインタフェースがありますが、今回は、10.0.2.15 に注目します。
先ほどの Wireshark で、10.0.0.0/24 の先頭あたりに ping を実行したときの様子を確認します。
フィルタを以下にします。10.0.2.1 は、ping の応答がありません。10.0.2.2 から 10.0.2.4 の 3つのアドレスは、ping の応答があります。
ip.addr == 10.0.2.1 or ip.addr == 10.0.2.2 or ip.addr == 10.0.2.3 or ip.addr == 10.0.2.4
10.0.2.1から10.0.2.4までのアドレスにフィルタを設定した
ARP コマンドを実行してみます。
$ arp 10.0.2.1
アドレス HWタイプ HWアドレス フラグ マスク インタフェース
10.0.2.1 (不完全) enp0s3
$ arp 10.0.2.2
アドレス HWタイプ HWアドレス フラグ マスク インタフェース
_gateway ether 52:54:00:12:35:02 C enp0s3
$ arp 10.0.2.3
アドレス HWタイプ HWアドレス フラグ マスク インタフェース
10.0.2.3 ether 52:54:00:12:35:03 C enp0s3
$ arp 10.0.2.4
アドレス HWタイプ HWアドレス フラグ マスク インタフェース
10.0.2.4 ether 52:54:00:12:35:04 C enp0s3
route コマンドに表示されていた _gateway に対して、ping を実行してみます。
$ ping _gateway
PING _gateway (10.0.2.2) 56(84) bytes of data.
64 bytes from _gateway (10.0.2.2): icmp_seq=1 ttl=64 time=0.151 ms
64 bytes from _gateway (10.0.2.2): icmp_seq=2 ttl=64 time=0.187 ms
^C
10.0.2.2 がルーターのようです。
DHCP関係を調べる
ネットワークをオフ→オンして、DHCP サーバから IP アドレスが再配布されるところを、Wireshark で確認してみます。
Ubuntu 22.04 のネットワークをコマンドラインでオフ→オンする方法を調べたんですが、いろいろ書かれていて、自分の状況とは違って、よく分かりませんでした。
なので、GUI で単純にネットワークをオフ→オンしたいと思います。
Wireshark を起動して、キャプチャを開始しておきます。
次に、ネットワークをオフ→オンします。
ネットワークのオフ→オンはここで行う
以下はそのときの Wireshark のキャプチャ結果です。10.0.2.2 から DHCP に関する応答が返ってきてるようです。
Wiresharkのキャプチャ結果
10.0.2.2 はルーターであり、DHCP サーバも兼ねているということのようです。
DNS関係を調べる
続いて、DNS 関係を調べます。nslookup コマンドと、dig コマンドを使います。
$ nslookup www.yahoo.co.jp
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
www.yahoo.co.jp canonical name = edge12.g.yimg.jp.
Name: edge12.g.yimg.jp
Address: 182.22.16.123
$ dig www.yahoo.co.jp
; <<>> DiG 9.18.24-0ubuntu0.22.04.1-Ubuntu <<>> www.yahoo.co.jp
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62763
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.yahoo.co.jp. IN A
;; ANSWER SECTION:
www.yahoo.co.jp. 481 IN CNAME edge12.g.yimg.jp.
edge12.g.yimg.jp. 40 IN A 182.22.25.124
;; AUTHORITY SECTION:
g.yimg.jp. 12 IN NS gns02.yahoo.co.jp.
g.yimg.jp. 12 IN NS gns12.yahoo.co.jp.
;; ADDITIONAL SECTION:
gns02.yahoo.co.jp. 520 IN A 118.151.254.148
gns12.yahoo.co.jp. 520 IN A 124.83.255.100
;; Query time: 8 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Sun Jul 14 19:51:27 JST 2024
;; MSG SIZE rcvd: 160
なるほど、特に VirtualBox で DNS サーバを持っているというわけではなさそうです。
netdiscoverコマンドを使ってみる
さて、行き詰ったので、いろいろ調べてみると、netdiscover コマンドという便利なコマンドがあるようです。
なお、このコマンドは、指定したネットワークに対して、単純に arp コマンドを投げまくるツールらしいので、他の人の迷惑にならないところで使わないといけません。今回は、VirtualBox 内のネットワークに対して使用します。
まず、netdiscover コマンドをインストールします。
$ sudo apt install netdiscover
無事にインストール出来たので、早速使ってみます。
-r オプションで、ネットワークの範囲を指定して実行するのが、一般的な使い方のようです。
$ sudo netdiscover -r 10.0.2.0/24
Currently scanning: Finished! | Screen View: Unique Hosts
4 Captured ARP Req/Rep packets, from 3 hosts. Total size: 240
_____________________________________________________________________________
IP At MAC Address Count Len MAC Vendor / Hostname
-----------------------------------------------------------------------------
10.0.2.2 52:54:00:12:35:02 2 120 Unknown vendor
10.0.2.3 52:54:00:12:35:03 1 60 Unknown vendor
10.0.2.4 52:54:00:12:35:04 1 60 Unknown vendor
3つのホストが見つかりました。手動で ping を実行した結果と同じですね。
nmapコマンドを使ってみる
ホストが絞り込めたので、次はそれぞれに対して、nmap コマンドによるポートスキャンを行ってみます。
この nmap コマンドも、自分の管理する範囲内での使用に留める必要があります。他の管理者が管理するネットワークで使用すると、ログからスキャンしてることが分かりますので、注意が必要です。
では、まず nmap コマンドをインストールします。
$ sudo apt install nmap
無事にインストールが完了したので、早速使っていきます。
まず、ルータと思われる 10.0.0.2 に対して実行します。
$ sudo nmap -sT 10.0.2.2
Starting Nmap 7.80 ( https://nmap.org ) at 2024-07-14 20:50 JST
Nmap scan report for _gateway (10.0.2.2)
Host is up (0.0092s latency).
Not shown: 992 filtered ports
PORT STATE SERVICE
22/tcp open ssh
135/tcp open msrpc
445/tcp open microsoft-ds
808/tcp open ccproxy-http
5357/tcp open wsdapi
9010/tcp open sdr
9080/tcp open glrpc
9100/tcp open jetdirect
MAC Address: 52:54:00:12:35:02 (QEMU virtual NIC)
Nmap done: 1 IP address (1 host up) scanned in 4.49 seconds
オープンしてるポート番号が表示されます。
残り2つのホストに対しても実行してみます。
$ sudo nmap -sT 10.0.2.3
Starting Nmap 7.80 ( https://nmap.org ) at 2024-07-14 20:52 JST
Nmap scan report for 10.0.2.3
Host is up (0.0094s latency).
Not shown: 992 filtered ports
PORT STATE SERVICE
22/tcp open ssh
135/tcp open msrpc
445/tcp open microsoft-ds
808/tcp open ccproxy-http
5357/tcp open wsdapi
9010/tcp open sdr
9080/tcp open glrpc
9100/tcp open jetdirect
MAC Address: 52:54:00:12:35:03 (QEMU virtual NIC)
Nmap done: 1 IP address (1 host up) scanned in 5.21 seconds
$ sudo nmap -sT 10.0.2.4
Starting Nmap 7.80 ( https://nmap.org ) at 2024-07-14 20:52 JST
Nmap scan report for 10.0.2.4
Host is up (0.010s latency).
Not shown: 992 filtered ports
PORT STATE SERVICE
22/tcp open ssh
135/tcp open msrpc
445/tcp open microsoft-ds
808/tcp open ccproxy-http
5357/tcp open wsdapi
9010/tcp open sdr
9080/tcp open glrpc
9100/tcp open jetdirect
MAC Address: 52:54:00:12:35:04 (QEMU virtual NIC)
Nmap done: 1 IP address (1 host up) scanned in 5.42 seconds
3つのホストで結果がほぼ同じになりました。どういうことなんでしょうか。
もう少し詳細に nmap コマンドを使います。だいぶ時間がかかります。
$ sudo nmap -sC -sV -Pn -p- 10.0.2.2 -oN router.txt
Starting Nmap 7.80 ( https://nmap.org ) at 2024-07-21 22:26 JST
Nmap scan report for _gateway (10.0.2.2)
Host is up (0.018s latency).
Not shown: 65512 filtered ports
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH for_Windows_8.1 (protocol 2.0)
| ssh-hostkey:
| 3072 60:1b:23:e8:a2:36:eb:21:00:b5:8b:27:f9:0c:29:64 (RSA)
| 256 d1:82:35:e8:d0:17:ef:0e:5d:03:5b:2e:52:9d:00:cd (ECDSA)
|_ 256 af:5b:0a:48:bc:03:4f:d0:9b:39:83:4d:db:c9:a9:48 (ED25519)
135/tcp open msrpc Microsoft Windows RPC
445/tcp open microsoft-ds?
808/tcp open mc-nmf .NET Message Framing
2869/tcp open http Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
|_http-server-header: Microsoft-HTTPAPI/2.0
|_http-title: Service Unavailable
5357/tcp open http Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
|_http-server-header: Microsoft-HTTPAPI/2.0
|_http-title: Service Unavailable
7680/tcp open pando-pub?
9010/tcp open websocket WebSocket++ 0.8.2
9080/tcp open glrpc?
| fingerprint-strings:
| DNSStatusRequestTCP, DNSVersionBindReqTCP, GenericLines, Help, RPCCheck, RTSPRequest, SSLSessionReq, TerminalServerCookie:
| HTTP/1.0 400 Bad Request
| Content-Length: 89
| Content-Type: text/html
| <html><head><title>Bad Request</title></head><body><h1>400 Bad Request</h1></body></html>
| GetRequest, HTTPOptions:
| HTTP/1.0 404 Not Found
| Content-Length: 85
| Content-Type: text/html
|_ <html><head><title>Not Found</title></head><body><h1>404 Not Found</h1></body></html>
9100/tcp open jetdirect?
9180/tcp open unknown
| fingerprint-strings:
| DNSStatusRequestTCP, DNSVersionBindReqTCP, GenericLines, Help, RPCCheck, RTSPRequest, SSLSessionReq, TerminalServerCookie:
| HTTP/1.0 400 Bad Request
| Content-Length: 89
| Content-Type: text/html
| <html><head><title>Bad Request</title></head><body><h1>400 Bad Request</h1></body></html>
| GetRequest, HTTPOptions:
| HTTP/1.0 404 Not Found
| Content-Length: 85
| Content-Type: text/html
|_ <html><head><title>Not Found</title></head><body><h1>404 Not Found</h1></body></html>
45654/tcp open websocket WebSocket++ 0.8.2
45769/tcp open unknown
49664/tcp open msrpc Microsoft Windows RPC
49665/tcp open msrpc Microsoft Windows RPC
49666/tcp open msrpc Microsoft Windows RPC
49667/tcp open msrpc Microsoft Windows RPC
49668/tcp open msrpc Microsoft Windows RPC
49669/tcp open msrpc Microsoft Windows RPC
49671/tcp open msrpc Microsoft Windows RPC
49772/tcp open http Node.js Express framework
|_http-cors: GET POST
|_http-title: Site doesn't have a title (text/html;charset=utf-8).
59392/tcp open tcpwrapped
65001/tcp open unknown
2 services unrecognized despite returning data. If you know the service/version, please submit the following fingerprints at https://nmap.org/cgi-bin/submit.cgi?new-service :
(省略)
かなり細かい情報が出ましたが、読み解くのも大変そうです。
VirtualBoxのネットワークについて調べる(Ubuntu 24.04)
ここからは追記部分になります。Ubuntu も Ubuntu 22.04 から Ubuntu 24.04 に変わっています。
手動でいくつかのコマンドを実行してみる
デフォルトで ifconfigコマンドがインストールされていません。代わりに ipコマンドを使います。ifconfig の方が見やすいですよね。。
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:e9:3d:b6 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
valid_lft 86317sec preferred_lft 86317sec
inet6 fd17:625c:f037:2:acaf:7165:ed28:9e34/64 scope global temporary dynamic
valid_lft 86318sec preferred_lft 14318sec
inet6 fd17:625c:f037:2:a00:27ff:fee9:3db6/64 scope global dynamic mngtmpaddr
valid_lft 86318sec preferred_lft 14318sec
inet6 fe80::a00:27ff:fee9:3db6/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:d0:8f:e2 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.102/24 brd 192.168.56.255 scope global dynamic noprefixroute enp0s8
valid_lft 517sec preferred_lft 517sec
inet6 fe80::340f:d80e:3f77:d501/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ルーティングテーブルを見ます。routeコマンドもインストールされていませんので、こちらも代わりに ipコマンドを使います。
こちらも routeコマンドの方が見やすいですが、見方を書いておきます。
最初のフィールドが対象のネットワークアドレス(default はどこにも該当しないケース)で、via は次に渡す先を指定していて、dev はパケットを送信するインタフェースで、proto dhcp は DHCP が自動生成した経路、proto kernel はカーネルが自動生成した経路という意味で、scope link は直接送信する経路で、scope が書かれてない場合は、scope global であり、ルーターを経由した他ネットワークへの経路を示します。
ちなみに、ルーティングテーブルは、実は複数存在することができます。何も指定しないと mainテーブルというのが使われています。以下で実行したコマンドは、$ ip route show と同じ意味ですが、$ ip route show table main とも同じ意味になります。
$ ip route
default via 10.0.2.2 dev enp0s3 proto dhcp src 10.0.2.15 metric 100
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100
192.168.56.0/24 dev enp0s8 proto kernel scope link src 192.168.56.102 metric 101
iptablesコマンド
iptablesコマンドは、ファイアウォールの設定を確認したり、変更したりすることができるコマンドです。
iptablesコマンドは、多くの操作を行うことができるため、とても難しいコマンドです。簡単に概要を書いておきます。
まず、iptablesコマンドで操作できるテーブルは、5つ(filter、nat、mangle、raw、security)あります。
| テーブル |
説明 |
使用できるチェーン |
| filter |
メインとなるテーブルで、パケットの通過、遮断を行うことができる |
INPUT、FORWARD、OUTPUT |
| nat |
アドレス、ポートを変換することができる |
PREROUTING、INPUT、OUTPUT、POSTROUTING |
| mangle |
IPヘッダを変更することができる |
PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING |
| raw |
パケットにマークを付けることができる |
PREROUTING、OUTPUT |
| security |
SELinuxのセキュリティを設定することができる |
INPUT、FORWARD、OUTPUT |
次に、チェーンについては、5つのチェーンがあります。下表は、5つのチェーンの説明です。受信したパケットが処理される順に並んでいます。
| チェーン |
説明 |
| PREROUTING |
受信直後に適用されるチェーン |
| INPUT |
受信先決定後に適用されるチェーン |
| FORWARD |
転送パケットに適用されるチェーン |
| OUTPUT |
送信されるパケットに適用されるチェーン |
| POSTROUTING |
送信直前に適用されるチェーン |
システムが受信するパケットは、PREROUTING → INPUTチェーンで処理されて受信されます。転送パケットは、PREROUTING → FORWARD → POSTROUTINGチェーンで処理されて転送されます。システムが送信するパケットは、OUTPUT → POSTROUTINGチェーンで処理されて送信されます。
最後に、ルールについてです。ルールとは、条件となるマッチングと、処理を決めるターゲットがあります。例えば、マッチングには、IPアドレスやプロトコル、ポート番号で条件を決めることができます。ターゲットは、ACCEPT(通過)、DROP(遮断)などがあります。
iptablesコマンドを使ってみる
iptablesコマンドを使ってみます。バージョンは、v1.8.10 のようです。
$ iptables
iptables v1.8.10 (nf_tables): no command specified
Try `iptables -h' or 'iptables --help' for more information.
ヘルプを確認します。
$ iptables --help
iptables v1.8.10 (nf_tables)
Usage: iptables -[ACD] chain rule-specification [options]
iptables -I chain [rulenum] rule-specification [options]
iptables -R chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LS] [chain [rulenum]] [options]
iptables -[FZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)
Commands:
Either long or short options are allowed.
--append -A chain Append to chain
--check -C chain Check for the existence of a rule
--delete -D chain Delete matching rule from chain
--delete -D chain rulenum
Delete rule rulenum (1 = first) from chain
--insert -I chain [rulenum]
Insert in chain as rulenum (default 1=first)
--replace -R chain rulenum
Replace rule rulenum (1 = first) in chain
--list -L [chain [rulenum]]
List the rules in a chain or all chains
--list-rules -S [chain [rulenum]]
Print the rules in a chain or all chains
--flush -F [chain] Delete all rules in chain or all chains
--zero -Z [chain [rulenum]]
Zero counters in chain or all chains
--new -N chain Create a new user-defined chain
--delete-chain
-X [chain] Delete a user-defined chain
--policy -P chain target
Change policy on chain to target
--rename-chain
-E old-chain new-chain
Change chain name, (moving any references)
Options:
--ipv4 -4 Nothing (line is ignored by ip6tables-restore)
--ipv6 -6 Error (line is ignored by iptables-restore)
[!] --protocol -p proto protocol: by number or name, eg. `tcp'
[!] --source -s address[/mask][...]
source specification
[!] --destination -d address[/mask][...]
destination specification
[!] --in-interface -i input name[+]
network interface name ([+] for wildcard)
--jump -j target
target for rule (may load target extension)
--goto -g chain
jump to chain with no return
--match -m match
extended match (may load extension)
--numeric -n numeric output of addresses and ports
[!] --out-interface -o output name[+]
network interface name ([+] for wildcard)
--table -t table table to manipulate (default: `filter')
--verbose -v verbose mode
--wait -w [seconds] maximum wait to acquire xtables lock before give up
--line-numbers print line numbers when listing
--exact -x expand numbers (display exact values)
[!] --fragment -f match second or further fragments only
--modprobe=<command> try to insert modules using this command
--set-counters -c PKTS BYTES set the counter during insert/append
[!] --version -V print package version.
まず、現在の設定を確認します。-L(--list)はチェーンのルールを一覧にしてくれるようです。-v(--vorbose)は冗長出力、-n(--numeric)はアドレスやポートを変換せずに数値で出力するオプションのようです。テーブルを指定(-t、--table)していないため、デフォルトで、filterテーブルが指定されます。
何もルールが定義されていませんね。INPUT は受信パケット、FORWARD は転送パケット、OUTPUT は送信パケットを意味します。
$ sudo iptables -L -v -n
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
他のテーブルも見てみます。それぞれ、使用できるチェーンごとにルールが出力されます。こちらもルールは定義されていません。ちなみに、それぞれのチェーンに policy ACCEPT と出力されています。これは、基本は通過で、遮断するルールを定義する方式(ブラックリスト方式)で、policy を DROP に変更すると、基本は遮断で、通過させるルールを定義する方式(ホワイトリスト方式)です。ルールは上から順番に処理されていきます。
$ sudo iptables -t nat -L -v -n
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
$ sudo iptables -t mangle -L -v -n
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
$ sudo iptables -t raw -L -v -n
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
$ sudo iptables -t security -L -v -n
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
iptablesコマンドでルールを追加してみる
現在は、ルールが何もない状態です。
policy を DROP(ホワイトリスト方式)に変更してみます。変更前にブラウザ(Firefox)で、Google検索のページが表示できることを確認しておきます。
policy を DROP に変更し、現在の状態を確認すると、policy が DROP に変わっていることを確認できます。この状態で、Google検索のページを表示するためにリロードすると、表示されません。受信パケットがドロップされるようになったからです。
$ sudo iptables -P INPUT DROP
$ sudo iptables -L -v -n
Chain INPUT (policy DROP 44 packets, 2866 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
元に戻しておきます。
$ sudo iptables -P INPUT ACCEPT
$ sudo iptables -L -v -n
Chain INPUT (policy ACCEPT 354 packets, 268K bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
ブラウザで、Google検索のページは表示できるようになります。
次に、ルールを追加してみます。現在、VirtualBox の Ubuntu に対して、SSH で接続しています。この SSH を遮断するルールを追加してみます。
ルール番号が分かりやすいように、--line-numbers を追加して表示しています。
このルールを追加すると、SSH で Ubuntu 24.04 に接続できなくなりました(接続していたターミナルが固まりました)。
$ sudo iptables -A INPUT -p tcp --dport 22 -j DROP
$ sudo iptables -L -v -n --line-numbers
Chain INPUT (policy ACCEPT 894 packets, 353K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
SSH接続を許可するクライアント(Windows11 の 192.168.56.1)を指定するルールを追加してみます。
この状態では、SSH接続はできませんでした。ルールは上から評価されるため、1番のルールで DROP されるためです。
$ sudo iptables -A INPUT -s 192.168.56.1 -j ACCEPT
$ sudo iptables -L -v -n --line-numbers
Chain INPUT (policy ACCEPT 1146 packets, 392K bytes)
num pkts bytes target prot opt in out source destination
1 59 11636 DROP 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
2 0 0 ACCEPT 0 -- * * 192.168.56.1 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1番と同じルールを再度追加します。
$ sudo iptables -A INPUT -p tcp --dport 22 -j DROP
$ sudo iptables -L -v -n --line-numbers
Chain INPUT (policy ACCEPT 1153 packets, 392K bytes)
num pkts bytes target prot opt in out source destination
1 62 11792 DROP 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
2 2 304 ACCEPT 0 -- * * 192.168.56.1 0.0.0.0/0
3 0 0 DROP 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
この状態でも、もちろん、SSH接続はできません。では、1番のルールを削除してみます。
SSH接続できるようになりました。ルールは上から評価されるため、2番に DROP があっても、1番のルールで ACCEPT するため、SSH接続できるという理由です。
$ sudo iptables -D INPUT 1
$ sudo iptables -L -v -n --line-numbers
Chain INPUT (policy ACCEPT 1186 packets, 401K bytes)
num pkts bytes target prot opt in out source destination
1 88 9559 ACCEPT 0 -- * * 192.168.56.1 0.0.0.0/0
2 0 0 DROP 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
ちなみに、ターゲットには、ACCEPT や DROP のように、処理が決定されるものと、別のチェーンにジャンプするようなターゲットも存在します。
iptablesコマンドの簡単な使い方は以上となります。
おわりに
今回は、VirtualBox のネットワークについて調べてみました。
あまり、有益な情報は無かったかもしれませんが、理解は深まりました。
最後になりましたが、エンジニアグループのランキングに参加中です。
気楽にポチッとよろしくお願いいたします🙇
今回は以上です!
最後までお読みいただき、ありがとうございました。