惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 李国宝

2206年最佳边缘计算集群方案:Tailscale + k3s 谁不想要自己的Tailscale内网呢~ 腾讯云API网关废了?集群开源方案平替 来一打自建IP Proxy玩玩之Majora Github Copilot 比在座各位更会写代码。jpg .NET Core + React Antd Pro脚手架 【爬虫系列】2. 打开App逆向“潘多拉魔盒” Ratel:一直站在Android逆向巅峰的平头哥 【爬虫系列】1. 无事,Python验证码识别入门 【爬虫系列】0. 无内鬼,破解前端JS参数签名 边缘计算k8s集群SuperEdge初体验 能动手绝不多说:开源评论系统remark42上手指南 一次依赖注入不慎引发的一连串事故 反手来个K8S入门到跑路 MySQL Online DDL导致全局锁表案例分析 .NET Core教程--给API加一个服务端缓存啦 任务队列和异步接口的正确打开方式(.NET Core版本) .NET Core中使用RabbitMQ正确方式 - 李国宝 - 博客园 .NET Core单元测试之搞死开发的覆盖率统计(coverlet + ReportGenerator )
利用容器逃逸实现远程登录k8s集群节点
李国宝 · 2021-01-21 · via 博客园 - 李国宝

某天,

某鱼说要吃瞄,

于是......

李国宝:边缘计算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
看描述和需求来说,完全符合我的要求。

$ 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

#!/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:容器逃逸技术概览