VirusTotal GitHub Action

repository·master·Indexed 42 days ago

https://github.com/crazy-max/ghaction-virustotal

A GitHub Action to upload and scan files with VirusTotal. It supports scanning local files via path globs, utilizing the VirusTotal Monitor endpoint, and automatically scanning and annotating GitHub Release assets. Version v5 includes features for rate limiting and the ability to append analysis links directly to the GitHub Release body.

Tokens
3.3K
Snippets
10
Records
14
Agent score
54%

What's inside VirusTotal GitHub Action

  1. Scan through VirusTotal Monitor

    master

    To scan assets through the VirusTotal Monitor endpoint, enable vt_monitor and specify a monitor_path. The monitor_path is a path relative to your current monitor user root folder.

    - name: VirusTotal Scan
      uses: crazy-max/ghaction-virustotal@v5
      with:
        vt_api_key: ${{ secrets.VT_API_KEY }}
        vt_monitor: true
        monitor_path: /ghaction-virustotal
        files: |
          ./foo-win32.exe
          ./foo-win64.exe
  2. Scan local files with VirusTotal

    master

    Use this action to upload and scan specific local files from your repository using your VirusTotal API key. The files input accepts a newline-delimited list of path globs or patterns.

    - name: VirusTotal Scan
      uses: crazy-max/ghaction-virustotal@v5
      with:
        vt_api_key: ${{ secrets.VT_API_KEY }}
        files: |
          ./foo-win32.exe
          ./foo-win64.exe
  3. Scan assets of a published GitHub release

    master

    You can trigger a scan automatically when a GitHub release is published.

    Requirements:

    1. The workflow must trigger on the release event.
    2. If you want to append analysis links to the release body using update_release_body: true, you must provide a github_token and ensure the job has contents: write permissions.
    3. If using update_release_body, the github_token is required to authenticate with the GitHub API.
    name: released
    
    permissions:
      contents: read
    
    on: release:
        types: [published]
    
    jobs:
      virustotal:
        runs-on: ubuntu-latest
        permissions:
          contents: write
        steps:
          - name: VirusTotal Scan
            uses: crazy-max/ghaction-virustotal@v5
            with:
              vt_api_key: ${{ secrets.VT_API_KEY }}
              update_release_body: true
              github_token: ${{ secrets.GITHUB_TOKEN }}
              files: |
                .exe$
  4. VirusTotal GitHub Action execution modes

    master

    The action automatically switches its logic based on the github.context.eventName:

    Local Files Mode

    Triggered by any event other than release.

    • It uses context.resolvePaths(inputs.files) to find files on the runner.
    • It iterates through files and uploads them using vt.files(filepath) or vt.monitorItems(filepath, inputs.monitorPath).

    Release Assets Mode

    Triggered by a release event.

    • It fetches the release and its assets using octokit.
    • It downloads assets to a temporary directory.
    • It uploads them to VirusTotal.
    • It can append the results to the release description if update_release_body is set to true.
  5. How the VirusTotal GitHub Action executes

    master

    The action operates in two distinct modes based on the GitHub event type:

    1. Local File Scan: If the event is NOT a release event, the action resolves the file paths provided in the files input (supporting globs) and uploads them directly to VirusTotal.
    2. Release Asset Scan: If the event is a release event, the action identifies assets in the release that match the files input patterns. It downloads these assets, uploads them to VirusTotal, and optionally updates the GitHub Release body with the analysis links.

    Key behaviors:

    • Rate Limiting: If requestRate is configured, the action uses a limiter to respect VirusTotal API constraints.
    • VirusTotal Monitor: If vtMonitor is enabled, files are uploaded via the VirusTotal Monitor endpoint using the provided monitorPath instead of the standard file upload endpoint.
    • Output: The action produces an analysis output containing a comma-separated list of filepath=url mappings.
  6. Configure inputs for ghaction-virustotal

    master

    The ghaction-virustotal GitHub Action consumes several inputs to configure its behavior. When defining your workflow YAML, use the following input keys:

    • vt_api_key: Your VirusTotal API key.
    • files: A list of files or glob patterns to scan. Files can be provided as a newline-separated list or a comma-separated list.
    • vt_monitor: Boolean flag to enable VirusTotal Monitor mode.
    • monitor_path: The path to monitor when vt_monitor is enabled (defaults to /).
    • update_release_body: Boolean flag to determine if the GitHub Release body should be updated with scan results.
    • github_token: GitHub token required if update_release_body is enabled.
    • request_rate: The number of requests per second to limit the rate (defaults to 0, which implies no limit).
    - uses: crazy-max/ghaction-virustotal@v5
      with:
        vt_api_key: ${{ secrets.VT_API_KEY }}
        files: |
          path/to/file1.exe
          path/to/file2.bin
        vt_monitor: true
        monitor_path: '/tmp'
        update_release_body: true
        github_token: ${{ secrets.GITHUB_TOKEN }}
        request_rate: 15
  7. Update GitHub Release body with analysis results

    master

    When running during a release event, you can automatically append the VirusTotal analysis links to the GitHub Release description.

    To enable this, set the update_release_body input to true.

    Formatting logic:

    • If there are 5 or fewer assets, the links are appended as a simple list.
    • If there are more than 5 assets, the links are wrapped in a <details> HTML tag to keep the release body clean.
  8. Configure VirusTotal Monitor usage

    master

    To use the VirusTotal Monitor feature instead of standard file uploads, set the vtMonitor input to true. When this is enabled, the action uses the monitorItems method which requires a monitorPath input.

    Inputs required:

    • vtMonitor: true
    • monitorPath: The path used for monitoring items.
  9. Reference: Action Outputs

    master

    The following outputs are available from the action.

    | Name | Type | Description |
    |---------------|----------------|----------------------------------------------------------------------------|
    | `analysis` | String | Analysis results formatted as `<filename>=<analysisURL>` (comma separated) |
  10. Reference: Action Inputs

    master

    The following inputs are available for crazy-max/ghaction-virustotal@v5 via step.with.

    | Name | Type | Default | Description |
    |----------------------------|--------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    | `vt_api_key` | String | | [VirusTotal API key](https://docs.virustotal.com/reference/authentication) to upload assets (**required**) |
    | `files` | String | | Newline-delimited list of path globs/patterns for asset files to upload for analysis (**required**) |
    | `vt_monitor` | Bool | `false` | If enabled, files will be uploaded to [VirusTotal Monitor](https://docs.virustotal.com/reference/software-publishers) endpoint |
    | `monitor_path` | String | `/` | A path relative to current monitor user root folder to upload files (Only available if `vt_monitor` is enabled) |
    | `update_release_body` | Bool | `false` | If enabled, analysis link(s) will be appended to the release body (Only available if [release event is triggered](#scan-assets-of-a-published-release) in your workflow) |
    | `github_token` | String | | [GitHub Token](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) used to create an authenticated client for GitHub API (Required if [release event is triggered](#scan-assets-of-a-published-release) in your workflow) |
    | `request_rate` | Number | `0` | API request-rate in requests/minute. Set to `4` or lower when using the standard free public API. `0` to disable rate-limit. |
  11. Use the VirusTotal class to upload files

    master

    The VirusTotal class provides an interface to interact with the VirusTotal API v3. You can instantiate it with an API key and use it to upload files for analysis or to the VirusTotal Monitor.

    import { VirusTotal } from './virustotal';
    
    const vt = new VirusTotal('YOUR_API_KEY');
    
    // Upload a file for standard analysis
    const uploadData = await vt.files('path/to/file.exe');
    console.log(uploadData.url); // https://www.virustotal.com/gui/file-analysis/${uploadData.id}/detection
    
    // Upload a file to VirusTotal Monitor
    const monitorData = await vt.monitorItems('path/to/file.exe', '/expected/path');
    console.log(monitorData.url); // https://www.virustotal.com/monitor/analyses/item:${monitorData.id}
  12. Understand the UploadData return type

    master

    When calling files() or monitorItems(), the returned UploadData object contains the following fields:

    • id: The unique identifier for the upload/analysis.
    • type: The type of the upload.
    • url: A direct link to the VirusTotal web interface for viewing the results.
    interface UploadData {
      id: string;
      type: string;
      url: string;
    }