Browse Source

Merge branch 'Ge0rg3-steganography'

n1474335 5 years ago
parent
commit
5bebd71a44

+ 1 - 1
src/core/Dish.mjs

@@ -177,7 +177,7 @@ class Dish {
         this.type = type;
 
         if (!this.valid()) {
-            const sample = Utils.truncate(JSON.stringify(this.value), 13);
+            const sample = Utils.truncate(JSON.stringify(this.value), 25);
             throw new DishError(`Data is not a valid ${Dish.enumLookup(type)}: ${sample}`);
         }
     }

+ 5 - 1
src/core/config/Categories.json

@@ -369,7 +369,11 @@
             "Scan for Embedded Files",
             "Extract Files",
             "Remove EXIF",
-            "Extract EXIF"
+            "Extract EXIF",
+            "Extract RGBA",
+            "View Bit Plane",
+            "Randomize Colour Palette",
+            "Extract LSB"
         ]
     },
     {

+ 4 - 1
src/core/lib/Base64.mjs

@@ -12,7 +12,7 @@ import Utils from "../Utils.mjs";
 /**
  * Base64's the input byte array using the given alphabet, returning a string.
  *
- * @param {byteArray|Uint8Array|string} data
+ * @param {byteArray|Uint8Array|ArrayBuffer|string} data
  * @param {string} [alphabet="A-Za-z0-9+/="]
  * @returns {string}
  *
@@ -25,6 +25,9 @@ import Utils from "../Utils.mjs";
  */
 export function toBase64(data, alphabet="A-Za-z0-9+/=") {
     if (!data) return "";
+    if (data instanceof ArrayBuffer) {
+        data = new Uint8Array(data);
+    }
     if (typeof data == "string") {
         data = Utils.strToByteArray(data);
     }

+ 9 - 0
src/core/lib/Delim.mjs

@@ -72,3 +72,12 @@ export const JOIN_DELIM_OPTIONS = [
     {name: "Nothing (join chars)", value: ""}
 ];
 
+/**
+ * RGBA list delimiters.
+ */
+export const RGBA_DELIM_OPTIONS = [
+    {name: "Comma", value: ","},
+    {name: "Space", value: " "},
+    {name: "CRLF", value: "\\r\\n"},
+    {name: "Line Feed", value: "\n"}
+];

+ 7 - 3
src/core/lib/FileType.mjs

@@ -75,7 +75,7 @@ function bytesMatch(sig, buf, offset=0) {
  * Given a buffer, detects magic byte sequences at specific positions and returns the
  * extension and mime type.
  *
- * @param {Uint8Array} buf
+ * @param {Uint8Array|ArrayBuffer} buf
  * @param {string[]} [categories=All] - Which categories of file to look for
  * @returns {Object[]} types
  * @returns {string} type.name - Name of file type
@@ -84,6 +84,10 @@ function bytesMatch(sig, buf, offset=0) {
  * @returns {string} [type.desc] - Description
  */
 export function detectFileType(buf, categories=Object.keys(FILE_SIGNATURES)) {
+    if (buf instanceof ArrayBuffer) {
+        buf = new Uint8Array(buf);
+    }
+
     if (!(buf && buf.length > 1)) {
         return [];
     }
@@ -203,7 +207,7 @@ function locatePotentialSig(buf, sig, offset) {
  * Detects whether the given buffer is a file of the type specified.
  *
  * @param {string|RegExp} type
- * @param {Uint8Array} buf
+ * @param {Uint8Array|ArrayBuffer} buf
  * @returns {string|false} The mime type or false if the type does not match
  */
 export function isType(type, buf) {
@@ -230,7 +234,7 @@ export function isType(type, buf) {
 /**
  * Detects whether the given buffer contains an image file.
  *
- * @param {Uint8Array} buf
+ * @param {Uint8Array|ArrayBuffer} buf
  * @returns {string|false} The mime type or false if the type does not match
  */
 export function isImage(buf) {

+ 1 - 1
src/core/operations/AddTextToImage.mjs

@@ -121,7 +121,7 @@ class AddTextToImage extends Operation {
         let xPos = args[3],
             yPos = args[4];
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/BlurImage.mjs

@@ -53,7 +53,7 @@ class BlurImage extends Operation {
     async run(input, args) {
         const [blurAmount, blurType] = args;
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/ContainImage.mjs

@@ -107,7 +107,7 @@ class ContainImage extends Operation {
             "Bottom": jimp.VERTICAL_ALIGN_BOTTOM
         };
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/ConvertImageFormat.mjs

@@ -93,7 +93,7 @@ class ConvertImageFormat extends Operation {
 
         const mime = formatMap[format];
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file format.");
         }
         let image;

+ 1 - 1
src/core/operations/CoverImage.mjs

@@ -102,7 +102,7 @@ class CoverImage extends Operation {
             "Bottom": jimp.VERTICAL_ALIGN_BOTTOM
         };
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/CropImage.mjs

@@ -93,7 +93,7 @@ class CropImage extends Operation {
      */
     async run(input, args) {
         const [xPos, yPos, width, height, autocrop, autoTolerance, autoFrames, autoSymmetric, autoBorder] = args;
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/DitherImage.mjs

@@ -38,7 +38,7 @@ class DitherImage extends Operation {
      * @returns {byteArray}
      */
     async run(input, args) {
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 114 - 0
src/core/operations/ExtractLSB.mjs

@@ -0,0 +1,114 @@
+/**
+ * @author Ge0rg3 [georgeomnet+cyberchef@gmail.com]
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation.mjs";
+import OperationError from "../errors/OperationError.mjs";
+import Utils from "../Utils.mjs";
+import { fromBinary } from "../lib/Binary.mjs";
+import { isImage } from "../lib/FileType.mjs";
+import jimp from "jimp";
+
+/**
+ * Extract LSB operation
+ */
+class ExtractLSB extends Operation {
+
+    /**
+     * ExtractLSB constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Extract LSB";
+        this.module = "Image";
+        this.description = "Extracts the Least Significant Bit data from each pixel in an image. This is a common way to hide data in Steganography.";
+        this.infoURL = "https://wikipedia.org/wiki/Bit_numbering#Least_significant_bit_in_digital_steganography";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "byteArray";
+        this.args = [
+            {
+                name: "Colour Pattern #1",
+                type: "option",
+                value: COLOUR_OPTIONS,
+            },
+            {
+                name: "Colour Pattern #2",
+                type: "option",
+                value: ["", ...COLOUR_OPTIONS],
+            },
+            {
+                name: "Colour Pattern #3",
+                type: "option",
+                value: ["", ...COLOUR_OPTIONS],
+            },
+            {
+                name: "Colour Pattern #4",
+                type: "option",
+                value: ["", ...COLOUR_OPTIONS],
+            },
+            {
+                name: "Pixel Order",
+                type: "option",
+                value: ["Row", "Column"],
+            },
+            {
+                name: "Bit",
+                type: "number",
+                value: 0
+            }
+        ];
+    }
+
+    /**
+     * @param {ArrayBuffer} input
+     * @param {Object[]} args
+     * @returns {byteArray}
+     */
+    async run(input, args) {
+        if (!isImage(input)) throw new OperationError("Please enter a valid image file.");
+
+        const bit = 7 - args.pop(),
+            pixelOrder = args.pop(),
+            colours = args.filter(option => option !== "").map(option => COLOUR_OPTIONS.indexOf(option)),
+            parsedImage = await jimp.read(input),
+            width = parsedImage.bitmap.width,
+            height = parsedImage.bitmap.height,
+            rgba = parsedImage.bitmap.data;
+
+        if (bit < 0 || bit > 7) {
+            throw new OperationError("Error: Bit argument must be between 0 and 7");
+        }
+
+        let i, combinedBinary = "";
+
+        if (pixelOrder === "Row") {
+            for (i = 0; i < rgba.length; i += 4) {
+                for (const colour of colours) {
+                    combinedBinary += Utils.bin(rgba[i + colour])[bit];
+                }
+            }
+        } else {
+            let rowWidth;
+            const pixelWidth = width * 4;
+            for (let col = 0; col < width; col++) {
+                for (let row = 0; row < height; row++) {
+                    rowWidth = row * pixelWidth;
+                    for (const colour of colours) {
+                        i = rowWidth + (col + colour * 4);
+                        combinedBinary += Utils.bin(rgba[i])[bit];
+                    }
+                }
+            }
+        }
+
+        return fromBinary(combinedBinary);
+    }
+
+}
+
+const COLOUR_OPTIONS = ["R", "G", "B", "A"];
+
+export default ExtractLSB;

+ 65 - 0
src/core/operations/ExtractRGBA.mjs

@@ -0,0 +1,65 @@
+/**
+ * @author Ge0rg3 [georgeomnet+cyberchef@gmail.com]
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation.mjs";
+import OperationError from "../errors/OperationError.mjs";
+import { isImage } from "../lib/FileType.mjs";
+import jimp from "jimp";
+
+import {RGBA_DELIM_OPTIONS} from "../lib/Delim.mjs";
+
+/**
+ * Extract RGBA operation
+ */
+class ExtractRGBA extends Operation {
+
+    /**
+     * ExtractRGBA constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Extract RGBA";
+        this.module = "Image";
+        this.description = "Extracts each pixel's RGBA value in an image. These are sometimes used in Steganography to hide text or data.";
+        this.infoURL = "https://wikipedia.org/wiki/RGBA_color_space";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "string";
+        this.args = [
+            {
+                name: "Delimiter",
+                type: "editableOption",
+                value: RGBA_DELIM_OPTIONS
+            },
+            {
+                name: "Include Alpha",
+                type: "boolean",
+                value: true
+            }
+        ];
+    }
+
+    /**
+     * @param {ArrayBuffer} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    async run(input, args) {
+        if (!isImage(input)) throw new OperationError("Please enter a valid image file.");
+
+        const delimiter = args[0],
+            includeAlpha = args[1],
+            parsedImage = await jimp.read(input);
+
+        let bitmap = parsedImage.bitmap.data;
+        bitmap = includeAlpha ? bitmap : bitmap.filter((val, idx) => idx % 4 !== 3);
+
+        return bitmap.join(delimiter);
+    }
+
+}
+
+export default ExtractRGBA;

+ 1 - 1
src/core/operations/FlipImage.mjs

@@ -45,7 +45,7 @@ class FlipImage extends Operation {
      */
     async run(input, args) {
         const [flipAxis] = args;
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid input file type.");
         }
 

+ 1 - 1
src/core/operations/ImageBrightnessContrast.mjs

@@ -54,7 +54,7 @@ class ImageBrightnessContrast extends Operation {
      */
     async run(input, args) {
         const [brightness, contrast] = args;
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/ImageFilter.mjs

@@ -48,7 +48,7 @@ class ImageFilter extends Operation {
      */
     async run(input, args) {
         const [filterType] = args;
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/ImageHueSaturationLightness.mjs

@@ -62,7 +62,7 @@ class ImageHueSaturationLightness extends Operation {
     async run(input, args) {
         const [hue, saturation, lightness] = args;
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/ImageOpacity.mjs

@@ -47,7 +47,7 @@ class ImageOpacity extends Operation {
      */
     async run(input, args) {
         const [opacity] = args;
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/InvertImage.mjs

@@ -38,7 +38,7 @@ class InvertImage extends Operation {
      * @returns {byteArray}
      */
     async run(input, args) {
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid input file format.");
         }
 

+ 1 - 1
src/core/operations/NormaliseImage.mjs

@@ -37,7 +37,7 @@ class NormaliseImage extends Operation {
      * @returns {byteArray}
      */
     async run(input, args) {
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/ParseQRCode.mjs

@@ -51,7 +51,7 @@ class ParseQRCode extends Operation {
     async run(input, args) {
         const [normalise] = args;
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
         return await parseQrCode(input, normalise);

+ 84 - 0
src/core/operations/RandomizeColourPalette.mjs

@@ -0,0 +1,84 @@
+/**
+ * @author Ge0rg3 [georgeomnet+cyberchef@gmail.com]
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation.mjs";
+import OperationError from "../errors/OperationError.mjs";
+import Utils from "../Utils.mjs";
+import { isImage } from "../lib/FileType.mjs";
+import { runHash } from "../lib/Hash.mjs";
+import { toBase64 } from "../lib/Base64.mjs";
+import jimp from "jimp";
+import { toHex } from "../lib/Hex.mjs";
+
+/**
+ * Randomize Colour Palette operation
+ */
+class RandomizeColourPalette extends Operation {
+
+    /**
+     * RandomizeColourPalette constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Randomize Colour Palette";
+        this.module = "Image";
+        this.description = "Randomizes each colour in an image's colour palette. This can often reveal text or symbols that were previously a very similar colour to their surroundings, a technique sometimes used in Steganography.";
+        this.infoURL = "https://wikipedia.org/wiki/Indexed_color";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
+        this.presentType = "html";
+        this.args = [
+            {
+                name: "Seed",
+                type: "string",
+                value: ""
+            }
+        ];
+    }
+
+    /**
+     * @param {ArrayBuffer} input
+     * @param {Object[]} args
+     * @returns {ArrayBuffer}
+     */
+    async run(input, args) {
+        if (!isImage(input)) throw new OperationError("Please enter a valid image file.");
+
+        const seed = args[0] || (Math.random().toString().substr(2)),
+            parsedImage = await jimp.read(input),
+            width = parsedImage.bitmap.width,
+            height = parsedImage.bitmap.height;
+
+        let rgbString, rgbHash, rgbHex;
+
+        parsedImage.scan(0, 0, width, height, function(x, y, idx) {
+            rgbString = this.bitmap.data.slice(idx, idx+3).join(".");
+            rgbHash = runHash("md5", Utils.strToArrayBuffer(seed + rgbString));
+            rgbHex = rgbHash.substr(0, 6) + "ff";
+            parsedImage.setPixelColor(parseInt(rgbHex, 16), x, y);
+        });
+
+        const imageBuffer = await parsedImage.getBufferAsync(jimp.AUTO);
+
+        return new Uint8Array(imageBuffer).buffer;
+    }
+
+    /**
+     * Displays the extracted data as an image for web apps.
+     * @param {ArrayBuffer} data
+     * @returns {html}
+     */
+    present(data) {
+        if (!data.byteLength) return "";
+        const type = isImage(data);
+
+        return `<img src="data:${type};base64,${toBase64(data)}">`;
+    }
+
+}
+
+export default RandomizeColourPalette;

+ 1 - 1
src/core/operations/ResizeImage.mjs

@@ -87,7 +87,7 @@ class ResizeImage extends Operation {
             "Bezier": jimp.RESIZE_BEZIER
         };
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/RotateImage.mjs

@@ -46,7 +46,7 @@ class RotateImage extends Operation {
     async run(input, args) {
         const [degrees] = args;
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/SharpenImage.mjs

@@ -62,7 +62,7 @@ class SharpenImage extends Operation {
     async run(input, args) {
         const [radius, amount, threshold] = args;
 
-        if (!isImage(new Uint8Array(input))) {
+        if (!isImage(input)) {
             throw new OperationError("Invalid file type.");
         }
 

+ 1 - 1
src/core/operations/ToBase64.mjs

@@ -40,7 +40,7 @@ class ToBase64 extends Operation {
      */
     run(input, args) {
         const alphabet = args[0];
-        return toBase64(new Uint8Array(input), alphabet);
+        return toBase64(input, alphabet);
     }
 
     /**

+ 107 - 0
src/core/operations/ViewBitPlane.mjs

@@ -0,0 +1,107 @@
+/**
+ * @author Ge0rg3 [georgeomnet+cyberchef@gmail.com]
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation.mjs";
+import OperationError from "../errors/OperationError.mjs";
+import Utils from "../Utils.mjs";
+import { isImage } from "../lib/FileType.mjs";
+import { toBase64 } from "../lib/Base64.mjs";
+import jimp from "jimp";
+
+/**
+ * View Bit Plane operation
+ */
+class ViewBitPlane extends Operation {
+
+    /**
+     * ViewBitPlane constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "View Bit Plane";
+        this.module = "Image";
+        this.description = "Extracts and displays a bit plane of any given image. These show only a single bit from each pixel, and can be used to hide messages in Steganography.";
+        this.infoURL = "https://wikipedia.org/wiki/Bit_plane";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
+        this.presentType = "html";
+        this.args = [
+            {
+                name: "Colour",
+                type: "option",
+                value: COLOUR_OPTIONS
+            },
+            {
+                name: "Bit",
+                type: "number",
+                value: 0
+            }
+        ];
+    }
+
+    /**
+     * @param {ArrayBuffer} input
+     * @param {Object[]} args
+     * @returns {ArrayBuffer}
+     */
+    async run(input, args) {
+        if (!isImage(input)) throw new OperationError("Please enter a valid image file.");
+
+        const [colour, bit] = args,
+            parsedImage = await jimp.read(input),
+            width = parsedImage.bitmap.width,
+            height = parsedImage.bitmap.height,
+            colourIndex = COLOUR_OPTIONS.indexOf(colour),
+            bitIndex = 7-bit;
+
+        if (bit < 0 || bit > 7) {
+            throw new OperationError("Error: Bit argument must be between 0 and 7");
+        }
+
+        let pixel, bin, newPixelValue;
+
+        parsedImage.scan(0, 0, width, height, function(x, y, idx) {
+            pixel = this.bitmap.data[idx + colourIndex];
+            bin = Utils.bin(pixel);
+            newPixelValue = 255;
+
+            if (bin.charAt(bitIndex) === "1") newPixelValue = 0;
+
+            for (let i=0; i < 3; i++) {
+                this.bitmap.data[idx + i] = newPixelValue;
+            }
+            this.bitmap.data[idx + 3] = 255;
+
+        });
+
+        const imageBuffer = await parsedImage.getBufferAsync(jimp.AUTO);
+
+        return new Uint8Array(imageBuffer).buffer;
+    }
+
+    /**
+     * Displays the extracted data as an image for web apps.
+     * @param {ArrayBuffer} data
+     * @returns {html}
+     */
+    present(data) {
+        if (!data.length) return "";
+        const type = isImage(data);
+
+        return `<img src="data:${type};base64,${toBase64(data)}">`;
+    }
+
+}
+
+const COLOUR_OPTIONS = [
+    "Red",
+    "Green",
+    "Blue",
+    "Alpha"
+];
+
+export default ViewBitPlane;

+ 0 - 1
tests/operations/index.mjs

@@ -110,4 +110,3 @@ const logOpsTestReport = logTestReport.bind(null, testStatus);
 
 TestRegister.runTests()
     .then(logOpsTestReport);
-

File diff suppressed because it is too large
+ 4 - 1
tests/operations/tests/Image.mjs


Some files were not shown because too many files changed in this diff