LoginSignup
7

More than 1 year has passed since last update.

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

Last updated at Posted at 2020-02-17

用意するもの

  • 母艦(mac)
  • Raspberry Pi(必要に応じてケースも)
  • ラスパイ用ACアダプタ、コード
  • LANケーブル
  • microSDカード
  • (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
driver=nl80211
country_code=DFS-JP
ssid=<SSID>
hw_mode=a
channel=36
max_num_sta=255
macaddr_acl=0
ignore_broadcast_ssid=0

auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_passphrase=<PASSWORD>

ieee80211n=0
wmm_enabled=1
ht_capab=[HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40][MAX-AMSDU-3839]

ieee80211ac=1
vht_capab=[MAX-MPDU-3895][SHORT-GI-80][SU-BEAMFORMEE]
vht_oper_chwidth=1
vht_oper_centr_freq_seg0_idx=42

ieee80211d=1
local_pwr_constraint=3
spectrum_mgmt_required=1

ieee80211h=1

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

参考文献

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
7