Last active
June 15, 2017 09:45
-
-
Save 0x9090/92b6116206b87b0a8d01660f1e18aeb7 to your computer and use it in GitHub Desktop.
Binary File to C String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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