본문 바로가기
정보기술/일반

OpenVPN 을 이용한 Traffic 우회 방법

by fermi 2013. 2. 7.

1. OpenVPN Server 설치


http://openvpn.net/index.php/open-source/downloads.html


Windows XP를 이용할 경우 openvpn-install-2.3.0-I001-i686.exe 를 설치한다.


2. Server Key 생성


참고: http://jaesoo.com/index.php?mid=study_board&document_srl=7622


1) c:\program files\openvpn\easy-rsa 폴더로 이동


2) init-config.bat 파일 실행


3) texteditor로 vars.bat 파일을 열어 파일 맨 하단부에서 다음 부분을 찾는다.


set KEY_̲COUNTRY=US

set KEY_̲PROVINCE=CA

set KEY_̲CITY=SanFrancisco

set KEY_̲ORG=FortFunston

set KEY_̲EMAIL=mail@host.domain


4) 여기서 다음과 같이 수정


set KEY_̲COUNTRY=KR

set KEY_̲PROVINCE=Seoul

set KEY_̲CITY=Seoul

set KEY_̲ORG=your_org_name

set KEY_̲EMAIL=foo@foo.com



5) DOS Command 창으로 c:\program files\openvpn\easy-rsa 폴더에 들어가서 다음을 차례대로 실행


vars.bat 실행

clean-all.bat 실행

build-ca.bat 실행


그냥 ENTER를 계속 누르되 common name/host name은 server로 입력. 잘못 입력했으면 다시 build-ca.bat를 실행하여 입력


6) 서버 인증서 생성


DOS Command 창으로 c:\program files\openvpn\easy-rsa 폴더에 들어가서 다음을 실행


build-key-server server


- 그냥 ENTER를 계속 누르되 common name/host name은 server로 입력

- A challenge password는 적당한것을 입력

- y/n을 묻는 부분에서는 무조건 y


7) 클라이언트 인증서 생성


DOS Command 창으로 c:\program files\openvpn\easy‒rsa 폴더에 들어가서 다음을 실행


build‒key client


- 그냥 ENTER를 계속 누르되 common name/host name은 client로 입력

- A challenge password는 적당한것을 입력

- y/n을 묻는 부분에서는 무조건 y


※ 접속할 사용자가 여러명이면 다음과 같이 인증서를 여러개 만들 수 있다.


ex)

build-key fermi

build-key vpn


8) DH 파라메터 생성


DOS Command 창으로 c:\program files\openvpn\easy‒rsa 폴더에 들어가서 다음을 실행


build-dh.bat


9) 인증서 생성 마무리


c:\program files\openvpn\easy-rsa\keys에서 다음의 파일들을 c:\program files\openvpn\config 디렉토리에 복사


- ca.crt

- dh1024.pem

- server.crt

- server.key


다음의 파일은 클라이언트에서의 사용을 위해 별도로 저장해둔다.

(아무나 막 사용할 수도 있으므로 안전하게 저장해 두는 것이 좋다.)


- ca.crt

- client.crt

- client.key



3. VPN 서버 환경 설정


c:\program files\openvpn\sample‒config에서 server.ovpn 파일을 복사하여 c:\program files\openvpn\config 폴더에 복사한다.


4. 방화벽 해제 (8. Server의 NAT 설정을 수행할 경우 필요 없음)


Windows 방화벽이 설정되어져 있다면 다음과 같이 예외설정을 해준다.


이름: OpenVPN

프로토콜: UDP

포트번호: 1194



5. VPN 서버 실행


시작 > 프로그램 > OpenVPN > OpenVPN GUI 실행 (오른쪽 하단에 빨간색 아이콘이 생긴다)


오른쪽 버튼을 눌러 Popup Menu를 띄우고 Connect버튼을 누른다.


녹색으로 변신하면 OK, 실패했다면 c:\program files\openvpn\easy‒rsa\keys 폴더를 지우고 2. Key 생성을 처음부터 다시 한다.


6. 서버를 자동 실행시키고 싶은 경우

제어판 > 관리도구 > 서비스 > OpenVPN 자동실행으로 변경


7. 접속된 클라이언트의 모든 트래픽을 VPN 서버로 보내기


c:\program files\openvpn\config\server.ovpn 파일에 다음 부분을 찾아서 추가한다.


;push "redirect-gateway def1 bypass-dhcp"


push "redirect-gateway def1 bypass-dhcp"


(클라이언트의 모든 트래픽을 VPN으로 우회)


클라이언트 IP를 할당할 때 DNS를 지정하고 싶으면 다음 부분을 찾아서 추가한다.


;push "dhcp-option DNS 208.67.222.222"

;push "dhcp-option DNS 208.67.220.220"


push "dhcp-option DNS 8.8.8.8"


(Google DNS로 지정)


8. Server의 NAT 설정


참고: http://www.nanodocumet.com/?p=14


클라이언트의 모든 트래픽을 VPN으로 우회한 후, 인터넷이 되도록 하기 위해서는 서버의 NIC에 NAT 설정을 해주어야 한다.

(The OpenVPN server machine may need to NAT or bridge the TUN/TAP interface to the internet in order for this to work properly.)


Windows XP의 자체 NAT 기능을 설정하기 위하여 다음을 진행한다.

(Follow these steps to have your Windows XP to work as a NAT but without the restrictions of ICS (Internet Connection Sharing)


1) Requirements:


A. To have two network cards:


NIC1: One connected to the internal network (any network not only 192.168.1.0).

NIC2: The other one connected to the Internet (or other network you want to connect to)


B. To have administrative access to stop/start some services.


2) Steps:


A. Disable ICS on both NICs.


Right-click on NIC -> Properties -> Advanced. Make sure Allow other network users to connect through this computer’s Internet connection is unchecked.


B. Enable Routing and Remote Access Service.


Control Panel -> Administrative Tools -> Services. Enable Routing and Remote Access


C. Disable Firewall/ICS Service (which might take precedence over step 1)


Control Panel -> Administrative Tools -> Services. Disable Windows Firewall/Internet Connection Sharing (ICS)


D. Configure the NICs:


Only 1 should have a gateway (In my example is NIC2). Disable the gateway on the other NIC (NIC1)


Add the NAT configuration.

- You still need to find out the name of your network cards. Here are presenting them as NIC1 and NIC2. In reality they could be something like “Local Area Connection”


type the command "netsh"


netsh


pushd routing ip nat

uninstall

install

set global tcptimeoutmins=1440 udptimeoutmins=1 loglevel=ERROR

add interface name="NIC2" mode=FULL

add interface name="NIC1" mode=PRIVATE

popd



9. OpenVPN 클라이언트 설치


- iOS 와 Android 디바이스들은 App Store 와 Market 에서 다운로드

- Mac OS X 용 클라이언트: http://code.google.com/p/tunnelblick

- Windows: http://openvpn.net/index.php/open-source/downloads.html


10. 클라이언트 환경설정


1) Key 복사


2의 8)에서 저장해둔 ca.crt, client.crt, client.key파일을 c:\program files\openvpn\config 폴더에 복사한다.


2) 환경설정


c:\program files\openvpn\sample-config에서 client.ovpn 파일을 복사하여 c:\program files\openvpn\config 폴더에 복사한 후 Notepad로 열어 다음의 부분을 찾는다.


# The hostname/IP and port of the server. # You can have multiple remote entries

# to load balance between the servers

;remote my‒server‒1 1194

;remote my‒server-2 1194


여기서 아래와 같이 my-server-1을 접속하고자 하는 서버의 domain name 또는 IP Address로 적어준 후 파일을 저장한다.


remote vpn.xxx.net 1194


3) 환경설정 적용


대부분의 클라이언트에서 다음 파일들을 import 할 수 있다.


ca.crt

client.crt

client.key

client.ovpn



11. Ubuntu 에서 OpenVPN Server 설치


NAT 면에서 Ubuntu Server LTS 최신 버전을 이용하는 것이 Windows 보다 잇점이 있다.


https://help.ubuntu.com/12.04/serverguide/openvpn.html


http://openvpn.net/index.php/open-source/documentation/howto.html#redirect

http://blog.wains.be/2008/07/18/openvpn-routing-all-traffic-through-the-vpn-tunnel/

http://codeghar.wordpress.com/2012/05/02/ubuntu-12-04-ipv4-nat-gateway-and-dhcp-server/


On Linux, you could use a command such as this to NAT the VPN client traffic to the internet:


1) Enable IP forwarding


/etc/sysctl.conf 파일을 열어서 “net.ipv4.ip_forward” line을 찾아 set it to 1


# Uncomment the next line to enable packet forwarding for IPv4

net.ipv4.ip_forward = 1


2) iptable 수정을 자동으로 하기


/etc/rc.local 파일을 열어서 exit 0 줄 위에 다음 한 줄을 추가

/sbin/iptables --table nat -A POSTROUTING -o eth0 -j MASQUERADE


/sbin/iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

exit 0