一、双因素认证
双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统。双因素认证是一种采用时间同步技术的系统,采用了基于时间、事件和密钥三变量而产生的一次性密码来代替传统的静态密码。每个动态密码卡都有一个唯一的密钥,该密钥同时存放在服务器端,每次认证时动态密码卡与服务器分别根据同样的密钥,同样的随机参数(时间、事件)和同样的算法计算了认证的动态密码,从而确保密码的一致性,从而实现了用户的认证。
说白了,就像我们几年前去银行办卡送的口令牌,以及网易游戏中的将军令,在你使用网银或登陆游戏时会再让你输入动态口令的。
二、产品种类
市面上有基于硬件的,也有基于软件的产品,具体可以另搜啊,本人喜欢开源的东东,并找到了Google开源的二次认证系统Google Authenticator OpenSource,可以利用智能手机生产30秒动态口令配合登陆linux系统,该验证器提供了一个六位数的一次性密码。目前ios 和Android 都有客户端供于下载。
三、功能
1.实现登陆linux服务器时,先输入动态口令,认证成功后,在下一步输入用户密码。如果口令失败,不会进行下一步的本地密码认证。
2.实现连接OpenVPN服务器时,结合客户端证书,通过动态验证码认证登录。
3.部署完成后,即使服务器不能上网,或者手机客户端不能上网,整个二步验证系统还是可以正常运行的。
四、部署
1.环境:
CentOS6.5 X86_64 IP:192.168.28.130
2.iptables及selinux:
关闭iptables
iptables -F && service iptables stop
关闭selinux(否则chrony无法正常启动)
sed -i 's@SELINUX=enforcing@SELINUX=disabled@' /etc/selinux/config
临时关闭:setenforce 0
查询状态:getenforce
3.软件依赖
第三方源:epel-release
编译所需:gcc gcc-c++ auotmake auotconf make libpng-devel libtool wget git
认证所需:chrony pam-devel google-authenticator qrencode libpng libpng-devel
4.安装第三方源及编译工具
yum install -y epel-release
yum install -y gcc gcc-c++ auotmake auotconf make libpng-devel libtool wget git
5.安装认证所需
yum install pam-devel pam pam_kr5 samba-winbind -y
6.安装chrony,动态口令在验证时用到了时间,所以要保持时间上的一致性,chrony是网络时间协议的(NTP)的另一种实现,与网络时间协议后台程序(ntpd)不同,它可以更快地更准确地同步系统时钟。如果要使用ntp需要单独安装。
yum install chrony -y
service chronyd start && chronyc sources && chkconfig chronyd on
或
yum install ntpdate -y
crontab添加
*/1 * * * * /usr/sbin/ntpdate 0.rhel.pool.ntp.org > /dev/null 2>&1
service crond restart
7.核对时区
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
8.谷歌github将libpam认证模块放到一个单独项目内,下面是下载源码方式:
git clone https://github.com/google/google-authenticator-libpam.git
cd google-authenticator-libpam
./bootstrap.sh && ./configure && make && make install
9.将pam_google_authenticator.so复制或者链接至/lib64/security/,防止下一步添加报错
cp -a /usr/local/lib/security/pam_google_authenticator.so /lib64/security/pam_google_authenticator.so
或
ln -s /usr/local/lib/security/pam_google_authenticator.so /lib64/security/pam_google_authenticator.so
10./etc/pam.d/下创建认证配置文件
vim /etc/pam.d/openvpn
account [success=2 new_authtok_reqd=done default=ignore] pam_unix.so
account [success=1 new_authtok_reqd=done default=ignore] pam_winbind.so
account requisite pam_deny.so
account required pam_permit.so
11.安装openvpn
yum install openssl openssl-devel lzo lzo-devel pam pam-devel pam_mysql automake pkgconfig gcc gcc-c++
cd /usr/local/src/
tar -zxvf openvpn-2.4.4.tar.gz
cd openvpn-2.4.4
./configure --prefix=/opt/openvpn && make && make install
cp -a sample/sample-config-files/server.conf /opt/openvpn/
#最好放在/opt/openvpn/下
cp -a distro/rpm/openvpn.init.d.rhel /etc/init.d/openvpn
#创建启动脚本
ln -s /opt/openvpn/sbin/openvpn /usr/sbin/openvpn
#启动脚本中会用到,也可以不执行此命令,直接在启动脚本中修改
vi /etc/init.d/openvpn
#在85行,修改为:work=/opt/openvpn
cd /opt/openvpn/ && mv server.conf server.conf.bak
vi server.conf #修改配置文件; ';'为注释
local 192.168.28.130
port 1194
proto udp
dev tun
topology subnet
ca keys/ca.crt
cert keys/server.crt
key keys/server.key
dh keys/dh2048.pem
tls-auth keys/ta.key 0
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 10.8.0.0 255.255.0.0"
route 192.168.28.130 255.255.255.0 10.8.0.1
keepalive 10 120
comp-lzo
persist-key
persist-tun
cipher AES-256-CBC
verb 3
status logs/openvpn-status.log
log logs/openvpn.log
log-append logs/openvpn.log
plugin /opt/openvpn/openvpn-auth-pam.so openvpn
reneg-sec 0
mkdir logs #创建日志目录
chown nobody:nobody logs/ -R
mkdir keys #创建key目录
生成openvpn-auth-pam.so,使用openvpn-2.0.*.tar.gz源码,否则会报错:AUTH-PAM:BACKGROUND: user 'tester' failed to authenticate: Permission denied
tar -zxvf openvpn-2.0.9.tar.gz
cd open-2.0.9/plugin/auth-pam
make
cp -a openvpn-auth-pam.so /opt/openvpn
cd /opt/openvpn && chmod 755 openvpn-auth-pam.so
安装easy-rsa,用来生成证书和密钥:
cd /usr/local/src/
wget http://download.freenas.org/distfiles/easy-rsa-2.2.0_master.tar.gz
#http://download.freenas.org/distfiles/
tar -zxvf easy-rsa-2.2.0_master.tar.gz
cp -a easy-rsa-2.2.0_master/easy-rsa /opt/openvpn/
cd /opt/openvpn/easy-rsa/2.0/
mv vars vars.bak
vi vars #修改配置文件
export EASY_RSA="`pwd`"
export OPENSSL="openssl"
export PKCS11TOOL="pkcs11-tool"
export GREP="grep"
export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`
export KEY_DIR="$EASY_RSA/keys"
echo NOTE: If you run ./clean-all, I will be doing a rm -rf on $KEY_DIR
export PKCS11_MODULE_PATH="dummy"
export PKCS11_PIN="dummy"
export KEY_SIZE=2048 #修改为2048
export CA_EXPIRE=3650
export KEY_EXPIRE=3650
export KEY_COUNTRY="CN" #以下根据自己情况修改
export KEY_PROVINCE="ShaanXi"
export KEY_CITY="XA"
export KEY_ORG="yjz"
export KEY_EMAIL="xx@yjz.cn"
export KEY_CN=yjz
export KEY_NAME=yjz
export KEY_OU=yjz
ln -s openssl-1.0.0.cnf openssl.cnf
source vars
#全局变量
##生成证书,以下命令全部一直回车
./clean-all
#清空所有证书(keys目录下)
./build-ca
#生成服务器ca证书
./build-key-server server
#生成服务端证书
./build-dh
#生成DH验证文件(dh2048.pem)
openvpn --genkey --secret ta.key
#降低DDoS风险
./build-key client
#生成客户端证书(建议以使用者命名)
设置外网访问:
vim /etc/sysctl.conf #将net.ipv4.ip_forward = 0改为1
sysctl -p
配置nat表将vpn网段IP转发到server内网:很重要
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
#注意接口(eth0)是内网的接口,其它选项不要修改
iptables -A INPUT -p TCP --dport 1194 -j ACCEPT
#开启防火墙1194端口
service iptables restart #POSTROUTING需要保存并重启服务才能生效
chkconfig iptables on
启动服务
#拷贝证书到/opt/openvpn/keys目录下
cd /opt/openvpn/easy-rsa/2.0/keys/
cp -a ca.crt server.crt dh2048.pem server.key /opt/openvpn/keys
cd .. && cp ta.key /opt/openvpn/keys
/etc/init.d/openvpn start
chkconfig openvpn on
12.安装二维码生成工具(可选)
yum install qrencode libpng libpng-devel -y
13.命令行运行google-authenticator命令,将会在当前登陆用户的家目录中生成一个新的密钥
google-authenticator
#用于配置google-authenticator的用户名即是连接OpenVPN时需要输入的用户名
Do you want authentication tokens to be time-based (y/n) y
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@Test1%3Fsecret%3DSSY64ZPQHPEGFRQIU4YNPOZQEU%26issuer%3DTest1
这里是生成的二维码图片
Your new secret key is: SSY64ZPQHPEGFRQIU4YNPOZQEU
Your verification code is 782390
Your emergency scratch codes are:
60320384
44748688
51040059
54687098
43300392
Do you want me to update your "/root/.google_authenticator" file? (y/n) y
Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y
By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y
If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) y
连续输入5个yes(第1个选no,基于计数的令牌,可选)
第1个:问你是否想做一个基于时间的令牌
第2个:是否更新你的google认证文件,由于第一次设置,所以一定选y
第3个:是否禁止口令多用,这里选择y,禁止它,以防止中间人欺骗
第4个:默认情况,1个口令的有效期是30s,这里是为了防止主机时间和口令客户端时间不一致,设置的误差,可以选择y也可选n,看要求严谨程度
第5个:是否打开尝试次数限制,默认情况,30s内不得超过3次登陆测试,防止别人暴力破解。
14.上述设置将被存储在用户的〜/.google_authenticator文件中,Your emergency scratch codes are: 中的5个代码是紧急代码,请牢记,这是在你的动态口令无法使用的情况下使用的,记住,用一个失效一个。后期可登录重新生成!!
确保/home/username/.google_authenticator除了使用它的用户具有读取权限,其他用户没有任何权限
15.手机安装谷歌身份验证器,苹果手机打开app store搜索应用Google Authenticator,安卓系统打开相应的应用进行安装即可,我使用的是安卓系统,安装安卓版(我使用GO谷歌安装器安装谷歌相关的套件)
16.打开APP后点击“开始设置”,扫描终端的二维码,需要安装专用的条码扫描器,其他软件都无法扫描二维码无效,上一步已安装好身份认证器,直接扫描即可。(或使用秘钥的方式,根据具体的情况具体操作)
17.从VPN服务端下载ca.crt,client.crt,client.key,ta.key证书
将ca.crt,client.crt,client.key,ta.key证书放到C:/Program Files/OpenVPN/config目录下
vim client.ovpn
client
dev tun
proto udp
remote 192.168.28.130 1194
resolv-retry infinite
remote-random
nobind
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
tls-auth ta.key 1
cipher AES-256-CBC
auth-user-pass
auth-nocache
remote-cert-tls server
comp-lzo
需要输入的用户名为当前登录服务器的用户名,获取动态验证码,连接OpenVPN