ScanForEmbeddedFiles.mjs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import Utils from "../Utils";
  8. import {scanForFileTypes} from "../lib/FileType";
  9. import {FILE_SIGNATURES} from "../lib/FileSignatures";
  10. /**
  11. * Scan for Embedded Files operation
  12. */
  13. class ScanForEmbeddedFiles extends Operation {
  14. /**
  15. * ScanForEmbeddedFiles constructor
  16. */
  17. constructor() {
  18. super();
  19. this.name = "Scan for Embedded Files";
  20. this.module = "Default";
  21. this.description = "Scans the data for potential embedded files by looking for magic bytes at all offsets. This operation is prone to false positives.<br><br>WARNING: Files over about 100KB in size will take a VERY long time to process.";
  22. this.infoURL = "https://wikipedia.org/wiki/List_of_file_signatures";
  23. this.inputType = "ArrayBuffer";
  24. this.outputType = "string";
  25. this.args = Object.keys(FILE_SIGNATURES).map(cat => {
  26. return {
  27. name: cat,
  28. type: "boolean",
  29. value: cat === "Miscellaneous" ? false : true
  30. };
  31. });
  32. }
  33. /**
  34. * @param {ArrayBuffer} input
  35. * @param {Object[]} args
  36. * @returns {string}
  37. */
  38. run(input, args) {
  39. let output = "Scanning data for 'magic bytes' which may indicate embedded files. The following results may be false positives and should not be treat as reliable. Any suffiently long file is likely to contain these magic bytes coincidentally.\n",
  40. numFound = 0;
  41. const categories = [],
  42. data = new Uint8Array(input);
  43. args.forEach((cat, i) => {
  44. if (cat) categories.push(Object.keys(FILE_SIGNATURES)[i]);
  45. });
  46. const types = scanForFileTypes(data, categories);
  47. if (types.length) {
  48. types.forEach(type => {
  49. numFound++;
  50. output += "\nOffset " + type.offset + " (0x" + Utils.hex(type.offset) + "):\n" +
  51. " File extension: " + type.fileDetails.extension + "\n" +
  52. " MIME type: " + type.fileDetails.mime + "\n";
  53. if (type.fileDetails.description && type.fileDetails.description.length) {
  54. output += " Description: " + type.fileDetails.description + "\n";
  55. }
  56. });
  57. }
  58. if (numFound === 0) {
  59. output += "\nNo embedded files were found.";
  60. }
  61. return output;
  62. }
  63. }
  64. export default ScanForEmbeddedFiles;