SSHGoogleAuthenticator安全加固
1。SSH连接
SecureShell(安全外壳协议,简称SSH)是一种加密的网络传输协议,可在不安全的网络中为网络服务提供安全的传输环境。SSH通过在网络中创建安全隧道来实现SSH客户端与服务器之间的连接。虽然任何网络服务都可以通过SSH实现安全传输,SSH最常见的用途是远程登录系统,人们通常利用SSH来传输命令行界面和远程执行命令。使用频率最高的场合类Unix系统,但是Windows操作系统也能有限度地使用SSH。
SSH本身是一个非常安全的认证连接方式。不过由于人过等方面的原因,难免会造成密码的泄露。针对这种问题我们不妨给SSH再加一把锁。当然,增加这层锁的方式有很多种。例如:knockd、SKEY、OPIEOPTW、Twofactorauthentication等。
2。GoogleAuthenticator
Google身份验证器是一款基于时间与哈希的一次性密码算法的两步验证软件令牌,此软件用于Google的认证服务。此项服务所使用的算法已列于RFC6238和RFC4226中。
Google身份验证器给予用户一个六位到八位的一次性密码用于进行登录Google或其他站点时的附加验证。其同样可以给第三方应用生成口令,例如密码管家程序或网络硬盘。先前版本的Google身份验证器开放源代码,但之后的版本以专有软件的形式公开。
3。Linux中安装
3。1系统环境说明
〔rootclsn。ioroot〕clsn。ioBlogWebSite
catetcredhatrelease
CentOSrelease6。8(Final)
〔rootclsn。ioroot〕clsn。ioBlogWebSite
unamea
Linuxclsn。io4。10。51。el6。elrepo。x86641SMPWedMar2214:55:33EDT2017x8664x8664x8664GNULinux
〔rootclsn。ioroot〕clsn。ioBlogWebSite
sestatus
SELinuxstatus:disabled
3。2安装GoogleAuthenticator
3。2。1安装依赖包
yumyinstallwgetgccmakepamdevellibpngdevel
3。2。2GoogleAuthenticatorPAM插件安装
可在google的github下载
wgethttps:github。comgooglegoogleauthenticatorarchive1。02。tar。gz
tarxf1。02。tar。gz
cdgoogleauthenticator1。02libpam
。bootstrap。sh
。configure
makemakeinstall
安装完成后会在usrlocallibsecuritypamgoogleauthenticator。so生成一个库文件,
系统还会多在usrlocalbin目录生成一个googleauthenticator可执行文件,通过运行该命令进行配置。
3。2。3复制so文件
cpusrlocallibsecuritypamgoogleauthenticator。solib64security
4。配置SSHGoogleAuthenticator
4。1初始配置GoogleAuthenticator
〔rootclsn。iolib64security〕clsn。ioBlogWebSite
googleauthenticator
Doyouwantauthenticationtokenstobetimebased(yn)n
是否基于时间的认证,为了防止不同跨时区的问题,这里选择n
https:www。google。comchart?chs200x200chldM0chtqrchlotpauth:hotprootclsn。io3Fsecret326issuer3Dclsn。io
s生成的二维码
Yournewsecretkeyis:
Yourverificationcodeis50
Youremergencyscratchcodesare:
4084
1995
6078
8392
3158
这5个码用于在取不到或错的验证码有错时,用于应急用的。不过每个只能用一次,不能重复使用。
Doyouwantmetoupdateyourroot。googleauthenticatorfile?(yn)y
Bydefault,threetokensarevalidatanyonetime。Thisaccountsfor
generatedbutnotusedtokensandfailedloginattempts。Inorderto
decreasethelikelihoodofsynchronizationproblems,thiswindowcanbe
increasedfromitsdefaultsizeof3to17。Doyouwanttodoso(yn)y
Ifthecomputerthatyouareloggingintoisnthardenedagainstbruteforce
loginattempts,youcanenableratelimitingfortheauthenticationmodule。
Bydefault,thislimitsattackerstonomorethan3loginattemptsevery30s。
Doyouwanttoenableratelimiting(yn)y
4。2SSH调用及客户端配置
添加pam认证,在第一行添加
vimetcpam。dsshd
authrequiredpamgoogleauthenticator。so
catetcpam。dsshd
PAM1。0
authrequiredpamsepermit。so
authrequiredpamgoogleauthenticator。so
authincludepasswordauth
accountrequiredpamnologin。so
accountincludepasswordauth
passwordincludepasswordauth
pamselinux。socloseshouldbethefirstsessionrule
sessionrequiredpamselinux。soclose
sessionrequiredpamloginuid。so
pamselinux。soopenshouldonlybefollowedbysessionstobeexecutedintheusercontext
sessionrequiredpamselinux。soopenenvparams
sessionrequiredpamnamespace。so
sessionoptionalpamkeyinit。soforcerevoke
sessionincludepasswordauth
修改sshd配置
vimetcsshsshdconfig
ChallengeResponseAuthenticationno
把上面配置改成
重启sshd服务
servicesshdrestart
5。客户端使用
5。1Android客户端
(版本5。00,更新日期2017年9月27日)
下载地址:https:play。google。comstoreappsdetails?idcom。google。android。apps。authenticator2hlzh
CLSN镜像地址https:clsn。iofilesgooglecom。google。android。apps。authenticator。apk
6。2浏览器客户端
获取30秒一次的动态码的客户端是浏览器(仅支持chrome、firefox)、Android设备、苹果IOS设备、Blackberry、WP手持设备。各自程序的下载地址为:
chromegoogleauthenticator插件
firefoxgoogleauthenticator插件
6。3Python客户端
importhmac,base64,struct,hashlib,time
defcalGoogleCode(secretKey):
inputint(time。time())30
keybase64。b32decode(secretKey)
msgstruct。pack(Q,input)
googleCodehmac。new(key,msg,hashlib。sha1)。digest()
oord(googleCode〔19〕)15
googleCodestr((struct。unpack(I,googleCode〔o:o4〕)〔0〕0x7fffffff)1000000)
iflen(googleCode)5:
googleCode0googleCode
returngoogleCode
secretKey这里填秘钥
printcalGoogleCode(secretKey)