首页 > 解决方案 > 如何更新 GitHub Actions CI 以检测木马代码提交(恶意 [双向] unicode 字符、python)

问题描述

如何更新我的 GitHub Actions CI 管道,以便如果木马代码白皮书中演示的任何攻击变体作为 PR 提交到我的 GitHub 存储库,PR 要么自动拒绝提交,要么在 PR 警告中添加评论关于漏洞。

背景:在 2021 年 10 月 30 日,Nicholas Boucher 和 Ross Anderson 发表了一篇题为Trojan Source: Invisible Vulnerabilities的论文——其中概述了 unicode 在看起来(逐个像素)与非恶意代码,但实际上是恶意的。除了用于定义和调用不同函数的更明显的“模糊字符”外,它们还专门描述了聪明的攻击者如何利用 unicode 双向控制字符来做一些非常讨厌的事情

更多背景信息:我管理一个托管在 GitHub 上的开源 python 项目。撇开这篇论文发表后,GitHub 在查看包含潜在恶意 unicode 字符的代码时添加了警告,在合并 PR 时,在 GitHub WUI 中无法在 PR 中直观地检测到这些问题。

我的问题是:如何保护自己免受尚未被发现的恶意 unicode 提交?还有其他字面上看不到的漏洞?

我可以在我的 GitHub Actions CI 管道中添加什么来警告我用户贡献的 Python 代码中的隐形危险?

编辑:应捕获的示例包括以下 python 片段:

标签: pythonsecuritygithubunicodecontinuous-integration

解决方案


您可以将工作流添加到 GitHub Actions 管道,以检测非 ascii 字符并自动向 PR 注释 WARNING。

将其添加到.github/workflows/unicode_warn.yml您的存储库的根目录中:

################################################################################
# File:    .github/workflows/unicode_warn.yml
# Version: 0.1
# Purpose: Detects Unicode in PRs and comments the results of findings in PR
#           * https://tech.michaelaltfield.net/bidi-unicode-github-defense/
# Authors: Michael Altfield <michael@michaelaltfield.net>
# Created: 2021-11-20
# Updated: 2021-11-20
################################################################################
name: malicious_sanity_checks
 
# execute this workflow automatically on all PRs
on: [pull_request]
 
jobs:
 
  unicode_warn:
 
    runs-on: ubuntu-latest
    container: debian:bullseye-slim
 
    steps:
 
    - name: Prereqs
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        apt-get update
        apt-get install -y git bsdmainutils
        git clone "https://token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" .
      shell: bash
 
    - name: Check diff for unicode
      id: unicode_diff
      run: |
        set -x
        diff=`git diff --unified=0 ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -E "^[+]" | grep -Ev '^(--- a/|\+\+\+ b/)'`
        unicode_diff=`echo -n "${diff}" | grep -oP "[^\x00-\x7F]*"`
        unicode_grep_exit_code=$?
        echo "${unicode_diff}"
 
        unicode_diff_hexdump=`echo -n "${unicode_diff}" | hd`
        echo "${unicode_diff_hexdump}"
 
        # did we select any unicode characters?
        if [[ "${unicode_diff_hexdump}" == "" ]]; then
          # we didn't find any unicode characters
          human_result="INFO: No unicode characters found in PR's commits"
          echo "${human_result}"
 
        else
          # we found at least 1 unicode character
          human_result="^^ WARNING: Unicode characters found in diff!"
          echo "${human_result}"
          echo "${diff}"
 
        fi
 
        echo "UNICODE_HUMAN_RESULT=${human_result}" >> $GITHUB_ENV
 
      shell: bash {0}
 
    # leave a comment on the PR. See also
    #  * https://stackoverflow.com/a/64126737
    # make sure this doesn't open command injection risks
    #  * https://github.com/victoriadrake/github-guestbook/issues/1#issuecomment-657121754
    - name: Leave comment on PR
      uses: actions/github-script@v5
      with:
        github-token: ${{secrets.GITHUB_TOKEN}}
        script: |
          github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: "${{ env.UNICODE_HUMAN_RESULT }}"
          })

上面的文件定义了一个名为malware_sanity_checks 的工作流,作业名为unicode_warn。该作业包含多个步骤,每次在您的存储库中创建新 PR 时都会执行这些步骤:

  1. Prereqs - 首先,安装了 git 和 hd 等基本依赖项
  2. 检查 unicode 的差异- 一个简单的 BASH 脚本使用 grep 来检测 PR 的待合并提交的差异中的非 ascii 字符
  3. 对 PR 发表评论- 在 PR 上添加评论,指示提交是否包含 unicode 字符

有关此功能以及可以保护您免受木马源漏洞的更多信息,请参阅源代码

也可以看看


推荐阅读