mirror of
https://github.com/gchq/CyberChef
synced 2024-12-27 04:53:16 +00:00
Added operation: "XPath expression"
This commit is contained in:
parent
09d515cbae
commit
51b627ab29
6 changed files with 6464 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ build/dev
|
||||||
docs/*
|
docs/*
|
||||||
!docs/*.conf.json
|
!docs/*.conf.json
|
||||||
!docs/*.ico
|
!docs/*.ico
|
||||||
|
.idea/*
|
|
@ -123,6 +123,7 @@ module.exports = function(grunt) {
|
||||||
"src/js/lib/vkbeautify.js",
|
"src/js/lib/vkbeautify.js",
|
||||||
"src/js/lib/Sortable.js",
|
"src/js/lib/Sortable.js",
|
||||||
"src/js/lib/bootstrap-colorpicker.js",
|
"src/js/lib/bootstrap-colorpicker.js",
|
||||||
|
'src/js/lib/jquery.xpath.js',
|
||||||
|
|
||||||
// Custom libraries
|
// Custom libraries
|
||||||
"src/js/lib/canvas_components.js",
|
"src/js/lib/canvas_components.js",
|
||||||
|
|
|
@ -186,6 +186,7 @@ var Categories = [
|
||||||
"Extract file paths",
|
"Extract file paths",
|
||||||
"Extract dates",
|
"Extract dates",
|
||||||
"Regular expression",
|
"Regular expression",
|
||||||
|
"XPath expression",
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -1893,6 +1893,24 @@ var OperationConfig = {
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"XPath expression": {
|
||||||
|
description: "Extract information from an xml document with an XPath query",
|
||||||
|
run: Extract.run_xpath,
|
||||||
|
input_type: "string",
|
||||||
|
output_type: "string",
|
||||||
|
args: [
|
||||||
|
{
|
||||||
|
name: "XPath",
|
||||||
|
type: "string",
|
||||||
|
value: Extract.XPATH_INITIAL
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Result delimiter",
|
||||||
|
type: "binary_short_string",
|
||||||
|
value: Extract.XPATH_DELIMITER
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"From UNIX Timestamp": {
|
"From UNIX Timestamp": {
|
||||||
description: "Converts a UNIX timestamp to a datetime string.<br><br>e.g. <code>978346800</code> becomes <code>Mon 1 January 2001 11:00:00 UTC</code>",
|
description: "Converts a UNIX timestamp to a datetime string.<br><br>e.g. <code>978346800</code> becomes <code>Mon 1 January 2001 11:00:00 UTC</code>",
|
||||||
run: DateTime.run_from_unix_timestamp,
|
run: DateTime.run_from_unix_timestamp,
|
||||||
|
|
6380
src/js/lib/jquery.xpath.js
Normal file
6380
src/js/lib/jquery.xpath.js
Normal file
File diff suppressed because it is too large
Load diff
|
@ -10,7 +10,7 @@
|
||||||
var Extract = {
|
var Extract = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs search operations across the input data using refular expressions.
|
* Runs search operations across the input data using regular expressions.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {string} input
|
* @param {string} input
|
||||||
|
@ -293,5 +293,66 @@ var Extract = {
|
||||||
output += Extract.run_dates(input, []);
|
output += Extract.run_dates(input, []);
|
||||||
return output;
|
return output;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constant
|
||||||
|
* @default
|
||||||
|
*/
|
||||||
|
XPATH_INITIAL: "",
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constant
|
||||||
|
* @default
|
||||||
|
*/
|
||||||
|
XPATH_DELIMITER: "\\n",
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract information (from an xml document) with an XPath query
|
||||||
|
*
|
||||||
|
* @param {string} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
run_xpath:function(input, args) {
|
||||||
|
var query = args[0];
|
||||||
|
var delimiter = args[1];
|
||||||
|
|
||||||
|
try {
|
||||||
|
var xml = $.parseXML(input);
|
||||||
|
} catch (err) {
|
||||||
|
return "Invalid input XML.";
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
var result = $.xpath(xml, query);
|
||||||
|
} catch (err) {
|
||||||
|
return "Invalid XPath. Details:\n" + err.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
var serializer = new XMLSerializer();
|
||||||
|
var output = "";
|
||||||
|
for (var i = 0; i < result.length; i++) {
|
||||||
|
if (i > 0) output += delimiter;
|
||||||
|
|
||||||
|
switch (result[i].nodeType) {
|
||||||
|
case Node.ELEMENT_NODE:
|
||||||
|
output += serializer.serializeToString(result[i]);
|
||||||
|
break;
|
||||||
|
case Node.ATTRIBUTE_NODE:
|
||||||
|
output += result[i].value;
|
||||||
|
break;
|
||||||
|
case Node.TEXT_NODE:
|
||||||
|
output += result[i].wholeText;
|
||||||
|
break;
|
||||||
|
case Node.COMMENT_NODE:
|
||||||
|
output += result[i].data;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error("Unknown Node Type: " + result[i].nodeType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue