Blog | G5 Cyber Security

Make Files Non-Executable: Binary to ASCII

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

  1. Backup your file: Always create a backup before modifying any file.
  2. Convert Binary to Hexadecimal: Use xxd to convert the binary file into a hexadecimal representation.
    xxd my_executable > my_executable.hex

    This creates a new file, my_executable.hex, containing the hexadecimal data of your original file.

  3. Remove Hexadecimal Formatting: The output from xxd includes offsets and other information we don’t need. Use sed to extract only the hexadecimal bytes.
    sed 's/^[0-9a-fA-F ]*//g' my_executable.hex > my_executable.txt

    This command removes all lines starting with numbers and spaces, leaving just the hex data in my_executable.txt.

  4. Convert Hexadecimal to ASCII: Use xxd -r to convert the hexadecimal representation back into a text file.
    xxd -r my_executable.txt > my_executable.ascii

    This 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.

  5. Verify Non-Executability: Attempt to execute the new file.
    ./my_executable.ascii

    You 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)

  1. Convert ASCII to Hexadecimal: Use xxd to convert the ASCII file into hexadecimal representation.
    xxd my_executable.ascii > my_executable.hex2
  2. Remove Formatting (again): Remove the offset information from the hex file.
    sed 's/^[0-9a-fA-F ]*//g' my_executable.hex2 > my_executable.txt2
  3. Convert Hexadecimal back to Binary: Use xxd -r to convert the hexadecimal data back into binary.
    xxd -r my_executable.txt2 > restored_executable

    This creates a new file, restored_executable, which should be identical to your original my_executable.

  4. Restore Executable Permissions: Make the restored file executable.
    chmod +x restored_executable
  5. Verify Restoration: Attempt to execute the restored file.
    ./restored_executable

    It should now run as expected.

Important Considerations

Exit mobile version