巩固SSH安全访问的构想及操作方法

由 xchen 在 12-19-2003 14:42 发表:

巩固SSH安全访问的构想及操作方法

吃饱了饭没事干,随便灌下^_^!!!

之所以要折腾这个东东,是因为很快要过年了,现在server上ssh是指定固定ip访问,回家去可能是动态ip,又不想取消限制,所以有了此文。

一、构想

想用的时候才开启,不用的时候关闭服务/禁用远程访问/禁止所有用户的su权限。

二、实现方法

利用crond/atd/ftp服务来实现。crond开一后台进程,随时监测配置文件,根据其变量值来配置“远程访问ip/su使用权限/开启关闭服务”等,将配置文件放在ftp目录,使用时远程登录ftp修改配置文件即可。

三、具体步骤

其实很多朋友看到这里就已经够了,因为操作起是很简单的,反正现在也没事,继续灌吧。

1、需要的程序

sed程序,用以修改系统文件,可用rpm -q sed来查询有无安装。

crond服务,必须,若无请安装。

atd服务,必须,若无请安装。

2、所须文件列表

除cxdr.txt和log.txt外,所有文件属性均为700

/etc/rc.d/cxdr.txt //开启即加载监测进程

/etc/rc.d/shell/kxgpass.txt //访问密码设置脚本

/etc/rc.d/shell/adminshell.txt //后台监控脚本

/etc/rc.d/shell/sshdstop.txt //定时停止服务脚本

/etc/rc.d/shell/log.txt //日志文件

/home/abc/kxgvar.txt //远程控制变量脚本

3、go

chkconfig --level crond on

chkconfig --level atd on

vi /etc/pam.d/su //设置只允许wheel组的用户有SU使用权限

找到auth required /lib/security/pam_wheel.so group=,改成

auth required /lib/security/pam_wheel.so group=wheel

:wq //存盘退出

echo crontab -u root /etc/rc.d/cxdr>>/etc/rc.d/rc.local

cp所有文件到位,设一ftp用户能完全访问kxgvar.txt,OK后reboot。

4、使用

登录ftp,修改kxgvar.txt中的访问密码及变量值,若服务密码正确,系统执行定义的操作。

★可将/etc/rc.d/shell目录改为ftp管理,这样可以查看日志或修改adminshell文件。

例1、使用ssh,并在10分钟后关闭ssh服务

password="abc" //密码不正确不执行,在/etc/rc.d/shell/kxgpass.txt中定义密码

sshd="start" //启动sshd服务

sshdip="1.2.3.4" //允许ip为1.2.3.4的主机访问ssh

suser="username" //只允许username有SU使用权限

sshdclose="10" //10分钟后关闭ssh服务

例2、更改访问密码

password="abc" //访问密码

newpass="cba" //定义新密码即可

日志内容如下

Fri Dec 19 13:12:00 EST 2003

/etc/rc.d/init.d/sshd start

Fri Dec 19 13:12:00 EST 2003

设置ssh客户端IP为:1.2.3.4!!

Fri Dec 19 13:12:00 EST 2003

SU使用权限赋予cx

Fri Dec 19 13:12:00 EST 2003

SSH服务将在1分钟后关闭!

四、分析(单从密码破解来看)

来看看通过密码破解如何才行进入这个系统

1、入侵者得找到kxgvar.txt的ftp用户及密码。

2、要知道正确的文件名。原文件名为kxgvar.txt,若执行成功会将自已的文件名改得乱七八糟。

3、还要知道服务密码是什么。密码不正确,不会执行任何东东的。

4、还要知道系统中某个有shell权限的用户,然后将su权限给该用户。

5、现在可以用该用户的id登录了,su -,倒,你还得有root的密码才行。。。。

要是前4步有一步没法通过,就算给root密码也没用的。。

当然,这里所说的只是让sshd安全一些,防一些笨贼而已,要安全最大化,还得从防火墙/内核更新/系统漏洞补丁等多方面入手。。


附主要文件内容:

哎,第一次写shell,乱七八糟的,将就罗。。。

cat /etc/rc.d/cxdr

*/1 * * * * /etc/rc.d/shell/adminshell.txt //每分钟检查一下kxgvar.txt文件

cat /etc/rc.d/shell/kxgpass.txt

cmdpath=/etc/rc.d/init.d/

kxgpass=abc

export cmdpath kxgpass

cat /etc/rc.d/shell/adminshell.txt

#!/bin/bash

if [ -e /home/abc/kxgvar.txt ];then . /etc/rc.d/kxgpass;. /home/abc/kxgvar.txt;

if [ $password == $kxgpass ];then

if [ -n "$proftpd" ];then $cmdpath"proftpd" $proftpd;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo $cmdpath"proftpd" $proftpd>>/home/cx/log.txt;fi;

if [ -n "$httpd" ];then $cmdpath"httpd" $httpd;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo $cmdpath"httpd" $httpd>>/home/cx/log.txt;fi;

if [ -n "$sshd" ];then $cmdpath"sshd" $sshd;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo $cmdpath"sshd" $sshd>>/home/cx/log.txt;fi;

if [ $sshd == "start" ];then

if [ -n "$sshdip" ];then sed -e 's/^sshd.*/sshd:'${sshdip}'/g' /etc/hosts.allow>/etc/hosts.allowa;mv -f /etc/hosts.allowa /etc/hosts.allow;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo "设置SSH客户端IP为:"$sshdip"!!">>/home/cx/log.txt;fi;

if [ -n "$suser" ];then sed -e 's/^wheel.*/wheel❌10:'${suser}'/g' /etc/group>/etc/groupa;mv -f /etc/groupa /etc/group;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo "SU使用权限赋予"$suser>>/home/cx/log.txt;fi;

if [ -n "$sshdclose" ];then at now+${sshdclose}minutes -f /etc/rc.d/sshdstop.txt;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo "SSH服务将在"$sshdclose"分钟后关闭!">>/home/cx/log.txt;else at now+2minutes -f /etc/rc.d/sshdstop.txt;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo "SSH服务将在2分钟后关闭!">>/home/cx/log.txt;fi;fi;

if [ -n "$newpass" ];then sed -e 's/^kxgpass.*/kxgpass='${newpass}'/g' /etc/rc.d/kxgpass>/etc/rc.d/kxgpassa;mv -f /etc/rc.d/kxgpassa /etc/rc.d/kxgpass;echo >>/home/cx/log.txt;date>>/home/cx/log.txt;echo "更改服务密码为:"$newpass>>/home/cx/log.txt;fi;

sed -e 's/^password./#password=""/g' /home/abc/kxgvar.txt | sed -e 's/^newpass./#newpass=""/g' | sed -e 's/^sshd=./#sshd=""/g' | sed -e 's/^sshdip./#sshdip=""/g' | sed -e 's/^sshdclose./#sshdclose=""/g' | sed -e 's/^suser./#suser=""/g' | sed -e 's/^httpd./#httpd=""/g' | sed -e 's/^proftpd./#proftpd=""/g'>/home/abc/kxgvara.txt

mv -f /home/abc/kxgvara.txt /home/abc/kxgvar.txt

chmod 700 /home/abc/kxgvar.txt

chown -R shell /home/abc/kxgvar.txt

chgrp -R shell /home/abc/kxgvar.txt

chown -R cx /etc/rc.d/shell/log.txt

chgrp -R cx /etc/rc.d/shell/log.txt

mv /home/abc/kxgvar.txt /home/abc/d@s~ln!3a4e5z.txt

unset httpd proftpd sshd sshdip sshdclose password newpass suser

fi

fi

cat /etc/rc.d/shell/sshdstop.txt

#!/bin/bash

/etc/rc.d/init.d/sshd stop

sed -e 's/^sshd.*/sshd:/g' /etc/hosts.allow >/etc/hosts.allowa

sed -e 's/^wheel.*/wheel❌10:/g' /etc/group >/etc/groupa

mv -f /etc/hosts.allowa /etc/hosts.allow

mv -f /etc/groupa /etc/group

echo >>/etc/rc.d/shel

Published At
Categories with 服务器类
Tagged with
comments powered by Disqus