2018-01-03 16:51:10 +00:00
|
|
|
import XRegExp from "xregexp";
|
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Identifier extraction operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const Extract = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-29 19:28:07 +01:00
|
|
|
* Runs search operations across the input data using regular expressions.
|
2016-11-28 10:42:58 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} input
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {RegExp} searchRegex
|
|
|
|
* @param {RegExp} removeRegex - A regular expression defining results to remove from the
|
2016-11-28 10:42:58 +00:00
|
|
|
* final list
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {boolean} includeTotal - Whether or not to include the total number of results
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
_search: function(input, searchRegex, removeRegex, includeTotal) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let output = "",
|
2016-11-28 10:42:58 +00:00
|
|
|
total = 0,
|
|
|
|
match;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
while ((match = searchRegex.exec(input))) {
|
2018-01-10 19:44:25 +00:00
|
|
|
// Moves pointer when an empty string is matched (prevents infinite loop)
|
|
|
|
if (match.index === searchRegex.lastIndex) {
|
|
|
|
searchRegex.lastIndex++;
|
|
|
|
}
|
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (removeRegex && removeRegex.test(match[0]))
|
2016-11-28 10:42:58 +00:00
|
|
|
continue;
|
|
|
|
total++;
|
|
|
|
output += match[0] + "\n";
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (includeTotal)
|
2016-11-28 10:42:58 +00:00
|
|
|
output = "Total found: " + total + "\n\n" + output;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
2018-01-12 23:09:27 +00:00
|
|
|
MIN_STRING_LEN: 4,
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
2018-01-12 23:09:27 +00:00
|
|
|
STRING_MATCH_TYPE: [
|
|
|
|
"[ASCII]", "Alphanumeric + punctuation (A)", "All printable chars (A)", "Null-terminated strings (A)",
|
|
|
|
"[Unicode]", "Alphanumeric + punctuation (U)", "All printable chars (U)", "Null-terminated strings (U)"
|
|
|
|
],
|
2018-01-03 16:51:10 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
2018-01-12 23:09:27 +00:00
|
|
|
ENCODING_LIST: ["Single byte", "16-bit littleendian", "16-bit bigendian", "All"],
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
DISPLAY_TOTAL: false,
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Strings operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runStrings: function(input, args) {
|
2018-01-12 23:09:27 +00:00
|
|
|
const encoding = args[0],
|
|
|
|
minLen = args[1],
|
|
|
|
matchType = args[2],
|
|
|
|
displayTotal = args[3],
|
|
|
|
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;
|
|
|
|
}
|
2018-01-03 16:51:10 +00:00
|
|
|
|
2018-01-12 23:09:27 +00:00
|
|
|
// UTF-16 support is hacked in by allowing null bytes on either side of the matched chars
|
2018-01-03 16:51:10 +00:00
|
|
|
switch (encoding) {
|
|
|
|
case "All":
|
2018-01-12 23:09:27 +00:00
|
|
|
strings = `(\x00?${strings}\x00?)`;
|
2018-01-03 16:51:10 +00:00
|
|
|
break;
|
|
|
|
case "16-bit littleendian":
|
2018-01-12 23:09:27 +00:00
|
|
|
strings = `(${strings}\x00)`;
|
2018-01-03 16:51:10 +00:00
|
|
|
break;
|
|
|
|
case "16-bit bigendian":
|
2018-01-12 23:09:27 +00:00
|
|
|
strings = `(\x00${strings})`;
|
2018-01-03 16:51:10 +00:00
|
|
|
break;
|
|
|
|
case "Single byte":
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:09:27 +00:00
|
|
|
strings = `${strings}{${minLen},}`;
|
|
|
|
|
|
|
|
if (matchType.includes("Null-terminated")) {
|
|
|
|
strings += "\x00";
|
|
|
|
}
|
|
|
|
|
|
|
|
const regex = new XRegExp(strings, "ig");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INCLUDE_IPV4: true,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INCLUDE_IPV6: false,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
REMOVE_LOCAL: false,
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Extract IP addresses operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runIp: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let includeIpv4 = args[0],
|
2017-01-31 18:24:56 +00:00
|
|
|
includeIpv6 = args[1],
|
|
|
|
removeLocal = args[2],
|
|
|
|
displayTotal = args[3],
|
2016-11-28 10:42:58 +00:00
|
|
|
ipv4 = "(?:(?:\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d|\\d)(?:\\/\\d{1,2})?",
|
|
|
|
ipv6 = "((?=.*::)(?!.*::.+::)(::)?([\\dA-F]{1,4}:(:|\\b)|){5}|([\\dA-F]{1,4}:){6})((([\\dA-F]{1,4}((?!\\3)::|:\\b|(?![\\dA-F])))|(?!\\2\\3)){2}|(((2[0-4]|1\\d|[1-9])?\\d|25[0-5])\\.?\\b){4})",
|
|
|
|
ips = "";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (includeIpv4 && includeIpv6) {
|
2016-11-28 10:42:58 +00:00
|
|
|
ips = ipv4 + "|" + ipv6;
|
2017-01-31 18:24:56 +00:00
|
|
|
} else if (includeIpv4) {
|
2016-11-28 10:42:58 +00:00
|
|
|
ips = ipv4;
|
2017-01-31 18:24:56 +00:00
|
|
|
} else if (includeIpv6) {
|
2016-11-28 10:42:58 +00:00
|
|
|
ips = ipv6;
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (ips) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const regex = new RegExp(ips, "ig");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (removeLocal) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let ten = "10\\..+",
|
2016-11-28 10:42:58 +00:00
|
|
|
oneninetwo = "192\\.168\\..+",
|
|
|
|
oneseventwo = "172\\.(?:1[6-9]|2\\d|3[01])\\..+",
|
|
|
|
onetwoseven = "127\\..+",
|
2017-01-31 18:24:56 +00:00
|
|
|
removeRegex = new RegExp("^(?:" + ten + "|" + oneninetwo +
|
2016-11-28 10:42:58 +00:00
|
|
|
"|" + oneseventwo + "|" + onetwoseven + ")");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, removeRegex, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
} else {
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Extract email addresses operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runEmail: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let displayTotal = args[0],
|
2016-11-28 10:42:58 +00:00
|
|
|
regex = /\w[-.\w]*@[-\w]+(?:\.[-\w]+)*\.[A-Z]{2,4}/ig;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Extract MAC addresses operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runMac: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let displayTotal = args[0],
|
2016-11-28 10:42:58 +00:00
|
|
|
regex = /[A-F\d]{2}(?:[:-][A-F\d]{2}){5}/ig;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Extract URLs operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runUrls: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let displayTotal = args[0],
|
2016-11-28 10:42:58 +00:00
|
|
|
protocol = "[A-Z]+://",
|
|
|
|
hostname = "[-\\w]+(?:\\.\\w[-\\w]*)+",
|
|
|
|
port = ":\\d+",
|
2017-08-15 16:44:45 +00:00
|
|
|
path = "/[^.!,?\"<>\\[\\]{}\\s\\x7F-\\xFF]*";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-08-15 16:44:45 +00:00
|
|
|
path += "(?:[.!,?]+[^.!,?\"<>\\[\\]{}\\s\\x7F-\\xFF]+)*";
|
2017-04-13 18:08:50 +01:00
|
|
|
const regex = new RegExp(protocol + hostname + "(?:" + port +
|
2016-11-28 10:42:58 +00:00
|
|
|
")?(?:" + path + ")?", "ig");
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Extract domains operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runDomains: function(input, args) {
|
2017-09-06 16:43:30 +00:00
|
|
|
const displayTotal = args[0],
|
|
|
|
regex = /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/ig;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INCLUDE_WIN_PATH: true,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INCLUDE_UNIX_PATH: true,
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Extract file paths operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runFilePaths: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let includeWinPath = args[0],
|
2017-01-31 18:24:56 +00:00
|
|
|
includeUnixPath = args[1],
|
|
|
|
displayTotal = args[2],
|
|
|
|
winDrive = "[A-Z]:\\\\",
|
2018-01-24 15:50:05 +00:00
|
|
|
winName = "[A-Z\\d][A-Z\\d\\- '_\\(\\)~]{0,61}",
|
2017-01-31 18:24:56 +00:00
|
|
|
winExt = "[A-Z\\d]{1,6}",
|
|
|
|
winPath = winDrive + "(?:" + winName + "\\\\?)*" + winName +
|
|
|
|
"(?:\\." + winExt + ")?",
|
|
|
|
unixPath = "(?:/[A-Z\\d.][A-Z\\d\\-.]{0,61})+",
|
|
|
|
filePaths = "";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (includeWinPath && includeUnixPath) {
|
|
|
|
filePaths = winPath + "|" + unixPath;
|
|
|
|
} else if (includeWinPath) {
|
|
|
|
filePaths = winPath;
|
|
|
|
} else if (includeUnixPath) {
|
|
|
|
filePaths = unixPath;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (filePaths) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const regex = new RegExp(filePaths, "ig");
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Extract dates operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runDates: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let displayTotal = args[0],
|
2016-11-28 10:42:58 +00:00
|
|
|
date1 = "(?:19|20)\\d\\d[- /.](?:0[1-9]|1[012])[- /.](?:0[1-9]|[12][0-9]|3[01])", // yyyy-mm-dd
|
|
|
|
date2 = "(?:0[1-9]|[12][0-9]|3[01])[- /.](?:0[1-9]|1[012])[- /.](?:19|20)\\d\\d", // dd/mm/yyyy
|
|
|
|
date3 = "(?:0[1-9]|1[012])[- /.](?:0[1-9]|[12][0-9]|3[01])[- /.](?:19|20)\\d\\d", // mm/dd/yyyy
|
|
|
|
regex = new RegExp(date1 + "|" + date2 + "|" + date3, "ig");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return Extract._search(input, regex, null, displayTotal);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default Extract;
|