CICD
CI/CD
CI: 即 持续集成 ,是代码开发人员在将代码提交到代码存储库中。自动进行频繁的自动构建和测试阶段,频繁的将开发者的代码合并到主干,减少代码的质量问题:smile:
在多人协作的项目中,各开发人员负责各自的开发模块。每当完成一项需求后,开发人员需将代码提交至代码仓库。若每次提交后都人工进行构建和测试,工作量繁重且效率低下。而若等到一段时间后再统一测试,一旦发现问题,由于距离编写代码已过去一段时间,开发人员可能遗忘细节,导致修复困难重重。:face_with_head_bandage:为此,引入 Jenkins 等工具,在每次代码提交后自动执行构建和测试。若测试通过,代码自动合并至主分支;若不通过,则立即通知开发者修复。这种方式显著提升了开发效率,降低了问题排查和修复的成本。
关键组件
版本控制系统(VCS)
如 Git、SVN 等,用于管理代码的版本和多人协作,确保代码提交的追踪和管理。构建工具
如 Maven、Gradle、Ant 等,负责将源代码编译成可执行文件或可部署的软件包。自动化测试框架
如 JUnit、Selenium、Pytest 等,用于运行单元测试、集成测试等,确保代码质量。CI 服务器/工具
如 Jenkins、GitLab CI/CD、CircleCI 等,负责监听代码提交,触发自动化构建和测试流程,并提供执行结果反馈。
CD:即 持续部署,将通过CI测试的代码,自动化部署到 测试、生产环境中。实现软件的快速交付和更新:wrench:
自动化部署工具
例如Docker、Ansible、Kubernetes等,用于自动化地部署应用程序和其依赖。环境配置管理
工具如Terraform,确保不同环境(开发、测试、生产)的一致性。持续监控和反馈
使用工具如Prometheus、Grafana,确保在部署后能够监控应用程序的性能和稳定性。
Poisoned Pipeline Execution(PPE)
中毒管道执行(PPE)是指攻击者可以访问代码存储系统,无法访问代码构建系统。通过在存储的代码中注入恶意代码来进行攻击代码构建系统。
D-PPE(Direct PPE, 直接PPE)
攻击者可以直接对代码存储库中的CI配置文件进行修改(例如Jenkins的配置文件Jenkinsfile)。导致在代码配置的过程中执行了恶意的命令。
I-PPE(Indirect PPE, 间接PPE)
攻击者无法对修改CI配置文件,但是可以修改管道中引用的命令的配置文件进行恶意代码注入。比如代码在构建的过程中使用了make
的命令,刚好你有修改makefile文件的权限,这个时候就可以将恶意命令注入到makefile中来进行攻击:smile:
3PE(Indirect PPE, 公共PPE)
这个攻击针对与公开的存储库。匿名攻击者通过请求拉取注入恶意代码,如果管道在不经审查的情况下就去构建代码。就可能会执行攻击者注入的恶意代码
示例
1 | 127.0.0.1:3000 是一个gitea服务 |
D-PPE
**题目:**White Rabbit
描述:
I’m late, I’m late! No time to say ״hello, goodbye״! Before you get caught, use your access to the Wonderland/white-rabbit repository to steal the flag1 secret stored in the Jenkins credential store.
我迟到了,我迟到了!没有时间说“你好,再见”了!在被抓住之前,利用你对 Wonderland/white-rabbit 存储库的访问权限窃取存储在 Jenkins 凭证存储中的 flag1 机密。
hint:
- Try to trigger a pipeline through the repository.
- How can you access credentials using the Jenkinsfile?
Gitea中的White Rabbit对所有人都用编辑权限
直接对Jenkinsfile进行修改
stage阶段 ()
中的是阶段名称
step这是该stage时期要做的时期
然后提交代码创建合并请求
然后Jenkins就会根据Jenkinsfile的配置自动对代码进行构建
看shell阶段的logs
命令被成功执行
I-PPE
**题目:**Mad Hatter
描述:
Jenkinsfile is protected? Sounds like an unbirthday party. Use your access to the Wonderland/mad-hatter repository to steal the flag3 secret.
Jenkinsfile 受到保护?听起来像是一个非生日聚会。使用您对 Wonderland/mad-hatter 存储库的访问权限窃取 flag3 机密。
hint:
- Where’s the Jenkinsfile stored? Search for the repo name in the Wonderland organization, you might find some helpful repos.
- What commands are run by the Jenkinsfile?
Gitea中的mad-hatter
所有人都用编辑权限
但是在这个代码存储库中并没有看到Jenkinsfile这个配置文件
这个配置文件在另外一个私有仓库mad-hatter-pipeline
存放, 我们没有对立面文件的修改权限
但是他在构建的过程中使用了make命令
:bulb: 在 Linux 中,make 命令是一个非常常用的构建工具,主要用于自动化编译和链接程序,尤其是当你处理大型项目时。它通过读取一个叫做 Makefile(或 makefile)的文件来决定哪些文件需要编译、如何编译以及如何生成最终的可执行文件。
makefile在mad-hatter
仓库中存放。并且我们对他有修改权限。
在mad-hatter
中修改makefile之后提交代码
提交了之后,到Jenkins中看make部分的日志
3PE
**题目:**Caterpillar
描述:
Who. Are. You? You just have read permissions… is that enough? Use your access to the Wonderland/caterpillar repository to steal the flag2 secret, which is stored in the Jenkins credential store.
你是谁?你只有读取权限……这就够了吗?使用你对 Wonderland/caterpillar 存储库的访问权限来窃取存储在 Jenkins 凭证存储中的 flag2 机密。
hint:
- Fork the repository and create a pull request to trigger a malicious pipeline.
- After you execute your malicious code in the Jenkins job, what environment variables can help you move forward?
- Found that Gitea access token from the pipeline? Great. There’s another pipeline which is triggered by pushing to the main branch. Maybe you can access the flag from there!
我们没有Gitea上面caterpillar
这个存储库的编辑权限
fork一个当自己的库
在自己的caterpillar
库中编辑好Jenkinsfile
然后将它合并到远程的仓库中
然后Jenkins的wonderland-caterpillar-test
流水线上面的日志里面就可以看到我们的执行结果
不过这个题目的做法是 ,你需要在wonderland-caterpillar-test
上面执行env 拿到 caterpillar
这个库的管理员凭证。然后去修改caterpillar
中的Jenkinsfile
进而在wonderland-caterpillar
上面执行命令
我们修改Jenkinsfile
再次提交
拿到凭证后将代码克隆到本地,修改Jenkinsfile
然后再推送到远程仓库
1 | ┌──(root㉿LAPTOP-40PQI58C)-[~/Desktop/cicd/cicd-goat/reports] |