Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@labelbox/magic-bytes.js
Advanced tools
Magic Bytes is a javascript library analyzing the first bytes of a file to tell you its type. The procedure is based on https://en.wikipedia.org/wiki/List_of_file_signatures.
Run npm install magic-bytes.js
On server:
import filetype from 'magic-bytes.js'
filetype(fs.readFileSync("myimage.png")) // ["png"]
Using HTML:
<input type="file" id="file" />
<script src="node_modules/magic-bytes.js/dist/browser.js" type="application/javascript"></script>
<script>
document.getElementById("file").addEventListener('change', (event, x) => {
const fileReader = new FileReader();
fileReader.onloadend = (f) => {
const bytes = new Uint8Array(f.target.result);
console.log("Possible filetypes: " + filetypeinfo(bytes))
}
fileReader.readAsArrayBuffer(event.target.files[0])
})
</script>
The following functions are availble:
filetypeinfo(bytes: number[])
Contains typeinformation like name, extension and mime type: [{typename: "zip"}, {typename: "jar"}]
filetypenames(bytes: number[])
: Contains type names only: ["zip", "jar"]
filetypemime(bytes: number[])
: Contains type mime types only: ["application/zip", "application/jar"]
filetypeextensions(bytes: number[])
: Contains type extensions only: ["zip", "jar"]
Both function return an empty array []
otherwise, which means it could not detect the file signature. Keep in mind that
txt files for example fall in this category.
You don't have to load the whole file in memory. For validating a file uploaded to S3 using Lambda for example, it may be
enough to load the files first 100 bytes and validate against them. This is especially useful for big files.
see examples for practical usage.
Run npm test
See examples/
The create-snapshot.js
creates a new tree. The tree has a similar shape to the following
{
"0x47": {
"0x49": {
"0x46": {
"0x38": {
"0x37": {
"0x61": {
"matches": [
{
"typename": "gif",
"mime": "image/gif",
"extension": "gif"
}
]
}
},
}
}
}
}
}
It acts as a giant lookup map for the given byte signatures. To check all available entries, have a look at pattern-tree.js
and its
generated pattern-tree.snapshot
, which acts as a static resource.
Please refer to src/pattern-tree.js
FAQs
Detect Filetype by bytes
We found that @labelbox/magic-bytes.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 39 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.