qjerome<p>🚀 Porting libmagic to Rust: Safer File Identification 🚀</p><p>A little while ago (maybe a month or two), I started porting a great project to Rust: libmagic, the library behind the `file` command utility.</p><p>🤔 Why did I do that?<br>For years, I've faced the same issue with file identification: embedding a C library that does intensive parsing into my memory-safe code (Rust, Go, Python, etc.). While I trust the file/libmagic developer community's code quality, I know there are skilled people capable of finding and exploiting bugs—especially in C/C++ parsers. So I've always been reluctant to run libmagic on untrusted input, which creates a tricky situation that kind of defeats its original purpose: categorizing files you know nothing about.</p><p>🦀 Why Rust?<br>Rust is the perfect fit for this port. It provides strong safety guarantees while maintaining high performance and easy bindings to other languages (C/C++, Python, Go, Elixir, etc.). Bonus: the library and tool will be portable to all Rust-supported targets.</p><p>💻 Show me the code!<br>Here it is: <a href="https://github.com/qjerome/magic-rs/pull/1" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">github.com/qjerome/magic-rs/pu</span><span class="invisible">ll/1</span></a><br>⚠️ Note: This isn't a full implementation yet—it's still a work in progress and needs more work for a usable, clean version (see PR description). That said, if you're feeling adventurous, you can test it out. It already successfully identifies several file types: MS-DOS executables, ELF binaries, scripts, and more.</p><p>🔄 Compatibility with C libmagic file format?<br>This project aims for 99% compatibility with libmagic's rule file format. A few rules (such as those using ternary notations in messages) may need adjustments, but the goal is to stay true to the existing specification.</p><p>📌 What's next?<br>- Finalize the library implementation and publish a Rust crate<br>- Complete the CLI tool (a `file`-equivalent implementation)<br>- Create bindings for other languages</p><p>If you're interested in this work, don't hesitate to follow along or reach out!</p><p>Special thanks to <span class="h-card" translate="no"><a href="https://infosec.exchange/@adulau" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>adulau</span></a></span> and <span class="h-card" translate="no"><a href="https://social.circl.lu/@circl" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>circl</span></a></span> for supporting this work! 🙏</p><p><a href="https://infosec.exchange/tags/Rust" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Rust</span></a> <a href="https://infosec.exchange/tags/Libmagic" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Libmagic</span></a> <a href="https://infosec.exchange/tags/Security" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Security</span></a> <a href="https://infosec.exchange/tags/OpenSource" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>OpenSource</span></a> <a href="https://infosec.exchange/tags/Programming" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Programming</span></a> <a href="https://infosec.exchange/tags/SoftwareDevelopment" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>SoftwareDevelopment</span></a></p>