Skip to content

Instantly share code, notes, and snippets.

@0x9090
Last active June 15, 2017 09:45
Show Gist options
  • Save 0x9090/92b6116206b87b0a8d01660f1e18aeb7 to your computer and use it in GitHub Desktop.
Save 0x9090/92b6116206b87b0a8d01660f1e18aeb7 to your computer and use it in GitHub Desktop.
Binary File to C String
#!/usr/bin/env python
import os, binascii
# Converts the target binary file to a C formatted string. Useful for embedding binary files in C source code
target = "C:\\Windows\\System32\\cmd.exe"
output_file = "C:\\file.txt"
bytes_per_line = 16
count = 0;
index = 0;
output = "unsigned char binary[] = {\n\t"
with open(target, "rb") as f:
hexdata = binascii.hexlify(f.read())
hexlist = map(''.join, zip(*[iter(hexdata)]*2))
for hex in hexlist:
if count >= bytes_per_line:
output += "\n\t"
count = 0;
output += "0x" + str(hexlist[index]).upper() + ","
count += 1;
index += 1;
output += "\n};\n"
out = open(output_file, "w")
out.write(output)
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment