2018-05-15 17:30:17 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
2018-05-15 17:30:17 +02:00
|
|
|
import XRegExp from "xregexp";
|
2019-07-09 13:23:59 +02:00
|
|
|
import { search } from "../lib/Extract.mjs";
|
2022-04-14 19:08:16 +02:00
|
|
|
import { caseInsensitiveSort } from "../lib/Sort.mjs";
|
2018-05-16 11:17:49 +02:00
|
|
|
|
2018-05-15 17:30:17 +02:00
|
|
|
/**
|
|
|
|
* Strings operation
|
|
|
|
*/
|
|
|
|
class Strings extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strings constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Strings";
|
|
|
|
this.module = "Regex";
|
|
|
|
this.description = "Extracts all strings from the input.";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Strings_(Unix)";
|
2018-05-15 17:30:17 +02:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
2022-04-14 19:08:16 +02:00
|
|
|
name: "Encoding",
|
|
|
|
type: "option",
|
|
|
|
value: ["Single byte", "16-bit littleendian", "16-bit bigendian", "All"]
|
2018-05-15 17:30:17 +02:00
|
|
|
},
|
|
|
|
{
|
2022-04-14 19:08:16 +02:00
|
|
|
name: "Minimum length",
|
|
|
|
type: "number",
|
|
|
|
value: 4
|
2018-05-15 17:30:17 +02:00
|
|
|
},
|
|
|
|
{
|
2022-04-14 19:08:16 +02:00
|
|
|
name: "Match",
|
|
|
|
type: "option",
|
|
|
|
value: [
|
2018-05-15 17:30:17 +02:00
|
|
|
"[ASCII]", "Alphanumeric + punctuation (A)", "All printable chars (A)", "Null-terminated strings (A)",
|
|
|
|
"[Unicode]", "Alphanumeric + punctuation (U)", "All printable chars (U)", "Null-terminated strings (U)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2022-04-14 19:08:16 +02:00
|
|
|
name: "Display total",
|
|
|
|
type: "boolean",
|
|
|
|
value: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Sort",
|
|
|
|
type: "boolean",
|
|
|
|
value: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Unique",
|
|
|
|
type: "boolean",
|
|
|
|
value: false
|
2018-05-15 17:30:17 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
2022-04-14 19:08:16 +02:00
|
|
|
const [encoding, minLen, matchType, displayTotal, sort, unique] = args,
|
2018-05-15 17:30:17 +02:00
|
|
|
alphanumeric = "A-Z\\d",
|
|
|
|
punctuation = "/\\-:.,_$%'\"()<>= !\\[\\]{}@",
|
|
|
|
printable = "\x20-\x7e",
|
|
|
|
uniAlphanumeric = "\\pL\\pN",
|
|
|
|
uniPunctuation = "\\pP\\pZ",
|
|
|
|
uniPrintable = "\\pL\\pM\\pZ\\pS\\pN\\pP";
|
|
|
|
|
|
|
|
let strings = "";
|
|
|
|
|
|
|
|
switch (matchType) {
|
|
|
|
case "Alphanumeric + punctuation (A)":
|
|
|
|
strings = `[${alphanumeric + punctuation}]`;
|
|
|
|
break;
|
|
|
|
case "All printable chars (A)":
|
|
|
|
case "Null-terminated strings (A)":
|
|
|
|
strings = `[${printable}]`;
|
|
|
|
break;
|
|
|
|
case "Alphanumeric + punctuation (U)":
|
|
|
|
strings = `[${uniAlphanumeric + uniPunctuation}]`;
|
|
|
|
break;
|
|
|
|
case "All printable chars (U)":
|
|
|
|
case "Null-terminated strings (U)":
|
|
|
|
strings = `[${uniPrintable}]`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UTF-16 support is hacked in by allowing null bytes on either side of the matched chars
|
|
|
|
switch (encoding) {
|
|
|
|
case "All":
|
|
|
|
strings = `(\x00?${strings}\x00?)`;
|
|
|
|
break;
|
|
|
|
case "16-bit littleendian":
|
|
|
|
strings = `(${strings}\x00)`;
|
|
|
|
break;
|
|
|
|
case "16-bit bigendian":
|
|
|
|
strings = `(\x00${strings})`;
|
|
|
|
break;
|
|
|
|
case "Single byte":
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
strings = `${strings}{${minLen},}`;
|
|
|
|
|
|
|
|
if (matchType.includes("Null-terminated")) {
|
|
|
|
strings += "\x00";
|
|
|
|
}
|
|
|
|
|
|
|
|
const regex = new XRegExp(strings, "ig");
|
2022-04-14 19:08:16 +02:00
|
|
|
const results = search(
|
|
|
|
input,
|
|
|
|
regex,
|
|
|
|
null,
|
|
|
|
sort ? caseInsensitiveSort : null,
|
|
|
|
unique
|
|
|
|
);
|
2018-05-15 17:30:17 +02:00
|
|
|
|
2022-04-14 19:08:16 +02:00
|
|
|
if (displayTotal) {
|
|
|
|
return `Total found: ${results.length}\n\n${results.join("\n")}`;
|
|
|
|
} else {
|
|
|
|
return results.join("\n");
|
|
|
|
}
|
2018-05-15 17:30:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Strings;
|