LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2434|回复: 0

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

[复制链接]
发表于 2003-12-19 14:42:48 | 显示全部楼层 |阅读模式
  吃饱了饭没事干,随便灌下^_^!!!
  之所以要折腾这个东东,是因为很快要过年了,现在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:x: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:x:10:/g' /etc/group >/etc/groupa
mv -f /etc/hosts.allowa /etc/hosts.allow
mv -f /etc/groupa /etc/group
echo >>/etc/rc.d/shell/log.txt
date>>/etc/rc.d/shell/log.txt
echo "关闭SSHD服务/取消所有用户SU使用权限/清除SSHD访问IP!">>/etc/rc.d/shell/log.txt
chown -R cx /etc/rc.d/shell/log.txt
chgrp -R cx /etc/rc.d/shell/log.txt

cat /home/abc/kxgvar.txt
########################################
##                                    ##
##    60服务器远程控制脚本变量集      ##
##                                    ##
########################################
#!/bin/bash
#-----script BEGIN-----

# 使用方法:
# 1、去掉password前#号,填入正确密码
# 2、选择要控制的服务,去掉#号,再填入控制值(stop/start/restart/.....)

#********************************************************************************
#变量说明
# password="abc",设置运行密码,若密码不对,脚本便无法运行
# newpass="bcd",修改密码,一般情况下宜频繁修改,容易搞忘的^_^!!!!
# sshd="stop",sshd服务停止
# sshd="start",sshd服务启动
# sshd="restart",sshd服务重启
# sshdip="1.1.1.1",设置putty.exe客户端ip,未设置将无法访问使用ssh
# suser="cx",给cx使用su的权限,未设置将无法使用su命令
# sshdclose="10",10分钟后关闭ssh服务,若sshd="start"且未设置sshdclose变量,则系统默认“2分钟关闭sshd服务”
# proftpd="stop",ftp服务停止
# proftpd="start",ftp服务启动
# proftpd="restart",ftp服务重启
# httpd="stop",http服务停止
# httpd="start",http服务启动
# httpd="restart",http服务重启

#********************************************************************************
#设置要使用的变量,去掉#号即可,事关系统服务,请慎之!
#password=""
#newpass=""
#sshd=""
#sshdip=""
#suser=""
#sshdclose=""
#proftpd=""
#httpd=""

#********************************************************************************
#以下语句不要修改!
if [ -n "$password" ];then export password;fi;
if [ -n "$sshd" ];then export sshd;fi;
if [ -n "$sshdip" ];then export sshdip;fi;
if [ -n "$sshdclose" ];then export sshdclose;fi;
if [ -n "$suser" ];then export suser;fi;
if [ -n "$proftpd" ];then export proftpd;fi;
if [ -n "$httpd" ];then export httpd;fi;
if [ -n "$newpass" ];then export newpass;fi;

#-----script END-----
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表