TL;DR
To prevent a file from running (make it non-executable), convert its binary data into plain text (ASCII). This removes the executable flags. We’ll use command-line tools like xxd and sed to achieve this, then reverse the process if needed.
How to Make a File Non-Executable
- Backup your file: Always create a backup before modifying any file.
- Convert Binary to Hexadecimal: Use
xxdto convert the binary file into a hexadecimal representation.xxd my_executable > my_executable.hexThis creates a new file,
my_executable.hex, containing the hexadecimal data of your original file. - Remove Hexadecimal Formatting: The output from
xxdincludes offsets and other information we don’t need. Usesedto extract only the hexadecimal bytes.sed 's/^[0-9a-fA-F ]*//g' my_executable.hex > my_executable.txtThis command removes all lines starting with numbers and spaces, leaving just the hex data in
my_executable.txt. - Convert Hexadecimal to ASCII: Use
xxd -rto convert the hexadecimal representation back into a text file.xxd -r my_executable.txt > my_executable.asciiThis creates
my_executable.ascii, which now contains the original file’s data as ASCII characters. Because binary data isn’t usually valid ASCII, this will likely result in many unprintable characters. - Verify Non-Executability: Attempt to execute the new file.
./my_executable.asciiYou should receive a “Permission denied” or similar error message indicating that the file is not executable.
How to Restore the File (ASCII back to Binary)
- Convert ASCII to Hexadecimal: Use
xxdto convert the ASCII file into hexadecimal representation.xxd my_executable.ascii > my_executable.hex2 - Remove Formatting (again): Remove the offset information from the hex file.
sed 's/^[0-9a-fA-F ]*//g' my_executable.hex2 > my_executable.txt2 - Convert Hexadecimal back to Binary: Use
xxd -rto convert the hexadecimal data back into binary.xxd -r my_executable.txt2 > restored_executableThis creates a new file,
restored_executable, which should be identical to your originalmy_executable. - Restore Executable Permissions: Make the restored file executable.
chmod +x restored_executable - Verify Restoration: Attempt to execute the restored file.
./restored_executableIt should now run as expected.
Important Considerations
- File Size: Converting binary to ASCII will slightly increase the file size because each byte is represented by two hexadecimal characters.
- Character Encoding: This method assumes a basic character encoding. If your original file contains specific encoding requirements, you may need to adjust the conversion process accordingly.
- cyber security: While this prevents *direct* execution, it doesn’t protect against all cyber security threats. A determined attacker could still attempt to analyze and potentially exploit the ASCII representation of the data.