k8s

kubescape使用

介绍

GitHub地址:https://github.com/kubescape/kubescape
官方文档地址:https://hub.armosec.io/docs

Kubescape是一个开源的Kubernetes安全平台。它包括风险分析、安全合规性和错误配置扫描。它面向DevSecOps从业者或平台工程师,提供易于使用的CLI界面、灵活的输出格式和自动扫描功能。它为Kubernetes用户和管理员节省了宝贵的时间、精力和资源。

Kubescape扫描集群、YAML文件和Helm Charts。它根据多个框架(包括NSA-CISA,MITRE ATT&CK®和CIS基准)检测错误配置。

Kubescape由ARMO创建,是一个云原生计算基金会(CNCF) 沙盒项目。

安装

有两种安装方式,一种是使用命令行执行脚本安装。一种是在集群中安装。

命令行方式

curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | /bin/bash

脚本如下:

#!/bin/bash
set -e

while getopts v: option
do
    case ${option} in
        v) RELEASE="download/${OPTARG}";;
        *) ;;
    esac
done

if [ -z "${RELEASE}" ]; then
    RELEASE="latest/download"
fi

echo -e "\033[0;36mInstalling Kubescape..."
echo

BASE_DIR=~/.kubescape
KUBESCAPE_EXEC=kubescape

osName=$(uname -s)
if [[ $osName == *"MINGW"* ]]; then
    osName=windows
elif [[ $osName == *"Darwin"* ]]; then
    osName=macos
else
    osName=ubuntu
fi

arch=$(uname -m)
if [[ $arch == *"aarch64"* || $arch == *"arm64"* ]]; then
    arch="-arm64"
else
    if [[ $arch != *"x86_64"* ]]; then
        echo -e "\033[33mArchitecture $arch may be unsupported, will try to install the amd64 one anyway."
    fi
    arch=""
fi

mkdir -p $BASE_DIR 

OUTPUT=$BASE_DIR/$KUBESCAPE_EXEC
DOWNLOAD_URL="https://github.com/kubescape/kubescape/releases/${RELEASE}/kubescape${arch}-${osName}-latest"

curl --progress-bar -L $DOWNLOAD_URL -o $OUTPUT

# Find install dir
install_dir=/usr/local/bin # default if running as root
if [ "$(id -u)" -ne 0 ]; then
  install_dir=$BASE_DIR/bin # if not running as root, install to user dir
  export PATH=$PATH:$BASE_DIR/bin
fi

# Create install dir if it does not exist
if [ ! -d "$install_dir" ]; then
  mkdir -p $install_dir
fi

chmod +x $OUTPUT 2>/dev/null

# cleaning up old install
SUDO=
if [ "$(id -u)" -ne 0 ] && [ -n "$(which sudo)" ] && [ "$KUBESCAPE_EXEC" != "" ] && [ -f /usr/local/bin/$KUBESCAPE_EXEC ]; then
    SUDO=sudo
    echo -e "\n\033[33mOld installation as root found, do you want to remove it? [\033[0my\033[33m/n]:"
    read -n 1 -r
    if [[ ! $REPLY =~ ^[Yy]$ ]] && [[ "$REPLY" != "" ]]; then
        echo -e "\n\033[0mSkipping old installation as root removal."
    else
        echo -e "\n\033[0mWe will need the root access to uninstall the old kubescape CLI."
        if $SUDO rm -f /usr/local/bin/$KUBESCAPE_EXEC 2>/dev/null; then
            echo -e "\033[32mRemoved old installation as root at /usr/local/bin/$KUBESCAPE_EXEC"
        else
            echo -e "\033[31mFailed to remove old installation as root at /usr/local/bin/$KUBESCAPE_EXEC, please remove it manually."
        fi
    fi
fi

if [ "$KUBESCAPE_EXEC" != "" ]; then
    if [ "${SUDO_USER:-$USER}" != "" ]; then
        rm -f /home/"${SUDO_USER:-$USER}"/.kubescape/bin/$KUBESCAPE_EXEC 2>/dev/null || true
    fi
    if [ "$BASE_DIR" != "" ]; then
        rm -f $BASE_DIR/bin/$KUBESCAPE_EXEC 2>/dev/null || true
    fi
fi

# Old install location, clean all those things up
for pdir in ${PATH//:/ }; do
    edir="${pdir/#\~/$HOME}"
    if [[ $edir == $HOME/* ]] && [[ -f $edir/$KUBESCAPE_EXEC ]]; then
        echo -e "\n\033[33mOld installation found at $edir/$KUBESCAPE_EXEC, do you want to remove it? [\033[0my\033[33m/n]:"
        read -n 1 -r
        if [[ ! $REPLY =~ ^[Yy]$ ]] && [[ "$REPLY" != "" ]]; then
            continue
        fi
        if rm -f "$edir"/$KUBESCAPE_EXEC 2>/dev/null; then
            echo -e "\n\033[32mRemoved old installation at $edir/$KUBESCAPE_EXEC"
        else
            echo -e "\n\033[31mFailed to remove old installation as root at $edir/$KUBESCAPE_EXEC, please remove it manually."
        fi
    fi
done

cp $OUTPUT $install_dir/$KUBESCAPE_EXEC
rm -f $OUTPUT

echo
echo -e "\033[32mFinished Installation."

echo -e "\033[0m"
$KUBESCAPE_EXEC version
echo

echo -e "\033[35mUsage: $ $KUBESCAPE_EXEC scan"

if [ "$(id -u)" -ne 0 ]; then
  echo -e "\nRemember to add the Kubescape CLI to your path with:"
  echo -e "  export PATH=\$PATH:$BASE_DIR/bin"
fi

echo -e "\033[0m"

脚本中下载的URL为https://github.com/kubescape/kubescape/releases/${RELEASE}/kubescape${arch}-${osName}-latest,下载好后,复制到/root/.kubescape/kubescape,然后可以修改脚本,注释curl --progress-bar -L $DOWNLOAD_URL -o $OUTPUT,再次执行。

wget https://github.com/kubescape/kubescape/releases/download/v2.3.7/kubescape-ubuntu-latest
mv kubescape-ubuntu-latest kubescape
cp kubescape  /root/.kubescape/kubescape
vim install-kubescape.sh
sh install-kubescape.sh

file

执行

kubescape scan --format html --output results.html  --verbose

file

输出有安全等级,告警项,参考文档,修复方案。

file

文档内容:可以根据示例进行修复。

file

结果汇总:

file

results.html

file

其他用法

# 扫描当前Kubernetes集群:
kubescape scan --verbose
# 扫描其他集群:
kubescape scan --kubeconfig config.conf
# 扫描特定的命名空间:
kubescape scan --include-namespaces kube-system
# 排除某些命名空间:
kubescape scan --exclude-namespaces default
# 扫描本地 YAML/JSON 文件:
kubescape scan nginx.yaml
# 从Git仓库扫描Kubernetes清单文件:
kubescape scan https://github.com/kubescape/kubescape
# 扫描Helm Chart,kubescape将加载默认的VALUES文件。
kubescape scan </path/to/directory>
# 扫描Kustomize目录,Kubescape将使用kustomize文件生成Kubernetes YAML对象并扫描它们以确保安全。
kubescape scan </path/to/directory>

输出格式

# JSON:
kubescape scan --format json --format-version v2 --output results.json
# XML:
kubescape scan --format junit --output results.xml
# PDF:
kubescape scan --format pdf --output results.pdf
# 普罗米修斯指标:
kubescape scan --format prometheus
# HTML
kubescape scan --format html --output results.html

集群方式安装

文档地址:https://hub.armosec.io/docs/installation-of-armo-in-cluster

前提

  • 需要先注册Kubescape Cloud账户,注册地址:https://portal.armo.cloud/
  • 有集群的管理权限
  • 有kubectl和helm
  • Kubescape组件至少需要400MibRAM和400mCPU
  • pod需要挂载pvc,需要存储。

file

注册登录后,会弹出一个添加集群的链接。复制粘贴即可。

helm repo add kubescape https://kubescape.github.io/helm-charts/
helm repo update
helm upgrade --install kubescape kubescape/kubescape-cloud-operator -n kubescape --create-namespace --set clusterName=`kubectl config current-context` --set account=fe437c28-7af5- --set capabilities.relevancy=detect --set clusterServer=`kubectl config view -o jsonpath="{.clusters[?(@.name=='$(kubectl config current-context)')].cluster.server}"`

file

集群版本1.20.15,执行会报错:no matches for kind "CronJob" in version "batch/v1",查看api-resources,确认cronjob在batch/v1beta1中。

file

需要下载helm chart,修改cronjob的api版本。我这里跳过了换了个1.24版本的集群hhh

file

返回ARMO页面,等待集群连接。

file

集群连接上后会自动扫描。

file

file

查看合规信息。

file

点击Fix,可以看到修复方案。

file

git仓库扫描:

镜像仓库扫描:

查看Kubescape在集群中运行状态,

file

查看定时任务:

file

和kube-score对比

GitHub地址:https://github.com/zegl/kube-score

  • kube-score也是一个对Kubernetes对象定义执行静态代码分析的工具。
  • 它的输出是可以改进哪些建议的列表,以使应用程序更安全、更具弹性。
  • 可以使用demo在浏览器中测试kube分数。https://kube-score.com/
  • 但是只有扫描合规性的提示,没有规范出处。

file

安装

https://github.com/zegl/kube-score/releases
docker pull zegl/kube-score
brew install kube-score
kubectl krew install score

使用

kube-score score my-app/*.yaml
helm template my-app | kube-score score -
kustomize build . | kube-score score -
kubectl api-resources --verbs=list --namespaced -o name \
  | xargs -n1 -I{} bash -c "kubectl get {} --all-namespaces -oyaml && echo ---" \
  | kube-score score -
分类: k8s
0 0 投票数
文章评分
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部
0
希望看到您的想法,请您发表评论x