ExtractFiles.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2018
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. // import OperationError from "../errors/OperationError";
  8. import Utils from "../Utils";
  9. import {scanForFileTypes, extractFile} from "../lib/FileType";
  10. import {FILE_SIGNATURES} from "../lib/FileSignatures";
  11. /**
  12. * Extract Files operation
  13. */
  14. class ExtractFiles extends Operation {
  15. /**
  16. * ExtractFiles constructor
  17. */
  18. constructor() {
  19. super();
  20. this.name = "Extract Files";
  21. this.module = "Default";
  22. this.description = "TODO";
  23. this.infoURL = "https://forensicswiki.org/wiki/File_Carving";
  24. this.inputType = "ArrayBuffer";
  25. this.outputType = "List<File>";
  26. this.presentType = "html";
  27. this.args = Object.keys(FILE_SIGNATURES).map(cat => {
  28. return {
  29. name: cat,
  30. type: "boolean",
  31. value: cat === "Miscellaneous" ? false : true
  32. };
  33. });
  34. }
  35. /**
  36. * @param {ArrayBuffer} input
  37. * @param {Object[]} args
  38. * @returns {List<File>}
  39. */
  40. run(input, args) {
  41. const bytes = new Uint8Array(input),
  42. categories = [];
  43. args.forEach((cat, i) => {
  44. if (cat) categories.push(Object.keys(FILE_SIGNATURES)[i]);
  45. });
  46. // Scan for embedded files
  47. const detectedFiles = scanForFileTypes(bytes, categories);
  48. // Extract each file that we support
  49. const files = [];
  50. detectedFiles.forEach(detectedFile => {
  51. try {
  52. files.push(extractFile(bytes, detectedFile.fileDetails, detectedFile.offset));
  53. } catch (err) {
  54. if (err.message.indexOf("No extraction algorithm available") < 0)
  55. throw err;
  56. }
  57. });
  58. return files;
  59. }
  60. /**
  61. * Displays the files in HTML for web apps.
  62. *
  63. * @param {File[]} files
  64. * @returns {html}
  65. */
  66. async present(files) {
  67. return await Utils.displayFilesAsHTML(files);
  68. }
  69. }
  70. export default ExtractFiles;