centos制作openssh8.6的RPM包,避免掉坑2021-06-24

怎么简单怎么来
坑的区域我会做特别说明

制作前给大家另一种避开公共漏洞的方法,一般扫描工具会放弃检测你
#查询当前sshd版本,记录版本号

[root@localhost ~]# sshd -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]
            [-E log_file] [-f config_file] [-g login_grace_time]
            [-h host_key_file] [-o option] [-p port] [-u len]

#搜索对应的版本,看看能不能找到文件

[root@localhost ~]# strings /usr/sbin/sshd |grep OpenSSH_7.4
OpenSSH_7.4p1-RHEL7-7.4p1-21
OpenSSH_7.4
OpenSSH_7.4p1

#出现以上内容就执行

sed -i 's/OpenSSH_7.4/OpenSSH_D.0/g' /usr/sbin/sshd && systemctl restart sshd
[root@localhost ~]# sshd -V
OpenSSH_D.0p1, OpenSSL 1.0.2k-fips  26 Jan 2017
usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]
            [-E log_file] [-f config_file] [-g login_grace_time]
            [-h host_key_file] [-o option] [-p port] [-u len]

这样可以在不升级ssh的情况下避开漏洞扫描

当然以上的方法是在无网的环境下,又没有安装包的情况下临时的解决方案
接下来我们进入主题,开始制作RPM包
首先纯净的centos7/8下安装好必要的编译插件

yum install wget rpm-build zlib-devel openssl-devel gcc perl-devel pam-devel unzip epel-release make -y   
#这里需要注意,很多人都没有安装make,包括一些大神的帖子都没有,当时我表示很震惊,居然安装了GCC都不安装make。

下载openssh跟X11

wget https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-8.6p1.tar.gz
wget https://src.fedoraproject.org/repo/pkgs/openssh/x11-ssh-askpass-1.2.4.1.tar.gz/8f2e41f3f7eaa8543a2440454637f3c3/x11-ssh-askpass-1.2.4.1.tar.gz
[root@localhost ~]# ls 
anaconda-ks.cfg    openssh-8.6p1.tar.gz    x11-ssh-askpass-1.2.4.1.tar.gz

解压openssh-8.6p1.tar.gz 创建所需要的目录,并将openssh.spec复制到SPECS目录下

tar zxvf openssh-8.6p1.tar.gz 
mkdir -p /root/rpmbuild/{SOURCES,SPECS}
cp -a openssh-8.1p1/contrib/redhat/openssh.spec  /root/rpmbuild/SPECS
[root@localhost ~]# vi /root/rpmbuild/SPECS/openssh.spec
#将原先openssh.spec中的
%global no_x11_askpass 0
%global no_gnome_askpass 0 
#修改为
%global no_x11_askpass 1 
%global no_gnome_askpass 1
#注释掉或删除
BuildRequires: openssl-devel < 1.1  让openssh不在依赖openssl1.1.1

如果需要提前修改好sshd的配置,如:端口、启用root登录等等

[root@localhost ~]# vi /root/openssh8.6p1/sshd_config
#增加如下
PermitRootLogin yes
PubkeyAuthentication yes
PasswordAuthentication yes
[root@localhost ~]# vi openssh8.6p1/contrib/redhat/sshd.pam
#替换内容如下(虚线内的内容就是你的机器原来的内容,部分有做PAM验证的记得先备份好自己的原文件,更新好了以后直接回复即可)
-----------------------------------------------
#%PAM-1.0
auth       required     pam_sepermit.so
auth       substack     password-auth
auth       include      postlogin
# Used with polkit to reauthorize users in remote sessions
-auth      optional     pam_reauthorize.so prepare
account    required     pam_nologin.so
account    include      password-auth
password   include      password-auth
# pam_selinux.so close should be the first session rule
session    required     pam_selinux.so close
session    required     pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session    required     pam_selinux.so open env_params
session    required     pam_namespace.so
session    optional     pam_keyinit.so force revoke
session    include      password-auth
session    include      postlogin
# Used with polkit to reauthorize users in remote sessions
-session   optional     pam_reauthorize.so prepare
-----------------------------------------------

修改完成后重新打包

mv openssh8.6p1.tar.gz  openssh8.6p1.tar.gz.old
tar zcvf openssh8.6p1.tar.gz openssh8.6p1/
mv openssh-8.6p1.tar.gz x11-ssh-askpass-1.2.4.1.tar.gz /root/rpmbuild/SOURCES/

进入编译目录并执行编译打包操作

cd /root/rpmbuild/SPECS/
rpmbuild -ba openssh.spec     #这里需要注意下,如果是centos8的话需要在尾部加上 --nodeps 不然会寻找依赖包失败而打包失败。
#等待打包完成后rpm包在   /root/rpmbuild/RPMS/x86_64/ 目录下

至此RPM制作就完成了

安装的话因为环境不一样,就不叙述了,建议先卸载再安装,注意可以避免各种各样的问题
yum remove openssh && rm -rf /etc/ssh/ && rpm -ivh openssh-*

附一个我个人使用的完整安装脚本,基本无死角(保留原端口,并备份原配置)

#!/bin/bash -
#update sshd
work_path=$(dirname $(readlink -f $0))
cd $work_path
config=`ls /etc/ssh |grep sshd_config |awk NR==1`
pam=`ls /etc/pam.d/ |grep sshd |awk NR==1`
if [ "$config" = "sshd_config" ]; then
cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
echo "文件备份完成"
else
echo "没有找到文件,直接安装..."
fi
if [ "$pam" = "sshd" ]; then
cp -a /etc/pam.d/sshd /etc/pam.d/sshd.bak
echo "文件备份完成"
else
echo "没有找到文件,直接安装..."
fi
number=`ss -ntlp |grep sshd | awk 'NR==1{print$4}'`
port=`echo $number |awk -F ":" '{print$2}'`
if [ "$port" = "22" ];then
yum remove openssh -y  && rm -rf /etc/ssh/ && rpm -ivh openssh-* && systemctl restart sshd  && chmod 600  /etc/ssh/ssh_host_*_key && systemctl restart sshd
ss -ntlp |grep sshd
exit
else
yum remove openssh -y  && rm -rf /etc/ssh/ && rpm -ivh openssh-* && sed -i -e "s/#Port 22/Port $port/g" /etc/ssh/sshd_config && systemctl start sshd  && chmod -R 600  /etc/ssh/ssh_host_*_key && systemctl restart sshd
ss -ntlp |grep sshd
fi

安装完成后如果无法登陆可以将之前的备份的sshd_config跟sshd还原即可
分别在/etc/ssh/sshd_config.bak 与 /etc/pam.d/sshd.bak

[root@localhost ~]# sshd -V
unknown option -- V
OpenSSH_8.6p1, OpenSSL 1.0.2k-fips  26 Jan 2017
usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]
            [-E log_file] [-f config_file] [-g login_grace_time]
            [-h host_key_file] [-o option] [-p port] [-u len]
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值