2022-04-28 16:01:33 +00:00
|
|
|
|
<details>
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks云 ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 推特 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 你在**网络安全公司**工作吗?你想在HackTricks中看到你的**公司广告**吗?或者你想获得**PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f)或[**电报群组**](https://t.me/peass),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **通过向[hacktricks repo](https://github.com/carlospolop/hacktricks)和[hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)提交PR来分享你的黑客技巧**。
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
# 基本信息
|
2021-01-07 15:07:51 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
**Seccomp**或安全计算模式,简而言之,是Linux内核的一个功能,可以充当**系统调用过滤器**。\
|
|
|
|
|
Seccomp有两种模式。
|
2021-01-07 15:07:51 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
**seccomp**(安全计算模式)是Linux内核中的一种计算机安全功能。seccomp允许进程进入“安全”状态,其中**除了**`exit()`、`sigreturn()`、`read()`和`write()`对**已打开**的文件描述符进行系统调用外,**不能进行任何其他系统调用**。如果尝试进行其他系统调用,内核将使用SIGKILL或SIGSYS终止进程。从这个意义上说,它不会虚拟化系统的资源,而是完全将进程与它们隔离开来。
|
2021-10-10 21:46:47 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
通过使用`prctl(2)`系统调用的`PR_SET_SECCOMP`参数,或者(自Linux内核3.17以来)通过`seccomp(2)`系统调用,可以启用seccomp模式。seccomp模式曾经通过写入文件`/proc/self/seccomp`来启用,但这种方法已被`prctl()`取代。在某些内核版本中,seccomp禁用了`RDTSC` x86指令,该指令返回自上电以来经过的处理器周期数,用于高精度计时。
|
2021-10-10 21:46:47 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
**seccomp-bpf**是seccomp的扩展,它允许使用可配置策略的Berkeley Packet Filter规则对系统调用进行过滤。它被OpenSSH和vsftpd以及Chrome OS和Linux上的Google Chrome/Chromium Web浏览器使用。(在这方面,seccomp-bpf实现了类似的功能,但具有更高的灵活性和性能,与不再支持Linux的旧版systrace相似。)
|
2021-10-10 21:46:47 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
## **原始/严格模式**
|
2021-01-07 15:07:51 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
在这种模式下,Seccomp只允许对已打开的文件描述符进行`exit()`、`sigreturn()`、`read()`和`write()`系统调用。如果进行任何其他系统调用,进程将被使用SIGKILL终止。
|
2021-01-07 15:07:51 +00:00
|
|
|
|
|
2021-10-10 21:46:47 +00:00
|
|
|
|
{% code title="seccomp_strict.c" %}
|
2021-01-07 15:07:51 +00:00
|
|
|
|
```c
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <linux/seccomp.h>
|
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
|
|
|
|
|
//From https://sysdig.com/blog/selinux-seccomp-falco-technical-discussion/
|
|
|
|
|
//gcc seccomp_strict.c -o seccomp_strict
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2023-08-03 19:12:22 +00:00
|
|
|
|
int output = open("output.txt", O_WRONLY);
|
|
|
|
|
const char *val = "test";
|
|
|
|
|
|
|
|
|
|
//enables strict seccomp mode
|
|
|
|
|
printf("Calling prctl() to set seccomp strict mode...\n");
|
|
|
|
|
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
|
|
|
|
|
|
|
|
|
|
//This is allowed as the file was already opened
|
|
|
|
|
printf("Writing to an already open file...\n");
|
|
|
|
|
write(output, val, strlen(val)+1);
|
|
|
|
|
|
|
|
|
|
//This isn't allowed
|
|
|
|
|
printf("Trying to open file for reading...\n");
|
|
|
|
|
int input = open("output.txt", O_RDONLY);
|
|
|
|
|
|
|
|
|
|
printf("You will not see this message--the process will be killed first\n");
|
2021-01-07 15:07:51 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-05-01 12:41:36 +00:00
|
|
|
|
## Seccomp-bpf
|
2021-01-07 15:07:51 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
这种模式允许使用可配置的策略来实现使用伯克利数据包过滤器规则对系统调用进行过滤。
|
2021-01-07 15:07:51 +00:00
|
|
|
|
|
2021-10-10 21:46:47 +00:00
|
|
|
|
{% code title="seccomp_bpf.c" %}
|
2021-01-07 15:07:51 +00:00
|
|
|
|
```c
|
|
|
|
|
#include <seccomp.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
//https://security.stackexchange.com/questions/168452/how-is-sandboxing-implemented/175373
|
|
|
|
|
//gcc seccomp_bpf.c -o seccomp_bpf -lseccomp
|
|
|
|
|
|
|
|
|
|
void main(void) {
|
2023-08-03 19:12:22 +00:00
|
|
|
|
/* initialize the libseccomp context */
|
|
|
|
|
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL);
|
|
|
|
|
|
|
|
|
|
/* allow exiting */
|
|
|
|
|
printf("Adding rule : Allow exit_group\n");
|
|
|
|
|
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
|
|
|
|
|
|
|
|
|
|
/* allow getting the current pid */
|
|
|
|
|
//printf("Adding rule : Allow getpid\n");
|
|
|
|
|
//seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getpid), 0);
|
|
|
|
|
|
|
|
|
|
printf("Adding rule : Deny getpid\n");
|
|
|
|
|
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EBADF), SCMP_SYS(getpid), 0);
|
|
|
|
|
/* allow changing data segment size, as required by glibc */
|
|
|
|
|
printf("Adding rule : Allow brk\n");
|
|
|
|
|
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
|
|
|
|
|
|
|
|
|
|
/* allow writing up to 512 bytes to fd 1 */
|
|
|
|
|
printf("Adding rule : Allow write upto 512 bytes to FD 1\n");
|
|
|
|
|
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 2,
|
|
|
|
|
SCMP_A0(SCMP_CMP_EQ, 1),
|
|
|
|
|
SCMP_A2(SCMP_CMP_LE, 512));
|
|
|
|
|
|
|
|
|
|
/* if writing to any other fd, return -EBADF */
|
|
|
|
|
printf("Adding rule : Deny write to any FD except 1 \n");
|
|
|
|
|
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EBADF), SCMP_SYS(write), 1,
|
|
|
|
|
SCMP_A0(SCMP_CMP_NE, 1));
|
|
|
|
|
|
|
|
|
|
/* load and enforce the filters */
|
|
|
|
|
printf("Load rules and enforce \n");
|
|
|
|
|
seccomp_load(ctx);
|
|
|
|
|
seccomp_release(ctx);
|
|
|
|
|
//Get the getpid is denied, a weird number will be returned like
|
|
|
|
|
//this process is -9
|
|
|
|
|
printf("this process is %d\n", getpid());
|
2021-01-07 15:07:51 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
{% endcode %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
# Docker中的Seccomp
|
2021-01-07 16:33:00 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
**Seccomp-bpf**被**Docker**支持,可以限制容器中的**系统调用**,从而有效减少攻击面。您可以在[https://docs.docker.com/engine/security/seccomp/](https://docs.docker.com/engine/security/seccomp/)找到**默认情况下被阻止的系统调用**,并且默认的seccomp配置文件可以在[https://github.com/moby/moby/blob/master/profiles/seccomp/default.json](https://github.com/moby/moby/blob/master/profiles/seccomp/default.json)找到。\
|
|
|
|
|
您可以使用以下命令在Docker容器中运行具有**不同seccomp策略**的容器:
|
2021-01-07 16:33:00 +00:00
|
|
|
|
```bash
|
|
|
|
|
docker run --rm \
|
2023-08-03 19:12:22 +00:00
|
|
|
|
-it \
|
|
|
|
|
--security-opt seccomp=/path/to/seccomp/profile.json \
|
|
|
|
|
hello-world
|
2021-01-07 16:33:00 +00:00
|
|
|
|
```
|
2023-08-03 19:12:22 +00:00
|
|
|
|
如果你想禁止容器执行某些系统调用,比如 `uname`,你可以从 [https://github.com/moby/moby/blob/master/profiles/seccomp/default.json](https://github.com/moby/moby/blob/master/profiles/seccomp/default.json) 下载默认配置文件,并从列表中**删除 `uname` 字符串**。\
|
|
|
|
|
如果你想确保**某个二进制文件在 Docker 容器中无法运行**,你可以使用 strace 列出该二进制文件使用的系统调用,然后禁止它们。\
|
|
|
|
|
以下示例展示了如何发现 `uname` 的系统调用:
|
2021-01-07 16:33:00 +00:00
|
|
|
|
```bash
|
2021-06-22 22:41:24 +00:00
|
|
|
|
docker run -it --security-opt seccomp=default.json modified-ubuntu strace uname
|
2021-01-07 16:33:00 +00:00
|
|
|
|
```
|
2021-10-10 22:24:28 +00:00
|
|
|
|
{% hint style="info" %}
|
2023-08-03 19:12:22 +00:00
|
|
|
|
如果你只是使用Docker来启动一个应用程序,你可以使用`strace`对其进行分析,并只允许它需要的系统调用。
|
2021-10-10 22:24:28 +00:00
|
|
|
|
{% endhint %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
## 在Docker中禁用seccomp
|
2021-10-10 21:46:47 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
使用标志`--security-opt seccomp=unconfined`启动一个容器
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<details>
|
|
|
|
|
|
2023-04-25 18:35:28 +00:00
|
|
|
|
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 你在一家**网络安全公司**工作吗?你想在HackTricks中看到你的**公司广告**吗?或者你想获得**PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 发现我们的独家[NFTs](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 获得[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f)或[**电报群组**](https://t.me/peass),或在**Twitter**上**关注**我[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **通过向[hacktricks repo](https://github.com/carlospolop/hacktricks)和[hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)提交PR来分享你的黑客技巧**。
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
|
|
|
|
</details>
|