Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Text Encoding Brute Force operation #439

Merged
merged 3 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"Change IP format",
"Encode text",
"Decode text",
"Text Encoding Brute Force",
"Swap endianness",
"To MessagePack",
"From MessagePack",
Expand Down
69 changes: 69 additions & 0 deletions src/core/operations/TextEncodingBruteForce.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @author Cynser
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

import Operation from "../Operation";
import Utils from "../Utils";
import cptable from "../vendor/js-codepage/cptable.js";
import {IO_FORMAT} from "../lib/ChrEnc";

/**
* Text Encoding Brute Force operation
*/
class TextEncodingBruteForce extends Operation {

/**
* TextEncodingBruteForce constructor
*/
constructor() {
super();

this.name = "Text Encoding Brute Force";
this.module = "CharEnc";
this.description = "Enumerate all possible text encodings for input.";
this.infoURL = "https://wikipedia.org/wiki/Character_encoding";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Mode",
type: "option",
value: ["Encode", "Decode"]
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const output = [],
charSets = Object.keys(IO_FORMAT),
mode = args[0];

for (let i = 0; i < charSets.length; i++) {
let currentEncoding = charSets[i] + ": ";

try {
if (mode === "Decode") {
currentEncoding += cptable.utils.decode(IO_FORMAT[charSets[i]], input);
} else {
currentEncoding += cptable.utils.encode(IO_FORMAT[charSets[i]], input);
}
} catch (err) {
currentEncoding += "Could not decode.";
}

output.push(Utils.printable(currentEncoding, true));
}

return output.join("\n");
}

}

export default TextEncodingBruteForce;
1 change: 1 addition & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import "./tests/operations/SetIntersection";
import "./tests/operations/SetUnion";
import "./tests/operations/StrUtils";
import "./tests/operations/SymmetricDifference";
import "./tests/operations/TextEncodingBruteForce";
import "./tests/operations/ToGeohash.mjs";
import "./tests/operations/TranslateDateTimeFormat";
import "./tests/operations/Magic";
Expand Down
35 changes: 35 additions & 0 deletions test/tests/operations/TextEncodingBruteForce.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Text Encoding Brute Force tests.
*
* @author Cynser
*
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import TestRegister from "../../TestRegister";

TestRegister.addTests([
{
name: "Text Encoding Brute Force - Encode",
input: "Булкі праз ляніва сабаку.",
expectedMatch: /Windows-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./,
recipeConfig: [
{
op: "Text Encoding Brute Force",
args: ["Encode"],
},
],
},
{
name: "Text Encoding Brute Force - Decode",
input: "Áóëê³ ïðàç ëÿí³âà ñàáàêó.",
expectedMatch: /Windows-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./,
recipeConfig: [
{
op: "Text Encoding Brute Force",
args: ["Decode"],
},
],
}
]);