first attempt

This commit is contained in:
Andy7475 2026-02-28 09:50:12 +00:00
parent 1eccfa729b
commit 974be37b3b
4 changed files with 226 additions and 1 deletions

44
docs/GoogleCloudSetup.md Normal file
View File

@ -0,0 +1,44 @@
# Google Cloud Setup Guide for CyberChef
To use Google Cloud capabilities (like Google Translate) within CyberChef, you need to configure a Google Cloud Project and obtain an authentication string (either an API Key or an OAuth Token).
## Method 1: API Key (Recommended for simplicity)
1. **Create a Project:**
- Go to the [Google Cloud Console](https://console.cloud.google.com/).
- Click on the project dropdown at the top and select **New Project**.
- Name your project (e.g., `cyberchefcloud`) and click **Create**.
2. **Enable the API:**
- In the Cloud Console search bar, type **"Cloud Translation API"** and select it.
- Click **Enable**.
- *(Note: You will need to have billing enabled on your Google Cloud account for the Translation API, even for the free tier).*
3. **Create the API Key:**
- Navigate to **APIs & Services > Credentials** in the left sidebar.
- Click **+ CREATE CREDENTIALS** at the top and select **API Key**.
- Your API Key will be generated. Copy this key; you will need it for the CyberChef "GCP Auth String" input.
4. **Secure the API Key (CRITICAL):**
- Since CyberChef runs entirely in your browser, your API Key will be visible to anyone you share your CyberChef recipe with or who inspects the network traffic. you **MUST** restrict it.
- Click on the newly created API Key to edit its settings.
- Under **Application restrictions**, select **Websites**.
- Under **Website restrictions**, click **ADD**.
- Enter the URLs where your CyberChef instance is hosted (e.g., `https://gchq.github.io/CyberChef/*` or `http://localhost:8080/*` for local testing).
- Under **API restrictions**, select **Restrict key**.
- Check the box for **Cloud Translation API**.
- Click **SAVE**.
## Method 2: Temporary OAuth Token (Recommended for Security)
If you have the Google Cloud SDK (`gcloud`) installed locally and you are authorized in your project, you can generate a short-lived token to use instead of an API Key. This is much more secure because the token expires automatically.
1. Ensure you are logged into your `gcloud` CLI:
```bash
gcloud auth login
```
2. Generate an access token:
```bash
gcloud auth print-access-token
```
3. Copy the output token and paste it into the CyberChef "GCP Auth String" input, making sure to change the "Auth Type" dropdown to **OAuth Token**.

55
docs/LocalTesting.md Normal file
View File

@ -0,0 +1,55 @@
# Local Testing Guide for CyberChef
If you are developing new Cloud API capabilities (like the Google Translate operation) and you want to test them locally on your machine, follow these steps to spin up the local CyberChef development server.
## Prerequisites
1. **Node.js**: Ensure you have Node.js installed. CyberChef requires Node 16 or later.
```bash
node -v
```
2. **NPM**: Ensure you have npm installed.
```bash
npm -v
```
## Setup and Running
1. **Install Dependencies:**
Open a terminal, navigate to the `CyberChefCloud` directory, and run:
```bash
npm install
```
*This might take a minute as it downloads everything required to build CyberChef.*
2. **Start the Development Server:**
Run the following command to start the local instance:
```bash
npm run start
```
*This will run a Grunt task that compiles the web interface, resolves operations, and provisions a local HTTP server using Webpack Dev Server.*
3. **Access CyberChef:**
By default, the server will host CyberChef on port 8080.
- Open your web browser.
- Go to `http://localhost:8080`.
## Testing the Translate Operation
1. With the local CyberChef instance open, type "Google Translate" into the **Operations** search bar in the top left.
2. Drag the `Google Translate` operation into the **Recipe** column.
3. In the **Input** column, type some text (e.g., "Hello world").
4. In the `Google Translate` operation configuration:
- Make sure **Source Language** is correct (e.g., `en`).
- Make sure **Target Language** is correct (e.g., `es`).
- If using an API key, leave Auth Type as **API Key** and paste your API key into the **GCP Auth String** box.
- *(Note: Ensure your API key restrictions at console.cloud.google.com temporarily allow `http://localhost:8080/*`).*
5. Check the **Manual Bake** checkbox at the bottom of the recipe column if it isn't checked by default, or just click **Bake!**.
6. The translated output should appear in the **Output** column.
## Advanced Testing (Command Line)
To ensure the CyberChef engine builds cleanly and passes its internal checks without UI verification, you can run the automated tests:
```bash
npm run test
```

View File

@ -541,6 +541,12 @@
"Heatmap chart" "Heatmap chart"
] ]
}, },
{
"name": "Cloud",
"ops": [
"Google Translate"
]
},
{ {
"name": "Other", "name": "Other",
"ops": [ "ops": [

View File

@ -0,0 +1,120 @@
/**
* @author CyberChefCloud
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
/**
* Google Translate operation
*/
class GoogleTranslate extends Operation {
/**
* GoogleTranslate constructor
*/
constructor() {
super();
this.name = "Google Translate";
this.module = "Cloud";
this.description = [
"Translates text using the Google Cloud Translation API.",
"<br><br>",
"Supports providing an API Key or an OAuth Bearer Token. ",
"See the setup guide in the documentation for how to secure your Cloud project.",
].join("\n");
this.infoURL = "https://cloud.google.com/translate/docs/reference/rest/v2/translate";
this.inputType = "string";
this.outputType = "string";
this.manualBake = true;
this.args = [
{
"name": "Source Language (ISO-639-1)",
"type": "string",
"value": "en"
},
{
"name": "Target Language (ISO-639-1)",
"type": "string",
"value": "es"
},
{
"name": "Auth Type",
"type": "option",
"value": ["API Key", "OAuth Token"]
},
{
"name": "GCP Auth String",
"type": "string",
"value": ""
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [sourceLanguage, targetLanguage, authType, authString] = args;
if (input.length === 0) return "";
if (!authString) throw new OperationError("Error: Please provide a valid GCP Auth String (API Key or OAuth Token).");
let url = "https://translation.googleapis.com/language/translate/v2";
const headers = new Headers();
headers.set("Content-Type", "application/json; charset=utf-8");
if (authType === "API Key") {
url += `?key=${encodeURIComponent(authString)}`;
} else if (authType === "OAuth Token") {
headers.set("Authorization", `Bearer ${authString}`);
}
const body = JSON.stringify({
q: input,
source: sourceLanguage,
target: targetLanguage,
format: "text"
});
const config = {
method: "POST",
headers: headers,
body: body,
mode: "cors",
cache: "no-cache",
};
return fetch(url, config)
.then(r => {
if (!r.ok) {
return r.json().then(err => {
let msg = err?.error?.message || r.statusText;
throw new OperationError(`Google Translation API Error (${r.status}): ${msg}`);
}).catch(() => {
throw new OperationError(`Google Translation API Error: ${r.status} ${r.statusText}`);
});
}
return r.json();
})
.then(data => {
if (data && data.data && data.data.translations && data.data.translations.length > 0) {
return data.data.translations[0].translatedText;
}
throw new OperationError("Error: Unexpected response format from Google Translation API.");
})
.catch(e => {
if (e instanceof OperationError) throw e;
throw new OperationError(e.toString() +
"\n\nThis error could be caused by a network issue or invalid authentication.");
});
}
}
export default GoogleTranslate;