Linux 之不同运维人员共用root 账户权限审计

发布时间:2024-01-21 点击:94
建站服务器
一、为什么?
在中小型企业,公司不同运维人员基本都是以root 账户进行服务器的登陆管理,缺少了账户权限审计制度。不出问题还好,出了问题,就很难找出源头。
这里介绍下,如何利用编译bash 使不同的客户端在使用root 登陆服务器使,记录各自的操作,并且可以在结合elk 日志分析系统,来收集登陆操作日志
二、环境
服务器:centos 6.5、development tools、使用密钥认证,selinux 关闭。
客户端:生成密钥对,用于登录服务器 (2台)
三、搭建部署(服务器操作 192.168.30.72)
3.1 下载编译bash
[root@open1~]#wgethttp://ftp.gnu.org/gnu/bash/bash-4.1.tar.gz[root@open1~]#tarxvfbash-4.1.tar.gz[root@open1~]#cdbash-4.13.2先修改下 config-top.c文件,大概94行、104行,由于c 语言中 注释是// ,所以不要删除错了。修改如下:
[root@open1bash-4.1]#vimconfig-top.c#definessh_source_bashrc#definesyslog_history3.3 修改下bashhist.c 文件,让终端上的命令记录到系统messages 中,并且以指定的格式。并传入获得的变量。修改后的内容如下:
[root@open1bash-4.1]#vimbashhist.c#...省略部分段落voidbash_syslog_history(line)constchar*line;{chartrunc[syslog_maxlen];constchar*p;p=getenv(name_of_key);if(strlen(line)<syslog_maxlen)syslog(syslog_facility|syslog_level,history:pid=%dppid=%dsid=%duser=%suser=%scmd=%s,getpid(),getppid(),getsid(getpid()),current_user.user_name,p,line);else{strncpy(trunc,line,syslog_maxlen);trunc[syslog_maxlen-1]=\\\'\\\';syslog(syslog_facility|syslog_level,history(truncated):pid=%dppid=%dsid=%duser=%suser=%scmd=%s,getpid(),getppid(),getsid(getpid()),current_user.user_name,p,trunc);}}
3.4 配置安装路径,编译安装,编译到/usr/local/目录下。
[root@open1bash-4.1]#./configure--prefix=/usr/local/bash_new[root@open1bash-4.1]#make&&makeinstall...iftestbash=gettext-tools;then\\\\/bin/sh/root/bash-4.1/./support/mkinstalldirs/usr/local/bash_new/share/gettext/po;\\\\forfileinmakefile.in.inremove-potcdate.sinquot.sedboldquot.seden@quot.headeren@boldquot.headerinsert-header.sinrules-quotmakevars.template;do\\\\/usr/bin/install-c-m644./$file\\\\/usr/local/bash_new/share/gettext/po/$file;\\\\done;\\\\forfileinmakevars;do\\\\rm-f/usr/local/bash_new/share/gettext/po/$file;\\\\done;\\\\else\\\\:;\\\\fimake[1]:leavingdirectory`/root/bash-4.1/po\\\'
编译完成后,将新的bash 追加到 /etc/shells 中,并修改root用户的登陆shell 环境为新编译的shell。如下
[root@open1bash-4.1]#echo/usr/local/bash_new/bin/bash>>/etc/shells[root@open1bash-4.1]#cat/etc/shells/bin/sh/bin/bash/sbin/nologin/bin/dash/usr/local/bash_new/bin/bash
[root@open1bash-4.1]#vim/etc/passwdroot:x:0:0:root:/root:/usr/local/bash_new/bin/bash注销当前root用户,重新登陆后,查看/var/log/messages,如下就可以看到记录了操作命令
四、ssh客户端生成密钥部分
4.1 在client1上(192.168.30.99)操作,用户zhangsan
view code
-t 加密算法
-c 注释 (加上这个也是为了最后进行对服务器访问人员进行辨别的一个关键点)
将公钥上传到服务器上的.ssh/authorized_keys 文件中。ssh-copy-id 命令会自动在服务器上创建.ssh/authorized_keys文件,即使该目录不存在,并自动赋予600权限。
[root@rsyslog~]#ssh-copy-id-i/root/.ssh/id_rsa.pubroot@192.168.30.72root@192.168.30.72\\\'stryloggingintothemachine,withssh\\\'root@192.168.30.72\\\',andcheckin:.ssh/authorized_keystomakesurewehaven\\\'taddedextrakeysthatyouweren\\\'texpecting.
4.3 client 2(192.168.30.71) 上同样的操作,用户lisi
view code
上传公钥到服务器上
[root@swift3~]#ssh-copy-id-i/root/.ssh/id_rsa.pubroot@192.168.30.72theauthenticityofhost\\\'192.168.30.72(192.168.30.72)\\\'can\\\'tbeestablished.rsakeyfingerprintis8f:a7:1b:8d:e4:92:ad:ae:ea:1b:fb:67:0b:0b:7c:ac.areyousureyouwanttocontinueconnecting(yes/no)?yeswarning:permanentlyadded\\\'192.168.30.72\\\'(rsa)tothelistofknownhosts.root@192.168.30.72\\\'stryloggingintothemachine,withssh\\\'root@192.168.30.72\\\',andcheckin:.ssh/authorized_keystomakesurewehaven\\\'taddedextrakeysthatyouweren\\\'texpecting.
4.4 现在去服务器上验证下该文件。
view code
现在上面两个客户端已经可以免密钥登陆了,现在去服务器上配置,并创建脚本。
五、配置服务器
5.1 在log目录下创建一个 keys 文件,用于登陆时存进去公钥,之后对其进行取出判断的
[root@open1~]#touch/var/log/keys创建检测脚本,内容如下:
view code
5.2 配置 profile,在文件末尾添加一行内容,如下:
[root@open1~]#echotest-f/etc/checkuser.sh&&./etc/checkuser.sh>>/etc/profile在/etc/bashrc 末尾添加下面内容:
[root@open1~]#tail-1f/etc/bashrctest-z$bash_execution_string||{test-f/etc/checkuser.sh&&./etc/checkuser.sh;logger-t-bash-shistory$ssh_clientuser=$name_of_keycmd=$bash_execution_string>/dev/null2>&1;}5.3 修改sshd 配置文件,开启debug 模式,并重启sshd 服务
[root@open1~]#sed-i\\\'s/#loglevelinfo/logleveldebug/g\\\'/etc/ssh/s

百度云服务器不续费多久清理数据
php怎么去掉bom头
香港阿里云服务器优惠购买
网站域名会过期吗? 域名过期网站还有用吗?
谷歌CEO皮查伊:支持经合组织达成全球性数字税方案
个人建站适合租用云服务器吗
腾讯云服务器怎么修改系统吗
服务器被攻击问题-云服务器问题