手把手教你使用GitHub Actions进行安全开发
字数 1025 2025-08-15 21:32:10
GitHub Actions安全开发实战指南
一、GitHub Actions简介
GitHub Actions是GitHub于2019年11月正式推出的CI/CD服务,它允许开发人员基于代码提交(check-ins)和拉取请求(pull request)等事件触发器来自动化构建、测试和部署代码。
核心特性
- 基于事件驱动的工作流自动化
- 支持私有仓库和公开仓库
- 提供Windows、Linux和macOS运行环境
- 可与其他GitHub功能无缝集成
二、准备工作
1. 创建私有代码库
建议使用私有仓库存储安全工具,避免公开敏感信息。
2. 添加工具子模块
git submodule add https://github.com/BloodHoundAD/SharpHound3.git
子模块的优势:
- 保持与原始代码库版本同步
- 便于工具更新
- 确保私有仓库稳定运行
三、配置工作流
1. 创建工作流文件
在仓库的"Actions"标签页中创建新的工作流,系统会自动生成.github/workflows目录和YAML脚手架文件。
2. 基础工作流示例
name: Build
on: [push]
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Checkout submodules
shell: bash
run: |
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
git submodule sync --recursive
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
- name: Setup Nuget.exe
uses: warrenbuckley/Setup-Nuget@v1
- name: Nuget Restore
run: nuget restore $Env:GITHUB_WORKSPACE\SharpHound3\SharpHound3.sln
- name: Build SharpHound3
run: |
cd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\"
.\MSBuild.exe $Env:GITHUB_WORKSPACE\SharpHound3\SharpHound3\SharpHound3.csproj /property:Configuration=Release
- uses: actions/upload-artifact@master
with:
name: SharpHound3
path: SharpHound3\SharpHound3\bin\Release\SharpHound.exe
工作流解析
- 运行环境:
windows-latest指定使用Windows容器 - 步骤详解:
- 检出代码库和子模块
- 设置NuGet环境
- 恢复项目依赖
- 使用MSBuild编译项目(Release模式)
- 上传构建产物
四、GitHub API访问
1. 创建Personal Access Token(PAT)
- 访问GitHub设置页面生成PAT
- 选择适当的权限范围(至少需要
repo权限)
2. API请求示例
curl -u username:token 'https://api.github.com/repos/owner/repo/actions/artifacts'
3. 脚本化API调用
sub make_API_request {
$cmd = @('curl', '-u ' . $username . ':' . $access_token, $api_url . $repo_url . $endpoint);
$curl_command = exec($cmd);
$data = readAll($curl_command);
closef($curl_command);
return $data;
}
五、安全工具集成
1. 工具获取流程
- 通过API查询可用工具
- 获取工具下载URL
- 下载包含工具的ZIP文件
- 提取并执行工具
2. Cobalt Strike集成
使用Execute-Assembly命令执行从GitHub Actions构建的工具:
execute-assembly /path/to/SharpHound.exe
六、最佳实践
- 安全存储凭证:使用GitHub Secrets存储敏感信息如PAT
- 最小权限原则:为PAT分配最小必要权限
- 构建隔离:每个构建运行在临时容器中,完成后自动销毁
- 产物管理:及时清理不再需要的构建产物
- 日志监控:定期审查工作流执行日志
七、资源链接
- GitHub Actions官方文档
- Personal Access Token管理
- 示例攻击脚本 (原文中链接已失效,请替换为实际可用资源)
通过本指南,您可以充分利用GitHub Actions实现安全工具的自动化构建和部署,提高红队行动的效率和隐蔽性。