# RaspberryPiでOpenWrtを使って、VPNクライアントを併用したリモートワーク用の便利アクセスポイントを建てる(備忘録) ## 概要 RaspberryPiとOpenWrt(汎用ルーターOS)を使ったVPNルーターを安価に作成し、業務用VPNが常に有効なWi-Fiアクセスポイントを建てることが目的です。 特に下記の課題に付いても解決を目指していきます。 ・リモートワーク上での顧客環境へのアクセスの際に、自宅IPが不意に漏れないようにする ・スマホ端末上の特定の通信をプロキシサーバに流す ## OpenWrtのセットアップ Raspberry Pi3 Model B BCM2837:互換bcm2710 1.最新版を以下から取得する https://downloads.openwrt.org/releases/ 2.チップセットに合わせて、ファクトリイメージを選択 brcm2708->bcm2710->rpi-3-ext4-factory.img.gz balenaEtcherでsdcardに書き込みする https://www.balena.io/etcher/ 3.LANからhttp://192.168.1.1でアクセス出来る ### RaspberryPi4の場合 ここから https://downloads.openwrt.org/snapshots/targets/bcm27xx/bcm2711/ なお、Luciがデフォルトで入ってないのでSSHからネットワークの設定をしてあげる PCとラズパイをLANで接続する `ssh root@192.168.1.1` ``` uci set network.lan=interface uci set network.lan.ifname='eth0' uci set network.lan.proto='static' uci set network.lan.netmask='255.255.255.0' uci set network.lan.delegate='0' uci set network.lan.gateway='192.168.xx.1' uci set network.lan.ipaddr='192.168.xx.x' uci set network.lan.dns='8.8.8.8' uci commit service network restart //ネット繋げてLuciをインストール opkg update opkg install luci opkg install \ luci-i18n-base-ja \ luci-i18n-firewall-ja \ luci-i18n-opkg-ja service uhttpd restart ``` ## OpenWrt最低限のセキュリティ * Router Passwordを設定する ※rootのパスワードである点に注意 * WebInterfaceへのアクセスを制限する (理想 *必須ではない)特定のInterfaceのみlistenさせる ![](https://i.imgur.com/qH3zn2z.png) * SSHのパスワードを設定する (理想 *必須ではない) 可能であれば公開鍵認証にしたり、root以外で作業するようにする * 作業用のゲストユーザを作成する (理想 *必須ではない) root以外で作業するようにする ## APを建てる ### セキュリティ 起動前にパスフレーズ(WPA2)は最低限設定する ### Tips 動作周波数をレガシーに設定して、周波数を指定しないと起動しない可能性がある チャンネルは * 5Ghz帯なら5200Mhz * 2.4Ghz帯なら2462Mhz ### 技適準拠 この設定は保険でしかない。 なるべく技適マーク付きのハード側で制御されているものを使いたい? * TransmitPowerを10 dBm以下に設定 * Country CodeをJPに設定 ### Interfacesの設定 Network->Interfacesで下記の項目の設定をしておく ・IPv4 Addresssの設定(任意のアドレス) ・IPv4 netmaskの設定 ・DHCP Serverの有効化 ・Physical Settingsでwlan0を紐付ける ・Firewall Settingsでルールを作成しておく ## DNS設定 ``` rm /etc/resolv.conf echo "nameserver 8.8.8.8" > /etc/resolv.conf ``` ## StrongSwanとxl2tpdをインストール ### opkgのアップデート ``` opkg update ``` ### StrongSwan ``` opkg install strongswan-default strongswan-mod-dhcp strongswan-mod-af-alg opkg install strongswan-mod-gcrypt strongswan-mod-blowfish strongswan-mod-md4 strongswan-mod-openssl opkg install strongswan-mod-pkcs11 strongswan-mod-pkcs8 strongswan-mod-test-vectors strongswan-mod-farp ``` ### xl2tpd ``` opkg install xl2tpd ``` ## StrongSwan(ipsec)の設定 ### swanctl.confの設定 `/etc/swanctl/swanctl.conf` ``` connections { new_vpn { remote_addrs=@vpn_gw remote { auth = psk } children { l2tp-ipsec { remote_ts=@vpn_gw[udp/1701] start_action = start close_action = start dpd_action = start esp_proposals=default mode = transport } } keyingtries = 3 version = 0 proposals = default } } secrets { ike-l2tp-ipsec { secret = "@secret" } } ``` @vpn_gwにゲートウェイを指定 @secretにシークレットを指定 ## xl2tpd(L2TP)の設定 `/etc/xl2tpd/xl2tpd.conf` ``` [global] auth file = /etc/ppp/chap-secrets [lac L2TP1] lns = @vpn_gw require chap = yes refuse pap = yes require authentication = yes ppp debug = yes pppoptfile = /etc/ppp/options.l2tpd.client length bit = yes redial = yes redial timeout = 60 max redials = 10 ``` @vpn_gwにゲートウェイを指定 #### 認証情報を記載 `/etc/ppp/chap-secrets` ``` @username * "@password" ``` #### pppの設定 `/etc/ppp/options.l2tpd.client` ``` name "@username" ipcp-accept-local ipcp-accept-remote noccp noauth idle 1800 nodefaultroute mtu 1200 mru 1200 debug logfile /var/log/xl2tpd.conn-1.log refuse-pap refuse-eap usepeerdns connect-delay 5000 ``` @usernameにVPN認証情報でのユーザ名を指定 ## VPN Interfaceの作成 一通り設定が終わった後、下記のコマンドでサービスを立ち上げる。 ``` /etc/init.d/swanctl start sleep 1 /etc/init.d/xl2tpd start /bin/echo "c L2TP1" > /var/run/xl2tpd/l2tp-control sleep 4 ``` ## Firewallの設定 ### Zonesの設定 #### lan(wan) -> REJECT * Masqueradingを有効化 * Forwardings * Input:reject(必要であれば、sshとWebInterfaceを許可しておく) * Output:accept * Forward:reject #### wlan->VPN * MSS Clampingを有効化 * Forwardings * Input:accept * Output:accept * Forward:reject #### VPN -> REJECT * Masqueradingを有効化 * Forwardings * Input:reject * Output:accept * Forward:reject ![](https://i.imgur.com/CH0UCIx.png) ## Tips ### スマホ端末の通信をプロキシサーバに横流しする Firewall - Custom Rulesに以下を追加 ``` iptables -t nat -A zone_WLAN_prerouting -s @source_ip -m multiport -p tcp --dports 80,443 -j DNAT --to-destination @proxy_server_ip:@proxy_server_port iptables -t nat -A zone_WLAN_postrouting -s @source_ip -j MASQUERADE ``` または、スクリプトを書いておく #### 自動スクリプト ##### Enable ```sh iptables -t nat -A zone_wlan_prerouting -s 192.168.11.5 -p tcp --dport 80 -j DNAT --to-destination 192.168.11.2:8889 iptables -t nat -A zone_wlan_prerouting -s 192.168.11.5 -p tcp --dport 443 -j DNAT --to-destination 192.168.11.2:8889 iptables -t nat -A zone_wlan_prerouting -s 192.168.11.6 -p tcp --dport 80 -j DNAT --to-destination 192.168.11.2:8889 iptables -t nat -A zone_wlan_prerouting -s 192.168.11.6 -p tcp --dport 443 -j DNAT --to-destination 192.168.11.2:8889 iptables -t nat -A zone_wlan_postrouting -s 192.168.11.5 -j MASQUERADE iptables -t nat -A zone_wlan_postrouting -s 192.168.11.6 -j MASQUERADE ``` ##### Disable ```sh iptables -L zone_wlan_prerouting -t nat iptables -L zone_wlan_postrouting -t nat iptables -F zone_wlan_prerouting -t nat iptables -F zone_wlan_postrouting -t nat echo "Deleted" iptables -L zone_wlan_prerouting -t nat iptables -F zone_wlan_postrouting -t nat ``` ### 自動スクリプトの作成 #### 起動用スクリプト bashで書き直した `opkg install bash` ```bash #!/bin/bash # Refer -> https://gist.github.com/Hexcles/6562821 PPP_TIMEOUT=30 L2TPD_TIMEOUT=5 L2TPD_CONTROL_FILE=/var/run/xl2tpd/l2tp-control SWANCTL_TIMEOUT=5 VPN_ACCOUNT=L2TP1 VPN_GW=@vpn_gw LOCAL_GW=@local_gw function start_xl2tpd { /etc/init.d/xl2tpd start &> /dev/null for i in $(seq 0 $L2TPD_TIMEOUT) do if [ -e $L2TPD_CONTROL_FILE ]; then return 0 fi sleep 1 done return 1 } function start_swanctl { /etc/init.d/swanctl start &> /dev/null for i in $(seq 0 $SWANCTL_TIMEOUT) do swanctl --list-conns &> /dev/null if [ "$?" -eq 0 ]; then return 0 fi sleep 1 done return 1 } function vpn_start { # stop SWANCTL swanctl --list-conns &> /dev/null if [ "$?" -eq 0 ]; then /usr/sbin/swanctl -t &> /dev/null /etc/init.d/swanctl stop &> /dev/null fi # stop XL2TP if [ -e $L2TPD_CONTROL_FILE ]; then xl2tpd-control disconnect-lac $VPN_ACCOUNT &> /dev/null /etc/init.d/xl2tpd stop &> /dev/null fi if ! start_swanctl; then echo "Failed starting swanctl" return 1 fi if ! start_xl2tpd; then echo "Failed starting xl2tpd" return 1 fi # connection swanctl -q &> /dev/null sleep 5 xl2tpd-control connect-lac $VPN_ACCOUNT &> /dev/null # if already routed then del if ip route show $VPN_GW > /dev/null; then ip route del $VPN_GW via $LOCAL_GW dev eth0 &> /dev/null ip route add default via $LOCAL_GW &> /dev/null fi for i in $(seq 0 20) do GW=`/sbin/ifconfig $1 | grep "P-t-P" | awk -F: '{print $2}' | awk '{print $1}'` if [ ! -z $GW ]; then break fi sleep 1 done if [ ! -z $GW ]; then ip route add $VPN_GW via $LOCAL_GW dev eth0 &> /dev/null ip route del default via $LOCAL_GW &> /dev/null ip route add default via $GW &> /dev/null fi if [ -z $GW ]; then echo "Not found vpn gw" exit 1 fi for i in $(seq 0 $PPP_TIMEOUT) do if ip addr show | grep 'inet.*ppp0' > /dev/null; then echo "Connected" exit 0 fi sleep 1 done # failed swanctl -t &> /dev/null /etc/init.d/swanctl stop &> /dev/null xl2tpd-control disconnect-lac $VPN_ACCOUNT &> /dev/null /etc/init.d/xl2tpd stop &> /dev/null echo "Connection time out" exit 1 } function vpn_stop { # stop SWANCTL swanctl --list-conns &> /dev/null if [ "$?" -eq 0 ]; then /usr/sbin/swanctl -t &> /dev/null /etc/init.d/swanctl stop &> /dev/null fi # stop XL2TP if [ -e $L2TPD_CONTROL_FILE ]; then xl2tpd-control disconnect-lac $VPN_ACCOUNT &> /dev/null /etc/init.d/xl2tpd stop &> /dev/null fi # if already routed then del if ip route show $VPN_GW > /dev/null; then ip route del $VPN_GW via $LOCAL_GW dev eth0 &> /dev/null ip route add default via $LOCAL_GW &> /dev/null fi echo "Disconnected" } function usage { echo 'set args [stop or start]' } function main { if [ $1 -ne 1 ]; then usage exit 1 fi if [ $2 = start ]; then vpn_start exit 0 fi if [ $2 = stop ]; then vpn_stop exit 0 fi usage } main $# $1 ``` @vpn_gwにゲートウェイを指定 @local_gwにルーターのゲートウェイを指定 ### 自動起動 `/etc/rc.local`に書くだけ ## Reference https://wiki.archlinux.jp/index.php/Openswan_L2TP/IPsec_VPN_%E3%82%AF%E3%83%A9%E3%82%A4%E3%82%A2%E3%83%B3%E3%83%88%E8%A8%AD%E5%AE%9A https://qiita.com/xecus/items/04e648e64fc8925015ca https://qiita.com/__cooper/items/4b5ab7ae40b0e95f0d19 https://qiita.com/Lyxion/items/c4cc414f43c0d2b444da