前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用容器逃逸实现远程登录k8s集群节点

利用容器逃逸实现远程登录k8s集群节点

原创
作者头像
李国宝
修改2021-05-21 11:09:52
1.5K0
修改2021-05-21 11:09:52
举报

某天,

某鱼说要吃瞄,

于是......

李国宝:边缘计算k8s集群SuperEdge初体验​zhuanlan.zhihu.com

图标
图标

照着上一篇文章来说,我这边边缘计算集群有一堆节点。

每个节点都在不同的网络环境下。

他们的共同点都是可以访问内网,

部分是某云学生主机,

部分是跑在家庭网络环境下的虚拟机,

甚至假设中还有一些是树莓派之类的机器。

所以他们另一个共同点是,基本都没有公网IP。

这样一来,我要实现远程登录到某些节点搞事的时候,

只有内网穿透这一条路子了。

使用frp进行内网穿透 - 少数派​sspai.com

图标
图标

https://github.com/fatedier/frp​github.com

内网穿透倒没什么,在公司使用这货长期跑了两年垮大洋穿透也很稳定。

只是...

只是...

只是...

要一台台机器配置一次,要维护一个稳定的公网服务器作为桥接。

就是...麻烦了点。


然后想了下。

当前的kube superedge边缘计算集群本身就实现了4层和7层的内网穿透,

理论上直接使用它的能力也可以做到远程登录的。

于是开始研究了一下怎么实现在只有kubectl环境的机器上,

直接登录k8s容器集群的node节点。

搜了一波之后首先发现的是这个项目。

A kubectl plugin to SSH into Kubernetes nodes using a SSH jump host Pod​github.com

看描述和需求来说,完全符合我的要求。

代码语言:javascript
复制
$ kubectl krew install ssh-jump

照着教程配置好插件,装好环境之后实践了一下。

....

一切都好,就是连不上去。

蛋疼了...

接着又找了一波,发现了一个Redhat老哥的博客。

A consistent, provider-agnostic way to SSH into any Kubernetes node

完美。

我想要的就是这个。

看了下插件代码。luksa/kubectl-plugins看了下插件代码。

https://github.com/luksa/kubectl-plugins/blob/master/kubectl-ssh​github.com

代码语言:javascript
复制
#!/usr/bin/env bash

set -e

ssh_node() {
  node=$1
  if [ "$node" = "" ]; then
    node=$(kubectl get node -o name | sed 's/node\///' | tr '\n' ' ')
    node=${node::-1}

    if [[ "$node" =~ " " ]]; then
      echo "Node name must be specified. Choose one of: [$node]"
      exit 1
    else
      echo "Single-node cluster detected. Defaulting to node $node"
    fi
  fi

  pod=$(
    kubectl create -o name -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  generateName: ssh-node-
  labels:
    plugin: ssh-node
spec:
  nodeName: $node
  containers:
  - name: ssh-node
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["chroot", "/host"]
    tty: true
    stdin: true
    stdinOnce: true
    securityContext:
      privileged: true
    volumeMounts:
    - name: host
      mountPath: /host
  volumes:
  - name: host
    hostPath:
      path: /
  hostNetwork: true
  hostIPC: true
  hostPID: true
  restartPolicy: Never
EOF
  )

  deletePod() {
    kubectl delete $pod --wait=false
  }
  trap deletePod EXIT

  echo "Created $pod"
  echo "Waiting for container to start..."
  kubectl wait --for=condition=Ready $pod >/dev/null
  kubectl attach -it $pod -c ssh-node

}

ssh_pod() {
  # TODO: improve this
  if [ "$1" == "" ]; then
    echo "Pod name must be specified."
    exit 1
  fi
  kubectl exec -it "$@" bash || (
    echo "Running bash in pod failed; trying with sh"
    kubectl exec -it "$@" sh
  )
}

print_usage() {
  echo "Provider-agnostic way of opening a remote shell to a Kubernetes node."
  echo
  echo "Enables you to access a node even when it doesn't run an SSH server or"
  echo "when you don't have the required credentials. Also, the way you log in"
  echo "is always the same, regardless of what provides the Kubernetes cluster"
  echo "(e.g. Minikube, Kind, Docker Desktop, GKE, AKS, EKS, ...)"
  echo
  echo "You must have cluster-admin rights to use this plugin."
  echo
  echo "The primary focus of this plugin is to provide access to nodes, but it"
  echo "also provides a quick way of running a shell inside a pod."
  echo
  echo "Examples: "
  echo "  # Open a shell to node of a single-node cluster (e.g. Docker Desktop)"
  echo "  kubectl ssh node"
  echo
  echo "  # Open a shell to node of a multi-node cluster (e.g. GKE)"
  echo "  kubectl ssh node my-worker-node-1"
  echo
  echo "  # Open a shell to a pod"
  echo "  kubectl ssh pod my-pod"
  echo
  echo "Usage:"
  echo "  kubectl ssh node [nodeName]"
  echo "  kubectl ssh pod [podName] [-n namespace] [-c container]"
  exit 0
}

if [ "$1" == "--help" ]; then
  print_usage
fi

if [[ "$1" == node/* ]]; then
  ssh_node ${1:5}
elif [ "$1" == "node" ]; then
  ssh_node $2
elif [[ "$1" == pod/* ]]; then
  ssh_pod "$@"
elif [ "$1" == "pod" ]; then
  shift
  ssh_pod "$@"
else
  print_usage
fi

认真看了一下这个脚本。

直呼人才啊。

果然是玩Linux的老哥啊。

牛逼啊。

太牛逼了。

太有趣了。

额。

讲人话。


这个脚本使用busybox镜像启动了容器实例,

通过chroot到 /host + 把宿主机所有文件挂在到容器实例的方式,

实现了在容器实例直接对宿主机系统一对一“Copy”(可能表达不太准确),

进而实现直接在这个容器实例中操作宿主机的所有资源。

是的,所有资源。

是的,所有资源。

是的,所有资源。

着这里直接能看到其他程序的进程,

免密码直接操作其他用户的数据。

所谓,

这就是容器逃逸。

然后....

我们的目的确实也达到了。

通过这种方式确实可以直接实现登录任意一台k8s node节点,

再也不需要密码和授权。


总结。

很好玩。

不明镜像确实有风险。

这个世界一直都不太安全。

参考资料:

docker 容器逃逸漏洞(CVE-2020-15257)风险通告

容器逃逸技术概览 - DockOne.io

rambo1412:容器逃逸技术概览

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档