Last active
December 17, 2022 15:00
-
-
Save mgeeky/43d7b5c5ee2338ff8580f0a2c2d30555 to your computer and use it in GitHub Desktop.
Binary blob to C-array simple converting script. Useful when embedding compiled binary shellcode within C program.
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/python | |
# Disasm of 64-bit binary: | |
# $ objdump -b binary -D -m i386:x86-64 <file> | |
# | |
# Usage: | |
# $ ./bin2shellcode.py <file> num | |
# Where: | |
# num - number of bytes to convert into array. | |
# `num` can be negative, resulting in `size-num` | |
# bytes be converted. | |
import sys | |
if __name__ == '__main__': | |
if len(sys.argv) < 2 or len(sys.argv) > 3: | |
print("Usage: %s <file> [len]" % sys.argv[0] ) | |
else: | |
f = open(sys.argv[1], 'rb') | |
bytes = f.read() | |
num = len(bytes) | |
if len(sys.argv) > 2: | |
# if [len] is negative - substract it from | |
# total length. | |
num0 = int(sys.argv[2]) | |
if num0 < 0 and -num0 <= num: | |
num += num0 -1 | |
elif -num0 > num: | |
print('[!] To large negative value. Fallback to 0.') | |
else: | |
num = num0 | |
array = 'char shellcode[%d] = \n\t"' % (num) | |
for b in range(len(bytes)): | |
if b > num: break | |
if b % 16 == 0 and b > 0: | |
array += '"\n\t"' | |
array += '\\x%02x' % bytes[b] | |
array += '";\n' | |
print(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment