CTF-PWN-沙箱逃脱-【seccomp和prtcl-1】

文章目录

  • 啥是seccomp
  • #ifndef #define #endif使用
    • 使用格式
  • seccomp
    • 无参数条件禁用系统调用
    • 有参数条件禁用系统调用
  • prctl
    • 实例
  • seccomp_export_bpf

啥是seccomp

就是可以禁用掉某些系统调用,然后只能允许某些系统调用
在这里插入图片描述

#ifndef #define #endif使用

#ifndef #define #endif作用就是防止头文件被重复引用,

其实“被重复引用”是指一个头文件在同一个cpp文件中被include了多次,这种错误常常是由于include嵌套造成的或者是简单地include多次。比如:存在a.h文件 #include "c.h"而此时b.cpp文件还有了=#include “a.h” 和#include "c.h"此时就会造成c.h重复引用。

头文件被重复引用引起的后果:
有些头文件重复引用只是增加了编译工作的工作量,不会引起太大的问题,仅仅是编译效率低一些,但是对于大工程而言编译效率低下那就比较严重了。
有些头文件重复包含,会引起错误,比如在头文件中定义了全局变量(虽然这种方式不被推荐,但确实是C规范允许的),在重复引用后会引起重复定义。这样就可能会报错、

    下面给一个#ifndef/#define/#endif的格式:#ifndef A_H意思是"if not define a.h"  如果不存在a.h接着的语句应该#define A_H 然后相关头文件的内容最后一句应该写#endif  A_H 结束过程

使用格式

#ifndef <标识> 
#define <标识> 
....
#endif <标识> 

<标识>在理论上来说可以 是自由命名的,但每个头文件的这个“标识”都应该是唯一的。标识的命名规则一般是头文件名全大写,前后加下划线,并把文件名中的“.”也变成下划线,如:stdio.h 对应得标识为 STDIO_H

例如要编写头文件sys.h,需要在sys.h文件开头写上:
#ifndef SYS_H
#define SYS_H
....
#endif SYS_H
那么当#include"sys.h"时会判断是否sys.h已经被导入了
如果此时是第一次#include"sys.h",那么会执行#define SYS_H,此时_SYS_H被定义了
当第二次#include"sys.h",由于此时已经定义了SYS_H,所以不会执行#define SYS_H以及之后的内容

seccomp

seccomp学习

一般seccomp得使用流程

ctx = seccomp_init(SCMP_ACT_ALLOW);

seccomp_init(uint32_t def_action)

def_action 是过滤器工作模式
seccomp_init是初始化的过滤状态,这里用的是SCMP_ACT_ALLOW,表示默认允许所有的syscacll.如果初始化状态为SCMP_ACT_KILL,则表示默认不允许所有的syscall

seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 0);

int seccomp_rule_add(scmp_filter_ctx " ctx ", uint32_t " action ",int " syscall ",unsigned int " arg_cnt ", " ... ");

ctx :初始化定义的过滤器
action :规则模式,匹配到系统调用后执行的操作,和初始化过滤器的 def_action类型一样
syscall :系统调用号,可以使用 SCMP_SYS(函数名) 代替具体调用号
arg_cnt :需要参数限制规则个数 seccomp_rule_add 限制参数接口定义在 seccomp.h.in 。主要是取参数操作 SCMP_AX ,运算比较 scmp_compare 。当这些规则都满足后才能此seccomp_rule_add函数成功执行

只要匹配系统调用,不管参数内容:那么arg_cnt=0。匹配系统调用后,根据参数内容决定是否执行 action :arg_cnt=N(规则条数),后面再跟N个参数

seccomp_load(ctx);

seccomp_load是应用过滤,加载沙箱规则,如果不调用seccomp_load则上面所有的过滤都不会生效

无参数条件禁用系统调用

  seccomp_rule_add(ctx,SCMP_ACT_KILL,SCMP_SYS(execve),0); //add roles into filterseccomp_rule_add(ctx,SCMP_ACT_KILL,SCMP_SYS(write),0); //add roles into filter

此时禁止write和execve的系统调用,不管其系统调用对应函数的参数咋样

有参数条件禁用系统调用

当有限制规则时,对应规则通过以下宏编写

/*** Specify an argument comparison struct for use in declaring rules* @param arg the argument number, starting at 0* @param op the comparison operator, e.g. SCMP_CMP_** @param datum_a dependent on comparison* @param datum_b dependent on comparison, optional*/
#define SCMP_CMP(...)		((struct scmp_arg_cmp){__VA_ARGS__})/*** Specify an argument comparison struct for argument 0*/
#define SCMP_A0(...)		SCMP_CMP(0, __VA_ARGS__)/*** Specify an argument comparison struct for argument 1*/
#define SCMP_A1(...)		SCMP_CMP(1, __VA_ARGS__)/*** Specify an argument comparison struct for argument 2*/
#define SCMP_A2(...)		SCMP_CMP(2, __VA_ARGS__)/*** Specify an argument comparison struct for argument 3*/
#define SCMP_A3(...)		SCMP_CMP(3, __VA_ARGS__)/*** Specify an argument comparison struct for argument 4*/
#define SCMP_A4(...)		SCMP_CMP(4, __VA_ARGS__)/*** Specify an argument comparison struct for argument 5*/
#define SCMP_A5(...)		SCMP_CMP(5, __VA_ARGS__)/*** Comparison operators*/
enum scmp_compare {_SCMP_CMP_MIN = 0,SCMP_CMP_NE = 1,		/**< not equal */SCMP_CMP_LT = 2,		/**< less than */SCMP_CMP_LE = 3,		/**< less than or equal */SCMP_CMP_EQ = 4,		/**< equal */SCMP_CMP_GE = 5,		/**< greater than or equal */SCMP_CMP_GT = 6,		/**< greater than */SCMP_CMP_MASKED_EQ = 7,		/**< masked equality */_SCMP_CMP_MAX,
};/*** Argument datum*/
typedef uint64_t scmp_datum_t;/*** Argument / Value comparison definition*/
struct scmp_arg_cmp {unsigned int arg;	/**< argument number, starting at 0 */enum scmp_compare op;	/**< the comparison op, e.g. SCMP_CMP_* */scmp_datum_t datum_a;scmp_datum_t datum_b;
};
 seccomp_rule_add(ctx,SCMP_ACT_KILL,SCMP_SYS(write),1,SCMP_A0(SCMP_CMP_LE,0x10)); //add roles into filter/*read的第0个参数为文件描述符,其小于等于0x10时不能使用*/seccomp_rule_add(ctx,SCMP_ACT_KILL,SCMP_SYS(read),2,SCMP_A0(SCMP_CMP_LE,0x10),SCMP_A2(SCMP_CMP_GT,0x8)); //add roles into filter此时read有两天规则,第0个参数需要小于等于0x10,第2个参数需要大于0x8,那么将禁止其使用
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(write),1,SCMP_A2(SCMP_CMP_EQ,0x10));//第2(从0开始数起)个参数等于0x10 此时如果write函数的第2个参数即写入长度等于0x10将使用失败

prctl

int prctl(int option, unsigned long arg2, unsigned long arg3,unsigned long arg4, unsigned long arg5);
prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0);

当option为PR_SET_NO_NEW_PRIVS(38),且arg2为1时,将无法获得特权,即能getshell,但无法获得root权限
在这里插入图片描述
当option为PR_SET_SECCOMP(22)时,效果就是我们上面的seccomp了,只不过这里的格式略有不同。此时还与参数2有关
如果arg2为SECCOMP_MODE_STRICT(1),则只允许调用read,write,_exit(not exit_group),sigreturn这几个系统调用
如果arg2为SECCOMP_MODE_FILTER(2),则为过滤模式,其中对syscall的限制通过arg3用BPF(Berkeley Packet Filter)的形式传进来,是指向struct sock_fprog数组的指针

/**	Try and keep these values and structures similar to BSD, especially*	the BPF code definitions which need to match so you can share filters*/struct sock_filter {	/* Filter block */__u16	code;   /* Actual filter code */__u8	jt;	/* Jump true 符合jmp条件时跳过的规则数*/__u8	jf;	/* Jump false不符合jmp条件时跳过的规则数 */__u32	k;      /* Generic multiuse field */
};
struct sock_fprog {	/* Required for SO_ATTACH_FILTER. */unsigned short		len;	/* Number of filter blocks */struct sock_filter *filter;
};

关于filter中的格式其实可以分为BPF_STMT和BPF_JUMP两种格式,注意k的位置在宏定义中的参数和对应的filter中是不同,k在filter中都是第四个,但BPF_JUMP(code, k, jt, jf)为第二个

#ifndef BPF_STMT
#define BPF_STMT(code, k) { (unsigned short)(code), 0, 0, k }
#endif
#ifndef BPF_JUMP
#define BPF_JUMP(code, k, jt, jf) { (unsigned short)(code), jt, jf, k }
#endif

此时两种格式中code的值是由各种宏或得到的

/* Instruction classes */                    
#define BPF_CLASS(code) ((code) & 0x07)    //指定操作的类别
#define        BPF_LD        0x00               //将值复制到累加器中
#define        BPF_LDX        0x01               //将值加载到索引寄存器中
#define        BPF_ST        0x02               //将累加器中的值存到暂存器
#define        BPF_STX        0x03               //将索引寄存器的值存储在暂存器中
#define        BPF_ALU        0x04               //用索引寄存器或常数作为操作数在累加器上执行算数或逻辑运算
#define        BPF_JMP        0x05               //跳转
#define        BPF_RET        0x06               //返回
#define        BPF_MISC        0x07           // 其他类别/* ld/ldx fields */
#define BPF_SIZE(code)  ((code) & 0x18)
#define        BPF_W        0x00 /* 32-bit */       //字
#define        BPF_H        0x08 /* 16-bit */       //半字
#define        BPF_B        0x10 /*  8-bit */       //字节
/* eBPF        BPF_DW        0x18    64-bit */       //双字
#define BPF_MODE(code)  ((code) & 0xe0)
#define        BPF_IMM        0x00                  //常数 
#define        BPF_ABS        0x20                  //固定偏移量的数据包数据(绝对偏移)
#define        BPF_IND        0x40                  //可变偏移量的数据包数据(相对偏移)
#define        BPF_MEM        0x60                  //暂存器中的一个字
#define        BPF_LEN        0x80                  //数据包长度
#define        BPF_MSH        0xa0/* alu/jmp fields */
#define BPF_OP(code)    ((code) & 0xf0)       //当操作码类型为ALU时,指定具体运算符   
#define        BPF_ADD        0x00        
#define        BPF_SUB        0x10
#define        BPF_MUL        0x20
#define        BPF_DIV        0x30
#define        BPF_OR        0x40
#define        BPF_AND        0x50
#define        BPF_LSH        0x60
#define        BPF_RSH        0x70
#define        BPF_NEG        0x80
#define        BPF_MOD        0x90
#define        BPF_XOR        0xa0//当操作码是jmp时指定跳转类型
#define        BPF_JA        0x00
#define        BPF_JEQ        0x10
#define        BPF_JGT        0x20
#define        BPF_JGE        0x30
#define        BPF_JSET        0x40
#define BPF_SRC(code)   ((code) & 0x08)
#define        BPF_K        0x00                    //常数
#define        BPF_X        0x08                    //索引寄存器

k也可以是以下宏定义

/** All BPF programs must return a 32-bit value.* The bottom 16-bits are for optional return data.* The upper 16-bits are ordered from least permissive values to most,* as a signed value (so 0x8000000 is negative).** The ordering ensures that a min_t() over composed return values always* selects the least permissive choice.*/
#define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process */
#define SECCOMP_RET_KILL_THREAD	 0x00000000U /* kill the thread */
#define SECCOMP_RET_KILL	 SECCOMP_RET_KILL_THREAD
#define SECCOMP_RET_TRAP	 0x00030000U /* disallow and force a SIGSYS */
#define SECCOMP_RET_ERRNO	 0x00050000U /* returns an errno */
#define SECCOMP_RET_USER_NOTIF	 0x7fc00000U /* notifies userspace */
#define SECCOMP_RET_TRACE	 0x7ff00000U /* pass to a tracer or disallow */
#define SECCOMP_RET_LOG		 0x7ffc0000U /* allow after logging */
#define SECCOMP_RET_ALLOW	 0x7fff0000U /* allow *//* Masks for the return value sections. */
#define SECCOMP_RET_ACTION_FULL	0xffff0000U
#define SECCOMP_RET_ACTION	0x7fff0000U
#define SECCOMP_RET_DATA	0x0000ffffU

下面这段代码定义了一些编写seccomp BPF code可能会用到的东西,根据注释可知,我们可以在BPF code中获取该系统调用的:系统调用号、处理器架构、指令地址、6个参数的值。具体选择获取什么通过字段k来决定,k相当于seccomp_data结构体的偏移量,若指定k=0,则为获取nr,即系统调用号,若k=4,则为获取处理器架构等。

/*** struct seccomp_data - the format the BPF program executes over.* @nr: the system call number* @arch: indicates system call convention as an AUDIT_ARCH_* value*        as defined in <linux/audit.h>.* @instruction_pointer: at the time of the system call.* @args: up to 6 system call arguments always stored as 64-bit values*        regardless of the architecture.*/
struct seccomp_data {int nr;__u32 arch;__u64 instruction_pointer;__u64 args[6];
};

实例

K指的是AUDIT_ARCH_X86_64,定义于/include/uapi/linux/audit.h,其中为所有架构都定义了独特的标识符,而0xc000003e则是AUDIT_ARCH_X86_64的值。对于整个seccomp code而言,可能需要的外部数据也就只有seccomp_data了。

 line  CODE  JT   JF      K
=================================0000: 0x20 0x00 0x00 0x00000004  LD | ABS | Word, R0 = arch0001: 0x15 0x00 0x19 0xc000003e  JMP | JEQ after 0x19, R0 == AUDIT_ARCH_X86_64 0002: 0x20 0x00 0x00 0x00000000  LD | ABS | Word, R0 = nr0003: 0x35 0x00 0x01 0x40000000  JMP | JGE after 0x01, R0 >= 0x40000000 ?0004: 0x15 0x00 0x16 0xffffffff  JMP | JEQ after 0x16, R0 == 0xFFFFFFFF ?0005: 0x15 0x15 0x00 0x00000000  JMP | JEQ after 0x15, R0 == 0 ?0006: 0x15 0x14 0x00 0x00000001  JMP | JEQ after 0x14, R0 == 1 ?0007: 0x15 0x13 0x00 0x00000002  JMP | JEQ after 0x13, R0 == 2 ?...0026: 0x06 0x00 0x00 0x7fff0000  return SECCOMP_RET_ALLOW0027: 0x06 0x00 0x00 0x00000000  return SECCOMP_RET_KILL
 line  CODE  JT   JF      K
=================================0000: 0x20 0x00 0x00 0x00000004  LD | ABS | Word, R0 = arch

code为0x20是BDF_LD(0x00)|BDF_ABS(0X20)|BOF_W的结果,就是将一个字的值通过固定偏移量复制到累加器中,此时k对应为偏移量4,即seccomp_data偏移量为4的位置正好是arch的结构

 line  CODE  JT   JF      K
=================================0001: 0x15 0x00 0x19 0xc000003e  JMP | JEQ after 0x19, R0 == AUDIT_ARCH_X86_64 

此时code 15是BPF_JMP(0x05)|BPF_JEQ(0x10)的结果,此时是相等跳转指令,相等则跳转到下一条指令,不相等则跳转到下条指令+0x19的位置,将累加器与k的值做对比,此时累加器的值是之前取的arch

seccomp_export_bpf

参考
seccomp_export_bpf的函数能够将设置的seccomp以bpf的形式导出

//gcc -g simple_syscall_seccomp.c -o simple_syscall_seccomp -lseccomp
#include <unistd.h>
#include <seccomp.h>
#include <linux/seccomp.h>
#include <fcntl.h>
int main(void){scmp_filter_ctx ctx;ctx = seccomp_init(SCMP_ACT_ALLOW);seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(write),1,SCMP_A2(SCMP_CMP_EQ,0x10));seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve),0);seccomp_load(ctx);int fd = open("bpf.out",O_WRONLY);seccomp_export_bpf(ctx,fd);close(fd);char * filename = "/bin/sh";char * argv[] = {"/bin/sh",NULL};char * envp[] = {NULL};write(1,"i will give you a shell\n",24);write(1,"1234567812345678",0x10);syscall(0x4000003b,filename,argv,envp);//execvereturn 0;
}

得到对应的bpf的二进制文件

$ hexdump bpf.out
0000000 0020 0000 0004 0000 0015 0900 003e c000
0000010 0020 0000 0000 0000 0035 0007 0000 4000
0000020 0015 0006 003b 0000 0015 0400 0001 0000
0000030 0020 0000 0024 0000 0015 0200 0000 0000
0000040 0020 0000 0020 0000 0015 0001 0010 0000
0000050 0006 0000 0000 7fff 0006 0000 0000 0000
0000060 0000 0000 0000 0000 0000 0000 0000 0000

改用prctl

#include <unistd.h>
#include <sys/prctl.h>
#include <linux/filter.h>
#include <linux/seccomp.h>int main(void){prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0);struct sock_filter sfi[] = {{0x20,0x00,0x00,0x00000004},{0x15,0x00,0x09,0xc000003e},{0x20,0x00,0x00,0x00000000},{0x35,0x07,0x00,0x40000000},{0x15,0x06,0x00,0x0000003b},{0x15,0x00,0x04,0x00000001},{0x20,0x00,0x00,0x00000024},{0x15,0x00,0x02,0x00000000},{0x20,0x00,0x00,0x00000020},{0x15,0x01,0x00,0x00000010},{0x06,0x00,0x00,0x7fff0000},{0x06,0x00,0x00,0x00000000}};struct sock_fprog sfp = {12,sfi};prctl(PR_SET_SECCOMP,SECCOMP_MODE_FILTER,&sfp);char * filename = "/bin/sh";char * argv[] = {"/bin/sh",NULL};char * envp[] = {NULL};write(1,"i will give you a shell\n",24);write(1,"1234567812345678",0x10);syscall(0x4000003b,filename,argv,envp);//execvereturn 0;
}

成功拦截函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/333163.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

http介绍

http http工作原理 工作于客户端—服务端架&#xff08;client/server&#xff09;构上。浏览器作为HTTP客户端通过URL向HTTP服务端即WEB服务器发送所有请求。 Web服务器有&#xff1a;Nginx&#xff0c;Apache服务器&#xff0c;IIS服务器&#xff08;Internet Information …

Spring Cloud Gateway整合Sentinel

日升时奋斗&#xff0c;日落时自省 目录 1、实现整合 1.1、添加框架依赖 1.2、设置配置文件 1.3、设置限流和熔断规则 1.3.1、限流配置 Route ID限流配置 API限流配置 1.3.2、熔断配置 2、实现原理 先前Sentinel针对是业务微服务&#xff0c;没有整合Sentinel到Spring…

PPT插件-大珩助手-倒计时增加姓名

姓名倒计时 为方便使用同一台电脑进行计时&#xff0c;需要对不同的计时器进行区分&#xff0c;因此在原有的计时器上增加了可编辑的姓名&#xff0c;方便多人计时使用。 软件介绍 PPT大珩助手是一款全新设计的Office PPT插件&#xff0c;它是一款功能强大且实用的PPT辅助工具…

7 集中式日志和分布式跟踪

文章目录 日志聚合模式日志集中化的简单解决方案使用日志并输出分布式跟踪Spring Cloud Sleuth实现分布式跟踪 小结 前面的文章&#xff1a; 1、 1 一个测试驱动的Spring Boot应用程序开发 2、 2 使用React构造前端应用 3、 3 试驱动的Spring Boot应用程序开发数据层示例 4、…

腾讯云域名外部入库流程

注册商是腾讯云&#xff0c;且在腾讯云管理的&#xff0c;请使用此教程外部入库。 如您的域名注册商是腾讯云但在聚名管理&#xff0c;请参考教程&#xff1a;https://www.west.cn/faq/list.asp?unid2539 在外部入库操作之前&#xff0c;请先登录腾讯云获取账号ID信息。…

springboot基于java的小区物业管理系统(保安巡逻绿化消防)设计+jsp

小区物业管理系统采用的是JAVA语言开发&#xff0c;利用MySQL为数据库&#xff0c; 使用IDEA平台来编写代码&#xff0c;框架方面选择的是springbootweb框架&#xff0c;采用B/S结构实现系统。本系统的设计与开发过程中严格遵守软件工程的规范&#xff0c;运用软件设计模式&…

2024第15届电子教育、电子商务、电子管理和电子学习国际会议

第十五届电子教育、电子商务、电子管理和电子学习国际会议&#xff08;IC4E 2024&#xff09;将于2024年3月18日-21日在日本福冈举办。本次会议以电子技术为核心&#xff0c;围绕电子教育、电子商务、电子管理以及电子学习等各个方面展开研讨&#xff0c;为相关领域的专家学者们…

LeetCode-1822/1502/896/13

1.数组元素积的符号&#xff08;1822&#xff09; 题目描述&#xff1a; 已知函数 signFunc(x) 将会根据 x 的正负返回特定值&#xff1a; 如果 x 是正数&#xff0c;返回 1 。 如果 x 是负数&#xff0c;返回 -1 。 如果 x 是等于 0 &#xff0c;返回 0 。 给你一个整数数组…

JavaScript的闭包、执行上下文、到底是怎么回事?还有必要学吗?

在上一课&#xff0c;我们了解了 JavaScript 执行中最粗粒度的任务&#xff1a;传给引擎执行的代码段。并且&#xff0c;我们还根据“由 JavaScript 引擎发起”还是“由宿主发起”&#xff0c;分成了宏观任务和微观任务&#xff0c;接下来我们继续去看一看更细的执行粒度。 一…

java 体育明星管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 java Web 体育明星管理系统是一套完善的java web信息管理系统&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,Myeclipse8.5开发&#xff0c;数据库为Mysq…

25 心形按钮

效果演示 实现了一个心形的心形图案&#xff0c;当用户点击图案时&#xff0c;图案会旋转并缩小&#xff0c;同时背景颜色会变成白色。 Code <div class"love"><input id"switch" type"checkbox"><label class"love-heart&…

今日实践 — 附加数据库/重定向失败如何解决?

WMS数据库与重定向 前言正文如何建立数据库连接&#xff1f;第一步&#xff1a;打开SSMS&#xff0c;右击数据库&#xff0c;点击附加第二步&#xff1a;点击添加第三步&#xff1a;找到自己的数据库文件&#xff0c;点击确定按钮第四步&#xff1a;若有多个数据库&#xff0c;…