本文以 alpine
镜像为例,alpine
作为最轻量的容器镜像,只有 5m
左右。所以导致很多常用的命令都没有,包括 curl
和 wget
,并且容器中是没有 /dev/tcp
设备的,exec
的 trick
也没法使用。
在攻防对抗中,我们通常会拿到容器的权限,可能是命令执行或者反弹 shell
。但是容器中没有任何可以下载的工具,也无法通过 apk
去安装命令。
cat /etc/os-release
/etc/os-release
可判断容器所以使用镜像源
既然没办法在容器内安装下载的命令工具,我们可以手动写一个编译好的下载工具到容器中。
这里我用 C
实现了一个最简单的文件下载工具,只支持 http
协议。
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, " C Downloader \n\n Usage: %s <url>\n only support http protocol \n", argv[0]);
return 1;
}
char *url = argv[1];
char *host_start = strstr(url, "http://");
if (host_start) {
host_start += 7;
} else {
host_start = url;
}
char *host = strtok(host_start, "/");
char *port_str = "80";
char *path = strtok(NULL, "");
char *colon = strchr(host, ':');
if (colon) {
*colon = '\0';
port_str = colon + 1;
}
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char request[256];
char buffer[1024];
int file;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "failed to connect\n");
return 2;
}
freeaddrinfo(servinfo);
snprintf(request, sizeof(request), "GET /%s HTTP/1.1\r\nHost: %s:%s\r\nConnection: close\r\n\r\n", path, host, port_str);
send(sockfd, request, strlen(request), 0);
file = open("downloaded_file", O_WRONLY | O_CREAT | O_TRUNC, 0644);
int received;
int header_end = 0;
while ((received = recv(sockfd, buffer, sizeof(buffer), 0)) > 0) {
if (!header_end) {
char *header_end_ptr = strstr(buffer, "\r\n\r\n");
if (header_end_ptr) {
header_end = 1;
write(file, header_end_ptr + 4, received - (header_end_ptr - buffer) - 4);
}
} else {
write(file, buffer, received);
}
}
close(file);
close(sockfd);
return 0;
}
我们需要编译上面这段 C
的代码,这里要注意宿主机和 alpine
容器中的动态链接库是不同的。
这里我们用 gcc
编译同样的 C
代码。
gcc -Os -s -fdata-sections -ffunction-sections -flto -Wl,--gc-sections -o downloader downloader.c
所以在宿主机上编译的程序是没办法在容器中运行的,需要在容器中重新编译!
因为 Alpine Linux
使用 musl libc
作为默认的 C
库,所以在 Alpine
环境中编译的程序将自动链接到 musl libc
要在 Alpine Linux
上编译一个依赖于 musl libc
库的二进制文件,需要使用一个 Alpine Linux
环境,并确保已安装必要的编译工具。
docker run --name apline -dit alpine
// 进入容器中
docker exec -it apline sh
// 安装gcc
apk add --no-cache build-base
// 编译工具代码
gcc -Os -s -fdata-sections -ffunction-sections -flto -Wl,--gc-sections -o downloader downloader.c
编译好的二进制文件通常有十几 kb
,虽然算不上大但还是会影响我们传输。可以通过 gzip
将体积压缩到最小。
gzip
压缩后已经到了可接受的长度。这里我把压缩后的 shellcode
放在下面。
H4sIAAAAAAAAA+1bfWwT5xl/7cTENYljykdTQOPUGQZlOHYGLA1NiQmBCwojLYFGW8EY+5x4OD7vfOFLqA0F2prMhfWPauomjW3VhugfRdNUFbYy89mxThPdtLWaKoHWQs9jG3TdEG0B73nu3jd578itqNKkarpHsX/3+73v87wfd/e+du7xEx1dy9wuF2FWRR4iyG5MNHgb1b2RkSqgNZNaeG8g95BxwKu5elY84Dajd6Qdw+8C1a14LzGji8NqYm+Cz4wkMOrn4bgVRY8ZeT+9PYE1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K//f+M3uFasxfoP5e6t9tqS9Y+C9o+7etc1RYYNGt14+p75y1Uv9TNv7M/gMXGvOjCDcAAA==
/ # cat output | base64 -d >new.gz
/ # gzip -d new.gz
/ # chmod +x new
/ # ./new
C Downloader Usage: ./new <url>
only support http protocol
部分 alpine
镜像其实是带有 busybox
的,如果有就没有必要那么麻烦直接用 busybox
种的 curl
或 wget
进行下载即可。
本文作者:Zgao
原文地址:https://zgao.top/%e6%9e%81%e7%ab%af%e5%ae%b9%e5%99%a8%e5%9c%ba%e6%99%af%e4%b8%8b%e7%9a%84%e8%bf%9c%e7%a8%8b%e6%96%87%e4%bb%b6%e4%b8%8b%e8%bd%bd%e6%80%9d%e8%b7%af/
文章来源:刨洞安全团队
黑白之道发布、转载的文章中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途及盈利等目的,否则后果自行承担!
如侵权请私聊我们删文
END