Skip to content

Commit

Permalink
Magic operation now calculates the entropy of each option and display…
Browse files Browse the repository at this point in the history
…s tooltips explaining the properties.
  • Loading branch information
n1474335 committed Feb 19, 2018
1 parent 559741f commit 56d33ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
30 changes: 22 additions & 8 deletions src/core/FlowControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ const FlowControl = {
<th>Properties</th>
</tr>`;

/**
* Returns a CSS colour value based on an integer input.
*
* @param {number} val
* @returns {string}
*/
function chooseColour(val) {
if (val < 3) return "green";
if (val < 5) return "goldenrod";
return "red";
}

options.forEach(option => {
// Construct recipe URL
// Replace this Magic op with the generated recipe
Expand All @@ -295,36 +307,38 @@ const FlowControl = {
fileType = "",
matchingOps = "",
useful = "",
validUTF8 = option.isUTF8 ? "Valid UTF8\n" : "";
entropy = `<span data-toggle="tooltip" data-container="body" title="Shannon Entropy is measured from 0 to 8. High entropy suggests encrypted or compressed data. Normal text is usually around 3.5 to 5.">Entropy: <span style="color: ${chooseColour(option.entropy)}">${option.entropy.toFixed(2)}</span></span>`,
validUTF8 = option.isUTF8 ? "<span data-toggle='tooltip' data-container='body' title='The data could be a valid UTF8 string based on its encoding.'>Valid UTF8</span>\n" : "";

if (option.languageScores[0].probability > 0) {
let likelyLangs = option.languageScores.filter(l => l.probability > 0);
if (likelyLangs.length < 1) likelyLangs = [option.languageScores[0]];
language = "Possible languages:\n " + likelyLangs.map(lang => {
return Magic.codeToLanguage(lang.lang);
}).join("\n ") + "\n";
language = "<span data-toggle='tooltip' data-container='body' title='Based on a statistical comparison of the frequency of bytes in various languages. Ordered by likelihood.'>" +
"Possible languages:\n " + likelyLangs.map(lang => {
return Magic.codeToLanguage(lang.lang);
}).join("\n ") + "</span>\n";
}

if (option.fileType) {
fileType = `File type: ${option.fileType.mime} (${option.fileType.ext})\n`;
fileType = `<span data-toggle="tooltip" data-container="body" title="Based on the presence of magic bytes.">File type: ${option.fileType.mime} (${option.fileType.ext})</span>\n`;
}

if (option.matchingOps.length) {
matchingOps = `Matching ops: ${[...new Set(option.matchingOps.map(op => op.op))].join(", ")}\n`;
}

if (option.useful) {
useful = "Useful op detected\n";
useful = "<span data-toggle='tooltip' data-container='body' title='This could be an operation that displays data in a useful way, such as rendering an image.'>Useful op detected</span>\n";
}

output += `<tr>
<td><a href="#${recipeURL}">${Utils.generatePrettyRecipe(option.recipe, true)}</a></td>
<td>${Utils.escapeHtml(Utils.printable(Utils.truncate(option.data, 99)))}</td>
<td>${language}${fileType}${matchingOps}${useful}${validUTF8}</td>
<td>${language}${fileType}${matchingOps}${useful}${validUTF8}${entropy}</td>
</tr>`;
});

output += "</table>";
output += "</table><script type='application/javascript'>$('[data-toggle=\"tooltip\"]').tooltip()</script>";

if (!options.length) {
output = "Nothing of interest could be detected about the input data.\nHave you tried modifying the operation arguments?";
Expand Down
30 changes: 28 additions & 2 deletions src/core/lib/Magic.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 56d33ea

Please sign in to comment.