3
@maru2213

ラズベリーを玉ねぎへ 〜Raspberry PiのOnion Pi化〜

用意するもの

  • 母艦(mac)
  • Raspberry Pi(必要に応じてケースも)
  • ラスパイ用ACアダプタ、コード
  • LANケーブル
  • microSDカード(4GBで行ける、8GBで十分、16GBだと余裕)→じゃあ16GB使おう
  • (SDカードリーダー・ライター)

今回の環境

  • Macbook Pro 13inch early 2015
  • macOS Mojave(10.14.6)
  • Raspberry Pi 3 Model B+
  • Raspbian Buster Lite(2020-02-05)

とりあえずRaspberry Piのセットアップ

 このページの以下の作業をしておきます。

  • Raspbianをダウンロード
  • Raspbianを焼く
  • SSHを有効にしておく
  • 起動とSSHでのログイン
  • (swapを無効にする)
  • ラズパイの設定 ※特にWifi countryの設定は必須です。

ソフトウェアアップデート

※1行目のコマンドで日本ミラーを使用するように設定し、2,3行目のコマンドでアップデートします。

コマンド
$ sudo sed -i.bak -e "s%http://raspbian.raspberrypi.org/raspbian/%http://ftp.jaist.ac.jp/raspbian/%g" /etc/apt/sources.list
$ sudo apt update
$ sudo apt -y upgrade

必要なソフトウェアのインストール

1./etc/apt/sources.listに以下を追記します。

/etc/apt/sources.list
deb https://deb.torproject.org/torproject.org buster main
deb-src https://deb.torproject.org/torproject.org buster main

2.以下のコマンドを実行します。

コマンド
$ curl https://deb.torproject.org/torproject.org/A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc | gpg --import
$ gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | sudo apt-key add -
$ sudo apt update
$ sudo apt -y install hostapd dnsmasq tor

3.一旦hostapd, dnsmasqを停止させておきます。

コマンド
$ sudo systemctl stop hostapd
$ sudo systemctl stop dnsmasq

dhcpの設定

1./etc/dhcpcd.confに以下を追記します。ip_addressは好きなように変えて大丈夫です。

/etc/dhcpcd.conf
interface wlan0
static ip_address=192.168.42.1/24

hostapdの設定

1./etc/hostapd/hostapd.confを新規作成し、以下の内容で保存します。

/etc/hostapd/hostapd.conf
interface=wlan0
ssid=<SSID>
hw_mode=g
channel=6
wmm_enabled=0
macaddr_acl=0
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_passphrase=<PASSWORD>

2./etc/default/hostapdを開き、#DAEMON_CONF=""DAEMON_CONF="/etc/hostapd/hostapd.conf"にします。

/etc/default/hostapd
# Defaults for hostapd initscript
#
# WARNING: The DAEMON_CONF setting has been deprecated and will be removed
#          in future package releases.
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#       -d   show more debug messages (-dd for even more)
#       -K   include key data in debug messages
#       -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""

dnsmasqの設定

1./etc/dnsmasq.confに以下を追記します。

/etc/dnsmasq.conf
interface=wlan0
dhcp-range=192.168.42.2,192.168.42.99,255.255.255.0,24h

torの設定

1./etc/tor/torrcに以下を追記します。

/etc/tor/torrc
VirtualAddrNetworkIPv4 10.192.0.0/10
AutomapHostsOnResolve 1
TransPort 192.168.42.1:9040
DNSPort 192.168.42.1:53

2./lib/systemd/system/tor@default.service[Service]の項目にRestartSec=1sを追記します。(RaspberryPiを再起動した時にコケるため)

/lib/systemd/system/tor@default.service
[Unit]
Description=Anonymizing overlay network for TCP
After=network.target nss-lookup.target
PartOf=tor.service
ReloadPropagatedFrom=tor.service

[Service]
Type=notify
NotifyAccess=all
PIDFile=/run/tor/tor.pid
PermissionsStartOnly=yes
ExecStartPre=/usr/bin/install -Z -m 02755 -o debian-tor -g debian-tor -d /run/tor
ExecStartPre=/usr/bin/tor --defaults-torrc /usr/share/tor/tor-service-defaults-torrc -f /etc/tor/torrc --RunAsDaemon 0 --verify-config
ExecStart=/usr/bin/tor --defaults-torrc /usr/share/tor/tor-service-defaults-torrc -f /etc/tor/torrc --RunAsDaemon 0
ExecReload=/bin/kill -HUP ${MAINPID}
KillSignal=SIGINT
RestartSec=1s                  # ←←←←これです←←←← #
TimeoutStartSec=300
TimeoutStopSec=60
Restart=on-failure
LimitNOFILE=65536

# Hardening
AppArmorProfile=-system_tor
NoNewPrivileges=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectHome=yes
ProtectSystem=full
ReadOnlyDirectories=/
ReadWriteDirectories=-/proc
ReadWriteDirectories=-/var/lib/tor
ReadWriteDirectories=-/var/log/tor
ReadWriteDirectories=-/run
CapabilityBoundingSet=CAP_SETUID CAP_SETGID CAP_NET_BIND_SERVICE CAP_DAC_READ_SEARCH

サービスの起動

1.以下のコマンドを実行します。

コマンド
$ sudo systemctl restart dhcpcd
$ sudo systemctl unmask hostapd
$ sudo systemctl enable hostapd
$ sudo systemctl start hostapd
$ sudo systemctl start dnsmasq
$ sudo systemctl restart tor@default

IP転送の設定

1./etc/sysctl.confを開き、28行目あたりにある以下のコメントアウトを外します。

/etc/sysctl.conf
net.ipv4.ip_forward=1

2.以下のコマンドを実行します。

コマンド
$ sudo iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 22 -j REDIRECT --to-ports 22
$ sudo iptables -t nat -A PREROUTING -i wlan0 -p udp --dport 53 -j REDIRECT --to-ports 53
$ sudo iptables -t nat -A PREROUTING -i wlan0 -p tcp --syn -j REDIRECT --to-ports 9040
$ sudo su
# sudo sh -c iptables-save > /etc/iptables.ipv4.nat
# exit

〜〜2020/6/7追記〜〜
もしこの手順2でコケる場合、Raspbianをbuster化したら、iptablesでちょっとだけハマるを参照したら幸せになれるかもしれないです。
〜〜追記終わり〜〜

3./etc/rc.localを開き、exit 0の直前にiptables-restore < /etc/iptables.ipv4.natと追記します。

/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

iptables-restore < /etc/iptables.ipv4.nat

exit 0

4.最後にsudo rebootで再起動して完成です。
5.この後、一時的にただのアクセスポイントにしたい場合、毎回以下のコマンドを実行します。

コマンド
$ sudo iptables -F
$ sudo iptables -t nat -F
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

参考文献

3
ユーザー登録して、Qiitaをもっと便利に使ってみませんか。
  1. あなたにマッチした記事をお届けします
    ユーザーやタグをフォローすることで、あなたが興味を持つ技術分野の情報をまとめてキャッチアップできます
  2. 便利な情報をあとで効率的に読み返せます
    気に入った記事を「ストック」することで、あとからすぐに検索できます
maru2213
その辺にいるただの高校3年生。 色々な言語を齧ったけどいつも使ってるのはJavaだけ。 電子工作方面にも興味あります。

コメント

この記事にコメントはありません。
あなたもコメントしてみませんか :)
ユーザー登録
すでにアカウントを持っている方はログイン
記事投稿イベント開催中
Azure AIを活用した機械学習に関する記事を投稿しよう!
~
フロントエンド強化月間 - 開発する上で知っておくべき知見を共有しよう
~