Skip to content

Instantly share code, notes, and snippets.

@mgeeky
Created March 22, 2016 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgeeky/bc6291acd597acdda971 to your computer and use it in GitHub Desktop.
Save mgeeky/bc6291acd597acdda971 to your computer and use it in GitHub Desktop.
Shellcode 2 binary script that reads C-formatted array from clipboard and converts it to binary output written to desired file.
#!/usr/bin/python
# This script converts shellcode's C-char array with hex
# values to ASCII-HEX binary file with shellcode inserted in it.
# ASCII-HEX values can be read either from file (if specified
# in first param) or from Win32 clipboard (if on win32 platform)
import sys
import random
import struct
if sys.platform == 'win32':
import ctypes
data = ""
if len(sys.argv) == 2:
try:
f = open(sys.argv[1])
data = f.read()
f.close()
except:
print "[!] You must specify file name with shellcode data!"
exit(1)
else:
# file not specified, try to read win32 clipboard
try:
if sys.platform == 'win32':
user = ctypes.windll.LoadLibrary("user32.dll")
if user.OpenClipboard(0):
handle = user.GetClipboardData( 1) # CF_TEXT
if handle:
kernel = ctypes.windll.kernel32
addr = kernel.GlobalLock( handle)
data = ctypes.c_char_p(addr).value
print "[+] Data read from clipboard. Len:",\
len(data)
kernel.GlobalUnlock( addr)
else:
print "[!] GetClipboardData() failed:", \
kernel.GetLastError()
exit(1)
else:
print "[!] OpenClipboard() failed:", \
kernel.GetLastError()
exit(1)
else:
print "[?] You must specify as a first param file name"\
" with shellcode"
exit(1)
except:
print "[!] Clipboard reading FAILED !"
exit(1)
data = data.replace(" ", "")
data = data.replace('"', "")
data = data.replace(";", "")
data = data.replace(r"\x", "")
data = data.replace("\x0a", "")
data = data.replace("\x0d", "")
data = ''.join(data)
try:
g = open("shellcode.bin", 'wb')
except:
r = random.randint(1,1000)
g = open("shellcode%d.bin" % r, 'wb')
if not g:
print "[!] Cannot create file to write into!"
exit(1)
for i in range(0, len(data), 2):
a = struct.pack("B", int(data[i:i+2],16))
g.write(a)
g.close()
print "[+] Shellcode file created succesfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment