From 71c743ff5a6c4f27c9088329bb41211c10845336 Mon Sep 17 00:00:00 2001 From: Cynser <42423063+Cynser@users.noreply.github.com> Date: Wed, 12 Dec 2018 17:34:45 +0000 Subject: [PATCH 1/4] Add Text Encoding Brute Force operation --- src/core/config/Categories.json | 1 + .../operations/TextEncodingBruteForce.mjs | 52 +++++++++++++++++++ test/index.mjs | 1 + .../operations/TextEncodingBruteForce.mjs | 24 +++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/core/operations/TextEncodingBruteForce.mjs create mode 100644 test/tests/operations/TextEncodingBruteForce.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 4fac84c9..9db13647 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -49,6 +49,7 @@ "Change IP format", "Encode text", "Decode text", + "Text Encoding Brute Force", "Swap endianness", "To MessagePack", "From MessagePack", diff --git a/src/core/operations/TextEncodingBruteForce.mjs b/src/core/operations/TextEncodingBruteForce.mjs new file mode 100644 index 00000000..ef2d6500 --- /dev/null +++ b/src/core/operations/TextEncodingBruteForce.mjs @@ -0,0 +1,52 @@ +/** + * @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 = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const output = [], + charSets = Object.keys(IO_FORMAT); + + for (let i = 0; i < charSets.length; i++) { + let currentEncoding = Utils.printable(charSets[i] + ": ", false); + currentEncoding += cptable.utils.encode(IO_FORMAT[charSets[i]], input); + output.push(currentEncoding); + } + + return output.join("\n"); + } + +} + +export default TextEncodingBruteForce; diff --git a/test/index.mjs b/test/index.mjs index 9c11f6ae..89e09ea4 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -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"; diff --git a/test/tests/operations/TextEncodingBruteForce.mjs b/test/tests/operations/TextEncodingBruteForce.mjs new file mode 100644 index 00000000..5316eb16 --- /dev/null +++ b/test/tests/operations/TextEncodingBruteForce.mjs @@ -0,0 +1,24 @@ +/** + * 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", + input: "Булкі праз ляніва сабаку.", + expectedMatch: /Windows\-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./, + recipeConfig: [ + { + op: "Text Encoding Brute Force", + args: [], + }, + ], + } +]); + From 3f7059a2352fc397ec035233f6958877687ceae3 Mon Sep 17 00:00:00 2001 From: Cynser <42423063+Cynser@users.noreply.github.com> Date: Wed, 12 Dec 2018 17:49:11 +0000 Subject: [PATCH 2/4] Remove unnecessary escape character --- test/tests/operations/TextEncodingBruteForce.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests/operations/TextEncodingBruteForce.mjs b/test/tests/operations/TextEncodingBruteForce.mjs index 5316eb16..e79b24af 100644 --- a/test/tests/operations/TextEncodingBruteForce.mjs +++ b/test/tests/operations/TextEncodingBruteForce.mjs @@ -12,7 +12,7 @@ TestRegister.addTests([ { name: "Text Encoding Brute Force", input: "Булкі праз ляніва сабаку.", - expectedMatch: /Windows\-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./, + expectedMatch: /Windows-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./, recipeConfig: [ { op: "Text Encoding Brute Force", From dacb3ef6c3da374ef6aa721675a4b4a697d4fea0 Mon Sep 17 00:00:00 2001 From: Cynser <42423063+Cynser@users.noreply.github.com> Date: Mon, 17 Dec 2018 19:39:12 +0000 Subject: [PATCH 3/4] Added decode option --- .../operations/TextEncodingBruteForce.mjs | 27 +++++++++++++++---- .../operations/TextEncodingBruteForce.mjs | 15 +++++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/core/operations/TextEncodingBruteForce.mjs b/src/core/operations/TextEncodingBruteForce.mjs index ef2d6500..9c606eaa 100644 --- a/src/core/operations/TextEncodingBruteForce.mjs +++ b/src/core/operations/TextEncodingBruteForce.mjs @@ -26,7 +26,13 @@ class TextEncodingBruteForce extends Operation { this.infoURL = "https://wikipedia.org/wiki/Character_encoding"; this.inputType = "string"; this.outputType = "string"; - this.args = []; + this.args = [ + { + name: "Mode", + type: "option", + value: ["Encode", "Decode"] + } + ]; } /** @@ -36,12 +42,23 @@ class TextEncodingBruteForce extends Operation { */ run(input, args) { const output = [], - charSets = Object.keys(IO_FORMAT); + charSets = Object.keys(IO_FORMAT), + mode = args[0]; for (let i = 0; i < charSets.length; i++) { - let currentEncoding = Utils.printable(charSets[i] + ": ", false); - currentEncoding += cptable.utils.encode(IO_FORMAT[charSets[i]], input); - output.push(currentEncoding); + 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"); diff --git a/test/tests/operations/TextEncodingBruteForce.mjs b/test/tests/operations/TextEncodingBruteForce.mjs index e79b24af..3b16453d 100644 --- a/test/tests/operations/TextEncodingBruteForce.mjs +++ b/test/tests/operations/TextEncodingBruteForce.mjs @@ -10,13 +10,24 @@ import TestRegister from "../../TestRegister"; TestRegister.addTests([ { - name: "Text Encoding Brute Force", + name: "Text Encoding Brute Force - Encode", input: "Булкі праз ляніва сабаку.", expectedMatch: /Windows-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./, recipeConfig: [ { op: "Text Encoding Brute Force", - args: [], + args: ["Encode"], + }, + ], + }, + { + name: "Text Encoding Brute Force - Decode", + input: "Áóëê³ ïðàç ëÿí³âà ñàáàêó.", + expectedMatch: /Windows-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./, + recipeConfig: [ + { + op: "Text Encoding Brute Force", + args: ["Decode"], }, ], } From 076a1f97c29ab3857b5801bf1d1c0991a432dfc3 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Tue, 18 Dec 2018 13:50:10 +0000 Subject: [PATCH 4/4] Tidied up 'Text Encoding Brute Force' operations and updated CHANGELOG --- CHANGELOG.md | 6 +++ babel.config.js | 2 +- .../operations/TextEncodingBruteForce.mjs | 49 ++++++++++++++----- src/core/operations/ToBase62.mjs | 2 +- .../operations/TextEncodingBruteForce.mjs | 4 +- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15943e7e..6355831e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All major and minor version changes will be documented in this file. Details of patch-level version changes can be found in [commit messages](https://github.com/gchq/CyberChef/commits/master). +### [8.15.0] - 2018-12-18 +- 'Text Encoding Brute Force' operation added [@Cynser] | [#439] + ### [8.14.0] - 2018-12-18 - 'To Base62' and 'From Base62' operations added [@tcode2k16] | [#443] @@ -73,6 +76,7 @@ All major and minor version changes will be documented in this file. Details of +[8.15.0]: https://github.com/gchq/CyberChef/releases/tag/v8.15.0 [8.14.0]: https://github.com/gchq/CyberChef/releases/tag/v8.14.0 [8.13.0]: https://github.com/gchq/CyberChef/releases/tag/v8.13.0 [8.12.0]: https://github.com/gchq/CyberChef/releases/tag/v8.12.0 @@ -107,6 +111,7 @@ All major and minor version changes will be documented in this file. Details of [@bwhitn]: https://github.com/bwhitn [@jarmovanlenthe]: https://github.com/jarmovanlenthe [@tcode2k16]: https://github.com/tcode2k16 +[@Cynser]: https://github.com/Cynser [#95]: https://github.com/gchq/CyberChef/pull/299 [#173]: https://github.com/gchq/CyberChef/pull/173 @@ -130,5 +135,6 @@ All major and minor version changes will be documented in this file. Details of [#387]: https://github.com/gchq/CyberChef/pull/387 [#394]: https://github.com/gchq/CyberChef/pull/394 [#428]: https://github.com/gchq/CyberChef/pull/428 +[#439]: https://github.com/gchq/CyberChef/pull/439 [#441]: https://github.com/gchq/CyberChef/pull/441 [#443]: https://github.com/gchq/CyberChef/pull/443 diff --git a/babel.config.js b/babel.config.js index 2362c42a..5459f6c8 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,7 +1,7 @@ module.exports = function(api) { api.cache.forever(); - return { + return { "presets": [ ["@babel/preset-env", { "targets": { diff --git a/src/core/operations/TextEncodingBruteForce.mjs b/src/core/operations/TextEncodingBruteForce.mjs index 9c606eaa..3919dcd9 100644 --- a/src/core/operations/TextEncodingBruteForce.mjs +++ b/src/core/operations/TextEncodingBruteForce.mjs @@ -1,5 +1,6 @@ /** * @author Cynser + * @author n1474335 [n1474335@gmail.com] * @copyright Crown Copyright 2018 * @license Apache-2.0 */ @@ -22,10 +23,18 @@ class TextEncodingBruteForce extends Operation { this.name = "Text Encoding Brute Force"; this.module = "CharEnc"; - this.description = "Enumerate all possible text encodings for input."; + this.description = [ + "Enumerates all supported text encodings for the input, allowing you to quickly spot the correct one.", + "

", + "Supported charsets are:", + "" + ].join("\n"); this.infoURL = "https://wikipedia.org/wiki/Character_encoding"; this.inputType = "string"; - this.outputType = "string"; + this.outputType = "json"; + this.presentType = "html"; this.args = [ { name: "Mode", @@ -38,30 +47,44 @@ class TextEncodingBruteForce extends Operation { /** * @param {string} input * @param {Object[]} args - * @returns {string} + * @returns {json} */ run(input, args) { - const output = [], - charSets = Object.keys(IO_FORMAT), + const output = {}, + charsets = Object.keys(IO_FORMAT), mode = args[0]; - for (let i = 0; i < charSets.length; i++) { - let currentEncoding = charSets[i] + ": "; - + charsets.forEach(charset => { try { if (mode === "Decode") { - currentEncoding += cptable.utils.decode(IO_FORMAT[charSets[i]], input); + output[charset] = cptable.utils.decode(IO_FORMAT[charset], input); } else { - currentEncoding += cptable.utils.encode(IO_FORMAT[charSets[i]], input); + output[charset] = Utils.arrayBufferToStr(cptable.utils.encode(IO_FORMAT[charset], input)); } } catch (err) { - currentEncoding += "Could not decode."; + output[charset] = "Could not decode."; } + }); - output.push(Utils.printable(currentEncoding, true)); + return output; + } + + /** + * Displays the encodings in an HTML table for web apps. + * + * @param {Object[]} encodings + * @returns {html} + */ + present(encodings) { + let table = ""; + + for (const enc in encodings) { + const value = Utils.printable(encodings[enc], true); + table += ``; } - return output.join("\n"); + table += "
EncodingValue
${enc}${value}
"; + return table; } } diff --git a/src/core/operations/ToBase62.mjs b/src/core/operations/ToBase62.mjs index 3f615db2..51f89ecd 100644 --- a/src/core/operations/ToBase62.mjs +++ b/src/core/operations/ToBase62.mjs @@ -23,7 +23,7 @@ class ToBase62 extends Operation { this.name = "To Base62"; this.module = "Default"; this.description = "Base62 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers. The high number base results in shorter strings than with the decimal or hexadecimal system."; - this.infoURL = "https://en.wikipedia.org/wiki/List_of_numeral_systems"; + this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems"; this.inputType = "byteArray"; this.outputType = "string"; this.args = [ diff --git a/test/tests/operations/TextEncodingBruteForce.mjs b/test/tests/operations/TextEncodingBruteForce.mjs index 3b16453d..22e8f7c5 100644 --- a/test/tests/operations/TextEncodingBruteForce.mjs +++ b/test/tests/operations/TextEncodingBruteForce.mjs @@ -12,7 +12,7 @@ TestRegister.addTests([ { name: "Text Encoding Brute Force - Encode", input: "Булкі праз ляніва сабаку.", - expectedMatch: /Windows-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./, + expectedMatch: /Windows-1251 Cyrillic \(1251\).{1,10}Булкі праз ляніва сабаку\./, recipeConfig: [ { op: "Text Encoding Brute Force", @@ -23,7 +23,7 @@ TestRegister.addTests([ { name: "Text Encoding Brute Force - Decode", input: "Áóëê³ ïðàç ëÿí³âà ñàáàêó.", - expectedMatch: /Windows-1251 Cyrillic \(1251\): Булкі праз ляніва сабаку\./, + expectedMatch: /Windows-1251 Cyrillic \(1251\).{1,10}Булкі праз ляніва сабаку\./, recipeConfig: [ { op: "Text Encoding Brute Force",