블로그 이미지
훅크선장

카테고리

분류 전체보기 (361)
사진이야기 (23)
펭귄컴퓨팅 (120)
컴퓨터보안 (84)
절름발이 프로그래머 (59)
하드웨어개조 (23)
멀알려줄까 (35)
홈베이킹&홈쿠킹 (2)
잡다한것들 (15)
Total
Today
Yesterday

달력

« » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

공지사항

태그목록

최근에 올라온 글

OpenWRT 사이트에 있는 Wireguard의 서버와 클라이언트 설정 내용을 참고로 번역하고, 실제로 구성해 본 내용을 정리한 것입니다.

https://openwrt.org/docs/guide-user/services/vpn/wireguard/server

https://openwrt.org/docs/guide-user/services/vpn/wireguard/client

 

Wireguard 구성 개념에서 원래 Server, Client 라는 단어는 존재하지 않고, 그냥 모두 동등한 Peer 일 뿐입니다만,

인터넷 공인 IP를 가진 라우터를 Server 라 칭하고, NAT 내부에서 사설 IP로 동작하는 라우터를 Client라고 지칭해서,

NAT 내부의 Client 라우터가 인터넷 공인IP의 Server라우터에 연결되어, 클라이언트의 트래픽을 VPN 터널링을 통하여 서버측으로 안전하게 이동시킨 후에 인터넷에 접속되도록 하는 것을 구성한 것입니다. 서버와 클라이언트 설정은 아래에 기술합니다.

 

WireGuard server

0. 소프트웨어 설치
opkg update
opkg install luci-proto-wireguard wireguard-tools luci-app-wireguard


1. config용 파라미터 설정
vpn 포트는 udp 51820 이고, 서버측 vpn 인터페이스 IP 주소는 172.16.9.1/24, fd00:9::1/64
※ 클라이언트측 vpn 인터페이스 IP 주소는 172.16.9.2/24, fd00:9::2/64

VPN_IF="vpn"
VPN_PORT="51820"
VPN_ADDR="172.16.9.1/24"
VPN_ADDR6="fd00:9::1/64"

2. 암호화 키 생성
1) 키 생성
umask go=
wg genkey | tee wgserver.key | wg pubkey > wgserver.pub
wg genkey | tee wgclient.key | wg pubkey > wgclient.pub
wg genpsk > wgclient.psk

2) config용 키값 설정 
VPN_KEY="$(cat wgserver.key)"
VPN_PSK="$(cat wgclient.psk)"
VPN_PUB="$(cat wgclient.pub)"

※클라이언트 설정을 위해서, 생성된 키들의 값들 중에서 다음 키들을  확인하고 별도로 저장해두어야 한다!
root@WG_Server:~# cat wgclient.key 
oPPxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
root@WG_Server:~# cat wgclient.psk
XbUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
root@WG_Server:~# cat wgserver.pub
g6XrNLHkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk=

3. 방화벽
VPN zone을 private 영역으로 만든다.
방화벽 설정을 최소화하기 위해서, VPN zone을 LAN zone에 할당하여 합친다.

config zone 'lan'
option name 'lan'
list network 'lan'
list network 'vpn'
option input 'ACCEPT'
option output 'ACCEPT'
option forward 'ACCEPT'

그리고, WAN zone에서 VPN 서버로 연결을 허용한다.

config rule 'wg'
option name 'Allow-WireGuard'
option src 'wan'
option dest_port '51820'
option proto 'udp'
option target 'ACCEPT'

uci rename firewall.@zone[0]="lan"
uci rename firewall.@zone[1]="wan"
uci del_list firewall.lan.network="${VPN_IF}"
uci add_list firewall.lan.network="${VPN_IF}"
uci -q delete firewall.wg
uci set firewall.wg="rule"
uci set firewall.wg.name="Allow-WireGuard"
uci set firewall.wg.src="wan"
uci set firewall.wg.dest_port="${VPN_PORT}"
uci set firewall.wg.proto="udp"
uci set firewall.wg.target="ACCEPT"
uci commit firewall
service firewall restart

4. VPN 네트워크 인터페이스 설정과 peer 추가
1) Wireguard 인터페이스 설정
uci -q delete network.${VPN_IF}
uci set network.${VPN_IF}="interface"
uci set network.${VPN_IF}.proto="wireguard"
uci set network.${VPN_IF}.private_key="${VPN_KEY}"
uci set network.${VPN_IF}.listen_port="${VPN_PORT}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR6}"
 
2) VPN peers 추가
uci -q delete network.wgclient
uci set network.wgclient="wireguard_${VPN_IF}"
uci set network.wgclient.public_key="${VPN_PUB}"
uci set network.wgclient.preshared_key="${VPN_PSK}"
uci add_list network.wgclient.allowed_ips="${VPN_ADDR%.*}.2/32"
uci add_list network.wgclient.allowed_ips="${VPN_ADDR6%:*}:2/128"
uci commit network
service network restart

 

WireGuard client

0. 소프트웨어 설치
opkg update
opkg install luci-proto-wireguard wireguard-tools luci-app-wireguard

1. config용 파라미터 설정
vpn 포트는 udp 51820 이고, 클라이언트측 vpn 인터페이스 IP 주소는 172.16.9.2/24, fd00:9::2/64
VPN_SERV 항목에 반드시 서버측의 공인 IP주소 또는 도메인명을 적어준다. 
※ 서버측 vpn 인터페이스 IP 주소는 172.16.9.1/24, fd00:9::1/64

VPN_IF="vpn"
VPN_SERV="xxx.xxx.xxx.xxx"
VPN_PORT="51820"
VPN_ADDR="172.16.9.2/24"
VPN_ADDR6="fd00:9::2/64"
※ xxx.xxx.xxx.xxx 는 반드시 서버의 인터넷 공인 IP 또는 도메인명으로 교체해야 한다.


2. 암호화 키 생성 

※ 서버측에서 생성한 키를 그대로 사용해야만 한다!
1) 키 생성
umask go=
wg genkey | tee wgserver.key | wg pubkey > wgserver.pub
wg genkey | tee wgclient.key | wg pubkey > wgclient.pub
wg genpsk > wgclient.psk

 

root@WG_Server:~# cat wgclient.key 
oPPxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
root@WG_Server:~# cat wgclient.psk
XbUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
root@WG_Server:~# cat wgserver.pub
g6XrNLHkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk=


2) config용 키값 설정 
VPN_KEY="$(cat wgclient.key)"
VPN_PSK="$(cat wgclient.psk)"
VPN_PUB="$(cat wgserver.pub)"
VPN_KEY="oPPxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="
VPN_PSK="XbUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
VPN_PUB="g6XrNLHkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk="

3. 방화벽
VPN zone을 public 영역으로 만든다.


config zone 'wan'
option name 'wan'
list network 'wan'
list network 'wan6'
list network 'vpn'
option input 'REJECT'
option output 'ACCEPT'
option forward 'REJECT'
option masq '1'
option mtu_fix '1'


방화벽 설정을 최소화하기 위해서, VPN zone을 WAN zone에 할당하여 합친다.
uci rename firewall.@zone[0]="lan"
uci rename firewall.@zone[1]="wan"
uci del_list firewall.wan.network="${VPN_IF}"
uci add_list firewall.wan.network="${VPN_IF}"
uci commit firewall
service firewall restart

4. VPN 네트워크 인터페이스 설정과 peer 추가
1) Wireguard 인터페이스 설정
uci -q delete network.${VPN_IF}
uci set network.${VPN_IF}="interface"
uci set network.${VPN_IF}.proto="wireguard"
uci set network.${VPN_IF}.private_key="${VPN_KEY}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR6}"
 
2) VPN peers 추가
uci -q delete network.wgserver
uci set network.wgserver="wireguard_${VPN_IF}"
uci set network.wgserver.public_key="${VPN_PUB}"
uci set network.wgserver.preshared_key="${VPN_PSK}"
uci set network.wgserver.endpoint_host="${VPN_SERV}"
uci set network.wgserver.endpoint_port="${VPN_PORT}"
uci set network.wgserver.persistent_keepalive="25"
uci set network.wgserver.route_allowed_ips="1"
uci add_list network.wgserver.allowed_ips="0.0.0.0/0"
uci add_list network.wgserver.allowed_ips="::/0"
uci commit network
service network restart

Resolve race conditions를 해결하고, 필요하다면 동적연결(dynamic connection)을 구성한다.

 

----------------------------------------------------------------------

5. 테스팅
VPN 연결이 확립되면, traceroute 와  traceroute6 명령어로 확인한다.

traceroute openwrt.org
traceroute6 openwrt.org

현재 접속되고 있는 IP 와 DNS 설정을 확인한다.

클라이언트측에 연결된 PC에서 웹브라우저로 다음  URL에 접근해서, 현재 접속된 IP 주소를 확인해본다.

(서버측의 공인 IP 주소가 출력되어야 한다!)

ipleak.net
dnsleaktest.com

6. 문제 발생시, 확인할 사항들
다음의 정보를 분석하고 수집한다.
1) 서비스 재시작
service log restart; service network restart; sleep 10

2) 로그와 상태
logread -e vpn; netstat -l -n -p | grep -e "^udp\s.*\s-$"
 
3) Runtime 설정
pgrep -f -a wg; wg show; wg showconf vpn
ip address show; ip route show table all
ip rule show; ip -6 rule show; nft list ruleset

4) 지속 설정 
uci show network; uci show firewall; crontab -l

 

Posted by 훅크선장
, |