SW 개발

ipSec 사용하기 - strongswan 사용법 / debuging

. . . 2015. 7. 29. 22:47
반응형

일단 ipsec을 올려야할 일이 생겨서...

ㅋㅋㅋ;;

Step.1 : Linux <-> win7 사용하기

일단 우분투를 서버로 사용하기로 한다.

Linux 서버설정 : strong swan 설치

우분투에서... 일단 strong swan 을 설치한다.

sudo apt-get install strongswan


헐... 진짜 설치가된다!!

Linux 서버설정 : 서버인증서 설치 및 서비스 재시작..

이래저래 wiki 문서를 살펴보니 인증서를 설치해야 ipsec 서버를 사용가능할것 같다.


root@test-pc:/etc/ipsec.d# ls -al
합계 56
drwxr-xr-x  12 root root  4096 10월 29 13:02 .
drwxr-xr-x 163 root root 12288 10월 29 13:27 ..
drwxr-xr-x   2 root root  4096 10월 19  2011 aacerts
drwxr-xr-x   2 root root  4096 10월 19  2011 acerts
drwxr-xr-x   2 root root  4096 10월 19  2011 cacerts
drwxr-xr-x   2 root root  4096 10월 19  2011 certs
drwxr-xr-x   2 root root  4096 10월 19  2011 crls
drwxr-xr-x   2 root root  4096 10월 29 12:57 examples
drwxr-xr-x   2 root root  4096 10월 19  2011 ocspcerts
drwxr-xr-x   2 root root  4096 10월 29 12:57 policies
drwx------   2 root root  4096 10월 19  2011 private
drwxr-xr-x   2 root root  4096 10월 19  2011 reqs


윗중에 각각의 폴더에 알맞는 인증서를 설치해야하는듯하다;;

근데 위의 문서가 제대로 나와있지 않아서 구글검색을 하다보니 좋은 사이트 발견!!


위의 사이트에서 요약본을 따오면..
Create Certificate Authority (CA) Certificate

Generate the private key

openssl genrsa -des3 -out ca.key 4096


       ==> 제대로 strong swan 에서 사용하게 하려면 이부분에서 일단 ca.key 를 아래의 게시물을 참고하여 PKCS#1 로 변환시켜야한다!!!!

# mv ca.key ca8.key
# openssl rsa -in ca8.key -out ca.key

Generate the certificate request.  The answers to the questions aren’t relevant.  Common name (CN) is generally displayed so give that a useful name.

openssl req -new -key ca.key -out ca.csr



Sign the certificate request with the private key, in essence, creating the certificate.  This also adds on other information, such as expiration time.

openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt



Create VPN Server Certificate

The first two steps are the same.  Create private key.

openssl genrsa -des3 -out server.key 4096

      ==> 제대로 strong swan 에서 사용하게 하려면 이부분에서 일단 ca.key 를 아래의 게시물을 참고하여 PKCS#1 로 변환시켜야한다!!!!

# mv server.key server8.key
# openssl rsa -in server8.key -out server.key

Create the certificate request.  This time, the CN must be the FQDN of the VPN host.

openssl req -new -key server.key -out server.csr



The VPN certificate must have some special attributes set in order for Windows 7 to accept it.  Create gateway.conf with the following contents, with “<FQDN>” replaced with the FQDN, the same as the CN from above.

extendedKeyUsage = serverAuth, 1.3.6.1.5.5.8.2.2
subjectAltName = DNS:<FQDN>



Sign the certificate request with the CA private key and certificate.  This also adds on the additional info including that from the gateway.conf above.

openssl x509 -req -days 365 -in server.csr -CA ca.crt \
 -CAkey ca.key -set_serial 01 -out server.crt -extfile gateway.conf
위대로 할때 주의할점..

그대로 따라가도되는데... FQDN 부분을 CN 이랑 똑같이 맞춰야한다.

그리고...

The file names used in the config need to replaced with the ones generated above.  vpnCert.pem is server.crt, vpnKey.pem is server.key.  Copy those two files into the locations described by strongSwan’s instruction.

위의 말처럼 이름을 바꿔서 ipsec.d 폴더에 알아서 넣는다.

그리고... ipsec.conf 를 다음과 같이 설정한다.


그리고나서.. 커맨드창에... 다음과 같이 치면..

ipsec restart


설정파일대로 ipsec 서비스가 재시작

그러고나서 로그를 보면 에러가 발생한다!!

building CRED_PRIVATE_KEY - RSA failed, tried 8 builders

위의 에러를 검색해보니...
strong swan 이 PKCS#8 을 제대로 지원하지 않아서 발생하는 문제였다.

즉... openssl 로 인증서를 만들때 genrsa 명령어를 쓰면 기본적으로 PKCS#8 로 만들어지는듯...


그럼 PKCS#1 로 만들어야 하는데 위대로 인증서를 만들때부터 다시 만들어야 한다.

openssl 에서 PKCS#8 을 PKCS#1 로 변환하기

일단 genrsa 명령어로 만들어진것을 다음과 같이 한번 변환시킨다.

openssl rsa -in key8.pem -out key1.pem



위에대로 PKCS#1 로 변환시킨 인증서를 제대로 설치하고 ipsec 을 재설치 했다면...
정상적으로 서비스가 재시작할것이다!!!

strong swan Log 보기

일단 기본적으로 strongswan 은 syslogd 쪽으로 로그르 뿌린다.

tail -f /var/log/syslogd &


위와같이 켜놓고 테스트를 한다.

Client 설정 : win7 에 인증서 설치 및 접속


위와같이 설정하고 접속을 누르면!!!! 뭔가 데이터가 왔다갔다하는걸 linux ipsec 에서 볼수있다.!!

여기까지!!!!
... 인증서 문제로 뭔가 핸드쉐이킹으로 초기화하다가 실패;;;


Step2. Linux(Target) <-> Linux(Host) 사용하기

일단 단말에 4.4.0 을 크로스 컴파일해서 올린다.

후에 실행해보면.. 뭔가 문제가 발생한다.

네트워크 인터페이스도 잡고, 초기화는 시작했는데..
완료는 되지 않는다.

즉... ipsec.conf 에서 설정한 profile 이 ipsec 서비스에 등록이 되어야하는데...

예를들면, PC기준으로 로그를 다음과 비슷한 메시지가 나와야한다.

Nov  4 19:32:42 test-pc charon: 04[CFG] received stroke: add connection 'net-net'
Nov  4 19:32:42 test-pc charon: 04[CFG] added configuration 'net-net'


하지만 제대로 나오지 않는다.

문제 디버깅!!

이래저래 디버깅을 하다보니... 문제는 thread!!!


일단 해당 thread 의 갯수는 option  에서 조정가능하며, option 을 주지 않았을경우 디폴트로 16개가 생성되게 되어있다.

strongswan : Charon Thread 갯수 조절옵션

/etc/strongswan.conf 를 보면..
charon {

        # number of worker threads in charon
        threads = 7

        # plugins to load in charon
        # load = aes des sha1 md5 sha2 hmac gmp random pubkey xcbc x509 stroke

        load = aes des sha1 sha2 md5 gmp random nonce hmac stroke kernel-netlink socket-default updown
        # ...
}

위와같이 조절가능하다.


관련 옵션 문서 : http://wiki.strongswan.org/projects/strongswan/wiki/StrongswanConf


Number of worker threads in charon. Several of these are reserved for long running tasks in internal modules and plugins. Therefore, make sure you don't set this value too low. The number of idle worker threads listed in ipsec statusall might be used as indicator on the number of reserved threads (JobPriority has more on this).


결론은... 저 Thread 옵션을 적당한 갯수로하면... arm linux 시스템에서도 돌긴돈다는 뜻이다!!!!

실제 테스트 : net-net PSK 방식

step 1 에서 했던 인증서 방식을 테스트하려고했는데 잘 되지 않는관계로 그냥 PSK 인증키 교환방식으로 ㅋㅋ

strong swan 이 좋은 점은 예제가 많다는점!!!

여러 인증방식을 테스트해봤으나..
  --> 인증서 방식은 너무 무거워서 그런지 잘돌지 않음
  --> 다른 인증서 방식은 너무 어렵군;;;
 
그래서 그냥 PSK 방식으로 간단히 키교환 인증을 해보도록한다.

다음과 같은 방식으로 예제를 따라하면 된다.


위의 방식대로 하는데 주의해야할점..
  1 . strongswan.conf 의 thread 갯수를 조정해가면서 잘되는 값을 찾아야하고..
  2. ipsec.conf 에서 left / right 를 잘 구분해야한다.
     -  ==> left 는 자기 자신의 ip 정보
     -  ==> right 는 연결될 상대방의 ip 정보

위의 시나리오대로 하되.. ipsec.conf 를 설정만 잘하면 그냥 통신이된다.

config setup

conn %default
        ikelifetime=60m
        keylife=20m
        rekeymargin=3m
        keyingtries=1
        keyexchange=ikev1
        authby=secret
        type=tunnel

conn net-net
        left=192.168.20.31   ## 자기자신의 IP
        leftsourceip=10.1.0.1   ##ipsec 이 연결된후에 부여되는 private IP
        leftsubnet=10.1.0.0/16  
        leftid=@moon.strongswan.org
        leftfirewall=yes
        right=192.168.20.132   ## :: 연결할 GW 의 IP
        rightsubnet=10.2.0.0/16
        rightid=@sun.strongswan.org
        auto=add


위와같이 세팅하고..

ipsec up net-net 하면! 연결이 성공이 되고..
직접부여한 private IP 로 핑이 나가면성공!!!!!!

ipsec up net-net  명령어 후에 연결성공시의 단말에서 뿌리는 메시지

002 "net-net" #1: initiating Main Mode
104 "net-net" #1: STATE_MAIN_I1: initiate
003 "net-net" #1: received Vendor ID payload [strongSwan]
003 "net-net" #1: received Vendor ID payload [XAUTH]
003 "net-net" #1: received Vendor ID payload [Dead Peer Detection]
106 "net-net" #1: STATE_MAIN_I2: sent MI2, expecting MR2
108 "net-net" #1: STATE_MAIN_I3: sent MI3, expecting MR3
002 "net-net" #1: Peer ID is ID_FQDN: 'sun.strongswan.org'
002 "net-net" #1: ISAKMP SA established
004 "net-net" #1: STATE_MAIN_I4: ISAKMP SA established
002 "net-net" #2: initiating Quick Mode PSK+ENCRYPT+TUNNEL+PFS+UP {using isakmp#1}
112 "net-net" #2: STATE_QUICK_I1: initiate
<6>alg: No test for authenc(hmac(sha1),cbc(des3_ede)) (authenc(hmac(sha1-generic),cbc(des3_ede-generic)))
002 "net-net" #2: sent QI2, IPsec SA established {ESP=>0xc505c8fc <0x76700d85}
004 "net-net" #2: STATE_QUICK_I2: sent QI2, IPsec SA established {ESP=>0xc505c8fc <0x76700d85}


ipsec status


# ./ipsec status

000 "net-net": 10.1.0.0/16===192.168.20.31[moon.strongswan.org]...192.168.20.132[sun.strongswan.org]===10.2.0.0/16; erouted; eroute owner: #25

000 "net-net":   newest ISAKMP SA: #24; newest IPsec SA: #25; 

000 

000 #25: "net-net" STATE_QUICK_I2 (sent QI2, IPsec SA established); EVENT_SA_REPLACE in 571s; newest IPSEC; eroute owner

000 #25: "net-net" esp.ccd9e0c0@192.168.20.132 (0 bytes) esp.818f1e6e@192.168.20.31 (0 bytes); tunnel

000 #24: "net-net" STATE_MAIN_I4 (ISAKMP SA established); EVENT_SA_REPLACE in 2656s; newest ISAKMP

000 

Security Associations:

  none




반응형