本文转载自大神博客:
linux下自建ssh堡垒机:http://www.361way.com/linux-diy-fort-machine/3135.html
使用google Authenticator增强ssh: http://www.361way.com/google-authenticator-ssh/2186.html html
开源web终端ssh解决方案-gateone简介:http://itnihao.blog.51cto.com/1741976/1311506 linux
1、安装相关包git
一、安装相关包
yum -y install wget gcc make pam-devel libpng-develweb
二、安装qrencodechrome
wget http://fukuchi.org/works/qrencode/qrencode-3.4.1.tar.gz
tar zxf qrencode-3.4.1.tar.gz
cd qrencode-3.4.1
./configure --prefix=/usr && make && make install后端
该软件为非必须包,其主要用于在终端下生成二维码用。
三、google authenticator PAM插件安装
该插件有两种安装方式,一种是经过下载tar.gz包安装,一种是经过git下载安装。
tar.gz包安装:浏览器
wget http://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
tar jxf libpam-google-authenticator-1.0-source.tar.bz2
cd libpam-google-authenticator-1.0
make && make install安全
git下载安装:bash
git clone https://code.google.com/p/google-authenticator/
cd google-authenticator/libpam/
make install服务器
2、使用google Authenticator增强ssh:
ssh自己是一个很是安全的认证链接方式。不过因为人过等方面的缘由,不免会形成密码的泄露。针对这种问题咱们不妨给ssh再加一把锁。固然,增长这层锁的方式有不少种。例如:knockd、S/KEY、OPIE/OPTW、Two-factor authentication等(以上列举的几种方式中可能互相包含,有兴趣的能够去维基百科上查下,进而细分下)。而今天讲到的Google Authenticator就属于Two-factor authentication的表明。
一、配置google-authenticator
安装完成后会在/lib64/security/目录生成pam_google_authenticator.so文件,若是是32位系统会在/lib/security/目录生成。除此以外,系统还会多在/usr/local/bin目录生成一个google-authenticator可执行文件,运行该命令。
[root@localhost /]# google-authenticator
Do you want authentication tokens to be time-based (y/n) y
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@localhost.localdomain%3Fsecret%3DIO4SZL7YYHOTFNQJ
Your new secret key is: IO4SZL7YYHOTFNQJ
Your verification code is 236731
Your emergency scratch codes are:
98144901
30891929
79811882
84230327
53670228
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, tokens are good for 30 seconds and 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. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) n
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
注:
1.一、若是安装了qrencode包,会在出现google的连接后出一个二维码图标。方便手持设备扫描。固然,没装该包,能够打开https开头的url,打开也是一个二维码图片,同qrencode在终端下生成的是同样的。
1.二、"Your emergency scratch codes are:"下面的五个码用于在取不到或错的验证码有错时,用于应急用的。不过每一个只能用一次,不能重复使用。
1.三、第四处选择项是因为选择是否开启时间容错。若是客户端设备具备很好的同时间服务器同步功能,建议此处选择n。若是客户端设备(手机、ipad等)不具备很好的同ntp时间同步的功能,能够设为y —— 即开启时间容错。
二、ssh调用及客户端配置
2.一、sshd服务配置
按以上方式配置完成之后,是没有程序去调用的。想要ssh调用,还须要在pam.d认证上进行配置。打开 /etc/pam.d/sshd 文件在第一行添加
auth required pam_google_authenticator.so
再打开 /etc/ssh/sshd_config 文件
ChallengeResponseAuthentication no
把上面配置改为
ChallengeResponseAuthentication yes
service ssh restart重启ssh服务生效。
2.二、客户端配置
获取30秒一次的动态码的客户端是浏览器(仅支持chrome、firefox)、Android设备、苹果IOS设备、Blackberry、WP手持设备。各自程序的下载地址为:
chrome google-authenticator插件
firefox google-authenticator插件
手机应用下载地址 (该手机连接提示支持symbian系统,不过经本人手机测试,发现应用安装完之后帐户add没用反应)
账户名能够随意添写,安全key即为刚刚运行google-authenticator命令后生成的,add增长完成后。就会每过30秒新生成一个验证码。此时能够经过另一台主机链接的方式以下:
[root@localhost ~]# ssh root@192.168.10.14
Verification code:
Password:
Last login: Fri Jan 4 16:27:57 2013 from 192.168.10.11
[root@localhost ~]#
注:
一、若是命使用SecureCRT客户端登陆时,须要在 链接——属性——链接——ssh2——"鉴权/authenicaion" 中将 "键盘交互/keyboard Interactive" 调到第一位。否则链接会出错。
二、以上安全配置对于ssh密钥认证方式进行的登陆无效——即在pam配置中增长上面的配置后,经过密钥认证时不会要求输入验证码 。
3、 linux下自建ssh堡垒机
一、原理及原则
原理如上图所示,原则以下:
1、全部主机的链接都经过/etc/hosts.deny和/etc/hosts.allow控制只容许两台堡垒机链接(冗余考虑,避免一旦坏了全部主机都没法链接,这里图上只画了一个)。
二、全部主机关闭密码认证方式,只容许key认证。但切换用户可使用su - 用户名 的方式进行切换。
三、角色控制,虽然链接到堡垒机上的各用户名密码不一样,但后端全部的服务器按角色进行区分,如运维人员到后端服务器统一成oam用户、开发人员统一成dev用户。
四、堡垒机使用用户名+google authenticator+密码的方式认证。登陆用户在堡垒机上的权限是受chroot限制的。
二、ssh服务配置
在/etc/pam.d/sshd中首行添加
auth required pam_google_authenticator.so
在/etc/ssh/sshd_config中修改:
ChallengeResponseAuthentication 将no改成yes
UsePAM 将no改成yes
三、chroot配置
修改/etc/ssh/sshd_config:
ChrootDirectory /var/chroot
建立模拟系统环境:
mkdir /var/chroot/
cd /var/chroot/
mkdir {bin,dev,lib,lib64,etc,home}
建立块设备,并赋予全部用户可执行权限,null,zero,tty这三个模拟设备须要全部用户可写:
mknod dev/null c 1 3
mknod dev/zero c 1 5
mknod dev/random c 1 8
mknod dev/urandom c 1 9
mknod dev/tty c 5 0
chmod -R 755 /var/chroot/
chmod 0666 dev/{null,zero,tty}
注:这里块设备是在/var/chroot/dev目录下建立的,上面的dev前面是没加/的。mknod的用法也能够参看百度百科上的介绍。
复制lib库文件到chroot环境下:
#!/bin/bash
ldd /bin/bash /bin/ls /bin/cp /bin/mkdir /bin/mv /bin/rm /bin/rmdir|awk '{print $1}'|grep "/lib64" >> liblist
ldd /bin/bash /bin/ls /bin/cp /bin/mkdir /bin/mv /bin/rm /bin/rmdir|awk '{print $3}'|grep "/lib64" >> liblist
for libfile in `sort liblist|uniq`
do
cp -p $libfile /var/chroot/lib64/
done
特别注意的是还须要增长libnss库文件的支持,否则ssh登陆时会报“You don't exist, go away!”的错误,操做命令以下:
cp /lib64/libnss_* lib64/
以上操做都是64位系统下进行的,若是是32系统,直接使用lib目录下的库文件。
最后就是连接sh到bash,操做以下:
cd /var/chroot/bin
ln -s bash sh
四、建立用户及密钥文件
建立用户及passwd、group文件
useradd yang
passwd yang --abc123
grep ^yang /etc/passwd > /var/chroot/etc/passwd
grep ^yang /etc/group > /var/chroot/etc/group
建立用户chroot家目录
cd /var/chroot
mkdir home/yang
chown -R yang:yang home/yang
chmod 700 -R home/*
建立密钥文件
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yang/.ssh/id_rsa):/var/chroot/home/yang/.ssh/id_rsa
最后将生成的id_rsa.pub文件的内容传到后端受保护机相对应用户的.ssh/authorized_keys文件中,固然批量机器的话建议使用puppet、saltstack这类的自动化工具实现。
五、google认证
切换到相应的用户,例如,我这里是su - yang,执行google-authenticator命令,而后就是一路的按y,这里再也不列出,能够参看以前写的增强ssh安全的文章。
注:若要让某帐户不使用chroot环境,能够直接su到系统root帐户,则须修改/etc/ssh/sshd_config配置,在文件末尾添加:
Match User 用户名
ChrootDirectory /
六、后端服务器配置后端主机权限配置主要有如下几个地方能够操做6.一、只开启KEY认证,关掉密码认证RSAAuthentication yesPasswordAuthentication no6.二、设置hosts.allow和hosts.deny,指定容许远程链接的主机IP(堡垒机IP)如:#cat /etc/hosts.allowsshd:192.168.10.#cat /etc/hosts.denyALL:ALL记住hosts.allow的规则优先级高于hosts.deny就好了6.三、设置只容许ssh链接的用户(这个方法有好几种),本步略6.四、利用chattr给几个特定文件权限限定死,连root用户都只读而没法更改,本步略6.五、将用户操做命令记入日志(这个对bash有效,对个别csh等无效)建立/etc/profile.d/sshlog.sh文件,内容以下:PS1="`whoami`@`hostname`:"'[$PWD]'historyUSER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`if [ "$USER_IP" = "" ]then USER_IP=`hostname`fiif [ ! -d /var/log/history ]then mkdir /var/log/history chmod 777 /var/log/historyfiif [ ! -d /var/log/history/${LOGNAME} ]then mkdir /var/log/history/${LOGNAME} chmod 700 /var/log/history/${LOGNAME}fiexport HISTSIZE=4096DT=`date +"%Y%m%d_%H%M%S"`export HISTFILE="/var/log/history/${LOGNAME}/${USER_IP}_history.$DT"chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null在/var/log/history目录下,会在用户登陆并退出后,建立与用户名相同的目录,而且记录每次的登陆IP和时间,格式如:192.168.10.50_history.20130514_100547。6.六、利用/etc/sudoers命令给个别组用户一些特定权限(图个便利)例如:oam ALL=(ALL) NOPASSWD: /sbin/reboot, /sbin/ifconfig//oam为能够以root执行命令的用户,reboot、ifconfig为能够执行的命令,须要完整路径,多个命令用逗号加空格隔开