顯示具有 OpenVPN 標籤的文章。 顯示所有文章
顯示具有 OpenVPN 標籤的文章。 顯示所有文章

2015年11月4日 星期三

[Openvpn] Authentication method by using script and password file

1. Build up openvpn server (ubuntu) & client (windows)

2. Client configuration file settings
Client Part:
client
dev tun
proto udp
remote 111.2.1.50 1194
float
comp-lzo adaptive
keepalive 15 60
# Must use this option to authenticate user name and password
auth-user-pass
ns-cert-type server
<ca>
-----BEGIN CERTIFICATE-----
XXX
-----END CERTIFICATE-----
</ca>
<cert>
-----BEGIN CERTIFICATE-----
XXX
-----END CERTIFICATE-----
</cert>
<key>
-----BEGIN PRIVATE KEY-----
XXX
-----END PRIVATE KEY-----
</key>
resolv-retry infinite
nobind

3. Two method to set server configuration file

Method 1 :
Server Part:
# Automatically generated configuration
daemon
server 10.8.0.0 255.255.255.0
proto udp
port 1194
dev tun21
comp-lzo adaptive
keepalive 15 60
verb 3
push "route 192.168.1.0 255.255.255.0"
duplicate-cn
# plugin /usr/lib/openvpn-plugin-auth-pam.so openvpn
ca    /home/gogo/test/ca.crt
dh    /home/gogo/test/dh.pem
cert  /home/gogo/test/server.crt
key   /home/gogo/test/server.key
status-version 2
status status

;script-security 3 system
# via-env very important
auth-user-pass-verify /home/gogo/test/checkpassword.sh via-env

# Custom Configuration

Start up server by command
/usr/sbin/openvpn --daemon ovpn-server --cd /home/gogo/test --config config.ovpn --script-security 3

Method 2 :
Server Part:
# Automatically generated configuration
daemon
server 10.8.0.0 255.255.255.0
proto udp
port 1194
dev tun21
comp-lzo adaptive
keepalive 15 60
verb 3
push "route 192.168.1.0 255.255.255.0"
duplicate-cn
# plugin /usr/lib/openvpn-plugin-auth-pam.so openvpn
ca    /home/gogo/test/ca.crt
dh    /home/gogo/test/dh.pem
cert  /home/gogo/test/server.crt
key   /home/gogo/test/server.key
status-version 2
status status

script-security 3 system
auth-user-pass-verify /home/gogo/test/checkpassword.sh via-env

# Custom Configuration

Start up server by command
/usr/sbin/openvpn --daemon ovpn-server --cd /home/gogo/test --config config.ovpn

Reference:

vim checkpassword.sh
#!/bin/sh
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman <mathias@openvpn.se>
#
# This script will authenticate OpenVPN users against
# a plain text file. The passfile should simply contain
# one row per user with the username first followed by
# one or more space(s) or tab(s) and then the password.

PASSFILE="/home/gogo/test/password-file"
LOG_FILE="/var/log/openvpn/openvpn-password.log"
TIME_STAMP=`date "+%Y-%m-%d %T"`
###########################################################

if [ ! -r "${PASSFILE}" ]; then
  echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >> ${LOG_FILE}
  exit 1
fi

CORRECT_PASSWORD=`awk '!/^;/&&!/^#/&&$1=="'${username}'"{print $2;exit}' ${PASSFILE}`

if [ "${CORRECT_PASSWORD}" = "" ]; then
  echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
  exit 1
fi

if [ "${password}" = "${CORRECT_PASSWORD}" ]; then
  echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE}
  exit 0
fi

echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
exit 1
chmod +x checkpassword.sh






Create password file
# echo "test1 test1" > password-file
# chmod 400 password-file

2015年9月9日 星期三

2015年9月4日 星期五

[OpenVPN Client] How to Use OpenVPN Client in any linux distro



# openvpn --config /var/tmp/happy.ovpn --tmp-dir /var/tmp

Fri Sep  4 17:05:39 2000 OpenVPN 2.3.7 armeb-unknown-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [MH] [IPv6] built on Sep  4 2000
Fri Sep  4 17:05:39 2000 library versions: OpenSSL 1.0.1 14 Mar 2012, LZO 2.08
Enter Auth Username:
Enter Auth Password:
Fri Sep  4 17:05:49 2000 UDPv4 link local: [undef]
Fri Sep  4 17:05:49 2000 UDPv4 link remote: [AF_INET]222.6.3.52:1194
Fri Sep  4 17:05:49 2000 WARNING: this configuration may cache passwords in memory -- use the auth-nocache option to prevent this
Fri Sep  4 17:05:51 2000 [happy] Peer Connection Initiated with [AF_INET]222.6.3.52:1194
Fri Sep  4 17:05:53 2000 TUN/TAP device tun0 opened
Fri Sep  4 17:05:53 2000 do_ifconfig, tt->ipv6=0, tt->did_ifconfig_ipv6_setup=0
Fri Sep  4 17:05:53 2000 /sbin/ifconfig tun0 10.8.0.10 pointopoint 10.8.0.9 mtu 1500
Fri Sep  4 17:05:53 2000 Initialization Sequence Completed

Reference:

2015年8月31日 星期一

[Openvpn] Server configuration settings


# Automatically generated configuration
daemon
server 10.10.0.0 255.255.255.0
proto udp
port 1194
dev tun21
cipher AES-128-CBC
comp-lzo no
keepalive 15 60
verb 3
# This is very important part for WAN communication with LAN
# Without this option, the server don’t know how to route
push “route 192.168.1.0 255.255.255.0”
duplicate-cn
;plugin /usr/lib/openvpn-plugin-auth-pam.so openvpn
ca /tmp/ca.crt
dh /tmp/dh.pem
cert /tmp/server.crt
key /tmp/server.key
status-version 2
status /var/tmp/status
# Custom Configuration

2015年8月13日 星期四

[easy-rsa][Gentoo] How to use easy-rsa


Copy easy-rsa to home directory
cp -prv /usr/share/easy-rsa ~

Server Part:

Install environment settings

source ./vars

run ./clean-all, I will be doing a rm -rf on /home/vagrant/easy-rsa/keys

./clean-all

create ca.crt

vagrant@local ~/easy-rsa $ ./build-ca 
Generating a 2048 bit RSA private key
............................................................................+++
................+++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [US]:
State or Province Name (full name) [CA]:
Locality Name (eg, city) [SanFrancisco]:
Organization Name (eg, company) [Fort-Funston]:
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:
Common Name (eg, your name or your server's hostname) [Fort-Funston CA]:
Name [EasyRSA]:
Email Address [me@myhost.mydomain]:

Generate a certificate (public key) and private key for the server

vagrant@local ~/easy-rsa $ ./build-key-server happy
Generating a 2048 bit RSA private key
......................+++
.............................+++
writing new private key to 'happy.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [US]:
State or Province Name (full name) [CA]:
Locality Name (eg, city) [SanFrancisco]:
Organization Name (eg, company) [Fort-Funston]:
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:
Common Name (eg, your name or your server's hostname) [happy]:
Name [EasyRSA]:
Email Address [me@myhost.mydomain]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /home/vagrant/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'US'
stateOrProvinceName   :PRINTABLE:'CA'
localityName          :PRINTABLE:'SanFrancisco'
organizationName      :PRINTABLE:'Fort-Funston'
organizationalUnitName:PRINTABLE:'MyOrganizationalUnit'
commonName            :PRINTABLE:'happy'
name                  :PRINTABLE:'EasyRSA'
emailAddress          :IA5STRING:'me@myhost.mydomain'
Certificate is to be certified until Aug 10 07:15:27 2025 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

Generate Diffie Hellman

vagrant@local ~/easy-rsa $ ./build-dh 
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
............................................................................................................................................+....................+...................+.........................................................................+..........+........................+.................................................................+......................................+...............................................................................................................................................................................................................................................+................................................................................................+......................+................+..............+..+.........................................................................................................................................................................++*++*

Client Part:

Create Client Certificates

vagrant@local ~/easy-rsa $ ./build-key client1  # client1 is filename
Generating a 2048 bit RSA private key
.............+++
............+++
writing new private key to 'client1.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [US]:
State or Province Name (full name) [CA]:
Locality Name (eg, city) [SanFrancisco]:
Organization Name (eg, company) [Fort-Funston]:
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:
Common Name (eg, your name or your server's hostname) [client1]:
Name [EasyRSA]:
Email Address [me@myhost.mydomain]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /home/vagrant/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'US'
stateOrProvinceName   :PRINTABLE:'CA'
localityName          :PRINTABLE:'SanFrancisco'
organizationName      :PRINTABLE:'Fort-Funston'
organizationalUnitName:PRINTABLE:'MyOrganizationalUnit'
commonName            :PRINTABLE:'client1'
name                  :PRINTABLE:'EasyRSA'
emailAddress          :IA5STRING:'me@myhost.mydomain'
Certificate is to be certified until Aug 10 07:18:34 2025 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

Reference:

2015年8月6日 星期四

[OpenVPN] Incoming packet rejected

Q:
Thu Feb 06 00:00:00 2015 TCP/UDP: Incoming packet rejected from [AF_INET]192.6.1.191:1194[2], expected peer address: [AF_INET]192.6.3.52:1194 (allow this incoming source address/port by removing –remote or adding –float)

Client OpenVPN Part :
Add “float” into script

Reference:

2015年8月4日 星期二

[OpenVPN Client] How to Use OpenVPN Client


Put following file under C:\Program Files\OpenVPN\config

Client.crt
Client.key
ca.crt
Client.ovpn

OpenVPN GUI work



The content of Client.ovpn

##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one.  On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
;proto tcp
proto udp

# 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
remote 10.10.10.1 1194

# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nogroup

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca ca.crt
cert Client.crt
key Client.key

# Verify server certificate by checking
# that the certicate has the nsCertType
# field set to "server".  This is an
# important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the nsCertType
# field set to "server".  The build-key-server
# script in the easy-rsa folder will do this.
ns-cert-type server

# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20